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