machdep.c revision 82939
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 82939 2001-09-04 08:42:35Z 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_npx.h" 51#include "opt_perfmon.h" 52#include "opt_upages.h" 53/* #include "opt_userconfig.h" */ 54 55#include <sys/param.h> 56#include <sys/systm.h> 57#include <sys/sysproto.h> 58#include <sys/signalvar.h> 59#include <sys/kernel.h> 60#include <sys/ktr.h> 61#include <sys/linker.h> 62#include <sys/lock.h> 63#include <sys/malloc.h> 64#include <sys/mutex.h> 65#include <sys/pcpu.h> 66#include <sys/proc.h> 67#include <sys/bio.h> 68#include <sys/buf.h> 69#include <sys/reboot.h> 70#include <sys/smp.h> 71#include <sys/callout.h> 72#include <sys/msgbuf.h> 73#include <sys/sysent.h> 74#include <sys/sysctl.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 <sys/lock.h> 82#include <vm/vm_kern.h> 83#include <vm/vm_object.h> 84#include <vm/vm_page.h> 85#include <vm/vm_map.h> 86#include <vm/vm_pager.h> 87#include <vm/vm_extern.h> 88 89#include <sys/user.h> 90#include <sys/exec.h> 91#include <sys/cons.h> 92 93#include <ddb/ddb.h> 94 95#include <net/netisr.h> 96 97#include <machine/cpu.h> 98#include <machine/cputypes.h> 99#include <machine/reg.h> 100#include <machine/clock.h> 101#include <machine/specialreg.h> 102#include <machine/bootinfo.h> 103#include <machine/md_var.h> 104#include <machine/pc/bios.h> 105#include <machine/pcb_ext.h> /* pcb.h included via sys/user.h */ 106#include <machine/globals.h> 107#ifdef PERFMON 108#include <machine/perfmon.h> 109#endif 110#ifdef SMP 111#include <machine/privatespace.h> 112#endif 113 114#include <i386/isa/icu.h> 115#include <i386/isa/intr_machdep.h> 116#include <isa/rtc.h> 117#include <machine/vm86.h> 118#include <sys/ptrace.h> 119#include <machine/sigframe.h> 120 121extern void init386 __P((int first)); 122extern void dblfault_handler __P((void)); 123 124extern void printcpuinfo(void); /* XXX header file */ 125extern void earlysetcpuclass(void); /* same header file */ 126extern void finishidentcpu(void); 127extern void panicifcpuunsupported(void); 128extern void initializecpu(void); 129 130#define CS_SECURE(cs) (ISPL(cs) == SEL_UPL) 131#define EFL_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0) 132 133static void cpu_startup __P((void *)); 134#ifdef CPU_ENABLE_SSE 135static void set_fpregs_xmm __P((struct save87 *, struct savexmm *)); 136static void fill_fpregs_xmm __P((struct savexmm *, struct save87 *)); 137#endif /* CPU_ENABLE_SSE */ 138SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL) 139 140int _udatasel, _ucodesel; 141u_int atdevbase; 142 143#if defined(SWTCH_OPTIM_STATS) 144extern int swtch_optim_stats; 145SYSCTL_INT(_debug, OID_AUTO, swtch_optim_stats, 146 CTLFLAG_RD, &swtch_optim_stats, 0, ""); 147SYSCTL_INT(_debug, OID_AUTO, tlb_flush_count, 148 CTLFLAG_RD, &tlb_flush_count, 0, ""); 149#endif 150 151#ifdef PC98 152static int ispc98 = 1; 153#else 154static int ispc98 = 0; 155#endif 156SYSCTL_INT(_machdep, OID_AUTO, ispc98, CTLFLAG_RD, &ispc98, 0, ""); 157 158int physmem = 0; 159int cold = 1; 160 161#ifdef COMPAT_43 162static void osendsig __P((sig_t catcher, int sig, sigset_t *mask, u_long code)); 163#endif 164 165static int 166sysctl_hw_physmem(SYSCTL_HANDLER_ARGS) 167{ 168 int error = sysctl_handle_int(oidp, 0, ctob(physmem), req); 169 return (error); 170} 171 172SYSCTL_PROC(_hw, HW_PHYSMEM, physmem, CTLTYPE_INT|CTLFLAG_RD, 173 0, 0, sysctl_hw_physmem, "IU", ""); 174 175static int 176sysctl_hw_usermem(SYSCTL_HANDLER_ARGS) 177{ 178 int error = sysctl_handle_int(oidp, 0, 179 ctob(physmem - cnt.v_wire_count), req); 180 return (error); 181} 182 183SYSCTL_PROC(_hw, HW_USERMEM, usermem, CTLTYPE_INT|CTLFLAG_RD, 184 0, 0, sysctl_hw_usermem, "IU", ""); 185 186static int 187sysctl_hw_availpages(SYSCTL_HANDLER_ARGS) 188{ 189 int error = sysctl_handle_int(oidp, 0, 190 i386_btop(avail_end - avail_start), req); 191 return (error); 192} 193 194SYSCTL_PROC(_hw, OID_AUTO, availpages, CTLTYPE_INT|CTLFLAG_RD, 195 0, 0, sysctl_hw_availpages, "I", ""); 196 197int Maxmem = 0; 198long dumplo; 199 200vm_offset_t phys_avail[10]; 201 202/* must be 2 less so 0 0 can signal end of chunks */ 203#define PHYS_AVAIL_ARRAY_END ((sizeof(phys_avail) / sizeof(vm_offset_t)) - 2) 204 205struct kva_md_info kmi; 206 207static struct trapframe proc0_tf; 208#ifndef SMP 209static struct globaldata __globaldata; 210#endif 211 212struct mtx sched_lock; 213struct mtx Giant; 214 215static void 216cpu_startup(dummy) 217 void *dummy; 218{ 219 /* 220 * Good {morning,afternoon,evening,night}. 221 */ 222 earlysetcpuclass(); 223 startrtclock(); 224 printcpuinfo(); 225 panicifcpuunsupported(); 226#ifdef PERFMON 227 perfmon_init(); 228#endif 229 printf("real memory = %u (%uK bytes)\n", ptoa(Maxmem), 230 ptoa(Maxmem) / 1024); 231 /* 232 * Display any holes after the first chunk of extended memory. 233 */ 234 if (bootverbose) { 235 int indx; 236 237 printf("Physical memory chunk(s):\n"); 238 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) { 239 unsigned int size1; 240 241 size1 = phys_avail[indx + 1] - phys_avail[indx]; 242 printf("0x%08x - 0x%08x, %u bytes (%u pages)\n", 243 phys_avail[indx], phys_avail[indx + 1] - 1, size1, 244 size1 / PAGE_SIZE); 245 } 246 } 247 248 vm_ksubmap_init(&kmi); 249 250#if defined(USERCONFIG) 251 userconfig(); 252 cninit(); /* the preferred console may have changed */ 253#endif 254 255 printf("avail memory = %u (%uK bytes)\n", ptoa(cnt.v_free_count), 256 ptoa(cnt.v_free_count) / 1024); 257 258 /* 259 * Set up buffers, so they can be used to read disk labels. 260 */ 261 bufinit(); 262 vm_pager_bufferinit(); 263 264 globaldata_register(GLOBALDATA); 265#ifndef SMP 266 /* For SMP, we delay the cpu_setregs() until after SMP startup. */ 267 cpu_setregs(); 268#endif 269} 270 271/* 272 * Send an interrupt to process. 273 * 274 * Stack is set up to allow sigcode stored 275 * at top to call routine, followed by kcall 276 * to sigreturn routine below. After sigreturn 277 * resets the signal mask, the stack, and the 278 * frame pointer, it returns to the user 279 * specified pc, psl. 280 */ 281#ifdef COMPAT_43 282static void 283osendsig(catcher, sig, mask, code) 284 sig_t catcher; 285 int sig; 286 sigset_t *mask; 287 u_long code; 288{ 289 struct osigframe sf; 290 struct osigframe *fp; 291 struct proc *p; 292 struct sigacts *psp; 293 struct trapframe *regs; 294 int oonstack; 295 296 p = curproc; 297 PROC_LOCK(p); 298 psp = p->p_sigacts; 299 regs = p->p_frame; 300 oonstack = sigonstack(regs->tf_esp); 301 302 /* Allocate and validate space for the signal handler context. */ 303 if ((p->p_flag & P_ALTSTACK) && !oonstack && 304 SIGISMEMBER(psp->ps_sigonstack, sig)) { 305 fp = (struct osigframe *)(p->p_sigstk.ss_sp + 306 p->p_sigstk.ss_size - sizeof(struct osigframe)); 307#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 308 p->p_sigstk.ss_flags |= SS_ONSTACK; 309#endif 310 } else 311 fp = (struct osigframe *)regs->tf_esp - 1; 312 PROC_UNLOCK(p); 313 314 /* 315 * grow_stack() will return 0 if *fp does not fit inside the stack 316 * and the stack can not be grown. 317 * useracc() will return FALSE if access is denied. 318 */ 319 if (grow_stack(p, (int)fp) == 0 || 320 !useracc((caddr_t)fp, sizeof(*fp), VM_PROT_WRITE)) { 321 /* 322 * Process has trashed its stack; give it an illegal 323 * instruction to halt it in its tracks. 324 */ 325 PROC_LOCK(p); 326 SIGACTION(p, SIGILL) = SIG_DFL; 327 SIGDELSET(p->p_sigignore, SIGILL); 328 SIGDELSET(p->p_sigcatch, SIGILL); 329 SIGDELSET(p->p_sigmask, SIGILL); 330 psignal(p, SIGILL); 331 PROC_UNLOCK(p); 332 return; 333 } 334 335 /* Translate the signal if appropriate. */ 336 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize) 337 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)]; 338 339 /* Build the argument list for the signal handler. */ 340 sf.sf_signum = sig; 341 sf.sf_scp = (register_t)&fp->sf_siginfo.si_sc; 342 PROC_LOCK(p); 343 if (SIGISMEMBER(p->p_sigacts->ps_siginfo, sig)) { 344 /* Signal handler installed with SA_SIGINFO. */ 345 sf.sf_arg2 = (register_t)&fp->sf_siginfo; 346 sf.sf_siginfo.si_signo = sig; 347 sf.sf_siginfo.si_code = code; 348 sf.sf_ahu.sf_action = (__osiginfohandler_t *)catcher; 349 } else { 350 /* Old FreeBSD-style arguments. */ 351 sf.sf_arg2 = code; 352 sf.sf_addr = regs->tf_err; 353 sf.sf_ahu.sf_handler = catcher; 354 } 355 PROC_UNLOCK(p); 356 357 /* Save most if not all of trap frame. */ 358 sf.sf_siginfo.si_sc.sc_eax = regs->tf_eax; 359 sf.sf_siginfo.si_sc.sc_ebx = regs->tf_ebx; 360 sf.sf_siginfo.si_sc.sc_ecx = regs->tf_ecx; 361 sf.sf_siginfo.si_sc.sc_edx = regs->tf_edx; 362 sf.sf_siginfo.si_sc.sc_esi = regs->tf_esi; 363 sf.sf_siginfo.si_sc.sc_edi = regs->tf_edi; 364 sf.sf_siginfo.si_sc.sc_cs = regs->tf_cs; 365 sf.sf_siginfo.si_sc.sc_ds = regs->tf_ds; 366 sf.sf_siginfo.si_sc.sc_ss = regs->tf_ss; 367 sf.sf_siginfo.si_sc.sc_es = regs->tf_es; 368 sf.sf_siginfo.si_sc.sc_fs = regs->tf_fs; 369 sf.sf_siginfo.si_sc.sc_gs = rgs(); 370 sf.sf_siginfo.si_sc.sc_isp = regs->tf_isp; 371 372 /* Build the signal context to be used by osigreturn(). */ 373 sf.sf_siginfo.si_sc.sc_onstack = (oonstack) ? 1 : 0; 374 SIG2OSIG(*mask, sf.sf_siginfo.si_sc.sc_mask); 375 sf.sf_siginfo.si_sc.sc_sp = regs->tf_esp; 376 sf.sf_siginfo.si_sc.sc_fp = regs->tf_ebp; 377 sf.sf_siginfo.si_sc.sc_pc = regs->tf_eip; 378 sf.sf_siginfo.si_sc.sc_ps = regs->tf_eflags; 379 sf.sf_siginfo.si_sc.sc_trapno = regs->tf_trapno; 380 sf.sf_siginfo.si_sc.sc_err = regs->tf_err; 381 382 /* 383 * If we're a vm86 process, we want to save the segment registers. 384 * We also change eflags to be our emulated eflags, not the actual 385 * eflags. 386 */ 387 if (regs->tf_eflags & PSL_VM) { 388 /* XXX confusing names: `tf' isn't a trapframe; `regs' is. */ 389 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs; 390 struct vm86_kernel *vm86 = &p->p_addr->u_pcb.pcb_ext->ext_vm86; 391 392 sf.sf_siginfo.si_sc.sc_gs = tf->tf_vm86_gs; 393 sf.sf_siginfo.si_sc.sc_fs = tf->tf_vm86_fs; 394 sf.sf_siginfo.si_sc.sc_es = tf->tf_vm86_es; 395 sf.sf_siginfo.si_sc.sc_ds = tf->tf_vm86_ds; 396 397 if (vm86->vm86_has_vme == 0) 398 sf.sf_siginfo.si_sc.sc_ps = 399 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) | 400 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP)); 401 402 /* See sendsig() for comments. */ 403 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_T | PSL_VIF | PSL_VIP); 404 } 405 406 /* Copy the sigframe out to the user's stack. */ 407 if (copyout(&sf, fp, sizeof(*fp)) != 0) { 408 /* 409 * Something is wrong with the stack pointer. 410 * ...Kill the process. 411 */ 412 PROC_LOCK(p); 413 sigexit(p, SIGILL); 414 /* NOTREACHED */ 415 } 416 417 regs->tf_esp = (int)fp; 418 regs->tf_eip = PS_STRINGS - szosigcode; 419 regs->tf_cs = _ucodesel; 420 regs->tf_ds = _udatasel; 421 regs->tf_es = _udatasel; 422 regs->tf_fs = _udatasel; 423 load_gs(_udatasel); 424 regs->tf_ss = _udatasel; 425} 426#endif 427 428void 429sendsig(catcher, sig, mask, code) 430 sig_t catcher; 431 int sig; 432 sigset_t *mask; 433 u_long code; 434{ 435 struct sigframe sf; 436 struct proc *p; 437 struct sigacts *psp; 438 struct trapframe *regs; 439 struct sigframe *sfp; 440 int oonstack; 441 442 p = curproc; 443 PROC_LOCK(p); 444 psp = p->p_sigacts; 445#ifdef COMPAT_43 446 if (SIGISMEMBER(psp->ps_osigset, sig)) { 447 PROC_UNLOCK(p); 448 osendsig(catcher, sig, mask, code); 449 return; 450 } 451#endif 452 regs = p->p_frame; 453 oonstack = sigonstack(regs->tf_esp); 454 455 /* Save user context. */ 456 bzero(&sf, sizeof(sf)); 457 sf.sf_uc.uc_sigmask = *mask; 458 sf.sf_uc.uc_stack = p->p_sigstk; 459 sf.sf_uc.uc_stack.ss_flags = (p->p_flag & P_ALTSTACK) 460 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE; 461 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0; 462 sf.sf_uc.uc_mcontext.mc_gs = rgs(); 463 bcopy(regs, &sf.sf_uc.uc_mcontext.mc_fs, sizeof(*regs)); 464 465 /* Allocate and validate space for the signal handler context. */ 466 if ((p->p_flag & P_ALTSTACK) != 0 && !oonstack && 467 SIGISMEMBER(psp->ps_sigonstack, sig)) { 468 sfp = (struct sigframe *)(p->p_sigstk.ss_sp + 469 p->p_sigstk.ss_size - sizeof(struct sigframe)); 470#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 471 p->p_sigstk.ss_flags |= SS_ONSTACK; 472#endif 473 } else 474 sfp = (struct sigframe *)regs->tf_esp - 1; 475 PROC_UNLOCK(p); 476 477 /* 478 * grow_stack() will return 0 if *sfp does not fit inside the stack 479 * and the stack can not be grown. 480 * useracc() will return FALSE if access is denied. 481 */ 482 if (grow_stack(p, (int)sfp) == 0 || 483 !useracc((caddr_t)sfp, sizeof(*sfp), VM_PROT_WRITE)) { 484 /* 485 * Process has trashed its stack; give it an illegal 486 * instruction to halt it in its tracks. 487 */ 488#ifdef DEBUG 489 printf("process %d has trashed its stack\n", p->p_pid); 490#endif 491 PROC_LOCK(p); 492 SIGACTION(p, SIGILL) = SIG_DFL; 493 SIGDELSET(p->p_sigignore, SIGILL); 494 SIGDELSET(p->p_sigcatch, SIGILL); 495 SIGDELSET(p->p_sigmask, SIGILL); 496 psignal(p, SIGILL); 497 PROC_UNLOCK(p); 498 return; 499 } 500 501 /* Translate the signal if appropriate. */ 502 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize) 503 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)]; 504 505 /* Build the argument list for the signal handler. */ 506 sf.sf_signum = sig; 507 sf.sf_ucontext = (register_t)&sfp->sf_uc; 508 PROC_LOCK(p); 509 if (SIGISMEMBER(p->p_sigacts->ps_siginfo, sig)) { 510 /* Signal handler installed with SA_SIGINFO. */ 511 sf.sf_siginfo = (register_t)&sfp->sf_si; 512 sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher; 513 514 /* Fill siginfo structure. */ 515 sf.sf_si.si_signo = sig; 516 sf.sf_si.si_code = code; 517 sf.sf_si.si_addr = (void *)regs->tf_err; 518 } else { 519 /* Old FreeBSD-style arguments. */ 520 sf.sf_siginfo = code; 521 sf.sf_addr = regs->tf_err; 522 sf.sf_ahu.sf_handler = catcher; 523 } 524 PROC_UNLOCK(p); 525 526 /* 527 * If we're a vm86 process, we want to save the segment registers. 528 * We also change eflags to be our emulated eflags, not the actual 529 * eflags. 530 */ 531 if (regs->tf_eflags & PSL_VM) { 532 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs; 533 struct vm86_kernel *vm86 = &p->p_addr->u_pcb.pcb_ext->ext_vm86; 534 535 sf.sf_uc.uc_mcontext.mc_gs = tf->tf_vm86_gs; 536 sf.sf_uc.uc_mcontext.mc_fs = tf->tf_vm86_fs; 537 sf.sf_uc.uc_mcontext.mc_es = tf->tf_vm86_es; 538 sf.sf_uc.uc_mcontext.mc_ds = tf->tf_vm86_ds; 539 540 if (vm86->vm86_has_vme == 0) 541 sf.sf_uc.uc_mcontext.mc_eflags = 542 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) | 543 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP)); 544 545 /* 546 * We should never have PSL_T set when returning from vm86 547 * mode. It may be set here if we deliver a signal before 548 * getting to vm86 mode, so turn it off. 549 * 550 * Clear PSL_NT to inhibit T_TSSFLT faults on return from 551 * syscalls made by the signal handler. This just avoids 552 * wasting time for our lazy fixup of such faults. PSL_NT 553 * does nothing in vm86 mode, but vm86 programs can set it 554 * almost legitimately in probes for old cpu types. 555 */ 556 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_T | PSL_VIF | PSL_VIP); 557 } 558 559 /* Copy the sigframe out to the user's stack. */ 560 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) { 561 /* 562 * Something is wrong with the stack pointer. 563 * ...Kill the process. 564 */ 565 PROC_LOCK(p); 566 sigexit(p, SIGILL); 567 /* NOTREACHED */ 568 } 569 570 regs->tf_esp = (int)sfp; 571 regs->tf_eip = PS_STRINGS - *(p->p_sysent->sv_szsigcode); 572 regs->tf_cs = _ucodesel; 573 regs->tf_ds = _udatasel; 574 regs->tf_es = _udatasel; 575 regs->tf_fs = _udatasel; 576 regs->tf_ss = _udatasel; 577} 578 579/* 580 * System call to cleanup state after a signal 581 * has been taken. Reset signal mask and 582 * stack state from context left by sendsig (above). 583 * Return to previous pc and psl as specified by 584 * context left by sendsig. Check carefully to 585 * make sure that the user has not modified the 586 * state to gain improper privileges. 587 */ 588#ifdef COMPAT_43 589int 590osigreturn(p, uap) 591 struct proc *p; 592 struct osigreturn_args /* { 593 struct osigcontext *sigcntxp; 594 } */ *uap; 595{ 596 struct trapframe *regs; 597 struct osigcontext *scp; 598 int eflags; 599 600 regs = p->p_frame; 601 scp = uap->sigcntxp; 602 if (!useracc((caddr_t)scp, sizeof(*scp), VM_PROT_READ)) 603 return (EFAULT); 604 eflags = scp->sc_ps; 605 if (eflags & PSL_VM) { 606 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs; 607 struct vm86_kernel *vm86; 608 609 /* 610 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't 611 * set up the vm86 area, and we can't enter vm86 mode. 612 */ 613 if (p->p_addr->u_pcb.pcb_ext == 0) 614 return (EINVAL); 615 vm86 = &p->p_addr->u_pcb.pcb_ext->ext_vm86; 616 if (vm86->vm86_inited == 0) 617 return (EINVAL); 618 619 /* Go back to user mode if both flags are set. */ 620 if ((eflags & PSL_VIP) && (eflags & PSL_VIF)) 621 trapsignal(p, SIGBUS, 0); 622 623 if (vm86->vm86_has_vme) { 624 eflags = (tf->tf_eflags & ~VME_USERCHANGE) | 625 (eflags & VME_USERCHANGE) | PSL_VM; 626 } else { 627 vm86->vm86_eflags = eflags; /* save VIF, VIP */ 628 eflags = (tf->tf_eflags & ~VM_USERCHANGE) | 629 (eflags & VM_USERCHANGE) | PSL_VM; 630 } 631 tf->tf_vm86_ds = scp->sc_ds; 632 tf->tf_vm86_es = scp->sc_es; 633 tf->tf_vm86_fs = scp->sc_fs; 634 tf->tf_vm86_gs = scp->sc_gs; 635 tf->tf_ds = _udatasel; 636 tf->tf_es = _udatasel; 637 tf->tf_fs = _udatasel; 638 } else { 639 /* 640 * Don't allow users to change privileged or reserved flags. 641 */ 642 /* 643 * XXX do allow users to change the privileged flag PSL_RF. 644 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers 645 * should sometimes set it there too. tf_eflags is kept in 646 * the signal context during signal handling and there is no 647 * other place to remember it, so the PSL_RF bit may be 648 * corrupted by the signal handler without us knowing. 649 * Corruption of the PSL_RF bit at worst causes one more or 650 * one less debugger trap, so allowing it is fairly harmless. 651 */ 652 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) { 653 return (EINVAL); 654 } 655 656 /* 657 * Don't allow users to load a valid privileged %cs. Let the 658 * hardware check for invalid selectors, excess privilege in 659 * other selectors, invalid %eip's and invalid %esp's. 660 */ 661 if (!CS_SECURE(scp->sc_cs)) { 662 trapsignal(p, SIGBUS, T_PROTFLT); 663 return (EINVAL); 664 } 665 regs->tf_ds = scp->sc_ds; 666 regs->tf_es = scp->sc_es; 667 regs->tf_fs = scp->sc_fs; 668 } 669 670 /* Restore remaining registers. */ 671 regs->tf_eax = scp->sc_eax; 672 regs->tf_ebx = scp->sc_ebx; 673 regs->tf_ecx = scp->sc_ecx; 674 regs->tf_edx = scp->sc_edx; 675 regs->tf_esi = scp->sc_esi; 676 regs->tf_edi = scp->sc_edi; 677 regs->tf_cs = scp->sc_cs; 678 regs->tf_ss = scp->sc_ss; 679 regs->tf_isp = scp->sc_isp; 680 681 PROC_LOCK(p); 682#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 683 if (scp->sc_onstack & 1) 684 p->p_sigstk.ss_flags |= SS_ONSTACK; 685 else 686 p->p_sigstk.ss_flags &= ~SS_ONSTACK; 687#endif 688 689 SIGSETOLD(p->p_sigmask, scp->sc_mask); 690 SIG_CANTMASK(p->p_sigmask); 691 PROC_UNLOCK(p); 692 regs->tf_ebp = scp->sc_fp; 693 regs->tf_esp = scp->sc_sp; 694 regs->tf_eip = scp->sc_pc; 695 regs->tf_eflags = eflags; 696 return (EJUSTRETURN); 697} 698#endif 699 700int 701sigreturn(p, uap) 702 struct proc *p; 703 struct sigreturn_args /* { 704 ucontext_t *sigcntxp; 705 } */ *uap; 706{ 707 struct trapframe *regs; 708 ucontext_t *ucp; 709 int cs, eflags; 710 711 ucp = uap->sigcntxp; 712#ifdef COMPAT_43 713 if (!useracc((caddr_t)ucp, sizeof(struct osigcontext), VM_PROT_READ)) 714 return (EFAULT); 715 if (((struct osigcontext *)ucp)->sc_trapno == 0x01d516) 716 return (osigreturn(p, (struct osigreturn_args *)uap)); 717 /* 718 * Since ucp is not an osigcontext but a ucontext_t, we have to 719 * check again if all of it is accessible. A ucontext_t is 720 * much larger, so instead of just checking for the pointer 721 * being valid for the size of an osigcontext, now check for 722 * it being valid for a whole, new-style ucontext_t. 723 */ 724#endif 725 if (!useracc((caddr_t)ucp, sizeof(*ucp), VM_PROT_READ)) 726 return (EFAULT); 727 728 regs = p->p_frame; 729 eflags = ucp->uc_mcontext.mc_eflags; 730 if (eflags & PSL_VM) { 731 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs; 732 struct vm86_kernel *vm86; 733 734 /* 735 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't 736 * set up the vm86 area, and we can't enter vm86 mode. 737 */ 738 if (p->p_addr->u_pcb.pcb_ext == 0) 739 return (EINVAL); 740 vm86 = &p->p_addr->u_pcb.pcb_ext->ext_vm86; 741 if (vm86->vm86_inited == 0) 742 return (EINVAL); 743 744 /* Go back to user mode if both flags are set. */ 745 if ((eflags & PSL_VIP) && (eflags & PSL_VIF)) 746 trapsignal(p, SIGBUS, 0); 747 748 if (vm86->vm86_has_vme) { 749 eflags = (tf->tf_eflags & ~VME_USERCHANGE) | 750 (eflags & VME_USERCHANGE) | PSL_VM; 751 } else { 752 vm86->vm86_eflags = eflags; /* save VIF, VIP */ 753 eflags = (tf->tf_eflags & ~VM_USERCHANGE) | 754 (eflags & VM_USERCHANGE) | PSL_VM; 755 } 756 bcopy(&ucp->uc_mcontext.mc_fs, tf, sizeof(struct trapframe)); 757 tf->tf_eflags = eflags; 758 tf->tf_vm86_ds = tf->tf_ds; 759 tf->tf_vm86_es = tf->tf_es; 760 tf->tf_vm86_fs = tf->tf_fs; 761 tf->tf_vm86_gs = ucp->uc_mcontext.mc_gs; 762 tf->tf_ds = _udatasel; 763 tf->tf_es = _udatasel; 764 tf->tf_fs = _udatasel; 765 } else { 766 /* 767 * Don't allow users to change privileged or reserved flags. 768 */ 769 /* 770 * XXX do allow users to change the privileged flag PSL_RF. 771 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers 772 * should sometimes set it there too. tf_eflags is kept in 773 * the signal context during signal handling and there is no 774 * other place to remember it, so the PSL_RF bit may be 775 * corrupted by the signal handler without us knowing. 776 * Corruption of the PSL_RF bit at worst causes one more or 777 * one less debugger trap, so allowing it is fairly harmless. 778 */ 779 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) { 780 printf("sigreturn: eflags = 0x%x\n", eflags); 781 return (EINVAL); 782 } 783 784 /* 785 * Don't allow users to load a valid privileged %cs. Let the 786 * hardware check for invalid selectors, excess privilege in 787 * other selectors, invalid %eip's and invalid %esp's. 788 */ 789 cs = ucp->uc_mcontext.mc_cs; 790 if (!CS_SECURE(cs)) { 791 printf("sigreturn: cs = 0x%x\n", cs); 792 trapsignal(p, SIGBUS, T_PROTFLT); 793 return (EINVAL); 794 } 795 796 bcopy(&ucp->uc_mcontext.mc_fs, regs, sizeof(*regs)); 797 } 798 799 PROC_LOCK(p); 800#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 801 if (ucp->uc_mcontext.mc_onstack & 1) 802 p->p_sigstk.ss_flags |= SS_ONSTACK; 803 else 804 p->p_sigstk.ss_flags &= ~SS_ONSTACK; 805#endif 806 807 p->p_sigmask = ucp->uc_sigmask; 808 SIG_CANTMASK(p->p_sigmask); 809 PROC_UNLOCK(p); 810 return (EJUSTRETURN); 811} 812 813/* 814 * Machine dependent boot() routine 815 * 816 * I haven't seen anything to put here yet 817 * Possibly some stuff might be grafted back here from boot() 818 */ 819void 820cpu_boot(int howto) 821{ 822} 823 824/* 825 * Shutdown the CPU as much as possible 826 */ 827void 828cpu_halt(void) 829{ 830 for (;;) 831 __asm__ ("hlt"); 832} 833 834/* 835 * Hook to idle the CPU when possible. This currently only works in 836 * the !SMP case, as there is no clean way to ensure that a CPU will be 837 * woken when there is work available for it. 838 */ 839static int cpu_idle_hlt = 1; 840SYSCTL_INT(_machdep, OID_AUTO, cpu_idle_hlt, CTLFLAG_RW, 841 &cpu_idle_hlt, 0, "Idle loop HLT enable"); 842 843/* 844 * Note that we have to be careful here to avoid a race between checking 845 * procrunnable() and actually halting. If we don't do this, we may waste 846 * the time between calling hlt and the next interrupt even though there 847 * is a runnable process. 848 */ 849void 850cpu_idle(void) 851{ 852#ifndef SMP 853 if (cpu_idle_hlt) { 854 disable_intr(); 855 if (procrunnable()) 856 enable_intr(); 857 else { 858 enable_intr(); 859 __asm __volatile("hlt"); 860 } 861 } 862#endif 863} 864 865/* 866 * Clear registers on exec 867 */ 868void 869setregs(p, entry, stack, ps_strings) 870 struct proc *p; 871 u_long entry; 872 u_long stack; 873 u_long ps_strings; 874{ 875 struct trapframe *regs = p->p_frame; 876 struct pcb *pcb = &p->p_addr->u_pcb; 877 878 if (pcb->pcb_ldt) 879 user_ldt_free(pcb); 880 881 bzero((char *)regs, sizeof(struct trapframe)); 882 regs->tf_eip = entry; 883 regs->tf_esp = stack; 884 regs->tf_eflags = PSL_USER | (regs->tf_eflags & PSL_T); 885 regs->tf_ss = _udatasel; 886 regs->tf_ds = _udatasel; 887 regs->tf_es = _udatasel; 888 regs->tf_fs = _udatasel; 889 regs->tf_cs = _ucodesel; 890 891 /* PS_STRINGS value for BSD/OS binaries. It is 0 for non-BSD/OS. */ 892 regs->tf_ebx = ps_strings; 893 894 /* reset %gs as well */ 895 if (pcb == PCPU_GET(curpcb)) 896 load_gs(_udatasel); 897 else 898 pcb->pcb_gs = _udatasel; 899 900 /* 901 * Reset the hardware debug registers if they were in use. 902 * They won't have any meaning for the newly exec'd process. 903 */ 904 if (pcb->pcb_flags & PCB_DBREGS) { 905 pcb->pcb_dr0 = 0; 906 pcb->pcb_dr1 = 0; 907 pcb->pcb_dr2 = 0; 908 pcb->pcb_dr3 = 0; 909 pcb->pcb_dr6 = 0; 910 pcb->pcb_dr7 = 0; 911 if (pcb == PCPU_GET(curpcb)) { 912 /* 913 * Clear the debug registers on the running 914 * CPU, otherwise they will end up affecting 915 * the next process we switch to. 916 */ 917 reset_dbregs(); 918 } 919 pcb->pcb_flags &= ~PCB_DBREGS; 920 } 921 922 /* 923 * Initialize the math emulator (if any) for the current process. 924 * Actually, just clear the bit that says that the emulator has 925 * been initialized. Initialization is delayed until the process 926 * traps to the emulator (if it is done at all) mainly because 927 * emulators don't provide an entry point for initialization. 928 */ 929 p->p_addr->u_pcb.pcb_flags &= ~FP_SOFTFP; 930 931 /* 932 * Arrange to trap the next npx or `fwait' instruction (see npx.c 933 * for why fwait must be trapped at least if there is an npx or an 934 * emulator). This is mainly to handle the case where npx0 is not 935 * configured, since the npx routines normally set up the trap 936 * otherwise. It should be done only at boot time, but doing it 937 * here allows modifying `npx_exists' for testing the emulator on 938 * systems with an npx. 939 */ 940 load_cr0(rcr0() | CR0_MP | CR0_TS); 941 942#ifdef DEV_NPX 943 /* Initialize the npx (if any) for the current process. */ 944 npxinit(__INITIAL_NPXCW__); 945#endif 946 947 /* 948 * XXX - Linux emulator 949 * Make sure sure edx is 0x0 on entry. Linux binaries depend 950 * on it. 951 */ 952 p->p_retval[1] = 0; 953} 954 955void 956cpu_setregs(void) 957{ 958 unsigned int cr0; 959 960 cr0 = rcr0(); 961 cr0 |= CR0_NE; /* Done by npxinit() */ 962 cr0 |= CR0_MP | CR0_TS; /* Done at every execve() too. */ 963#ifndef I386_CPU 964 cr0 |= CR0_WP | CR0_AM; 965#endif 966 load_cr0(cr0); 967 load_gs(_udatasel); 968} 969 970static int 971sysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS) 972{ 973 int error; 974 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, 975 req); 976 if (!error && req->newptr) 977 resettodr(); 978 return (error); 979} 980 981SYSCTL_PROC(_machdep, CPU_ADJKERNTZ, adjkerntz, CTLTYPE_INT|CTLFLAG_RW, 982 &adjkerntz, 0, sysctl_machdep_adjkerntz, "I", ""); 983 984SYSCTL_INT(_machdep, CPU_DISRTCSET, disable_rtc_set, 985 CTLFLAG_RW, &disable_rtc_set, 0, ""); 986 987SYSCTL_STRUCT(_machdep, CPU_BOOTINFO, bootinfo, 988 CTLFLAG_RD, &bootinfo, bootinfo, ""); 989 990SYSCTL_INT(_machdep, CPU_WALLCLOCK, wall_cmos_clock, 991 CTLFLAG_RW, &wall_cmos_clock, 0, ""); 992 993/* 994 * Initialize 386 and configure to run kernel 995 */ 996 997/* 998 * Initialize segments & interrupt table 999 */ 1000 1001int _default_ldt; 1002union descriptor gdt[NGDT * MAXCPU]; /* global descriptor table */ 1003static struct gate_descriptor idt0[NIDT]; 1004struct gate_descriptor *idt = &idt0[0]; /* interrupt descriptor table */ 1005union descriptor ldt[NLDT]; /* local descriptor table */ 1006#ifdef SMP 1007/* table descriptors - used to load tables by microp */ 1008struct region_descriptor r_gdt, r_idt; 1009#endif 1010 1011int private_tss; /* flag indicating private tss */ 1012 1013#if defined(I586_CPU) && !defined(NO_F00F_HACK) 1014extern int has_f00f_bug; 1015#endif 1016 1017static struct i386tss dblfault_tss; 1018static char dblfault_stack[PAGE_SIZE]; 1019 1020extern struct user *proc0paddr; 1021 1022 1023/* software prototypes -- in more palatable form */ 1024struct soft_segment_descriptor gdt_segs[] = { 1025/* GNULL_SEL 0 Null Descriptor */ 1026{ 0x0, /* segment base address */ 1027 0x0, /* length */ 1028 0, /* segment type */ 1029 0, /* segment descriptor priority level */ 1030 0, /* segment descriptor present */ 1031 0, 0, 1032 0, /* default 32 vs 16 bit size */ 1033 0 /* limit granularity (byte/page units)*/ }, 1034/* GCODE_SEL 1 Code Descriptor for kernel */ 1035{ 0x0, /* segment base address */ 1036 0xfffff, /* length - all address space */ 1037 SDT_MEMERA, /* segment type */ 1038 0, /* segment descriptor priority level */ 1039 1, /* segment descriptor present */ 1040 0, 0, 1041 1, /* default 32 vs 16 bit size */ 1042 1 /* limit granularity (byte/page units)*/ }, 1043/* GDATA_SEL 2 Data Descriptor for kernel */ 1044{ 0x0, /* segment base address */ 1045 0xfffff, /* length - all address space */ 1046 SDT_MEMRWA, /* segment type */ 1047 0, /* segment descriptor priority level */ 1048 1, /* segment descriptor present */ 1049 0, 0, 1050 1, /* default 32 vs 16 bit size */ 1051 1 /* limit granularity (byte/page units)*/ }, 1052/* GPRIV_SEL 3 SMP Per-Processor Private Data Descriptor */ 1053{ 0x0, /* segment base address */ 1054 0xfffff, /* length - all address space */ 1055 SDT_MEMRWA, /* segment type */ 1056 0, /* segment descriptor priority level */ 1057 1, /* segment descriptor present */ 1058 0, 0, 1059 1, /* default 32 vs 16 bit size */ 1060 1 /* limit granularity (byte/page units)*/ }, 1061/* GPROC0_SEL 4 Proc 0 Tss Descriptor */ 1062{ 1063 0x0, /* segment base address */ 1064 sizeof(struct i386tss)-1,/* length - all address space */ 1065 SDT_SYS386TSS, /* segment type */ 1066 0, /* segment descriptor priority level */ 1067 1, /* segment descriptor present */ 1068 0, 0, 1069 0, /* unused - default 32 vs 16 bit size */ 1070 0 /* limit granularity (byte/page units)*/ }, 1071/* GLDT_SEL 5 LDT Descriptor */ 1072{ (int) ldt, /* segment base address */ 1073 sizeof(ldt)-1, /* length - all address space */ 1074 SDT_SYSLDT, /* segment type */ 1075 SEL_UPL, /* segment descriptor priority level */ 1076 1, /* segment descriptor present */ 1077 0, 0, 1078 0, /* unused - default 32 vs 16 bit size */ 1079 0 /* limit granularity (byte/page units)*/ }, 1080/* GUSERLDT_SEL 6 User LDT Descriptor per process */ 1081{ (int) ldt, /* segment base address */ 1082 (512 * sizeof(union descriptor)-1), /* length */ 1083 SDT_SYSLDT, /* segment type */ 1084 0, /* segment descriptor priority level */ 1085 1, /* segment descriptor present */ 1086 0, 0, 1087 0, /* unused - default 32 vs 16 bit size */ 1088 0 /* limit granularity (byte/page units)*/ }, 1089/* GTGATE_SEL 7 Null Descriptor - Placeholder */ 1090{ 0x0, /* segment base address */ 1091 0x0, /* length - all address space */ 1092 0, /* segment type */ 1093 0, /* segment descriptor priority level */ 1094 0, /* segment descriptor present */ 1095 0, 0, 1096 0, /* default 32 vs 16 bit size */ 1097 0 /* limit granularity (byte/page units)*/ }, 1098/* GBIOSLOWMEM_SEL 8 BIOS access to realmode segment 0x40, must be #8 in GDT */ 1099{ 0x400, /* segment base address */ 1100 0xfffff, /* length */ 1101 SDT_MEMRWA, /* segment type */ 1102 0, /* segment descriptor priority level */ 1103 1, /* segment descriptor present */ 1104 0, 0, 1105 1, /* default 32 vs 16 bit size */ 1106 1 /* limit granularity (byte/page units)*/ }, 1107/* GPANIC_SEL 9 Panic Tss Descriptor */ 1108{ (int) &dblfault_tss, /* segment base address */ 1109 sizeof(struct i386tss)-1,/* length - all address space */ 1110 SDT_SYS386TSS, /* segment type */ 1111 0, /* segment descriptor priority level */ 1112 1, /* segment descriptor present */ 1113 0, 0, 1114 0, /* unused - default 32 vs 16 bit size */ 1115 0 /* limit granularity (byte/page units)*/ }, 1116/* GBIOSCODE32_SEL 10 BIOS 32-bit interface (32bit Code) */ 1117{ 0, /* segment base address (overwritten) */ 1118 0xfffff, /* length */ 1119 SDT_MEMERA, /* segment type */ 1120 0, /* segment descriptor priority level */ 1121 1, /* segment descriptor present */ 1122 0, 0, 1123 0, /* default 32 vs 16 bit size */ 1124 1 /* limit granularity (byte/page units)*/ }, 1125/* GBIOSCODE16_SEL 11 BIOS 32-bit interface (16bit Code) */ 1126{ 0, /* segment base address (overwritten) */ 1127 0xfffff, /* length */ 1128 SDT_MEMERA, /* segment type */ 1129 0, /* segment descriptor priority level */ 1130 1, /* segment descriptor present */ 1131 0, 0, 1132 0, /* default 32 vs 16 bit size */ 1133 1 /* limit granularity (byte/page units)*/ }, 1134/* GBIOSDATA_SEL 12 BIOS 32-bit interface (Data) */ 1135{ 0, /* segment base address (overwritten) */ 1136 0xfffff, /* length */ 1137 SDT_MEMRWA, /* segment type */ 1138 0, /* segment descriptor priority level */ 1139 1, /* segment descriptor present */ 1140 0, 0, 1141 1, /* default 32 vs 16 bit size */ 1142 1 /* limit granularity (byte/page units)*/ }, 1143/* GBIOSUTIL_SEL 13 BIOS 16-bit interface (Utility) */ 1144{ 0, /* segment base address (overwritten) */ 1145 0xfffff, /* length */ 1146 SDT_MEMRWA, /* segment type */ 1147 0, /* segment descriptor priority level */ 1148 1, /* segment descriptor present */ 1149 0, 0, 1150 0, /* default 32 vs 16 bit size */ 1151 1 /* limit granularity (byte/page units)*/ }, 1152/* GBIOSARGS_SEL 14 BIOS 16-bit interface (Arguments) */ 1153{ 0, /* segment base address (overwritten) */ 1154 0xfffff, /* length */ 1155 SDT_MEMRWA, /* segment type */ 1156 0, /* segment descriptor priority level */ 1157 1, /* segment descriptor present */ 1158 0, 0, 1159 0, /* default 32 vs 16 bit size */ 1160 1 /* limit granularity (byte/page units)*/ }, 1161}; 1162 1163static struct soft_segment_descriptor ldt_segs[] = { 1164 /* Null Descriptor - overwritten by call gate */ 1165{ 0x0, /* segment base address */ 1166 0x0, /* length - all address space */ 1167 0, /* segment type */ 1168 0, /* segment descriptor priority level */ 1169 0, /* segment descriptor present */ 1170 0, 0, 1171 0, /* default 32 vs 16 bit size */ 1172 0 /* limit granularity (byte/page units)*/ }, 1173 /* Null Descriptor - overwritten by call gate */ 1174{ 0x0, /* segment base address */ 1175 0x0, /* length - all address space */ 1176 0, /* segment type */ 1177 0, /* segment descriptor priority level */ 1178 0, /* segment descriptor present */ 1179 0, 0, 1180 0, /* default 32 vs 16 bit size */ 1181 0 /* limit granularity (byte/page units)*/ }, 1182 /* Null Descriptor - overwritten by call gate */ 1183{ 0x0, /* segment base address */ 1184 0x0, /* length - all address space */ 1185 0, /* segment type */ 1186 0, /* segment descriptor priority level */ 1187 0, /* segment descriptor present */ 1188 0, 0, 1189 0, /* default 32 vs 16 bit size */ 1190 0 /* limit granularity (byte/page units)*/ }, 1191 /* Code Descriptor for user */ 1192{ 0x0, /* segment base address */ 1193 0xfffff, /* length - all address space */ 1194 SDT_MEMERA, /* segment type */ 1195 SEL_UPL, /* segment descriptor priority level */ 1196 1, /* segment descriptor present */ 1197 0, 0, 1198 1, /* default 32 vs 16 bit size */ 1199 1 /* limit granularity (byte/page units)*/ }, 1200 /* Null Descriptor - overwritten by call gate */ 1201{ 0x0, /* segment base address */ 1202 0x0, /* length - all address space */ 1203 0, /* segment type */ 1204 0, /* segment descriptor priority level */ 1205 0, /* segment descriptor present */ 1206 0, 0, 1207 0, /* default 32 vs 16 bit size */ 1208 0 /* limit granularity (byte/page units)*/ }, 1209 /* Data Descriptor for user */ 1210{ 0x0, /* segment base address */ 1211 0xfffff, /* length - all address space */ 1212 SDT_MEMRWA, /* segment type */ 1213 SEL_UPL, /* segment descriptor priority level */ 1214 1, /* segment descriptor present */ 1215 0, 0, 1216 1, /* default 32 vs 16 bit size */ 1217 1 /* limit granularity (byte/page units)*/ }, 1218}; 1219 1220void 1221setidt(idx, func, typ, dpl, selec) 1222 int idx; 1223 inthand_t *func; 1224 int typ; 1225 int dpl; 1226 int selec; 1227{ 1228 struct gate_descriptor *ip; 1229 1230 ip = idt + idx; 1231 ip->gd_looffset = (int)func; 1232 ip->gd_selector = selec; 1233 ip->gd_stkcpy = 0; 1234 ip->gd_xx = 0; 1235 ip->gd_type = typ; 1236 ip->gd_dpl = dpl; 1237 ip->gd_p = 1; 1238 ip->gd_hioffset = ((int)func)>>16 ; 1239} 1240 1241#define IDTVEC(name) __CONCAT(X,name) 1242 1243extern inthand_t 1244 IDTVEC(div), IDTVEC(dbg), IDTVEC(nmi), IDTVEC(bpt), IDTVEC(ofl), 1245 IDTVEC(bnd), IDTVEC(ill), IDTVEC(dna), IDTVEC(fpusegm), 1246 IDTVEC(tss), IDTVEC(missing), IDTVEC(stk), IDTVEC(prot), 1247 IDTVEC(page), IDTVEC(mchk), IDTVEC(rsvd), IDTVEC(fpu), IDTVEC(align), 1248 IDTVEC(xmm), IDTVEC(lcall_syscall), IDTVEC(int0x80_syscall); 1249 1250void 1251sdtossd(sd, ssd) 1252 struct segment_descriptor *sd; 1253 struct soft_segment_descriptor *ssd; 1254{ 1255 ssd->ssd_base = (sd->sd_hibase << 24) | sd->sd_lobase; 1256 ssd->ssd_limit = (sd->sd_hilimit << 16) | sd->sd_lolimit; 1257 ssd->ssd_type = sd->sd_type; 1258 ssd->ssd_dpl = sd->sd_dpl; 1259 ssd->ssd_p = sd->sd_p; 1260 ssd->ssd_def32 = sd->sd_def32; 1261 ssd->ssd_gran = sd->sd_gran; 1262} 1263 1264#define PHYSMAP_SIZE (2 * 8) 1265 1266/* 1267 * Populate the (physmap) array with base/bound pairs describing the 1268 * available physical memory in the system, then test this memory and 1269 * build the phys_avail array describing the actually-available memory. 1270 * 1271 * If we cannot accurately determine the physical memory map, then use 1272 * value from the 0xE801 call, and failing that, the RTC. 1273 * 1274 * Total memory size may be set by the kernel environment variable 1275 * hw.physmem or the compile-time define MAXMEM. 1276 */ 1277static void 1278getmemsize(int first) 1279{ 1280 int i, physmap_idx, pa_indx; 1281 u_int basemem, extmem; 1282 struct vm86frame vmf; 1283 struct vm86context vmc; 1284 vm_offset_t pa, physmap[PHYSMAP_SIZE]; 1285 pt_entry_t pte; 1286 const char *cp; 1287 struct bios_smap *smap; 1288 1289 bzero(&vmf, sizeof(struct vm86frame)); 1290 bzero(physmap, sizeof(physmap)); 1291 1292 /* 1293 * Perform "base memory" related probes & setup 1294 */ 1295 vm86_intcall(0x12, &vmf); 1296 basemem = vmf.vmf_ax; 1297 if (basemem > 640) { 1298 printf("Preposterous BIOS basemem of %uK, truncating to 640K\n", 1299 basemem); 1300 basemem = 640; 1301 } 1302 1303 /* 1304 * XXX if biosbasemem is now < 640, there is a `hole' 1305 * between the end of base memory and the start of 1306 * ISA memory. The hole may be empty or it may 1307 * contain BIOS code or data. Map it read/write so 1308 * that the BIOS can write to it. (Memory from 0 to 1309 * the physical end of the kernel is mapped read-only 1310 * to begin with and then parts of it are remapped. 1311 * The parts that aren't remapped form holes that 1312 * remain read-only and are unused by the kernel. 1313 * The base memory area is below the physical end of 1314 * the kernel and right now forms a read-only hole. 1315 * The part of it from PAGE_SIZE to 1316 * (trunc_page(biosbasemem * 1024) - 1) will be 1317 * remapped and used by the kernel later.) 1318 * 1319 * This code is similar to the code used in 1320 * pmap_mapdev, but since no memory needs to be 1321 * allocated we simply change the mapping. 1322 */ 1323 for (pa = trunc_page(basemem * 1024); 1324 pa < ISA_HOLE_START; pa += PAGE_SIZE) { 1325 pte = (pt_entry_t)vtopte(pa + KERNBASE); 1326 *pte = pa | PG_RW | PG_V; 1327 } 1328 1329 /* 1330 * if basemem != 640, map pages r/w into vm86 page table so 1331 * that the bios can scribble on it. 1332 */ 1333 pte = (pt_entry_t)vm86paddr; 1334 for (i = basemem / 4; i < 160; i++) 1335 pte[i] = (i << PAGE_SHIFT) | PG_V | PG_RW | PG_U; 1336 1337 /* 1338 * map page 1 R/W into the kernel page table so we can use it 1339 * as a buffer. The kernel will unmap this page later. 1340 */ 1341 pte = (pt_entry_t)vtopte(KERNBASE + (1 << PAGE_SHIFT)); 1342 *pte = (1 << PAGE_SHIFT) | PG_RW | PG_V; 1343 1344 /* 1345 * get memory map with INT 15:E820 1346 */ 1347 vmc.npages = 0; 1348 smap = (void *)vm86_addpage(&vmc, 1, KERNBASE + (1 << PAGE_SHIFT)); 1349 vm86_getptr(&vmc, (vm_offset_t)smap, &vmf.vmf_es, &vmf.vmf_di); 1350 1351 physmap_idx = 0; 1352 vmf.vmf_ebx = 0; 1353 do { 1354 vmf.vmf_eax = 0xE820; 1355 vmf.vmf_edx = SMAP_SIG; 1356 vmf.vmf_ecx = sizeof(struct bios_smap); 1357 i = vm86_datacall(0x15, &vmf, &vmc); 1358 if (i || vmf.vmf_eax != SMAP_SIG) 1359 break; 1360 if (boothowto & RB_VERBOSE) 1361 printf("SMAP type=%02x base=%08x %08x len=%08x %08x\n", 1362 smap->type, 1363 *(u_int32_t *)((char *)&smap->base + 4), 1364 (u_int32_t)smap->base, 1365 *(u_int32_t *)((char *)&smap->length + 4), 1366 (u_int32_t)smap->length); 1367 1368 if (smap->type != 0x01) 1369 goto next_run; 1370 1371 if (smap->length == 0) 1372 goto next_run; 1373 1374 if (smap->base >= 0xffffffff) { 1375 printf("%uK of memory above 4GB ignored\n", 1376 (u_int)(smap->length / 1024)); 1377 goto next_run; 1378 } 1379 1380 for (i = 0; i <= physmap_idx; i += 2) { 1381 if (smap->base < physmap[i + 1]) { 1382 if (boothowto & RB_VERBOSE) 1383 printf( 1384 "Overlapping or non-montonic memory region, ignoring second region\n"); 1385 goto next_run; 1386 } 1387 } 1388 1389 if (smap->base == physmap[physmap_idx + 1]) { 1390 physmap[physmap_idx + 1] += smap->length; 1391 goto next_run; 1392 } 1393 1394 physmap_idx += 2; 1395 if (physmap_idx == PHYSMAP_SIZE) { 1396 printf( 1397 "Too many segments in the physical address map, giving up\n"); 1398 break; 1399 } 1400 physmap[physmap_idx] = smap->base; 1401 physmap[physmap_idx + 1] = smap->base + smap->length; 1402next_run: 1403 } while (vmf.vmf_ebx != 0); 1404 1405 if (physmap[1] != 0) 1406 goto physmap_done; 1407 1408 /* 1409 * If we failed above, try memory map with INT 15:E801 1410 */ 1411 vmf.vmf_ax = 0xE801; 1412 if (vm86_intcall(0x15, &vmf) == 0) { 1413 extmem = vmf.vmf_cx + vmf.vmf_dx * 64; 1414 } else { 1415#if 0 1416 vmf.vmf_ah = 0x88; 1417 vm86_intcall(0x15, &vmf); 1418 extmem = vmf.vmf_ax; 1419#else 1420 /* 1421 * Prefer the RTC value for extended memory. 1422 */ 1423 extmem = rtcin(RTC_EXTLO) + (rtcin(RTC_EXTHI) << 8); 1424#endif 1425 } 1426 1427 /* 1428 * Special hack for chipsets that still remap the 384k hole when 1429 * there's 16MB of memory - this really confuses people that 1430 * are trying to use bus mastering ISA controllers with the 1431 * "16MB limit"; they only have 16MB, but the remapping puts 1432 * them beyond the limit. 1433 * 1434 * If extended memory is between 15-16MB (16-17MB phys address range), 1435 * chop it to 15MB. 1436 */ 1437 if ((extmem > 15 * 1024) && (extmem < 16 * 1024)) 1438 extmem = 15 * 1024; 1439 1440 physmap[0] = 0; 1441 physmap[1] = basemem * 1024; 1442 physmap_idx = 2; 1443 physmap[physmap_idx] = 0x100000; 1444 physmap[physmap_idx + 1] = physmap[physmap_idx] + extmem * 1024; 1445 1446physmap_done: 1447 /* 1448 * Now, physmap contains a map of physical memory. 1449 */ 1450 1451#ifdef SMP 1452 /* make hole for AP bootstrap code */ 1453 physmap[1] = mp_bootaddress(physmap[1] / 1024); 1454 1455 /* look for the MP hardware - needed for apic addresses */ 1456 i386_mp_probe(); 1457#endif 1458 1459 /* 1460 * Maxmem isn't the "maximum memory", it's one larger than the 1461 * highest page of the physical address space. It should be 1462 * called something like "Maxphyspage". We may adjust this 1463 * based on ``hw.physmem'' and the results of the memory test. 1464 */ 1465 Maxmem = atop(physmap[physmap_idx + 1]); 1466 1467#ifdef MAXMEM 1468 Maxmem = MAXMEM / 4; 1469#endif 1470 1471 /* 1472 * hw.physmem is a size in bytes; we also allow k, m, and g suffixes 1473 * for the appropriate modifiers. This overrides MAXMEM. 1474 */ 1475 if ((cp = getenv("hw.physmem")) != NULL) { 1476 u_int64_t AllowMem, sanity; 1477 char *ep; 1478 1479 sanity = AllowMem = strtouq(cp, &ep, 0); 1480 if ((ep != cp) && (*ep != 0)) { 1481 switch(*ep) { 1482 case 'g': 1483 case 'G': 1484 AllowMem <<= 10; 1485 case 'm': 1486 case 'M': 1487 AllowMem <<= 10; 1488 case 'k': 1489 case 'K': 1490 AllowMem <<= 10; 1491 break; 1492 default: 1493 AllowMem = sanity = 0; 1494 } 1495 if (AllowMem < sanity) 1496 AllowMem = 0; 1497 } 1498 if (AllowMem == 0) 1499 printf("Ignoring invalid memory size of '%s'\n", cp); 1500 else 1501 Maxmem = atop(AllowMem); 1502 } 1503 1504 if (atop(physmap[physmap_idx + 1]) != Maxmem && 1505 (boothowto & RB_VERBOSE)) 1506 printf("Physical memory use set to %uK\n", Maxmem * 4); 1507 1508 /* 1509 * If Maxmem has been increased beyond what the system has detected, 1510 * extend the last memory segment to the new limit. 1511 */ 1512 if (atop(physmap[physmap_idx + 1]) < Maxmem) 1513 physmap[physmap_idx + 1] = ptoa(Maxmem); 1514 1515 /* call pmap initialization to make new kernel address space */ 1516 pmap_bootstrap(first, 0); 1517 1518 /* 1519 * Size up each available chunk of physical memory. 1520 */ 1521 physmap[0] = PAGE_SIZE; /* mask off page 0 */ 1522 pa_indx = 0; 1523 phys_avail[pa_indx++] = physmap[0]; 1524 phys_avail[pa_indx] = physmap[0]; 1525#if 0 1526 pte = (pt_entry_t)vtopte(KERNBASE); 1527#else 1528 pte = (pt_entry_t)CMAP1; 1529#endif 1530 1531 /* 1532 * physmap is in bytes, so when converting to page boundaries, 1533 * round up the start address and round down the end address. 1534 */ 1535 for (i = 0; i <= physmap_idx; i += 2) { 1536 vm_offset_t end; 1537 1538 end = ptoa(Maxmem); 1539 if (physmap[i + 1] < end) 1540 end = trunc_page(physmap[i + 1]); 1541 for (pa = round_page(physmap[i]); pa < end; pa += PAGE_SIZE) { 1542 int tmp, page_bad; 1543#if 0 1544 int *ptr = 0; 1545#else 1546 int *ptr = (int *)CADDR1; 1547#endif 1548 1549 /* 1550 * block out kernel memory as not available. 1551 */ 1552 if (pa >= 0x100000 && pa < first) 1553 continue; 1554 1555 page_bad = FALSE; 1556 1557 /* 1558 * map page into kernel: valid, read/write,non-cacheable 1559 */ 1560 *pte = pa | PG_V | PG_RW | PG_N; 1561 invltlb(); 1562 1563 tmp = *(int *)ptr; 1564 /* 1565 * Test for alternating 1's and 0's 1566 */ 1567 *(volatile int *)ptr = 0xaaaaaaaa; 1568 if (*(volatile int *)ptr != 0xaaaaaaaa) { 1569 page_bad = TRUE; 1570 } 1571 /* 1572 * Test for alternating 0's and 1's 1573 */ 1574 *(volatile int *)ptr = 0x55555555; 1575 if (*(volatile int *)ptr != 0x55555555) { 1576 page_bad = TRUE; 1577 } 1578 /* 1579 * Test for all 1's 1580 */ 1581 *(volatile int *)ptr = 0xffffffff; 1582 if (*(volatile int *)ptr != 0xffffffff) { 1583 page_bad = TRUE; 1584 } 1585 /* 1586 * Test for all 0's 1587 */ 1588 *(volatile int *)ptr = 0x0; 1589 if (*(volatile int *)ptr != 0x0) { 1590 page_bad = TRUE; 1591 } 1592 /* 1593 * Restore original value. 1594 */ 1595 *(int *)ptr = tmp; 1596 1597 /* 1598 * Adjust array of valid/good pages. 1599 */ 1600 if (page_bad == TRUE) { 1601 continue; 1602 } 1603 /* 1604 * If this good page is a continuation of the 1605 * previous set of good pages, then just increase 1606 * the end pointer. Otherwise start a new chunk. 1607 * Note that "end" points one higher than end, 1608 * making the range >= start and < end. 1609 * If we're also doing a speculative memory 1610 * test and we at or past the end, bump up Maxmem 1611 * so that we keep going. The first bad page 1612 * will terminate the loop. 1613 */ 1614 if (phys_avail[pa_indx] == pa) { 1615 phys_avail[pa_indx] += PAGE_SIZE; 1616 } else { 1617 pa_indx++; 1618 if (pa_indx == PHYS_AVAIL_ARRAY_END) { 1619 printf( 1620 "Too many holes in the physical address space, giving up\n"); 1621 pa_indx--; 1622 break; 1623 } 1624 phys_avail[pa_indx++] = pa; /* start */ 1625 phys_avail[pa_indx] = pa + PAGE_SIZE; /* end */ 1626 } 1627 physmem++; 1628 } 1629 } 1630 *pte = 0; 1631 invltlb(); 1632 1633 /* 1634 * XXX 1635 * The last chunk must contain at least one page plus the message 1636 * buffer to avoid complicating other code (message buffer address 1637 * calculation, etc.). 1638 */ 1639 while (phys_avail[pa_indx - 1] + PAGE_SIZE + 1640 round_page(MSGBUF_SIZE) >= phys_avail[pa_indx]) { 1641 physmem -= atop(phys_avail[pa_indx] - phys_avail[pa_indx - 1]); 1642 phys_avail[pa_indx--] = 0; 1643 phys_avail[pa_indx--] = 0; 1644 } 1645 1646 Maxmem = atop(phys_avail[pa_indx]); 1647 1648 /* Trim off space for the message buffer. */ 1649 phys_avail[pa_indx] -= round_page(MSGBUF_SIZE); 1650 1651 avail_end = phys_avail[pa_indx]; 1652} 1653 1654void 1655init386(first) 1656 int first; 1657{ 1658 struct gate_descriptor *gdp; 1659 int gsel_tss, metadata_missing, off, x; 1660#ifndef SMP 1661 /* table descriptors - used to load tables by microp */ 1662 struct region_descriptor r_gdt, r_idt; 1663#endif 1664 1665 proc0.p_addr = proc0paddr; 1666 1667 atdevbase = ISA_HOLE_START + KERNBASE; 1668 1669 metadata_missing = 0; 1670 if (bootinfo.bi_modulep) { 1671 preload_metadata = (caddr_t)bootinfo.bi_modulep + KERNBASE; 1672 preload_bootstrap_relocate(KERNBASE); 1673 } else { 1674 metadata_missing = 1; 1675 } 1676 if (envmode == 1) 1677 kern_envp = static_env; 1678 else if (bootinfo.bi_envp) 1679 kern_envp = (caddr_t)bootinfo.bi_envp + KERNBASE; 1680 1681 /* Init basic tunables, hz etc */ 1682 init_param(); 1683 1684 /* 1685 * make gdt memory segments, the code segment goes up to end of the 1686 * page with etext in it, the data segment goes to the end of 1687 * the address space 1688 */ 1689 /* 1690 * XXX text protection is temporarily (?) disabled. The limit was 1691 * i386_btop(round_page(etext)) - 1. 1692 */ 1693 gdt_segs[GCODE_SEL].ssd_limit = atop(0 - 1); 1694 gdt_segs[GDATA_SEL].ssd_limit = atop(0 - 1); 1695#ifdef SMP 1696 gdt_segs[GPRIV_SEL].ssd_limit = 1697 atop(sizeof(struct privatespace) - 1); 1698 gdt_segs[GPRIV_SEL].ssd_base = (int) &SMP_prvspace[0]; 1699 gdt_segs[GPROC0_SEL].ssd_base = 1700 (int) &SMP_prvspace[0].globaldata.gd_common_tss; 1701 SMP_prvspace[0].globaldata.gd_prvspace = &SMP_prvspace[0].globaldata; 1702#else 1703 gdt_segs[GPRIV_SEL].ssd_limit = 1704 atop(sizeof(struct globaldata) - 1); 1705 gdt_segs[GPRIV_SEL].ssd_base = (int) &__globaldata; 1706 gdt_segs[GPROC0_SEL].ssd_base = 1707 (int) &__globaldata.gd_common_tss; 1708 __globaldata.gd_prvspace = &__globaldata; 1709#endif 1710 1711 for (x = 0; x < NGDT; x++) { 1712#ifdef BDE_DEBUGGER 1713 /* avoid overwriting db entries with APM ones */ 1714 if (x >= GAPMCODE32_SEL && x <= GAPMDATA_SEL) 1715 continue; 1716#endif 1717 ssdtosd(&gdt_segs[x], &gdt[x].sd); 1718 } 1719 1720 r_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1; 1721 r_gdt.rd_base = (int) gdt; 1722 lgdt(&r_gdt); 1723 1724 /* setup curproc so that mutexes work */ 1725 PCPU_SET(curproc, &proc0); 1726 PCPU_SET(spinlocks, NULL); 1727 1728 LIST_INIT(&proc0.p_contested); 1729 1730 /* 1731 * Initialize mutexes. 1732 */ 1733 mtx_init(&Giant, "Giant", MTX_DEF | MTX_RECURSE); 1734 mtx_init(&sched_lock, "sched lock", MTX_SPIN | MTX_RECURSE); 1735 mtx_init(&proc0.p_mtx, "process lock", MTX_DEF); 1736 mtx_init(&clock_lock, "clk", MTX_SPIN | MTX_RECURSE); 1737#ifdef SMP 1738 mtx_init(&imen_mtx, "imen", MTX_SPIN); 1739#endif 1740 mtx_lock(&Giant); 1741 1742 /* make ldt memory segments */ 1743 /* 1744 * XXX - VM_MAXUSER_ADDRESS is an end address, not a max. And it 1745 * should be spelled ...MAX_USER... 1746 */ 1747 ldt_segs[LUCODE_SEL].ssd_limit = atop(VM_MAXUSER_ADDRESS - 1); 1748 ldt_segs[LUDATA_SEL].ssd_limit = atop(VM_MAXUSER_ADDRESS - 1); 1749 for (x = 0; x < sizeof ldt_segs / sizeof ldt_segs[0]; x++) 1750 ssdtosd(&ldt_segs[x], &ldt[x].sd); 1751 1752 _default_ldt = GSEL(GLDT_SEL, SEL_KPL); 1753 lldt(_default_ldt); 1754 PCPU_SET(currentldt, _default_ldt); 1755 1756 /* exceptions */ 1757 for (x = 0; x < NIDT; x++) 1758 setidt(x, &IDTVEC(rsvd), SDT_SYS386TGT, SEL_KPL, 1759 GSEL(GCODE_SEL, SEL_KPL)); 1760 setidt(0, &IDTVEC(div), SDT_SYS386TGT, SEL_KPL, 1761 GSEL(GCODE_SEL, SEL_KPL)); 1762 setidt(1, &IDTVEC(dbg), SDT_SYS386IGT, SEL_KPL, 1763 GSEL(GCODE_SEL, SEL_KPL)); 1764 setidt(2, &IDTVEC(nmi), SDT_SYS386TGT, SEL_KPL, 1765 GSEL(GCODE_SEL, SEL_KPL)); 1766 setidt(3, &IDTVEC(bpt), SDT_SYS386IGT, SEL_UPL, 1767 GSEL(GCODE_SEL, SEL_KPL)); 1768 setidt(4, &IDTVEC(ofl), SDT_SYS386TGT, SEL_UPL, 1769 GSEL(GCODE_SEL, SEL_KPL)); 1770 setidt(5, &IDTVEC(bnd), SDT_SYS386TGT, SEL_KPL, 1771 GSEL(GCODE_SEL, SEL_KPL)); 1772 setidt(6, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL, 1773 GSEL(GCODE_SEL, SEL_KPL)); 1774 setidt(7, &IDTVEC(dna), SDT_SYS386TGT, SEL_KPL 1775 , GSEL(GCODE_SEL, SEL_KPL)); 1776 setidt(8, 0, SDT_SYSTASKGT, SEL_KPL, GSEL(GPANIC_SEL, SEL_KPL)); 1777 setidt(9, &IDTVEC(fpusegm), SDT_SYS386TGT, SEL_KPL, 1778 GSEL(GCODE_SEL, SEL_KPL)); 1779 setidt(10, &IDTVEC(tss), SDT_SYS386TGT, SEL_KPL, 1780 GSEL(GCODE_SEL, SEL_KPL)); 1781 setidt(11, &IDTVEC(missing), SDT_SYS386TGT, SEL_KPL, 1782 GSEL(GCODE_SEL, SEL_KPL)); 1783 setidt(12, &IDTVEC(stk), SDT_SYS386TGT, SEL_KPL, 1784 GSEL(GCODE_SEL, SEL_KPL)); 1785 setidt(13, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL, 1786 GSEL(GCODE_SEL, SEL_KPL)); 1787 setidt(14, &IDTVEC(page), SDT_SYS386IGT, SEL_KPL, 1788 GSEL(GCODE_SEL, SEL_KPL)); 1789 setidt(15, &IDTVEC(rsvd), SDT_SYS386TGT, SEL_KPL, 1790 GSEL(GCODE_SEL, SEL_KPL)); 1791 setidt(16, &IDTVEC(fpu), SDT_SYS386TGT, SEL_KPL, 1792 GSEL(GCODE_SEL, SEL_KPL)); 1793 setidt(17, &IDTVEC(align), SDT_SYS386TGT, SEL_KPL, 1794 GSEL(GCODE_SEL, SEL_KPL)); 1795 setidt(18, &IDTVEC(mchk), SDT_SYS386TGT, SEL_KPL, 1796 GSEL(GCODE_SEL, SEL_KPL)); 1797 setidt(19, &IDTVEC(xmm), SDT_SYS386TGT, SEL_KPL, 1798 GSEL(GCODE_SEL, SEL_KPL)); 1799 setidt(0x80, &IDTVEC(int0x80_syscall), SDT_SYS386TGT, SEL_UPL, 1800 GSEL(GCODE_SEL, SEL_KPL)); 1801 1802 r_idt.rd_limit = sizeof(idt0) - 1; 1803 r_idt.rd_base = (int) idt; 1804 lidt(&r_idt); 1805 1806 /* 1807 * Initialize the console before we print anything out. 1808 */ 1809 cninit(); 1810 1811 if (metadata_missing) 1812 printf("WARNING: loader(8) metadata is missing!\n"); 1813 1814#ifdef DEV_ISA 1815 isa_defaultirq(); 1816#endif 1817 1818#ifdef DDB 1819 kdb_init(); 1820 if (boothowto & RB_KDB) 1821 Debugger("Boot flags requested debugger"); 1822#endif 1823 1824 finishidentcpu(); /* Final stage of CPU initialization */ 1825 setidt(6, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL, 1826 GSEL(GCODE_SEL, SEL_KPL)); 1827 setidt(13, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL, 1828 GSEL(GCODE_SEL, SEL_KPL)); 1829 initializecpu(); /* Initialize CPU registers */ 1830 1831 /* make an initial tss so cpu can get interrupt stack on syscall! */ 1832 PCPU_SET(common_tss.tss_esp0, 1833 (int) proc0.p_addr + UPAGES*PAGE_SIZE - 16); 1834 PCPU_SET(common_tss.tss_ss0, GSEL(GDATA_SEL, SEL_KPL)); 1835 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL); 1836 private_tss = 0; 1837 PCPU_SET(tss_gdt, &gdt[GPROC0_SEL].sd); 1838 PCPU_SET(common_tssd, *PCPU_GET(tss_gdt)); 1839 PCPU_SET(common_tss.tss_ioopt, (sizeof (struct i386tss)) << 16); 1840 ltr(gsel_tss); 1841 1842 dblfault_tss.tss_esp = dblfault_tss.tss_esp0 = dblfault_tss.tss_esp1 = 1843 dblfault_tss.tss_esp2 = (int)&dblfault_stack[sizeof(dblfault_stack)]; 1844 dblfault_tss.tss_ss = dblfault_tss.tss_ss0 = dblfault_tss.tss_ss1 = 1845 dblfault_tss.tss_ss2 = GSEL(GDATA_SEL, SEL_KPL); 1846 dblfault_tss.tss_cr3 = (int)IdlePTD; 1847 dblfault_tss.tss_eip = (int)dblfault_handler; 1848 dblfault_tss.tss_eflags = PSL_KERNEL; 1849 dblfault_tss.tss_ds = dblfault_tss.tss_es = 1850 dblfault_tss.tss_gs = GSEL(GDATA_SEL, SEL_KPL); 1851 dblfault_tss.tss_fs = GSEL(GPRIV_SEL, SEL_KPL); 1852 dblfault_tss.tss_cs = GSEL(GCODE_SEL, SEL_KPL); 1853 dblfault_tss.tss_ldt = GSEL(GLDT_SEL, SEL_KPL); 1854 1855 vm86_initialize(); 1856 getmemsize(first); 1857 1858 /* now running on new page tables, configured,and u/iom is accessible */ 1859 1860 /* Map the message buffer. */ 1861 for (off = 0; off < round_page(MSGBUF_SIZE); off += PAGE_SIZE) 1862 pmap_kenter((vm_offset_t)msgbufp + off, avail_end + off); 1863 1864 msgbufinit(msgbufp, MSGBUF_SIZE); 1865 1866 /* make a call gate to reenter kernel with */ 1867 gdp = &ldt[LSYS5CALLS_SEL].gd; 1868 1869 x = (int) &IDTVEC(lcall_syscall); 1870 gdp->gd_looffset = x; 1871 gdp->gd_selector = GSEL(GCODE_SEL,SEL_KPL); 1872 gdp->gd_stkcpy = 1; 1873 gdp->gd_type = SDT_SYS386CGT; 1874 gdp->gd_dpl = SEL_UPL; 1875 gdp->gd_p = 1; 1876 gdp->gd_hioffset = x >> 16; 1877 1878 /* XXX does this work? */ 1879 ldt[LBSDICALLS_SEL] = ldt[LSYS5CALLS_SEL]; 1880 ldt[LSOL26CALLS_SEL] = ldt[LSYS5CALLS_SEL]; 1881 1882 /* transfer to user mode */ 1883 1884 _ucodesel = LSEL(LUCODE_SEL, SEL_UPL); 1885 _udatasel = LSEL(LUDATA_SEL, SEL_UPL); 1886 1887 /* setup proc 0's pcb */ 1888 proc0.p_addr->u_pcb.pcb_flags = 0; 1889 proc0.p_addr->u_pcb.pcb_cr3 = (int)IdlePTD; 1890 proc0.p_addr->u_pcb.pcb_ext = 0; 1891 proc0.p_frame = &proc0_tf; 1892} 1893 1894#if defined(I586_CPU) && !defined(NO_F00F_HACK) 1895static void f00f_hack(void *unused); 1896SYSINIT(f00f_hack, SI_SUB_INTRINSIC, SI_ORDER_FIRST, f00f_hack, NULL); 1897 1898static void 1899f00f_hack(void *unused) { 1900 struct gate_descriptor *new_idt; 1901#ifndef SMP 1902 struct region_descriptor r_idt; 1903#endif 1904 vm_offset_t tmp; 1905 1906 if (!has_f00f_bug) 1907 return; 1908 1909 GIANT_REQUIRED; 1910 1911 printf("Intel Pentium detected, installing workaround for F00F bug\n"); 1912 1913 r_idt.rd_limit = sizeof(idt0) - 1; 1914 1915 tmp = kmem_alloc(kernel_map, PAGE_SIZE * 2); 1916 if (tmp == 0) 1917 panic("kmem_alloc returned 0"); 1918 if (((unsigned int)tmp & (PAGE_SIZE-1)) != 0) 1919 panic("kmem_alloc returned non-page-aligned memory"); 1920 /* Put the first seven entries in the lower page */ 1921 new_idt = (struct gate_descriptor*)(tmp + PAGE_SIZE - (7*8)); 1922 bcopy(idt, new_idt, sizeof(idt0)); 1923 r_idt.rd_base = (int)new_idt; 1924 lidt(&r_idt); 1925 idt = new_idt; 1926 if (vm_map_protect(kernel_map, tmp, tmp + PAGE_SIZE, 1927 VM_PROT_READ, FALSE) != KERN_SUCCESS) 1928 panic("vm_map_protect failed"); 1929 return; 1930} 1931#endif /* defined(I586_CPU) && !NO_F00F_HACK */ 1932 1933int 1934ptrace_set_pc(p, addr) 1935 struct proc *p; 1936 unsigned long addr; 1937{ 1938 p->p_frame->tf_eip = addr; 1939 return (0); 1940} 1941 1942int 1943ptrace_single_step(p) 1944 struct proc *p; 1945{ 1946 p->p_frame->tf_eflags |= PSL_T; 1947 return (0); 1948} 1949 1950int 1951fill_regs(p, regs) 1952 struct proc *p; 1953 struct reg *regs; 1954{ 1955 struct pcb *pcb; 1956 struct trapframe *tp; 1957 1958 tp = p->p_frame; 1959 regs->r_fs = tp->tf_fs; 1960 regs->r_es = tp->tf_es; 1961 regs->r_ds = tp->tf_ds; 1962 regs->r_edi = tp->tf_edi; 1963 regs->r_esi = tp->tf_esi; 1964 regs->r_ebp = tp->tf_ebp; 1965 regs->r_ebx = tp->tf_ebx; 1966 regs->r_edx = tp->tf_edx; 1967 regs->r_ecx = tp->tf_ecx; 1968 regs->r_eax = tp->tf_eax; 1969 regs->r_eip = tp->tf_eip; 1970 regs->r_cs = tp->tf_cs; 1971 regs->r_eflags = tp->tf_eflags; 1972 regs->r_esp = tp->tf_esp; 1973 regs->r_ss = tp->tf_ss; 1974 pcb = &p->p_addr->u_pcb; 1975 regs->r_gs = pcb->pcb_gs; 1976 return (0); 1977} 1978 1979int 1980set_regs(p, regs) 1981 struct proc *p; 1982 struct reg *regs; 1983{ 1984 struct pcb *pcb; 1985 struct trapframe *tp; 1986 1987 tp = p->p_frame; 1988 if (!EFL_SECURE(regs->r_eflags, tp->tf_eflags) || 1989 !CS_SECURE(regs->r_cs)) 1990 return (EINVAL); 1991 tp->tf_fs = regs->r_fs; 1992 tp->tf_es = regs->r_es; 1993 tp->tf_ds = regs->r_ds; 1994 tp->tf_edi = regs->r_edi; 1995 tp->tf_esi = regs->r_esi; 1996 tp->tf_ebp = regs->r_ebp; 1997 tp->tf_ebx = regs->r_ebx; 1998 tp->tf_edx = regs->r_edx; 1999 tp->tf_ecx = regs->r_ecx; 2000 tp->tf_eax = regs->r_eax; 2001 tp->tf_eip = regs->r_eip; 2002 tp->tf_cs = regs->r_cs; 2003 tp->tf_eflags = regs->r_eflags; 2004 tp->tf_esp = regs->r_esp; 2005 tp->tf_ss = regs->r_ss; 2006 pcb = &p->p_addr->u_pcb; 2007 pcb->pcb_gs = regs->r_gs; 2008 return (0); 2009} 2010 2011#ifdef CPU_ENABLE_SSE 2012static void 2013fill_fpregs_xmm(sv_xmm, sv_87) 2014 struct savexmm *sv_xmm; 2015 struct save87 *sv_87; 2016{ 2017 register struct env87 *penv_87 = &sv_87->sv_env; 2018 register struct envxmm *penv_xmm = &sv_xmm->sv_env; 2019 int i; 2020 2021 /* FPU control/status */ 2022 penv_87->en_cw = penv_xmm->en_cw; 2023 penv_87->en_sw = penv_xmm->en_sw; 2024 penv_87->en_tw = penv_xmm->en_tw; 2025 penv_87->en_fip = penv_xmm->en_fip; 2026 penv_87->en_fcs = penv_xmm->en_fcs; 2027 penv_87->en_opcode = penv_xmm->en_opcode; 2028 penv_87->en_foo = penv_xmm->en_foo; 2029 penv_87->en_fos = penv_xmm->en_fos; 2030 2031 /* FPU registers */ 2032 for (i = 0; i < 8; ++i) 2033 sv_87->sv_ac[i] = sv_xmm->sv_fp[i].fp_acc; 2034 2035 sv_87->sv_ex_sw = sv_xmm->sv_ex_sw; 2036} 2037 2038static void 2039set_fpregs_xmm(sv_87, sv_xmm) 2040 struct save87 *sv_87; 2041 struct savexmm *sv_xmm; 2042{ 2043 register struct env87 *penv_87 = &sv_87->sv_env; 2044 register struct envxmm *penv_xmm = &sv_xmm->sv_env; 2045 int i; 2046 2047 /* FPU control/status */ 2048 penv_xmm->en_cw = penv_87->en_cw; 2049 penv_xmm->en_sw = penv_87->en_sw; 2050 penv_xmm->en_tw = penv_87->en_tw; 2051 penv_xmm->en_fip = penv_87->en_fip; 2052 penv_xmm->en_fcs = penv_87->en_fcs; 2053 penv_xmm->en_opcode = penv_87->en_opcode; 2054 penv_xmm->en_foo = penv_87->en_foo; 2055 penv_xmm->en_fos = penv_87->en_fos; 2056 2057 /* FPU registers */ 2058 for (i = 0; i < 8; ++i) 2059 sv_xmm->sv_fp[i].fp_acc = sv_87->sv_ac[i]; 2060 2061 sv_xmm->sv_ex_sw = sv_87->sv_ex_sw; 2062} 2063#endif /* CPU_ENABLE_SSE */ 2064 2065int 2066fill_fpregs(p, fpregs) 2067 struct proc *p; 2068 struct fpreg *fpregs; 2069{ 2070#ifdef CPU_ENABLE_SSE 2071 if (cpu_fxsr) { 2072 fill_fpregs_xmm(&p->p_addr->u_pcb.pcb_save.sv_xmm, 2073 (struct save87 *)fpregs); 2074 return (0); 2075 } 2076#endif /* CPU_ENABLE_SSE */ 2077 bcopy(&p->p_addr->u_pcb.pcb_save.sv_87, fpregs, sizeof *fpregs); 2078 return (0); 2079} 2080 2081int 2082set_fpregs(p, fpregs) 2083 struct proc *p; 2084 struct fpreg *fpregs; 2085{ 2086#ifdef CPU_ENABLE_SSE 2087 if (cpu_fxsr) { 2088 set_fpregs_xmm((struct save87 *)fpregs, 2089 &p->p_addr->u_pcb.pcb_save.sv_xmm); 2090 return (0); 2091 } 2092#endif /* CPU_ENABLE_SSE */ 2093 bcopy(fpregs, &p->p_addr->u_pcb.pcb_save.sv_87, sizeof *fpregs); 2094 return (0); 2095} 2096 2097int 2098fill_dbregs(p, dbregs) 2099 struct proc *p; 2100 struct dbreg *dbregs; 2101{ 2102 struct pcb *pcb; 2103 2104 if (p == NULL) { 2105 dbregs->dr0 = rdr0(); 2106 dbregs->dr1 = rdr1(); 2107 dbregs->dr2 = rdr2(); 2108 dbregs->dr3 = rdr3(); 2109 dbregs->dr4 = rdr4(); 2110 dbregs->dr5 = rdr5(); 2111 dbregs->dr6 = rdr6(); 2112 dbregs->dr7 = rdr7(); 2113 } 2114 else { 2115 pcb = &p->p_addr->u_pcb; 2116 dbregs->dr0 = pcb->pcb_dr0; 2117 dbregs->dr1 = pcb->pcb_dr1; 2118 dbregs->dr2 = pcb->pcb_dr2; 2119 dbregs->dr3 = pcb->pcb_dr3; 2120 dbregs->dr4 = 0; 2121 dbregs->dr5 = 0; 2122 dbregs->dr6 = pcb->pcb_dr6; 2123 dbregs->dr7 = pcb->pcb_dr7; 2124 } 2125 return (0); 2126} 2127 2128int 2129set_dbregs(p, dbregs) 2130 struct proc *p; 2131 struct dbreg *dbregs; 2132{ 2133 struct pcb *pcb; 2134 int i; 2135 u_int32_t mask1, mask2; 2136 2137 if (p == NULL) { 2138 load_dr0(dbregs->dr0); 2139 load_dr1(dbregs->dr1); 2140 load_dr2(dbregs->dr2); 2141 load_dr3(dbregs->dr3); 2142 load_dr4(dbregs->dr4); 2143 load_dr5(dbregs->dr5); 2144 load_dr6(dbregs->dr6); 2145 load_dr7(dbregs->dr7); 2146 } 2147 else { 2148 /* 2149 * Don't let an illegal value for dr7 get set. Specifically, 2150 * check for undefined settings. Setting these bit patterns 2151 * result in undefined behaviour and can lead to an unexpected 2152 * TRCTRAP. 2153 */ 2154 for (i = 0, mask1 = 0x3<<16, mask2 = 0x2<<16; i < 8; 2155 i++, mask1 <<= 2, mask2 <<= 2) 2156 if ((dbregs->dr7 & mask1) == mask2) 2157 return (EINVAL); 2158 2159 pcb = &p->p_addr->u_pcb; 2160 2161 /* 2162 * Don't let a process set a breakpoint that is not within the 2163 * process's address space. If a process could do this, it 2164 * could halt the system by setting a breakpoint in the kernel 2165 * (if ddb was enabled). Thus, we need to check to make sure 2166 * that no breakpoints are being enabled for addresses outside 2167 * process's address space, unless, perhaps, we were called by 2168 * uid 0. 2169 * 2170 * XXX - what about when the watched area of the user's 2171 * address space is written into from within the kernel 2172 * ... wouldn't that still cause a breakpoint to be generated 2173 * from within kernel mode? 2174 */ 2175 2176 if (suser(p) != 0) { 2177 if (dbregs->dr7 & 0x3) { 2178 /* dr0 is enabled */ 2179 if (dbregs->dr0 >= VM_MAXUSER_ADDRESS) 2180 return (EINVAL); 2181 } 2182 2183 if (dbregs->dr7 & (0x3<<2)) { 2184 /* dr1 is enabled */ 2185 if (dbregs->dr1 >= VM_MAXUSER_ADDRESS) 2186 return (EINVAL); 2187 } 2188 2189 if (dbregs->dr7 & (0x3<<4)) { 2190 /* dr2 is enabled */ 2191 if (dbregs->dr2 >= VM_MAXUSER_ADDRESS) 2192 return (EINVAL); 2193 } 2194 2195 if (dbregs->dr7 & (0x3<<6)) { 2196 /* dr3 is enabled */ 2197 if (dbregs->dr3 >= VM_MAXUSER_ADDRESS) 2198 return (EINVAL); 2199 } 2200 } 2201 2202 pcb->pcb_dr0 = dbregs->dr0; 2203 pcb->pcb_dr1 = dbregs->dr1; 2204 pcb->pcb_dr2 = dbregs->dr2; 2205 pcb->pcb_dr3 = dbregs->dr3; 2206 pcb->pcb_dr6 = dbregs->dr6; 2207 pcb->pcb_dr7 = dbregs->dr7; 2208 2209 pcb->pcb_flags |= PCB_DBREGS; 2210 } 2211 2212 return (0); 2213} 2214 2215/* 2216 * Return > 0 if a hardware breakpoint has been hit, and the 2217 * breakpoint was in user space. Return 0, otherwise. 2218 */ 2219int 2220user_dbreg_trap(void) 2221{ 2222 u_int32_t dr7, dr6; /* debug registers dr6 and dr7 */ 2223 u_int32_t bp; /* breakpoint bits extracted from dr6 */ 2224 int nbp; /* number of breakpoints that triggered */ 2225 caddr_t addr[4]; /* breakpoint addresses */ 2226 int i; 2227 2228 dr7 = rdr7(); 2229 if ((dr7 & 0x000000ff) == 0) { 2230 /* 2231 * all GE and LE bits in the dr7 register are zero, 2232 * thus the trap couldn't have been caused by the 2233 * hardware debug registers 2234 */ 2235 return 0; 2236 } 2237 2238 nbp = 0; 2239 dr6 = rdr6(); 2240 bp = dr6 & 0x0000000f; 2241 2242 if (!bp) { 2243 /* 2244 * None of the breakpoint bits are set meaning this 2245 * trap was not caused by any of the debug registers 2246 */ 2247 return 0; 2248 } 2249 2250 /* 2251 * at least one of the breakpoints were hit, check to see 2252 * which ones and if any of them are user space addresses 2253 */ 2254 2255 if (bp & 0x01) { 2256 addr[nbp++] = (caddr_t)rdr0(); 2257 } 2258 if (bp & 0x02) { 2259 addr[nbp++] = (caddr_t)rdr1(); 2260 } 2261 if (bp & 0x04) { 2262 addr[nbp++] = (caddr_t)rdr2(); 2263 } 2264 if (bp & 0x08) { 2265 addr[nbp++] = (caddr_t)rdr3(); 2266 } 2267 2268 for (i=0; i<nbp; i++) { 2269 if (addr[i] < 2270 (caddr_t)VM_MAXUSER_ADDRESS) { 2271 /* 2272 * addr[i] is in user space 2273 */ 2274 return nbp; 2275 } 2276 } 2277 2278 /* 2279 * None of the breakpoints are in user space. 2280 */ 2281 return 0; 2282} 2283 2284 2285#ifndef DDB 2286void 2287Debugger(const char *msg) 2288{ 2289 printf("Debugger(\"%s\") called.\n", msg); 2290} 2291#endif /* no DDB */ 2292 2293#include <sys/disklabel.h> 2294 2295/* 2296 * Determine the size of the transfer, and make sure it is 2297 * within the boundaries of the partition. Adjust transfer 2298 * if needed, and signal errors or early completion. 2299 */ 2300int 2301bounds_check_with_label(struct bio *bp, struct disklabel *lp, int wlabel) 2302{ 2303 struct partition *p = lp->d_partitions + dkpart(bp->bio_dev); 2304 int labelsect = lp->d_partitions[0].p_offset; 2305 int maxsz = p->p_size, 2306 sz = (bp->bio_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT; 2307 2308 /* overwriting disk label ? */ 2309 /* XXX should also protect bootstrap in first 8K */ 2310 if (bp->bio_blkno + p->p_offset <= LABELSECTOR + labelsect && 2311#if LABELSECTOR != 0 2312 bp->bio_blkno + p->p_offset + sz > LABELSECTOR + labelsect && 2313#endif 2314 (bp->bio_cmd == BIO_WRITE) && wlabel == 0) { 2315 bp->bio_error = EROFS; 2316 goto bad; 2317 } 2318 2319#if defined(DOSBBSECTOR) && defined(notyet) 2320 /* overwriting master boot record? */ 2321 if (bp->bio_blkno + p->p_offset <= DOSBBSECTOR && 2322 (bp->bio_cmd == BIO_WRITE) && wlabel == 0) { 2323 bp->bio_error = EROFS; 2324 goto bad; 2325 } 2326#endif 2327 2328 /* beyond partition? */ 2329 if (bp->bio_blkno < 0 || bp->bio_blkno + sz > maxsz) { 2330 /* if exactly at end of disk, return an EOF */ 2331 if (bp->bio_blkno == maxsz) { 2332 bp->bio_resid = bp->bio_bcount; 2333 return(0); 2334 } 2335 /* or truncate if part of it fits */ 2336 sz = maxsz - bp->bio_blkno; 2337 if (sz <= 0) { 2338 bp->bio_error = EINVAL; 2339 goto bad; 2340 } 2341 bp->bio_bcount = sz << DEV_BSHIFT; 2342 } 2343 2344 bp->bio_pblkno = bp->bio_blkno + p->p_offset; 2345 return(1); 2346 2347bad: 2348 bp->bio_flags |= BIO_ERROR; 2349 return(-1); 2350} 2351 2352#ifdef DDB 2353 2354/* 2355 * Provide inb() and outb() as functions. They are normally only 2356 * available as macros calling inlined functions, thus cannot be 2357 * called inside DDB. 2358 * 2359 * The actual code is stolen from <machine/cpufunc.h>, and de-inlined. 2360 */ 2361 2362#undef inb 2363#undef outb 2364 2365/* silence compiler warnings */ 2366u_char inb(u_int); 2367void outb(u_int, u_char); 2368 2369u_char 2370inb(u_int port) 2371{ 2372 u_char data; 2373 /* 2374 * We use %%dx and not %1 here because i/o is done at %dx and not at 2375 * %edx, while gcc generates inferior code (movw instead of movl) 2376 * if we tell it to load (u_short) port. 2377 */ 2378 __asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port)); 2379 return (data); 2380} 2381 2382void 2383outb(u_int port, u_char data) 2384{ 2385 u_char al; 2386 /* 2387 * Use an unnecessary assignment to help gcc's register allocator. 2388 * This make a large difference for gcc-1.40 and a tiny difference 2389 * for gcc-2.6.0. For gcc-1.40, al had to be ``asm("ax")'' for 2390 * best results. gcc-2.6.0 can't handle this. 2391 */ 2392 al = data; 2393 __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port)); 2394} 2395 2396#endif /* DDB */ 2397