cpu.h revision 1.28
1/* $NetBSD: cpu.h,v 1.28 2007/10/17 19:53:03 garbled 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 _AMD64_CPU_H_ 38#define _AMD64_CPU_H_ 39 40#if defined(_KERNEL) 41#if defined(_KERNEL_OPT) 42#include "opt_multiprocessor.h" 43#include "opt_lockdebug.h" 44#endif 45 46/* 47 * Definitions unique to x86-64 cpu support. 48 */ 49#include <machine/frame.h> 50#include <machine/segments.h> 51#include <machine/tss.h> 52#include <machine/intrdefs.h> 53#include <x86/cacheinfo.h> 54 55#include <sys/device.h> 56#include <sys/simplelock.h> 57#include <sys/cpu_data.h> 58#include <sys/cc_microtime.h> 59 60struct cpu_info { 61 struct device *ci_dev; 62 struct cpu_info *ci_self; 63 64 /* 65 * Will be accessed by other CPUs. 66 */ 67 struct cpu_info *ci_next; 68 struct lwp *ci_curlwp; 69 struct pmap_cpu *ci_pmap_cpu; 70 struct lwp *ci_fpcurlwp; 71 int ci_fpsaving; 72 u_int ci_cpuid; 73 int ci_cpumask; /* (1 << CPU ID) */ 74 u_int ci_apicid; 75 struct cpu_data ci_data; /* MI per-cpu data */ 76 struct cc_microtime_state ci_cc;/* cc_microtime state */ 77 78 /* 79 * Private members. 80 */ 81 struct evcnt ci_tlb_evcnt; /* tlb shootdown counter */ 82 int ci_need_tlbwait; /* need to wait for TLB invalidations */ 83 u_int64_t ci_scratch; 84 struct intrsource *ci_isources[MAX_INTR_SOURCES]; 85 volatile int ci_mtx_count; /* Negative count of spin mutexes */ 86 volatile int ci_mtx_oldspl; /* Old SPL at this ci_idepth */ 87 88 /* The following must be aligned for cmpxchg8b. */ 89 struct { 90 uint32_t ipending; 91 int ilevel; 92 } ci_istate __aligned(8); 93#define ci_ipending ci_istate.ipending 94#define ci_ilevel ci_istate.ilevel 95 96 int ci_idepth; 97 u_int32_t ci_imask[NIPL]; 98 u_int32_t ci_iunmask[NIPL]; 99 100 paddr_t ci_idle_pcb_paddr; 101 u_int ci_flags; 102 u_int32_t ci_ipis; 103 104 int32_t ci_cpuid_level; 105 uint32_t ci_signature; 106 uint32_t ci_feature_flags; 107 uint32_t ci_feature2_flags; 108 uint32_t ci_vendor[4]; /* vendor string */ 109 u_int64_t ci_tsc_freq; 110 111 const struct cpu_functions *ci_func; 112 void (*cpu_setup)(struct cpu_info *); 113 void (*ci_info)(struct cpu_info *); 114 115 int ci_want_resched; 116 struct trapframe *ci_ddb_regs; 117 118 struct x86_cache_info ci_cinfo[CAI_COUNT]; 119 120 char *ci_gdt; 121 122 struct x86_64_tss ci_doubleflt_tss; 123 struct x86_64_tss ci_ddbipi_tss; 124 125 char *ci_doubleflt_stack; 126 char *ci_ddbipi_stack; 127 128 struct evcnt ci_ipi_events[X86_NIPI]; 129}; 130 131#define CPUF_BSP 0x0001 /* CPU is the original BSP */ 132#define CPUF_AP 0x0002 /* CPU is an AP */ 133#define CPUF_SP 0x0004 /* CPU is only processor */ 134#define CPUF_PRIMARY 0x0008 /* CPU is active primary processor */ 135 136#define CPUF_PRESENT 0x1000 /* CPU is present */ 137#define CPUF_RUNNING 0x2000 /* CPU is running */ 138#define CPUF_PAUSE 0x4000 /* CPU is paused in DDB */ 139#define CPUF_GO 0x8000 /* CPU should start running */ 140 141 142extern struct cpu_info cpu_info_primary; 143extern struct cpu_info *cpu_info_list; 144 145#define CPU_INFO_ITERATOR int 146#define CPU_INFO_FOREACH(cii, ci) cii = 0, ci = cpu_info_list; \ 147 ci != NULL; ci = ci->ci_next 148 149#define X86_MAXPROCS 32 /* bitmask; can be bumped to 64 */ 150 151#define CPU_STARTUP(_ci) ((_ci)->ci_func->start(_ci)) 152#define CPU_STOP(_ci) ((_ci)->ci_func->stop(_ci)) 153#define CPU_START_CLEANUP(_ci) ((_ci)->ci_func->cleanup(_ci)) 154 155#if defined(__GNUC__) && defined(_KERNEL) 156static struct cpu_info *x86_curcpu(void); 157static lwp_t *x86_curlwp(void); 158 159__inline static struct cpu_info * __attribute__((__unused__)) 160x86_curcpu(void) 161{ 162 struct cpu_info *ci; 163 164 __asm volatile("movq %%gs:%1, %0" : 165 "=r" (ci) : 166 "m" 167 (*(struct cpu_info * const *)offsetof(struct cpu_info, ci_self))); 168 return ci; 169} 170 171__inline static lwp_t * __attribute__((__unused__)) 172x86_curlwp(void) 173{ 174 lwp_t *l; 175 176 __asm volatile("movq %%gs:%1, %0" : 177 "=r" (l) : 178 "m" 179 (*(struct cpu_info * const *)offsetof(struct cpu_info, ci_curlwp))); 180 return l; 181} 182#else /* __GNUC__ && _KERNEL */ 183/* For non-GCC and LKMs */ 184struct cpu_info *x86_curcpu(void); 185lwp_t *x86_curlwp(void); 186#endif /* __GNUC__ && _KERNEL */ 187 188#define cpu_number() (curcpu()->ci_cpuid) 189 190#define CPU_IS_PRIMARY(ci) ((ci)->ci_flags & CPUF_PRIMARY) 191 192extern struct cpu_info *cpu_info[X86_MAXPROCS]; 193 194void cpu_boot_secondary_processors(void); 195void cpu_init_idle_lwps(void); 196 197#define aston(l) ((l)->l_md.md_astpending = 1) 198 199extern u_int32_t cpus_attached; 200 201#define curcpu() x86_curcpu() 202#define curlwp x86_curlwp() 203#define curpcb (&curlwp->l_addr->u_pcb) 204 205/* 206 * Arguments to hardclock, softclock and statclock 207 * encapsulate the previous machine state in an opaque 208 * clockframe; for now, use generic intrframe. 209 */ 210struct clockframe { 211 struct intrframe cf_if; 212}; 213 214#define CLKF_USERMODE(frame) USERMODE((frame)->cf_if.if_cs, (frame)->cf_if.if_rflags) 215#define CLKF_PC(frame) ((frame)->cf_if.if_rip) 216#define CLKF_INTR(frame) (curcpu()->ci_idepth > 0) 217 218/* 219 * This is used during profiling to integrate system time. It can safely 220 * assume that the process is resident. 221 */ 222#define LWP_PC(l) ((l)->l_md.md_regs->tf_rip) 223 224/* 225 * Give a profiling tick to the current process when the user profiling 226 * buffer pages are invalid. On the i386, request an ast to send us 227 * through trap(), marking the proc as needing a profiling tick. 228 */ 229extern void cpu_need_proftick(struct lwp *); 230 231/* 232 * Notify an LWP that it has a signal pending, process as soon as possible. 233 */ 234extern void cpu_signotify(struct lwp *); 235 236/* 237 * We need a machine-independent name for this. 238 */ 239extern void (*delay_func)(int); 240 241#define DELAY(x) (*delay_func)(x) 242#define delay(x) (*delay_func)(x) 243 244 245/* 246 * pull in #defines for kinds of processors 247 */ 248 249extern int biosbasemem; 250extern int biosextmem; 251extern int cpu; 252extern int cpu_feature; 253extern int cpu_feature2; 254extern int cpu_id; 255extern char cpu_vendor[]; 256extern int cpuid_level; 257 258/* identcpu.c */ 259 260void identifycpu(struct cpu_info *); 261void cpu_probe_features(struct cpu_info *); 262 263/* machdep.c */ 264void dumpconf(void); 265int cpu_maxproc(void); 266void cpu_reset(void); 267void x86_64_proc0_tss_ldt_init(void); 268void x86_64_init_pcb_tss_ldt(struct cpu_info *); 269void cpu_proc_fork(struct proc *, struct proc *); 270 271struct region_descriptor; 272void lgdt(struct region_descriptor *); 273void fillw(short, void *, size_t); 274 275struct pcb; 276void savectx(struct pcb *); 277void lwp_trampoline(void); 278void child_trampoline(void); 279 280/* clock.c */ 281void initrtclock(u_long); 282void startrtclock(void); 283void i8254_delay(int); 284void i8254_microtime(struct timeval *); 285void i8254_initclocks(void); 286 287void cpu_init_msrs(struct cpu_info *); 288 289 290/* vm_machdep.c */ 291int kvtop(void *); 292 293/* trap.c */ 294void child_return(void *); 295 296/* consinit.c */ 297void kgdb_port_init(void); 298 299/* bus_machdep.c */ 300void x86_bus_space_init(void); 301void x86_bus_space_mallocok(void); 302 303#endif /* _KERNEL */ 304 305#include <machine/psl.h> 306 307/* 308 * CTL_MACHDEP definitions. 309 */ 310#define CPU_CONSDEV 1 /* dev_t: console terminal device */ 311#define CPU_BIOSBASEMEM 2 /* int: bios-reported base mem (K) */ 312#define CPU_BIOSEXTMEM 3 /* int: bios-reported ext. mem (K) */ 313#define CPU_NKPDE 4 /* int: number of kernel PDEs */ 314#define CPU_BOOTED_KERNEL 5 /* string: booted kernel name */ 315#define CPU_DISKINFO 6 /* disk geometry information */ 316#define CPU_FPU_PRESENT 7 /* FPU is present */ 317#define CPU_MAXID 8 /* number of valid machdep ids */ 318 319#define CTL_MACHDEP_NAMES { \ 320 { 0, 0 }, \ 321 { "console_device", CTLTYPE_STRUCT }, \ 322 { "biosbasemem", CTLTYPE_INT }, \ 323 { "biosextmem", CTLTYPE_INT }, \ 324 { "nkpde", CTLTYPE_INT }, \ 325 { "booted_kernel", CTLTYPE_STRING }, \ 326 { "diskinfo", CTLTYPE_STRUCT }, \ 327 { "fpu_present", CTLTYPE_INT }, \ 328} 329 330 331/* 332 * Structure for CPU_DISKINFO sysctl call. 333 * XXX this should be somewhere else. 334 */ 335#define MAX_BIOSDISKS 16 336 337struct disklist { 338 int dl_nbiosdisks; /* number of bios disks */ 339 struct biosdisk_info { 340 int bi_dev; /* BIOS device # (0x80 ..) */ 341 int bi_cyl; /* cylinders on disk */ 342 int bi_head; /* heads per track */ 343 int bi_sec; /* sectors per track */ 344 u_int64_t bi_lbasecs; /* total sec. (iff ext13) */ 345#define BIFLAG_INVALID 0x01 346#define BIFLAG_EXTINT13 0x02 347 int bi_flags; 348 } dl_biosdisks[MAX_BIOSDISKS]; 349 350 int dl_nnativedisks; /* number of native disks */ 351 struct nativedisk_info { 352 char ni_devname[16]; /* native device name */ 353 int ni_nmatches; /* # of matches w/ BIOS */ 354 int ni_biosmatches[MAX_BIOSDISKS]; /* indices in dl_biosdisks */ 355 } dl_nativedisks[1]; /* actually longer */ 356}; 357 358#endif /* !_AMD64_CPU_H_ */ 359