machdep.c revision 12429
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.151 1995/11/14 09:52:25 phk Exp $ 39 */ 40 41#include "npx.h" 42#include "isa.h" 43 44#include <sys/param.h> 45#include <sys/systm.h> 46#include <sys/sysproto.h> 47#include <sys/signalvar.h> 48#include <sys/kernel.h> 49#include <sys/proc.h> 50#include <sys/user.h> 51#include <sys/buf.h> 52#include <sys/reboot.h> 53#include <sys/conf.h> 54#include <sys/file.h> 55#include <sys/callout.h> 56#include <sys/malloc.h> 57#include <sys/mbuf.h> 58#include <sys/mount.h> 59#include <sys/msgbuf.h> 60#include <sys/ioctl.h> 61#include <sys/sysent.h> 62#include <sys/tty.h> 63#include <sys/sysctl.h> 64#include <sys/devconf.h> 65 66#ifdef SYSVSHM 67#include <sys/shm.h> 68#endif 69 70#ifdef SYSVMSG 71#include <sys/msg.h> 72#endif 73 74#ifdef SYSVSEM 75#include <sys/sem.h> 76#endif 77 78#include <vm/vm.h> 79#include <vm/vm_kern.h> 80#include <vm/vm_page.h> 81#include <vm/vm_pager.h> 82 83#include <sys/exec.h> 84#include <sys/vnode.h> 85 86#include <ddb/ddb.h> 87 88#include <net/netisr.h> 89 90/* XXX correctly declaring all the netisr's is painful. */ 91#include <net/if.h> 92#include <net/route.h> 93 94#include <netinet/in.h> 95#include <netinet/in_systm.h> 96#include <netinet/ip.h> 97#include <netinet/if_ether.h> 98#include <netinet/ip_var.h> 99 100#include <netns/ns.h> 101#include <netns/ns_if.h> 102 103#include <netiso/iso.h> 104#include <netiso/iso_var.h> 105 106#include <netccitt/dll.h> 107#include <netccitt/x25.h> 108#include <netccitt/pk.h> 109#include <sys/socketvar.h> 110#include <netccitt/pk_var.h> 111 112#include "ether.h" 113 114#include <machine/cpu.h> 115#include <machine/npx.h> 116#include <machine/reg.h> 117#include <machine/psl.h> 118#include <machine/clock.h> 119#include <machine/specialreg.h> 120#include <machine/sysarch.h> 121#include <machine/cons.h> 122#include <machine/devconf.h> 123#include <machine/bootinfo.h> 124#include <machine/md_var.h> 125 126#include <i386/isa/isa.h> 127#include <i386/isa/isa_device.h> 128#include <i386/isa/rtc.h> 129#include <machine/random.h> 130 131extern void diediedie __P((void)); 132extern void init386 __P((int first)); 133extern int ptrace_set_pc __P((struct proc *p, unsigned int addr)); 134extern int ptrace_single_step __P((struct proc *p)); 135extern int ptrace_getregs __P((struct proc *p, unsigned int *addr)); 136extern int ptrace_setregs __P((struct proc *p, unsigned int *addr)); 137extern int ptrace_write_u __P((struct proc *p, vm_offset_t off, int data)); 138 139static void cpu_startup __P((void *)); 140SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL) 141 142static void identifycpu(void); 143 144char machine[] = "i386"; 145char cpu_model[128]; 146 147struct kern_devconf kdc_cpu0 = { 148 0, 0, 0, /* filled in by dev_attach */ 149 "cpu", 0, { MDDT_CPU }, 150 0, 0, 0, CPU_EXTERNALLEN, 151 0, /* CPU has no parent */ 152 0, /* no parentdata */ 153 DC_BUSY, /* the CPU is always busy */ 154 cpu_model, /* no sense in duplication */ 155 DC_CLS_CPU /* class */ 156}; 157 158#ifndef PANIC_REBOOT_WAIT_TIME 159#define PANIC_REBOOT_WAIT_TIME 15 /* default to 15 seconds */ 160#endif 161 162#ifdef BOUNCE_BUFFERS 163extern char *bouncememory; 164extern int maxbkva; 165#ifdef BOUNCEPAGES 166int bouncepages = BOUNCEPAGES; 167#else 168int bouncepages = 0; 169#endif 170#endif /* BOUNCE_BUFFERS */ 171 172extern int freebufspace; 173int msgbufmapped = 0; /* set when safe to use msgbuf */ 174int _udatasel, _ucodesel; 175 176 177/* 178 * Machine-dependent startup code 179 */ 180int boothowto = 0, bootverbose = 0, Maxmem = 0, badpages = 0, physmem = 0; 181long dumplo; 182extern int bootdev; 183int biosmem; 184 185vm_offset_t phys_avail[10]; 186 187/* must be 2 less so 0 0 can signal end of chunks */ 188#define PHYS_AVAIL_ARRAY_END ((sizeof(phys_avail) / sizeof(vm_offset_t)) - 2) 189 190int cpu_class; 191 192void dumpsys __P((void)); 193void setup_netisrs __P((struct linker_set *)); /* XXX declare elsewhere */ 194 195vm_offset_t buffer_sva, buffer_eva; 196vm_offset_t clean_sva, clean_eva; 197vm_offset_t pager_sva, pager_eva; 198extern struct linker_set netisr_set; 199 200#define offsetof(type, member) ((size_t)(&((type *)0)->member)) 201 202static void 203cpu_startup(dummy) 204 void *dummy; 205{ 206 register unsigned i; 207 register caddr_t v; 208 vm_offset_t maxaddr; 209 vm_size_t size = 0; 210 int firstaddr; 211 vm_offset_t minaddr; 212 213 if (boothowto & RB_VERBOSE) 214 bootverbose++; 215 216 /* 217 * Initialize error message buffer (at end of core). 218 */ 219 220 /* avail_end was pre-decremented in init_386() to compensate */ 221 for (i = 0; i < btoc(sizeof (struct msgbuf)); i++) 222 pmap_enter(pmap_kernel(), (vm_offset_t)msgbufp, 223 avail_end + i * NBPG, 224 VM_PROT_ALL, TRUE); 225 msgbufmapped = 1; 226 227 /* 228 * Good {morning,afternoon,evening,night}. 229 */ 230 printf(version); 231 startrtclock(); 232 identifycpu(); 233 printf("real memory = %d (%dK bytes)\n", ptoa(Maxmem), ptoa(Maxmem) / 1024); 234 /* 235 * Display any holes after the first chunk of extended memory. 236 */ 237 if (badpages != 0) { 238 int indx = 1; 239 240 /* 241 * XXX skip reporting ISA hole & unmanaged kernel memory 242 */ 243 if (phys_avail[0] == PAGE_SIZE) 244 indx += 2; 245 246 printf("Physical memory hole(s):\n"); 247 for (; phys_avail[indx + 1] != 0; indx += 2) { 248 int size = phys_avail[indx + 1] - phys_avail[indx]; 249 250 printf("0x%08lx - 0x%08lx, %d bytes (%d pages)\n", phys_avail[indx], 251 phys_avail[indx + 1] - 1, size, size / PAGE_SIZE); 252 } 253 } 254 255 /* 256 * Quickly wire in netisrs. 257 */ 258 setup_netisrs(&netisr_set); 259 260/* 261#ifdef ISDN 262 DONET(isdnintr, NETISR_ISDN); 263#endif 264*/ 265 266 /* 267 * Allocate space for system data structures. 268 * The first available kernel virtual address is in "v". 269 * As pages of kernel virtual memory are allocated, "v" is incremented. 270 * As pages of memory are allocated and cleared, 271 * "firstaddr" is incremented. 272 * An index into the kernel page table corresponding to the 273 * virtual memory address maintained in "v" is kept in "mapaddr". 274 */ 275 276 /* 277 * Make two passes. The first pass calculates how much memory is 278 * needed and allocates it. The second pass assigns virtual 279 * addresses to the various data structures. 280 */ 281 firstaddr = 0; 282again: 283 v = (caddr_t)firstaddr; 284 285#define valloc(name, type, num) \ 286 (name) = (type *)v; v = (caddr_t)((name)+(num)) 287#define valloclim(name, type, num, lim) \ 288 (name) = (type *)v; v = (caddr_t)((lim) = ((name)+(num))) 289 valloc(callout, struct callout, ncallout); 290#ifdef SYSVSHM 291 valloc(shmsegs, struct shmid_ds, shminfo.shmmni); 292#endif 293#ifdef SYSVSEM 294 valloc(sema, struct semid_ds, seminfo.semmni); 295 valloc(sem, struct sem, seminfo.semmns); 296 /* This is pretty disgusting! */ 297 valloc(semu, int, (seminfo.semmnu * seminfo.semusz) / sizeof(int)); 298#endif 299#ifdef SYSVMSG 300 valloc(msgpool, char, msginfo.msgmax); 301 valloc(msgmaps, struct msgmap, msginfo.msgseg); 302 valloc(msghdrs, struct msg, msginfo.msgtql); 303 valloc(msqids, struct msqid_ds, msginfo.msgmni); 304#endif 305 306 if (nbuf == 0) { 307 nbuf = 30; 308 if( physmem > 1024) 309 nbuf += min((physmem - 1024) / 12, 1024); 310 } 311 nswbuf = min(nbuf, 128); 312 313 valloc(swbuf, struct buf, nswbuf); 314 valloc(buf, struct buf, nbuf); 315 316#ifdef BOUNCE_BUFFERS 317 /* 318 * If there is more than 16MB of memory, allocate some bounce buffers 319 */ 320 if (Maxmem > 4096) { 321 if (bouncepages == 0) { 322 bouncepages = 64; 323 bouncepages += ((Maxmem - 4096) / 2048) * 32; 324 } 325 v = (caddr_t)((vm_offset_t)((vm_offset_t)v + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)); 326 valloc(bouncememory, char, bouncepages * PAGE_SIZE); 327 } 328#endif 329 330 /* 331 * End of first pass, size has been calculated so allocate memory 332 */ 333 if (firstaddr == 0) { 334 size = (vm_size_t)(v - firstaddr); 335 firstaddr = (int)kmem_alloc(kernel_map, round_page(size)); 336 if (firstaddr == 0) 337 panic("startup: no room for tables"); 338 goto again; 339 } 340 341 /* 342 * End of second pass, addresses have been assigned 343 */ 344 if ((vm_size_t)(v - firstaddr) != size) 345 panic("startup: table size inconsistency"); 346 347#ifdef BOUNCE_BUFFERS 348 clean_map = kmem_suballoc(kernel_map, &clean_sva, &clean_eva, 349 (nbuf*MAXBSIZE) + (nswbuf*MAXPHYS) + 350 maxbkva + pager_map_size, TRUE); 351 io_map = kmem_suballoc(clean_map, &minaddr, &maxaddr, maxbkva, FALSE); 352#else 353 clean_map = kmem_suballoc(kernel_map, &clean_sva, &clean_eva, 354 (nbuf*MAXBSIZE) + (nswbuf*MAXPHYS) + pager_map_size, TRUE); 355#endif 356 buffer_map = kmem_suballoc(clean_map, &buffer_sva, &buffer_eva, 357 (nbuf*MAXBSIZE), TRUE); 358 pager_map = kmem_suballoc(clean_map, &pager_sva, &pager_eva, 359 (nswbuf*MAXPHYS) + pager_map_size, TRUE); 360 exec_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr, 361 (16*ARG_MAX), TRUE); 362 u_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr, 363 (maxproc*UPAGES*PAGE_SIZE), FALSE); 364 365 /* 366 * Finally, allocate mbuf pool. Since mclrefcnt is an off-size 367 * we use the more space efficient malloc in place of kmem_alloc. 368 */ 369 mclrefcnt = (char *)malloc(nmbclusters+CLBYTES/MCLBYTES, 370 M_MBUF, M_NOWAIT); 371 bzero(mclrefcnt, nmbclusters+CLBYTES/MCLBYTES); 372 mb_map = kmem_suballoc(kmem_map, (vm_offset_t *)&mbutl, &maxaddr, 373 nmbclusters * MCLBYTES, FALSE); 374 /* 375 * Initialize callouts 376 */ 377 callfree = callout; 378 for (i = 1; i < ncallout; i++) 379 callout[i-1].c_next = &callout[i]; 380 381 if (boothowto & RB_CONFIG) { 382 userconfig(); 383 cninit(); /* the preferred console may have changed */ 384 } 385 386#ifdef BOUNCE_BUFFERS 387 /* 388 * init bounce buffers 389 */ 390 vm_bounce_init(); 391#endif 392 /* 393 * XXX allocate a contiguous area for ISA (non busmaster) DMA 394 * operations. This _should_ only be done if the DMA channels 395 * will actually be used, but for now we do it always. 396 */ 397#define DMAPAGES 8 398 isaphysmem = 399 vm_page_alloc_contig(DMAPAGES * PAGE_SIZE, 0, 0xfffffful, 64*1024); 400 401 printf("avail memory = %d (%dK bytes)\n", ptoa(cnt.v_free_count), 402 ptoa(cnt.v_free_count) / 1024); 403 404 /* 405 * Set up buffers, so they can be used to read disk labels. 406 */ 407 bufinit(); 408 vm_pager_bufferinit(); 409 410 /* 411 * In verbose mode, print out the BIOS's idea of the disk geometries. 412 */ 413 if (bootverbose) { 414 printf("BIOS Geometries:\n"); 415 for (i = 0; i < N_BIOS_GEOM; i++) { 416 unsigned long bios_geom; 417 int max_cylinder, max_head, max_sector; 418 419 bios_geom = bootinfo.bi_bios_geom[i]; 420 421 /* 422 * XXX the bootstrap punts a 1200K floppy geometry 423 * when the get-disk-geometry interrupt fails. Skip 424 * drives that have this geometry. 425 */ 426 if (bios_geom == 0x4f010f) 427 continue; 428 429 printf(" %x:%08lx ", i, bios_geom); 430 max_cylinder = bios_geom >> 16; 431 max_head = (bios_geom >> 8) & 0xff; 432 max_sector = bios_geom & 0xff; 433 printf( 434 "0..%d=%d cylinders, 0..%d=%d heads, 1..%d=%d sectors\n", 435 max_cylinder, max_cylinder + 1, 436 max_head, max_head + 1, 437 max_sector, max_sector); 438 } 439 printf(" %d accounted for\n", bootinfo.bi_n_bios_used); 440 } 441} 442 443int 444register_netisr(num, handler) 445 int num; 446 netisr_t *handler; 447{ 448 449 if (num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs)) ) { 450 printf("register_netisr: bad isr number: %d\n", num); 451 return (EINVAL); 452 } 453 netisrs[num] = handler; 454 return (0); 455} 456 457void 458setup_netisrs(ls) 459 struct linker_set *ls; 460{ 461 int i; 462 const struct netisrtab *nit; 463 464 for(i = 0; ls->ls_items[i]; i++) { 465 nit = (const struct netisrtab *)ls->ls_items[i]; 466 register_netisr(nit->nit_num, nit->nit_isr); 467 } 468} 469 470struct cpu_nameclass i386_cpus[] = { 471 { "Intel 80286", CPUCLASS_286 }, /* CPU_286 */ 472 { "i386SX", CPUCLASS_386 }, /* CPU_386SX */ 473 { "i386DX", CPUCLASS_386 }, /* CPU_386 */ 474 { "i486SX", CPUCLASS_486 }, /* CPU_486SX */ 475 { "i486DX", CPUCLASS_486 }, /* CPU_486 */ 476 { "Pentium", CPUCLASS_586 }, /* CPU_586 */ 477 { "Cy486DLC", CPUCLASS_486 }, /* CPU_486DLC */ 478}; 479 480static void 481identifycpu() 482{ 483 printf("CPU: "); 484 if (cpu >= 0 485 && cpu < (sizeof i386_cpus/sizeof(struct cpu_nameclass))) { 486 cpu_class = i386_cpus[cpu].cpu_class; 487 strncpy(cpu_model, i386_cpus[cpu].cpu_name, sizeof cpu_model); 488 } else { 489 printf("unknown cpu type %d\n", cpu); 490 panic("startup: bad cpu id"); 491 } 492 493#if defined(I586_CPU) 494 if(cpu_class == CPUCLASS_586) { 495 calibrate_cyclecounter(); 496 printf("%d-MHz ", pentium_mhz); 497 } 498#endif 499#if defined(I486_CPU) || defined(I586_CPU) 500 if (!strcmp(cpu_vendor,"GenuineIntel")) { 501 if ((cpu_id & 0xf00) > 3) { 502 cpu_model[0] = '\0'; 503 504 switch (cpu_id & 0x3000) { 505 case 0x1000: 506 strcpy(cpu_model, "Overdrive "); 507 break; 508 case 0x2000: 509 strcpy(cpu_model, "Dual "); 510 break; 511 } 512 if ((cpu_id & 0xf00) == 0x400) { 513 strcat(cpu_model, "i486 "); 514#if defined(I586_CPU) 515 } else if ((cpu_id & 0xf00) == 0x500) { 516 strcat(cpu_model, "Pentium "); 517#endif 518 } else { 519 strcat(cpu_model, "unknown "); 520 } 521 522 switch (cpu_id & 0xff0) { 523 case 0x400: 524 strcat(cpu_model, "DX"); break; 525 case 0x410: 526 strcat(cpu_model, "DX"); break; 527 case 0x420: 528 strcat(cpu_model, "SX"); break; 529 case 0x430: 530 strcat(cpu_model, "DX2"); break; 531 case 0x440: 532 strcat(cpu_model, "SL"); break; 533 case 0x450: 534 strcat(cpu_model, "SX2"); break; 535 case 0x470: 536 strcat(cpu_model, "DX2 Write-Back Enhanced"); 537 break; 538 case 0x480: 539 strcat(cpu_model, "DX4"); break; 540#if defined(I586_CPU) 541 case 0x510: 542 if (pentium_mhz == 60) { 543 strcat(cpu_model, "510\\60"); 544 } else if (pentium_mhz == 66) { 545 strcat(cpu_model, "567\\66"); 546 } else { 547 strcat(cpu_model,"510\\60 or 567\\66"); 548 } 549 break; 550 case 0x520: 551 if (pentium_mhz == 90) { 552 strcat(cpu_model, "735\\90"); 553 } else if (pentium_mhz == 100) { 554 strcat(cpu_model, "815\\100"); 555 } else { 556 strcat(cpu_model,"735\\90 or 815\\100"); 557 } 558 break; 559#endif 560 } 561 } 562 } 563#endif 564 printf("%s (", cpu_model); 565 switch(cpu_class) { 566 case CPUCLASS_286: 567 printf("286"); 568 break; 569#if defined(I386_CPU) 570 case CPUCLASS_386: 571 printf("386"); 572 break; 573#endif 574#if defined(I486_CPU) 575 case CPUCLASS_486: 576 printf("486"); 577 break; 578#endif 579#if defined(I586_CPU) 580 case CPUCLASS_586: 581 printf("Pentium"); 582 break; 583#endif 584 default: 585 printf("unknown"); /* will panic below... */ 586 } 587 printf("-class CPU)\n"); 588#if defined(I486_CPU) || defined(I586_CPU) 589 if(*cpu_vendor) 590 printf(" Origin = \"%s\"",cpu_vendor); 591 if(cpu_id) 592 printf(" Id = 0x%lx",cpu_id); 593 594 if (!strcmp(cpu_vendor, "GenuineIntel")) { 595 printf(" Stepping=%ld", cpu_id & 0xf); 596 if (cpu_high > 0) { 597#define FEATUREFMT "\020\001FPU\002VME\003PSE\004MCE\005CX8\006APIC" 598 printf("\n Features=0x%b", cpu_feature, FEATUREFMT); 599 } 600 } 601 /* Avoid ugly blank lines: only print newline when we have to. */ 602 if (*cpu_vendor || cpu_id) 603 printf("\n"); 604#endif 605 /* 606 * Now that we have told the user what they have, 607 * let them know if that machine type isn't configured. 608 */ 609 switch (cpu_class) { 610 case CPUCLASS_286: /* a 286 should not make it this far, anyway */ 611#if !defined(I386_CPU) && !defined(I486_CPU) && !defined(I586_CPU) 612#error This kernel is not configured for one of the supported CPUs 613#endif 614#if !defined(I386_CPU) 615 case CPUCLASS_386: 616#endif 617#if !defined(I486_CPU) 618 case CPUCLASS_486: 619#endif 620#if !defined(I586_CPU) 621 case CPUCLASS_586: 622#endif 623 panic("CPU class not configured"); 624 default: 625 break; 626 } 627 dev_attach(&kdc_cpu0); 628} 629 630/* 631 * Send an interrupt to process. 632 * 633 * Stack is set up to allow sigcode stored 634 * in u. to call routine, followed by kcall 635 * to sigreturn routine below. After sigreturn 636 * resets the signal mask, the stack, and the 637 * frame pointer, it returns to the user 638 * specified pc, psl. 639 */ 640void 641sendsig(catcher, sig, mask, code) 642 sig_t catcher; 643 int sig, mask; 644 unsigned code; 645{ 646 register struct proc *p = curproc; 647 register int *regs; 648 register struct sigframe *fp; 649 struct sigframe sf; 650 struct sigacts *psp = p->p_sigacts; 651 int oonstack; 652 653 regs = p->p_md.md_regs; 654 oonstack = psp->ps_sigstk.ss_flags & SA_ONSTACK; 655 /* 656 * Allocate and validate space for the signal handler 657 * context. Note that if the stack is in P0 space, the 658 * call to grow() is a nop, and the useracc() check 659 * will fail if the process has not already allocated 660 * the space with a `brk'. 661 */ 662 if ((psp->ps_flags & SAS_ALTSTACK) && 663 (psp->ps_sigstk.ss_flags & SA_ONSTACK) == 0 && 664 (psp->ps_sigonstack & sigmask(sig))) { 665 fp = (struct sigframe *)(psp->ps_sigstk.ss_sp + 666 psp->ps_sigstk.ss_size - sizeof(struct sigframe)); 667 psp->ps_sigstk.ss_flags |= SA_ONSTACK; 668 } else { 669 fp = (struct sigframe *)(regs[tESP] 670 - sizeof(struct sigframe)); 671 } 672 673 /* 674 * grow() will return FALSE if the fp will not fit inside the stack 675 * and the stack can not be grown. useracc will return FALSE 676 * if access is denied. 677 */ 678 if ((grow(p, (int)fp) == FALSE) || 679 (useracc((caddr_t)fp, sizeof (struct sigframe), B_WRITE) == FALSE)) { 680 /* 681 * Process has trashed its stack; give it an illegal 682 * instruction to halt it in its tracks. 683 */ 684 SIGACTION(p, SIGILL) = SIG_DFL; 685 sig = sigmask(SIGILL); 686 p->p_sigignore &= ~sig; 687 p->p_sigcatch &= ~sig; 688 p->p_sigmask &= ~sig; 689 psignal(p, SIGILL); 690 return; 691 } 692 693 /* 694 * Build the argument list for the signal handler. 695 */ 696 if (p->p_sysent->sv_sigtbl) { 697 if (sig < p->p_sysent->sv_sigsize) 698 sig = p->p_sysent->sv_sigtbl[sig]; 699 else 700 sig = p->p_sysent->sv_sigsize + 1; 701 } 702 sf.sf_signum = sig; 703 sf.sf_code = code; 704 sf.sf_scp = &fp->sf_sc; 705 sf.sf_addr = (char *) regs[tERR]; 706 sf.sf_handler = catcher; 707 708 /* save scratch registers */ 709 sf.sf_sc.sc_eax = regs[tEAX]; 710 sf.sf_sc.sc_ebx = regs[tEBX]; 711 sf.sf_sc.sc_ecx = regs[tECX]; 712 sf.sf_sc.sc_edx = regs[tEDX]; 713 sf.sf_sc.sc_esi = regs[tESI]; 714 sf.sf_sc.sc_edi = regs[tEDI]; 715 sf.sf_sc.sc_cs = regs[tCS]; 716 sf.sf_sc.sc_ds = regs[tDS]; 717 sf.sf_sc.sc_ss = regs[tSS]; 718 sf.sf_sc.sc_es = regs[tES]; 719 sf.sf_sc.sc_isp = regs[tISP]; 720 721 /* 722 * Build the signal context to be used by sigreturn. 723 */ 724 sf.sf_sc.sc_onstack = oonstack; 725 sf.sf_sc.sc_mask = mask; 726 sf.sf_sc.sc_sp = regs[tESP]; 727 sf.sf_sc.sc_fp = regs[tEBP]; 728 sf.sf_sc.sc_pc = regs[tEIP]; 729 sf.sf_sc.sc_ps = regs[tEFLAGS]; 730 731 /* 732 * Copy the sigframe out to the user's stack. 733 */ 734 if (copyout(&sf, fp, sizeof(struct sigframe)) != 0) { 735 /* 736 * Something is wrong with the stack pointer. 737 * ...Kill the process. 738 */ 739 sigexit(p, SIGILL); 740 }; 741 742 regs[tESP] = (int)fp; 743 regs[tEIP] = (int)((struct pcb *)kstack)->pcb_sigc; 744 regs[tEFLAGS] &= ~PSL_VM; 745 regs[tCS] = _ucodesel; 746 regs[tDS] = _udatasel; 747 regs[tES] = _udatasel; 748 regs[tSS] = _udatasel; 749} 750 751/* 752 * System call to cleanup state after a signal 753 * has been taken. Reset signal mask and 754 * stack state from context left by sendsig (above). 755 * Return to previous pc and psl as specified by 756 * context left by sendsig. Check carefully to 757 * make sure that the user has not modified the 758 * state to gain improper privileges. 759 */ 760int 761sigreturn(p, uap, retval) 762 struct proc *p; 763 struct sigreturn_args /* { 764 struct sigcontext *sigcntxp; 765 } */ *uap; 766 int *retval; 767{ 768 register struct sigcontext *scp; 769 register struct sigframe *fp; 770 register int *regs = p->p_md.md_regs; 771 int eflags; 772 773 /* 774 * (XXX old comment) regs[tESP] points to the return address. 775 * The user scp pointer is above that. 776 * The return address is faked in the signal trampoline code 777 * for consistency. 778 */ 779 scp = uap->sigcntxp; 780 fp = (struct sigframe *) 781 ((caddr_t)scp - offsetof(struct sigframe, sf_sc)); 782 783 if (useracc((caddr_t)fp, sizeof (*fp), 0) == 0) 784 return(EINVAL); 785 786 /* 787 * Don't allow users to change privileged or reserved flags. 788 */ 789#define EFLAGS_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0) 790 eflags = scp->sc_ps; 791 /* 792 * XXX do allow users to change the privileged flag PSL_RF. The 793 * cpu sets PSL_RF in tf_eflags for faults. Debuggers should 794 * sometimes set it there too. tf_eflags is kept in the signal 795 * context during signal handling and there is no other place 796 * to remember it, so the PSL_RF bit may be corrupted by the 797 * signal handler without us knowing. Corruption of the PSL_RF 798 * bit at worst causes one more or one less debugger trap, so 799 * allowing it is fairly harmless. 800 */ 801 if (!EFLAGS_SECURE(eflags & ~PSL_RF, regs[tEFLAGS] & ~PSL_RF)) { 802#ifdef DEBUG 803 printf("sigreturn: eflags = 0x%x\n", eflags); 804#endif 805 return(EINVAL); 806 } 807 808 /* 809 * Don't allow users to load a valid privileged %cs. Let the 810 * hardware check for invalid selectors, excess privilege in 811 * other selectors, invalid %eip's and invalid %esp's. 812 */ 813#define CS_SECURE(cs) (ISPL(cs) == SEL_UPL) 814 if (!CS_SECURE(scp->sc_cs)) { 815#ifdef DEBUG 816 printf("sigreturn: cs = 0x%x\n", scp->sc_cs); 817#endif 818 trapsignal(p, SIGBUS, T_PROTFLT); 819 return(EINVAL); 820 } 821 822 /* restore scratch registers */ 823 regs[tEAX] = scp->sc_eax; 824 regs[tEBX] = scp->sc_ebx; 825 regs[tECX] = scp->sc_ecx; 826 regs[tEDX] = scp->sc_edx; 827 regs[tESI] = scp->sc_esi; 828 regs[tEDI] = scp->sc_edi; 829 regs[tCS] = scp->sc_cs; 830 regs[tDS] = scp->sc_ds; 831 regs[tES] = scp->sc_es; 832 regs[tSS] = scp->sc_ss; 833 regs[tISP] = scp->sc_isp; 834 835 if (useracc((caddr_t)scp, sizeof (*scp), 0) == 0) 836 return(EINVAL); 837 838 if (scp->sc_onstack & 01) 839 p->p_sigacts->ps_sigstk.ss_flags |= SA_ONSTACK; 840 else 841 p->p_sigacts->ps_sigstk.ss_flags &= ~SA_ONSTACK; 842 p->p_sigmask = scp->sc_mask &~ 843 (sigmask(SIGKILL)|sigmask(SIGCONT)|sigmask(SIGSTOP)); 844 regs[tEBP] = scp->sc_fp; 845 regs[tESP] = scp->sc_sp; 846 regs[tEIP] = scp->sc_pc; 847 regs[tEFLAGS] = eflags; 848 return(EJUSTRETURN); 849} 850 851/* 852 * a simple function to make the system panic (and dump a vmcore) 853 * in a predictable fashion 854 */ 855void diediedie() 856{ 857 panic("because you said to!"); 858} 859 860int waittime = -1; 861struct pcb dumppcb; 862 863__dead void 864boot(howto) 865 int howto; 866{ 867 if (!cold && (howto & RB_NOSYNC) == 0 && waittime < 0) { 868 register struct buf *bp; 869 int iter, nbusy; 870 871 waittime = 0; 872 printf("\nsyncing disks... "); 873 874 sync(&proc0, NULL, NULL); 875 876 for (iter = 0; iter < 20; iter++) { 877 nbusy = 0; 878 for (bp = &buf[nbuf]; --bp >= buf; ) { 879 if ((bp->b_flags & (B_BUSY | B_INVAL)) == B_BUSY) { 880 nbusy++; 881 } 882 } 883 if (nbusy == 0) 884 break; 885 printf("%d ", nbusy); 886 DELAY(40000 * iter); 887 } 888 if (nbusy) { 889 /* 890 * Failed to sync all blocks. Indicate this and don't 891 * unmount filesystems (thus forcing an fsck on reboot). 892 */ 893 printf("giving up\n"); 894#ifdef SHOW_BUSYBUFS 895 nbusy = 0; 896 for (bp = &buf[nbuf]; --bp >= buf; ) { 897 if ((bp->b_flags & (B_BUSY | B_INVAL)) == B_BUSY) { 898 nbusy++; 899 printf("%d: dev:%08x, flags:%08x, blkno:%d, lblkno:%d\n", nbusy, bp->b_dev, bp->b_flags, bp->b_blkno, bp->b_lblkno); 900 } 901 } 902 DELAY(5000000); /* 5 seconds */ 903#endif 904 } else { 905 printf("done\n"); 906 /* 907 * Unmount filesystems 908 */ 909 if (panicstr == 0) 910 vfs_unmountall(); 911 } 912 DELAY(100000); /* wait for console output to finish */ 913 dev_shutdownall(FALSE); 914 } 915 splhigh(); 916 if (howto & RB_HALT) { 917 printf("\n"); 918 printf("The operating system has halted.\n"); 919 printf("Please press any key to reboot.\n\n"); 920 cngetc(); 921 } else { 922 if (howto & RB_DUMP) { 923 if (!cold) { 924 savectx(&dumppcb, 0); 925 dumppcb.pcb_ptd = rcr3(); 926 dumpsys(); 927 } 928 929 if (PANIC_REBOOT_WAIT_TIME != 0) { 930 if (PANIC_REBOOT_WAIT_TIME != -1) { 931 int loop; 932 printf("Automatic reboot in %d seconds - press a key on the console to abort\n", 933 PANIC_REBOOT_WAIT_TIME); 934 for (loop = PANIC_REBOOT_WAIT_TIME * 10; loop > 0; --loop) { 935 DELAY(1000 * 100); /* 1/10th second */ 936 if (cncheckc()) /* Did user type a key? */ 937 break; 938 } 939 if (!loop) 940 goto die; 941 } 942 } else { /* zero time specified - reboot NOW */ 943 goto die; 944 } 945 printf("--> Press a key on the console to reboot <--\n"); 946 cngetc(); 947 } 948 } 949die: 950 printf("Rebooting...\n"); 951 DELAY(1000000); /* wait 1 sec for printf's to complete and be read */ 952 cpu_reset(); 953 for(;;) ; 954 /* NOTREACHED */ 955} 956 957unsigned long dumpmag = 0x8fca0101UL; /* magic number for savecore */ 958int dumpsize = 0; /* also for savecore */ 959 960int dodump = 1; 961 962/* 963 * Doadump comes here after turning off memory management and 964 * getting on the dump stack, either when called above, or by 965 * the auto-restart code. 966 */ 967void 968dumpsys() 969{ 970 971 if (!dodump) 972 return; 973 if (dumpdev == NODEV) 974 return; 975 if ((minor(dumpdev)&07) != 1) 976 return; 977 dumpsize = Maxmem; 978 printf("\ndumping to dev %lx, offset %ld\n", dumpdev, dumplo); 979 printf("dump "); 980 switch ((*bdevsw[major(dumpdev)].d_dump)(dumpdev)) { 981 982 case ENXIO: 983 printf("device bad\n"); 984 break; 985 986 case EFAULT: 987 printf("device not ready\n"); 988 break; 989 990 case EINVAL: 991 printf("area improper\n"); 992 break; 993 994 case EIO: 995 printf("i/o error\n"); 996 break; 997 998 case EINTR: 999 printf("aborted from console\n"); 1000 break; 1001 1002 default: 1003 printf("succeeded\n"); 1004 break; 1005 } 1006} 1007 1008/* 1009 * Clear registers on exec 1010 */ 1011void 1012setregs(p, entry, stack) 1013 struct proc *p; 1014 u_long entry; 1015 u_long stack; 1016{ 1017 int *regs = p->p_md.md_regs; 1018 1019 bzero(regs, sizeof(struct trapframe)); 1020 regs[tEIP] = entry; 1021 regs[tESP] = stack; 1022 regs[tEFLAGS] = PSL_USER | (regs[tEFLAGS] & PSL_T); 1023 regs[tSS] = _udatasel; 1024 regs[tDS] = _udatasel; 1025 regs[tES] = _udatasel; 1026 regs[tCS] = _ucodesel; 1027 1028 p->p_addr->u_pcb.pcb_flags = 0; /* no fp at all */ 1029 load_cr0(rcr0() | CR0_TS); /* start emulating */ 1030#if NNPX > 0 1031 npxinit(__INITIAL_NPXCW__); 1032#endif /* NNPX > 0 */ 1033} 1034 1035static int 1036sysctl_machdep_adjkerntz SYSCTL_HANDLER_ARGS 1037{ 1038 int error; 1039 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, 1040 req); 1041 if (!error && req->newptr) 1042 resettodr(); 1043 return (error); 1044} 1045 1046SYSCTL_PROC(_machdep, CPU_ADJKERNTZ, adjkerntz, 1047 CTLTYPE_INT|CTLFLAG_RW, &adjkerntz, 0, sysctl_machdep_adjkerntz, ""); 1048 1049static int 1050sysctl_machdep_consdev SYSCTL_HANDLER_ARGS 1051{ 1052 dev_t consdev; 1053 consdev = (cn_tty == NULL ? NODEV : cn_tty->t_dev); 1054 return (sysctl_handle_opaque(oidp, &consdev, sizeof consdev, req)); 1055} 1056 1057SYSCTL_PROC(_machdep, CPU_CONSDEV, consdev, 1058 CTLTYPE_OPAQUE|CTLFLAG_RD, 0, 0, sysctl_machdep_consdev, ""); 1059 1060SYSCTL_INT(_machdep, CPU_DISRTCSET, disable_rtc_set, 1061 CTLFLAG_RW, &disable_rtc_set, 0, ""); 1062 1063SYSCTL_STRUCT(_machdep, CPU_BOOTINFO, bootinfo, 1064 CTLFLAG_RD, &bootinfo, bootinfo, ""); 1065 1066/* 1067 * Initialize 386 and configure to run kernel 1068 */ 1069 1070/* 1071 * Initialize segments & interrupt table 1072 */ 1073 1074int currentldt; 1075int _default_ldt; 1076union descriptor gdt[NGDT]; /* global descriptor table */ 1077struct gate_descriptor idt[NIDT]; /* interrupt descriptor table */ 1078union descriptor ldt[NLDT]; /* local descriptor table */ 1079 1080struct i386tss tss, panic_tss; 1081 1082extern struct user *proc0paddr; 1083 1084/* software prototypes -- in more palatable form */ 1085struct soft_segment_descriptor gdt_segs[] = { 1086/* GNULL_SEL 0 Null Descriptor */ 1087{ 0x0, /* segment base address */ 1088 0x0, /* length */ 1089 0, /* segment type */ 1090 0, /* segment descriptor priority level */ 1091 0, /* segment descriptor present */ 1092 0, 0, 1093 0, /* default 32 vs 16 bit size */ 1094 0 /* limit granularity (byte/page units)*/ }, 1095/* GCODE_SEL 1 Code Descriptor for kernel */ 1096{ 0x0, /* segment base address */ 1097 0xfffff, /* length - all address space */ 1098 SDT_MEMERA, /* segment type */ 1099 0, /* segment descriptor priority level */ 1100 1, /* segment descriptor present */ 1101 0, 0, 1102 1, /* default 32 vs 16 bit size */ 1103 1 /* limit granularity (byte/page units)*/ }, 1104/* GDATA_SEL 2 Data Descriptor for kernel */ 1105{ 0x0, /* segment base address */ 1106 0xfffff, /* length - all address space */ 1107 SDT_MEMRWA, /* segment type */ 1108 0, /* segment descriptor priority level */ 1109 1, /* segment descriptor present */ 1110 0, 0, 1111 1, /* default 32 vs 16 bit size */ 1112 1 /* limit granularity (byte/page units)*/ }, 1113/* GLDT_SEL 3 LDT Descriptor */ 1114{ (int) ldt, /* segment base address */ 1115 sizeof(ldt)-1, /* length - all address space */ 1116 SDT_SYSLDT, /* segment type */ 1117 0, /* segment descriptor priority level */ 1118 1, /* segment descriptor present */ 1119 0, 0, 1120 0, /* unused - default 32 vs 16 bit size */ 1121 0 /* limit granularity (byte/page units)*/ }, 1122/* GTGATE_SEL 4 Null Descriptor - Placeholder */ 1123{ 0x0, /* segment base address */ 1124 0x0, /* length - all address space */ 1125 0, /* segment type */ 1126 0, /* segment descriptor priority level */ 1127 0, /* segment descriptor present */ 1128 0, 0, 1129 0, /* default 32 vs 16 bit size */ 1130 0 /* limit granularity (byte/page units)*/ }, 1131/* GPANIC_SEL 5 Panic Tss Descriptor */ 1132{ (int) &panic_tss, /* segment base address */ 1133 sizeof(tss)-1, /* length - all address space */ 1134 SDT_SYS386TSS, /* segment type */ 1135 0, /* segment descriptor priority level */ 1136 1, /* segment descriptor present */ 1137 0, 0, 1138 0, /* unused - default 32 vs 16 bit size */ 1139 0 /* limit granularity (byte/page units)*/ }, 1140/* GPROC0_SEL 6 Proc 0 Tss Descriptor */ 1141{ (int) kstack, /* segment base address */ 1142 sizeof(tss)-1, /* length - all address space */ 1143 SDT_SYS386TSS, /* segment type */ 1144 0, /* segment descriptor priority level */ 1145 1, /* segment descriptor present */ 1146 0, 0, 1147 0, /* unused - default 32 vs 16 bit size */ 1148 0 /* limit granularity (byte/page units)*/ }, 1149/* GUSERLDT_SEL 7 User LDT Descriptor per process */ 1150{ (int) ldt, /* segment base address */ 1151 (512 * sizeof(union descriptor)-1), /* length */ 1152 SDT_SYSLDT, /* segment type */ 1153 0, /* segment descriptor priority level */ 1154 1, /* segment descriptor present */ 1155 0, 0, 1156 0, /* unused - default 32 vs 16 bit size */ 1157 0 /* limit granularity (byte/page units)*/ }, 1158/* GAPMCODE32_SEL 8 APM BIOS 32-bit interface (32bit Code) */ 1159{ 0, /* segment base address (overwritten by APM) */ 1160 0xfffff, /* length */ 1161 SDT_MEMERA, /* segment type */ 1162 0, /* segment descriptor priority level */ 1163 1, /* segment descriptor present */ 1164 0, 0, 1165 1, /* default 32 vs 16 bit size */ 1166 1 /* limit granularity (byte/page units)*/ }, 1167/* GAPMCODE16_SEL 9 APM BIOS 32-bit interface (16bit Code) */ 1168{ 0, /* segment base address (overwritten by APM) */ 1169 0xfffff, /* length */ 1170 SDT_MEMERA, /* segment type */ 1171 0, /* segment descriptor priority level */ 1172 1, /* segment descriptor present */ 1173 0, 0, 1174 0, /* default 32 vs 16 bit size */ 1175 1 /* limit granularity (byte/page units)*/ }, 1176/* GAPMDATA_SEL 10 APM BIOS 32-bit interface (Data) */ 1177{ 0, /* segment base address (overwritten by APM) */ 1178 0xfffff, /* length */ 1179 SDT_MEMRWA, /* segment type */ 1180 0, /* segment descriptor priority level */ 1181 1, /* segment descriptor present */ 1182 0, 0, 1183 1, /* default 32 vs 16 bit size */ 1184 1 /* limit granularity (byte/page units)*/ }, 1185}; 1186 1187struct soft_segment_descriptor ldt_segs[] = { 1188 /* Null Descriptor - overwritten by call gate */ 1189{ 0x0, /* segment base address */ 1190 0x0, /* length - all address space */ 1191 0, /* segment type */ 1192 0, /* segment descriptor priority level */ 1193 0, /* segment descriptor present */ 1194 0, 0, 1195 0, /* default 32 vs 16 bit size */ 1196 0 /* limit granularity (byte/page units)*/ }, 1197 /* Null Descriptor - overwritten by call gate */ 1198{ 0x0, /* segment base address */ 1199 0x0, /* length - all address space */ 1200 0, /* segment type */ 1201 0, /* segment descriptor priority level */ 1202 0, /* segment descriptor present */ 1203 0, 0, 1204 0, /* default 32 vs 16 bit size */ 1205 0 /* limit granularity (byte/page units)*/ }, 1206 /* Null Descriptor - overwritten by call gate */ 1207{ 0x0, /* segment base address */ 1208 0x0, /* length - all address space */ 1209 0, /* segment type */ 1210 0, /* segment descriptor priority level */ 1211 0, /* segment descriptor present */ 1212 0, 0, 1213 0, /* default 32 vs 16 bit size */ 1214 0 /* limit granularity (byte/page units)*/ }, 1215 /* Code Descriptor for user */ 1216{ 0x0, /* segment base address */ 1217 0xfffff, /* length - all address space */ 1218 SDT_MEMERA, /* segment type */ 1219 SEL_UPL, /* segment descriptor priority level */ 1220 1, /* segment descriptor present */ 1221 0, 0, 1222 1, /* default 32 vs 16 bit size */ 1223 1 /* limit granularity (byte/page units)*/ }, 1224 /* Data Descriptor for user */ 1225{ 0x0, /* segment base address */ 1226 0xfffff, /* length - all address space */ 1227 SDT_MEMRWA, /* segment type */ 1228 SEL_UPL, /* segment descriptor priority level */ 1229 1, /* segment descriptor present */ 1230 0, 0, 1231 1, /* default 32 vs 16 bit size */ 1232 1 /* limit granularity (byte/page units)*/ }, 1233}; 1234 1235void 1236setidt(idx, func, typ, dpl) 1237 int idx; 1238 inthand_t *func; 1239 int typ; 1240 int dpl; 1241{ 1242 struct gate_descriptor *ip = idt + idx; 1243 1244 ip->gd_looffset = (int)func; 1245 ip->gd_selector = 8; 1246 ip->gd_stkcpy = 0; 1247 ip->gd_xx = 0; 1248 ip->gd_type = typ; 1249 ip->gd_dpl = dpl; 1250 ip->gd_p = 1; 1251 ip->gd_hioffset = ((int)func)>>16 ; 1252} 1253 1254#define IDTVEC(name) __CONCAT(X,name) 1255 1256extern inthand_t 1257 IDTVEC(div), IDTVEC(dbg), IDTVEC(nmi), IDTVEC(bpt), IDTVEC(ofl), 1258 IDTVEC(bnd), IDTVEC(ill), IDTVEC(dna), IDTVEC(dble), IDTVEC(fpusegm), 1259 IDTVEC(tss), IDTVEC(missing), IDTVEC(stk), IDTVEC(prot), 1260 IDTVEC(page), IDTVEC(rsvd), IDTVEC(fpu), IDTVEC(align), 1261 IDTVEC(syscall); 1262 1263#ifdef COMPAT_LINUX 1264extern inthand_t 1265 IDTVEC(linux_syscall); 1266#endif 1267 1268void 1269sdtossd(sd, ssd) 1270 struct segment_descriptor *sd; 1271 struct soft_segment_descriptor *ssd; 1272{ 1273 ssd->ssd_base = (sd->sd_hibase << 24) | sd->sd_lobase; 1274 ssd->ssd_limit = (sd->sd_hilimit << 16) | sd->sd_lolimit; 1275 ssd->ssd_type = sd->sd_type; 1276 ssd->ssd_dpl = sd->sd_dpl; 1277 ssd->ssd_p = sd->sd_p; 1278 ssd->ssd_def32 = sd->sd_def32; 1279 ssd->ssd_gran = sd->sd_gran; 1280} 1281 1282void 1283init386(first) 1284 int first; 1285{ 1286 int x; 1287 unsigned biosbasemem, biosextmem; 1288 struct gate_descriptor *gdp; 1289 int gsel_tss; 1290 /* table descriptors - used to load tables by microp */ 1291 struct region_descriptor r_gdt, r_idt; 1292 int pagesinbase, pagesinext; 1293 int target_page, pa_indx; 1294 1295 proc0.p_addr = proc0paddr; 1296 1297 /* 1298 * Initialize the console before we print anything out. 1299 */ 1300 cninit(); 1301 1302 /* 1303 * make gdt memory segments, the code segment goes up to end of the 1304 * page with etext in it, the data segment goes to the end of 1305 * the address space 1306 */ 1307 /* 1308 * XXX text protection is temporarily (?) disabled. The limit was 1309 * i386_btop(i386_round_page(etext)) - 1. 1310 */ 1311 gdt_segs[GCODE_SEL].ssd_limit = i386_btop(0) - 1; 1312 gdt_segs[GDATA_SEL].ssd_limit = i386_btop(0) - 1; 1313 for (x = 0; x < NGDT; x++) 1314 ssdtosd(&gdt_segs[x], &gdt[x].sd); 1315 1316 /* make ldt memory segments */ 1317 /* 1318 * The data segment limit must not cover the user area because we 1319 * don't want the user area to be writable in copyout() etc. (page 1320 * level protection is lost in kernel mode on 386's). Also, we 1321 * don't want the user area to be writable directly (page level 1322 * protection of the user area is not available on 486's with 1323 * CR0_WP set, because there is no user-read/kernel-write mode). 1324 * 1325 * XXX - VM_MAXUSER_ADDRESS is an end address, not a max. And it 1326 * should be spelled ...MAX_USER... 1327 */ 1328#define VM_END_USER_RW_ADDRESS VM_MAXUSER_ADDRESS 1329 /* 1330 * The code segment limit has to cover the user area until we move 1331 * the signal trampoline out of the user area. This is safe because 1332 * the code segment cannot be written to directly. 1333 */ 1334#define VM_END_USER_R_ADDRESS (VM_END_USER_RW_ADDRESS + UPAGES * NBPG) 1335 ldt_segs[LUCODE_SEL].ssd_limit = i386_btop(VM_END_USER_R_ADDRESS) - 1; 1336 ldt_segs[LUDATA_SEL].ssd_limit = i386_btop(VM_END_USER_RW_ADDRESS) - 1; 1337 /* Note. eventually want private ldts per process */ 1338 for (x = 0; x < NLDT; x++) 1339 ssdtosd(&ldt_segs[x], &ldt[x].sd); 1340 1341 /* exceptions */ 1342 for (x = 0; x < NIDT; x++) 1343 setidt(x, &IDTVEC(rsvd), SDT_SYS386TGT, SEL_KPL); 1344 setidt(0, &IDTVEC(div), SDT_SYS386TGT, SEL_KPL); 1345 setidt(1, &IDTVEC(dbg), SDT_SYS386TGT, SEL_KPL); 1346 setidt(2, &IDTVEC(nmi), SDT_SYS386TGT, SEL_KPL); 1347 setidt(3, &IDTVEC(bpt), SDT_SYS386TGT, SEL_UPL); 1348 setidt(4, &IDTVEC(ofl), SDT_SYS386TGT, SEL_UPL); 1349 setidt(5, &IDTVEC(bnd), SDT_SYS386TGT, SEL_KPL); 1350 setidt(6, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL); 1351 setidt(7, &IDTVEC(dna), SDT_SYS386TGT, SEL_KPL); 1352 setidt(8, &IDTVEC(dble), SDT_SYS386TGT, SEL_KPL); 1353 setidt(9, &IDTVEC(fpusegm), SDT_SYS386TGT, SEL_KPL); 1354 setidt(10, &IDTVEC(tss), SDT_SYS386TGT, SEL_KPL); 1355 setidt(11, &IDTVEC(missing), SDT_SYS386TGT, SEL_KPL); 1356 setidt(12, &IDTVEC(stk), SDT_SYS386TGT, SEL_KPL); 1357 setidt(13, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL); 1358 setidt(14, &IDTVEC(page), SDT_SYS386TGT, SEL_KPL); 1359 setidt(15, &IDTVEC(rsvd), SDT_SYS386TGT, SEL_KPL); 1360 setidt(16, &IDTVEC(fpu), SDT_SYS386TGT, SEL_KPL); 1361 setidt(17, &IDTVEC(align), SDT_SYS386TGT, SEL_KPL); 1362#ifdef COMPAT_LINUX 1363 setidt(0x80, &IDTVEC(linux_syscall), SDT_SYS386TGT, SEL_UPL); 1364#endif 1365 1366#include "isa.h" 1367#if NISA >0 1368 isa_defaultirq(); 1369#endif 1370 rand_initialize(); 1371 1372 r_gdt.rd_limit = sizeof(gdt) - 1; 1373 r_gdt.rd_base = (int) gdt; 1374 lgdt(&r_gdt); 1375 1376 r_idt.rd_limit = sizeof(idt) - 1; 1377 r_idt.rd_base = (int) idt; 1378 lidt(&r_idt); 1379 1380 _default_ldt = GSEL(GLDT_SEL, SEL_KPL); 1381 lldt(_default_ldt); 1382 currentldt = _default_ldt; 1383 1384#ifdef DDB 1385 kdb_init(); 1386 if (boothowto & RB_KDB) 1387 Debugger("Boot flags requested debugger"); 1388#endif 1389 1390 /* Use BIOS values stored in RTC CMOS RAM, since probing 1391 * breaks certain 386 AT relics. 1392 */ 1393 biosbasemem = rtcin(RTC_BASELO)+ (rtcin(RTC_BASEHI)<<8); 1394 biosextmem = rtcin(RTC_EXTLO)+ (rtcin(RTC_EXTHI)<<8); 1395 1396 /* 1397 * Print a warning if the official BIOS interface disagrees 1398 * with the hackish interface used above. Eventually only 1399 * the official interface should be used. 1400 */ 1401 if (bootinfo.bi_memsizes_valid) { 1402 if (bootinfo.bi_basemem != biosbasemem) 1403 printf("BIOS basemem (%ldK) != RTC basemem (%dK)\n", 1404 bootinfo.bi_basemem, biosbasemem); 1405 if (bootinfo.bi_extmem != biosextmem) 1406 printf("BIOS extmem (%ldK) != RTC extmem (%dK)\n", 1407 bootinfo.bi_extmem, biosextmem); 1408 } 1409 1410 /* 1411 * If BIOS tells us that it has more than 640k in the basemem, 1412 * don't believe it - set it to 640k. 1413 */ 1414 if (biosbasemem > 640) 1415 biosbasemem = 640; 1416 1417 /* 1418 * Some 386 machines might give us a bogus number for extended 1419 * mem. If this happens, stop now. 1420 */ 1421#ifndef LARGEMEM 1422 if (biosextmem > 65536) { 1423 panic("extended memory beyond limit of 64MB"); 1424 /* NOTREACHED */ 1425 } 1426#endif 1427 1428 pagesinbase = biosbasemem * 1024 / NBPG; 1429 pagesinext = biosextmem * 1024 / NBPG; 1430 1431 /* 1432 * Special hack for chipsets that still remap the 384k hole when 1433 * there's 16MB of memory - this really confuses people that 1434 * are trying to use bus mastering ISA controllers with the 1435 * "16MB limit"; they only have 16MB, but the remapping puts 1436 * them beyond the limit. 1437 */ 1438 /* 1439 * If extended memory is between 15-16MB (16-17MB phys address range), 1440 * chop it to 15MB. 1441 */ 1442 if ((pagesinext > 3840) && (pagesinext < 4096)) 1443 pagesinext = 3840; 1444 1445 /* 1446 * Maxmem isn't the "maximum memory", it's one larger than the 1447 * highest page of of the physical address space. It 1448 */ 1449 Maxmem = pagesinext + 0x100000/PAGE_SIZE; 1450 1451#ifdef MAXMEM 1452 Maxmem = MAXMEM/4; 1453#endif 1454 1455 /* call pmap initialization to make new kernel address space */ 1456 pmap_bootstrap (first, 0); 1457 1458 /* 1459 * Size up each available chunk of physical memory. 1460 */ 1461 1462 /* 1463 * We currently don't bother testing base memory. 1464 * XXX ...but we probably should. 1465 */ 1466 pa_indx = 0; 1467 badpages = 0; 1468 if (pagesinbase > 1) { 1469 phys_avail[pa_indx++] = PAGE_SIZE; /* skip first page of memory */ 1470 phys_avail[pa_indx] = ptoa(pagesinbase);/* memory up to the ISA hole */ 1471 physmem = pagesinbase - 1; 1472 } else { 1473 /* point at first chunk end */ 1474 pa_indx++; 1475 } 1476 1477 for (target_page = avail_start; target_page < ptoa(Maxmem); target_page += PAGE_SIZE) { 1478 int tmp, page_bad = FALSE; 1479 1480 /* 1481 * map page into kernel: valid, read/write, non-cacheable 1482 */ 1483 *(int *)CMAP1 = PG_V | PG_KW | PG_N | target_page; 1484 pmap_update(); 1485 1486 tmp = *(int *)CADDR1; 1487 /* 1488 * Test for alternating 1's and 0's 1489 */ 1490 *(int *)CADDR1 = 0xaaaaaaaa; 1491 if (*(int *)CADDR1 != 0xaaaaaaaa) { 1492 page_bad = TRUE; 1493 } 1494 /* 1495 * Test for alternating 0's and 1's 1496 */ 1497 *(int *)CADDR1 = 0x55555555; 1498 if (*(int *)CADDR1 != 0x55555555) { 1499 page_bad = TRUE; 1500 } 1501 /* 1502 * Test for all 1's 1503 */ 1504 *(int *)CADDR1 = 0xffffffff; 1505 if (*(int *)CADDR1 != 0xffffffff) { 1506 page_bad = TRUE; 1507 } 1508 /* 1509 * Test for all 0's 1510 */ 1511 *(int *)CADDR1 = 0x0; 1512 if (*(int *)CADDR1 != 0x0) { 1513 /* 1514 * test of page failed 1515 */ 1516 page_bad = TRUE; 1517 } 1518 /* 1519 * Restore original value. 1520 */ 1521 *(int *)CADDR1 = tmp; 1522 1523 /* 1524 * Adjust array of valid/good pages. 1525 */ 1526 if (page_bad == FALSE) { 1527 /* 1528 * If this good page is a continuation of the 1529 * previous set of good pages, then just increase 1530 * the end pointer. Otherwise start a new chunk. 1531 * Note that "end" points one higher than end, 1532 * making the range >= start and < end. 1533 */ 1534 if (phys_avail[pa_indx] == target_page) { 1535 phys_avail[pa_indx] += PAGE_SIZE; 1536 } else { 1537 pa_indx++; 1538 if (pa_indx == PHYS_AVAIL_ARRAY_END) { 1539 printf("Too many holes in the physical address space, giving up\n"); 1540 pa_indx--; 1541 break; 1542 } 1543 phys_avail[pa_indx++] = target_page; /* start */ 1544 phys_avail[pa_indx] = target_page + PAGE_SIZE; /* end */ 1545 } 1546 physmem++; 1547 } else { 1548 badpages++; 1549 page_bad = FALSE; 1550 } 1551 } 1552 1553 *(int *)CMAP1 = 0; 1554 pmap_update(); 1555 1556 /* 1557 * XXX 1558 * The last chunk must contain at least one page plus the message 1559 * buffer to avoid complicating other code (message buffer address 1560 * calculation, etc.). 1561 */ 1562 while (phys_avail[pa_indx - 1] + PAGE_SIZE + 1563 round_page(sizeof(struct msgbuf)) >= phys_avail[pa_indx]) { 1564 physmem -= atop(phys_avail[pa_indx] - phys_avail[pa_indx - 1]); 1565 phys_avail[pa_indx--] = 0; 1566 phys_avail[pa_indx--] = 0; 1567 } 1568 1569 Maxmem = atop(phys_avail[pa_indx]); 1570 1571 /* Trim off space for the message buffer. */ 1572 phys_avail[pa_indx] -= round_page(sizeof(struct msgbuf)); 1573 1574 avail_end = phys_avail[pa_indx]; 1575 1576 /* now running on new page tables, configured,and u/iom is accessible */ 1577 1578 /* make a initial tss so microp can get interrupt stack on syscall! */ 1579 proc0.p_addr->u_pcb.pcb_tss.tss_esp0 = (int) kstack + UPAGES*NBPG; 1580 proc0.p_addr->u_pcb.pcb_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL) ; 1581 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL); 1582 1583 ((struct i386tss *)gdt_segs[GPROC0_SEL].ssd_base)->tss_ioopt = 1584 (sizeof(tss))<<16; 1585 1586 ltr(gsel_tss); 1587 1588 /* make a call gate to reenter kernel with */ 1589 gdp = &ldt[LSYS5CALLS_SEL].gd; 1590 1591 x = (int) &IDTVEC(syscall); 1592 gdp->gd_looffset = x++; 1593 gdp->gd_selector = GSEL(GCODE_SEL,SEL_KPL); 1594 gdp->gd_stkcpy = 1; 1595 gdp->gd_type = SDT_SYS386CGT; 1596 gdp->gd_dpl = SEL_UPL; 1597 gdp->gd_p = 1; 1598 gdp->gd_hioffset = ((int) &IDTVEC(syscall)) >>16; 1599 1600 /* transfer to user mode */ 1601 1602 _ucodesel = LSEL(LUCODE_SEL, SEL_UPL); 1603 _udatasel = LSEL(LUDATA_SEL, SEL_UPL); 1604 1605 /* setup proc 0's pcb */ 1606 bcopy(&sigcode, proc0.p_addr->u_pcb.pcb_sigc, szsigcode); 1607 proc0.p_addr->u_pcb.pcb_flags = 0; 1608 proc0.p_addr->u_pcb.pcb_ptd = IdlePTD; 1609} 1610 1611/* 1612 * The registers are in the frame; the frame is in the user area of 1613 * the process in question; when the process is active, the registers 1614 * are in "the kernel stack"; when it's not, they're still there, but 1615 * things get flipped around. So, since p->p_md.md_regs is the whole address 1616 * of the register set, take its offset from the kernel stack, and 1617 * index into the user block. Don't you just *love* virtual memory? 1618 * (I'm starting to think seymour is right...) 1619 */ 1620#define TF_REGP(p) ((struct trapframe *) \ 1621 ((char *)(p)->p_addr \ 1622 + ((char *)(p)->p_md.md_regs - kstack))) 1623 1624int 1625ptrace_set_pc(p, addr) 1626 struct proc *p; 1627 unsigned int addr; 1628{ 1629 TF_REGP(p)->tf_eip = addr; 1630 return (0); 1631} 1632 1633int 1634ptrace_single_step(p) 1635 struct proc *p; 1636{ 1637 TF_REGP(p)->tf_eflags |= PSL_T; 1638 return (0); 1639} 1640 1641int 1642ptrace_getregs(p, addr) 1643 struct proc *p; 1644 unsigned int *addr; 1645{ 1646 int error; 1647 struct reg regs; 1648 1649 error = fill_regs(p, ®s); 1650 if (error) 1651 return (error); 1652 return (copyout(®s, addr, sizeof regs)); 1653} 1654 1655int 1656ptrace_setregs(p, addr) 1657 struct proc *p; 1658 unsigned int *addr; 1659{ 1660 int error; 1661 struct reg regs; 1662 1663 error = copyin(addr, ®s, sizeof regs); 1664 if (error) 1665 return (error); 1666 return (set_regs(p, ®s)); 1667} 1668 1669int ptrace_write_u(p, off, data) 1670 struct proc *p; 1671 vm_offset_t off; 1672 int data; 1673{ 1674 struct trapframe frame_copy; 1675 vm_offset_t min; 1676 struct trapframe *tp; 1677 1678 /* 1679 * Privileged kernel state is scattered all over the user area. 1680 * Only allow write access to parts of regs and to fpregs. 1681 */ 1682 min = (char *)p->p_md.md_regs - kstack; 1683 if (off >= min && off <= min + sizeof(struct trapframe) - sizeof(int)) { 1684 tp = TF_REGP(p); 1685 frame_copy = *tp; 1686 *(int *)((char *)&frame_copy + (off - min)) = data; 1687 if (!EFLAGS_SECURE(frame_copy.tf_eflags, tp->tf_eflags) || 1688 !CS_SECURE(frame_copy.tf_cs)) 1689 return (EINVAL); 1690 *(int*)((char *)p->p_addr + off) = data; 1691 return (0); 1692 } 1693 min = offsetof(struct user, u_pcb) + offsetof(struct pcb, pcb_savefpu); 1694 if (off >= min && off <= min + sizeof(struct save87) - sizeof(int)) { 1695 *(int*)((char *)p->p_addr + off) = data; 1696 return (0); 1697 } 1698 return (EFAULT); 1699} 1700 1701int 1702fill_regs(p, regs) 1703 struct proc *p; 1704 struct reg *regs; 1705{ 1706 struct trapframe *tp; 1707 1708 tp = TF_REGP(p); 1709 regs->r_es = tp->tf_es; 1710 regs->r_ds = tp->tf_ds; 1711 regs->r_edi = tp->tf_edi; 1712 regs->r_esi = tp->tf_esi; 1713 regs->r_ebp = tp->tf_ebp; 1714 regs->r_ebx = tp->tf_ebx; 1715 regs->r_edx = tp->tf_edx; 1716 regs->r_ecx = tp->tf_ecx; 1717 regs->r_eax = tp->tf_eax; 1718 regs->r_eip = tp->tf_eip; 1719 regs->r_cs = tp->tf_cs; 1720 regs->r_eflags = tp->tf_eflags; 1721 regs->r_esp = tp->tf_esp; 1722 regs->r_ss = tp->tf_ss; 1723 return (0); 1724} 1725 1726int 1727set_regs(p, regs) 1728 struct proc *p; 1729 struct reg *regs; 1730{ 1731 struct trapframe *tp; 1732 1733 tp = TF_REGP(p); 1734 if (!EFLAGS_SECURE(regs->r_eflags, tp->tf_eflags) || 1735 !CS_SECURE(regs->r_cs)) 1736 return (EINVAL); 1737 tp->tf_es = regs->r_es; 1738 tp->tf_ds = regs->r_ds; 1739 tp->tf_edi = regs->r_edi; 1740 tp->tf_esi = regs->r_esi; 1741 tp->tf_ebp = regs->r_ebp; 1742 tp->tf_ebx = regs->r_ebx; 1743 tp->tf_edx = regs->r_edx; 1744 tp->tf_ecx = regs->r_ecx; 1745 tp->tf_eax = regs->r_eax; 1746 tp->tf_eip = regs->r_eip; 1747 tp->tf_cs = regs->r_cs; 1748 tp->tf_eflags = regs->r_eflags; 1749 tp->tf_esp = regs->r_esp; 1750 tp->tf_ss = regs->r_ss; 1751 return (0); 1752} 1753 1754#ifndef DDB 1755void 1756Debugger(const char *msg) 1757{ 1758 printf("Debugger(\"%s\") called.\n", msg); 1759} 1760#endif /* no DDB */ 1761 1762#include <sys/disklabel.h> 1763#define b_cylin b_resid 1764/* 1765 * Determine the size of the transfer, and make sure it is 1766 * within the boundaries of the partition. Adjust transfer 1767 * if needed, and signal errors or early completion. 1768 */ 1769int 1770bounds_check_with_label(struct buf *bp, struct disklabel *lp, int wlabel) 1771{ 1772 struct partition *p = lp->d_partitions + dkpart(bp->b_dev); 1773 int labelsect = lp->d_partitions[0].p_offset; 1774 int maxsz = p->p_size, 1775 sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT; 1776 1777 /* overwriting disk label ? */ 1778 /* XXX should also protect bootstrap in first 8K */ 1779 if (bp->b_blkno + p->p_offset <= LABELSECTOR + labelsect && 1780#if LABELSECTOR != 0 1781 bp->b_blkno + p->p_offset + sz > LABELSECTOR + labelsect && 1782#endif 1783 (bp->b_flags & B_READ) == 0 && wlabel == 0) { 1784 bp->b_error = EROFS; 1785 goto bad; 1786 } 1787 1788#if defined(DOSBBSECTOR) && defined(notyet) 1789 /* overwriting master boot record? */ 1790 if (bp->b_blkno + p->p_offset <= DOSBBSECTOR && 1791 (bp->b_flags & B_READ) == 0 && wlabel == 0) { 1792 bp->b_error = EROFS; 1793 goto bad; 1794 } 1795#endif 1796 1797 /* beyond partition? */ 1798 if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) { 1799 /* if exactly at end of disk, return an EOF */ 1800 if (bp->b_blkno == maxsz) { 1801 bp->b_resid = bp->b_bcount; 1802 return(0); 1803 } 1804 /* or truncate if part of it fits */ 1805 sz = maxsz - bp->b_blkno; 1806 if (sz <= 0) { 1807 bp->b_error = EINVAL; 1808 goto bad; 1809 } 1810 bp->b_bcount = sz << DEV_BSHIFT; 1811 } 1812 1813 /* calculate cylinder for disksort to order transfers with */ 1814 bp->b_pblkno = bp->b_blkno + p->p_offset; 1815 bp->b_cylin = bp->b_pblkno / lp->d_secpercyl; 1816 return(1); 1817 1818bad: 1819 bp->b_flags |= B_ERROR; 1820 return(-1); 1821} 1822 1823int 1824disk_externalize(int drive, struct sysctl_req *req) 1825{ 1826 return SYSCTL_OUT(req, &drive, sizeof drive); 1827} 1828