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