machdep.c revision 140555
197403Sobrien/*- 297403Sobrien * Copyright (c) 2003 Peter Wemm. 3169691Skan * Copyright (c) 1992 Terrence R. Lambert. 497403Sobrien * Copyright (c) 1982, 1987, 1990 The Regents of the University of California. 597403Sobrien * All rights reserved. 697403Sobrien * 797403Sobrien * This code is derived from software contributed to Berkeley by 897403Sobrien * William Jolitz. 997403Sobrien * 1097403Sobrien * Redistribution and use in source and binary forms, with or without 1197403Sobrien * modification, are permitted provided that the following conditions 1297403Sobrien * are met: 1397403Sobrien * 1. Redistributions of source code must retain the above copyright 1497403Sobrien * notice, this list of conditions and the following disclaimer. 1597403Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1697403Sobrien * notice, this list of conditions and the following disclaimer in the 1797403Sobrien * documentation and/or other materials provided with the distribution. 18169691Skan * 3. All advertising materials mentioning features or use of this software 1997403Sobrien * must display the following acknowledgement: 2097403Sobrien * This product includes software developed by the University of 2197403Sobrien * California, Berkeley and its contributors. 2297403Sobrien * 4. Neither the name of the University nor the names of its contributors 2397403Sobrien * may be used to endorse or promote products derived from this software 2497403Sobrien * without specific prior written permission. 2597403Sobrien * 2697403Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2797403Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2897403Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2997403Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 3097403Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3197403Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3297403Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3397403Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3497403Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3597403Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3697403Sobrien * SUCH DAMAGE. 3797403Sobrien * 3897403Sobrien * from: @(#)machdep.c 7.4 (Berkeley) 6/3/91 3997403Sobrien */ 4097403Sobrien 4197403Sobrien#include <sys/cdefs.h> 4297403Sobrien__FBSDID("$FreeBSD: head/sys/amd64/amd64/machdep.c 140555 2005-01-21 06:01:20Z peter $"); 4397403Sobrien 4497403Sobrien#include "opt_atalk.h" 4597403Sobrien#include "opt_atpic.h" 4697403Sobrien#include "opt_compat.h" 4797403Sobrien#include "opt_cpu.h" 4897403Sobrien#include "opt_ddb.h" 4997403Sobrien#include "opt_inet.h" 5097403Sobrien#include "opt_ipx.h" 5197403Sobrien#include "opt_isa.h" 5297403Sobrien#include "opt_kstack_pages.h" 5397403Sobrien#include "opt_maxmem.h" 5497403Sobrien#include "opt_msgbuf.h" 5597403Sobrien#include "opt_perfmon.h" 5697403Sobrien 5797403Sobrien#include <sys/param.h> 5897403Sobrien#include <sys/systm.h> 5997403Sobrien#include <sys/sysproto.h> 6097403Sobrien#include <sys/signalvar.h> 61132720Skan#include <sys/imgact.h> 62132720Skan#include <sys/kdb.h> 6397403Sobrien#include <sys/kernel.h> 6497403Sobrien#include <sys/ktr.h> 6597403Sobrien#include <sys/linker.h> 66169691Skan#include <sys/lock.h> 6797403Sobrien#include <sys/malloc.h> 68132720Skan#include <sys/memrange.h> 69132720Skan#include <sys/mutex.h> 70132720Skan#include <sys/pcpu.h> 71132720Skan#include <sys/proc.h> 72132720Skan#include <sys/bio.h> 73132720Skan#include <sys/buf.h> 74132720Skan#include <sys/reboot.h> 75132720Skan#include <sys/callout.h> 76132720Skan#include <sys/msgbuf.h> 77132720Skan#include <sys/sched.h> 78132720Skan#include <sys/sysent.h> 79132720Skan#include <sys/sysctl.h> 80132720Skan#include <sys/ucontext.h> 81132720Skan#include <sys/vmmeter.h> 82132720Skan#include <sys/bus.h> 83132720Skan#include <sys/eventhandler.h> 84132720Skan 85132720Skan#include <machine/pcb.h> 86132720Skan 87132720Skan#include <vm/vm.h> 88169691Skan#include <vm/vm_param.h> 89169691Skan#include <vm/vm_kern.h> 90132720Skan#include <vm/vm_object.h> 91132720Skan#include <vm/vm_page.h> 92132720Skan#include <vm/vm_map.h> 93169691Skan#include <vm/vm_pager.h> 94132720Skan#include <vm/vm_extern.h> 95132720Skan 96132720Skan#include <sys/exec.h> 97169691Skan#include <sys/cons.h> 9897403Sobrien 99132720Skan#ifdef DDB 100132720Skan#ifndef KDB 101132720Skan#error KDB must be enabled in order for DDB to work! 102132720Skan#endif 103132720Skan#endif 104132720Skan#include <ddb/ddb.h> 105169691Skan 10697403Sobrien#include <net/netisr.h> 107132720Skan 108132720Skan#include <machine/cpu.h> 109169691Skan#include <machine/cputypes.h> 110169691Skan#include <machine/reg.h> 111169691Skan#include <machine/clock.h> 112169691Skan#include <machine/specialreg.h> 113132720Skan#include <machine/intr_machdep.h> 114132720Skan#include <machine/md_var.h> 11597403Sobrien#include <machine/pc/bios.h> 116132720Skan#include <machine/metadata.h> 117169691Skan#include <machine/proc.h> 118169691Skan#ifdef PERFMON 119169691Skan#include <machine/perfmon.h> 120169691Skan#endif 121132720Skan#include <machine/tss.h> 122132720Skan#ifdef SMP 123132720Skan#include <machine/smp.h> 124169691Skan#endif 125169691Skan 126169691Skan#include <amd64/isa/icu.h> 127169691Skan 128169691Skan#include <isa/isareg.h> 129169691Skan#include <isa/rtc.h> 13097403Sobrien#include <sys/ptrace.h> 131169691Skan#include <machine/sigframe.h> 13297403Sobrien 133169691Skan/* Sanity check for __curthread() */ 134169691SkanCTASSERT(offsetof(struct pcpu, pc_curthread) == 0); 135169691Skan 136132720Skanextern u_int64_t hammer_time(u_int64_t, u_int64_t); 137236829Spfgextern void dblfault_handler(void); 13897403Sobrien 139132720Skanextern void printcpuinfo(void); /* XXX header file */ 140132720Skanextern void identify_cpu(void); 141132720Skanextern void panicifcpuunsupported(void); 142132720Skan 14397403Sobrien#define CS_SECURE(cs) (ISPL(cs) == SEL_UPL) 144132720Skan#define EFL_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0) 145132720Skan 146132720Skanstatic void cpu_startup(void *); 147132720Skanstatic void get_fpcontext(struct thread *td, mcontext_t *mcp); 148132720Skanstatic int set_fpcontext(struct thread *td, const mcontext_t *mcp); 149132720SkanSYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL) 150132720Skan 151132720Skan#ifdef DDB 152132720Skanextern vm_offset_t ksym_start, ksym_end; 153132720Skan#endif 154132720Skan 155236829Spfgint _udatasel, _ucodesel, _ucode32sel; 156169691Skan 15797403Sobrienint cold = 1; 158132720Skan 159132720Skanlong Maxmem = 0; 160132720Skan 161132720Skanvm_paddr_t phys_avail[20]; 162132720Skan 163132720Skan/* must be 2 less so 0 0 can signal end of chunks */ 164132720Skan#define PHYS_AVAIL_ARRAY_END ((sizeof(phys_avail) / sizeof(vm_offset_t)) - 2) 165132720Skan 166132720Skanstruct kva_md_info kmi; 167132720Skan 168132720Skanstatic struct trapframe proc0_tf; 169132720Skanstruct region_descriptor r_gdt, r_idt; 170132720Skan 171132720Skanstruct pcpu __pcpu[MAXCPU]; 172132720Skan 173132720Skanstruct mtx icu_lock; 174169691Skan 17597403Sobrienstruct mem_range_softc mem_range_softc; 176132720Skan 177132720Skanstatic void 178132720Skancpu_startup(dummy) 179132720Skan void *dummy; 180132720Skan{ 181132720Skan /* 182132720Skan * Good {morning,afternoon,evening,night}. 183236829Spfg */ 184132720Skan startrtclock(); 18597403Sobrien printcpuinfo(); 186132720Skan panicifcpuunsupported(); 187132720Skan#ifdef PERFMON 188132720Skan perfmon_init(); 189132720Skan#endif 190132720Skan printf("real memory = %ju (%ju MB)\n", ptoa((uintmax_t)Maxmem), 191132720Skan ptoa((uintmax_t)Maxmem) / 1048576); 192132720Skan /* 193236829Spfg * Display any holes after the first chunk of extended memory. 194236829Spfg */ 195132720Skan if (bootverbose) { 196132720Skan int indx; 197132720Skan 198132720Skan printf("Physical memory chunk(s):\n"); 19997403Sobrien for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) { 200132720Skan vm_paddr_t size; 20197403Sobrien 202132720Skan size = phys_avail[indx + 1] - phys_avail[indx]; 203132720Skan printf( 204132720Skan "0x%016jx - 0x%016jx, %ju bytes (%ju pages)\n", 205132720Skan (uintmax_t)phys_avail[indx], 206132720Skan (uintmax_t)phys_avail[indx + 1] - 1, 207132720Skan (uintmax_t)size, (uintmax_t)size / PAGE_SIZE); 208132720Skan } 209132720Skan } 210132720Skan 211132720Skan vm_ksubmap_init(&kmi); 212132720Skan 213132720Skan printf("avail memory = %ju (%ju MB)\n", 21497403Sobrien ptoa((uintmax_t)cnt.v_free_count), 215132720Skan ptoa((uintmax_t)cnt.v_free_count) / 1048576); 216132720Skan 217132720Skan /* 218132720Skan * Set up buffers, so they can be used to read disk labels. 219132720Skan */ 220132720Skan bufinit(); 221132720Skan vm_pager_bufferinit(); 222132720Skan 22397403Sobrien cpu_setregs(); 224132720Skan} 225132720Skan 226132720Skan/* 227132720Skan * Send an interrupt to process. 228132720Skan * 229132720Skan * Stack is set up to allow sigcode stored 230132720Skan * at top to call routine, followed by kcall 231132720Skan * to sigreturn routine below. After sigreturn 23297403Sobrien * resets the signal mask, the stack, and the 233132720Skan * frame pointer, it returns to the user 234132720Skan * specified pc, psl. 235132720Skan */ 236132720Skanvoid 237132720Skansendsig(catcher, sig, mask, code) 238132720Skan sig_t catcher; 239132720Skan int sig; 240132720Skan sigset_t *mask; 24197403Sobrien u_long code; 242132720Skan{ 243132720Skan struct sigframe sf, *sfp; 244132720Skan struct proc *p; 245132720Skan struct thread *td; 246132720Skan struct sigacts *psp; 247132720Skan char *sp; 248132720Skan struct trapframe *regs; 249132720Skan int oonstack; 25097403Sobrien 251132720Skan td = curthread; 252132720Skan p = td->td_proc; 253132720Skan PROC_LOCK_ASSERT(p, MA_OWNED); 254132720Skan psp = p->p_sigacts; 25597403Sobrien mtx_assert(&psp->ps_mtx, MA_OWNED); 256132720Skan regs = td->td_frame; 257132720Skan oonstack = sigonstack(regs->tf_rsp); 258132720Skan 259132720Skan /* Save user context. */ 26097403Sobrien bzero(&sf, sizeof(sf)); 261132720Skan sf.sf_uc.uc_sigmask = *mask; 262132720Skan sf.sf_uc.uc_stack = td->td_sigstk; 263132720Skan sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) 264132720Skan ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 26597403Sobrien sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0; 266132720Skan bcopy(regs, &sf.sf_uc.uc_mcontext.mc_rdi, sizeof(*regs)); 267132720Skan sf.sf_uc.uc_mcontext.mc_len = sizeof(sf.sf_uc.uc_mcontext); /* magic */ 268132720Skan get_fpcontext(td, &sf.sf_uc.uc_mcontext); 269132720Skan fpstate_drop(td); 270132720Skan 271132720Skan /* Allocate space for the signal handler context. */ 272132720Skan if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack && 273132720Skan SIGISMEMBER(psp->ps_sigonstack, sig)) { 274132720Skan sp = td->td_sigstk.ss_sp + 275132720Skan td->td_sigstk.ss_size - sizeof(struct sigframe); 276132720Skan#if defined(COMPAT_43) 277132720Skan td->td_sigstk.ss_flags |= SS_ONSTACK; 278236829Spfg#endif 279132720Skan } else 28097403Sobrien sp = (char *)regs->tf_rsp - sizeof(struct sigframe) - 128; 281132720Skan /* Align to 16 bytes. */ 282132720Skan sfp = (struct sigframe *)((unsigned long)sp & ~0xFul); 283132720Skan 284132720Skan /* Translate the signal if appropriate. */ 285132720Skan if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize) 286132720Skan sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)]; 287132720Skan 288132720Skan /* Build the argument list for the signal handler. */ 289132720Skan regs->tf_rdi = sig; /* arg 1 in %rdi */ 290132720Skan regs->tf_rdx = (register_t)&sfp->sf_uc; /* arg 3 in %rdx */ 291132720Skan if (SIGISMEMBER(psp->ps_siginfo, sig)) { 292132720Skan /* Signal handler installed with SA_SIGINFO. */ 293132720Skan regs->tf_rsi = (register_t)&sfp->sf_si; /* arg 2 in %rsi */ 294132720Skan sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher; 295169691Skan 29697403Sobrien /* Fill in POSIX parts */ 297132720Skan sf.sf_si.si_signo = sig; 298132720Skan sf.sf_si.si_code = code; 299132720Skan regs->tf_rcx = regs->tf_addr; /* arg 4 in %rcx */ 300132720Skan } else { 301132720Skan /* Old FreeBSD-style arguments. */ 302132720Skan regs->tf_rsi = code; /* arg 2 in %rsi */ 303132720Skan regs->tf_rcx = regs->tf_addr; /* arg 4 in %rcx */ 304132720Skan sf.sf_ahu.sf_handler = catcher; 305132720Skan } 306132720Skan mtx_unlock(&psp->ps_mtx); 307132720Skan PROC_UNLOCK(p); 308132720Skan 309132720Skan /* 310132720Skan * Copy the sigframe out to the user's stack. 311132720Skan */ 312132720Skan if (copyout(&sf, sfp, sizeof(*sfp)) != 0) { 313132720Skan#ifdef DEBUG 314132720Skan printf("process %ld has trashed its stack\n", (long)p->p_pid); 315132720Skan#endif 316132720Skan PROC_LOCK(p); 317132720Skan sigexit(td, SIGILL); 318132720Skan } 319169691Skan 32097403Sobrien regs->tf_rsp = (long)sfp; 321132720Skan regs->tf_rip = PS_STRINGS - *(p->p_sysent->sv_szsigcode); 322132720Skan regs->tf_rflags &= ~PSL_T; 323132720Skan regs->tf_cs = _ucodesel; 324132720Skan PROC_LOCK(p); 325132720Skan mtx_lock(&psp->ps_mtx); 326132720Skan} 327132720Skan 328132720Skan/* 329132720Skan * Build siginfo_t for SA thread 330132720Skan */ 331132720Skanvoid 332169691Skancpu_thread_siginfo(int sig, u_long code, siginfo_t *si) 33397403Sobrien{ 334132720Skan struct proc *p; 335132720Skan struct thread *td; 336132720Skan struct trapframe *regs; 337132720Skan 338132720Skan td = curthread; 339132720Skan p = td->td_proc; 340132720Skan regs = td->td_frame; 341132720Skan PROC_LOCK_ASSERT(p, MA_OWNED); 342132720Skan 343132720Skan bzero(si, sizeof(*si)); 344132720Skan si->si_signo = sig; 345132720Skan si->si_code = code; 346169691Skan si->si_addr = (void *)regs->tf_addr; 34797403Sobrien /* XXXKSE fill other fields */ 348132720Skan} 349132720Skan 350132720Skan/* 351132720Skan * System call to cleanup state after a signal 352132720Skan * has been taken. Reset signal mask and 353132720Skan * stack state from context left by sendsig (above). 354132720Skan * Return to previous pc and psl as specified by 355132720Skan * context left by sendsig. Check carefully to 356132720Skan * make sure that the user has not modified the 357132720Skan * state to gain improper privileges. 358132720Skan * 359132720Skan * MPSAFE 360132720Skan */ 361132720Skanint 36297403Sobriensigreturn(td, uap) 363132720Skan struct thread *td; 364132720Skan struct sigreturn_args /* { 365132720Skan const __ucontext *sigcntxp; 366132720Skan } */ *uap; 367132720Skan{ 368132720Skan ucontext_t uc; 369132720Skan struct proc *p = td->td_proc; 370132720Skan struct trapframe *regs; 371132720Skan const ucontext_t *ucp; 372132720Skan long rflags; 373132720Skan int cs, error, ret; 374132720Skan 375132720Skan error = copyin(uap->sigcntxp, &uc, sizeof(uc)); 376169691Skan if (error != 0) 37797403Sobrien return (error); 378132720Skan ucp = &uc; 379132720Skan regs = td->td_frame; 380132720Skan rflags = ucp->uc_mcontext.mc_rflags; 381132720Skan /* 382132720Skan * Don't allow users to change privileged or reserved flags. 383132720Skan */ 384132720Skan /* 385132720Skan * XXX do allow users to change the privileged flag PSL_RF. 386132720Skan * The cpu sets PSL_RF in tf_rflags for faults. Debuggers 387132720Skan * should sometimes set it there too. tf_rflags is kept in 388132720Skan * the signal context during signal handling and there is no 389132720Skan * other place to remember it, so the PSL_RF bit may be 390132720Skan * corrupted by the signal handler without us knowing. 391132720Skan * Corruption of the PSL_RF bit at worst causes one more or 392132720Skan * one less debugger trap, so allowing it is fairly harmless. 393132720Skan */ 394132720Skan if (!EFL_SECURE(rflags & ~PSL_RF, regs->tf_rflags & ~PSL_RF)) { 395132720Skan printf("sigreturn: rflags = 0x%lx\n", rflags); 396132720Skan return (EINVAL); 397132720Skan } 398132720Skan 399132720Skan /* 400132720Skan * Don't allow users to load a valid privileged %cs. Let the 401132720Skan * hardware check for invalid selectors, excess privilege in 402132720Skan * other selectors, invalid %eip's and invalid %esp's. 403132720Skan */ 404132720Skan cs = ucp->uc_mcontext.mc_cs; 405132720Skan if (!CS_SECURE(cs)) { 406132720Skan printf("sigreturn: cs = 0x%x\n", cs); 407132720Skan trapsignal(td, SIGBUS, T_PROTFLT); 408132720Skan return (EINVAL); 409132720Skan } 410132720Skan 411132720Skan ret = set_fpcontext(td, &ucp->uc_mcontext); 412132720Skan if (ret != 0) 413132720Skan return (ret); 414132720Skan bcopy(&ucp->uc_mcontext.mc_rdi, regs, sizeof(*regs)); 415132720Skan 416132720Skan PROC_LOCK(p); 417132720Skan#if defined(COMPAT_43) 418132720Skan if (ucp->uc_mcontext.mc_onstack & 1) 419132720Skan td->td_sigstk.ss_flags |= SS_ONSTACK; 420132720Skan else 421132720Skan td->td_sigstk.ss_flags &= ~SS_ONSTACK; 422132720Skan#endif 423132720Skan 424132720Skan td->td_sigmask = ucp->uc_sigmask; 425132720Skan SIG_CANTMASK(td->td_sigmask); 426132720Skan signotify(td); 427132720Skan PROC_UNLOCK(p); 428132720Skan td->td_pcb->pcb_flags |= PCB_FULLCTX; 429132720Skan return (EJUSTRETURN); 430132720Skan} 431132720Skan 432132720Skan#ifdef COMPAT_FREEBSD4 433132720Skanint 434132720Skanfreebsd4_sigreturn(struct thread *td, struct freebsd4_sigreturn_args *uap) 435132720Skan{ 436132720Skan 437132720Skan return sigreturn(td, (struct sigreturn_args *)uap); 438132720Skan} 439132720Skan#endif 440132720Skan 441132720Skan 442132720Skan/* 443132720Skan * Machine dependent boot() routine 444132720Skan * 445132720Skan * I haven't seen anything to put here yet 446132720Skan * Possibly some stuff might be grafted back here from boot() 447132720Skan */ 448132720Skanvoid 449132720Skancpu_boot(int howto) 450132720Skan{ 451132720Skan} 452132720Skan 453132720Skan/* 454132720Skan * Shutdown the CPU as much as possible 455132720Skan */ 456132720Skanvoid 457132720Skancpu_halt(void) 458132720Skan{ 459132720Skan for (;;) 460132720Skan __asm__ ("hlt"); 461132720Skan} 462132720Skan 463132720Skan/* 464132720Skan * Hook to idle the CPU when possible. In the SMP case we default to 465132720Skan * off because a halted cpu will not currently pick up a new thread in the 466132720Skan * run queue until the next timer tick. If turned on this will result in 467132720Skan * approximately a 4.2% loss in real time performance in buildworld tests 468132720Skan * (but improves user and sys times oddly enough), and saves approximately 469132720Skan * 5% in power consumption on an idle machine (tests w/2xCPU 1.1GHz P3). 470132720Skan * 471132720Skan * XXX we need to have a cpu mask of idle cpus and generate an IPI or 472132720Skan * otherwise generate some sort of interrupt to wake up cpus sitting in HLT. 473132720Skan * Then we can have our cake and eat it too. 474132720Skan * 475169691Skan * XXX I'm turning it on for SMP as well by default for now. It seems to 476132720Skan * help lock contention somewhat, and this is critical for HTT. -Peter 477132720Skan */ 478132720Skanstatic int cpu_idle_hlt = 1; 479169691SkanSYSCTL_INT(_machdep, OID_AUTO, cpu_idle_hlt, CTLFLAG_RW, 480132720Skan &cpu_idle_hlt, 0, "Idle loop HLT enable"); 481132720Skan 482132720Skanstatic void 483132720Skancpu_idle_default(void) 484132720Skan{ 485169691Skan /* 486169691Skan * we must absolutely guarentee that hlt is the 487132720Skan * absolute next instruction after sti or we 488132720Skan * introduce a timing window. 489132720Skan */ 490169691Skan __asm __volatile("sti; hlt"); 491169691Skan} 492132720Skan 493132720Skan/* 494132720Skan * Note that we have to be careful here to avoid a race between checking 495132720Skan * sched_runnable() and actually halting. If we don't do this, we may waste 496132720Skan * the time between calling hlt and the next interrupt even though there 497132720Skan * is a runnable process. 498132720Skan */ 499132720Skanvoid 500132720Skancpu_idle(void) 501132720Skan{ 502132720Skan 503132720Skan#ifdef SMP 504132720Skan if (mp_grab_cpu_hlt()) 505132720Skan return; 506132720Skan#endif 507169691Skan if (cpu_idle_hlt) { 508169691Skan disable_intr(); 509132720Skan if (sched_runnable()) 510132720Skan enable_intr(); 511132720Skan else 512132720Skan (*cpu_idle_hook)(); 513132720Skan } 514132720Skan} 515132720Skan 516132720Skan/* Other subsystems (e.g., ACPI) can hook this later. */ 517132720Skanvoid (*cpu_idle_hook)(void) = cpu_idle_default; 518132720Skan 519132720Skan/* 520132720Skan * Clear registers on exec 521132720Skan */ 522132720Skanvoid 523132720Skanexec_setregs(td, entry, stack, ps_strings) 524169691Skan struct thread *td; 525169691Skan u_long entry; 526132720Skan u_long stack; 527132720Skan u_long ps_strings; 528132720Skan{ 529132720Skan struct trapframe *regs = td->td_frame; 530132720Skan struct pcb *pcb = td->td_pcb; 531169691Skan 532169691Skan wrmsr(MSR_FSBASE, 0); 533132720Skan wrmsr(MSR_KGSBASE, 0); /* User value while we're in the kernel */ 534132720Skan pcb->pcb_fsbase = 0; 535132720Skan pcb->pcb_gsbase = 0; 536132720Skan load_ds(_udatasel); 537132720Skan load_es(_udatasel); 538132720Skan load_fs(_udatasel); 539132720Skan load_gs(_udatasel); 540132720Skan pcb->pcb_ds = _udatasel; 541132720Skan pcb->pcb_es = _udatasel; 542132720Skan pcb->pcb_fs = _udatasel; 543132720Skan pcb->pcb_gs = _udatasel; 544132720Skan 545169691Skan bzero((char *)regs, sizeof(struct trapframe)); 546169691Skan regs->tf_rip = entry; 547132720Skan regs->tf_rsp = ((stack - 8) & ~0xFul) + 8; 548132720Skan regs->tf_rdi = stack; /* argv */ 549132720Skan regs->tf_rflags = PSL_USER | (regs->tf_rflags & PSL_T); 550132720Skan regs->tf_ss = _udatasel; 551132720Skan regs->tf_cs = _ucodesel; 552169691Skan 553169691Skan /* 554132720Skan * Reset the hardware debug registers if they were in use. 555132720Skan * They won't have any meaning for the newly exec'd process. 556132720Skan */ 557132720Skan if (pcb->pcb_flags & PCB_DBREGS) { 558132720Skan pcb->pcb_dr0 = 0; 559169691Skan pcb->pcb_dr1 = 0; 560169691Skan pcb->pcb_dr2 = 0; 561132720Skan pcb->pcb_dr3 = 0; 562132720Skan pcb->pcb_dr6 = 0; 563169691Skan pcb->pcb_dr7 = 0; 56497403Sobrien if (pcb == PCPU_GET(curpcb)) { 565132720Skan /* 566 * Clear the debug registers on the running 567 * CPU, otherwise they will end up affecting 568 * the next process we switch to. 569 */ 570 reset_dbregs(); 571 } 572 pcb->pcb_flags &= ~PCB_DBREGS; 573 } 574 575 /* 576 * Drop the FP state if we hold it, so that the process gets a 577 * clean FP state if it uses the FPU again. 578 */ 579 fpstate_drop(td); 580} 581 582void 583cpu_setregs(void) 584{ 585 register_t cr0; 586 587 cr0 = rcr0(); 588 /* 589 * CR0_MP, CR0_NE and CR0_TS are also set by npx_probe() for the 590 * BSP. See the comments there about why we set them. 591 */ 592 cr0 |= CR0_MP | CR0_NE | CR0_TS | CR0_WP | CR0_AM; 593 load_cr0(cr0); 594} 595 596static int 597sysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS) 598{ 599 int error; 600 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, 601 req); 602 if (!error && req->newptr) 603 resettodr(); 604 return (error); 605} 606 607SYSCTL_PROC(_machdep, CPU_ADJKERNTZ, adjkerntz, CTLTYPE_INT|CTLFLAG_RW, 608 &adjkerntz, 0, sysctl_machdep_adjkerntz, "I", ""); 609 610SYSCTL_INT(_machdep, CPU_DISRTCSET, disable_rtc_set, 611 CTLFLAG_RW, &disable_rtc_set, 0, ""); 612 613SYSCTL_INT(_machdep, CPU_WALLCLOCK, wall_cmos_clock, 614 CTLFLAG_RW, &wall_cmos_clock, 0, ""); 615 616/* 617 * Initialize 386 and configure to run kernel 618 */ 619 620/* 621 * Initialize segments & interrupt table 622 */ 623 624struct user_segment_descriptor gdt[NGDT * MAXCPU];/* global descriptor table */ 625static struct gate_descriptor idt0[NIDT]; 626struct gate_descriptor *idt = &idt0[0]; /* interrupt descriptor table */ 627 628static char dblfault_stack[PAGE_SIZE] __aligned(16); 629 630struct amd64tss common_tss[MAXCPU]; 631 632/* software prototypes -- in more palatable form */ 633struct soft_segment_descriptor gdt_segs[] = { 634/* GNULL_SEL 0 Null Descriptor */ 635{ 0x0, /* segment base address */ 636 0x0, /* length */ 637 0, /* segment type */ 638 0, /* segment descriptor priority level */ 639 0, /* segment descriptor present */ 640 0, /* long */ 641 0, /* default 32 vs 16 bit size */ 642 0 /* limit granularity (byte/page units)*/ }, 643/* GCODE_SEL 1 Code Descriptor for kernel */ 644{ 0x0, /* segment base address */ 645 0xfffff, /* length - all address space */ 646 SDT_MEMERA, /* segment type */ 647 SEL_KPL, /* segment descriptor priority level */ 648 1, /* segment descriptor present */ 649 1, /* long */ 650 0, /* default 32 vs 16 bit size */ 651 1 /* limit granularity (byte/page units)*/ }, 652/* GDATA_SEL 2 Data Descriptor for kernel */ 653{ 0x0, /* segment base address */ 654 0xfffff, /* length - all address space */ 655 SDT_MEMRWA, /* segment type */ 656 SEL_KPL, /* segment descriptor priority level */ 657 1, /* segment descriptor present */ 658 1, /* long */ 659 0, /* default 32 vs 16 bit size */ 660 1 /* limit granularity (byte/page units)*/ }, 661/* GUCODE32_SEL 3 32 bit Code Descriptor for user */ 662{ 0x0, /* segment base address */ 663 0xfffff, /* length - all address space */ 664 SDT_MEMERA, /* segment type */ 665 SEL_UPL, /* segment descriptor priority level */ 666 1, /* segment descriptor present */ 667 0, /* long */ 668 1, /* default 32 vs 16 bit size */ 669 1 /* limit granularity (byte/page units)*/ }, 670/* GUDATA_SEL 4 32/64 bit Data Descriptor for user */ 671{ 0x0, /* segment base address */ 672 0xfffff, /* length - all address space */ 673 SDT_MEMRWA, /* segment type */ 674 SEL_UPL, /* segment descriptor priority level */ 675 1, /* segment descriptor present */ 676 0, /* long */ 677 1, /* default 32 vs 16 bit size */ 678 1 /* limit granularity (byte/page units)*/ }, 679/* GUCODE_SEL 5 64 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 1, /* long */ 686 0, /* default 32 vs 16 bit size */ 687 1 /* limit granularity (byte/page units)*/ }, 688/* GPROC0_SEL 6 Proc 0 Tss Descriptor */ 689{ 690 0x0, /* segment base address */ 691 sizeof(struct amd64tss)-1,/* length - all address space */ 692 SDT_SYSTSS, /* segment type */ 693 SEL_KPL, /* segment descriptor priority level */ 694 1, /* segment descriptor present */ 695 0, /* long */ 696 0, /* unused - default 32 vs 16 bit size */ 697 0 /* limit granularity (byte/page units)*/ }, 698/* Actually, the TSS is a system descriptor which is double size */ 699{ 0x0, /* segment base address */ 700 0x0, /* length */ 701 0, /* segment type */ 702 0, /* segment descriptor priority level */ 703 0, /* segment descriptor present */ 704 0, /* long */ 705 0, /* default 32 vs 16 bit size */ 706 0 /* limit granularity (byte/page units)*/ }, 707}; 708 709void 710setidt(idx, func, typ, dpl, ist) 711 int idx; 712 inthand_t *func; 713 int typ; 714 int dpl; 715 int ist; 716{ 717 struct gate_descriptor *ip; 718 719 ip = idt + idx; 720 ip->gd_looffset = (uintptr_t)func; 721 ip->gd_selector = GSEL(GCODE_SEL, SEL_KPL); 722 ip->gd_ist = ist; 723 ip->gd_xx = 0; 724 ip->gd_type = typ; 725 ip->gd_dpl = dpl; 726 ip->gd_p = 1; 727 ip->gd_hioffset = ((uintptr_t)func)>>16 ; 728} 729 730#define IDTVEC(name) __CONCAT(X,name) 731 732extern inthand_t 733 IDTVEC(div), IDTVEC(dbg), IDTVEC(nmi), IDTVEC(bpt), IDTVEC(ofl), 734 IDTVEC(bnd), IDTVEC(ill), IDTVEC(dna), IDTVEC(fpusegm), 735 IDTVEC(tss), IDTVEC(missing), IDTVEC(stk), IDTVEC(prot), 736 IDTVEC(page), IDTVEC(mchk), IDTVEC(rsvd), IDTVEC(fpu), IDTVEC(align), 737 IDTVEC(xmm), IDTVEC(dblfault), 738 IDTVEC(fast_syscall), IDTVEC(fast_syscall32); 739 740void 741sdtossd(sd, ssd) 742 struct user_segment_descriptor *sd; 743 struct soft_segment_descriptor *ssd; 744{ 745 746 ssd->ssd_base = (sd->sd_hibase << 24) | sd->sd_lobase; 747 ssd->ssd_limit = (sd->sd_hilimit << 16) | sd->sd_lolimit; 748 ssd->ssd_type = sd->sd_type; 749 ssd->ssd_dpl = sd->sd_dpl; 750 ssd->ssd_p = sd->sd_p; 751 ssd->ssd_long = sd->sd_long; 752 ssd->ssd_def32 = sd->sd_def32; 753 ssd->ssd_gran = sd->sd_gran; 754} 755 756void 757ssdtosd(ssd, sd) 758 struct soft_segment_descriptor *ssd; 759 struct user_segment_descriptor *sd; 760{ 761 762 sd->sd_lobase = (ssd->ssd_base) & 0xffffff; 763 sd->sd_hibase = (ssd->ssd_base >> 24) & 0xff; 764 sd->sd_lolimit = (ssd->ssd_limit) & 0xffff; 765 sd->sd_hilimit = (ssd->ssd_limit >> 16) & 0xf; 766 sd->sd_type = ssd->ssd_type; 767 sd->sd_dpl = ssd->ssd_dpl; 768 sd->sd_p = ssd->ssd_p; 769 sd->sd_long = ssd->ssd_long; 770 sd->sd_def32 = ssd->ssd_def32; 771 sd->sd_gran = ssd->ssd_gran; 772} 773 774void 775ssdtosyssd(ssd, sd) 776 struct soft_segment_descriptor *ssd; 777 struct system_segment_descriptor *sd; 778{ 779 780 sd->sd_lobase = (ssd->ssd_base) & 0xffffff; 781 sd->sd_hibase = (ssd->ssd_base >> 24) & 0xfffffffffful; 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_gran = ssd->ssd_gran; 788} 789 790#if !defined(DEV_ATPIC) && defined(DEV_ISA) 791#include <isa/isavar.h> 792u_int 793isa_irq_pending(void) 794{ 795 796 return (0); 797} 798#endif 799 800#define PHYSMAP_SIZE (2 * 8) 801 802u_int basemem; 803 804/* 805 * Populate the (physmap) array with base/bound pairs describing the 806 * available physical memory in the system, then test this memory and 807 * build the phys_avail array describing the actually-available memory. 808 * 809 * If we cannot accurately determine the physical memory map, then use 810 * value from the 0xE801 call, and failing that, the RTC. 811 * 812 * Total memory size may be set by the kernel environment variable 813 * hw.physmem or the compile-time define MAXMEM. 814 * 815 * XXX first should be vm_paddr_t. 816 */ 817static void 818getmemsize(caddr_t kmdp, u_int64_t first) 819{ 820 int i, physmap_idx, pa_indx; 821 vm_paddr_t pa, physmap[PHYSMAP_SIZE]; 822 pt_entry_t *pte; 823 char *cp; 824 struct bios_smap *smapbase, *smap, *smapend; 825 u_int32_t smapsize; 826 quad_t dcons_addr, dcons_size; 827 828 bzero(physmap, sizeof(physmap)); 829 basemem = 0; 830 physmap_idx = 0; 831 832 /* 833 * get memory map from INT 15:E820, kindly supplied by the loader. 834 * 835 * subr_module.c says: 836 * "Consumer may safely assume that size value precedes data." 837 * ie: an int32_t immediately precedes smap. 838 */ 839 smapbase = (struct bios_smap *)preload_search_info(kmdp, 840 MODINFO_METADATA | MODINFOMD_SMAP); 841 if (smapbase == NULL) 842 panic("No BIOS smap info from loader!"); 843 844 smapsize = *((u_int32_t *)smapbase - 1); 845 smapend = (struct bios_smap *)((uintptr_t)smapbase + smapsize); 846 847 for (smap = smapbase; smap < smapend; smap++) { 848 if (boothowto & RB_VERBOSE) 849 printf("SMAP type=%02x base=%016lx len=%016lx\n", 850 smap->type, smap->base, smap->length); 851 852 if (smap->type != 0x01) 853 continue; 854 855 if (smap->length == 0) 856 continue; 857 858 for (i = 0; i <= physmap_idx; i += 2) { 859 if (smap->base < physmap[i + 1]) { 860 if (boothowto & RB_VERBOSE) 861 printf( 862 "Overlapping or non-montonic memory region, ignoring second region\n"); 863 goto next_run; 864 } 865 } 866 867 if (smap->base == physmap[physmap_idx + 1]) { 868 physmap[physmap_idx + 1] += smap->length; 869next_run: 870 continue; 871 } 872 873 physmap_idx += 2; 874 if (physmap_idx == PHYSMAP_SIZE) { 875 printf( 876 "Too many segments in the physical address map, giving up\n"); 877 break; 878 } 879 physmap[physmap_idx] = smap->base; 880 physmap[physmap_idx + 1] = smap->base + smap->length; 881 } 882 883 /* 884 * Find the 'base memory' segment for SMP 885 */ 886 basemem = 0; 887 for (i = 0; i <= physmap_idx; i += 2) { 888 if (physmap[i] == 0x00000000) { 889 basemem = physmap[i + 1] / 1024; 890 break; 891 } 892 } 893 if (basemem == 0) 894 panic("BIOS smap did not include a basemem segment!"); 895 896#ifdef SMP 897 /* make hole for AP bootstrap code */ 898 physmap[1] = mp_bootaddress(physmap[1] / 1024); 899#endif 900 901 /* 902 * Maxmem isn't the "maximum memory", it's one larger than the 903 * highest page of the physical address space. It should be 904 * called something like "Maxphyspage". We may adjust this 905 * based on ``hw.physmem'' and the results of the memory test. 906 */ 907 Maxmem = atop(physmap[physmap_idx + 1]); 908 909#ifdef MAXMEM 910 Maxmem = MAXMEM / 4; 911#endif 912 913 /* 914 * hw.physmem is a size in bytes; we also allow k, m, and g suffixes 915 * for the appropriate modifiers. This overrides MAXMEM. 916 */ 917 cp = getenv("hw.physmem"); 918 if (cp != NULL) { 919 u_int64_t AllowMem, sanity; 920 char *ep; 921 922 sanity = AllowMem = strtouq(cp, &ep, 0); 923 if ((ep != cp) && (*ep != 0)) { 924 switch(*ep) { 925 case 'g': 926 case 'G': 927 AllowMem <<= 10; 928 case 'm': 929 case 'M': 930 AllowMem <<= 10; 931 case 'k': 932 case 'K': 933 AllowMem <<= 10; 934 break; 935 default: 936 AllowMem = sanity = 0; 937 } 938 if (AllowMem < sanity) 939 AllowMem = 0; 940 } 941 if (AllowMem == 0) 942 printf("Ignoring invalid memory size of '%s'\n", cp); 943 else 944 Maxmem = atop(AllowMem); 945 freeenv(cp); 946 } 947 948 if (atop(physmap[physmap_idx + 1]) != Maxmem && 949 (boothowto & RB_VERBOSE)) 950 printf("Physical memory use set to %ldK\n", Maxmem * 4); 951 952 /* 953 * If Maxmem has been increased beyond what the system has detected, 954 * extend the last memory segment to the new limit. 955 */ 956 if (atop(physmap[physmap_idx + 1]) < Maxmem) 957 physmap[physmap_idx + 1] = ptoa((vm_paddr_t)Maxmem); 958 959 /* call pmap initialization to make new kernel address space */ 960 pmap_bootstrap(&first); 961 962 /* 963 * Size up each available chunk of physical memory. 964 */ 965 physmap[0] = PAGE_SIZE; /* mask off page 0 */ 966 pa_indx = 0; 967 phys_avail[pa_indx++] = physmap[0]; 968 phys_avail[pa_indx] = physmap[0]; 969 pte = CMAP1; 970 971 /* 972 * Get dcons buffer address 973 */ 974 if (getenv_quad("dcons.addr", &dcons_addr) == 0 || 975 getenv_quad("dcons.size", &dcons_size) == 0) 976 dcons_addr = 0; 977 978 /* 979 * physmap is in bytes, so when converting to page boundaries, 980 * round up the start address and round down the end address. 981 */ 982 for (i = 0; i <= physmap_idx; i += 2) { 983 vm_paddr_t end; 984 985 end = ptoa((vm_paddr_t)Maxmem); 986 if (physmap[i + 1] < end) 987 end = trunc_page(physmap[i + 1]); 988 for (pa = round_page(physmap[i]); pa < end; pa += PAGE_SIZE) { 989 int tmp, page_bad; 990 int *ptr = (int *)CADDR1; 991 992 /* 993 * block out kernel memory as not available. 994 */ 995 if (pa >= 0x100000 && pa < first) 996 continue; 997 998 /* 999 * block out dcons buffer 1000 */ 1001 if (dcons_addr > 0 1002 && pa >= trunc_page(dcons_addr) 1003 && pa < dcons_addr + dcons_size) 1004 continue; 1005 1006 page_bad = FALSE; 1007 1008 /* 1009 * map page into kernel: valid, read/write,non-cacheable 1010 */ 1011 *pte = pa | PG_V | PG_RW | PG_N; 1012 invltlb(); 1013 1014 tmp = *(int *)ptr; 1015 /* 1016 * Test for alternating 1's and 0's 1017 */ 1018 *(volatile int *)ptr = 0xaaaaaaaa; 1019 if (*(volatile int *)ptr != 0xaaaaaaaa) 1020 page_bad = TRUE; 1021 /* 1022 * Test for alternating 0's and 1's 1023 */ 1024 *(volatile int *)ptr = 0x55555555; 1025 if (*(volatile int *)ptr != 0x55555555) 1026 page_bad = TRUE; 1027 /* 1028 * Test for all 1's 1029 */ 1030 *(volatile int *)ptr = 0xffffffff; 1031 if (*(volatile int *)ptr != 0xffffffff) 1032 page_bad = TRUE; 1033 /* 1034 * Test for all 0's 1035 */ 1036 *(volatile int *)ptr = 0x0; 1037 if (*(volatile int *)ptr != 0x0) 1038 page_bad = TRUE; 1039 /* 1040 * Restore original value. 1041 */ 1042 *(int *)ptr = tmp; 1043 1044 /* 1045 * Adjust array of valid/good pages. 1046 */ 1047 if (page_bad == TRUE) 1048 continue; 1049 /* 1050 * If this good page is a continuation of the 1051 * previous set of good pages, then just increase 1052 * the end pointer. Otherwise start a new chunk. 1053 * Note that "end" points one higher than end, 1054 * making the range >= start and < end. 1055 * If we're also doing a speculative memory 1056 * test and we at or past the end, bump up Maxmem 1057 * so that we keep going. The first bad page 1058 * will terminate the loop. 1059 */ 1060 if (phys_avail[pa_indx] == pa) { 1061 phys_avail[pa_indx] += PAGE_SIZE; 1062 } else { 1063 pa_indx++; 1064 if (pa_indx == PHYS_AVAIL_ARRAY_END) { 1065 printf( 1066 "Too many holes in the physical address space, giving up\n"); 1067 pa_indx--; 1068 break; 1069 } 1070 phys_avail[pa_indx++] = pa; /* start */ 1071 phys_avail[pa_indx] = pa + PAGE_SIZE; /* end */ 1072 } 1073 physmem++; 1074 } 1075 } 1076 *pte = 0; 1077 invltlb(); 1078 1079 /* 1080 * XXX 1081 * The last chunk must contain at least one page plus the message 1082 * buffer to avoid complicating other code (message buffer address 1083 * calculation, etc.). 1084 */ 1085 while (phys_avail[pa_indx - 1] + PAGE_SIZE + 1086 round_page(MSGBUF_SIZE) >= phys_avail[pa_indx]) { 1087 physmem -= atop(phys_avail[pa_indx] - phys_avail[pa_indx - 1]); 1088 phys_avail[pa_indx--] = 0; 1089 phys_avail[pa_indx--] = 0; 1090 } 1091 1092 Maxmem = atop(phys_avail[pa_indx]); 1093 1094 /* Trim off space for the message buffer. */ 1095 phys_avail[pa_indx] -= round_page(MSGBUF_SIZE); 1096 1097 avail_end = phys_avail[pa_indx]; 1098} 1099 1100u_int64_t 1101hammer_time(u_int64_t modulep, u_int64_t physfree) 1102{ 1103 caddr_t kmdp; 1104 int gsel_tss, off, x; 1105 struct pcpu *pc; 1106 u_int64_t msr; 1107 char *env; 1108 1109#ifdef DEV_ISA 1110 /* Preemptively mask the atpics and leave them shut down */ 1111 outb(IO_ICU1 + ICU_IMR_OFFSET, 0xff); 1112 outb(IO_ICU2 + ICU_IMR_OFFSET, 0xff); 1113#else 1114#error "have you forgotten the isa device?"; 1115#endif 1116 1117 thread0.td_kstack = physfree + KERNBASE; 1118 bzero((void *)thread0.td_kstack, KSTACK_PAGES * PAGE_SIZE); 1119 physfree += KSTACK_PAGES * PAGE_SIZE; 1120 thread0.td_pcb = (struct pcb *) 1121 (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1; 1122 1123 /* 1124 * This may be done better later if it gets more high level 1125 * components in it. If so just link td->td_proc here. 1126 */ 1127 proc_linkup(&proc0, &ksegrp0, &thread0); 1128 1129 preload_metadata = (caddr_t)(uintptr_t)(modulep + KERNBASE); 1130 preload_bootstrap_relocate(KERNBASE); 1131 kmdp = preload_search_by_type("elf kernel"); 1132 if (kmdp == NULL) 1133 kmdp = preload_search_by_type("elf64 kernel"); 1134 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int); 1135 kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *) + KERNBASE; 1136#ifdef DDB 1137 ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t); 1138 ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t); 1139#endif 1140 1141 /* Init basic tunables, hz etc */ 1142 init_param1(); 1143 1144 /* 1145 * make gdt memory segments 1146 */ 1147 gdt_segs[GPROC0_SEL].ssd_base = (uintptr_t)&common_tss[0]; 1148 1149 for (x = 0; x < NGDT; x++) { 1150 if (x != GPROC0_SEL && x != (GPROC0_SEL + 1)) 1151 ssdtosd(&gdt_segs[x], &gdt[x]); 1152 } 1153 ssdtosyssd(&gdt_segs[GPROC0_SEL], 1154 (struct system_segment_descriptor *)&gdt[GPROC0_SEL]); 1155 1156 r_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1; 1157 r_gdt.rd_base = (long) gdt; 1158 lgdt(&r_gdt); 1159 pc = &__pcpu[0]; 1160 1161 wrmsr(MSR_FSBASE, 0); /* User value */ 1162 wrmsr(MSR_GSBASE, (u_int64_t)pc); 1163 wrmsr(MSR_KGSBASE, 0); /* User value while in the kernel */ 1164 1165 pcpu_init(pc, 0, sizeof(struct pcpu)); 1166 PCPU_SET(prvspace, pc); 1167 PCPU_SET(curthread, &thread0); 1168 PCPU_SET(curpcb, thread0.td_pcb); 1169 PCPU_SET(tssp, &common_tss[0]); 1170 1171 /* 1172 * Initialize mutexes. 1173 * 1174 * icu_lock: in order to allow an interrupt to occur in a critical 1175 * section, to set pcpu->ipending (etc...) properly, we 1176 * must be able to get the icu lock, so it can't be 1177 * under witness. 1178 */ 1179 mutex_init(); 1180 mtx_init(&clock_lock, "clk", NULL, MTX_SPIN); 1181 mtx_init(&icu_lock, "icu", NULL, MTX_SPIN | MTX_NOWITNESS); 1182 1183 /* exceptions */ 1184 for (x = 0; x < NIDT; x++) 1185 setidt(x, &IDTVEC(rsvd), SDT_SYSIGT, SEL_KPL, 0); 1186 setidt(IDT_DE, &IDTVEC(div), SDT_SYSIGT, SEL_KPL, 0); 1187 setidt(IDT_DB, &IDTVEC(dbg), SDT_SYSIGT, SEL_KPL, 0); 1188 setidt(IDT_NMI, &IDTVEC(nmi), SDT_SYSIGT, SEL_KPL, 0); 1189 setidt(IDT_BP, &IDTVEC(bpt), SDT_SYSIGT, SEL_UPL, 0); 1190 setidt(IDT_OF, &IDTVEC(ofl), SDT_SYSIGT, SEL_KPL, 0); 1191 setidt(IDT_BR, &IDTVEC(bnd), SDT_SYSIGT, SEL_KPL, 0); 1192 setidt(IDT_UD, &IDTVEC(ill), SDT_SYSIGT, SEL_KPL, 0); 1193 setidt(IDT_NM, &IDTVEC(dna), SDT_SYSIGT, SEL_KPL, 0); 1194 setidt(IDT_DF, &IDTVEC(dblfault), SDT_SYSIGT, SEL_KPL, 1); 1195 setidt(IDT_FPUGP, &IDTVEC(fpusegm), SDT_SYSIGT, SEL_KPL, 0); 1196 setidt(IDT_TS, &IDTVEC(tss), SDT_SYSIGT, SEL_KPL, 0); 1197 setidt(IDT_NP, &IDTVEC(missing), SDT_SYSIGT, SEL_KPL, 0); 1198 setidt(IDT_SS, &IDTVEC(stk), SDT_SYSIGT, SEL_KPL, 0); 1199 setidt(IDT_GP, &IDTVEC(prot), SDT_SYSIGT, SEL_KPL, 0); 1200 setidt(IDT_PF, &IDTVEC(page), SDT_SYSIGT, SEL_KPL, 0); 1201 setidt(IDT_MF, &IDTVEC(fpu), SDT_SYSIGT, SEL_KPL, 0); 1202 setidt(IDT_AC, &IDTVEC(align), SDT_SYSIGT, SEL_KPL, 0); 1203 setidt(IDT_MC, &IDTVEC(mchk), SDT_SYSIGT, SEL_KPL, 0); 1204 setidt(IDT_XF, &IDTVEC(xmm), SDT_SYSIGT, SEL_KPL, 0); 1205 1206 r_idt.rd_limit = sizeof(idt0) - 1; 1207 r_idt.rd_base = (long) idt; 1208 lidt(&r_idt); 1209 1210 /* 1211 * Initialize the console before we print anything out. 1212 */ 1213 cninit(); 1214 1215#ifdef DEV_ATPIC 1216 elcr_probe(); 1217 atpic_startup(); 1218#endif 1219 1220 kdb_init(); 1221 1222#ifdef KDB 1223 if (boothowto & RB_KDB) 1224 kdb_enter("Boot flags requested debugger"); 1225#endif 1226 1227 identify_cpu(); /* Final stage of CPU initialization */ 1228 initializecpu(); /* Initialize CPU registers */ 1229 1230 /* make an initial tss so cpu can get interrupt stack on syscall! */ 1231 common_tss[0].tss_rsp0 = thread0.td_kstack + \ 1232 KSTACK_PAGES * PAGE_SIZE - sizeof(struct pcb); 1233 /* Ensure the stack is aligned to 16 bytes */ 1234 common_tss[0].tss_rsp0 &= ~0xFul; 1235 PCPU_SET(rsp0, common_tss[0].tss_rsp0); 1236 1237 /* doublefault stack space, runs on ist1 */ 1238 common_tss[0].tss_ist1 = (long)&dblfault_stack[sizeof(dblfault_stack)]; 1239 1240 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL); 1241 ltr(gsel_tss); 1242 1243 /* Set up the fast syscall stuff */ 1244 msr = rdmsr(MSR_EFER) | EFER_SCE; 1245 wrmsr(MSR_EFER, msr); 1246 wrmsr(MSR_LSTAR, (u_int64_t)IDTVEC(fast_syscall)); 1247 wrmsr(MSR_CSTAR, (u_int64_t)IDTVEC(fast_syscall32)); 1248 msr = ((u_int64_t)GSEL(GCODE_SEL, SEL_KPL) << 32) | 1249 ((u_int64_t)GSEL(GUCODE32_SEL, SEL_UPL) << 48); 1250 wrmsr(MSR_STAR, msr); 1251 wrmsr(MSR_SF_MASK, PSL_NT|PSL_T|PSL_I|PSL_C|PSL_D); 1252 1253 getmemsize(kmdp, physfree); 1254 init_param2(physmem); 1255 1256 /* now running on new page tables, configured,and u/iom is accessible */ 1257 1258 /* Map the message buffer. */ 1259 for (off = 0; off < round_page(MSGBUF_SIZE); off += PAGE_SIZE) 1260 pmap_kenter((vm_offset_t)msgbufp + off, avail_end + off); 1261 1262 msgbufinit(msgbufp, MSGBUF_SIZE); 1263 fpuinit(); 1264 1265 /* transfer to user mode */ 1266 1267 _ucodesel = GSEL(GUCODE_SEL, SEL_UPL); 1268 _udatasel = GSEL(GUDATA_SEL, SEL_UPL); 1269 _ucode32sel = GSEL(GUCODE32_SEL, SEL_UPL); 1270 1271 /* setup proc 0's pcb */ 1272 thread0.td_pcb->pcb_flags = 0; /* XXXKSE */ 1273 thread0.td_pcb->pcb_cr3 = KPML4phys; 1274 thread0.td_frame = &proc0_tf; 1275 1276 env = getenv("kernelname"); 1277 if (env != NULL) 1278 strlcpy(kernelname, env, sizeof(kernelname)); 1279 1280 /* Location of kernel stack for locore */ 1281 return ((u_int64_t)thread0.td_pcb); 1282} 1283 1284void 1285cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size) 1286{ 1287 1288 pcpu->pc_acpi_id = 0xffffffff; 1289} 1290 1291/* 1292 * Construct a PCB from a trapframe. This is called from kdb_trap() where 1293 * we want to start a backtrace from the function that caused us to enter 1294 * the debugger. We have the context in the trapframe, but base the trace 1295 * on the PCB. The PCB doesn't have to be perfect, as long as it contains 1296 * enough for a backtrace. 1297 */ 1298void 1299makectx(struct trapframe *tf, struct pcb *pcb) 1300{ 1301 1302 pcb->pcb_r12 = tf->tf_r12; 1303 pcb->pcb_r13 = tf->tf_r13; 1304 pcb->pcb_r14 = tf->tf_r14; 1305 pcb->pcb_r15 = tf->tf_r15; 1306 pcb->pcb_rbp = tf->tf_rbp; 1307 pcb->pcb_rbx = tf->tf_rbx; 1308 pcb->pcb_rip = tf->tf_rip; 1309 pcb->pcb_rsp = (ISPL(tf->tf_cs)) ? tf->tf_rsp : (long)(tf + 1) - 8; 1310} 1311 1312int 1313ptrace_set_pc(struct thread *td, unsigned long addr) 1314{ 1315 td->td_frame->tf_rip = addr; 1316 return (0); 1317} 1318 1319int 1320ptrace_single_step(struct thread *td) 1321{ 1322 td->td_frame->tf_rflags |= PSL_T; 1323 return (0); 1324} 1325 1326int 1327ptrace_clear_single_step(struct thread *td) 1328{ 1329 td->td_frame->tf_rflags &= ~PSL_T; 1330 return (0); 1331} 1332 1333int 1334fill_regs(struct thread *td, struct reg *regs) 1335{ 1336 struct pcb *pcb; 1337 struct trapframe *tp; 1338 1339 tp = td->td_frame; 1340 regs->r_r15 = tp->tf_r15; 1341 regs->r_r14 = tp->tf_r14; 1342 regs->r_r13 = tp->tf_r13; 1343 regs->r_r12 = tp->tf_r12; 1344 regs->r_r11 = tp->tf_r11; 1345 regs->r_r10 = tp->tf_r10; 1346 regs->r_r9 = tp->tf_r9; 1347 regs->r_r8 = tp->tf_r8; 1348 regs->r_rdi = tp->tf_rdi; 1349 regs->r_rsi = tp->tf_rsi; 1350 regs->r_rbp = tp->tf_rbp; 1351 regs->r_rbx = tp->tf_rbx; 1352 regs->r_rdx = tp->tf_rdx; 1353 regs->r_rcx = tp->tf_rcx; 1354 regs->r_rax = tp->tf_rax; 1355 regs->r_rip = tp->tf_rip; 1356 regs->r_cs = tp->tf_cs; 1357 regs->r_rflags = tp->tf_rflags; 1358 regs->r_rsp = tp->tf_rsp; 1359 regs->r_ss = tp->tf_ss; 1360 pcb = td->td_pcb; 1361 return (0); 1362} 1363 1364int 1365set_regs(struct thread *td, struct reg *regs) 1366{ 1367 struct pcb *pcb; 1368 struct trapframe *tp; 1369 register_t rflags; 1370 1371 tp = td->td_frame; 1372 rflags = regs->r_rflags & 0xffffffff; 1373 if (!EFL_SECURE(rflags, tp->tf_rflags) || !CS_SECURE(regs->r_cs)) 1374 return (EINVAL); 1375 tp->tf_r15 = regs->r_r15; 1376 tp->tf_r14 = regs->r_r14; 1377 tp->tf_r13 = regs->r_r13; 1378 tp->tf_r12 = regs->r_r12; 1379 tp->tf_r11 = regs->r_r11; 1380 tp->tf_r10 = regs->r_r10; 1381 tp->tf_r9 = regs->r_r9; 1382 tp->tf_r8 = regs->r_r8; 1383 tp->tf_rdi = regs->r_rdi; 1384 tp->tf_rsi = regs->r_rsi; 1385 tp->tf_rbp = regs->r_rbp; 1386 tp->tf_rbx = regs->r_rbx; 1387 tp->tf_rdx = regs->r_rdx; 1388 tp->tf_rcx = regs->r_rcx; 1389 tp->tf_rax = regs->r_rax; 1390 tp->tf_rip = regs->r_rip; 1391 tp->tf_cs = regs->r_cs; 1392 tp->tf_rflags = rflags; 1393 tp->tf_rsp = regs->r_rsp; 1394 tp->tf_ss = regs->r_ss; 1395 pcb = td->td_pcb; 1396 return (0); 1397} 1398 1399/* XXX check all this stuff! */ 1400/* externalize from sv_xmm */ 1401static void 1402fill_fpregs_xmm(struct savefpu *sv_xmm, struct fpreg *fpregs) 1403{ 1404 struct envxmm *penv_fpreg = (struct envxmm *)&fpregs->fpr_env; 1405 struct envxmm *penv_xmm = &sv_xmm->sv_env; 1406 int i; 1407 1408 /* pcb -> fpregs */ 1409 bzero(fpregs, sizeof(*fpregs)); 1410 1411 /* FPU control/status */ 1412 penv_fpreg->en_cw = penv_xmm->en_cw; 1413 penv_fpreg->en_sw = penv_xmm->en_sw; 1414 penv_fpreg->en_tw = penv_xmm->en_tw; 1415 penv_fpreg->en_opcode = penv_xmm->en_opcode; 1416 penv_fpreg->en_rip = penv_xmm->en_rip; 1417 penv_fpreg->en_rdp = penv_xmm->en_rdp; 1418 penv_fpreg->en_mxcsr = penv_xmm->en_mxcsr; 1419 penv_fpreg->en_mxcsr_mask = penv_xmm->en_mxcsr_mask; 1420 1421 /* FPU registers */ 1422 for (i = 0; i < 8; ++i) 1423 bcopy(sv_xmm->sv_fp[i].fp_acc.fp_bytes, fpregs->fpr_acc[i], 10); 1424 1425 /* SSE registers */ 1426 for (i = 0; i < 16; ++i) 1427 bcopy(sv_xmm->sv_xmm[i].xmm_bytes, fpregs->fpr_xacc[i], 16); 1428} 1429 1430/* internalize from fpregs into sv_xmm */ 1431static void 1432set_fpregs_xmm(struct fpreg *fpregs, struct savefpu *sv_xmm) 1433{ 1434 struct envxmm *penv_xmm = &sv_xmm->sv_env; 1435 struct envxmm *penv_fpreg = (struct envxmm *)&fpregs->fpr_env; 1436 int i; 1437 1438 /* fpregs -> pcb */ 1439 /* FPU control/status */ 1440 penv_xmm->en_cw = penv_fpreg->en_cw; 1441 penv_xmm->en_sw = penv_fpreg->en_sw; 1442 penv_xmm->en_tw = penv_fpreg->en_tw; 1443 penv_xmm->en_opcode = penv_fpreg->en_opcode; 1444 penv_xmm->en_rip = penv_fpreg->en_rip; 1445 penv_xmm->en_rdp = penv_fpreg->en_rdp; 1446 penv_xmm->en_mxcsr = penv_fpreg->en_mxcsr; 1447 penv_xmm->en_mxcsr_mask = penv_fpreg->en_mxcsr_mask; 1448 1449 /* FPU registers */ 1450 for (i = 0; i < 8; ++i) 1451 bcopy(fpregs->fpr_acc[i], sv_xmm->sv_fp[i].fp_acc.fp_bytes, 10); 1452 1453 /* SSE registers */ 1454 for (i = 0; i < 16; ++i) 1455 bcopy(fpregs->fpr_xacc[i], sv_xmm->sv_xmm[i].xmm_bytes, 16); 1456} 1457 1458/* externalize from td->pcb */ 1459int 1460fill_fpregs(struct thread *td, struct fpreg *fpregs) 1461{ 1462 1463 fill_fpregs_xmm(&td->td_pcb->pcb_save, fpregs); 1464 return (0); 1465} 1466 1467/* internalize to td->pcb */ 1468int 1469set_fpregs(struct thread *td, struct fpreg *fpregs) 1470{ 1471 1472 set_fpregs_xmm(fpregs, &td->td_pcb->pcb_save); 1473 return (0); 1474} 1475 1476/* 1477 * Get machine context. 1478 */ 1479int 1480get_mcontext(struct thread *td, mcontext_t *mcp, int flags) 1481{ 1482 struct trapframe *tp; 1483 1484 tp = td->td_frame; 1485 PROC_LOCK(curthread->td_proc); 1486 mcp->mc_onstack = sigonstack(tp->tf_rsp); 1487 PROC_UNLOCK(curthread->td_proc); 1488 mcp->mc_r15 = tp->tf_r15; 1489 mcp->mc_r14 = tp->tf_r14; 1490 mcp->mc_r13 = tp->tf_r13; 1491 mcp->mc_r12 = tp->tf_r12; 1492 mcp->mc_r11 = tp->tf_r11; 1493 mcp->mc_r10 = tp->tf_r10; 1494 mcp->mc_r9 = tp->tf_r9; 1495 mcp->mc_r8 = tp->tf_r8; 1496 mcp->mc_rdi = tp->tf_rdi; 1497 mcp->mc_rsi = tp->tf_rsi; 1498 mcp->mc_rbp = tp->tf_rbp; 1499 mcp->mc_rbx = tp->tf_rbx; 1500 mcp->mc_rcx = tp->tf_rcx; 1501 if (flags & GET_MC_CLEAR_RET) { 1502 mcp->mc_rax = 0; 1503 mcp->mc_rdx = 0; 1504 } else { 1505 mcp->mc_rax = tp->tf_rax; 1506 mcp->mc_rdx = tp->tf_rdx; 1507 } 1508 mcp->mc_rip = tp->tf_rip; 1509 mcp->mc_cs = tp->tf_cs; 1510 mcp->mc_rflags = tp->tf_rflags; 1511 mcp->mc_rsp = tp->tf_rsp; 1512 mcp->mc_ss = tp->tf_ss; 1513 mcp->mc_len = sizeof(*mcp); 1514 get_fpcontext(td, mcp); 1515 return (0); 1516} 1517 1518/* 1519 * Set machine context. 1520 * 1521 * However, we don't set any but the user modifiable flags, and we won't 1522 * touch the cs selector. 1523 */ 1524int 1525set_mcontext(struct thread *td, const mcontext_t *mcp) 1526{ 1527 struct trapframe *tp; 1528 long rflags; 1529 int ret; 1530 1531 tp = td->td_frame; 1532 if (mcp->mc_len != sizeof(*mcp)) 1533 return (EINVAL); 1534 rflags = (mcp->mc_rflags & PSL_USERCHANGE) | 1535 (tp->tf_rflags & ~PSL_USERCHANGE); 1536 ret = set_fpcontext(td, mcp); 1537 if (ret != 0) 1538 return (ret); 1539 tp->tf_r15 = mcp->mc_r15; 1540 tp->tf_r14 = mcp->mc_r14; 1541 tp->tf_r13 = mcp->mc_r13; 1542 tp->tf_r12 = mcp->mc_r12; 1543 tp->tf_r11 = mcp->mc_r11; 1544 tp->tf_r10 = mcp->mc_r10; 1545 tp->tf_r9 = mcp->mc_r9; 1546 tp->tf_r8 = mcp->mc_r8; 1547 tp->tf_rdi = mcp->mc_rdi; 1548 tp->tf_rsi = mcp->mc_rsi; 1549 tp->tf_rbp = mcp->mc_rbp; 1550 tp->tf_rbx = mcp->mc_rbx; 1551 tp->tf_rdx = mcp->mc_rdx; 1552 tp->tf_rcx = mcp->mc_rcx; 1553 tp->tf_rax = mcp->mc_rax; 1554 tp->tf_rip = mcp->mc_rip; 1555 tp->tf_rflags = rflags; 1556 tp->tf_rsp = mcp->mc_rsp; 1557 tp->tf_ss = mcp->mc_ss; 1558 td->td_pcb->pcb_flags |= PCB_FULLCTX; 1559 return (0); 1560} 1561 1562static void 1563get_fpcontext(struct thread *td, mcontext_t *mcp) 1564{ 1565 1566 mcp->mc_ownedfp = fpugetregs(td, (struct savefpu *)&mcp->mc_fpstate); 1567 mcp->mc_fpformat = fpuformat(); 1568} 1569 1570static int 1571set_fpcontext(struct thread *td, const mcontext_t *mcp) 1572{ 1573 1574 if (mcp->mc_fpformat == _MC_FPFMT_NODEV) 1575 return (0); 1576 else if (mcp->mc_fpformat != _MC_FPFMT_XMM) 1577 return (EINVAL); 1578 else if (mcp->mc_ownedfp == _MC_FPOWNED_NONE) 1579 /* We don't care what state is left in the FPU or PCB. */ 1580 fpstate_drop(td); 1581 else if (mcp->mc_ownedfp == _MC_FPOWNED_FPU || 1582 mcp->mc_ownedfp == _MC_FPOWNED_PCB) { 1583 /* 1584 * XXX we violate the dubious requirement that fpusetregs() 1585 * be called with interrupts disabled. 1586 * XXX obsolete on trap-16 systems? 1587 */ 1588 fpusetregs(td, (struct savefpu *)&mcp->mc_fpstate); 1589 } else 1590 return (EINVAL); 1591 return (0); 1592} 1593 1594void 1595fpstate_drop(struct thread *td) 1596{ 1597 register_t s; 1598 1599 s = intr_disable(); 1600 if (PCPU_GET(fpcurthread) == td) 1601 fpudrop(); 1602 /* 1603 * XXX force a full drop of the fpu. The above only drops it if we 1604 * owned it. 1605 * 1606 * XXX I don't much like fpugetregs()'s semantics of doing a full 1607 * drop. Dropping only to the pcb matches fnsave's behaviour. 1608 * We only need to drop to !PCB_INITDONE in sendsig(). But 1609 * sendsig() is the only caller of fpugetregs()... perhaps we just 1610 * have too many layers. 1611 */ 1612 curthread->td_pcb->pcb_flags &= ~PCB_FPUINITDONE; 1613 intr_restore(s); 1614} 1615 1616int 1617fill_dbregs(struct thread *td, struct dbreg *dbregs) 1618{ 1619 struct pcb *pcb; 1620 1621 if (td == NULL) { 1622 dbregs->dr[0] = rdr0(); 1623 dbregs->dr[1] = rdr1(); 1624 dbregs->dr[2] = rdr2(); 1625 dbregs->dr[3] = rdr3(); 1626 dbregs->dr[6] = rdr6(); 1627 dbregs->dr[7] = rdr7(); 1628 } else { 1629 pcb = td->td_pcb; 1630 dbregs->dr[0] = pcb->pcb_dr0; 1631 dbregs->dr[1] = pcb->pcb_dr1; 1632 dbregs->dr[2] = pcb->pcb_dr2; 1633 dbregs->dr[3] = pcb->pcb_dr3; 1634 dbregs->dr[6] = pcb->pcb_dr6; 1635 dbregs->dr[7] = pcb->pcb_dr7; 1636 } 1637 dbregs->dr[4] = 0; 1638 dbregs->dr[5] = 0; 1639 dbregs->dr[8] = 0; 1640 dbregs->dr[9] = 0; 1641 dbregs->dr[10] = 0; 1642 dbregs->dr[11] = 0; 1643 dbregs->dr[12] = 0; 1644 dbregs->dr[13] = 0; 1645 dbregs->dr[14] = 0; 1646 dbregs->dr[15] = 0; 1647 return (0); 1648} 1649 1650int 1651set_dbregs(struct thread *td, struct dbreg *dbregs) 1652{ 1653 struct pcb *pcb; 1654 int i; 1655 u_int64_t mask1, mask2; 1656 1657 if (td == NULL) { 1658 load_dr0(dbregs->dr[0]); 1659 load_dr1(dbregs->dr[1]); 1660 load_dr2(dbregs->dr[2]); 1661 load_dr3(dbregs->dr[3]); 1662 load_dr6(dbregs->dr[6]); 1663 load_dr7(dbregs->dr[7]); 1664 } else { 1665 /* 1666 * Don't let an illegal value for dr7 get set. Specifically, 1667 * check for undefined settings. Setting these bit patterns 1668 * result in undefined behaviour and can lead to an unexpected 1669 * TRCTRAP or a general protection fault right here. 1670 */ 1671 for (i = 0, mask1 = 0x3<<16, mask2 = 0x2<<16; i < 8; 1672 i++, mask1 <<= 2, mask2 <<= 2) 1673 if ((dbregs->dr[7] & mask1) == mask2) 1674 return (EINVAL); 1675 1676 pcb = td->td_pcb; 1677 1678 /* 1679 * Don't let a process set a breakpoint that is not within the 1680 * process's address space. If a process could do this, it 1681 * could halt the system by setting a breakpoint in the kernel 1682 * (if ddb was enabled). Thus, we need to check to make sure 1683 * that no breakpoints are being enabled for addresses outside 1684 * process's address space, unless, perhaps, we were called by 1685 * uid 0. 1686 * 1687 * XXX - what about when the watched area of the user's 1688 * address space is written into from within the kernel 1689 * ... wouldn't that still cause a breakpoint to be generated 1690 * from within kernel mode? 1691 */ 1692 1693 if (suser(td) != 0) { 1694 if (dbregs->dr[7] & 0x3) { 1695 /* dr0 is enabled */ 1696 if (dbregs->dr[0] >= VM_MAXUSER_ADDRESS) 1697 return (EINVAL); 1698 } 1699 if (dbregs->dr[7] & 0x3<<2) { 1700 /* dr1 is enabled */ 1701 if (dbregs->dr[1] >= VM_MAXUSER_ADDRESS) 1702 return (EINVAL); 1703 } 1704 if (dbregs->dr[7] & 0x3<<4) { 1705 /* dr2 is enabled */ 1706 if (dbregs->dr[2] >= VM_MAXUSER_ADDRESS) 1707 return (EINVAL); 1708 } 1709 if (dbregs->dr[7] & 0x3<<6) { 1710 /* dr3 is enabled */ 1711 if (dbregs->dr[3] >= VM_MAXUSER_ADDRESS) 1712 return (EINVAL); 1713 } 1714 } 1715 1716 pcb->pcb_dr0 = dbregs->dr[0]; 1717 pcb->pcb_dr1 = dbregs->dr[1]; 1718 pcb->pcb_dr2 = dbregs->dr[2]; 1719 pcb->pcb_dr3 = dbregs->dr[3]; 1720 pcb->pcb_dr6 = dbregs->dr[6]; 1721 pcb->pcb_dr7 = dbregs->dr[7]; 1722 1723 pcb->pcb_flags |= PCB_DBREGS; 1724 } 1725 1726 return (0); 1727} 1728 1729void 1730reset_dbregs(void) 1731{ 1732 1733 load_dr7(0); /* Turn off the control bits first */ 1734 load_dr0(0); 1735 load_dr1(0); 1736 load_dr2(0); 1737 load_dr3(0); 1738 load_dr6(0); 1739} 1740 1741/* 1742 * Return > 0 if a hardware breakpoint has been hit, and the 1743 * breakpoint was in user space. Return 0, otherwise. 1744 */ 1745int 1746user_dbreg_trap(void) 1747{ 1748 u_int64_t dr7, dr6; /* debug registers dr6 and dr7 */ 1749 u_int64_t bp; /* breakpoint bits extracted from dr6 */ 1750 int nbp; /* number of breakpoints that triggered */ 1751 caddr_t addr[4]; /* breakpoint addresses */ 1752 int i; 1753 1754 dr7 = rdr7(); 1755 if ((dr7 & 0x000000ff) == 0) { 1756 /* 1757 * all GE and LE bits in the dr7 register are zero, 1758 * thus the trap couldn't have been caused by the 1759 * hardware debug registers 1760 */ 1761 return 0; 1762 } 1763 1764 nbp = 0; 1765 dr6 = rdr6(); 1766 bp = dr6 & 0x0000000f; 1767 1768 if (!bp) { 1769 /* 1770 * None of the breakpoint bits are set meaning this 1771 * trap was not caused by any of the debug registers 1772 */ 1773 return 0; 1774 } 1775 1776 /* 1777 * at least one of the breakpoints were hit, check to see 1778 * which ones and if any of them are user space addresses 1779 */ 1780 1781 if (bp & 0x01) { 1782 addr[nbp++] = (caddr_t)rdr0(); 1783 } 1784 if (bp & 0x02) { 1785 addr[nbp++] = (caddr_t)rdr1(); 1786 } 1787 if (bp & 0x04) { 1788 addr[nbp++] = (caddr_t)rdr2(); 1789 } 1790 if (bp & 0x08) { 1791 addr[nbp++] = (caddr_t)rdr3(); 1792 } 1793 1794 for (i=0; i<nbp; i++) { 1795 if (addr[i] < 1796 (caddr_t)VM_MAXUSER_ADDRESS) { 1797 /* 1798 * addr[i] is in user space 1799 */ 1800 return nbp; 1801 } 1802 } 1803 1804 /* 1805 * None of the breakpoints are in user space. 1806 */ 1807 return 0; 1808} 1809 1810#ifdef KDB 1811 1812/* 1813 * Provide inb() and outb() as functions. They are normally only 1814 * available as macros calling inlined functions, thus cannot be 1815 * called from the debugger. 1816 * 1817 * The actual code is stolen from <machine/cpufunc.h>, and de-inlined. 1818 */ 1819 1820#undef inb 1821#undef outb 1822 1823/* silence compiler warnings */ 1824u_char inb(u_int); 1825void outb(u_int, u_char); 1826 1827u_char 1828inb(u_int port) 1829{ 1830 u_char data; 1831 /* 1832 * We use %%dx and not %1 here because i/o is done at %dx and not at 1833 * %edx, while gcc generates inferior code (movw instead of movl) 1834 * if we tell it to load (u_short) port. 1835 */ 1836 __asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port)); 1837 return (data); 1838} 1839 1840void 1841outb(u_int port, u_char data) 1842{ 1843 u_char al; 1844 /* 1845 * Use an unnecessary assignment to help gcc's register allocator. 1846 * This make a large difference for gcc-1.40 and a tiny difference 1847 * for gcc-2.6.0. For gcc-1.40, al had to be ``asm("ax")'' for 1848 * best results. gcc-2.6.0 can't handle this. 1849 */ 1850 al = data; 1851 __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port)); 1852} 1853 1854#endif /* KDB */ 1855