machdep.c revision 163219
1/*- 2 * Copyright (c) 2003 Peter Wemm. 3 * Copyright (c) 1992 Terrence R. Lambert. 4 * Copyright (c) 1982, 1987, 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. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * from: @(#)machdep.c 7.4 (Berkeley) 6/3/91 39 */ 40 41#include <sys/cdefs.h> 42__FBSDID("$FreeBSD: head/sys/amd64/amd64/machdep.c 163219 2006-10-10 23:23:12Z jhb $"); 43 44#include "opt_atalk.h" 45#include "opt_atpic.h" 46#include "opt_compat.h" 47#include "opt_cpu.h" 48#include "opt_ddb.h" 49#include "opt_inet.h" 50#include "opt_ipx.h" 51#include "opt_isa.h" 52#include "opt_kstack_pages.h" 53#include "opt_maxmem.h" 54#include "opt_msgbuf.h" 55#include "opt_perfmon.h" 56 57#include <sys/param.h> 58#include <sys/proc.h> 59#include <sys/systm.h> 60#include <sys/bio.h> 61#include <sys/buf.h> 62#include <sys/bus.h> 63#include <sys/callout.h> 64#include <sys/clock.h> 65#include <sys/cons.h> 66#include <sys/cpu.h> 67#include <sys/eventhandler.h> 68#include <sys/exec.h> 69#include <sys/imgact.h> 70#include <sys/kdb.h> 71#include <sys/kernel.h> 72#include <sys/ktr.h> 73#include <sys/linker.h> 74#include <sys/lock.h> 75#include <sys/malloc.h> 76#include <sys/memrange.h> 77#include <sys/msgbuf.h> 78#include <sys/mutex.h> 79#include <sys/pcpu.h> 80#include <sys/ptrace.h> 81#include <sys/reboot.h> 82#include <sys/sched.h> 83#include <sys/signalvar.h> 84#include <sys/sysctl.h> 85#include <sys/sysent.h> 86#include <sys/sysproto.h> 87#include <sys/ucontext.h> 88#include <sys/vmmeter.h> 89 90#include <vm/vm.h> 91#include <vm/vm_extern.h> 92#include <vm/vm_kern.h> 93#include <vm/vm_page.h> 94#include <vm/vm_map.h> 95#include <vm/vm_object.h> 96#include <vm/vm_pager.h> 97#include <vm/vm_param.h> 98 99#ifdef DDB 100#ifndef KDB 101#error KDB must be enabled in order for DDB to work! 102#endif 103#endif 104#include <ddb/ddb.h> 105 106#include <net/netisr.h> 107 108#include <machine/clock.h> 109#include <machine/cpu.h> 110#include <machine/cputypes.h> 111#include <machine/intr_machdep.h> 112#include <machine/md_var.h> 113#include <machine/metadata.h> 114#include <machine/pc/bios.h> 115#include <machine/pcb.h> 116#include <machine/proc.h> 117#include <machine/reg.h> 118#include <machine/sigframe.h> 119#include <machine/specialreg.h> 120#ifdef PERFMON 121#include <machine/perfmon.h> 122#endif 123#include <machine/tss.h> 124#ifdef SMP 125#include <machine/smp.h> 126#endif 127 128#include <dev/ic/i8259.h> 129#include <amd64/isa/icu.h> 130#include <machine/apicvar.h> 131 132#include <isa/isareg.h> 133#include <isa/rtc.h> 134 135/* Sanity check for __curthread() */ 136CTASSERT(offsetof(struct pcpu, pc_curthread) == 0); 137 138extern u_int64_t hammer_time(u_int64_t, u_int64_t); 139extern void dblfault_handler(void); 140 141extern void printcpuinfo(void); /* XXX header file */ 142extern void identify_cpu(void); 143extern void panicifcpuunsupported(void); 144 145#define CS_SECURE(cs) (ISPL(cs) == SEL_UPL) 146#define EFL_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0) 147 148static void cpu_startup(void *); 149static void get_fpcontext(struct thread *td, mcontext_t *mcp); 150static int set_fpcontext(struct thread *td, const mcontext_t *mcp); 151SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL) 152 153#ifdef DDB 154extern vm_offset_t ksym_start, ksym_end; 155#endif 156 157int _udatasel, _ucodesel, _ucode32sel; 158 159int cold = 1; 160 161long Maxmem = 0; 162long realmem = 0; 163 164#define PHYSMAP_SIZE (2 * 30) 165 166vm_paddr_t phys_avail[PHYSMAP_SIZE + 2]; 167vm_paddr_t dump_avail[PHYSMAP_SIZE + 2]; 168 169/* must be 2 less so 0 0 can signal end of chunks */ 170#define PHYS_AVAIL_ARRAY_END ((sizeof(phys_avail) / sizeof(phys_avail[0])) - 2) 171#define DUMP_AVAIL_ARRAY_END ((sizeof(dump_avail) / sizeof(dump_avail[0])) - 2) 172 173struct kva_md_info kmi; 174 175static struct trapframe proc0_tf; 176struct region_descriptor r_gdt, r_idt; 177 178struct pcpu __pcpu[MAXCPU]; 179 180struct mtx icu_lock; 181 182struct mem_range_softc mem_range_softc; 183 184static void 185cpu_startup(dummy) 186 void *dummy; 187{ 188 /* 189 * Good {morning,afternoon,evening,night}. 190 */ 191 startrtclock(); 192 printcpuinfo(); 193 panicifcpuunsupported(); 194#ifdef PERFMON 195 perfmon_init(); 196#endif 197 printf("usable memory = %ju (%ju MB)\n", ptoa((uintmax_t)physmem), 198 ptoa((uintmax_t)physmem) / 1048576); 199 realmem = Maxmem; 200 /* 201 * Display any holes after the first chunk of extended memory. 202 */ 203 if (bootverbose) { 204 int indx; 205 206 printf("Physical memory chunk(s):\n"); 207 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) { 208 vm_paddr_t size; 209 210 size = phys_avail[indx + 1] - phys_avail[indx]; 211 printf( 212 "0x%016jx - 0x%016jx, %ju bytes (%ju pages)\n", 213 (uintmax_t)phys_avail[indx], 214 (uintmax_t)phys_avail[indx + 1] - 1, 215 (uintmax_t)size, (uintmax_t)size / PAGE_SIZE); 216 } 217 } 218 219 vm_ksubmap_init(&kmi); 220 221 printf("avail memory = %ju (%ju MB)\n", 222 ptoa((uintmax_t)cnt.v_free_count), 223 ptoa((uintmax_t)cnt.v_free_count) / 1048576); 224 225 /* 226 * Set up buffers, so they can be used to read disk labels. 227 */ 228 bufinit(); 229 vm_pager_bufferinit(); 230 231 cpu_setregs(); 232} 233 234/* 235 * Send an interrupt to process. 236 * 237 * Stack is set up to allow sigcode stored 238 * at top to call routine, followed by kcall 239 * to sigreturn routine below. After sigreturn 240 * resets the signal mask, the stack, and the 241 * frame pointer, it returns to the user 242 * specified pc, psl. 243 */ 244void 245sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) 246{ 247 struct sigframe sf, *sfp; 248 struct proc *p; 249 struct thread *td; 250 struct sigacts *psp; 251 char *sp; 252 struct trapframe *regs; 253 int sig; 254 int oonstack; 255 256 td = curthread; 257 p = td->td_proc; 258 PROC_LOCK_ASSERT(p, MA_OWNED); 259 sig = ksi->ksi_signo; 260 psp = p->p_sigacts; 261 mtx_assert(&psp->ps_mtx, MA_OWNED); 262 regs = td->td_frame; 263 oonstack = sigonstack(regs->tf_rsp); 264 265 /* Save user context. */ 266 bzero(&sf, sizeof(sf)); 267 sf.sf_uc.uc_sigmask = *mask; 268 sf.sf_uc.uc_stack = td->td_sigstk; 269 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) 270 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 271 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0; 272 bcopy(regs, &sf.sf_uc.uc_mcontext.mc_rdi, sizeof(*regs)); 273 sf.sf_uc.uc_mcontext.mc_len = sizeof(sf.sf_uc.uc_mcontext); /* magic */ 274 get_fpcontext(td, &sf.sf_uc.uc_mcontext); 275 fpstate_drop(td); 276 277 /* Allocate space for the signal handler context. */ 278 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack && 279 SIGISMEMBER(psp->ps_sigonstack, sig)) { 280 sp = td->td_sigstk.ss_sp + 281 td->td_sigstk.ss_size - sizeof(struct sigframe); 282#if defined(COMPAT_43) 283 td->td_sigstk.ss_flags |= SS_ONSTACK; 284#endif 285 } else 286 sp = (char *)regs->tf_rsp - sizeof(struct sigframe) - 128; 287 /* Align to 16 bytes. */ 288 sfp = (struct sigframe *)((unsigned long)sp & ~0xFul); 289 290 /* Translate the signal if appropriate. */ 291 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize) 292 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)]; 293 294 /* Build the argument list for the signal handler. */ 295 regs->tf_rdi = sig; /* arg 1 in %rdi */ 296 regs->tf_rdx = (register_t)&sfp->sf_uc; /* arg 3 in %rdx */ 297 if (SIGISMEMBER(psp->ps_siginfo, sig)) { 298 /* Signal handler installed with SA_SIGINFO. */ 299 regs->tf_rsi = (register_t)&sfp->sf_si; /* arg 2 in %rsi */ 300 sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher; 301 302 /* Fill in POSIX parts */ 303 sf.sf_si = ksi->ksi_info; 304 sf.sf_si.si_signo = sig; /* maybe a translated signal */ 305 regs->tf_rcx = (register_t)ksi->ksi_addr; /* arg 4 in %rcx */ 306 } else { 307 /* Old FreeBSD-style arguments. */ 308 regs->tf_rsi = ksi->ksi_code; /* arg 2 in %rsi */ 309 regs->tf_rcx = (register_t)ksi->ksi_addr; /* arg 4 in %rcx */ 310 sf.sf_ahu.sf_handler = catcher; 311 } 312 mtx_unlock(&psp->ps_mtx); 313 PROC_UNLOCK(p); 314 315 /* 316 * Copy the sigframe out to the user's stack. 317 */ 318 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) { 319#ifdef DEBUG 320 printf("process %ld has trashed its stack\n", (long)p->p_pid); 321#endif 322 PROC_LOCK(p); 323 sigexit(td, SIGILL); 324 } 325 326 regs->tf_rsp = (long)sfp; 327 regs->tf_rip = PS_STRINGS - *(p->p_sysent->sv_szsigcode); 328 regs->tf_rflags &= ~PSL_T; 329 regs->tf_cs = _ucodesel; 330 PROC_LOCK(p); 331 mtx_lock(&psp->ps_mtx); 332} 333 334/* 335 * System call to cleanup state after a signal 336 * has been taken. Reset signal mask and 337 * stack state from context left by sendsig (above). 338 * Return to previous pc and psl as specified by 339 * context left by sendsig. Check carefully to 340 * make sure that the user has not modified the 341 * state to gain improper privileges. 342 * 343 * MPSAFE 344 */ 345int 346sigreturn(td, uap) 347 struct thread *td; 348 struct sigreturn_args /* { 349 const struct __ucontext *sigcntxp; 350 } */ *uap; 351{ 352 ucontext_t uc; 353 struct proc *p = td->td_proc; 354 struct trapframe *regs; 355 const ucontext_t *ucp; 356 long rflags; 357 int cs, error, ret; 358 ksiginfo_t ksi; 359 360 error = copyin(uap->sigcntxp, &uc, sizeof(uc)); 361 if (error != 0) 362 return (error); 363 ucp = &uc; 364 regs = td->td_frame; 365 rflags = ucp->uc_mcontext.mc_rflags; 366 /* 367 * Don't allow users to change privileged or reserved flags. 368 */ 369 /* 370 * XXX do allow users to change the privileged flag PSL_RF. 371 * The cpu sets PSL_RF in tf_rflags for faults. Debuggers 372 * should sometimes set it there too. tf_rflags is kept in 373 * the signal context during signal handling and there is no 374 * other place to remember it, so the PSL_RF bit may be 375 * corrupted by the signal handler without us knowing. 376 * Corruption of the PSL_RF bit at worst causes one more or 377 * one less debugger trap, so allowing it is fairly harmless. 378 */ 379 if (!EFL_SECURE(rflags & ~PSL_RF, regs->tf_rflags & ~PSL_RF)) { 380 printf("sigreturn: rflags = 0x%lx\n", rflags); 381 return (EINVAL); 382 } 383 384 /* 385 * Don't allow users to load a valid privileged %cs. Let the 386 * hardware check for invalid selectors, excess privilege in 387 * other selectors, invalid %eip's and invalid %esp's. 388 */ 389 cs = ucp->uc_mcontext.mc_cs; 390 if (!CS_SECURE(cs)) { 391 printf("sigreturn: cs = 0x%x\n", cs); 392 ksiginfo_init_trap(&ksi); 393 ksi.ksi_signo = SIGBUS; 394 ksi.ksi_code = BUS_OBJERR; 395 ksi.ksi_trapno = T_PROTFLT; 396 ksi.ksi_addr = (void *)regs->tf_rip; 397 trapsignal(td, &ksi); 398 return (EINVAL); 399 } 400 401 ret = set_fpcontext(td, &ucp->uc_mcontext); 402 if (ret != 0) 403 return (ret); 404 bcopy(&ucp->uc_mcontext.mc_rdi, regs, sizeof(*regs)); 405 406 PROC_LOCK(p); 407#if defined(COMPAT_43) 408 if (ucp->uc_mcontext.mc_onstack & 1) 409 td->td_sigstk.ss_flags |= SS_ONSTACK; 410 else 411 td->td_sigstk.ss_flags &= ~SS_ONSTACK; 412#endif 413 414 td->td_sigmask = ucp->uc_sigmask; 415 SIG_CANTMASK(td->td_sigmask); 416 signotify(td); 417 PROC_UNLOCK(p); 418 td->td_pcb->pcb_flags |= PCB_FULLCTX; 419 return (EJUSTRETURN); 420} 421 422#ifdef COMPAT_FREEBSD4 423int 424freebsd4_sigreturn(struct thread *td, struct freebsd4_sigreturn_args *uap) 425{ 426 427 return sigreturn(td, (struct sigreturn_args *)uap); 428} 429#endif 430 431 432/* 433 * Machine dependent boot() routine 434 * 435 * I haven't seen anything to put here yet 436 * Possibly some stuff might be grafted back here from boot() 437 */ 438void 439cpu_boot(int howto) 440{ 441} 442 443/* Get current clock frequency for the given cpu id. */ 444int 445cpu_est_clockrate(int cpu_id, uint64_t *rate) 446{ 447 register_t reg; 448 uint64_t tsc1, tsc2; 449 450 if (pcpu_find(cpu_id) == NULL || rate == NULL) 451 return (EINVAL); 452 453 /* If we're booting, trust the rate calibrated moments ago. */ 454 if (cold) { 455 *rate = tsc_freq; 456 return (0); 457 } 458 459#ifdef SMP 460 /* Schedule ourselves on the indicated cpu. */ 461 mtx_lock_spin(&sched_lock); 462 sched_bind(curthread, cpu_id); 463 mtx_unlock_spin(&sched_lock); 464#endif 465 466 /* Calibrate by measuring a short delay. */ 467 reg = intr_disable(); 468 tsc1 = rdtsc(); 469 DELAY(1000); 470 tsc2 = rdtsc(); 471 intr_restore(reg); 472 473#ifdef SMP 474 mtx_lock_spin(&sched_lock); 475 sched_unbind(curthread); 476 mtx_unlock_spin(&sched_lock); 477#endif 478 479 /* 480 * Calculate the difference in readings, convert to Mhz, and 481 * subtract 0.5% of the total. Empirical testing has shown that 482 * overhead in DELAY() works out to approximately this value. 483 */ 484 tsc2 -= tsc1; 485 *rate = tsc2 * 1000 - tsc2 * 5; 486 return (0); 487} 488 489/* 490 * Shutdown the CPU as much as possible 491 */ 492void 493cpu_halt(void) 494{ 495 for (;;) 496 __asm__ ("hlt"); 497} 498 499/* 500 * Hook to idle the CPU when possible. In the SMP case we default to 501 * off because a halted cpu will not currently pick up a new thread in the 502 * run queue until the next timer tick. If turned on this will result in 503 * approximately a 4.2% loss in real time performance in buildworld tests 504 * (but improves user and sys times oddly enough), and saves approximately 505 * 5% in power consumption on an idle machine (tests w/2xCPU 1.1GHz P3). 506 * 507 * XXX we need to have a cpu mask of idle cpus and generate an IPI or 508 * otherwise generate some sort of interrupt to wake up cpus sitting in HLT. 509 * Then we can have our cake and eat it too. 510 * 511 * XXX I'm turning it on for SMP as well by default for now. It seems to 512 * help lock contention somewhat, and this is critical for HTT. -Peter 513 */ 514static int cpu_idle_hlt = 1; 515SYSCTL_INT(_machdep, OID_AUTO, cpu_idle_hlt, CTLFLAG_RW, 516 &cpu_idle_hlt, 0, "Idle loop HLT enable"); 517 518static void 519cpu_idle_default(void) 520{ 521 /* 522 * we must absolutely guarentee that hlt is the 523 * absolute next instruction after sti or we 524 * introduce a timing window. 525 */ 526 __asm __volatile("sti; hlt"); 527} 528 529/* 530 * Note that we have to be careful here to avoid a race between checking 531 * sched_runnable() and actually halting. If we don't do this, we may waste 532 * the time between calling hlt and the next interrupt even though there 533 * is a runnable process. 534 */ 535void 536cpu_idle(void) 537{ 538 539#ifdef SMP 540 if (mp_grab_cpu_hlt()) 541 return; 542#endif 543 if (cpu_idle_hlt) { 544 disable_intr(); 545 if (sched_runnable()) 546 enable_intr(); 547 else 548 (*cpu_idle_hook)(); 549 } 550} 551 552/* Other subsystems (e.g., ACPI) can hook this later. */ 553void (*cpu_idle_hook)(void) = cpu_idle_default; 554 555/* 556 * Clear registers on exec 557 */ 558void 559exec_setregs(td, entry, stack, ps_strings) 560 struct thread *td; 561 u_long entry; 562 u_long stack; 563 u_long ps_strings; 564{ 565 struct trapframe *regs = td->td_frame; 566 struct pcb *pcb = td->td_pcb; 567 568 critical_enter(); 569 wrmsr(MSR_FSBASE, 0); 570 wrmsr(MSR_KGSBASE, 0); /* User value while we're in the kernel */ 571 pcb->pcb_fsbase = 0; 572 pcb->pcb_gsbase = 0; 573 critical_exit(); 574 load_ds(_udatasel); 575 load_es(_udatasel); 576 load_fs(_udatasel); 577 load_gs(_udatasel); 578 pcb->pcb_ds = _udatasel; 579 pcb->pcb_es = _udatasel; 580 pcb->pcb_fs = _udatasel; 581 pcb->pcb_gs = _udatasel; 582 583 bzero((char *)regs, sizeof(struct trapframe)); 584 regs->tf_rip = entry; 585 regs->tf_rsp = ((stack - 8) & ~0xFul) + 8; 586 regs->tf_rdi = stack; /* argv */ 587 regs->tf_rflags = PSL_USER | (regs->tf_rflags & PSL_T); 588 regs->tf_ss = _udatasel; 589 regs->tf_cs = _ucodesel; 590 591 /* 592 * Reset the hardware debug registers if they were in use. 593 * They won't have any meaning for the newly exec'd process. 594 */ 595 if (pcb->pcb_flags & PCB_DBREGS) { 596 pcb->pcb_dr0 = 0; 597 pcb->pcb_dr1 = 0; 598 pcb->pcb_dr2 = 0; 599 pcb->pcb_dr3 = 0; 600 pcb->pcb_dr6 = 0; 601 pcb->pcb_dr7 = 0; 602 if (pcb == PCPU_GET(curpcb)) { 603 /* 604 * Clear the debug registers on the running 605 * CPU, otherwise they will end up affecting 606 * the next process we switch to. 607 */ 608 reset_dbregs(); 609 } 610 pcb->pcb_flags &= ~PCB_DBREGS; 611 } 612 613 /* 614 * Drop the FP state if we hold it, so that the process gets a 615 * clean FP state if it uses the FPU again. 616 */ 617 fpstate_drop(td); 618} 619 620void 621cpu_setregs(void) 622{ 623 register_t cr0; 624 625 cr0 = rcr0(); 626 /* 627 * CR0_MP, CR0_NE and CR0_TS are also set by npx_probe() for the 628 * BSP. See the comments there about why we set them. 629 */ 630 cr0 |= CR0_MP | CR0_NE | CR0_TS | CR0_WP | CR0_AM; 631 load_cr0(cr0); 632} 633 634/* 635 * Initialize amd64 and configure to run kernel 636 */ 637 638/* 639 * Initialize segments & interrupt table 640 */ 641 642struct user_segment_descriptor gdt[NGDT * MAXCPU];/* global descriptor table */ 643static struct gate_descriptor idt0[NIDT]; 644struct gate_descriptor *idt = &idt0[0]; /* interrupt descriptor table */ 645 646static char dblfault_stack[PAGE_SIZE] __aligned(16); 647 648struct amd64tss common_tss[MAXCPU]; 649 650/* software prototypes -- in more palatable form */ 651struct soft_segment_descriptor gdt_segs[] = { 652/* GNULL_SEL 0 Null Descriptor */ 653{ 0x0, /* segment base address */ 654 0x0, /* length */ 655 0, /* segment type */ 656 0, /* segment descriptor priority level */ 657 0, /* segment descriptor present */ 658 0, /* long */ 659 0, /* default 32 vs 16 bit size */ 660 0 /* limit granularity (byte/page units)*/ }, 661/* GCODE_SEL 1 Code Descriptor for kernel */ 662{ 0x0, /* segment base address */ 663 0xfffff, /* length - all address space */ 664 SDT_MEMERA, /* segment type */ 665 SEL_KPL, /* segment descriptor priority level */ 666 1, /* segment descriptor present */ 667 1, /* long */ 668 0, /* default 32 vs 16 bit size */ 669 1 /* limit granularity (byte/page units)*/ }, 670/* GDATA_SEL 2 Data Descriptor for kernel */ 671{ 0x0, /* segment base address */ 672 0xfffff, /* length - all address space */ 673 SDT_MEMRWA, /* segment type */ 674 SEL_KPL, /* segment descriptor priority level */ 675 1, /* segment descriptor present */ 676 1, /* long */ 677 0, /* default 32 vs 16 bit size */ 678 1 /* limit granularity (byte/page units)*/ }, 679/* GUCODE32_SEL 3 32 bit Code Descriptor for user */ 680{ 0x0, /* segment base address */ 681 0xfffff, /* length - all address space */ 682 SDT_MEMERA, /* segment type */ 683 SEL_UPL, /* segment descriptor priority level */ 684 1, /* segment descriptor present */ 685 0, /* long */ 686 1, /* default 32 vs 16 bit size */ 687 1 /* limit granularity (byte/page units)*/ }, 688/* GUDATA_SEL 4 32/64 bit Data Descriptor for user */ 689{ 0x0, /* segment base address */ 690 0xfffff, /* length - all address space */ 691 SDT_MEMRWA, /* segment type */ 692 SEL_UPL, /* segment descriptor priority level */ 693 1, /* segment descriptor present */ 694 0, /* long */ 695 1, /* default 32 vs 16 bit size */ 696 1 /* limit granularity (byte/page units)*/ }, 697/* GUCODE_SEL 5 64 bit Code Descriptor for user */ 698{ 0x0, /* segment base address */ 699 0xfffff, /* length - all address space */ 700 SDT_MEMERA, /* segment type */ 701 SEL_UPL, /* segment descriptor priority level */ 702 1, /* segment descriptor present */ 703 1, /* long */ 704 0, /* default 32 vs 16 bit size */ 705 1 /* limit granularity (byte/page units)*/ }, 706/* GPROC0_SEL 6 Proc 0 Tss Descriptor */ 707{ 708 0x0, /* segment base address */ 709 sizeof(struct amd64tss)-1,/* length - all address space */ 710 SDT_SYSTSS, /* segment type */ 711 SEL_KPL, /* segment descriptor priority level */ 712 1, /* segment descriptor present */ 713 0, /* long */ 714 0, /* unused - default 32 vs 16 bit size */ 715 0 /* limit granularity (byte/page units)*/ }, 716/* Actually, the TSS is a system descriptor which is double size */ 717{ 0x0, /* segment base address */ 718 0x0, /* length */ 719 0, /* segment type */ 720 0, /* segment descriptor priority level */ 721 0, /* segment descriptor present */ 722 0, /* long */ 723 0, /* default 32 vs 16 bit size */ 724 0 /* limit granularity (byte/page units)*/ }, 725}; 726 727void 728setidt(idx, func, typ, dpl, ist) 729 int idx; 730 inthand_t *func; 731 int typ; 732 int dpl; 733 int ist; 734{ 735 struct gate_descriptor *ip; 736 737 ip = idt + idx; 738 ip->gd_looffset = (uintptr_t)func; 739 ip->gd_selector = GSEL(GCODE_SEL, SEL_KPL); 740 ip->gd_ist = ist; 741 ip->gd_xx = 0; 742 ip->gd_type = typ; 743 ip->gd_dpl = dpl; 744 ip->gd_p = 1; 745 ip->gd_hioffset = ((uintptr_t)func)>>16 ; 746} 747 748#define IDTVEC(name) __CONCAT(X,name) 749 750extern inthand_t 751 IDTVEC(div), IDTVEC(dbg), IDTVEC(nmi), IDTVEC(bpt), IDTVEC(ofl), 752 IDTVEC(bnd), IDTVEC(ill), IDTVEC(dna), IDTVEC(fpusegm), 753 IDTVEC(tss), IDTVEC(missing), IDTVEC(stk), IDTVEC(prot), 754 IDTVEC(page), IDTVEC(mchk), IDTVEC(rsvd), IDTVEC(fpu), IDTVEC(align), 755 IDTVEC(xmm), IDTVEC(dblfault), 756 IDTVEC(fast_syscall), IDTVEC(fast_syscall32); 757 758void 759sdtossd(sd, ssd) 760 struct user_segment_descriptor *sd; 761 struct soft_segment_descriptor *ssd; 762{ 763 764 ssd->ssd_base = (sd->sd_hibase << 24) | sd->sd_lobase; 765 ssd->ssd_limit = (sd->sd_hilimit << 16) | sd->sd_lolimit; 766 ssd->ssd_type = sd->sd_type; 767 ssd->ssd_dpl = sd->sd_dpl; 768 ssd->ssd_p = sd->sd_p; 769 ssd->ssd_long = sd->sd_long; 770 ssd->ssd_def32 = sd->sd_def32; 771 ssd->ssd_gran = sd->sd_gran; 772} 773 774void 775ssdtosd(ssd, sd) 776 struct soft_segment_descriptor *ssd; 777 struct user_segment_descriptor *sd; 778{ 779 780 sd->sd_lobase = (ssd->ssd_base) & 0xffffff; 781 sd->sd_hibase = (ssd->ssd_base >> 24) & 0xff; 782 sd->sd_lolimit = (ssd->ssd_limit) & 0xffff; 783 sd->sd_hilimit = (ssd->ssd_limit >> 16) & 0xf; 784 sd->sd_type = ssd->ssd_type; 785 sd->sd_dpl = ssd->ssd_dpl; 786 sd->sd_p = ssd->ssd_p; 787 sd->sd_long = ssd->ssd_long; 788 sd->sd_def32 = ssd->ssd_def32; 789 sd->sd_gran = ssd->ssd_gran; 790} 791 792void 793ssdtosyssd(ssd, sd) 794 struct soft_segment_descriptor *ssd; 795 struct system_segment_descriptor *sd; 796{ 797 798 sd->sd_lobase = (ssd->ssd_base) & 0xffffff; 799 sd->sd_hibase = (ssd->ssd_base >> 24) & 0xfffffffffful; 800 sd->sd_lolimit = (ssd->ssd_limit) & 0xffff; 801 sd->sd_hilimit = (ssd->ssd_limit >> 16) & 0xf; 802 sd->sd_type = ssd->ssd_type; 803 sd->sd_dpl = ssd->ssd_dpl; 804 sd->sd_p = ssd->ssd_p; 805 sd->sd_gran = ssd->ssd_gran; 806} 807 808#if !defined(DEV_ATPIC) && defined(DEV_ISA) 809#include <isa/isavar.h> 810u_int 811isa_irq_pending(void) 812{ 813 814 return (0); 815} 816#endif 817 818u_int basemem; 819 820/* 821 * Populate the (physmap) array with base/bound pairs describing the 822 * available physical memory in the system, then test this memory and 823 * build the phys_avail array describing the actually-available memory. 824 * 825 * If we cannot accurately determine the physical memory map, then use 826 * value from the 0xE801 call, and failing that, the RTC. 827 * 828 * Total memory size may be set by the kernel environment variable 829 * hw.physmem or the compile-time define MAXMEM. 830 * 831 * XXX first should be vm_paddr_t. 832 */ 833static void 834getmemsize(caddr_t kmdp, u_int64_t first) 835{ 836 int i, physmap_idx, pa_indx, da_indx; 837 vm_paddr_t pa, physmap[PHYSMAP_SIZE]; 838 u_long physmem_tunable; 839 pt_entry_t *pte; 840 struct bios_smap *smapbase, *smap, *smapend; 841 u_int32_t smapsize; 842 quad_t dcons_addr, dcons_size; 843 844 bzero(physmap, sizeof(physmap)); 845 basemem = 0; 846 physmap_idx = 0; 847 848 /* 849 * get memory map from INT 15:E820, kindly supplied by the loader. 850 * 851 * subr_module.c says: 852 * "Consumer may safely assume that size value precedes data." 853 * ie: an int32_t immediately precedes smap. 854 */ 855 smapbase = (struct bios_smap *)preload_search_info(kmdp, 856 MODINFO_METADATA | MODINFOMD_SMAP); 857 if (smapbase == NULL) 858 panic("No BIOS smap info from loader!"); 859 860 smapsize = *((u_int32_t *)smapbase - 1); 861 smapend = (struct bios_smap *)((uintptr_t)smapbase + smapsize); 862 863 for (smap = smapbase; smap < smapend; smap++) { 864 if (boothowto & RB_VERBOSE) 865 printf("SMAP type=%02x base=%016lx len=%016lx\n", 866 smap->type, smap->base, smap->length); 867 868 if (smap->type != 0x01) 869 continue; 870 871 if (smap->length == 0) 872 continue; 873 874 for (i = 0; i <= physmap_idx; i += 2) { 875 if (smap->base < physmap[i + 1]) { 876 if (boothowto & RB_VERBOSE) 877 printf( 878 "Overlapping or non-montonic memory region, ignoring second region\n"); 879 continue; 880 } 881 } 882 883 if (smap->base == physmap[physmap_idx + 1]) { 884 physmap[physmap_idx + 1] += smap->length; 885 continue; 886 } 887 888 physmap_idx += 2; 889 if (physmap_idx == PHYSMAP_SIZE) { 890 printf( 891 "Too many segments in the physical address map, giving up\n"); 892 break; 893 } 894 physmap[physmap_idx] = smap->base; 895 physmap[physmap_idx + 1] = smap->base + smap->length; 896 } 897 898 /* 899 * Find the 'base memory' segment for SMP 900 */ 901 basemem = 0; 902 for (i = 0; i <= physmap_idx; i += 2) { 903 if (physmap[i] == 0x00000000) { 904 basemem = physmap[i + 1] / 1024; 905 break; 906 } 907 } 908 if (basemem == 0) 909 panic("BIOS smap did not include a basemem segment!"); 910 911#ifdef SMP 912 /* make hole for AP bootstrap code */ 913 physmap[1] = mp_bootaddress(physmap[1] / 1024); 914#endif 915 916 /* 917 * Maxmem isn't the "maximum memory", it's one larger than the 918 * highest page of the physical address space. It should be 919 * called something like "Maxphyspage". We may adjust this 920 * based on ``hw.physmem'' and the results of the memory test. 921 */ 922 Maxmem = atop(physmap[physmap_idx + 1]); 923 924#ifdef MAXMEM 925 Maxmem = MAXMEM / 4; 926#endif 927 928 if (TUNABLE_ULONG_FETCH("hw.physmem", &physmem_tunable)) 929 Maxmem = atop(physmem_tunable); 930 931 /* 932 * Don't allow MAXMEM or hw.physmem to extend the amount of memory 933 * in the system. 934 */ 935 if (Maxmem > atop(physmap[physmap_idx + 1])) 936 Maxmem = atop(physmap[physmap_idx + 1]); 937 938 if (atop(physmap[physmap_idx + 1]) != Maxmem && 939 (boothowto & RB_VERBOSE)) 940 printf("Physical memory use set to %ldK\n", Maxmem * 4); 941 942 /* call pmap initialization to make new kernel address space */ 943 pmap_bootstrap(&first); 944 945 /* 946 * Size up each available chunk of physical memory. 947 */ 948 physmap[0] = PAGE_SIZE; /* mask off page 0 */ 949 pa_indx = 0; 950 da_indx = 1; 951 phys_avail[pa_indx++] = physmap[0]; 952 phys_avail[pa_indx] = physmap[0]; 953 dump_avail[da_indx] = physmap[0]; 954 pte = CMAP1; 955 956 /* 957 * Get dcons buffer address 958 */ 959 if (getenv_quad("dcons.addr", &dcons_addr) == 0 || 960 getenv_quad("dcons.size", &dcons_size) == 0) 961 dcons_addr = 0; 962 963 /* 964 * physmap is in bytes, so when converting to page boundaries, 965 * round up the start address and round down the end address. 966 */ 967 for (i = 0; i <= physmap_idx; i += 2) { 968 vm_paddr_t end; 969 970 end = ptoa((vm_paddr_t)Maxmem); 971 if (physmap[i + 1] < end) 972 end = trunc_page(physmap[i + 1]); 973 for (pa = round_page(physmap[i]); pa < end; pa += PAGE_SIZE) { 974 int tmp, page_bad, full; 975 int *ptr = (int *)CADDR1; 976 977 full = FALSE; 978 /* 979 * block out kernel memory as not available. 980 */ 981 if (pa >= 0x100000 && pa < first) 982 goto do_dump_avail; 983 984 /* 985 * block out dcons buffer 986 */ 987 if (dcons_addr > 0 988 && pa >= trunc_page(dcons_addr) 989 && pa < dcons_addr + dcons_size) 990 goto do_dump_avail; 991 992 page_bad = FALSE; 993 994 /* 995 * map page into kernel: valid, read/write,non-cacheable 996 */ 997 *pte = pa | PG_V | PG_RW | PG_N; 998 invltlb(); 999 1000 tmp = *(int *)ptr; 1001 /* 1002 * Test for alternating 1's and 0's 1003 */ 1004 *(volatile int *)ptr = 0xaaaaaaaa; 1005 if (*(volatile int *)ptr != 0xaaaaaaaa) 1006 page_bad = TRUE; 1007 /* 1008 * Test for alternating 0's and 1's 1009 */ 1010 *(volatile int *)ptr = 0x55555555; 1011 if (*(volatile int *)ptr != 0x55555555) 1012 page_bad = TRUE; 1013 /* 1014 * Test for all 1's 1015 */ 1016 *(volatile int *)ptr = 0xffffffff; 1017 if (*(volatile int *)ptr != 0xffffffff) 1018 page_bad = TRUE; 1019 /* 1020 * Test for all 0's 1021 */ 1022 *(volatile int *)ptr = 0x0; 1023 if (*(volatile int *)ptr != 0x0) 1024 page_bad = TRUE; 1025 /* 1026 * Restore original value. 1027 */ 1028 *(int *)ptr = tmp; 1029 1030 /* 1031 * Adjust array of valid/good pages. 1032 */ 1033 if (page_bad == TRUE) 1034 continue; 1035 /* 1036 * If this good page is a continuation of the 1037 * previous set of good pages, then just increase 1038 * the end pointer. Otherwise start a new chunk. 1039 * Note that "end" points one higher than end, 1040 * making the range >= start and < end. 1041 * If we're also doing a speculative memory 1042 * test and we at or past the end, bump up Maxmem 1043 * so that we keep going. The first bad page 1044 * will terminate the loop. 1045 */ 1046 if (phys_avail[pa_indx] == pa) { 1047 phys_avail[pa_indx] += PAGE_SIZE; 1048 } else { 1049 pa_indx++; 1050 if (pa_indx == PHYS_AVAIL_ARRAY_END) { 1051 printf( 1052 "Too many holes in the physical address space, giving up\n"); 1053 pa_indx--; 1054 full = TRUE; 1055 goto do_dump_avail; 1056 } 1057 phys_avail[pa_indx++] = pa; /* start */ 1058 phys_avail[pa_indx] = pa + PAGE_SIZE; /* end */ 1059 } 1060 physmem++; 1061do_dump_avail: 1062 if (dump_avail[da_indx] == pa) { 1063 dump_avail[da_indx] += PAGE_SIZE; 1064 } else { 1065 da_indx++; 1066 if (da_indx == DUMP_AVAIL_ARRAY_END) { 1067 da_indx--; 1068 goto do_next; 1069 } 1070 dump_avail[da_indx++] = pa; /* start */ 1071 dump_avail[da_indx] = pa + PAGE_SIZE; /* end */ 1072 } 1073do_next: 1074 if (full) 1075 break; 1076 } 1077 } 1078 *pte = 0; 1079 invltlb(); 1080 1081 /* 1082 * XXX 1083 * The last chunk must contain at least one page plus the message 1084 * buffer to avoid complicating other code (message buffer address 1085 * calculation, etc.). 1086 */ 1087 while (phys_avail[pa_indx - 1] + PAGE_SIZE + 1088 round_page(MSGBUF_SIZE) >= phys_avail[pa_indx]) { 1089 physmem -= atop(phys_avail[pa_indx] - phys_avail[pa_indx - 1]); 1090 phys_avail[pa_indx--] = 0; 1091 phys_avail[pa_indx--] = 0; 1092 } 1093 1094 Maxmem = atop(phys_avail[pa_indx]); 1095 1096 /* Trim off space for the message buffer. */ 1097 phys_avail[pa_indx] -= round_page(MSGBUF_SIZE); 1098 1099 avail_end = phys_avail[pa_indx]; 1100} 1101 1102u_int64_t 1103hammer_time(u_int64_t modulep, u_int64_t physfree) 1104{ 1105 caddr_t kmdp; 1106 int gsel_tss, off, x; 1107 struct pcpu *pc; 1108 u_int64_t msr; 1109 char *env; 1110 1111 thread0.td_kstack = physfree + KERNBASE; 1112 bzero((void *)thread0.td_kstack, KSTACK_PAGES * PAGE_SIZE); 1113 physfree += KSTACK_PAGES * PAGE_SIZE; 1114 thread0.td_pcb = (struct pcb *) 1115 (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1; 1116 1117 /* 1118 * This may be done better later if it gets more high level 1119 * components in it. If so just link td->td_proc here. 1120 */ 1121 proc_linkup(&proc0, &ksegrp0, &thread0); 1122 1123 preload_metadata = (caddr_t)(uintptr_t)(modulep + KERNBASE); 1124 preload_bootstrap_relocate(KERNBASE); 1125 kmdp = preload_search_by_type("elf kernel"); 1126 if (kmdp == NULL) 1127 kmdp = preload_search_by_type("elf64 kernel"); 1128 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int); 1129 kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *) + KERNBASE; 1130#ifdef DDB 1131 ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t); 1132 ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t); 1133#endif 1134 1135 /* Init basic tunables, hz etc */ 1136 init_param1(); 1137 1138 /* 1139 * make gdt memory segments 1140 */ 1141 gdt_segs[GPROC0_SEL].ssd_base = (uintptr_t)&common_tss[0]; 1142 1143 for (x = 0; x < NGDT; x++) { 1144 if (x != GPROC0_SEL && x != (GPROC0_SEL + 1)) 1145 ssdtosd(&gdt_segs[x], &gdt[x]); 1146 } 1147 ssdtosyssd(&gdt_segs[GPROC0_SEL], 1148 (struct system_segment_descriptor *)&gdt[GPROC0_SEL]); 1149 1150 r_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1; 1151 r_gdt.rd_base = (long) gdt; 1152 lgdt(&r_gdt); 1153 pc = &__pcpu[0]; 1154 1155 wrmsr(MSR_FSBASE, 0); /* User value */ 1156 wrmsr(MSR_GSBASE, (u_int64_t)pc); 1157 wrmsr(MSR_KGSBASE, 0); /* User value while in the kernel */ 1158 1159 pcpu_init(pc, 0, sizeof(struct pcpu)); 1160 PCPU_SET(prvspace, pc); 1161 PCPU_SET(curthread, &thread0); 1162 PCPU_SET(curpcb, thread0.td_pcb); 1163 PCPU_SET(tssp, &common_tss[0]); 1164 1165 /* 1166 * Initialize mutexes. 1167 * 1168 * icu_lock: in order to allow an interrupt to occur in a critical 1169 * section, to set pcpu->ipending (etc...) properly, we 1170 * must be able to get the icu lock, so it can't be 1171 * under witness. 1172 */ 1173 mutex_init(); 1174 mtx_init(&clock_lock, "clk", NULL, MTX_SPIN); 1175 mtx_init(&icu_lock, "icu", NULL, MTX_SPIN | MTX_NOWITNESS); 1176 1177 /* exceptions */ 1178 for (x = 0; x < NIDT; x++) 1179 setidt(x, &IDTVEC(rsvd), SDT_SYSIGT, SEL_KPL, 0); 1180 setidt(IDT_DE, &IDTVEC(div), SDT_SYSIGT, SEL_KPL, 0); 1181 setidt(IDT_DB, &IDTVEC(dbg), SDT_SYSIGT, SEL_KPL, 0); 1182 setidt(IDT_NMI, &IDTVEC(nmi), SDT_SYSIGT, SEL_KPL, 0); 1183 setidt(IDT_BP, &IDTVEC(bpt), SDT_SYSIGT, SEL_UPL, 0); 1184 setidt(IDT_OF, &IDTVEC(ofl), SDT_SYSIGT, SEL_KPL, 0); 1185 setidt(IDT_BR, &IDTVEC(bnd), SDT_SYSIGT, SEL_KPL, 0); 1186 setidt(IDT_UD, &IDTVEC(ill), SDT_SYSIGT, SEL_KPL, 0); 1187 setidt(IDT_NM, &IDTVEC(dna), SDT_SYSIGT, SEL_KPL, 0); 1188 setidt(IDT_DF, &IDTVEC(dblfault), SDT_SYSIGT, SEL_KPL, 1); 1189 setidt(IDT_FPUGP, &IDTVEC(fpusegm), SDT_SYSIGT, SEL_KPL, 0); 1190 setidt(IDT_TS, &IDTVEC(tss), SDT_SYSIGT, SEL_KPL, 0); 1191 setidt(IDT_NP, &IDTVEC(missing), SDT_SYSIGT, SEL_KPL, 0); 1192 setidt(IDT_SS, &IDTVEC(stk), SDT_SYSIGT, SEL_KPL, 0); 1193 setidt(IDT_GP, &IDTVEC(prot), SDT_SYSIGT, SEL_KPL, 0); 1194 setidt(IDT_PF, &IDTVEC(page), SDT_SYSIGT, SEL_KPL, 0); 1195 setidt(IDT_MF, &IDTVEC(fpu), SDT_SYSIGT, SEL_KPL, 0); 1196 setidt(IDT_AC, &IDTVEC(align), SDT_SYSIGT, SEL_KPL, 0); 1197 setidt(IDT_MC, &IDTVEC(mchk), SDT_SYSIGT, SEL_KPL, 0); 1198 setidt(IDT_XF, &IDTVEC(xmm), SDT_SYSIGT, SEL_KPL, 0); 1199 1200 r_idt.rd_limit = sizeof(idt0) - 1; 1201 r_idt.rd_base = (long) idt; 1202 lidt(&r_idt); 1203 1204 /* 1205 * Initialize the console before we print anything out. 1206 */ 1207 cninit(); 1208 1209#ifdef DEV_ISA 1210#ifdef DEV_ATPIC 1211 elcr_probe(); 1212 atpic_startup(); 1213#else 1214 /* Reset and mask the atpics and leave them shut down. */ 1215 atpic_reset(); 1216 1217 /* 1218 * Point the ICU spurious interrupt vectors at the APIC spurious 1219 * interrupt handler. 1220 */ 1221 setidt(IDT_IO_INTS + 7, IDTVEC(spuriousint), SDT_SYSIGT, SEL_KPL, 0); 1222 setidt(IDT_IO_INTS + 15, IDTVEC(spuriousint), SDT_SYSIGT, SEL_KPL, 0); 1223#endif 1224#else 1225#error "have you forgotten the isa device?"; 1226#endif 1227 1228 kdb_init(); 1229 1230#ifdef KDB 1231 if (boothowto & RB_KDB) 1232 kdb_enter("Boot flags requested debugger"); 1233#endif 1234 1235 identify_cpu(); /* Final stage of CPU initialization */ 1236 initializecpu(); /* Initialize CPU registers */ 1237 1238 /* make an initial tss so cpu can get interrupt stack on syscall! */ 1239 common_tss[0].tss_rsp0 = thread0.td_kstack + \ 1240 KSTACK_PAGES * PAGE_SIZE - sizeof(struct pcb); 1241 /* Ensure the stack is aligned to 16 bytes */ 1242 common_tss[0].tss_rsp0 &= ~0xFul; 1243 PCPU_SET(rsp0, common_tss[0].tss_rsp0); 1244 1245 /* doublefault stack space, runs on ist1 */ 1246 common_tss[0].tss_ist1 = (long)&dblfault_stack[sizeof(dblfault_stack)]; 1247 1248 /* Set the IO permission bitmap (empty due to tss seg limit) */ 1249 common_tss[0].tss_iobase = sizeof(struct amd64tss); 1250 1251 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL); 1252 ltr(gsel_tss); 1253 1254 /* Set up the fast syscall stuff */ 1255 msr = rdmsr(MSR_EFER) | EFER_SCE; 1256 wrmsr(MSR_EFER, msr); 1257 wrmsr(MSR_LSTAR, (u_int64_t)IDTVEC(fast_syscall)); 1258 wrmsr(MSR_CSTAR, (u_int64_t)IDTVEC(fast_syscall32)); 1259 msr = ((u_int64_t)GSEL(GCODE_SEL, SEL_KPL) << 32) | 1260 ((u_int64_t)GSEL(GUCODE32_SEL, SEL_UPL) << 48); 1261 wrmsr(MSR_STAR, msr); 1262 wrmsr(MSR_SF_MASK, PSL_NT|PSL_T|PSL_I|PSL_C|PSL_D); 1263 1264 getmemsize(kmdp, physfree); 1265 init_param2(physmem); 1266 1267 /* now running on new page tables, configured,and u/iom is accessible */ 1268 1269 /* Map the message buffer. */ 1270 for (off = 0; off < round_page(MSGBUF_SIZE); off += PAGE_SIZE) 1271 pmap_kenter((vm_offset_t)msgbufp + off, avail_end + off); 1272 1273 msgbufinit(msgbufp, MSGBUF_SIZE); 1274 fpuinit(); 1275 1276 /* transfer to user mode */ 1277 1278 _ucodesel = GSEL(GUCODE_SEL, SEL_UPL); 1279 _udatasel = GSEL(GUDATA_SEL, SEL_UPL); 1280 _ucode32sel = GSEL(GUCODE32_SEL, SEL_UPL); 1281 1282 /* setup proc 0's pcb */ 1283 thread0.td_pcb->pcb_flags = 0; /* XXXKSE */ 1284 thread0.td_pcb->pcb_cr3 = KPML4phys; 1285 thread0.td_frame = &proc0_tf; 1286 1287 env = getenv("kernelname"); 1288 if (env != NULL) 1289 strlcpy(kernelname, env, sizeof(kernelname)); 1290 1291 /* Location of kernel stack for locore */ 1292 return ((u_int64_t)thread0.td_pcb); 1293} 1294 1295void 1296cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size) 1297{ 1298 1299 pcpu->pc_acpi_id = 0xffffffff; 1300} 1301 1302void 1303spinlock_enter(void) 1304{ 1305 struct thread *td; 1306 1307 td = curthread; 1308 if (td->td_md.md_spinlock_count == 0) 1309 td->td_md.md_saved_flags = intr_disable(); 1310 td->td_md.md_spinlock_count++; 1311 critical_enter(); 1312} 1313 1314void 1315spinlock_exit(void) 1316{ 1317 struct thread *td; 1318 1319 td = curthread; 1320 critical_exit(); 1321 td->td_md.md_spinlock_count--; 1322 if (td->td_md.md_spinlock_count == 0) 1323 intr_restore(td->td_md.md_saved_flags); 1324} 1325 1326/* 1327 * Construct a PCB from a trapframe. This is called from kdb_trap() where 1328 * we want to start a backtrace from the function that caused us to enter 1329 * the debugger. We have the context in the trapframe, but base the trace 1330 * on the PCB. The PCB doesn't have to be perfect, as long as it contains 1331 * enough for a backtrace. 1332 */ 1333void 1334makectx(struct trapframe *tf, struct pcb *pcb) 1335{ 1336 1337 pcb->pcb_r12 = tf->tf_r12; 1338 pcb->pcb_r13 = tf->tf_r13; 1339 pcb->pcb_r14 = tf->tf_r14; 1340 pcb->pcb_r15 = tf->tf_r15; 1341 pcb->pcb_rbp = tf->tf_rbp; 1342 pcb->pcb_rbx = tf->tf_rbx; 1343 pcb->pcb_rip = tf->tf_rip; 1344 pcb->pcb_rsp = (ISPL(tf->tf_cs)) ? tf->tf_rsp : (long)(tf + 1) - 8; 1345} 1346 1347int 1348ptrace_set_pc(struct thread *td, unsigned long addr) 1349{ 1350 td->td_frame->tf_rip = addr; 1351 return (0); 1352} 1353 1354int 1355ptrace_single_step(struct thread *td) 1356{ 1357 td->td_frame->tf_rflags |= PSL_T; 1358 return (0); 1359} 1360 1361int 1362ptrace_clear_single_step(struct thread *td) 1363{ 1364 td->td_frame->tf_rflags &= ~PSL_T; 1365 return (0); 1366} 1367 1368int 1369fill_regs(struct thread *td, struct reg *regs) 1370{ 1371 struct trapframe *tp; 1372 1373 tp = td->td_frame; 1374 regs->r_r15 = tp->tf_r15; 1375 regs->r_r14 = tp->tf_r14; 1376 regs->r_r13 = tp->tf_r13; 1377 regs->r_r12 = tp->tf_r12; 1378 regs->r_r11 = tp->tf_r11; 1379 regs->r_r10 = tp->tf_r10; 1380 regs->r_r9 = tp->tf_r9; 1381 regs->r_r8 = tp->tf_r8; 1382 regs->r_rdi = tp->tf_rdi; 1383 regs->r_rsi = tp->tf_rsi; 1384 regs->r_rbp = tp->tf_rbp; 1385 regs->r_rbx = tp->tf_rbx; 1386 regs->r_rdx = tp->tf_rdx; 1387 regs->r_rcx = tp->tf_rcx; 1388 regs->r_rax = tp->tf_rax; 1389 regs->r_rip = tp->tf_rip; 1390 regs->r_cs = tp->tf_cs; 1391 regs->r_rflags = tp->tf_rflags; 1392 regs->r_rsp = tp->tf_rsp; 1393 regs->r_ss = tp->tf_ss; 1394 return (0); 1395} 1396 1397int 1398set_regs(struct thread *td, struct reg *regs) 1399{ 1400 struct trapframe *tp; 1401 register_t rflags; 1402 1403 tp = td->td_frame; 1404 rflags = regs->r_rflags & 0xffffffff; 1405 if (!EFL_SECURE(rflags, tp->tf_rflags) || !CS_SECURE(regs->r_cs)) 1406 return (EINVAL); 1407 tp->tf_r15 = regs->r_r15; 1408 tp->tf_r14 = regs->r_r14; 1409 tp->tf_r13 = regs->r_r13; 1410 tp->tf_r12 = regs->r_r12; 1411 tp->tf_r11 = regs->r_r11; 1412 tp->tf_r10 = regs->r_r10; 1413 tp->tf_r9 = regs->r_r9; 1414 tp->tf_r8 = regs->r_r8; 1415 tp->tf_rdi = regs->r_rdi; 1416 tp->tf_rsi = regs->r_rsi; 1417 tp->tf_rbp = regs->r_rbp; 1418 tp->tf_rbx = regs->r_rbx; 1419 tp->tf_rdx = regs->r_rdx; 1420 tp->tf_rcx = regs->r_rcx; 1421 tp->tf_rax = regs->r_rax; 1422 tp->tf_rip = regs->r_rip; 1423 tp->tf_cs = regs->r_cs; 1424 tp->tf_rflags = rflags; 1425 tp->tf_rsp = regs->r_rsp; 1426 tp->tf_ss = regs->r_ss; 1427 td->td_pcb->pcb_flags |= PCB_FULLCTX; 1428 return (0); 1429} 1430 1431/* XXX check all this stuff! */ 1432/* externalize from sv_xmm */ 1433static void 1434fill_fpregs_xmm(struct savefpu *sv_xmm, struct fpreg *fpregs) 1435{ 1436 struct envxmm *penv_fpreg = (struct envxmm *)&fpregs->fpr_env; 1437 struct envxmm *penv_xmm = &sv_xmm->sv_env; 1438 int i; 1439 1440 /* pcb -> fpregs */ 1441 bzero(fpregs, sizeof(*fpregs)); 1442 1443 /* FPU control/status */ 1444 penv_fpreg->en_cw = penv_xmm->en_cw; 1445 penv_fpreg->en_sw = penv_xmm->en_sw; 1446 penv_fpreg->en_tw = penv_xmm->en_tw; 1447 penv_fpreg->en_opcode = penv_xmm->en_opcode; 1448 penv_fpreg->en_rip = penv_xmm->en_rip; 1449 penv_fpreg->en_rdp = penv_xmm->en_rdp; 1450 penv_fpreg->en_mxcsr = penv_xmm->en_mxcsr; 1451 penv_fpreg->en_mxcsr_mask = penv_xmm->en_mxcsr_mask; 1452 1453 /* FPU registers */ 1454 for (i = 0; i < 8; ++i) 1455 bcopy(sv_xmm->sv_fp[i].fp_acc.fp_bytes, fpregs->fpr_acc[i], 10); 1456 1457 /* SSE registers */ 1458 for (i = 0; i < 16; ++i) 1459 bcopy(sv_xmm->sv_xmm[i].xmm_bytes, fpregs->fpr_xacc[i], 16); 1460} 1461 1462/* internalize from fpregs into sv_xmm */ 1463static void 1464set_fpregs_xmm(struct fpreg *fpregs, struct savefpu *sv_xmm) 1465{ 1466 struct envxmm *penv_xmm = &sv_xmm->sv_env; 1467 struct envxmm *penv_fpreg = (struct envxmm *)&fpregs->fpr_env; 1468 int i; 1469 1470 /* fpregs -> pcb */ 1471 /* FPU control/status */ 1472 penv_xmm->en_cw = penv_fpreg->en_cw; 1473 penv_xmm->en_sw = penv_fpreg->en_sw; 1474 penv_xmm->en_tw = penv_fpreg->en_tw; 1475 penv_xmm->en_opcode = penv_fpreg->en_opcode; 1476 penv_xmm->en_rip = penv_fpreg->en_rip; 1477 penv_xmm->en_rdp = penv_fpreg->en_rdp; 1478 penv_xmm->en_mxcsr = penv_fpreg->en_mxcsr; 1479 penv_xmm->en_mxcsr_mask = penv_fpreg->en_mxcsr_mask & cpu_mxcsr_mask; 1480 1481 /* FPU registers */ 1482 for (i = 0; i < 8; ++i) 1483 bcopy(fpregs->fpr_acc[i], sv_xmm->sv_fp[i].fp_acc.fp_bytes, 10); 1484 1485 /* SSE registers */ 1486 for (i = 0; i < 16; ++i) 1487 bcopy(fpregs->fpr_xacc[i], sv_xmm->sv_xmm[i].xmm_bytes, 16); 1488} 1489 1490/* externalize from td->pcb */ 1491int 1492fill_fpregs(struct thread *td, struct fpreg *fpregs) 1493{ 1494 1495 fill_fpregs_xmm(&td->td_pcb->pcb_save, fpregs); 1496 return (0); 1497} 1498 1499/* internalize to td->pcb */ 1500int 1501set_fpregs(struct thread *td, struct fpreg *fpregs) 1502{ 1503 1504 set_fpregs_xmm(fpregs, &td->td_pcb->pcb_save); 1505 return (0); 1506} 1507 1508/* 1509 * Get machine context. 1510 */ 1511int 1512get_mcontext(struct thread *td, mcontext_t *mcp, int flags) 1513{ 1514 struct trapframe *tp; 1515 1516 tp = td->td_frame; 1517 PROC_LOCK(curthread->td_proc); 1518 mcp->mc_onstack = sigonstack(tp->tf_rsp); 1519 PROC_UNLOCK(curthread->td_proc); 1520 mcp->mc_r15 = tp->tf_r15; 1521 mcp->mc_r14 = tp->tf_r14; 1522 mcp->mc_r13 = tp->tf_r13; 1523 mcp->mc_r12 = tp->tf_r12; 1524 mcp->mc_r11 = tp->tf_r11; 1525 mcp->mc_r10 = tp->tf_r10; 1526 mcp->mc_r9 = tp->tf_r9; 1527 mcp->mc_r8 = tp->tf_r8; 1528 mcp->mc_rdi = tp->tf_rdi; 1529 mcp->mc_rsi = tp->tf_rsi; 1530 mcp->mc_rbp = tp->tf_rbp; 1531 mcp->mc_rbx = tp->tf_rbx; 1532 mcp->mc_rcx = tp->tf_rcx; 1533 mcp->mc_rflags = tp->tf_rflags; 1534 if (flags & GET_MC_CLEAR_RET) { 1535 mcp->mc_rax = 0; 1536 mcp->mc_rdx = 0; 1537 mcp->mc_rflags &= ~PSL_C; 1538 } else { 1539 mcp->mc_rax = tp->tf_rax; 1540 mcp->mc_rdx = tp->tf_rdx; 1541 } 1542 mcp->mc_rip = tp->tf_rip; 1543 mcp->mc_cs = tp->tf_cs; 1544 mcp->mc_rsp = tp->tf_rsp; 1545 mcp->mc_ss = tp->tf_ss; 1546 mcp->mc_len = sizeof(*mcp); 1547 get_fpcontext(td, mcp); 1548 return (0); 1549} 1550 1551/* 1552 * Set machine context. 1553 * 1554 * However, we don't set any but the user modifiable flags, and we won't 1555 * touch the cs selector. 1556 */ 1557int 1558set_mcontext(struct thread *td, const mcontext_t *mcp) 1559{ 1560 struct trapframe *tp; 1561 long rflags; 1562 int ret; 1563 1564 tp = td->td_frame; 1565 if (mcp->mc_len != sizeof(*mcp)) 1566 return (EINVAL); 1567 rflags = (mcp->mc_rflags & PSL_USERCHANGE) | 1568 (tp->tf_rflags & ~PSL_USERCHANGE); 1569 ret = set_fpcontext(td, mcp); 1570 if (ret != 0) 1571 return (ret); 1572 tp->tf_r15 = mcp->mc_r15; 1573 tp->tf_r14 = mcp->mc_r14; 1574 tp->tf_r13 = mcp->mc_r13; 1575 tp->tf_r12 = mcp->mc_r12; 1576 tp->tf_r11 = mcp->mc_r11; 1577 tp->tf_r10 = mcp->mc_r10; 1578 tp->tf_r9 = mcp->mc_r9; 1579 tp->tf_r8 = mcp->mc_r8; 1580 tp->tf_rdi = mcp->mc_rdi; 1581 tp->tf_rsi = mcp->mc_rsi; 1582 tp->tf_rbp = mcp->mc_rbp; 1583 tp->tf_rbx = mcp->mc_rbx; 1584 tp->tf_rdx = mcp->mc_rdx; 1585 tp->tf_rcx = mcp->mc_rcx; 1586 tp->tf_rax = mcp->mc_rax; 1587 tp->tf_rip = mcp->mc_rip; 1588 tp->tf_rflags = rflags; 1589 tp->tf_rsp = mcp->mc_rsp; 1590 tp->tf_ss = mcp->mc_ss; 1591 td->td_pcb->pcb_flags |= PCB_FULLCTX; 1592 return (0); 1593} 1594 1595static void 1596get_fpcontext(struct thread *td, mcontext_t *mcp) 1597{ 1598 1599 mcp->mc_ownedfp = fpugetregs(td, (struct savefpu *)&mcp->mc_fpstate); 1600 mcp->mc_fpformat = fpuformat(); 1601} 1602 1603static int 1604set_fpcontext(struct thread *td, const mcontext_t *mcp) 1605{ 1606 struct savefpu *fpstate; 1607 1608 if (mcp->mc_fpformat == _MC_FPFMT_NODEV) 1609 return (0); 1610 else if (mcp->mc_fpformat != _MC_FPFMT_XMM) 1611 return (EINVAL); 1612 else if (mcp->mc_ownedfp == _MC_FPOWNED_NONE) 1613 /* We don't care what state is left in the FPU or PCB. */ 1614 fpstate_drop(td); 1615 else if (mcp->mc_ownedfp == _MC_FPOWNED_FPU || 1616 mcp->mc_ownedfp == _MC_FPOWNED_PCB) { 1617 /* 1618 * XXX we violate the dubious requirement that fpusetregs() 1619 * be called with interrupts disabled. 1620 * XXX obsolete on trap-16 systems? 1621 */ 1622 fpstate = (struct savefpu *)&mcp->mc_fpstate; 1623 fpstate->sv_env.en_mxcsr &= cpu_mxcsr_mask; 1624 fpusetregs(td, fpstate); 1625 } else 1626 return (EINVAL); 1627 return (0); 1628} 1629 1630void 1631fpstate_drop(struct thread *td) 1632{ 1633 register_t s; 1634 1635 s = intr_disable(); 1636 if (PCPU_GET(fpcurthread) == td) 1637 fpudrop(); 1638 /* 1639 * XXX force a full drop of the fpu. The above only drops it if we 1640 * owned it. 1641 * 1642 * XXX I don't much like fpugetregs()'s semantics of doing a full 1643 * drop. Dropping only to the pcb matches fnsave's behaviour. 1644 * We only need to drop to !PCB_INITDONE in sendsig(). But 1645 * sendsig() is the only caller of fpugetregs()... perhaps we just 1646 * have too many layers. 1647 */ 1648 curthread->td_pcb->pcb_flags &= ~PCB_FPUINITDONE; 1649 intr_restore(s); 1650} 1651 1652int 1653fill_dbregs(struct thread *td, struct dbreg *dbregs) 1654{ 1655 struct pcb *pcb; 1656 1657 if (td == NULL) { 1658 dbregs->dr[0] = rdr0(); 1659 dbregs->dr[1] = rdr1(); 1660 dbregs->dr[2] = rdr2(); 1661 dbregs->dr[3] = rdr3(); 1662 dbregs->dr[6] = rdr6(); 1663 dbregs->dr[7] = rdr7(); 1664 } else { 1665 pcb = td->td_pcb; 1666 dbregs->dr[0] = pcb->pcb_dr0; 1667 dbregs->dr[1] = pcb->pcb_dr1; 1668 dbregs->dr[2] = pcb->pcb_dr2; 1669 dbregs->dr[3] = pcb->pcb_dr3; 1670 dbregs->dr[6] = pcb->pcb_dr6; 1671 dbregs->dr[7] = pcb->pcb_dr7; 1672 } 1673 dbregs->dr[4] = 0; 1674 dbregs->dr[5] = 0; 1675 dbregs->dr[8] = 0; 1676 dbregs->dr[9] = 0; 1677 dbregs->dr[10] = 0; 1678 dbregs->dr[11] = 0; 1679 dbregs->dr[12] = 0; 1680 dbregs->dr[13] = 0; 1681 dbregs->dr[14] = 0; 1682 dbregs->dr[15] = 0; 1683 return (0); 1684} 1685 1686int 1687set_dbregs(struct thread *td, struct dbreg *dbregs) 1688{ 1689 struct pcb *pcb; 1690 int i; 1691 u_int64_t mask1, mask2; 1692 1693 if (td == NULL) { 1694 load_dr0(dbregs->dr[0]); 1695 load_dr1(dbregs->dr[1]); 1696 load_dr2(dbregs->dr[2]); 1697 load_dr3(dbregs->dr[3]); 1698 load_dr6(dbregs->dr[6]); 1699 load_dr7(dbregs->dr[7]); 1700 } else { 1701 /* 1702 * Don't let an illegal value for dr7 get set. Specifically, 1703 * check for undefined settings. Setting these bit patterns 1704 * result in undefined behaviour and can lead to an unexpected 1705 * TRCTRAP or a general protection fault right here. 1706 * Upper bits of dr6 and dr7 must not be set 1707 */ 1708 for (i = 0, mask1 = 0x3<<16, mask2 = 0x2<<16; i < 8; 1709 i++, mask1 <<= 2, mask2 <<= 2) 1710 if ((dbregs->dr[7] & mask1) == mask2) 1711 return (EINVAL); 1712 if ((dbregs->dr[6] & 0xffffffff00000000ul) != 0 || 1713 (dbregs->dr[7] & 0xffffffff00000000ul) != 0) 1714 return (EINVAL); 1715 1716 pcb = td->td_pcb; 1717 1718 /* 1719 * Don't let a process set a breakpoint that is not within the 1720 * process's address space. If a process could do this, it 1721 * could halt the system by setting a breakpoint in the kernel 1722 * (if ddb was enabled). Thus, we need to check to make sure 1723 * that no breakpoints are being enabled for addresses outside 1724 * process's address space. 1725 * 1726 * XXX - what about when the watched area of the user's 1727 * address space is written into from within the kernel 1728 * ... wouldn't that still cause a breakpoint to be generated 1729 * from within kernel mode? 1730 */ 1731 1732 if (dbregs->dr[7] & 0x3) { 1733 /* dr0 is enabled */ 1734 if (dbregs->dr[0] >= VM_MAXUSER_ADDRESS) 1735 return (EINVAL); 1736 } 1737 if (dbregs->dr[7] & 0x3<<2) { 1738 /* dr1 is enabled */ 1739 if (dbregs->dr[1] >= VM_MAXUSER_ADDRESS) 1740 return (EINVAL); 1741 } 1742 if (dbregs->dr[7] & 0x3<<4) { 1743 /* dr2 is enabled */ 1744 if (dbregs->dr[2] >= VM_MAXUSER_ADDRESS) 1745 return (EINVAL); 1746 } 1747 if (dbregs->dr[7] & 0x3<<6) { 1748 /* dr3 is enabled */ 1749 if (dbregs->dr[3] >= VM_MAXUSER_ADDRESS) 1750 return (EINVAL); 1751 } 1752 1753 pcb->pcb_dr0 = dbregs->dr[0]; 1754 pcb->pcb_dr1 = dbregs->dr[1]; 1755 pcb->pcb_dr2 = dbregs->dr[2]; 1756 pcb->pcb_dr3 = dbregs->dr[3]; 1757 pcb->pcb_dr6 = dbregs->dr[6]; 1758 pcb->pcb_dr7 = dbregs->dr[7]; 1759 1760 pcb->pcb_flags |= PCB_DBREGS; 1761 } 1762 1763 return (0); 1764} 1765 1766void 1767reset_dbregs(void) 1768{ 1769 1770 load_dr7(0); /* Turn off the control bits first */ 1771 load_dr0(0); 1772 load_dr1(0); 1773 load_dr2(0); 1774 load_dr3(0); 1775 load_dr6(0); 1776} 1777 1778/* 1779 * Return > 0 if a hardware breakpoint has been hit, and the 1780 * breakpoint was in user space. Return 0, otherwise. 1781 */ 1782int 1783user_dbreg_trap(void) 1784{ 1785 u_int64_t dr7, dr6; /* debug registers dr6 and dr7 */ 1786 u_int64_t bp; /* breakpoint bits extracted from dr6 */ 1787 int nbp; /* number of breakpoints that triggered */ 1788 caddr_t addr[4]; /* breakpoint addresses */ 1789 int i; 1790 1791 dr7 = rdr7(); 1792 if ((dr7 & 0x000000ff) == 0) { 1793 /* 1794 * all GE and LE bits in the dr7 register are zero, 1795 * thus the trap couldn't have been caused by the 1796 * hardware debug registers 1797 */ 1798 return 0; 1799 } 1800 1801 nbp = 0; 1802 dr6 = rdr6(); 1803 bp = dr6 & 0x0000000f; 1804 1805 if (!bp) { 1806 /* 1807 * None of the breakpoint bits are set meaning this 1808 * trap was not caused by any of the debug registers 1809 */ 1810 return 0; 1811 } 1812 1813 /* 1814 * at least one of the breakpoints were hit, check to see 1815 * which ones and if any of them are user space addresses 1816 */ 1817 1818 if (bp & 0x01) { 1819 addr[nbp++] = (caddr_t)rdr0(); 1820 } 1821 if (bp & 0x02) { 1822 addr[nbp++] = (caddr_t)rdr1(); 1823 } 1824 if (bp & 0x04) { 1825 addr[nbp++] = (caddr_t)rdr2(); 1826 } 1827 if (bp & 0x08) { 1828 addr[nbp++] = (caddr_t)rdr3(); 1829 } 1830 1831 for (i=0; i<nbp; i++) { 1832 if (addr[i] < 1833 (caddr_t)VM_MAXUSER_ADDRESS) { 1834 /* 1835 * addr[i] is in user space 1836 */ 1837 return nbp; 1838 } 1839 } 1840 1841 /* 1842 * None of the breakpoints are in user space. 1843 */ 1844 return 0; 1845} 1846 1847#ifdef KDB 1848 1849/* 1850 * Provide inb() and outb() as functions. They are normally only 1851 * available as macros calling inlined functions, thus cannot be 1852 * called from the debugger. 1853 * 1854 * The actual code is stolen from <machine/cpufunc.h>, and de-inlined. 1855 */ 1856 1857#undef inb 1858#undef outb 1859 1860/* silence compiler warnings */ 1861u_char inb(u_int); 1862void outb(u_int, u_char); 1863 1864u_char 1865inb(u_int port) 1866{ 1867 u_char data; 1868 /* 1869 * We use %%dx and not %1 here because i/o is done at %dx and not at 1870 * %edx, while gcc generates inferior code (movw instead of movl) 1871 * if we tell it to load (u_short) port. 1872 */ 1873 __asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port)); 1874 return (data); 1875} 1876 1877void 1878outb(u_int port, u_char data) 1879{ 1880 u_char al; 1881 /* 1882 * Use an unnecessary assignment to help gcc's register allocator. 1883 * This make a large difference for gcc-1.40 and a tiny difference 1884 * for gcc-2.6.0. For gcc-1.40, al had to be ``asm("ax")'' for 1885 * best results. gcc-2.6.0 can't handle this. 1886 */ 1887 al = data; 1888 __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port)); 1889} 1890 1891#endif /* KDB */ 1892