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