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