1 /* $NetBSD: cpu.h,v 1.42 2011/11/10 00:12:05 jym 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 _X86_CPU_H_ 38 #define _X86_CPU_H_ 39 40 #if defined(_KERNEL) || defined(_STANDALONE) 41 #include <sys/types.h> 42 #else 43 #include <stdbool.h> 44 #endif /* _KERNEL || _STANDALONE */ 45 46 #if defined(_KERNEL) || defined(_KMEMUSER) 47 #if defined(_KERNEL_OPT) 48 #include "opt_xen.h" 49 #ifdef i386 50 #include "opt_user_ldt.h" 51 #include "opt_vm86.h" 52 #endif 53 #endif 54 55 /* 56 * Definitions unique to x86 cpu support. 57 */ 58 #include <machine/frame.h> 59 #include <machine/pte.h> 60 #include <machine/segments.h> 61 #include <machine/tss.h> 62 #include <machine/intrdefs.h> 63 64 #include <x86/cacheinfo.h> 65 66 #include <sys/cpu_data.h> 67 #include <sys/evcnt.h> 68 #include <sys/device_if.h> /* for device_t */ 69 70 #ifdef XEN 71 #include <xen/xen3-public/xen.h> 72 #include <xen/xen3-public/event_channel.h> 73 #endif /* XEN */ 74 75 struct intrsource; 76 struct pmap; 77 struct device; 78 79 #ifdef __x86_64__ 80 #define i386tss x86_64_tss 81 #endif 82 83 #define NIOPORTS 1024 /* # of ports we allow to be mapped */ 84 #define IOMAPSIZE (NIOPORTS / 8) /* I/O bitmap size in bytes */ 85 86 /* 87 * a bunch of this belongs in cpuvar.h; move it later.. 88 */ 89 90 struct cpu_info { 91 struct cpu_data ci_data; /* MI per-cpu data */ 92 device_t ci_dev; /* pointer to our device */ 93 struct cpu_info *ci_self; /* self-pointer */ 94 volatile struct vcpu_info *ci_vcpu; /* for XEN */ 95 void *ci_tlog_base; /* Trap log base */ 96 int32_t ci_tlog_offset; /* Trap log current offset */ 97 98 /* 99 * Will be accessed by other CPUs. 100 */ 101 struct cpu_info *ci_next; /* next cpu */ 102 struct lwp *ci_curlwp; /* current owner of the processor */ 103 struct lwp *ci_fpcurlwp; /* current owner of the FPU */ 104 int ci_fpsaving; /* save in progress */ 105 int ci_fpused; /* XEN: FPU was used by curlwp */ 106 cpuid_t ci_cpuid; /* our CPU ID */ 107 int ci_cpumask; /* (1 << CPU ID) */ 108 uint32_t ci_acpiid; /* our ACPI/MADT ID */ 109 uint32_t ci_initapicid; /* our intitial APIC ID */ 110 111 /* 112 * Private members. 113 */ 114 struct evcnt ci_tlb_evcnt; /* tlb shootdown counter */ 115 struct pmap *ci_pmap; /* current pmap */ 116 int ci_need_tlbwait; /* need to wait for TLB invalidations */ 117 int ci_want_pmapload; /* pmap_load() is needed */ 118 volatile int ci_tlbstate; /* one of TLBSTATE_ states. see below */ 119 #define TLBSTATE_VALID 0 /* all user tlbs are valid */ 120 #define TLBSTATE_LAZY 1 /* tlbs are valid but won't be kept uptodate */ 121 #define TLBSTATE_STALE 2 /* we might have stale user tlbs */ 122 int ci_curldt; /* current LDT descriptor */ 123 int ci_nintrhand; /* number of H/W interrupt handlers */ 124 uint64_t ci_scratch; 125 uintptr_t ci_pmap_data[128 / sizeof(uintptr_t)]; 126 127 #ifdef XEN 128 struct iplsource *ci_isources[NIPL]; 129 #else 130 struct intrsource *ci_isources[MAX_INTR_SOURCES]; 131 #endif 132 volatile int ci_mtx_count; /* Negative count of spin mutexes */ 133 volatile int ci_mtx_oldspl; /* Old SPL at this ci_idepth */ 134 135 /* The following must be aligned for cmpxchg8b. */ 136 struct { 137 uint32_t ipending; 138 int ilevel; 139 } ci_istate __aligned(8); 140 #define ci_ipending ci_istate.ipending 141 #define ci_ilevel ci_istate.ilevel 142 143 int ci_idepth; 144 void * ci_intrstack; 145 uint32_t ci_imask[NIPL]; 146 uint32_t ci_iunmask[NIPL]; 147 148 uint32_t ci_flags; /* flags; see below */ 149 uint32_t ci_ipis; /* interprocessor interrupts pending */ 150 uint32_t sc_apic_version; /* local APIC version */ 151 152 uint32_t ci_signature; /* X86 cpuid type */ 153 uint32_t ci_vendor[4]; /* vendor string */ 154 uint32_t ci_cpu_serial[3]; /* PIII serial number */ 155 volatile uint32_t ci_lapic_counter; 156 157 uint32_t ci_feat_val[5]; /* X86 CPUID feature bits 158 * [0] basic features %edx 159 * [1] basic features %ecx 160 * [2] extended features %edx 161 * [3] extended features %ecx 162 * [4] VIA padlock features 163 */ 164 165 const struct cpu_functions *ci_func; /* start/stop functions */ 166 struct trapframe *ci_ddb_regs; 167 168 u_int ci_cflush_lsize; /* CFLUSH insn line size */ 169 struct x86_cache_info ci_cinfo[CAI_COUNT]; 170 171 union descriptor *ci_gdt; 172 173 #ifdef i386 174 struct i386tss ci_doubleflt_tss; 175 struct i386tss ci_ddbipi_tss; 176 #endif 177 178 #ifdef PAE 179 paddr_t ci_pae_l3_pdirpa; /* PA of L3 PD */ 180 pd_entry_t * ci_pae_l3_pdir; /* VA pointer to L3 PD */ 181 #endif 182 183 #if defined(XEN) && (defined(PAE) || defined(__x86_64__)) 184 /* Currently active user PGD (can't use rcr3() with Xen) */ 185 pd_entry_t * ci_kpm_pdir; /* per-cpu PMD (va) */ 186 paddr_t ci_kpm_pdirpa; /* per-cpu PMD (pa) */ 187 #if defined(__x86_64__) 188 paddr_t ci_xen_current_user_pgd; 189 #endif /* __x86_64__ */ 190 #endif /* XEN et.al */ 191 192 193 char *ci_doubleflt_stack; 194 char *ci_ddbipi_stack; 195 196 #ifndef XEN 197 struct evcnt ci_ipi_events[X86_NIPI]; 198 #else /* XEN */ 199 struct evcnt ci_ipi_events[XEN_NIPIS]; 200 evtchn_port_t ci_ipi_evtchn; 201 #endif /* XEN */ 202 203 device_t ci_frequency; /* Frequency scaling technology */ 204 device_t ci_padlock; /* VIA PadLock private storage */ 205 device_t ci_temperature; /* Intel coretemp(4) or equivalent */ 206 device_t ci_vm; /* Virtual machine guest driver */ 207 208 struct i386tss ci_tss; /* Per-cpu TSS; shared among LWPs */ 209 char ci_iomap[IOMAPSIZE]; /* I/O Bitmap */ 210 int ci_tss_sel; /* TSS selector of this cpu */ 211 212 /* 213 * The following two are actually region_descriptors, 214 * but that would pollute the namespace. 215 */ 216 uintptr_t ci_suspend_gdt; 217 uint16_t ci_suspend_gdt_padding; 218 uintptr_t ci_suspend_idt; 219 uint16_t ci_suspend_idt_padding; 220 221 uint16_t ci_suspend_tr; 222 uint16_t ci_suspend_ldt; 223 uintptr_t ci_suspend_fs; 224 uintptr_t ci_suspend_gs; 225 uintptr_t ci_suspend_kgs; 226 uintptr_t ci_suspend_efer; 227 uintptr_t ci_suspend_reg[12]; 228 uintptr_t ci_suspend_cr0; 229 uintptr_t ci_suspend_cr2; 230 uintptr_t ci_suspend_cr3; 231 uintptr_t ci_suspend_cr4; 232 uintptr_t ci_suspend_cr8; 233 234 /* The following must be in a single cache line. */ 235 int ci_want_resched __aligned(64); 236 int ci_padout __aligned(64); 237 }; 238 239 /* 240 * Macros to handle (some) trapframe registers for common x86 code. 241 */ 242 #ifdef __x86_64__ 243 #define X86_TF_RAX(tf) tf->tf_rax 244 #define X86_TF_RDX(tf) tf->tf_rdx 245 #define X86_TF_RSP(tf) tf->tf_rsp 246 #define X86_TF_RIP(tf) tf->tf_rip 247 #define X86_TF_RFLAGS(tf) tf->tf_rflags 248 #else 249 #define X86_TF_RAX(tf) tf->tf_eax 250 #define X86_TF_RDX(tf) tf->tf_edx 251 #define X86_TF_RSP(tf) tf->tf_esp 252 #define X86_TF_RIP(tf) tf->tf_eip 253 #define X86_TF_RFLAGS(tf) tf->tf_eflags 254 #endif 255 256 /* 257 * Processor flag notes: The "primary" CPU has certain MI-defined 258 * roles (mostly relating to hardclock handling); we distinguish 259 * betwen the processor which booted us, and the processor currently 260 * holding the "primary" role just to give us the flexibility later to 261 * change primaries should we be sufficiently twisted. 262 */ 263 264 #define CPUF_BSP 0x0001 /* CPU is the original BSP */ 265 #define CPUF_AP 0x0002 /* CPU is an AP */ 266 #define CPUF_SP 0x0004 /* CPU is only processor */ 267 #define CPUF_PRIMARY 0x0008 /* CPU is active primary processor */ 268 269 #define CPUF_SYNCTSC 0x0800 /* Synchronize TSC */ 270 #define CPUF_PRESENT 0x1000 /* CPU is present */ 271 #define CPUF_RUNNING 0x2000 /* CPU is running */ 272 #define CPUF_PAUSE 0x4000 /* CPU is paused in DDB */ 273 #define CPUF_GO 0x8000 /* CPU should start running */ 274 275 #endif /* _KERNEL || __KMEMUSER */ 276 277 #ifdef _KERNEL 278 /* 279 * We statically allocate the CPU info for the primary CPU (or, 280 * the only CPU on uniprocessors), and the primary CPU is the 281 * first CPU on the CPU info list. 282 */ 283 extern struct cpu_info cpu_info_primary; 284 extern struct cpu_info *cpu_info_list; 285 286 #define CPU_INFO_ITERATOR int 287 #define CPU_INFO_FOREACH(cii, ci) cii = 0, ci = cpu_info_list; \ 288 ci != NULL; ci = ci->ci_next 289 290 #define CPU_STARTUP(_ci, _target) ((_ci)->ci_func->start(_ci, _target)) 291 #define CPU_STOP(_ci) ((_ci)->ci_func->stop(_ci)) 292 #define CPU_START_CLEANUP(_ci) ((_ci)->ci_func->cleanup(_ci)) 293 294 #if !defined(__GNUC__) || defined(_MODULE) 295 /* For non-GCC and modules */ 296 struct cpu_info *x86_curcpu(void); 297 void cpu_set_curpri(int); 298 # ifdef __GNUC__ 299 lwp_t *x86_curlwp(void) __attribute__ ((const)); 300 # else 301 lwp_t *x86_curlwp(void); 302 # endif 303 #endif 304 305 #define cpu_number() (cpu_index(curcpu())) 306 307 #define CPU_IS_PRIMARY(ci) ((ci)->ci_flags & CPUF_PRIMARY) 308 309 #define X86_AST_GENERIC 0x01 310 #define X86_AST_PREEMPT 0x02 311 312 #define aston(l, why) ((l)->l_md.md_astpending |= (why)) 313 #define cpu_did_resched(l) ((l)->l_md.md_astpending &= ~X86_AST_PREEMPT) 314 315 void cpu_boot_secondary_processors(void); 316 void cpu_init_idle_lwps(void); 317 void cpu_init_msrs(struct cpu_info *, bool); 318 void cpu_load_pmap(struct pmap *); 319 void cpu_broadcast_halt(void); 320 void cpu_kick(struct cpu_info *); 321 322 extern uint32_t cpus_attached; 323 324 #define curcpu() x86_curcpu() 325 #define curlwp x86_curlwp() 326 #define curpcb ((struct pcb *)lwp_getpcb(curlwp)) 327 328 /* 329 * Arguments to hardclock, softclock and statclock 330 * encapsulate the previous machine state in an opaque 331 * clockframe; for now, use generic intrframe. 332 */ 333 struct clockframe { 334 struct intrframe cf_if; 335 }; 336 337 /* 338 * Give a profiling tick to the current process when the user profiling 339 * buffer pages are invalid. On the i386, request an ast to send us 340 * through trap(), marking the proc as needing a profiling tick. 341 */ 342 extern void cpu_need_proftick(struct lwp *l); 343 344 /* 345 * Notify the LWP l that it has a signal pending, process as soon as 346 * possible. 347 */ 348 extern void cpu_signotify(struct lwp *); 349 350 /* 351 * We need a machine-independent name for this. 352 */ 353 extern void (*delay_func)(unsigned int); 354 struct timeval; 355 356 #define DELAY(x) (*delay_func)(x) 357 #define delay(x) (*delay_func)(x) 358 359 extern int biosbasemem; 360 extern int biosextmem; 361 extern int cpu; 362 extern int cpuid_level; 363 extern int cpu_class; 364 extern char cpu_brand_string[]; 365 extern int use_pae; 366 367 extern int i386_use_fxsave; 368 extern int i386_has_sse; 369 extern int i386_has_sse2; 370 371 extern void (*x86_cpu_idle)(void); 372 #define cpu_idle() (*x86_cpu_idle)() 373 374 /* machdep.c */ 375 void dumpconf(void); 376 void cpu_reset(void); 377 void i386_proc0_tss_ldt_init(void); 378 void dumpconf(void); 379 void cpu_reset(void); 380 void x86_64_proc0_tss_ldt_init(void); 381 void x86_64_init_pcb_tss_ldt(struct cpu_info *); 382 383 /* longrun.c */ 384 u_int tmx86_get_longrun_mode(void); 385 void tmx86_get_longrun_status(u_int *, u_int *, u_int *); 386 void tmx86_init_longrun(void); 387 388 /* identcpu.c */ 389 void cpu_probe(struct cpu_info *); 390 void cpu_identify(struct cpu_info *); 391 392 /* cpu_topology.c */ 393 void x86_cpu_topology(struct cpu_info *); 394 395 /* vm_machdep.c */ 396 void cpu_proc_fork(struct proc *, struct proc *); 397 398 /* locore.s */ 399 struct region_descriptor; 400 void lgdt(struct region_descriptor *); 401 #ifdef XEN 402 void lgdt_finish(void); 403 void i386_switch_context(lwp_t *); 404 #endif 405 406 struct pcb; 407 void savectx(struct pcb *); 408 void lwp_trampoline(void); 409 void child_trampoline(void); 410 #ifdef XEN 411 void startrtclock(void); 412 void xen_delay(unsigned int); 413 void xen_initclocks(void); 414 void xen_suspendclocks(void); 415 void xen_resumeclocks(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 paddr_t kvtop(void *); 435 436 #ifdef USER_LDT 437 /* sys_machdep.h */ 438 int x86_get_ldt(struct lwp *, void *, register_t *); 439 int x86_set_ldt(struct lwp *, void *, register_t *); 440 #endif 441 442 /* isa_machdep.c */ 443 void isa_defaultirq(void); 444 int isa_nmi(void); 445 446 #ifdef VM86 447 /* vm86.c */ 448 void vm86_gpfault(struct lwp *, int); 449 #endif /* VM86 */ 450 451 /* consinit.c */ 452 void kgdb_port_init(void); 453 454 /* bus_machdep.c */ 455 void x86_bus_space_init(void); 456 void x86_bus_space_mallocok(void); 457 458 #endif /* _KERNEL */ 459 460 #if defined(_KERNEL) || defined(_KMEMUSER) 461 #include <machine/psl.h> /* Must be after struct cpu_info declaration */ 462 #endif /* _KERNEL || __KMEMUSER */ 463 464 /* 465 * CTL_MACHDEP definitions. 466 */ 467 #define CPU_CONSDEV 1 /* dev_t: console terminal device */ 468 #define CPU_BIOSBASEMEM 2 /* int: bios-reported base mem (K) */ 469 #define CPU_BIOSEXTMEM 3 /* int: bios-reported ext. mem (K) */ 470 /* CPU_NKPDE 4 obsolete: int: number of kernel PDEs */ 471 #define CPU_BOOTED_KERNEL 5 /* string: booted kernel name */ 472 #define CPU_DISKINFO 6 /* struct disklist *: 473 * disk geometry information */ 474 #define CPU_FPU_PRESENT 7 /* int: FPU is present */ 475 #define CPU_OSFXSR 8 /* int: OS uses FXSAVE/FXRSTOR */ 476 #define CPU_SSE 9 /* int: OS/CPU supports SSE */ 477 #define CPU_SSE2 10 /* int: OS/CPU supports SSE2 */ 478 #define CPU_TMLR_MODE 11 /* int: longrun mode 479 * 0: minimum frequency 480 * 1: economy 481 * 2: performance 482 * 3: maximum frequency 483 */ 484 #define CPU_TMLR_FREQUENCY 12 /* int: current frequency */ 485 #define CPU_TMLR_VOLTAGE 13 /* int: curret voltage */ 486 #define CPU_TMLR_PERCENTAGE 14 /* int: current clock percentage */ 487 #define CPU_MAXID 15 /* number of valid machdep ids */ 488 489 /* 490 * Structure for CPU_DISKINFO sysctl call. 491 * XXX this should be somewhere else. 492 */ 493 #define MAX_BIOSDISKS 16 494 495 struct disklist { 496 int dl_nbiosdisks; /* number of bios disks */ 497 struct biosdisk_info { 498 int bi_dev; /* BIOS device # (0x80 ..) */ 499 int bi_cyl; /* cylinders on disk */ 500 int bi_head; /* heads per track */ 501 int bi_sec; /* sectors per track */ 502 uint64_t bi_lbasecs; /* total sec. (iff ext13) */ 503 #define BIFLAG_INVALID 0x01 504 #define BIFLAG_EXTINT13 0x02 505 int bi_flags; 506 } dl_biosdisks[MAX_BIOSDISKS]; 507 508 int dl_nnativedisks; /* number of native disks */ 509 struct nativedisk_info { 510 char ni_devname[16]; /* native device name */ 511 int ni_nmatches; /* # of matches w/ BIOS */ 512 int ni_biosmatches[MAX_BIOSDISKS]; /* indices in dl_biosdisks */ 513 } dl_nativedisks[1]; /* actually longer */ 514 }; 515 #endif /* !_X86_CPU_H_ */ 516