Home | History | Annotate | Line # | Download | only in include
cpu.h revision 1.159
      1 /*	$NetBSD: cpu.h,v 1.159 2008/01/15 14:50:09 joerg 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 #ifdef _KERNEL
     41 #if defined(_KERNEL_OPT)
     42 #include "opt_multiprocessor.h"
     43 #include "opt_math_emulate.h"
     44 #include "opt_user_ldt.h"
     45 #include "opt_vm86.h"
     46 #include "opt_xen.h"
     47 #endif
     48 
     49 /*
     50  * Definitions unique to i386 cpu support.
     51  */
     52 #include <machine/frame.h>
     53 #include <machine/segments.h>
     54 #include <machine/tss.h>
     55 #include <machine/intrdefs.h>
     56 #include <x86/cacheinfo.h>
     57 #include <x86/via_padlock.h>
     58 
     59 #include <sys/device.h>
     60 #include <sys/cpu_data.h>
     61 #include <sys/cc_microtime.h>
     62 
     63 #include <lib/libkern/libkern.h>	/* offsetof */
     64 
     65 struct intrsource;
     66 struct pmap;
     67 
     68 #define	NIOPORTS	1024		/* # of ports we allow to be mapped */
     69 #define	IOMAPSIZE	(NIOPORTS / 8)	/* I/O bitmap size in bytes */
     70 
     71 /*
     72  * a bunch of this belongs in cpuvar.h; move it later..
     73  */
     74 
     75 struct cpu_info {
     76 	struct device *ci_dev;		/* pointer to our device */
     77 	struct cpu_info *ci_self;	/* self-pointer */
     78 	void	*ci_tlog_base;		/* Trap log base */
     79 	int32_t ci_tlog_offset;		/* Trap log current offset */
     80 
     81 	/*
     82 	 * Will be accessed by other CPUs.
     83 	 */
     84 	struct cpu_info *ci_next;	/* next cpu */
     85 	struct lwp *ci_curlwp;		/* current owner of the processor */
     86 	struct pmap_cpu *ci_pmap_cpu;	/* per-CPU pmap data */
     87 	struct lwp *ci_fpcurlwp;	/* current owner of the FPU */
     88 	int	ci_fpsaving;		/* save in progress */
     89 	cpuid_t ci_cpuid;		/* our CPU ID */
     90 	int	ci_cpumask;		/* (1 << CPU ID) */
     91 	u_int ci_apicid;		/* our APIC ID */
     92 	uint8_t ci_initapicid;		/* our intitial APIC ID */
     93 	uint8_t ci_packageid;
     94 	uint8_t ci_coreid;
     95 	uint8_t ci_smtid;
     96 	struct cpu_data ci_data;	/* MI per-cpu data */
     97 	struct cc_microtime_state ci_cc;/* cc_microtime state */
     98 
     99 	/*
    100 	 * Private members.
    101 	 */
    102 	struct evcnt ci_tlb_evcnt;	/* tlb shootdown counter */
    103 	struct pmap *ci_pmap;		/* current pmap */
    104 	int ci_need_tlbwait;		/* need to wait for TLB invalidations */
    105 	int ci_want_pmapload;		/* pmap_load() is needed */
    106 	volatile int ci_tlbstate;	/* one of TLBSTATE_ states. see below */
    107 #define	TLBSTATE_VALID	0	/* all user tlbs are valid */
    108 #define	TLBSTATE_LAZY	1	/* tlbs are valid but won't be kept uptodate */
    109 #define	TLBSTATE_STALE	2	/* we might have stale user tlbs */
    110 
    111 #ifdef XEN
    112 	struct iplsource  *ci_isources[NIPL];
    113 #else
    114 	struct intrsource *ci_isources[MAX_INTR_SOURCES];
    115 #endif
    116 	volatile int	ci_mtx_count;	/* Negative count of spin mutexes */
    117 	volatile int	ci_mtx_oldspl;	/* Old SPL at this ci_idepth */
    118 
    119 	/* The following must be aligned for cmpxchg8b. */
    120 	struct {
    121 		uint32_t	ipending;
    122 		int		ilevel;
    123 	} ci_istate __aligned(8);
    124 #define ci_ipending	ci_istate.ipending
    125 #define	ci_ilevel	ci_istate.ilevel
    126 
    127 	int		ci_idepth;
    128 	void *		ci_intrstack;
    129 	uint32_t	ci_imask[NIPL];
    130 	uint32_t	ci_iunmask[NIPL];
    131 
    132 	uint32_t ci_flags;		/* flags; see below */
    133 	uint32_t ci_ipis;		/* interprocessor interrupts pending */
    134 	int sc_apic_version;		/* local APIC version */
    135 
    136 	int32_t		ci_cpuid_level;
    137 	uint32_t	ci_signature;	 /* X86 cpuid type */
    138 	uint32_t	ci_feature_flags;/* X86 %edx CPUID feature bits */
    139 	uint32_t	ci_feature2_flags;/* X86 %ecx CPUID feature bits */
    140 	uint32_t	ci_feature3_flags;/* X86 extended feature bits */
    141 	uint32_t	ci_padlock_flags;/* VIA PadLock feature bits */
    142 	uint32_t	ci_cpu_class;	 /* CPU class */
    143 	uint32_t	ci_brand_id;	 /* Intel brand id */
    144 	uint32_t	ci_vendor[4];	 /* vendor string */
    145 	uint32_t	ci_cpu_serial[3]; /* PIII serial number */
    146 	uint64_t	ci_tsc_freq;	 /* cpu cycles/second */
    147 	volatile uint32_t	ci_lapic_counter;
    148 
    149 	const struct cpu_functions *ci_func;  /* start/stop functions */
    150 	void (*cpu_setup)(struct cpu_info *);
    151  					/* proc-dependant init */
    152 	void (*ci_info)(struct cpu_info *);
    153 
    154 	int		ci_want_resched;
    155 	struct trapframe *ci_ddb_regs;
    156 
    157 	u_int ci_cflush_lsize;	/* CFLUSH insn line size */
    158 	struct x86_cache_info ci_cinfo[CAI_COUNT];
    159 
    160 	union descriptor *ci_gdt;
    161 
    162 	struct i386tss	ci_doubleflt_tss;
    163 	struct i386tss	ci_ddbipi_tss;
    164 
    165 	char *ci_doubleflt_stack;
    166 	char *ci_ddbipi_stack;
    167 
    168 	struct evcnt ci_ipi_events[X86_NIPI];
    169 
    170 	struct via_padlock	ci_vp;	/* VIA PadLock private storage */
    171 
    172 	struct i386tss	ci_tss;		/* Per-cpu TSS; shared among LWPs */
    173 	char		ci_iomap[IOMAPSIZE]; /* I/O Bitmap */
    174 	int ci_tss_sel;			/* TSS selector of this cpu */
    175 
    176 	/*
    177 	 * The following two are actually region_descriptors,
    178 	 * but that would pollute the namespace.
    179 	 */
    180 	uint32_t	ci_suspend_gdt;
    181 	uint16_t	ci_suspend_gdt_padding;
    182 	uint32_t	ci_suspend_idt;
    183 	uint16_t	ci_suspend_idt_padding;
    184 
    185 	uint16_t	ci_suspend_tr;
    186 	uint16_t	ci_suspend_ldt;
    187 	uint16_t	ci_suspend_fs;
    188 	uint16_t	ci_suspend_gs;
    189 	uint32_t	ci_suspend_ebx;
    190 	uint32_t	ci_suspend_esi;
    191 	uint32_t	ci_suspend_edi;
    192 	uint32_t	ci_suspend_ebp;
    193 	uint32_t	ci_suspend_esp;
    194 	uint32_t	ci_suspend_efl;
    195 	uint32_t	ci_suspend_cr0;
    196 	uint32_t	ci_suspend_cr2;
    197 	uint32_t	ci_suspend_cr3;
    198 	uint32_t	ci_suspend_cr4;
    199 #ifdef XEN
    200 	int		ci_fpused;	/* FPU was used by curlwp */
    201 #endif
    202 };
    203 
    204 /*
    205  * Processor flag notes: The "primary" CPU has certain MI-defined
    206  * roles (mostly relating to hardclock handling); we distinguish
    207  * betwen the processor which booted us, and the processor currently
    208  * holding the "primary" role just to give us the flexibility later to
    209  * change primaries should we be sufficiently twisted.
    210  */
    211 
    212 #define	CPUF_BSP	0x0001		/* CPU is the original BSP */
    213 #define	CPUF_AP		0x0002		/* CPU is an AP */
    214 #define	CPUF_SP		0x0004		/* CPU is only processor */
    215 #define	CPUF_PRIMARY	0x0008		/* CPU is active primary processor */
    216 
    217 #define CPUF_APIC_CD    0x0010		/* CPU has apic configured */
    218 
    219 #define	CPUF_PRESENT	0x1000		/* CPU is present */
    220 #define	CPUF_RUNNING	0x2000		/* CPU is running */
    221 #define	CPUF_PAUSE	0x4000		/* CPU is paused in DDB */
    222 #define	CPUF_GO		0x8000		/* CPU should start running */
    223 
    224 /*
    225  * We statically allocate the CPU info for the primary CPU (or,
    226  * the only CPU on uniprocessors), and the primary CPU is the
    227  * first CPU on the CPU info list.
    228  */
    229 extern struct cpu_info cpu_info_primary;
    230 extern struct cpu_info *cpu_info_list;
    231 
    232 #define	CPU_INFO_ITERATOR		int
    233 #define	CPU_INFO_FOREACH(cii, ci)	cii = 0, ci = cpu_info_list; \
    234 					ci != NULL; ci = ci->ci_next
    235 
    236 #define X86_MAXPROCS		32	/* because we use a bitmask */
    237 
    238 #define CPU_STARTUP(_ci, _target)	((_ci)->ci_func->start(_ci, _target))
    239 #define CPU_STOP(_ci)	        	((_ci)->ci_func->stop(_ci))
    240 #define CPU_START_CLEANUP(_ci)		((_ci)->ci_func->cleanup(_ci))
    241 
    242 #if defined(__GNUC__) && defined(_KERNEL)
    243 static struct cpu_info *x86_curcpu(void);
    244 static lwp_t *x86_curlwp(void);
    245 
    246 __inline static struct cpu_info * __unused
    247 x86_curcpu(void)
    248 {
    249 	struct cpu_info *ci;
    250 
    251 	__asm volatile("movl %%fs:%1, %0" :
    252 	    "=r" (ci) :
    253 	    "m"
    254 	    (*(struct cpu_info * const *)offsetof(struct cpu_info, ci_self)));
    255 	return ci;
    256 }
    257 
    258 __inline static lwp_t * __unused
    259 x86_curlwp(void)
    260 {
    261 	lwp_t *l;
    262 
    263 	__asm volatile("movl %%fs:%1, %0" :
    264 	    "=r" (l) :
    265 	    "m"
    266 	    (*(struct cpu_info * const *)offsetof(struct cpu_info, ci_curlwp)));
    267 	return l;
    268 }
    269 #else	/* __GNUC__ && _KERNEL */
    270 /* For non-GCC and LKMs */
    271 struct cpu_info	*x86_curcpu(void);
    272 lwp_t	*x86_curlwp(void);
    273 #endif	/* __GNUC__ && _KERNEL */
    274 
    275 #define cpu_number() 		(curcpu()->ci_cpuid)
    276 
    277 #define CPU_IS_PRIMARY(ci)	((ci)->ci_flags & CPUF_PRIMARY)
    278 
    279 #define aston(l)		((l)->l_md.md_astpending = 1)
    280 
    281 extern	struct cpu_info *cpu_info[X86_MAXPROCS];
    282 
    283 void cpu_boot_secondary_processors(void);
    284 void cpu_init_idle_lwps(void);
    285 
    286 extern uint32_t cpus_attached;
    287 #ifndef XEN
    288 #define	curcpu()		x86_curcpu()
    289 #define	curlwp			x86_curlwp()
    290 #else
    291 /* XXX initgdt() calls pmap_kenter_pa() which calls splvm() before %fs is set */
    292 #define curcpu()		(&cpu_info_primary)
    293 #define curlwp			curcpu()->ci_curlwp
    294 #endif
    295 #define	curpcb			(&curlwp->l_addr->u_pcb)
    296 
    297 /*
    298  * Arguments to hardclock, softclock and statclock
    299  * encapsulate the previous machine state in an opaque
    300  * clockframe; for now, use generic intrframe.
    301  */
    302 struct clockframe {
    303 	struct intrframe cf_if;
    304 };
    305 
    306 #define	CLKF_USERMODE(frame)	USERMODE((frame)->cf_if.if_cs, (frame)->cf_if.if_eflags)
    307 #define	CLKF_PC(frame)		((frame)->cf_if.if_eip)
    308 #define	CLKF_INTR(frame)	(curcpu()->ci_idepth > 0)
    309 
    310 /*
    311  * This is used during profiling to integrate system time.  It can safely
    312  * assume that the process is resident.
    313  */
    314 #define	LWP_PC(l)		((l)->l_md.md_regs->tf_eip)
    315 
    316 /*
    317  * Give a profiling tick to the current process when the user profiling
    318  * buffer pages are invalid.  On the i386, request an ast to send us
    319  * through trap(), marking the proc as needing a profiling tick.
    320  */
    321 extern void	cpu_need_proftick(struct lwp *l);
    322 
    323 /*
    324  * Notify the LWP l that it has a signal pending, process as soon as
    325  * possible.
    326  */
    327 extern void	cpu_signotify(struct lwp *);
    328 
    329 /*
    330  * We need a machine-independent name for this.
    331  */
    332 extern void (*delay_func)(unsigned int);
    333 struct timeval;
    334 
    335 #define	DELAY(x)		(*delay_func)(x)
    336 #define delay(x)		(*delay_func)(x)
    337 
    338 /*
    339  * pull in #defines for kinds of processors
    340  */
    341 #include <machine/cputypes.h>
    342 
    343 struct cpu_nocpuid_nameclass {
    344 	int cpu_vendor;
    345 	const char *cpu_vendorname;
    346 	const char *cpu_name;
    347 	int cpu_class;
    348 	void (*cpu_setup)(struct cpu_info *);
    349 	void (*cpu_cacheinfo)(struct cpu_info *);
    350 	void (*cpu_info)(struct cpu_info *);
    351 };
    352 
    353 
    354 struct cpu_cpuid_nameclass {
    355 	const char *cpu_id;
    356 	int cpu_vendor;
    357 	const char *cpu_vendorname;
    358 	struct cpu_cpuid_family {
    359 		int cpu_class;
    360 		const char *cpu_models[CPU_MAXMODEL+2];
    361 		void (*cpu_setup)(struct cpu_info *);
    362 		void (*cpu_probe)(struct cpu_info *);
    363 		void (*cpu_info)(struct cpu_info *);
    364 	} cpu_family[CPU_MAXFAMILY - CPU_MINFAMILY + 1];
    365 };
    366 
    367 extern int biosbasemem;
    368 extern int biosextmem;
    369 extern unsigned int cpu_feature;
    370 extern unsigned int cpu_feature2;
    371 extern unsigned int cpu_feature_padlock;
    372 extern int cpu;
    373 extern int cpu_class;
    374 extern char cpu_brand_string[];
    375 extern const struct cpu_nocpuid_nameclass i386_nocpuid_cpus[];
    376 extern const struct cpu_cpuid_nameclass i386_cpuid_cpus[];
    377 
    378 extern int i386_use_fxsave;
    379 extern int i386_has_sse;
    380 extern int i386_has_sse2;
    381 
    382 /* machdep.c */
    383 void	dumpconf(void);
    384 void	cpu_reset(void);
    385 void	i386_proc0_tss_ldt_init(void);
    386 
    387 extern int tmx86_has_longrun;
    388 extern u_int crusoe_longrun;
    389 extern u_int crusoe_frequency;
    390 extern u_int crusoe_voltage;
    391 extern u_int crusoe_percentage;
    392 extern u_int tmx86_set_longrun_mode(u_int);
    393 void tmx86_get_longrun_status_all(void);
    394 u_int tmx86_get_longrun_mode(void);
    395 void identifycpu(struct cpu_info *);
    396 
    397 /* vm_machdep.c */
    398 void	cpu_proc_fork(struct proc *, struct proc *);
    399 
    400 /* locore.s */
    401 struct region_descriptor;
    402 void	lgdt(struct region_descriptor *);
    403 #ifdef XEN
    404 void	lgdt_finish(void);
    405 void	i386_switch_context(lwp_t *);
    406 #endif
    407 void	fillw(short, void *, size_t);
    408 
    409 struct pcb;
    410 void	savectx(struct pcb *);
    411 void	lwp_trampoline(void);
    412 #ifdef XEN
    413 void	startrtclock(void);
    414 void	xen_delay(unsigned int);
    415 void	xen_initclocks(void);
    416 #else
    417 /* clock.c */
    418 void	initrtclock(u_long);
    419 void	startrtclock(void);
    420 void	i8254_delay(unsigned int);
    421 void	i8254_microtime(struct timeval *);
    422 void	i8254_initclocks(void);
    423 #endif
    424 
    425 /* cpu.c */
    426 
    427 void	cpu_probe_features(struct cpu_info *);
    428 
    429 /* npx.c */
    430 void	npxsave_lwp(struct lwp *, bool);
    431 void	npxsave_cpu(bool);
    432 
    433 /* vm_machdep.c */
    434 int kvtop(void *);
    435 
    436 #ifdef MATH_EMULATE
    437 /* math_emulate.c */
    438 int	math_emulate(struct trapframe *, ksiginfo_t *);
    439 #endif
    440 
    441 #ifdef USER_LDT
    442 /* sys_machdep.h */
    443 int	x86_get_ldt(struct lwp *, void *, register_t *);
    444 int	x86_set_ldt(struct lwp *, void *, register_t *);
    445 #endif
    446 
    447 /* isa_machdep.c */
    448 void	isa_defaultirq(void);
    449 int	isa_nmi(void);
    450 
    451 #ifdef VM86
    452 /* vm86.c */
    453 void	vm86_gpfault(struct lwp *, int);
    454 #endif /* VM86 */
    455 
    456 /* consinit.c */
    457 void kgdb_port_init(void);
    458 
    459 /* bus_machdep.c */
    460 void x86_bus_space_init(void);
    461 void x86_bus_space_mallocok(void);
    462 
    463 #include <machine/psl.h>	/* Must be after struct cpu_info declaration */
    464 
    465 #endif /* _KERNEL */
    466 
    467 /*
    468  * CTL_MACHDEP definitions.
    469  */
    470 #define	CPU_CONSDEV		1	/* dev_t: console terminal device */
    471 #define	CPU_BIOSBASEMEM		2	/* int: bios-reported base mem (K) */
    472 #define	CPU_BIOSEXTMEM		3	/* int: bios-reported ext. mem (K) */
    473 /* 	CPU_NKPDE		4	obsolete: int: number of kernel PDEs */
    474 #define	CPU_BOOTED_KERNEL	5	/* string: booted kernel name */
    475 #define CPU_DISKINFO		6	/* struct disklist *:
    476 					 * disk geometry information */
    477 #define CPU_FPU_PRESENT		7	/* int: FPU is present */
    478 #define	CPU_OSFXSR		8	/* int: OS uses FXSAVE/FXRSTOR */
    479 #define	CPU_SSE			9	/* int: OS/CPU supports SSE */
    480 #define	CPU_SSE2		10	/* int: OS/CPU supports SSE2 */
    481 #define CPU_TMLR_MODE		11 	/* int: longrun mode
    482 					 * 0: minimum frequency
    483 					 * 1: economy
    484 					 * 2: performance
    485 					 * 3: maximum frequency
    486 					 */
    487 #define CPU_TMLR_FREQUENCY	12 	/* int: current frequency */
    488 #define CPU_TMLR_VOLTAGE	13 	/* int: curret voltage */
    489 #define CPU_TMLR_PERCENTAGE	14	/* int: current clock percentage */
    490 #define	CPU_MAXID		15	/* number of valid machdep ids */
    491 
    492 #define	CTL_MACHDEP_NAMES { \
    493 	{ 0, 0 }, \
    494 	{ "console_device", CTLTYPE_STRUCT }, \
    495 	{ "biosbasemem", CTLTYPE_INT }, \
    496 	{ "biosextmem", CTLTYPE_INT }, \
    497 	{ "booted_kernel", CTLTYPE_STRING }, \
    498 	{ "diskinfo", CTLTYPE_STRUCT }, \
    499 	{ "fpu_present", CTLTYPE_INT }, \
    500 	{ "osfxsr", CTLTYPE_INT }, \
    501 	{ "sse", CTLTYPE_INT }, \
    502 	{ "sse2", CTLTYPE_INT }, \
    503 	{ "tm_longrun_mode", CTLTYPE_INT }, \
    504 	{ "tm_longrun_frequency", CTLTYPE_INT }, \
    505 	{ "tm_longrun_voltage", CTLTYPE_INT }, \
    506 	{ "tm_longrun_percentage", CTLTYPE_INT }, \
    507 }
    508 
    509 /*
    510  * Structure for CPU_DISKINFO sysctl call.
    511  * XXX this should be somewhere else.
    512  */
    513 #define MAX_BIOSDISKS	16
    514 
    515 struct disklist {
    516 	int dl_nbiosdisks;			   /* number of bios disks */
    517 	struct biosdisk_info {
    518 		int bi_dev;			   /* BIOS device # (0x80 ..) */
    519 		int bi_cyl;			   /* cylinders on disk */
    520 		int bi_head;			   /* heads per track */
    521 		int bi_sec;			   /* sectors per track */
    522 		uint64_t bi_lbasecs;		   /* total sec. (iff ext13) */
    523 #define BIFLAG_INVALID		0x01
    524 #define BIFLAG_EXTINT13		0x02
    525 		int bi_flags;
    526 	} dl_biosdisks[MAX_BIOSDISKS];
    527 
    528 	int dl_nnativedisks;			   /* number of native disks */
    529 	struct nativedisk_info {
    530 		char ni_devname[16];		   /* native device name */
    531 		int ni_nmatches; 		   /* # of matches w/ BIOS */
    532 		int ni_biosmatches[MAX_BIOSDISKS]; /* indices in dl_biosdisks */
    533 	} dl_nativedisks[1];			   /* actually longer */
    534 };
    535 #endif /* !_I386_CPU_H_ */
    536