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