machdep.c revision 42705
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 * $Id: machdep.c,v 1.321 1999/01/09 15:41:49 bde Exp $ 39 */ 40 41#include "apm.h" 42#include "ether.h" 43#include "npx.h" 44#include "opt_atalk.h" 45#include "opt_cpu.h" 46#include "opt_ddb.h" 47#include "opt_inet.h" 48#include "opt_ipx.h" 49#include "opt_maxmem.h" 50#include "opt_msgbuf.h" 51#include "opt_perfmon.h" 52#include "opt_smp.h" 53#include "opt_sysvipc.h" 54#include "opt_user_ldt.h" 55#include "opt_userconfig.h" 56#include "opt_vm86.h" 57 58#include <sys/param.h> 59#include <sys/systm.h> 60#include <sys/sysproto.h> 61#include <sys/signalvar.h> 62#include <sys/kernel.h> 63#include <sys/linker.h> 64#include <sys/proc.h> 65#include <sys/buf.h> 66#include <sys/reboot.h> 67#include <sys/callout.h> 68#include <sys/malloc.h> 69#include <sys/mbuf.h> 70#include <sys/msgbuf.h> 71#include <sys/sysent.h> 72#include <sys/sysctl.h> 73#include <sys/vmmeter.h> 74 75#ifdef SYSVSHM 76#include <sys/shm.h> 77#endif 78 79#ifdef SYSVMSG 80#include <sys/msg.h> 81#endif 82 83#ifdef SYSVSEM 84#include <sys/sem.h> 85#endif 86 87#include <vm/vm.h> 88#include <vm/vm_param.h> 89#include <vm/vm_prot.h> 90#include <sys/lock.h> 91#include <vm/vm_kern.h> 92#include <vm/vm_object.h> 93#include <vm/vm_page.h> 94#include <vm/vm_map.h> 95#include <vm/vm_pager.h> 96#include <vm/vm_extern.h> 97 98#include <sys/user.h> 99#include <sys/exec.h> 100 101#include <ddb/ddb.h> 102 103#if defined(INET) || defined(IPX) || defined(NATM) || defined(NETATALK) \ 104 || NETHER > 0 || defined(NS) 105#define NETISR 106#endif 107 108#ifdef NETISR 109#include <net/netisr.h> 110#endif 111 112#include <machine/cpu.h> 113#include <machine/reg.h> 114#include <machine/clock.h> 115#include <machine/specialreg.h> 116#include <machine/cons.h> 117#include <machine/bootinfo.h> 118#include <machine/ipl.h> 119#include <machine/md_var.h> 120#include <machine/pcb_ext.h> /* pcb.h included via sys/user.h */ 121#ifdef SMP 122#include <machine/smp.h> 123#endif 124#ifdef PERFMON 125#include <machine/perfmon.h> 126#endif 127 128#include <i386/isa/isa_device.h> 129#include <i386/isa/intr_machdep.h> 130#ifndef VM86 131#include <i386/isa/rtc.h> 132#endif 133#include <machine/random.h> 134#include <sys/ptrace.h> 135 136extern void init386 __P((int first)); 137extern void dblfault_handler __P((void)); 138 139extern void printcpuinfo(void); /* XXX header file */ 140extern void earlysetcpuclass(void); /* same header file */ 141extern void finishidentcpu(void); 142extern void panicifcpuunsupported(void); 143extern void initializecpu(void); 144 145static void cpu_startup __P((void *)); 146SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL) 147 148static MALLOC_DEFINE(M_MBUF, "mbuf", "mbuf"); 149 150int _udatasel, _ucodesel; 151u_int atdevbase; 152 153#if defined(SWTCH_OPTIM_STATS) 154extern int swtch_optim_stats; 155SYSCTL_INT(_debug, OID_AUTO, swtch_optim_stats, 156 CTLFLAG_RD, &swtch_optim_stats, 0, ""); 157SYSCTL_INT(_debug, OID_AUTO, tlb_flush_count, 158 CTLFLAG_RD, &tlb_flush_count, 0, ""); 159#endif 160 161#ifdef PC98 162static int ispc98 = 1; 163#else 164static int ispc98 = 0; 165#endif 166SYSCTL_INT(_machdep, OID_AUTO, ispc98, CTLFLAG_RD, &ispc98, 0, ""); 167 168int physmem = 0; 169int cold = 1; 170 171static int 172sysctl_hw_physmem SYSCTL_HANDLER_ARGS 173{ 174 int error = sysctl_handle_int(oidp, 0, ctob(physmem), req); 175 return (error); 176} 177 178SYSCTL_PROC(_hw, HW_PHYSMEM, physmem, CTLTYPE_INT|CTLFLAG_RD, 179 0, 0, sysctl_hw_physmem, "I", ""); 180 181static int 182sysctl_hw_usermem SYSCTL_HANDLER_ARGS 183{ 184 int error = sysctl_handle_int(oidp, 0, 185 ctob(physmem - cnt.v_wire_count), req); 186 return (error); 187} 188 189SYSCTL_PROC(_hw, HW_USERMEM, usermem, CTLTYPE_INT|CTLFLAG_RD, 190 0, 0, sysctl_hw_usermem, "I", ""); 191 192static int 193sysctl_hw_availpages SYSCTL_HANDLER_ARGS 194{ 195 int error = sysctl_handle_int(oidp, 0, 196 i386_btop(avail_end - avail_start), req); 197 return (error); 198} 199 200SYSCTL_PROC(_hw, OID_AUTO, availpages, CTLTYPE_INT|CTLFLAG_RD, 201 0, 0, sysctl_hw_availpages, "I", ""); 202 203static int 204sysctl_machdep_msgbuf SYSCTL_HANDLER_ARGS 205{ 206 int error; 207 208 /* Unwind the buffer, so that it's linear (possibly starting with 209 * some initial nulls). 210 */ 211 error=sysctl_handle_opaque(oidp,msgbufp->msg_ptr+msgbufp->msg_bufr, 212 msgbufp->msg_size-msgbufp->msg_bufr,req); 213 if(error) return(error); 214 if(msgbufp->msg_bufr>0) { 215 error=sysctl_handle_opaque(oidp,msgbufp->msg_ptr, 216 msgbufp->msg_bufr,req); 217 } 218 return(error); 219} 220 221SYSCTL_PROC(_machdep, OID_AUTO, msgbuf, CTLTYPE_STRING|CTLFLAG_RD, 222 0, 0, sysctl_machdep_msgbuf, "A","Contents of kernel message buffer"); 223 224static int msgbuf_clear; 225 226static int 227sysctl_machdep_msgbuf_clear SYSCTL_HANDLER_ARGS 228{ 229 int error; 230 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, 231 req); 232 if (!error && req->newptr) { 233 /* Clear the buffer and reset write pointer */ 234 bzero(msgbufp->msg_ptr,msgbufp->msg_size); 235 msgbufp->msg_bufr=msgbufp->msg_bufx=0; 236 msgbuf_clear=0; 237 } 238 return (error); 239} 240 241SYSCTL_PROC(_machdep, OID_AUTO, msgbuf_clear, CTLTYPE_INT|CTLFLAG_RW, 242 &msgbuf_clear, 0, sysctl_machdep_msgbuf_clear, "I", 243 "Clear kernel message buffer"); 244 245int bootverbose = 0, Maxmem = 0; 246long dumplo; 247 248vm_offset_t phys_avail[10]; 249 250/* must be 2 less so 0 0 can signal end of chunks */ 251#define PHYS_AVAIL_ARRAY_END ((sizeof(phys_avail) / sizeof(vm_offset_t)) - 2) 252 253#ifdef NETISR 254static void setup_netisrs __P((struct linker_set *)); 255#endif 256 257static vm_offset_t buffer_sva, buffer_eva; 258vm_offset_t clean_sva, clean_eva; 259static vm_offset_t pager_sva, pager_eva; 260#ifdef NETISR 261extern struct linker_set netisr_set; 262#endif 263#if NNPX > 0 264extern struct isa_driver npxdriver; 265#endif 266 267#define offsetof(type, member) ((size_t)(&((type *)0)->member)) 268 269static void 270cpu_startup(dummy) 271 void *dummy; 272{ 273 register unsigned i; 274 register caddr_t v; 275 vm_offset_t maxaddr; 276 vm_size_t size = 0; 277 int firstaddr; 278 vm_offset_t minaddr; 279 280 if (boothowto & RB_VERBOSE) 281 bootverbose++; 282 283 /* 284 * Good {morning,afternoon,evening,night}. 285 */ 286 printf(version); 287 earlysetcpuclass(); 288 startrtclock(); 289 printcpuinfo(); 290 panicifcpuunsupported(); 291#ifdef PERFMON 292 perfmon_init(); 293#endif 294 printf("real memory = %d (%dK bytes)\n", ptoa(Maxmem), ptoa(Maxmem) / 1024); 295 /* 296 * Display any holes after the first chunk of extended memory. 297 */ 298 if (bootverbose) { 299 int indx; 300 301 printf("Physical memory chunk(s):\n"); 302 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) { 303 int size1 = phys_avail[indx + 1] - phys_avail[indx]; 304 305 printf("0x%08x - 0x%08x, %d bytes (%d pages)\n", 306 phys_avail[indx], phys_avail[indx + 1] - 1, size1, 307 size1 / PAGE_SIZE); 308 } 309 } 310 311#ifdef NETISR 312 /* 313 * Quickly wire in netisrs. 314 */ 315 setup_netisrs(&netisr_set); 316#endif 317 318 /* 319 * Calculate callout wheel size 320 */ 321 for (callwheelsize = 1, callwheelbits = 0; 322 callwheelsize < ncallout; 323 callwheelsize <<= 1, ++callwheelbits) 324 ; 325 callwheelmask = callwheelsize - 1; 326 327 /* 328 * Allocate space for system data structures. 329 * The first available kernel virtual address is in "v". 330 * As pages of kernel virtual memory are allocated, "v" is incremented. 331 * As pages of memory are allocated and cleared, 332 * "firstaddr" is incremented. 333 * An index into the kernel page table corresponding to the 334 * virtual memory address maintained in "v" is kept in "mapaddr". 335 */ 336 337 /* 338 * Make two passes. The first pass calculates how much memory is 339 * needed and allocates it. The second pass assigns virtual 340 * addresses to the various data structures. 341 */ 342 firstaddr = 0; 343again: 344 v = (caddr_t)firstaddr; 345 346#define valloc(name, type, num) \ 347 (name) = (type *)v; v = (caddr_t)((name)+(num)) 348#define valloclim(name, type, num, lim) \ 349 (name) = (type *)v; v = (caddr_t)((lim) = ((name)+(num))) 350 351 valloc(callout, struct callout, ncallout); 352 valloc(callwheel, struct callout_tailq, callwheelsize); 353#ifdef SYSVSHM 354 valloc(shmsegs, struct shmid_ds, shminfo.shmmni); 355#endif 356#ifdef SYSVSEM 357 valloc(sema, struct semid_ds, seminfo.semmni); 358 valloc(sem, struct sem, seminfo.semmns); 359 /* This is pretty disgusting! */ 360 valloc(semu, int, (seminfo.semmnu * seminfo.semusz) / sizeof(int)); 361#endif 362#ifdef SYSVMSG 363 valloc(msgpool, char, msginfo.msgmax); 364 valloc(msgmaps, struct msgmap, msginfo.msgseg); 365 valloc(msghdrs, struct msg, msginfo.msgtql); 366 valloc(msqids, struct msqid_ds, msginfo.msgmni); 367#endif 368 369 if (nbuf == 0) { 370 nbuf = 30; 371 if( physmem > 1024) 372 nbuf += min((physmem - 1024) / 8, 2048); 373 } 374 nswbuf = max(min(nbuf/4, 64), 16); 375 376 valloc(swbuf, struct buf, nswbuf); 377 valloc(buf, struct buf, nbuf); 378 379 380 /* 381 * End of first pass, size has been calculated so allocate memory 382 */ 383 if (firstaddr == 0) { 384 size = (vm_size_t)(v - firstaddr); 385 firstaddr = (int)kmem_alloc(kernel_map, round_page(size)); 386 if (firstaddr == 0) 387 panic("startup: no room for tables"); 388 goto again; 389 } 390 391 /* 392 * End of second pass, addresses have been assigned 393 */ 394 if ((vm_size_t)(v - firstaddr) != size) 395 panic("startup: table size inconsistency"); 396 397 clean_map = kmem_suballoc(kernel_map, &clean_sva, &clean_eva, 398 (nbuf*BKVASIZE) + (nswbuf*MAXPHYS) + pager_map_size); 399 buffer_map = kmem_suballoc(clean_map, &buffer_sva, &buffer_eva, 400 (nbuf*BKVASIZE)); 401 pager_map = kmem_suballoc(clean_map, &pager_sva, &pager_eva, 402 (nswbuf*MAXPHYS) + pager_map_size); 403 pager_map->system_map = 1; 404 exec_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr, 405 (16*(ARG_MAX+(PAGE_SIZE*3)))); 406 407 /* 408 * Finally, allocate mbuf pool. Since mclrefcnt is an off-size 409 * we use the more space efficient malloc in place of kmem_alloc. 410 */ 411 { 412 vm_offset_t mb_map_size; 413 int xclusters; 414 415 /* Allow override of NMBCLUSTERS from the kernel environment */ 416 if (getenv_int("kern.ipc.nmbclusters", &xclusters) && 417 xclusters > nmbclusters) 418 nmbclusters = xclusters; 419 420 mb_map_size = nmbufs * MSIZE + nmbclusters * MCLBYTES; 421 mb_map_size = roundup2(mb_map_size, max(MCLBYTES, PAGE_SIZE)); 422 mclrefcnt = malloc(mb_map_size / MCLBYTES, M_MBUF, M_NOWAIT); 423 bzero(mclrefcnt, mb_map_size / MCLBYTES); 424 mb_map = kmem_suballoc(kmem_map, (vm_offset_t *)&mbutl, &maxaddr, 425 mb_map_size); 426 mb_map->system_map = 1; 427 } 428 429 /* 430 * Initialize callouts 431 */ 432 SLIST_INIT(&callfree); 433 for (i = 0; i < ncallout; i++) { 434 SLIST_INSERT_HEAD(&callfree, &callout[i], c_links.sle); 435 } 436 437 for (i = 0; i < callwheelsize; i++) { 438 TAILQ_INIT(&callwheel[i]); 439 } 440 441#if defined(USERCONFIG) 442 userconfig(); 443 cninit(); /* the preferred console may have changed */ 444#endif 445 446 printf("avail memory = %d (%dK bytes)\n", ptoa(cnt.v_free_count), 447 ptoa(cnt.v_free_count) / 1024); 448 449 /* 450 * Set up buffers, so they can be used to read disk labels. 451 */ 452 bufinit(); 453 vm_pager_bufferinit(); 454 455#ifdef SMP 456 /* 457 * OK, enough kmem_alloc/malloc state should be up, lets get on with it! 458 */ 459 mp_start(); /* fire up the APs and APICs */ 460 mp_announce(); 461#endif /* SMP */ 462} 463 464#ifdef NETISR 465int 466register_netisr(num, handler) 467 int num; 468 netisr_t *handler; 469{ 470 471 if (num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs)) ) { 472 printf("register_netisr: bad isr number: %d\n", num); 473 return (EINVAL); 474 } 475 netisrs[num] = handler; 476 return (0); 477} 478 479static void 480setup_netisrs(ls) 481 struct linker_set *ls; 482{ 483 int i; 484 const struct netisrtab *nit; 485 486 for(i = 0; ls->ls_items[i]; i++) { 487 nit = (const struct netisrtab *)ls->ls_items[i]; 488 register_netisr(nit->nit_num, nit->nit_isr); 489 } 490} 491#endif /* NETISR */ 492 493/* 494 * Send an interrupt to process. 495 * 496 * Stack is set up to allow sigcode stored 497 * at top to call routine, followed by kcall 498 * to sigreturn routine below. After sigreturn 499 * resets the signal mask, the stack, and the 500 * frame pointer, it returns to the user 501 * specified pc, psl. 502 */ 503void 504sendsig(catcher, sig, mask, code) 505 sig_t catcher; 506 int sig, mask; 507 u_long code; 508{ 509 register struct proc *p = curproc; 510 register struct trapframe *regs; 511 register struct sigframe *fp; 512 struct sigframe sf; 513 struct sigacts *psp = p->p_sigacts; 514 int oonstack; 515 516 regs = p->p_md.md_regs; 517 oonstack = psp->ps_sigstk.ss_flags & SS_ONSTACK; 518 /* 519 * Allocate and validate space for the signal handler context. 520 */ 521 if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack && 522 (psp->ps_sigonstack & sigmask(sig))) { 523 fp = (struct sigframe *)(psp->ps_sigstk.ss_sp + 524 psp->ps_sigstk.ss_size - sizeof(struct sigframe)); 525 psp->ps_sigstk.ss_flags |= SS_ONSTACK; 526 } else { 527 fp = (struct sigframe *)regs->tf_esp - 1; 528 } 529 530 /* 531 * grow() will return FALSE if the fp will not fit inside the stack 532 * and the stack can not be grown. useracc will return FALSE 533 * if access is denied. 534 */ 535#ifdef VM_STACK 536 if ((grow_stack (p, (int)fp) == FALSE) || 537#else 538 if ((grow(p, (int)fp) == FALSE) || 539#endif 540 (useracc((caddr_t)fp, sizeof(struct sigframe), B_WRITE) == FALSE)) { 541 /* 542 * Process has trashed its stack; give it an illegal 543 * instruction to halt it in its tracks. 544 */ 545 SIGACTION(p, SIGILL) = SIG_DFL; 546 sig = sigmask(SIGILL); 547 p->p_sigignore &= ~sig; 548 p->p_sigcatch &= ~sig; 549 p->p_sigmask &= ~sig; 550 psignal(p, SIGILL); 551 return; 552 } 553 554 /* 555 * Build the argument list for the signal handler. 556 */ 557 if (p->p_sysent->sv_sigtbl) { 558 if (sig < p->p_sysent->sv_sigsize) 559 sig = p->p_sysent->sv_sigtbl[sig]; 560 else 561 sig = p->p_sysent->sv_sigsize + 1; 562 } 563 sf.sf_signum = sig; 564 sf.sf_code = code; 565 sf.sf_scp = &fp->sf_sc; 566 sf.sf_addr = (char *) regs->tf_err; 567 sf.sf_handler = catcher; 568 569 /* save scratch registers */ 570 sf.sf_sc.sc_eax = regs->tf_eax; 571 sf.sf_sc.sc_ebx = regs->tf_ebx; 572 sf.sf_sc.sc_ecx = regs->tf_ecx; 573 sf.sf_sc.sc_edx = regs->tf_edx; 574 sf.sf_sc.sc_esi = regs->tf_esi; 575 sf.sf_sc.sc_edi = regs->tf_edi; 576 sf.sf_sc.sc_cs = regs->tf_cs; 577 sf.sf_sc.sc_ds = regs->tf_ds; 578 sf.sf_sc.sc_ss = regs->tf_ss; 579 sf.sf_sc.sc_es = regs->tf_es; 580 sf.sf_sc.sc_isp = regs->tf_isp; 581 582 /* 583 * Build the signal context to be used by sigreturn. 584 */ 585 sf.sf_sc.sc_onstack = oonstack; 586 sf.sf_sc.sc_mask = mask; 587 sf.sf_sc.sc_sp = regs->tf_esp; 588 sf.sf_sc.sc_fp = regs->tf_ebp; 589 sf.sf_sc.sc_pc = regs->tf_eip; 590 sf.sf_sc.sc_ps = regs->tf_eflags; 591 sf.sf_sc.sc_trapno = regs->tf_trapno; 592 sf.sf_sc.sc_err = regs->tf_err; 593 594#ifdef VM86 595 /* 596 * If we're a vm86 process, we want to save the segment registers. 597 * We also change eflags to be our emulated eflags, not the actual 598 * eflags. 599 */ 600 if (regs->tf_eflags & PSL_VM) { 601 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs; 602 struct vm86_kernel *vm86 = &p->p_addr->u_pcb.pcb_ext->ext_vm86; 603 604 sf.sf_sc.sc_gs = tf->tf_vm86_gs; 605 sf.sf_sc.sc_fs = tf->tf_vm86_fs; 606 sf.sf_sc.sc_es = tf->tf_vm86_es; 607 sf.sf_sc.sc_ds = tf->tf_vm86_ds; 608 609 if (vm86->vm86_has_vme == 0) 610 sf.sf_sc.sc_ps = (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) 611 | (vm86->vm86_eflags & (PSL_VIF | PSL_VIP)); 612 613 /* 614 * We should never have PSL_T set when returning from vm86 615 * mode. It may be set here if we deliver a signal before 616 * getting to vm86 mode, so turn it off. 617 */ 618 tf->tf_eflags &= ~(PSL_VM | PSL_T | PSL_VIF | PSL_VIP); 619 } 620#endif /* VM86 */ 621 622 /* 623 * Copy the sigframe out to the user's stack. 624 */ 625 if (copyout(&sf, fp, sizeof(struct sigframe)) != 0) { 626 /* 627 * Something is wrong with the stack pointer. 628 * ...Kill the process. 629 */ 630 sigexit(p, SIGILL); 631 } 632 633 regs->tf_esp = (int)fp; 634 regs->tf_eip = PS_STRINGS - *(p->p_sysent->sv_szsigcode); 635 regs->tf_cs = _ucodesel; 636 regs->tf_ds = _udatasel; 637 regs->tf_es = _udatasel; 638 regs->tf_ss = _udatasel; 639} 640 641/* 642 * System call to cleanup state after a signal 643 * has been taken. Reset signal mask and 644 * stack state from context left by sendsig (above). 645 * Return to previous pc and psl as specified by 646 * context left by sendsig. Check carefully to 647 * make sure that the user has not modified the 648 * state to gain improper privileges. 649 */ 650int 651sigreturn(p, uap) 652 struct proc *p; 653 struct sigreturn_args /* { 654 struct sigcontext *sigcntxp; 655 } */ *uap; 656{ 657 register struct sigcontext *scp; 658 register struct sigframe *fp; 659 register struct trapframe *regs = p->p_md.md_regs; 660 int eflags; 661 662 /* 663 * (XXX old comment) regs->tf_esp points to the return address. 664 * The user scp pointer is above that. 665 * The return address is faked in the signal trampoline code 666 * for consistency. 667 */ 668 scp = uap->sigcntxp; 669 fp = (struct sigframe *) 670 ((caddr_t)scp - offsetof(struct sigframe, sf_sc)); 671 672 if (useracc((caddr_t)fp, sizeof (*fp), B_WRITE) == 0) 673 return(EFAULT); 674 675 eflags = scp->sc_ps; 676#ifdef VM86 677 if (eflags & PSL_VM) { 678 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs; 679 struct vm86_kernel *vm86; 680 681 /* 682 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't 683 * set up the vm86 area, and we can't enter vm86 mode. 684 */ 685 if (p->p_addr->u_pcb.pcb_ext == 0) 686 return (EINVAL); 687 vm86 = &p->p_addr->u_pcb.pcb_ext->ext_vm86; 688 if (vm86->vm86_inited == 0) 689 return (EINVAL); 690 691 /* go back to user mode if both flags are set */ 692 if ((eflags & PSL_VIP) && (eflags & PSL_VIF)) 693 trapsignal(p, SIGBUS, 0); 694 695 if (vm86->vm86_has_vme) { 696 eflags = (tf->tf_eflags & ~VME_USERCHANGE) | 697 (eflags & VME_USERCHANGE) | PSL_VM; 698 } else { 699 vm86->vm86_eflags = eflags; /* save VIF, VIP */ 700 eflags = (tf->tf_eflags & ~VM_USERCHANGE) | (eflags & VM_USERCHANGE) | PSL_VM; 701 } 702 tf->tf_vm86_ds = scp->sc_ds; 703 tf->tf_vm86_es = scp->sc_es; 704 tf->tf_vm86_fs = scp->sc_fs; 705 tf->tf_vm86_gs = scp->sc_gs; 706 tf->tf_ds = _udatasel; 707 tf->tf_es = _udatasel; 708 } else { 709#endif /* VM86 */ 710 /* 711 * Don't allow users to change privileged or reserved flags. 712 */ 713#define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0) 714 /* 715 * XXX do allow users to change the privileged flag PSL_RF. 716 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers 717 * should sometimes set it there too. tf_eflags is kept in 718 * the signal context during signal handling and there is no 719 * other place to remember it, so the PSL_RF bit may be 720 * corrupted by the signal handler without us knowing. 721 * Corruption of the PSL_RF bit at worst causes one more or 722 * one less debugger trap, so allowing it is fairly harmless. 723 */ 724 if (!EFLAGS_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) { 725#ifdef DEBUG 726 printf("sigreturn: eflags = 0x%x\n", eflags); 727#endif 728 return(EINVAL); 729 } 730 731 /* 732 * Don't allow users to load a valid privileged %cs. Let the 733 * hardware check for invalid selectors, excess privilege in 734 * other selectors, invalid %eip's and invalid %esp's. 735 */ 736#define CS_SECURE(cs) (ISPL(cs) == SEL_UPL) 737 if (!CS_SECURE(scp->sc_cs)) { 738#ifdef DEBUG 739 printf("sigreturn: cs = 0x%x\n", scp->sc_cs); 740#endif 741 trapsignal(p, SIGBUS, T_PROTFLT); 742 return(EINVAL); 743 } 744 regs->tf_ds = scp->sc_ds; 745 regs->tf_es = scp->sc_es; 746#ifdef VM86 747 } 748#endif 749 750 /* restore scratch registers */ 751 regs->tf_eax = scp->sc_eax; 752 regs->tf_ebx = scp->sc_ebx; 753 regs->tf_ecx = scp->sc_ecx; 754 regs->tf_edx = scp->sc_edx; 755 regs->tf_esi = scp->sc_esi; 756 regs->tf_edi = scp->sc_edi; 757 regs->tf_cs = scp->sc_cs; 758 regs->tf_ss = scp->sc_ss; 759 regs->tf_isp = scp->sc_isp; 760 761 if (useracc((caddr_t)scp, sizeof (*scp), B_WRITE) == 0) 762 return(EINVAL); 763 764 if (scp->sc_onstack & 01) 765 p->p_sigacts->ps_sigstk.ss_flags |= SS_ONSTACK; 766 else 767 p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK; 768 p->p_sigmask = scp->sc_mask & ~sigcantmask; 769 regs->tf_ebp = scp->sc_fp; 770 regs->tf_esp = scp->sc_sp; 771 regs->tf_eip = scp->sc_pc; 772 regs->tf_eflags = eflags; 773 return(EJUSTRETURN); 774} 775 776/* 777 * Machine dependent boot() routine 778 * 779 * I haven't seen anything to put here yet 780 * Possibly some stuff might be grafted back here from boot() 781 */ 782void 783cpu_boot(int howto) 784{ 785} 786 787/* 788 * Shutdown the CPU as much as possible 789 */ 790void 791cpu_halt(void) 792{ 793 for (;;) 794 __asm__ ("hlt"); 795} 796 797/* 798 * Clear registers on exec 799 */ 800void 801setregs(p, entry, stack) 802 struct proc *p; 803 u_long entry; 804 u_long stack; 805{ 806 struct trapframe *regs = p->p_md.md_regs; 807 struct pcb *pcb = &p->p_addr->u_pcb; 808 809#ifdef USER_LDT 810 /* was i386_user_cleanup() in NetBSD */ 811 if (pcb->pcb_ldt) { 812 if (pcb == curpcb) { 813 lldt(_default_ldt); 814 currentldt = _default_ldt; 815 } 816 kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ldt, 817 pcb->pcb_ldt_len * sizeof(union descriptor)); 818 pcb->pcb_ldt_len = (int)pcb->pcb_ldt = 0; 819 } 820#endif 821 822 bzero((char *)regs, sizeof(struct trapframe)); 823 regs->tf_eip = entry; 824 regs->tf_esp = stack; 825 regs->tf_eflags = PSL_USER | (regs->tf_eflags & PSL_T); 826 regs->tf_ss = _udatasel; 827 regs->tf_ds = _udatasel; 828 regs->tf_es = _udatasel; 829 regs->tf_cs = _ucodesel; 830 831 /* reset %fs and %gs as well */ 832 pcb->pcb_fs = _udatasel; 833 pcb->pcb_gs = _udatasel; 834 if (pcb == curpcb) { 835 __asm("movw %w0,%%fs" : : "r" (_udatasel)); 836 __asm("movw %w0,%%gs" : : "r" (_udatasel)); 837 } 838 839 /* 840 * Initialize the math emulator (if any) for the current process. 841 * Actually, just clear the bit that says that the emulator has 842 * been initialized. Initialization is delayed until the process 843 * traps to the emulator (if it is done at all) mainly because 844 * emulators don't provide an entry point for initialization. 845 */ 846 p->p_addr->u_pcb.pcb_flags &= ~FP_SOFTFP; 847 848 /* 849 * Arrange to trap the next npx or `fwait' instruction (see npx.c 850 * for why fwait must be trapped at least if there is an npx or an 851 * emulator). This is mainly to handle the case where npx0 is not 852 * configured, since the npx routines normally set up the trap 853 * otherwise. It should be done only at boot time, but doing it 854 * here allows modifying `npx_exists' for testing the emulator on 855 * systems with an npx. 856 */ 857 load_cr0(rcr0() | CR0_MP | CR0_TS); 858 859#if NNPX > 0 860 /* Initialize the npx (if any) for the current process. */ 861 npxinit(__INITIAL_NPXCW__); 862#endif 863} 864 865static int 866sysctl_machdep_adjkerntz SYSCTL_HANDLER_ARGS 867{ 868 int error; 869 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, 870 req); 871 if (!error && req->newptr) 872 resettodr(); 873 return (error); 874} 875 876SYSCTL_PROC(_machdep, CPU_ADJKERNTZ, adjkerntz, CTLTYPE_INT|CTLFLAG_RW, 877 &adjkerntz, 0, sysctl_machdep_adjkerntz, "I", ""); 878 879SYSCTL_INT(_machdep, CPU_DISRTCSET, disable_rtc_set, 880 CTLFLAG_RW, &disable_rtc_set, 0, ""); 881 882SYSCTL_STRUCT(_machdep, CPU_BOOTINFO, bootinfo, 883 CTLFLAG_RD, &bootinfo, bootinfo, ""); 884 885SYSCTL_INT(_machdep, CPU_WALLCLOCK, wall_cmos_clock, 886 CTLFLAG_RW, &wall_cmos_clock, 0, ""); 887 888/* 889 * Initialize 386 and configure to run kernel 890 */ 891 892/* 893 * Initialize segments & interrupt table 894 */ 895 896int _default_ldt; 897#ifdef SMP 898union descriptor gdt[NGDT + NCPU]; /* global descriptor table */ 899#else 900union descriptor gdt[NGDT]; /* global descriptor table */ 901#endif 902struct gate_descriptor idt[NIDT]; /* interrupt descriptor table */ 903union descriptor ldt[NLDT]; /* local descriptor table */ 904#ifdef SMP 905/* table descriptors - used to load tables by microp */ 906struct region_descriptor r_gdt, r_idt; 907#endif 908 909extern struct i386tss common_tss; /* One tss per cpu */ 910#ifdef VM86 911extern struct segment_descriptor common_tssd; 912extern int private_tss; /* flag indicating private tss */ 913extern u_int my_tr; /* which task register setting */ 914#endif /* VM86 */ 915 916#if defined(I586_CPU) && !defined(NO_F00F_HACK) 917struct gate_descriptor *t_idt; 918extern int has_f00f_bug; 919#endif 920 921static struct i386tss dblfault_tss; 922static char dblfault_stack[PAGE_SIZE]; 923 924extern struct user *proc0paddr; 925 926 927/* software prototypes -- in more palatable form */ 928struct soft_segment_descriptor gdt_segs[ 929#ifdef SMP 930 NGDT + NCPU 931#endif 932 ] = { 933/* GNULL_SEL 0 Null Descriptor */ 934{ 0x0, /* segment base address */ 935 0x0, /* length */ 936 0, /* segment type */ 937 0, /* segment descriptor priority level */ 938 0, /* segment descriptor present */ 939 0, 0, 940 0, /* default 32 vs 16 bit size */ 941 0 /* limit granularity (byte/page units)*/ }, 942/* GCODE_SEL 1 Code Descriptor for kernel */ 943{ 0x0, /* segment base address */ 944 0xfffff, /* length - all address space */ 945 SDT_MEMERA, /* segment type */ 946 0, /* segment descriptor priority level */ 947 1, /* segment descriptor present */ 948 0, 0, 949 1, /* default 32 vs 16 bit size */ 950 1 /* limit granularity (byte/page units)*/ }, 951/* GDATA_SEL 2 Data Descriptor for kernel */ 952{ 0x0, /* segment base address */ 953 0xfffff, /* length - all address space */ 954 SDT_MEMRWA, /* segment type */ 955 0, /* segment descriptor priority level */ 956 1, /* segment descriptor present */ 957 0, 0, 958 1, /* default 32 vs 16 bit size */ 959 1 /* limit granularity (byte/page units)*/ }, 960/* GLDT_SEL 3 LDT Descriptor */ 961{ (int) ldt, /* segment base address */ 962 sizeof(ldt)-1, /* length - all address space */ 963 SDT_SYSLDT, /* segment type */ 964 SEL_UPL, /* segment descriptor priority level */ 965 1, /* segment descriptor present */ 966 0, 0, 967 0, /* unused - default 32 vs 16 bit size */ 968 0 /* limit granularity (byte/page units)*/ }, 969/* GTGATE_SEL 4 Null Descriptor - Placeholder */ 970{ 0x0, /* segment base address */ 971 0x0, /* length - all address space */ 972 0, /* segment type */ 973 0, /* segment descriptor priority level */ 974 0, /* segment descriptor present */ 975 0, 0, 976 0, /* default 32 vs 16 bit size */ 977 0 /* limit granularity (byte/page units)*/ }, 978/* GPANIC_SEL 5 Panic Tss Descriptor */ 979{ (int) &dblfault_tss, /* segment base address */ 980 sizeof(struct i386tss)-1,/* length - all address space */ 981 SDT_SYS386TSS, /* segment type */ 982 0, /* segment descriptor priority level */ 983 1, /* segment descriptor present */ 984 0, 0, 985 0, /* unused - default 32 vs 16 bit size */ 986 0 /* limit granularity (byte/page units)*/ }, 987/* GPROC0_SEL 6 Proc 0 Tss Descriptor */ 988{ 989 (int) &common_tss, /* segment base address */ 990 sizeof(struct i386tss)-1,/* length - all address space */ 991 SDT_SYS386TSS, /* segment type */ 992 0, /* segment descriptor priority level */ 993 1, /* segment descriptor present */ 994 0, 0, 995 0, /* unused - default 32 vs 16 bit size */ 996 0 /* limit granularity (byte/page units)*/ }, 997/* GUSERLDT_SEL 7 User LDT Descriptor per process */ 998{ (int) ldt, /* segment base address */ 999 (512 * sizeof(union descriptor)-1), /* length */ 1000 SDT_SYSLDT, /* segment type */ 1001 0, /* segment descriptor priority level */ 1002 1, /* segment descriptor present */ 1003 0, 0, 1004 0, /* unused - default 32 vs 16 bit size */ 1005 0 /* limit granularity (byte/page units)*/ }, 1006/* GAPMCODE32_SEL 8 APM BIOS 32-bit interface (32bit Code) */ 1007{ 0, /* segment base address (overwritten by APM) */ 1008 0xfffff, /* length */ 1009 SDT_MEMERA, /* segment type */ 1010 0, /* segment descriptor priority level */ 1011 1, /* segment descriptor present */ 1012 0, 0, 1013 1, /* default 32 vs 16 bit size */ 1014 1 /* limit granularity (byte/page units)*/ }, 1015/* GAPMCODE16_SEL 9 APM BIOS 32-bit interface (16bit Code) */ 1016{ 0, /* segment base address (overwritten by APM) */ 1017 0xfffff, /* length */ 1018 SDT_MEMERA, /* segment type */ 1019 0, /* segment descriptor priority level */ 1020 1, /* segment descriptor present */ 1021 0, 0, 1022 0, /* default 32 vs 16 bit size */ 1023 1 /* limit granularity (byte/page units)*/ }, 1024/* GAPMDATA_SEL 10 APM BIOS 32-bit interface (Data) */ 1025{ 0, /* segment base address (overwritten by APM) */ 1026 0xfffff, /* length */ 1027 SDT_MEMRWA, /* segment type */ 1028 0, /* segment descriptor priority level */ 1029 1, /* segment descriptor present */ 1030 0, 0, 1031 1, /* default 32 vs 16 bit size */ 1032 1 /* limit granularity (byte/page units)*/ }, 1033}; 1034 1035static struct soft_segment_descriptor ldt_segs[] = { 1036 /* Null Descriptor - overwritten by call gate */ 1037{ 0x0, /* segment base address */ 1038 0x0, /* length - all address space */ 1039 0, /* segment type */ 1040 0, /* segment descriptor priority level */ 1041 0, /* segment descriptor present */ 1042 0, 0, 1043 0, /* default 32 vs 16 bit size */ 1044 0 /* limit granularity (byte/page units)*/ }, 1045 /* Null Descriptor - overwritten by call gate */ 1046{ 0x0, /* segment base address */ 1047 0x0, /* length - all address space */ 1048 0, /* segment type */ 1049 0, /* segment descriptor priority level */ 1050 0, /* segment descriptor present */ 1051 0, 0, 1052 0, /* default 32 vs 16 bit size */ 1053 0 /* limit granularity (byte/page units)*/ }, 1054 /* Null Descriptor - overwritten by call gate */ 1055{ 0x0, /* segment base address */ 1056 0x0, /* length - all address space */ 1057 0, /* segment type */ 1058 0, /* segment descriptor priority level */ 1059 0, /* segment descriptor present */ 1060 0, 0, 1061 0, /* default 32 vs 16 bit size */ 1062 0 /* limit granularity (byte/page units)*/ }, 1063 /* Code Descriptor for user */ 1064{ 0x0, /* segment base address */ 1065 0xfffff, /* length - all address space */ 1066 SDT_MEMERA, /* segment type */ 1067 SEL_UPL, /* segment descriptor priority level */ 1068 1, /* segment descriptor present */ 1069 0, 0, 1070 1, /* default 32 vs 16 bit size */ 1071 1 /* limit granularity (byte/page units)*/ }, 1072 /* Data Descriptor for user */ 1073{ 0x0, /* segment base address */ 1074 0xfffff, /* length - all address space */ 1075 SDT_MEMRWA, /* segment type */ 1076 SEL_UPL, /* segment descriptor priority level */ 1077 1, /* segment descriptor present */ 1078 0, 0, 1079 1, /* default 32 vs 16 bit size */ 1080 1 /* limit granularity (byte/page units)*/ }, 1081}; 1082 1083void 1084setidt(idx, func, typ, dpl, selec) 1085 int idx; 1086 inthand_t *func; 1087 int typ; 1088 int dpl; 1089 int selec; 1090{ 1091 struct gate_descriptor *ip; 1092 1093#if defined(I586_CPU) && !defined(NO_F00F_HACK) 1094 ip = (t_idt != NULL ? t_idt : idt) + idx; 1095#else 1096 ip = idt + idx; 1097#endif 1098 ip->gd_looffset = (int)func; 1099 ip->gd_selector = selec; 1100 ip->gd_stkcpy = 0; 1101 ip->gd_xx = 0; 1102 ip->gd_type = typ; 1103 ip->gd_dpl = dpl; 1104 ip->gd_p = 1; 1105 ip->gd_hioffset = ((int)func)>>16 ; 1106} 1107 1108#define IDTVEC(name) __CONCAT(X,name) 1109 1110extern inthand_t 1111 IDTVEC(div), IDTVEC(dbg), IDTVEC(nmi), IDTVEC(bpt), IDTVEC(ofl), 1112 IDTVEC(bnd), IDTVEC(ill), IDTVEC(dna), IDTVEC(fpusegm), 1113 IDTVEC(tss), IDTVEC(missing), IDTVEC(stk), IDTVEC(prot), 1114 IDTVEC(page), IDTVEC(mchk), IDTVEC(rsvd), IDTVEC(fpu), IDTVEC(align), 1115 IDTVEC(syscall), IDTVEC(int0x80_syscall); 1116 1117void 1118sdtossd(sd, ssd) 1119 struct segment_descriptor *sd; 1120 struct soft_segment_descriptor *ssd; 1121{ 1122 ssd->ssd_base = (sd->sd_hibase << 24) | sd->sd_lobase; 1123 ssd->ssd_limit = (sd->sd_hilimit << 16) | sd->sd_lolimit; 1124 ssd->ssd_type = sd->sd_type; 1125 ssd->ssd_dpl = sd->sd_dpl; 1126 ssd->ssd_p = sd->sd_p; 1127 ssd->ssd_def32 = sd->sd_def32; 1128 ssd->ssd_gran = sd->sd_gran; 1129} 1130 1131void 1132init386(first) 1133 int first; 1134{ 1135 int x; 1136 unsigned biosbasemem, biosextmem; 1137 struct gate_descriptor *gdp; 1138 int gsel_tss; 1139 1140 struct isa_device *idp; 1141#ifndef SMP 1142 /* table descriptors - used to load tables by microp */ 1143 struct region_descriptor r_gdt, r_idt; 1144#endif 1145 int pagesinbase, pagesinext; 1146 int target_page, pa_indx; 1147 int off; 1148 int speculative_mprobe; 1149 1150 /* 1151 * Prevent lowering of the ipl if we call tsleep() early. 1152 */ 1153 safepri = cpl; 1154 1155 proc0.p_addr = proc0paddr; 1156 1157 atdevbase = ISA_HOLE_START + KERNBASE; 1158 1159 /* 1160 * Initialize the console before we print anything out. 1161 */ 1162 cninit(); 1163 1164 /* 1165 * make gdt memory segments, the code segment goes up to end of the 1166 * page with etext in it, the data segment goes to the end of 1167 * the address space 1168 */ 1169 /* 1170 * XXX text protection is temporarily (?) disabled. The limit was 1171 * i386_btop(round_page(etext)) - 1. 1172 */ 1173 gdt_segs[GCODE_SEL].ssd_limit = i386_btop(0) - 1; 1174 gdt_segs[GDATA_SEL].ssd_limit = i386_btop(0) - 1; 1175#ifdef BDE_DEBUGGER 1176#define NGDT1 8 /* avoid overwriting db entries with APM ones */ 1177#else 1178#define NGDT1 (sizeof gdt_segs / sizeof gdt_segs[0]) 1179#endif 1180 for (x = 0; x < NGDT1; x++) 1181 ssdtosd(&gdt_segs[x], &gdt[x].sd); 1182#ifdef VM86 1183 common_tssd = gdt[GPROC0_SEL].sd; 1184#endif /* VM86 */ 1185 1186#ifdef SMP 1187 /* 1188 * Spin these up now. init_secondary() grabs them. We could use 1189 * #for(x,y,z) / #endfor cpp directives if they existed. 1190 */ 1191 for (x = 0; x < NCPU; x++) { 1192 gdt_segs[NGDT + x] = gdt_segs[GPROC0_SEL]; 1193 ssdtosd(&gdt_segs[NGDT + x], &gdt[NGDT + x].sd); 1194 } 1195#endif 1196 1197 /* make ldt memory segments */ 1198 /* 1199 * The data segment limit must not cover the user area because we 1200 * don't want the user area to be writable in copyout() etc. (page 1201 * level protection is lost in kernel mode on 386's). Also, we 1202 * don't want the user area to be writable directly (page level 1203 * protection of the user area is not available on 486's with 1204 * CR0_WP set, because there is no user-read/kernel-write mode). 1205 * 1206 * XXX - VM_MAXUSER_ADDRESS is an end address, not a max. And it 1207 * should be spelled ...MAX_USER... 1208 */ 1209#define VM_END_USER_RW_ADDRESS VM_MAXUSER_ADDRESS 1210 /* 1211 * The code segment limit has to cover the user area until we move 1212 * the signal trampoline out of the user area. This is safe because 1213 * the code segment cannot be written to directly. 1214 */ 1215#define VM_END_USER_R_ADDRESS (VM_END_USER_RW_ADDRESS + UPAGES * PAGE_SIZE) 1216 ldt_segs[LUCODE_SEL].ssd_limit = i386_btop(VM_END_USER_R_ADDRESS) - 1; 1217 ldt_segs[LUDATA_SEL].ssd_limit = i386_btop(VM_END_USER_RW_ADDRESS) - 1; 1218 for (x = 0; x < sizeof ldt_segs / sizeof ldt_segs[0]; x++) 1219 ssdtosd(&ldt_segs[x], &ldt[x].sd); 1220 1221 /* exceptions */ 1222 for (x = 0; x < NIDT; x++) 1223 setidt(x, &IDTVEC(rsvd), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 1224 setidt(0, &IDTVEC(div), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 1225 setidt(1, &IDTVEC(dbg), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 1226 setidt(2, &IDTVEC(nmi), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 1227 setidt(3, &IDTVEC(bpt), SDT_SYS386TGT, SEL_UPL, GSEL(GCODE_SEL, SEL_KPL)); 1228 setidt(4, &IDTVEC(ofl), SDT_SYS386TGT, SEL_UPL, GSEL(GCODE_SEL, SEL_KPL)); 1229 setidt(5, &IDTVEC(bnd), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 1230 setidt(6, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 1231 setidt(7, &IDTVEC(dna), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 1232 setidt(8, 0, SDT_SYSTASKGT, SEL_KPL, GSEL(GPANIC_SEL, SEL_KPL)); 1233 setidt(9, &IDTVEC(fpusegm), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 1234 setidt(10, &IDTVEC(tss), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 1235 setidt(11, &IDTVEC(missing), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 1236 setidt(12, &IDTVEC(stk), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 1237 setidt(13, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 1238 setidt(14, &IDTVEC(page), SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 1239 setidt(15, &IDTVEC(rsvd), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 1240 setidt(16, &IDTVEC(fpu), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 1241 setidt(17, &IDTVEC(align), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 1242 setidt(18, &IDTVEC(mchk), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 1243 setidt(0x80, &IDTVEC(int0x80_syscall), 1244 SDT_SYS386TGT, SEL_UPL, GSEL(GCODE_SEL, SEL_KPL)); 1245 1246#include "isa.h" 1247#if NISA >0 1248 isa_defaultirq(); 1249#endif 1250 rand_initialize(); 1251 1252 r_gdt.rd_limit = sizeof(gdt) - 1; 1253 r_gdt.rd_base = (int) gdt; 1254 lgdt(&r_gdt); 1255 1256 r_idt.rd_limit = sizeof(idt) - 1; 1257 r_idt.rd_base = (int) idt; 1258 lidt(&r_idt); 1259 1260 _default_ldt = GSEL(GLDT_SEL, SEL_KPL); 1261 lldt(_default_ldt); 1262#ifdef USER_LDT 1263 currentldt = _default_ldt; 1264#endif 1265 1266#ifdef DDB 1267 kdb_init(); 1268 if (boothowto & RB_KDB) 1269 Debugger("Boot flags requested debugger"); 1270#endif 1271 1272 finishidentcpu(); /* Final stage of CPU initialization */ 1273 setidt(6, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 1274 setidt(13, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL)); 1275 initializecpu(); /* Initialize CPU registers */ 1276 1277 /* make an initial tss so cpu can get interrupt stack on syscall! */ 1278#ifdef VM86 1279 common_tss.tss_esp0 = (int) proc0.p_addr + UPAGES*PAGE_SIZE - 16; 1280#else 1281 common_tss.tss_esp0 = (int) proc0.p_addr + UPAGES*PAGE_SIZE; 1282#endif /* VM86 */ 1283 common_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL) ; 1284 common_tss.tss_ioopt = (sizeof common_tss) << 16; 1285 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL); 1286 ltr(gsel_tss); 1287#ifdef VM86 1288 private_tss = 0; 1289 my_tr = GPROC0_SEL; 1290#endif 1291 1292 dblfault_tss.tss_esp = dblfault_tss.tss_esp0 = dblfault_tss.tss_esp1 = 1293 dblfault_tss.tss_esp2 = (int) &dblfault_stack[sizeof(dblfault_stack)]; 1294 dblfault_tss.tss_ss = dblfault_tss.tss_ss0 = dblfault_tss.tss_ss1 = 1295 dblfault_tss.tss_ss2 = GSEL(GDATA_SEL, SEL_KPL); 1296 dblfault_tss.tss_cr3 = (int)IdlePTD; 1297 dblfault_tss.tss_eip = (int) dblfault_handler; 1298 dblfault_tss.tss_eflags = PSL_KERNEL; 1299 dblfault_tss.tss_ds = dblfault_tss.tss_es = dblfault_tss.tss_fs = 1300 dblfault_tss.tss_gs = GSEL(GDATA_SEL, SEL_KPL); 1301 dblfault_tss.tss_cs = GSEL(GCODE_SEL, SEL_KPL); 1302 dblfault_tss.tss_ldt = GSEL(GLDT_SEL, SEL_KPL); 1303 1304#ifdef VM86 1305 initial_bioscalls(&biosbasemem, &biosextmem); 1306#else 1307 1308 /* Use BIOS values stored in RTC CMOS RAM, since probing 1309 * breaks certain 386 AT relics. 1310 */ 1311 biosbasemem = rtcin(RTC_BASELO)+ (rtcin(RTC_BASEHI)<<8); 1312 biosextmem = rtcin(RTC_EXTLO)+ (rtcin(RTC_EXTHI)<<8); 1313#endif 1314 1315 /* 1316 * If BIOS tells us that it has more than 640k in the basemem, 1317 * don't believe it - set it to 640k. 1318 */ 1319 if (biosbasemem > 640) { 1320 printf("Preposterous RTC basemem of %dK, truncating to 640K\n", 1321 biosbasemem); 1322 biosbasemem = 640; 1323 } 1324 if (bootinfo.bi_memsizes_valid && bootinfo.bi_basemem > 640) { 1325 printf("Preposterous BIOS basemem of %dK, truncating to 640K\n", 1326 bootinfo.bi_basemem); 1327 bootinfo.bi_basemem = 640; 1328 } 1329 1330 /* 1331 * Warn if the official BIOS interface disagrees with the RTC 1332 * interface used above about the amount of base memory or the 1333 * amount of extended memory. Prefer the BIOS value for the base 1334 * memory. This is necessary for machines that `steal' base 1335 * memory for use as BIOS memory, at least if we are going to use 1336 * the BIOS for apm. Prefer the RTC value for extended memory. 1337 * Eventually the hackish interface shouldn't even be looked at. 1338 */ 1339 if (bootinfo.bi_memsizes_valid) { 1340 if (bootinfo.bi_basemem != biosbasemem) { 1341 vm_offset_t pa; 1342 1343 printf( 1344 "BIOS basemem (%uK) != RTC basemem (%uK), setting to BIOS value\n", 1345 bootinfo.bi_basemem, biosbasemem); 1346 biosbasemem = bootinfo.bi_basemem; 1347 1348 /* 1349 * XXX if biosbasemem is now < 640, there is `hole' 1350 * between the end of base memory and the start of 1351 * ISA memory. The hole may be empty or it may 1352 * contain BIOS code or data. Map it read/write so 1353 * that the BIOS can write to it. (Memory from 0 to 1354 * the physical end of the kernel is mapped read-only 1355 * to begin with and then parts of it are remapped. 1356 * The parts that aren't remapped form holes that 1357 * remain read-only and are unused by the kernel. 1358 * The base memory area is below the physical end of 1359 * the kernel and right now forms a read-only hole. 1360 * The part of it from PAGE_SIZE to 1361 * (trunc_page(biosbasemem * 1024) - 1) will be 1362 * remapped and used by the kernel later.) 1363 * 1364 * This code is similar to the code used in 1365 * pmap_mapdev, but since no memory needs to be 1366 * allocated we simply change the mapping. 1367 */ 1368 for (pa = trunc_page(biosbasemem * 1024); 1369 pa < ISA_HOLE_START; pa += PAGE_SIZE) { 1370 unsigned *pte; 1371 1372 pte = (unsigned *)vtopte(pa + KERNBASE); 1373 *pte = pa | PG_RW | PG_V; 1374 } 1375 } 1376 if (bootinfo.bi_extmem != biosextmem) 1377 printf("BIOS extmem (%uK) != RTC extmem (%uK)\n", 1378 bootinfo.bi_extmem, biosextmem); 1379 } 1380 1381#ifdef SMP 1382 /* make hole for AP bootstrap code */ 1383 pagesinbase = mp_bootaddress(biosbasemem) / PAGE_SIZE; 1384#else 1385 pagesinbase = biosbasemem * 1024 / PAGE_SIZE; 1386#endif 1387 1388 pagesinext = biosextmem * 1024 / PAGE_SIZE; 1389 1390 /* 1391 * Special hack for chipsets that still remap the 384k hole when 1392 * there's 16MB of memory - this really confuses people that 1393 * are trying to use bus mastering ISA controllers with the 1394 * "16MB limit"; they only have 16MB, but the remapping puts 1395 * them beyond the limit. 1396 */ 1397 /* 1398 * If extended memory is between 15-16MB (16-17MB phys address range), 1399 * chop it to 15MB. 1400 */ 1401 if ((pagesinext > 3840) && (pagesinext < 4096)) 1402 pagesinext = 3840; 1403 1404 /* 1405 * Maxmem isn't the "maximum memory", it's one larger than the 1406 * highest page of the physical address space. It should be 1407 * called something like "Maxphyspage". 1408 */ 1409 Maxmem = pagesinext + 0x100000/PAGE_SIZE; 1410 /* 1411 * Indicate that we wish to do a speculative search for memory beyond 1412 * the end of the reported size if the indicated amount is 64MB (0x4000 1413 * pages) - which is the largest amount that the BIOS/bootblocks can 1414 * currently report. If a specific amount of memory is indicated via 1415 * the MAXMEM option or the npx0 "msize", then don't do the speculative 1416 * memory probe. 1417 */ 1418 if (Maxmem >= 0x4000) 1419 speculative_mprobe = TRUE; 1420 else 1421 speculative_mprobe = FALSE; 1422 1423#ifdef MAXMEM 1424 Maxmem = MAXMEM/4; 1425 speculative_mprobe = FALSE; 1426#endif 1427 1428#if NNPX > 0 1429 idp = find_isadev(isa_devtab_null, &npxdriver, 0); 1430 if (idp != NULL && idp->id_msize != 0) { 1431 Maxmem = idp->id_msize / 4; 1432 speculative_mprobe = FALSE; 1433 } 1434#endif 1435 1436#ifdef SMP 1437 /* look for the MP hardware - needed for apic addresses */ 1438 mp_probe(); 1439#endif 1440 1441 /* call pmap initialization to make new kernel address space */ 1442 pmap_bootstrap (first, 0); 1443 1444 /* 1445 * Size up each available chunk of physical memory. 1446 */ 1447 1448 /* 1449 * We currently don't bother testing base memory. 1450 * XXX ...but we probably should. 1451 */ 1452 pa_indx = 0; 1453 if (pagesinbase > 1) { 1454 phys_avail[pa_indx++] = PAGE_SIZE; /* skip first page of memory */ 1455 phys_avail[pa_indx] = ptoa(pagesinbase);/* memory up to the ISA hole */ 1456 physmem = pagesinbase - 1; 1457 } else { 1458 /* point at first chunk end */ 1459 pa_indx++; 1460 } 1461 1462 for (target_page = avail_start; target_page < ptoa(Maxmem); target_page += PAGE_SIZE) { 1463 int tmp, page_bad; 1464 1465 page_bad = FALSE; 1466 1467 /* 1468 * map page into kernel: valid, read/write, non-cacheable 1469 */ 1470 *(int *)CMAP1 = PG_V | PG_RW | PG_N | target_page; 1471 invltlb(); 1472 1473 tmp = *(int *)CADDR1; 1474 /* 1475 * Test for alternating 1's and 0's 1476 */ 1477 *(volatile int *)CADDR1 = 0xaaaaaaaa; 1478 if (*(volatile int *)CADDR1 != 0xaaaaaaaa) { 1479 page_bad = TRUE; 1480 } 1481 /* 1482 * Test for alternating 0's and 1's 1483 */ 1484 *(volatile int *)CADDR1 = 0x55555555; 1485 if (*(volatile int *)CADDR1 != 0x55555555) { 1486 page_bad = TRUE; 1487 } 1488 /* 1489 * Test for all 1's 1490 */ 1491 *(volatile int *)CADDR1 = 0xffffffff; 1492 if (*(volatile int *)CADDR1 != 0xffffffff) { 1493 page_bad = TRUE; 1494 } 1495 /* 1496 * Test for all 0's 1497 */ 1498 *(volatile int *)CADDR1 = 0x0; 1499 if (*(volatile int *)CADDR1 != 0x0) { 1500 /* 1501 * test of page failed 1502 */ 1503 page_bad = TRUE; 1504 } 1505 /* 1506 * Restore original value. 1507 */ 1508 *(int *)CADDR1 = tmp; 1509 1510 /* 1511 * Adjust array of valid/good pages. 1512 */ 1513 if (page_bad == FALSE) { 1514 /* 1515 * If this good page is a continuation of the 1516 * previous set of good pages, then just increase 1517 * the end pointer. Otherwise start a new chunk. 1518 * Note that "end" points one higher than end, 1519 * making the range >= start and < end. 1520 * If we're also doing a speculative memory 1521 * test and we at or past the end, bump up Maxmem 1522 * so that we keep going. The first bad page 1523 * will terminate the loop. 1524 */ 1525 if (phys_avail[pa_indx] == target_page) { 1526 phys_avail[pa_indx] += PAGE_SIZE; 1527 if (speculative_mprobe == TRUE && 1528 phys_avail[pa_indx] >= (64*1024*1024)) 1529 Maxmem++; 1530 } else { 1531 pa_indx++; 1532 if (pa_indx == PHYS_AVAIL_ARRAY_END) { 1533 printf("Too many holes in the physical address space, giving up\n"); 1534 pa_indx--; 1535 break; 1536 } 1537 phys_avail[pa_indx++] = target_page; /* start */ 1538 phys_avail[pa_indx] = target_page + PAGE_SIZE; /* end */ 1539 } 1540 physmem++; 1541 } 1542 } 1543 1544 *(int *)CMAP1 = 0; 1545 invltlb(); 1546 1547 /* 1548 * XXX 1549 * The last chunk must contain at least one page plus the message 1550 * buffer to avoid complicating other code (message buffer address 1551 * calculation, etc.). 1552 */ 1553 while (phys_avail[pa_indx - 1] + PAGE_SIZE + 1554 round_page(MSGBUF_SIZE) >= phys_avail[pa_indx]) { 1555 physmem -= atop(phys_avail[pa_indx] - phys_avail[pa_indx - 1]); 1556 phys_avail[pa_indx--] = 0; 1557 phys_avail[pa_indx--] = 0; 1558 } 1559 1560 Maxmem = atop(phys_avail[pa_indx]); 1561 1562 /* Trim off space for the message buffer. */ 1563 phys_avail[pa_indx] -= round_page(MSGBUF_SIZE); 1564 1565 avail_end = phys_avail[pa_indx]; 1566 1567 /* now running on new page tables, configured,and u/iom is accessible */ 1568 1569 /* Map the message buffer. */ 1570 for (off = 0; off < round_page(MSGBUF_SIZE); off += PAGE_SIZE) 1571 pmap_enter(kernel_pmap, (vm_offset_t)msgbufp + off, 1572 avail_end + off, VM_PROT_ALL, TRUE); 1573 1574 msgbufinit(msgbufp, MSGBUF_SIZE); 1575 1576 /* make a call gate to reenter kernel with */ 1577 gdp = &ldt[LSYS5CALLS_SEL].gd; 1578 1579 x = (int) &IDTVEC(syscall); 1580 gdp->gd_looffset = x++; 1581 gdp->gd_selector = GSEL(GCODE_SEL,SEL_KPL); 1582 gdp->gd_stkcpy = 1; 1583 gdp->gd_type = SDT_SYS386CGT; 1584 gdp->gd_dpl = SEL_UPL; 1585 gdp->gd_p = 1; 1586 gdp->gd_hioffset = ((int) &IDTVEC(syscall)) >>16; 1587 1588 /* XXX does this work? */ 1589 ldt[LBSDICALLS_SEL] = ldt[LSYS5CALLS_SEL]; 1590 1591 /* transfer to user mode */ 1592 1593 _ucodesel = LSEL(LUCODE_SEL, SEL_UPL); 1594 _udatasel = LSEL(LUDATA_SEL, SEL_UPL); 1595 1596 /* setup proc 0's pcb */ 1597 proc0.p_addr->u_pcb.pcb_flags = 0; 1598 proc0.p_addr->u_pcb.pcb_cr3 = (int)IdlePTD; 1599#ifdef SMP 1600 proc0.p_addr->u_pcb.pcb_mpnest = 1; 1601#endif 1602#ifdef VM86 1603 proc0.p_addr->u_pcb.pcb_ext = 0; 1604#endif 1605 1606 /* Sigh, relocate physical addresses left from bootstrap */ 1607 if (bootinfo.bi_modulep) { 1608 preload_metadata = (caddr_t)bootinfo.bi_modulep + KERNBASE; 1609 preload_bootstrap_relocate(KERNBASE); 1610 } 1611 if (bootinfo.bi_envp) 1612 kern_envp = (caddr_t)bootinfo.bi_envp + KERNBASE; 1613} 1614 1615#if defined(I586_CPU) && !defined(NO_F00F_HACK) 1616static void f00f_hack(void *unused); 1617SYSINIT(f00f_hack, SI_SUB_INTRINSIC, SI_ORDER_FIRST, f00f_hack, NULL); 1618 1619static void 1620f00f_hack(void *unused) { 1621#ifndef SMP 1622 struct region_descriptor r_idt; 1623#endif 1624 vm_offset_t tmp; 1625 1626 if (!has_f00f_bug) 1627 return; 1628 1629 printf("Intel Pentium detected, installing workaround for F00F bug\n"); 1630 1631 r_idt.rd_limit = sizeof(idt) - 1; 1632 1633 tmp = kmem_alloc(kernel_map, PAGE_SIZE * 2); 1634 if (tmp == 0) 1635 panic("kmem_alloc returned 0"); 1636 if (((unsigned int)tmp & (PAGE_SIZE-1)) != 0) 1637 panic("kmem_alloc returned non-page-aligned memory"); 1638 /* Put the first seven entries in the lower page */ 1639 t_idt = (struct gate_descriptor*)(tmp + PAGE_SIZE - (7*8)); 1640 bcopy(idt, t_idt, sizeof(idt)); 1641 r_idt.rd_base = (int)t_idt; 1642 lidt(&r_idt); 1643 if (vm_map_protect(kernel_map, tmp, tmp + PAGE_SIZE, 1644 VM_PROT_READ, FALSE) != KERN_SUCCESS) 1645 panic("vm_map_protect failed"); 1646 return; 1647} 1648#endif /* defined(I586_CPU) && !NO_F00F_HACK */ 1649 1650int 1651ptrace_set_pc(p, addr) 1652 struct proc *p; 1653 unsigned long addr; 1654{ 1655 p->p_md.md_regs->tf_eip = addr; 1656 return (0); 1657} 1658 1659int 1660ptrace_single_step(p) 1661 struct proc *p; 1662{ 1663 p->p_md.md_regs->tf_eflags |= PSL_T; 1664 return (0); 1665} 1666 1667int ptrace_read_u_check(p, addr, len) 1668 struct proc *p; 1669 vm_offset_t addr; 1670 size_t len; 1671{ 1672 vm_offset_t gap; 1673 1674 if ((vm_offset_t) (addr + len) < addr) 1675 return EPERM; 1676 if ((vm_offset_t) (addr + len) <= sizeof(struct user)) 1677 return 0; 1678 1679 gap = (char *) p->p_md.md_regs - (char *) p->p_addr; 1680 1681 if ((vm_offset_t) addr < gap) 1682 return EPERM; 1683 if ((vm_offset_t) (addr + len) <= 1684 (vm_offset_t) (gap + sizeof(struct trapframe))) 1685 return 0; 1686 return EPERM; 1687} 1688 1689int ptrace_write_u(p, off, data) 1690 struct proc *p; 1691 vm_offset_t off; 1692 long data; 1693{ 1694 struct trapframe frame_copy; 1695 vm_offset_t min; 1696 struct trapframe *tp; 1697 1698 /* 1699 * Privileged kernel state is scattered all over the user area. 1700 * Only allow write access to parts of regs and to fpregs. 1701 */ 1702 min = (char *)p->p_md.md_regs - (char *)p->p_addr; 1703 if (off >= min && off <= min + sizeof(struct trapframe) - sizeof(int)) { 1704 tp = p->p_md.md_regs; 1705 frame_copy = *tp; 1706 *(int *)((char *)&frame_copy + (off - min)) = data; 1707 if (!EFLAGS_SECURE(frame_copy.tf_eflags, tp->tf_eflags) || 1708 !CS_SECURE(frame_copy.tf_cs)) 1709 return (EINVAL); 1710 *(int*)((char *)p->p_addr + off) = data; 1711 return (0); 1712 } 1713 min = offsetof(struct user, u_pcb) + offsetof(struct pcb, pcb_savefpu); 1714 if (off >= min && off <= min + sizeof(struct save87) - sizeof(int)) { 1715 *(int*)((char *)p->p_addr + off) = data; 1716 return (0); 1717 } 1718 return (EFAULT); 1719} 1720 1721int 1722fill_regs(p, regs) 1723 struct proc *p; 1724 struct reg *regs; 1725{ 1726 struct pcb *pcb; 1727 struct trapframe *tp; 1728 1729 tp = p->p_md.md_regs; 1730 regs->r_es = tp->tf_es; 1731 regs->r_ds = tp->tf_ds; 1732 regs->r_edi = tp->tf_edi; 1733 regs->r_esi = tp->tf_esi; 1734 regs->r_ebp = tp->tf_ebp; 1735 regs->r_ebx = tp->tf_ebx; 1736 regs->r_edx = tp->tf_edx; 1737 regs->r_ecx = tp->tf_ecx; 1738 regs->r_eax = tp->tf_eax; 1739 regs->r_eip = tp->tf_eip; 1740 regs->r_cs = tp->tf_cs; 1741 regs->r_eflags = tp->tf_eflags; 1742 regs->r_esp = tp->tf_esp; 1743 regs->r_ss = tp->tf_ss; 1744 pcb = &p->p_addr->u_pcb; 1745 regs->r_fs = pcb->pcb_fs; 1746 regs->r_gs = pcb->pcb_gs; 1747 return (0); 1748} 1749 1750int 1751set_regs(p, regs) 1752 struct proc *p; 1753 struct reg *regs; 1754{ 1755 struct pcb *pcb; 1756 struct trapframe *tp; 1757 1758 tp = p->p_md.md_regs; 1759 if (!EFLAGS_SECURE(regs->r_eflags, tp->tf_eflags) || 1760 !CS_SECURE(regs->r_cs)) 1761 return (EINVAL); 1762 tp->tf_es = regs->r_es; 1763 tp->tf_ds = regs->r_ds; 1764 tp->tf_edi = regs->r_edi; 1765 tp->tf_esi = regs->r_esi; 1766 tp->tf_ebp = regs->r_ebp; 1767 tp->tf_ebx = regs->r_ebx; 1768 tp->tf_edx = regs->r_edx; 1769 tp->tf_ecx = regs->r_ecx; 1770 tp->tf_eax = regs->r_eax; 1771 tp->tf_eip = regs->r_eip; 1772 tp->tf_cs = regs->r_cs; 1773 tp->tf_eflags = regs->r_eflags; 1774 tp->tf_esp = regs->r_esp; 1775 tp->tf_ss = regs->r_ss; 1776 pcb = &p->p_addr->u_pcb; 1777 pcb->pcb_fs = regs->r_fs; 1778 pcb->pcb_gs = regs->r_gs; 1779 return (0); 1780} 1781 1782int 1783fill_fpregs(p, fpregs) 1784 struct proc *p; 1785 struct fpreg *fpregs; 1786{ 1787 bcopy(&p->p_addr->u_pcb.pcb_savefpu, fpregs, sizeof *fpregs); 1788 return (0); 1789} 1790 1791int 1792set_fpregs(p, fpregs) 1793 struct proc *p; 1794 struct fpreg *fpregs; 1795{ 1796 bcopy(fpregs, &p->p_addr->u_pcb.pcb_savefpu, sizeof *fpregs); 1797 return (0); 1798} 1799 1800#ifndef DDB 1801void 1802Debugger(const char *msg) 1803{ 1804 printf("Debugger(\"%s\") called.\n", msg); 1805} 1806#endif /* no DDB */ 1807 1808#include <sys/disklabel.h> 1809 1810/* 1811 * Determine the size of the transfer, and make sure it is 1812 * within the boundaries of the partition. Adjust transfer 1813 * if needed, and signal errors or early completion. 1814 */ 1815int 1816bounds_check_with_label(struct buf *bp, struct disklabel *lp, int wlabel) 1817{ 1818 struct partition *p = lp->d_partitions + dkpart(bp->b_dev); 1819 int labelsect = lp->d_partitions[0].p_offset; 1820 int maxsz = p->p_size, 1821 sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT; 1822 1823 /* overwriting disk label ? */ 1824 /* XXX should also protect bootstrap in first 8K */ 1825 if (bp->b_blkno + p->p_offset <= LABELSECTOR + labelsect && 1826#if LABELSECTOR != 0 1827 bp->b_blkno + p->p_offset + sz > LABELSECTOR + labelsect && 1828#endif 1829 (bp->b_flags & B_READ) == 0 && wlabel == 0) { 1830 bp->b_error = EROFS; 1831 goto bad; 1832 } 1833 1834#if defined(DOSBBSECTOR) && defined(notyet) 1835 /* overwriting master boot record? */ 1836 if (bp->b_blkno + p->p_offset <= DOSBBSECTOR && 1837 (bp->b_flags & B_READ) == 0 && wlabel == 0) { 1838 bp->b_error = EROFS; 1839 goto bad; 1840 } 1841#endif 1842 1843 /* beyond partition? */ 1844 if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) { 1845 /* if exactly at end of disk, return an EOF */ 1846 if (bp->b_blkno == maxsz) { 1847 bp->b_resid = bp->b_bcount; 1848 return(0); 1849 } 1850 /* or truncate if part of it fits */ 1851 sz = maxsz - bp->b_blkno; 1852 if (sz <= 0) { 1853 bp->b_error = EINVAL; 1854 goto bad; 1855 } 1856 bp->b_bcount = sz << DEV_BSHIFT; 1857 } 1858 1859 bp->b_pblkno = bp->b_blkno + p->p_offset; 1860 return(1); 1861 1862bad: 1863 bp->b_flags |= B_ERROR; 1864 return(-1); 1865} 1866 1867#ifdef DDB 1868 1869/* 1870 * Provide inb() and outb() as functions. They are normally only 1871 * available as macros calling inlined functions, thus cannot be 1872 * called inside DDB. 1873 * 1874 * The actual code is stolen from <machine/cpufunc.h>, and de-inlined. 1875 */ 1876 1877#undef inb 1878#undef outb 1879 1880/* silence compiler warnings */ 1881u_char inb(u_int); 1882void outb(u_int, u_char); 1883 1884u_char 1885inb(u_int port) 1886{ 1887 u_char data; 1888 /* 1889 * We use %%dx and not %1 here because i/o is done at %dx and not at 1890 * %edx, while gcc generates inferior code (movw instead of movl) 1891 * if we tell it to load (u_short) port. 1892 */ 1893 __asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port)); 1894 return (data); 1895} 1896 1897void 1898outb(u_int port, u_char data) 1899{ 1900 u_char al; 1901 /* 1902 * Use an unnecessary assignment to help gcc's register allocator. 1903 * This make a large difference for gcc-1.40 and a tiny difference 1904 * for gcc-2.6.0. For gcc-1.40, al had to be ``asm("ax")'' for 1905 * best results. gcc-2.6.0 can't handle this. 1906 */ 1907 al = data; 1908 __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port)); 1909} 1910 1911#endif /* DDB */ 1912