1 /*- 2 * Copyright (c) 1982, 1987, 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * William Jolitz. 7 * 8 * %sccs.include.redist.c% 9 * 10 * @(#)machdep.c 7.14 (Berkeley) 07/10/92 11 */ 12 13 #include "param.h" 14 #include "systm.h" 15 #include "signalvar.h" 16 #include "kernel.h" 17 #include "map.h" 18 #include "proc.h" 19 #include "user.h" 20 #include "buf.h" 21 #include "reboot.h" 22 #include "conf.h" 23 #include "file.h" 24 #include "clist.h" 25 #include "callout.h" 26 #include "malloc.h" 27 #include "mbuf.h" 28 #include "msgbuf.h" 29 #include "net/netisr.h" 30 31 #include "vm/vm.h" 32 #include "vm/vm_kern.h" 33 #include "vm/vm_page.h" 34 35 vm_map_t buffer_map; 36 extern vm_offset_t avail_end; 37 38 #include "machine/cpu.h" 39 #include "machine/reg.h" 40 #include "machine/psl.h" 41 #include "machine/specialreg.h" 42 #include "i386/isa/rtc.h" 43 44 /* 45 * Declare these as initialized data so we can patch them. 46 */ 47 int nswbuf = 0; 48 #ifdef NBUF 49 int nbuf = NBUF; 50 #else 51 int nbuf = 0; 52 #endif 53 #ifdef BUFPAGES 54 int bufpages = BUFPAGES; 55 #else 56 int bufpages = 0; 57 #endif 58 int msgbufmapped; /* set when safe to use msgbuf */ 59 60 /* 61 * Machine-dependent startup code 62 */ 63 int boothowto = 0, Maxmem = 0; 64 long dumplo; 65 int physmem, maxmem; 66 extern int bootdev; 67 #ifdef SMALL 68 extern int forcemaxmem; 69 #endif 70 int biosmem; 71 72 extern cyloffset; 73 74 cpu_startup(firstaddr) 75 int firstaddr; 76 { 77 register int unixsize; 78 register unsigned i; 79 register struct pte *pte; 80 int mapaddr, j; 81 register caddr_t v; 82 int maxbufs, base, residual; 83 extern long Usrptsize; 84 vm_offset_t minaddr, maxaddr; 85 vm_size_t size; 86 87 /* 88 * Initialize error message buffer (at end of core). 89 */ 90 91 /* avail_end was pre-decremented in pmap_bootstrap to compensate */ 92 for (i = 0; i < btoc(sizeof (struct msgbuf)); i++) 93 pmap_enter(kernel_pmap, msgbufp, avail_end + i * NBPG, 94 VM_PROT_ALL, TRUE); 95 msgbufmapped = 1; 96 97 #ifdef KDB 98 kdb_init(); /* startup kernel debugger */ 99 #endif 100 /* 101 * Good {morning,afternoon,evening,night}. 102 */ 103 printf(version); 104 printf("real mem = %d\n", ctob(physmem)); 105 106 /* 107 * Allocate space for system data structures. 108 * The first available real memory address is in "firstaddr". 109 * The first available kernel virtual address is in "v". 110 * As pages of kernel virtual memory are allocated, "v" is incremented. 111 * As pages of memory are allocated and cleared, 112 * "firstaddr" is incremented. 113 * An index into the kernel page table corresponding to the 114 * virtual memory address maintained in "v" is kept in "mapaddr". 115 */ 116 117 /* 118 * Make two passes. The first pass calculates how much memory is 119 * needed and allocates it. The second pass assigns virtual 120 * addresses to the various data structures. 121 */ 122 firstaddr = 0; 123 again: 124 v = (caddr_t)firstaddr; 125 126 #define valloc(name, type, num) \ 127 (name) = (type *)v; v = (caddr_t)((name)+(num)) 128 #define valloclim(name, type, num, lim) \ 129 (name) = (type *)v; v = (caddr_t)((lim) = ((name)+(num))) 130 valloc(cfree, struct cblock, nclist); 131 valloc(callout, struct callout, ncallout); 132 valloc(swapmap, struct map, nswapmap = maxproc * 2); 133 #ifdef SYSVSHM 134 valloc(shmsegs, struct shmid_ds, shminfo.shmmni); 135 #endif 136 /* 137 * Determine how many buffers to allocate. 138 * Use 10% of memory for the first 2 Meg, 5% of the remaining 139 * memory. Insure a minimum of 16 buffers. 140 * We allocate 1/2 as many swap buffer headers as file i/o buffers. 141 */ 142 if (bufpages == 0) 143 if (physmem < (2 * 1024 * 1024)) 144 bufpages = physmem / 10 / CLSIZE; 145 else 146 bufpages = ((2 * 1024 * 1024 + physmem) / 20) / CLSIZE; 147 if (nbuf == 0) { 148 nbuf = bufpages / 2; 149 if (nbuf < 16) 150 nbuf = 16; 151 } 152 if (nswbuf == 0) { 153 nswbuf = (nbuf / 2) &~ 1; /* force even */ 154 if (nswbuf > 256) 155 nswbuf = 256; /* sanity */ 156 } 157 valloc(swbuf, struct buf, nswbuf); 158 valloc(buf, struct buf, nbuf); 159 160 /* 161 * End of first pass, size has been calculated so allocate memory 162 */ 163 if (firstaddr == 0) { 164 size = (vm_size_t)(v - firstaddr); 165 firstaddr = (int)kmem_alloc(kernel_map, round_page(size)); 166 if (firstaddr == 0) 167 panic("startup: no room for tables"); 168 goto again; 169 } 170 /* 171 * End of second pass, addresses have been assigned 172 */ 173 if ((vm_size_t)(v - firstaddr) != size) 174 panic("startup: table size inconsistency"); 175 /* 176 * Now allocate buffers proper. They are different than the above 177 * in that they usually occupy more virtual memory than physical. 178 */ 179 size = MAXBSIZE * nbuf; 180 buffer_map = kmem_suballoc(kernel_map, (vm_offset_t)&buffers, 181 &maxaddr, size, FALSE); 182 minaddr = (vm_offset_t)buffers; 183 if (vm_map_find(buffer_map, vm_object_allocate(size), (vm_offset_t)0, 184 &minaddr, size, FALSE) != KERN_SUCCESS) 185 panic("startup: cannot allocate buffers"); 186 base = bufpages / nbuf; 187 residual = bufpages % nbuf; 188 for (i = 0; i < nbuf; i++) { 189 vm_size_t curbufsize; 190 vm_offset_t curbuf; 191 192 /* 193 * First <residual> buffers get (base+1) physical pages 194 * allocated for them. The rest get (base) physical pages. 195 * 196 * The rest of each buffer occupies virtual space, 197 * but has no physical memory allocated for it. 198 */ 199 curbuf = (vm_offset_t)buffers + i * MAXBSIZE; 200 curbufsize = CLBYTES * (i < residual ? base+1 : base); 201 vm_map_pageable(buffer_map, curbuf, curbuf+curbufsize, FALSE); 202 vm_map_simplify(buffer_map, curbuf); 203 } 204 /* 205 * Allocate a submap for exec arguments. This map effectively 206 * limits the number of processes exec'ing at any time. 207 */ 208 exec_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr, 209 16*NCARGS, TRUE); 210 /* 211 * Allocate a submap for physio 212 */ 213 phys_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr, 214 VM_PHYS_SIZE, TRUE); 215 216 /* 217 * Finally, allocate mbuf pool. Since mclrefcnt is an off-size 218 * we use the more space efficient malloc in place of kmem_alloc. 219 */ 220 mclrefcnt = (char *)malloc(NMBCLUSTERS+CLBYTES/MCLBYTES, 221 M_MBUF, M_NOWAIT); 222 bzero(mclrefcnt, NMBCLUSTERS+CLBYTES/MCLBYTES); 223 mb_map = kmem_suballoc(kernel_map, (vm_offset_t)&mbutl, &maxaddr, 224 VM_MBUF_SIZE, FALSE); 225 /* 226 * Initialize callouts 227 */ 228 callfree = callout; 229 for (i = 1; i < ncallout; i++) 230 callout[i-1].c_next = &callout[i]; 231 callout[i-1].c_next = NULL; 232 233 /*printf("avail mem = %d\n", ptoa(vm_page_free_count));*/ 234 printf("using %d buffers containing %d bytes of memory\n", 235 nbuf, bufpages * CLBYTES); 236 237 /* 238 * Set up CPU-specific registers, cache, etc. 239 */ 240 initcpu(); 241 242 /* 243 * Set up buffers, so they can be used to read disk labels. 244 */ 245 bufinit(); 246 247 /* 248 * Configure the system. 249 */ 250 configure(); 251 } 252 253 #ifdef PGINPROF 254 /* 255 * Return the difference (in microseconds) 256 * between the current time and a previous 257 * time as represented by the arguments. 258 * If there is a pending clock interrupt 259 * which has not been serviced due to high 260 * ipl, return error code. 261 */ 262 /*ARGSUSED*/ 263 vmtime(otime, olbolt, oicr) 264 register int otime, olbolt, oicr; 265 { 266 267 return (((time.tv_sec-otime)*60 + lbolt-olbolt)*16667); 268 } 269 #endif 270 271 struct sigframe { 272 int sf_signum; 273 int sf_code; 274 struct sigcontext *sf_scp; 275 sig_t sf_handler; 276 int sf_eax; 277 int sf_edx; 278 int sf_ecx; 279 struct sigcontext sf_sc; 280 } ; 281 282 extern int kstack[]; 283 284 /* 285 * Send an interrupt to process. 286 * 287 * Stack is set up to allow sigcode stored 288 * in u. to call routine, followed by kcall 289 * to sigreturn routine below. After sigreturn 290 * resets the signal mask, the stack, and the 291 * frame pointer, it returns to the user 292 * specified pc, psl. 293 */ 294 void 295 sendsig(catcher, sig, mask, code) 296 sig_t catcher; 297 int sig, mask; 298 unsigned code; 299 { 300 register struct proc *p = curproc; 301 register int *regs; 302 register struct sigframe *fp; 303 struct sigacts *psp = p->p_sigacts; 304 int oonstack, frmtrap; 305 306 regs = p->p_md.md_regs; 307 oonstack = psp->ps_sigstk.ss_flags & SA_ONSTACK; 308 frmtrap = curpcb->pcb_flags & FM_TRAP; 309 /* 310 * Allocate and validate space for the signal handler 311 * context. Note that if the stack is in P0 space, the 312 * call to grow() is a nop, and the useracc() check 313 * will fail if the process has not already allocated 314 * the space with a `brk'. 315 */ 316 if ((psp->ps_flags & SAS_ALTSTACK) && 317 (psp->ps_sigstk.ss_flags & SA_ONSTACK) == 0 && 318 (psp->ps_sigonstack & sigmask(sig))) { 319 fp = (struct sigframe *)(psp->ps_sigstk.ss_base + 320 psp->ps_sigstk.ss_size - sizeof(struct sigframe)); 321 psp->ps_sigstk.ss_flags |= SA_ONSTACK; 322 } else { 323 if (frmtrap) 324 fp = (struct sigframe *)(regs[tESP] 325 - sizeof(struct sigframe)); 326 else 327 fp = (struct sigframe *)(regs[sESP] 328 - sizeof(struct sigframe)); 329 } 330 331 if ((unsigned)fp <= USRSTACK - ctob(p->p_vmspace->vm_ssize)) 332 (void)grow(p, (unsigned)fp); 333 334 if (useracc((caddr_t)fp, sizeof (struct sigframe), B_WRITE) == 0) { 335 /* 336 * Process has trashed its stack; give it an illegal 337 * instruction to halt it in its tracks. 338 */ 339 SIGACTION(p, SIGILL) = SIG_DFL; 340 sig = sigmask(SIGILL); 341 p->p_sigignore &= ~sig; 342 p->p_sigcatch &= ~sig; 343 p->p_sigmask &= ~sig; 344 psignal(p, SIGILL); 345 return; 346 } 347 348 /* 349 * Build the argument list for the signal handler. 350 */ 351 fp->sf_signum = sig; 352 fp->sf_code = code; 353 fp->sf_scp = &fp->sf_sc; 354 fp->sf_handler = catcher; 355 356 /* save scratch registers */ 357 if(frmtrap) { 358 fp->sf_eax = regs[tEAX]; 359 fp->sf_edx = regs[tEDX]; 360 fp->sf_ecx = regs[tECX]; 361 } else { 362 fp->sf_eax = regs[sEAX]; 363 fp->sf_edx = regs[sEDX]; 364 fp->sf_ecx = regs[sECX]; 365 } 366 /* 367 * Build the signal context to be used by sigreturn. 368 */ 369 fp->sf_sc.sc_onstack = oonstack; 370 fp->sf_sc.sc_mask = mask; 371 if(frmtrap) { 372 fp->sf_sc.sc_sp = regs[tESP]; 373 fp->sf_sc.sc_fp = regs[tEBP]; 374 fp->sf_sc.sc_pc = regs[tEIP]; 375 fp->sf_sc.sc_ps = regs[tEFLAGS]; 376 regs[tESP] = (int)fp; 377 regs[tEIP] = (int)((struct pcb *)kstack)->pcb_sigc; 378 } else { 379 fp->sf_sc.sc_sp = regs[sESP]; 380 fp->sf_sc.sc_fp = regs[sEBP]; 381 fp->sf_sc.sc_pc = regs[sEIP]; 382 fp->sf_sc.sc_ps = regs[sEFLAGS]; 383 regs[sESP] = (int)fp; 384 regs[sEIP] = (int)((struct pcb *)kstack)->pcb_sigc; 385 } 386 } 387 388 /* 389 * System call to cleanup state after a signal 390 * has been taken. Reset signal mask and 391 * stack state from context left by sendsig (above). 392 * Return to previous pc and psl as specified by 393 * context left by sendsig. Check carefully to 394 * make sure that the user has not modified the 395 * psl to gain improper priviledges or to cause 396 * a machine fault. 397 */ 398 struct sigreturn_args { 399 struct sigcontext *sigcntxp; 400 }; 401 sigreturn(p, uap, retval) 402 struct proc *p; 403 struct sigreturn_args *uap; 404 int *retval; 405 { 406 register struct sigcontext *scp; 407 register struct sigframe *fp; 408 register int *regs = p->p_md.md_regs; 409 410 411 fp = (struct sigframe *) regs[sESP] ; 412 413 if (useracc((caddr_t)fp, sizeof (*fp), 0) == 0) 414 return(EINVAL); 415 416 /* restore scratch registers */ 417 regs[sEAX] = fp->sf_eax ; 418 regs[sEDX] = fp->sf_edx ; 419 regs[sECX] = fp->sf_ecx ; 420 421 scp = fp->sf_scp; 422 if (useracc((caddr_t)scp, sizeof (*scp), 0) == 0) 423 return(EINVAL); 424 #ifdef notyet 425 if ((scp->sc_ps & PSL_MBZ) != 0 || (scp->sc_ps & PSL_MBO) != PSL_MBO) { 426 return(EINVAL); 427 } 428 #endif 429 if (scp->sc_onstack & 01) 430 p->p_sigacts->ps_sigstk.ss_flags |= SA_ONSTACK; 431 else 432 p->p_sigacts->ps_sigstk.ss_flags &= ~SA_ONSTACK; 433 p->p_sigmask = scp->sc_mask &~ 434 (sigmask(SIGKILL)|sigmask(SIGCONT)|sigmask(SIGSTOP)); 435 regs[sEBP] = scp->sc_fp; 436 regs[sESP] = scp->sc_sp; 437 regs[sEIP] = scp->sc_pc; 438 regs[sEFLAGS] = scp->sc_ps; 439 return(EJUSTRETURN); 440 } 441 442 int waittime = -1; 443 444 boot(arghowto) 445 int arghowto; 446 { 447 register long dummy; /* r12 is reserved */ 448 register int howto; /* r11 == how to boot */ 449 register int devtype; /* r10 == major of root dev */ 450 extern char *panicstr; 451 extern int cold; 452 453 howto = arghowto; 454 if ((howto&RB_NOSYNC) == 0 && waittime < 0 && bfreelist[0].b_forw) { 455 register struct buf *bp; 456 int iter, nbusy; 457 458 waittime = 0; 459 (void) splnet(); 460 printf("syncing disks... "); 461 /* 462 * Release inodes held by texts before update. 463 */ 464 if (panicstr == 0) 465 vnode_pager_umount(NULL); 466 sync((struct sigcontext *)0); 467 468 for (iter = 0; iter < 20; iter++) { 469 nbusy = 0; 470 for (bp = &buf[nbuf]; --bp >= buf; ) 471 if ((bp->b_flags & (B_BUSY|B_INVAL)) == B_BUSY) 472 nbusy++; 473 if (nbusy == 0) 474 break; 475 printf("%d ", nbusy); 476 DELAY(40000 * iter); 477 } 478 if (nbusy) 479 printf("giving up\n"); 480 else 481 printf("done\n"); 482 DELAY(10000); /* wait for printf to finish */ 483 } 484 splhigh(); 485 devtype = major(rootdev); 486 if (howto&RB_HALT) { 487 printf("halting (in tight loop); hit reset\n\n"); 488 splx(0xfffd); /* all but keyboard XXX */ 489 for (;;) ; 490 } else { 491 if (howto & RB_DUMP) { 492 dumpsys(); 493 /*NOTREACHED*/ 494 } 495 } 496 #ifdef lint 497 dummy = 0; dummy = dummy; 498 printf("howto %d, devtype %d\n", arghowto, devtype); 499 #endif 500 #ifdef notdef 501 pg("pausing (hit any key to reset)"); 502 #endif 503 reset_cpu(); 504 for(;;) ; 505 /*NOTREACHED*/ 506 } 507 508 int dumpmag = 0x8fca0101; /* magic number for savecore */ 509 int dumpsize = 0; /* also for savecore */ 510 /* 511 * Doadump comes here after turning off memory management and 512 * getting on the dump stack, either when called above, or by 513 * the auto-restart code. 514 */ 515 dumpsys() 516 { 517 518 if (dumpdev == NODEV) 519 return; 520 if ((minor(dumpdev)&07) != 1) 521 return; 522 dumpsize = physmem; 523 printf("\ndumping to dev %x, offset %d\n", dumpdev, dumplo); 524 printf("dump "); 525 switch ((*bdevsw[major(dumpdev)].d_dump)(dumpdev)) { 526 527 case ENXIO: 528 printf("device bad\n"); 529 break; 530 531 case EFAULT: 532 printf("device not ready\n"); 533 break; 534 535 case EINVAL: 536 printf("area improper\n"); 537 break; 538 539 case EIO: 540 printf("i/o error\n"); 541 break; 542 543 default: 544 printf("succeeded\n"); 545 break; 546 } 547 printf("\n\n"); 548 DELAY(1000); 549 } 550 551 microtime(tvp) 552 register struct timeval *tvp; 553 { 554 int s = splhigh(); 555 556 *tvp = time; 557 tvp->tv_usec += tick; 558 while (tvp->tv_usec > 1000000) { 559 tvp->tv_sec++; 560 tvp->tv_usec -= 1000000; 561 } 562 splx(s); 563 } 564 565 physstrat(bp, strat, prio) 566 struct buf *bp; 567 int (*strat)(), prio; 568 { 569 register int s; 570 caddr_t baddr; 571 572 /* 573 * vmapbuf clobbers b_addr so we must remember it so that it 574 * can be restored after vunmapbuf. This is truely rude, we 575 * should really be storing this in a field in the buf struct 576 * but none are available and I didn't want to add one at 577 * this time. Note that b_addr for dirty page pushes is 578 * restored in vunmapbuf. (ugh!) 579 */ 580 baddr = bp->b_un.b_addr; 581 vmapbuf(bp); 582 (*strat)(bp); 583 /* pageout daemon doesn't wait for pushed pages */ 584 if (bp->b_flags & B_DIRTY) 585 return; 586 s = splbio(); 587 while ((bp->b_flags & B_DONE) == 0) 588 sleep((caddr_t)bp, prio); 589 splx(s); 590 vunmapbuf(bp); 591 bp->b_un.b_addr = baddr; 592 } 593 594 initcpu() 595 { 596 } 597 598 /* 599 * Clear registers on exec 600 */ 601 setregs(p, entry, retval) 602 register struct proc *p; 603 u_long entry; 604 int retval[2]; 605 { 606 p->p_md.md_regs[sEBP] = 0; /* bottom of the fp chain */ 607 p->p_md.md_regs[sEIP] = entry; 608 609 p->p_addr->u_pcb.pcb_flags = 0; /* no fp at all */ 610 load_cr0(rcr0() | CR0_EM); /* start emulating */ 611 #include "npx.h" 612 #if NNPX > 0 613 npxinit(0x262); 614 #endif 615 } 616 617 /* 618 * Initialize 386 and configure to run kernel 619 */ 620 621 /* 622 * Initialize segments & interrupt table 623 */ 624 625 626 #define GNULL_SEL 0 /* Null Descriptor */ 627 #define GCODE_SEL 1 /* Kernel Code Descriptor */ 628 #define GDATA_SEL 2 /* Kernel Data Descriptor */ 629 #define GLDT_SEL 3 /* LDT - eventually one per process */ 630 #define GTGATE_SEL 4 /* Process task switch gate */ 631 #define GPANIC_SEL 5 /* Task state to consider panic from */ 632 #define GPROC0_SEL 6 /* Task state process slot zero and up */ 633 #define NGDT GPROC0_SEL+1 634 635 union descriptor gdt[GPROC0_SEL+1]; 636 637 /* interrupt descriptor table */ 638 struct gate_descriptor idt[32+16]; 639 640 /* local descriptor table */ 641 union descriptor ldt[5]; 642 #define LSYS5CALLS_SEL 0 /* forced by intel BCS */ 643 #define LSYS5SIGR_SEL 1 644 645 #define L43BSDCALLS_SEL 2 /* notyet */ 646 #define LUCODE_SEL 3 647 #define LUDATA_SEL 4 648 /* seperate stack, es,fs,gs sels ? */ 649 /* #define LPOSIXCALLS_SEL 5 /* notyet */ 650 651 struct i386tss tss, panic_tss; 652 653 extern struct user *proc0paddr; 654 655 /* software prototypes -- in more palitable form */ 656 struct soft_segment_descriptor gdt_segs[] = { 657 /* Null Descriptor */ 658 { 0x0, /* segment base address */ 659 0x0, /* length - all address space */ 660 0, /* segment type */ 661 0, /* segment descriptor priority level */ 662 0, /* segment descriptor present */ 663 0,0, 664 0, /* default 32 vs 16 bit size */ 665 0 /* limit granularity (byte/page units)*/ }, 666 /* Code Descriptor for kernel */ 667 { 0x0, /* segment base address */ 668 0xfffff, /* length - all address space */ 669 SDT_MEMERA, /* segment type */ 670 0, /* segment descriptor priority level */ 671 1, /* segment descriptor present */ 672 0,0, 673 1, /* default 32 vs 16 bit size */ 674 1 /* limit granularity (byte/page units)*/ }, 675 /* Data Descriptor for kernel */ 676 { 0x0, /* segment base address */ 677 0xfffff, /* length - all address space */ 678 SDT_MEMRWA, /* segment type */ 679 0, /* segment descriptor priority level */ 680 1, /* segment descriptor present */ 681 0,0, 682 1, /* default 32 vs 16 bit size */ 683 1 /* limit granularity (byte/page units)*/ }, 684 /* LDT Descriptor */ 685 { (int) ldt, /* segment base address */ 686 sizeof(ldt)-1, /* length - all address space */ 687 SDT_SYSLDT, /* segment type */ 688 0, /* segment descriptor priority level */ 689 1, /* segment descriptor present */ 690 0,0, 691 0, /* unused - default 32 vs 16 bit size */ 692 0 /* limit granularity (byte/page units)*/ }, 693 /* Null Descriptor - Placeholder */ 694 { 0x0, /* segment base address */ 695 0x0, /* length - all address space */ 696 0, /* segment type */ 697 0, /* segment descriptor priority level */ 698 0, /* segment descriptor present */ 699 0,0, 700 0, /* default 32 vs 16 bit size */ 701 0 /* limit granularity (byte/page units)*/ }, 702 /* Panic Tss Descriptor */ 703 { (int) &panic_tss, /* segment base address */ 704 sizeof(tss)-1, /* length - all address space */ 705 SDT_SYS386TSS, /* segment type */ 706 0, /* segment descriptor priority level */ 707 1, /* segment descriptor present */ 708 0,0, 709 0, /* unused - default 32 vs 16 bit size */ 710 0 /* limit granularity (byte/page units)*/ }, 711 /* Proc 0 Tss Descriptor */ 712 { (int) kstack, /* segment base address */ 713 sizeof(tss)-1, /* length - all address space */ 714 SDT_SYS386TSS, /* segment type */ 715 0, /* segment descriptor priority level */ 716 1, /* segment descriptor present */ 717 0,0, 718 0, /* unused - default 32 vs 16 bit size */ 719 0 /* limit granularity (byte/page units)*/ }}; 720 721 struct soft_segment_descriptor ldt_segs[] = { 722 /* Null Descriptor - overwritten by call gate */ 723 { 0x0, /* segment base address */ 724 0x0, /* length - all address space */ 725 0, /* segment type */ 726 0, /* segment descriptor priority level */ 727 0, /* segment descriptor present */ 728 0,0, 729 0, /* default 32 vs 16 bit size */ 730 0 /* limit granularity (byte/page units)*/ }, 731 /* Null Descriptor - overwritten by call gate */ 732 { 0x0, /* segment base address */ 733 0x0, /* length - all address space */ 734 0, /* segment type */ 735 0, /* segment descriptor priority level */ 736 0, /* segment descriptor present */ 737 0,0, 738 0, /* default 32 vs 16 bit size */ 739 0 /* limit granularity (byte/page units)*/ }, 740 /* Null Descriptor - overwritten by call gate */ 741 { 0x0, /* segment base address */ 742 0x0, /* length - all address space */ 743 0, /* segment type */ 744 0, /* segment descriptor priority level */ 745 0, /* segment descriptor present */ 746 0,0, 747 0, /* default 32 vs 16 bit size */ 748 0 /* limit granularity (byte/page units)*/ }, 749 /* Code Descriptor for user */ 750 { 0x0, /* segment base address */ 751 0xfffff, /* length - all address space */ 752 SDT_MEMERA, /* segment type */ 753 SEL_UPL, /* segment descriptor priority level */ 754 1, /* segment descriptor present */ 755 0,0, 756 1, /* default 32 vs 16 bit size */ 757 1 /* limit granularity (byte/page units)*/ }, 758 /* Data Descriptor for user */ 759 { 0x0, /* segment base address */ 760 0xfffff, /* length - all address space */ 761 SDT_MEMRWA, /* segment type */ 762 SEL_UPL, /* segment descriptor priority level */ 763 1, /* segment descriptor present */ 764 0,0, 765 1, /* default 32 vs 16 bit size */ 766 1 /* limit granularity (byte/page units)*/ } }; 767 768 /* table descriptors - used to load tables by microp */ 769 struct region_descriptor r_gdt = { 770 sizeof(gdt)-1,(char *)gdt 771 }; 772 773 struct region_descriptor r_idt = { 774 sizeof(idt)-1,(char *)idt 775 }; 776 777 setidt(idx, func, typ, dpl) char *func; { 778 struct gate_descriptor *ip = idt + idx; 779 780 ip->gd_looffset = (int)func; 781 ip->gd_selector = 8; 782 ip->gd_stkcpy = 0; 783 ip->gd_xx = 0; 784 ip->gd_type = typ; 785 ip->gd_dpl = dpl; 786 ip->gd_p = 1; 787 ip->gd_hioffset = ((int)func)>>16 ; 788 } 789 790 #define IDTVEC(name) __CONCAT(X, name) 791 extern IDTVEC(div), IDTVEC(dbg), IDTVEC(nmi), IDTVEC(bpt), IDTVEC(ofl), 792 IDTVEC(bnd), IDTVEC(ill), IDTVEC(dna), IDTVEC(dble), IDTVEC(fpusegm), 793 IDTVEC(tss), IDTVEC(missing), IDTVEC(stk), IDTVEC(prot), 794 IDTVEC(page), IDTVEC(rsvd), IDTVEC(fpu), IDTVEC(rsvd0), 795 IDTVEC(rsvd1), IDTVEC(rsvd2), IDTVEC(rsvd3), IDTVEC(rsvd4), 796 IDTVEC(rsvd5), IDTVEC(rsvd6), IDTVEC(rsvd7), IDTVEC(rsvd8), 797 IDTVEC(rsvd9), IDTVEC(rsvd10), IDTVEC(rsvd11), IDTVEC(rsvd12), 798 IDTVEC(rsvd13), IDTVEC(rsvd14), IDTVEC(rsvd14), IDTVEC(syscall); 799 800 int lcr0(), lcr3(), rcr0(), rcr2(); 801 int _udatasel, _ucodesel, _gsel_tss; 802 803 init386(first) { extern ssdtosd(), lgdt(), lidt(), lldt(), etext; 804 int x, *pi; 805 unsigned biosbasemem, biosextmem; 806 struct gate_descriptor *gdp; 807 extern int sigcode,szsigcode; 808 809 proc0.p_addr = proc0paddr; 810 811 /* 812 * Initialize the console before we print anything out. 813 */ 814 815 cninit (KERNBASE+0xa0000); 816 817 /* make gdt memory segments */ 818 gdt_segs[GCODE_SEL].ssd_limit = btoc((int) &etext + NBPG); 819 for (x=0; x < NGDT; x++) ssdtosd(gdt_segs+x, gdt+x); 820 /* make ldt memory segments */ 821 ldt_segs[LUCODE_SEL].ssd_limit = btoc(UPT_MIN_ADDRESS); 822 ldt_segs[LUDATA_SEL].ssd_limit = btoc(UPT_MIN_ADDRESS); 823 /* Note. eventually want private ldts per process */ 824 for (x=0; x < 5; x++) ssdtosd(ldt_segs+x, ldt+x); 825 826 /* exceptions */ 827 setidt(0, &IDTVEC(div), SDT_SYS386TGT, SEL_KPL); 828 setidt(1, &IDTVEC(dbg), SDT_SYS386TGT, SEL_KPL); 829 setidt(2, &IDTVEC(nmi), SDT_SYS386TGT, SEL_KPL); 830 setidt(3, &IDTVEC(bpt), SDT_SYS386TGT, SEL_UPL); 831 setidt(4, &IDTVEC(ofl), SDT_SYS386TGT, SEL_KPL); 832 setidt(5, &IDTVEC(bnd), SDT_SYS386TGT, SEL_KPL); 833 setidt(6, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL); 834 setidt(7, &IDTVEC(dna), SDT_SYS386TGT, SEL_KPL); 835 setidt(8, &IDTVEC(dble), SDT_SYS386TGT, SEL_KPL); 836 setidt(9, &IDTVEC(fpusegm), SDT_SYS386TGT, SEL_KPL); 837 setidt(10, &IDTVEC(tss), SDT_SYS386TGT, SEL_KPL); 838 setidt(11, &IDTVEC(missing), SDT_SYS386TGT, SEL_KPL); 839 setidt(12, &IDTVEC(stk), SDT_SYS386TGT, SEL_KPL); 840 setidt(13, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL); 841 setidt(14, &IDTVEC(page), SDT_SYS386TGT, SEL_KPL); 842 setidt(15, &IDTVEC(rsvd), SDT_SYS386TGT, SEL_KPL); 843 setidt(16, &IDTVEC(fpu), SDT_SYS386TGT, SEL_KPL); 844 setidt(17, &IDTVEC(rsvd0), SDT_SYS386TGT, SEL_KPL); 845 setidt(18, &IDTVEC(rsvd1), SDT_SYS386TGT, SEL_KPL); 846 setidt(19, &IDTVEC(rsvd2), SDT_SYS386TGT, SEL_KPL); 847 setidt(20, &IDTVEC(rsvd3), SDT_SYS386TGT, SEL_KPL); 848 setidt(21, &IDTVEC(rsvd4), SDT_SYS386TGT, SEL_KPL); 849 setidt(22, &IDTVEC(rsvd5), SDT_SYS386TGT, SEL_KPL); 850 setidt(23, &IDTVEC(rsvd6), SDT_SYS386TGT, SEL_KPL); 851 setidt(24, &IDTVEC(rsvd7), SDT_SYS386TGT, SEL_KPL); 852 setidt(25, &IDTVEC(rsvd8), SDT_SYS386TGT, SEL_KPL); 853 setidt(26, &IDTVEC(rsvd9), SDT_SYS386TGT, SEL_KPL); 854 setidt(27, &IDTVEC(rsvd10), SDT_SYS386TGT, SEL_KPL); 855 setidt(28, &IDTVEC(rsvd11), SDT_SYS386TGT, SEL_KPL); 856 setidt(29, &IDTVEC(rsvd12), SDT_SYS386TGT, SEL_KPL); 857 setidt(30, &IDTVEC(rsvd13), SDT_SYS386TGT, SEL_KPL); 858 setidt(31, &IDTVEC(rsvd14), SDT_SYS386TGT, SEL_KPL); 859 860 #include "isa.h" 861 #if NISA >0 862 isa_defaultirq(); 863 #endif 864 865 lgdt(gdt, sizeof(gdt)-1); 866 lidt(idt, sizeof(idt)-1); 867 lldt(GSEL(GLDT_SEL, SEL_KPL)); 868 869 /* 870 * This memory size stuff is a real mess. Here is a simple 871 * setup that just believes the BIOS. After the rest of 872 * the system is a little more stable, we'll come back to 873 * this and deal with issues if incorrect BIOS information, 874 * and when physical memory is > 16 megabytes. 875 */ 876 biosbasemem = rtcin(RTC_BASELO)+ (rtcin(RTC_BASEHI)<<8); 877 biosextmem = rtcin(RTC_EXTLO)+ (rtcin(RTC_EXTHI)<<8); 878 Maxmem = btoc ((biosextmem + 1024) * 1024); 879 maxmem = Maxmem - 1; 880 physmem = btoc (biosbasemem * 1024 + (biosextmem - 1) * 1024); 881 printf ("bios %dK+%dK. maxmem %x, physmem %x\n", 882 biosbasemem, biosextmem, ctob (maxmem), ctob (physmem)); 883 884 vm_set_page_size(); 885 /* call pmap initialization to make new kernel address space */ 886 pmap_bootstrap (first, 0); 887 /* now running on new page tables, configured,and u/iom is accessible */ 888 889 /* make a initial tss so microp can get interrupt stack on syscall! */ 890 proc0.p_addr->u_pcb.pcb_tss.tss_esp0 = (int) kstack + UPAGES*NBPG; 891 proc0.p_addr->u_pcb.pcb_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL) ; 892 _gsel_tss = GSEL(GPROC0_SEL, SEL_KPL); 893 ltr(_gsel_tss); 894 895 /* make a call gate to reenter kernel with */ 896 gdp = &ldt[LSYS5CALLS_SEL].gd; 897 898 x = (int) &IDTVEC(syscall); 899 gdp->gd_looffset = x++; 900 gdp->gd_selector = GSEL(GCODE_SEL,SEL_KPL); 901 gdp->gd_stkcpy = 0; 902 gdp->gd_type = SDT_SYS386CGT; 903 gdp->gd_dpl = SEL_UPL; 904 gdp->gd_p = 1; 905 gdp->gd_hioffset = ((int) &IDTVEC(syscall)) >>16; 906 907 /* transfer to user mode */ 908 909 _ucodesel = LSEL(LUCODE_SEL, SEL_UPL); 910 _udatasel = LSEL(LUDATA_SEL, SEL_UPL); 911 912 /* setup proc 0's pcb */ 913 bcopy(&sigcode, proc0.p_addr->u_pcb.pcb_sigc, szsigcode); 914 proc0.p_addr->u_pcb.pcb_flags = 0; 915 proc0.p_addr->u_pcb.pcb_ptd = IdlePTD; 916 } 917 918 extern struct pte *CMAP1, *CMAP2; 919 extern caddr_t CADDR1, CADDR2; 920 /* 921 * zero out physical memory 922 * specified in relocation units (NBPG bytes) 923 */ 924 clearseg(n) { 925 926 *(int *)CMAP2 = PG_V | PG_KW | ctob(n); 927 load_cr3(rcr3()); 928 bzero(CADDR2,NBPG); 929 *(int *) CADDR2 = 0; 930 } 931 932 /* 933 * copy a page of physical memory 934 * specified in relocation units (NBPG bytes) 935 */ 936 copyseg(frm, n) { 937 938 *(int *)CMAP2 = PG_V | PG_KW | ctob(n); 939 load_cr3(rcr3()); 940 bcopy((void *)frm, (void *)CADDR2, NBPG); 941 } 942 943 /* 944 * copy a page of physical memory 945 * specified in relocation units (NBPG bytes) 946 */ 947 physcopyseg(frm, to) { 948 949 *(int *)CMAP1 = PG_V | PG_KW | ctob(frm); 950 *(int *)CMAP2 = PG_V | PG_KW | ctob(to); 951 load_cr3(rcr3()); 952 bcopy(CADDR1, CADDR2, NBPG); 953 } 954 955 /*aston() { 956 schednetisr(NETISR_AST); 957 }*/ 958 959 setsoftclock() { 960 schednetisr(NETISR_SCLK); 961 } 962 963 /* 964 * insert an element into a queue 965 */ 966 #undef insque 967 _insque(element, head) 968 register struct prochd *element, *head; 969 { 970 element->ph_link = head->ph_link; 971 head->ph_link = (struct proc *)element; 972 element->ph_rlink = (struct proc *)head; 973 ((struct prochd *)(element->ph_link))->ph_rlink=(struct proc *)element; 974 } 975 976 /* 977 * remove an element from a queue 978 */ 979 #undef remque 980 _remque(element) 981 register struct prochd *element; 982 { 983 ((struct prochd *)(element->ph_link))->ph_rlink = element->ph_rlink; 984 ((struct prochd *)(element->ph_rlink))->ph_link = element->ph_link; 985 element->ph_rlink = (struct proc *)0; 986 } 987 988 vmunaccess() {} 989 990 /* 991 * Below written in C to allow access to debugging code 992 */ 993 copyinstr(fromaddr, toaddr, maxlength, lencopied) u_int *lencopied, maxlength; 994 void *toaddr, *fromaddr; { 995 int c,tally; 996 997 tally = 0; 998 while (maxlength--) { 999 c = fubyte(fromaddr++); 1000 if (c == -1) { 1001 if(lencopied) *lencopied = tally; 1002 return(EFAULT); 1003 } 1004 tally++; 1005 *(char *)toaddr++ = (char) c; 1006 if (c == 0){ 1007 if(lencopied) *lencopied = tally; 1008 return(0); 1009 } 1010 } 1011 if(lencopied) *lencopied = tally; 1012 return(ENAMETOOLONG); 1013 } 1014 1015 copyoutstr(fromaddr, toaddr, maxlength, lencopied) u_int *lencopied, maxlength; 1016 void *fromaddr, *toaddr; { 1017 int c; 1018 int tally; 1019 1020 tally = 0; 1021 while (maxlength--) { 1022 c = subyte(toaddr++, *(char *)fromaddr); 1023 if (c == -1) return(EFAULT); 1024 tally++; 1025 if (*(char *)fromaddr++ == 0){ 1026 if(lencopied) *lencopied = tally; 1027 return(0); 1028 } 1029 } 1030 if(lencopied) *lencopied = tally; 1031 return(ENAMETOOLONG); 1032 } 1033 1034 copystr(fromaddr, toaddr, maxlength, lencopied) u_int *lencopied, maxlength; 1035 void *fromaddr, *toaddr; { 1036 u_int tally; 1037 1038 tally = 0; 1039 while (maxlength--) { 1040 *(u_char *)toaddr = *(u_char *)fromaddr++; 1041 tally++; 1042 if (*(u_char *)toaddr++ == 0) { 1043 if(lencopied) *lencopied = tally; 1044 return(0); 1045 } 1046 } 1047 if(lencopied) *lencopied = tally; 1048 return(ENAMETOOLONG); 1049 } 1050