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