cpu.h revision 1.105 1 /* $NetBSD: cpu.h,v 1.105 2003/10/09 13:46:43 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 1990 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * William Jolitz.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)cpu.h 5.4 (Berkeley) 5/9/91
35 */
36
37 #ifndef _I386_CPU_H_
38 #define _I386_CPU_H_
39
40 #if defined(_KERNEL_OPT)
41 #include "opt_multiprocessor.h"
42 #endif
43
44 /*
45 * Definitions unique to i386 cpu support.
46 */
47 #include <machine/frame.h>
48 #include <machine/segments.h>
49 #include <machine/tss.h>
50 #include <machine/intrdefs.h>
51 #include <x86/cacheinfo.h>
52
53 #include <sys/device.h>
54 #include <sys/lock.h> /* will also get LOCKDEBUG */
55 #include <sys/sched.h>
56
57 #include <lib/libkern/libkern.h> /* offsetof */
58
59 struct intrsource;
60
61 /*
62 * a bunch of this belongs in cpuvar.h; move it later..
63 */
64
65 struct cpu_info {
66 struct device *ci_dev; /* pointer to our device */
67 struct cpu_info *ci_self; /* self-pointer */
68 void *ci_tlog_base; /* Trap log base */
69 int32_t ci_tlog_offset; /* Trap log current offset */
70 struct schedstate_percpu ci_schedstate; /* scheduler state */
71 struct cpu_info *ci_next; /* next cpu */
72
73 /*
74 * Public members.
75 */
76 struct lwp *ci_curlwp; /* current owner of the processor */
77 struct simplelock ci_slock; /* lock on this data structure */
78 cpuid_t ci_cpuid; /* our CPU ID */
79 u_int ci_apicid; /* our APIC ID */
80 u_long ci_spin_locks; /* # of spin locks held */
81 u_long ci_simple_locks; /* # of simple locks held */
82
83 /*
84 * Private members.
85 */
86 struct lwp *ci_fpcurlwp; /* current owner of the FPU */
87 int ci_fpsaving; /* save in progress */
88
89 volatile u_int32_t ci_tlb_ipi_mask;
90
91 struct pcb *ci_curpcb; /* VA of current HW PCB */
92 struct pcb *ci_idle_pcb; /* VA of current PCB */
93 int ci_idle_tss_sel; /* TSS selector of idle PCB */
94
95 struct intrsource *ci_isources[MAX_INTR_SOURCES];
96 u_int32_t ci_ipending;
97 int ci_ilevel;
98 int ci_idepth;
99 u_int32_t ci_imask[NIPL];
100 u_int32_t ci_iunmask[NIPL];
101
102 paddr_t ci_idle_pcb_paddr; /* PA of idle PCB */
103 u_int32_t ci_flags; /* flags; see below */
104 u_int32_t ci_ipis; /* interprocessor interrupts pending */
105 int sc_apic_version; /* local APIC version */
106
107 int32_t ci_cpuid_level;
108 u_int32_t ci_signature; /* X86 cpuid type */
109 u_int32_t ci_feature_flags;/* X86 CPUID feature bits */
110 u_int32_t ci_cpu_class; /* CPU class */
111 u_int32_t ci_brand_id; /* Intel brand id */
112 u_int32_t ci_vendor[4]; /* vendor string */
113 u_int32_t ci_cpu_serial[3]; /* PIII serial number */
114 u_int64_t ci_tsc_freq; /* cpu cycles/second */
115
116 struct cpu_functions *ci_func; /* start/stop functions */
117 void (*cpu_setup) __P((struct cpu_info *));
118 /* proc-dependant init */
119 void (*ci_info) __P((struct cpu_info *));
120
121 int ci_want_resched;
122 int ci_astpending;
123 struct trapframe *ci_ddb_regs;
124
125 u_int ci_cflush_lsize; /* CFLUSH insn line size */
126 struct x86_cache_info ci_cinfo[CAI_COUNT];
127
128 /*
129 * Variables used by cc_microtime().
130 */
131 struct timeval ci_cc_time;
132 int64_t ci_cc_cc;
133 int64_t ci_cc_ms_delta;
134 int64_t ci_cc_denom;
135
136 union descriptor *ci_gdt;
137
138 struct i386tss ci_doubleflt_tss;
139 struct i386tss ci_ddbipi_tss;
140
141 char *ci_doubleflt_stack;
142 char *ci_ddbipi_stack;
143
144 struct evcnt ci_ipi_events[X86_NIPI];
145 };
146
147 /*
148 * Processor flag notes: The "primary" CPU has certain MI-defined
149 * roles (mostly relating to hardclock handling); we distinguish
150 * betwen the processor which booted us, and the processor currently
151 * holding the "primary" role just to give us the flexibility later to
152 * change primaries should we be sufficiently twisted.
153 */
154
155 #define CPUF_BSP 0x0001 /* CPU is the original BSP */
156 #define CPUF_AP 0x0002 /* CPU is an AP */
157 #define CPUF_SP 0x0004 /* CPU is only processor */
158 #define CPUF_PRIMARY 0x0008 /* CPU is active primary processor */
159
160 #define CPUF_APIC_CD 0x0010 /* CPU has apic configured */
161
162 #define CPUF_PRESENT 0x1000 /* CPU is present */
163 #define CPUF_RUNNING 0x2000 /* CPU is running */
164 #define CPUF_PAUSE 0x4000 /* CPU is paused in DDB */
165 #define CPUF_GO 0x8000 /* CPU should start running */
166
167 /*
168 * We statically allocate the CPU info for the primary CPU (or,
169 * the only CPU on uniprocessors), and the primary CPU is the
170 * first CPU on the CPU info list.
171 */
172 extern struct cpu_info cpu_info_primary;
173 extern struct cpu_info *cpu_info_list;
174
175 #define CPU_INFO_ITERATOR int
176 #define CPU_INFO_FOREACH(cii, ci) cii = 0, ci = cpu_info_list; \
177 ci != NULL; ci = ci->ci_next
178
179 #if defined(MULTIPROCESSOR)
180
181 #define X86_MAXPROCS 32 /* because we use a bitmask */
182
183 #define CPU_STARTUP(_ci) ((_ci)->ci_func->start(_ci))
184 #define CPU_STOP(_ci) ((_ci)->ci_func->stop(_ci))
185 #define CPU_START_CLEANUP(_ci) ((_ci)->ci_func->cleanup(_ci))
186
187 static struct cpu_info *curcpu(void);
188
189 __inline static struct cpu_info * __attribute__((__unused__))
190 curcpu()
191 {
192 struct cpu_info *ci;
193
194 __asm __volatile("movl %%fs:%1, %0" :
195 "=r" (ci) :
196 "m"
197 (*(struct cpuinfo * const *)offsetof(struct cpu_info, ci_self)));
198 return ci;
199 }
200
201 #define cpu_number() (curcpu()->ci_cpuid)
202
203 #define CPU_IS_PRIMARY(ci) ((ci)->ci_flags & CPUF_PRIMARY)
204
205 #if 0
206 #define x86_ipisend(ci) (((ci) != curcpu()) ? x86_send_ipi((ci),0) : 0)
207 #else
208 #define x86_ipisend(ci) 0
209 #endif
210
211 #define aston(p) ((p)->p_md.md_astpending = 1)
212
213 extern struct cpu_info *cpu_info[X86_MAXPROCS];
214
215 void cpu_boot_secondary_processors __P((void));
216 void cpu_init_idle_pcbs __P((void));
217
218 /*
219 * Preempt the current process if in interrupt from user mode,
220 * or after the current trap/syscall if in system mode.
221 */
222 extern void need_resched __P((struct cpu_info *));
223
224 #else /* !MULTIPROCESSOR */
225
226 #define X86_MAXPROCS 1
227
228 #ifdef _KERNEL
229
230 #define curcpu() (&cpu_info_primary)
231
232 #endif
233
234 /*
235 * definitions of cpu-dependent requirements
236 * referenced in generic code
237 */
238 #define cpu_number() 0
239 #define CPU_IS_PRIMARY(ci) 1
240
241 /*
242 * Preempt the current process if in interrupt from user mode,
243 * or after the current trap/syscall if in system mode.
244 */
245 #define need_resched(ci) \
246 do { \
247 struct cpu_info *__ci = (ci); \
248 __ci->ci_want_resched = 1; \
249 if (__ci->ci_curlwp != NULL) \
250 aston(__ci->ci_curlwp->l_proc); \
251 } while (/*CONSTCOND*/0)
252
253 #define aston(p) ((p)->p_md.md_astpending = 1)
254
255 #endif /* MULTIPROCESSOR */
256
257 extern u_int32_t cpus_attached;
258
259 #define curpcb curcpu()->ci_curpcb
260 #define curlwp curcpu()->ci_curlwp
261
262 /*
263 * Arguments to hardclock, softclock and statclock
264 * encapsulate the previous machine state in an opaque
265 * clockframe; for now, use generic intrframe.
266 *
267 * Note: Since spllowersoftclock() does not actually unmask the currently
268 * running (hardclock) interrupt, CLKF_BASEPRI() *must* always be 0; otherwise
269 * we could stall hardclock ticks if another interrupt takes too long.
270 */
271 #define clockframe intrframe
272
273 #define CLKF_USERMODE(frame) USERMODE((frame)->if_cs, (frame)->if_eflags)
274 #define CLKF_BASEPRI(frame) (0)
275 #define CLKF_PC(frame) ((frame)->if_eip)
276 #define CLKF_INTR(frame) (curcpu()->ci_idepth > 1)
277
278 /*
279 * This is used during profiling to integrate system time. It can safely
280 * assume that the process is resident.
281 */
282 #define LWP_PC(l) ((l)->l_md.md_regs->tf_eip)
283
284 /*
285 * Give a profiling tick to the current process when the user profiling
286 * buffer pages are invalid. On the i386, request an ast to send us
287 * through trap(), marking the proc as needing a profiling tick.
288 */
289 #define need_proftick(p) ((p)->p_flag |= P_OWEUPC, aston(p))
290
291 /*
292 * Notify the current process (p) that it has a signal pending,
293 * process as soon as possible.
294 */
295 #define signotify(p) aston(p)
296
297 /*
298 * We need a machine-independent name for this.
299 */
300 extern void (*delay_func) __P((int));
301 struct timeval;
302 extern void (*microtime_func) __P((struct timeval *));
303
304 #define DELAY(x) (*delay_func)(x)
305 #define delay(x) (*delay_func)(x)
306 #define microtime(tv) (*microtime_func)(tv)
307
308 /*
309 * pull in #defines for kinds of processors
310 */
311 #include <machine/cputypes.h>
312
313 struct cpu_nocpuid_nameclass {
314 int cpu_vendor;
315 const char *cpu_vendorname;
316 const char *cpu_name;
317 int cpu_class;
318 void (*cpu_setup) __P((struct cpu_info *));
319 void (*cpu_cacheinfo) __P((struct cpu_info *));
320 void (*cpu_info) __P((struct cpu_info *));
321 };
322
323
324 struct cpu_cpuid_nameclass {
325 const char *cpu_id;
326 int cpu_vendor;
327 const char *cpu_vendorname;
328 struct cpu_cpuid_family {
329 int cpu_class;
330 const char *cpu_models[CPU_MAXMODEL+2];
331 void (*cpu_setup) __P((struct cpu_info *));
332 void (*cpu_probe) __P((struct cpu_info *));
333 void (*cpu_info) __P((struct cpu_info *));
334 } cpu_family[CPU_MAXFAMILY - CPU_MINFAMILY + 1];
335 };
336
337 #ifdef _KERNEL
338 extern int biosbasemem;
339 extern int biosextmem;
340 extern unsigned int cpu_feature;
341 extern int cpu;
342 extern int cpu_class;
343 extern const struct cpu_nocpuid_nameclass i386_nocpuid_cpus[];
344 extern const struct cpu_cpuid_nameclass i386_cpuid_cpus[];
345
346 extern int i386_use_fxsave;
347 extern int i386_has_sse;
348 extern int i386_has_sse2;
349
350 /* machdep.c */
351 void dumpconf __P((void));
352 int cpu_maxproc __P((void));
353 void cpu_reset __P((void));
354 void i386_init_pcb_tss_ldt __P((struct cpu_info *));
355 void i386_proc0_tss_ldt_init __P((void));
356 void i386_bufinit __P((void));
357
358 /* identcpu.c */
359 extern int tmx86_has_longrun;
360 extern u_int crusoe_longrun;
361 extern u_int crusoe_frequency;
362 extern u_int crusoe_voltage;
363 extern u_int crusoe_percentage;
364 extern u_int tmx86_set_longrun_mode(u_int);
365 void tmx86_get_longrun_status_all(void);
366 u_int tmx86_get_longrun_mode(void);
367 void identifycpu __P((struct cpu_info *));
368
369 /* vm_machdep.c */
370 void cpu_proc_fork __P((struct proc *, struct proc *));
371
372 /* locore.s */
373 struct region_descriptor;
374 void lgdt __P((struct region_descriptor *));
375 void fillw __P((short, void *, size_t));
376
377 struct pcb;
378 void savectx __P((struct pcb *));
379 void switch_exit __P((struct lwp *, void (*)(struct lwp *)));
380 void proc_trampoline __P((void));
381
382 /* clock.c */
383 void initrtclock __P((void));
384 void startrtclock __P((void));
385 void i8254_delay __P((int));
386 void i8254_microtime __P((struct timeval *));
387 void i8254_initclocks __P((void));
388
389 /* kern_microtime.c */
390
391 extern struct timeval cc_microset_time;
392 void cc_microtime __P((struct timeval *));
393 void cc_microset __P((struct cpu_info *));
394
395 /* cpu.c */
396
397 void cpu_probe_features __P((struct cpu_info *));
398
399 /* npx.c */
400 void npxsave_lwp __P((struct lwp *, int));
401 void npxsave_cpu __P((struct cpu_info *, int));
402
403 /* vm_machdep.c */
404 int kvtop __P((caddr_t));
405
406 #if !defined(_LKM)
407 #include "opt_math_emulate.h"
408 #endif
409 #ifdef MATH_EMULATE
410 /* math_emulate.c */
411 int math_emulate __P((struct trapframe *, ksiginfo_t *));
412 #endif
413
414 #if !defined(_LKM)
415 #include "opt_user_ldt.h"
416 #endif
417 #ifdef USER_LDT
418 /* sys_machdep.h */
419 int i386_get_ldt __P((struct lwp *, void *, register_t *));
420 int i386_set_ldt __P((struct lwp *, void *, register_t *));
421 #endif
422
423 /* isa_machdep.c */
424 void isa_defaultirq __P((void));
425 int isa_nmi __P((void));
426
427 #if !defined(_LKM)
428 #include "opt_vm86.h"
429 #endif
430 #ifdef VM86
431 /* vm86.c */
432 void vm86_gpfault __P((struct lwp *, int));
433 #endif /* VM86 */
434
435 /* consinit.c */
436 void kgdb_port_init __P((void));
437
438 /* bus_machdep.c */
439 void x86_bus_space_init __P((void));
440 void x86_bus_space_mallocok __P((void));
441
442 #endif /* _KERNEL */
443
444 #include <machine/psl.h>
445
446 /*
447 * CTL_MACHDEP definitions.
448 */
449 #define CPU_CONSDEV 1 /* dev_t: console terminal device */
450 #define CPU_BIOSBASEMEM 2 /* int: bios-reported base mem (K) */
451 #define CPU_BIOSEXTMEM 3 /* int: bios-reported ext. mem (K) */
452 #define CPU_NKPDE 4 /* int: number of kernel PDEs */
453 #define CPU_BOOTED_KERNEL 5 /* string: booted kernel name */
454 #define CPU_DISKINFO 6 /* struct disklist *:
455 * disk geometry information */
456 #define CPU_FPU_PRESENT 7 /* int: FPU is present */
457 #define CPU_OSFXSR 8 /* int: OS uses FXSAVE/FXRSTOR */
458 #define CPU_SSE 9 /* int: OS/CPU supports SSE */
459 #define CPU_SSE2 10 /* int: OS/CPU supports SSE2 */
460 #define CPU_TMLR_MODE 11 /* int: longrun mode
461 * 0: minimum frequency
462 * 1: economy
463 * 2: performance
464 * 3: maximum frequency
465 */
466 #define CPU_TMLR_FREQUENCY 12 /* int: current frequency */
467 #define CPU_TMLR_VOLTAGE 13 /* int: curret voltage */
468 #define CPU_TMLR_PERCENTAGE 14 /* int: current clock percentage */
469 #define CPU_MAXID 15 /* number of valid machdep ids */
470
471 #define CTL_MACHDEP_NAMES { \
472 { 0, 0 }, \
473 { "console_device", CTLTYPE_STRUCT }, \
474 { "biosbasemem", CTLTYPE_INT }, \
475 { "biosextmem", CTLTYPE_INT }, \
476 { "nkpde", CTLTYPE_INT }, \
477 { "booted_kernel", CTLTYPE_STRING }, \
478 { "diskinfo", CTLTYPE_STRUCT }, \
479 { "fpu_present", CTLTYPE_INT }, \
480 { "osfxsr", CTLTYPE_INT }, \
481 { "sse", CTLTYPE_INT }, \
482 { "sse2", CTLTYPE_INT }, \
483 { "tm_longrun_mode", CTLTYPE_INT }, \
484 { "tm_longrun_frequency", CTLTYPE_INT }, \
485 { "tm_longrun_voltage", CTLTYPE_INT }, \
486 { "tm_longrun_percentage", CTLTYPE_INT }, \
487 }
488
489
490 /*
491 * Structure for CPU_DISKINFO sysctl call.
492 * XXX this should be somewhere else.
493 */
494 #define MAX_BIOSDISKS 16
495
496 struct disklist {
497 int dl_nbiosdisks; /* number of bios disks */
498 struct biosdisk_info {
499 int bi_dev; /* BIOS device # (0x80 ..) */
500 int bi_cyl; /* cylinders on disk */
501 int bi_head; /* heads per track */
502 int bi_sec; /* sectors per track */
503 u_int64_t bi_lbasecs; /* total sec. (iff ext13) */
504 #define BIFLAG_INVALID 0x01
505 #define BIFLAG_EXTINT13 0x02
506 int bi_flags;
507 } dl_biosdisks[MAX_BIOSDISKS];
508
509 int dl_nnativedisks; /* number of native disks */
510 struct nativedisk_info {
511 char ni_devname[16]; /* native device name */
512 int ni_nmatches; /* # of matches w/ BIOS */
513 int ni_biosmatches[MAX_BIOSDISKS]; /* indices in dl_biosdisks */
514 } dl_nativedisks[1]; /* actually longer */
515 };
516
517 #endif /* !_I386_CPU_H_ */
518