2 #include "../port/lib.h"
16 * Where configuration info is left for the loaded programme.
17 * This will turn into a structure as more is done by the boot loader
18 * (e.g. why parse the .ini file twice?).
19 * There are 3584 bytes available at CONFADDR.
21 #define BOOTLINE ((char*)CONFADDR)
22 #define BOOTLINELEN 64
23 #define BOOTARGS ((char*)(CONFADDR+BOOTLINELEN))
24 #define BOOTARGSLEN (4096-0x200-BOOTLINELEN)
27 char bootdisk[KNAMELEN];
29 char *confname[MAXCONF];
30 char *confval[MAXCONF];
32 uchar *sp; /* user stack of init proc */
42 extern ulong *multiboot;
48 if((multiboot[0] & (1<<2)) != 0)
49 strncpy(BOOTLINE, KADDR(multiboot[4]), BOOTLINELEN-1);
52 ep = cp + BOOTARGSLEN-1;
55 if((multiboot[0] & (1<<6)) != 0 && (l = multiboot[11]) >= 24){
56 cp = seprint(cp, ep, "*e820=");
57 m = KADDR(multiboot[12]);
58 while(m[0] >= 20 && m[0] <= l-4){
61 base = ((uvlong)m[0] | (uvlong)m[1]<<32);
62 size = ((uvlong)m[2] | (uvlong)m[3]<<32);
63 cp = seprint(cp, ep, "%.1lux %.16llux %.16llux ",
64 m[4] & 0xF, base, base+size);
66 m = (ulong*)((ulong)m + m[-1]);
71 /* plan9.ini passed as the first module */
72 if((multiboot[0] & (1<<3)) != 0 && multiboot[5] > 0){
73 m = KADDR(multiboot[6]);
88 char *cp, *line[MAXCONF], *p, *q;
93 * parse configuration args from dos file plan9.ini
95 cp = BOOTARGS; /* where b.com leaves its config */
96 cp[BOOTARGSLEN-1] = 0;
99 * Strip out '\r', change '\t' -> ' '.
102 for(q = cp; *q; q++){
111 n = getfields(cp, line, MAXCONF, 1, "\n");
112 for(i = 0; i < n; i++){
115 cp = strchr(line[i], '=');
119 confname[nconf] = line[i];
125 extern void mmuinit0(void);
126 extern void (*i8237alloc)(void);
127 extern void bootscreeninit(void);
152 if(i8237alloc != nil)
158 if(arch->intrinit) /* launches other processors on an mp */
163 if(arch->clockenable)
177 active.thunderbirdsarego = 1;
185 MACHP(0) = (Mach*)CPU0MACH;
186 m->pdb = (ulong*)CPU0PDB;
187 m->gdt = (Segdesc*)CPU0GDT;
205 memset(m, 0, sizeof(Mach));
212 * For polled uart output at boot, need
213 * a default delay constant. 100000 should
214 * be enough for a while. Cpuidentify will
215 * calculate the real value later.
217 m->loopconst = 100000;
224 char buf[2*KNAMELEN];
231 * These are o.k. because rootinit is null.
232 * Then early kproc's will have a root and dot.
234 up->slash = namec("#/", Atodir, 0, 0);
235 pathclose(up->slash->path);
236 up->slash->path = newpath("/");
237 up->dot = cclone(up->slash);
242 snprint(buf, sizeof(buf), "%s %s", arch->id, conffile);
243 ksetenv("terminal", buf, 0);
244 ksetenv("cputype", "386", 0);
246 ksetenv("service", "cpu", 0);
248 ksetenv("service", "terminal", 0);
249 for(i = 0; i < nconf; i++){
250 if(confname[i][0] != '*')
251 ksetenv(confname[i], confval[i], 0);
252 ksetenv(confname[i], confval[i], 1);
256 kproc("alarm", alarmkproc, 0);
270 p->egrp = smalloc(sizeof(Egrp));
272 p->fgrp = dupfgrp(nil);
277 kstrdup(&p->text, "*init*");
278 kstrdup(&p->user, eve);
285 * N.B. make sure there's enough space for syscall to check
287 * 4 bytes for gotolabel's return PC
289 p->sched.pc = (ulong)init0;
290 p->sched.sp = (ulong)p->kstack+KSTACK-(sizeof(Sargs)+BY2WD);
295 * N.B. cannot call newpage() with clear=1, because pc kmap
296 * requires up != nil. use tmpmap instead.
298 s = newseg(SG_STACK, USTKTOP-USTKSIZE, USTKSIZE/BY2PG);
300 pg = newpage(0, 0, USTKTOP-BY2PG);
310 s = newseg(SG_TEXT, UTZERO, 1);
313 pg = newpage(0, 0, UTZERO);
314 memset(pg->cachectl, PG_TXTFLUSH, sizeof(pg->cachectl));
318 memmove(v, initcode, sizeof initcode);
344 sp = (uchar*)base + BY2PG - sizeof(Tos);
347 av[ac++] = pusharg("boot");
349 /* when boot is changed to only use rc, this code can go away */
350 cp[BOOTLINELEN-1] = 0;
352 if(strncmp(cp, "fd", 2) == 0){
353 sprint(buf, "local!#f/fd%lddisk", strtol(cp+2, 0, 0));
354 av[ac++] = pusharg(buf);
355 } else if(strncmp(cp, "sd", 2) == 0){
356 sprint(buf, "local!#S/sd%c%c/fs", *(cp+2), *(cp+3));
357 av[ac++] = pusharg(buf);
358 } else if(strncmp(cp, "ether", 5) == 0)
359 av[ac++] = pusharg("-n");
361 /* 4 byte word align stack */
362 sp = (uchar*)((ulong)sp & ~3);
364 /* build argc, argv on stack */
365 sp -= (ac+1)*sizeof(sp);
367 for(i = 0; i < ac; i++)
368 lsp[i] = av[i] + ((USTKTOP - BY2PG) - (ulong)base);
370 sp += (USTKTOP - BY2PG) - (ulong)base;
379 for(i = 0; i < nconf; i++)
380 if(cistrcmp(confname[i], name) == 0)
398 /* convert to name=value\n format */
407 error("kernel configuration too large");
408 memset(BOOTLINE, 0, BOOTLINELEN);
409 memmove(BOOTARGS, p, n);
421 if(p = getconf("service")){
422 if(strcmp(p, "cpu") == 0)
424 else if(strcmp(p,"terminal") == 0)
428 if(p = getconf("*kernelpercent"))
429 userpcnt = 100 - strtol(p, 0, 0);
434 for(i=0; i<nelem(conf.mem); i++)
435 conf.npage += conf.mem[i].npage;
437 conf.nproc = 100 + ((conf.npage*BY2PG)/MB)*5;
440 if(conf.nproc > 2000)
443 conf.nswap = conf.nproc*80;
449 kpages = conf.npage - (conf.npage*userpcnt)/100;
452 * Hack for the big boys. Only good while physmem < 4GB.
453 * Give the kernel fixed max + enough to allocate the
455 * This is an overestimate as conf.upages < conf.npages.
456 * The patch of nimage is a band-aid, scanning the whole
457 * page list in imagereclaim just takes too long.
459 if(getconf("*imagemaxmb") == 0)
460 if(kpages > (64*MB + conf.npage*sizeof(Page))/BY2PG){
461 kpages = (64*MB + conf.npage*sizeof(Page))/BY2PG;
463 kpages += (conf.nproc*KSTACK)/BY2PG;
467 if(conf.npage*BY2PG < 16*MB)
472 kpages = conf.npage - (conf.npage*userpcnt)/100;
475 * Make sure terminals with low memory get at least
476 * 4MB on the first Image chunk allocation.
478 if(conf.npage*BY2PG < 16*MB)
479 imagmem->minarena = 4*MB;
483 * can't go past the end of virtual memory
484 * (ulong)-KZERO is 2^32 - KZERO
486 if(kpages > ((ulong)-KZERO)/BY2PG)
487 kpages = ((ulong)-KZERO)/BY2PG;
489 conf.upages = conf.npage - kpages;
490 conf.ialloc = (kpages/2)*BY2PG;
493 * Guess how much is taken by the large permanent
494 * datastructures. Mntcache and Mntrpc are not accounted for
498 kpages -= conf.upages*sizeof(Page)
499 + conf.nproc*sizeof(Proc)
500 + conf.nimage*sizeof(Image)
502 + conf.nswppo*sizeof(Page*);
503 mainmem->maxsize = kpages;
506 * the dynamic allocation will balance the load properly,
507 * hopefully. be careful with 32-bit overflow.
509 imagmem->maxsize = kpages - (kpages/10);
510 if(p = getconf("*imagemaxmb")){
511 imagmem->maxsize = strtol(p, nil, 0)*MB;
512 if(imagmem->maxsize > mainmem->maxsize)
513 imagmem->maxsize = mainmem->maxsize;
518 * we keep FPsave structure in sse format emulating FXSAVE / FXRSTOR
519 * instructions for legacy x87 fpu.
521 * Note that fpx87restore() and fpxsserestore() do modify the FPsave
522 * data structure for conversion / realignment shuffeling. this means
523 * that p->fpsave is only valid when p->fpstate == FPinactive.
526 fpx87save(FPsave *fps)
533 * convert x87 tag word to fxsave tag byte:
534 * 00, 01, 10 -> 1, 11 -> 0
537 tag = (tag | (tag >> 1)) & 0x5555;
538 tag = (tag | (tag >> 1)) & 0x3333;
539 tag = (tag | (tag >> 2)) & 0x0F0F;
540 tag = (tag | (tag >> 4)) & 0x00FF;
542 /* NOP fps->fcw = fps->control; */
543 fps->fsw = fps->status;
545 fps->fop = fps->opcode;
546 fps->fpuip = fps->pc;
547 fps->cs = fps->selector;
548 fps->fpudp = fps->operand;
549 fps->ds = fps->oselector;
552 *((ushort*)(d+8)) = *((ushort*)(s+8)), \
553 *((ulong*)(d+4)) = *((ulong*)(s+4)), \
554 *((ulong*)(d)) = *((ulong*)(s))
556 MOVA(fps->xregs+0x70, fps->regs+70);
557 MOVA(fps->xregs+0x60, fps->regs+60);
558 MOVA(fps->xregs+0x50, fps->regs+50);
559 MOVA(fps->xregs+0x40, fps->regs+40);
560 MOVA(fps->xregs+0x30, fps->regs+30);
561 MOVA(fps->xregs+0x20, fps->regs+20);
562 MOVA(fps->xregs+0x10, fps->regs+10);
563 MOVA(fps->xregs+0x00, fps->regs+00);
568 *((ulong*)(d)) = 0, \
569 *((ushort*)(d+4)) = 0
571 CLR6(fps->xregs+0x70+10);
572 CLR6(fps->xregs+0x60+10);
573 CLR6(fps->xregs+0x50+10);
574 CLR6(fps->xregs+0x40+10);
575 CLR6(fps->xregs+0x30+10);
576 CLR6(fps->xregs+0x20+10);
577 CLR6(fps->xregs+0x10+10);
578 CLR6(fps->xregs+0x00+10);
582 fps->rsrvd1 = fps->rsrvd2 = fps->mxcsr = fps->mxcsr_mask = 0;
586 fpx87restore(FPsave *fps)
588 ushort msk, tos, tag, *reg;
590 /* convert fxsave tag byte to x87 tag word */
592 tos = 7 - ((fps->fsw >> 11) & 7);
593 for(msk = 0x80; msk != 0; tos--, msk >>= 1){
595 if((fps->ftw & msk) != 0){
596 reg = (ushort*)&fps->xregs[(tos & 7) << 4];
597 switch(reg[4] & 0x7fff){
599 if((reg[0] | reg[1] | reg[2] | reg[3]) == 0){
600 tag |= 1; /* 01 zero */
605 tag |= 2; /* 10 special */
608 if((reg[3] & 0x8000) == 0)
609 break; /* 00 valid */
610 tag |= 2; /* 10 special */
614 tag |= 3; /* 11 empty */
619 *((ulong*)(d)) = *((ulong*)(s)), \
620 *((ulong*)(d+4)) = *((ulong*)(s+4)), \
621 *((ushort*)(d+8)) = *((ushort*)(s+8))
623 MOVA(fps->regs+00, fps->xregs+0x00);
624 MOVA(fps->regs+10, fps->xregs+0x10);
625 MOVA(fps->regs+20, fps->xregs+0x20);
626 MOVA(fps->regs+30, fps->xregs+0x30);
627 MOVA(fps->regs+40, fps->xregs+0x40);
628 MOVA(fps->regs+50, fps->xregs+0x50);
629 MOVA(fps->regs+60, fps->xregs+0x60);
630 MOVA(fps->regs+70, fps->xregs+0x70);
634 fps->oselector = fps->ds;
635 fps->operand = fps->fpudp;
636 fps->opcode = fps->fop & 0x7ff;
637 fps->selector = fps->cs;
638 fps->pc = fps->fpuip;
640 fps->status = fps->fsw;
641 /* NOP fps->control = fps->fcw; */
643 fps->r1 = fps->r2 = fps->r3 = fps->r4 = 0;
649 * sse fp save and restore buffers have to be 16-byte (FPalign) aligned,
650 * so we shuffle the data up and down as needed or make copies.
653 fpssesave(FPsave *fps)
657 afps = (FPsave *)ROUND(((uintptr)fps), FPalign);
659 if(fps != afps) /* not aligned? shuffle down from aligned buffer */
660 memmove(fps, afps, sizeof(FPssestate) - FPalign);
664 fpsserestore(FPsave *fps)
668 afps = (FPsave *)ROUND(((uintptr)fps), FPalign);
669 if(fps != afps) /* shuffle up to make aligned */
670 memmove(afps, fps, sizeof(FPssestate) - FPalign);
674 static char* mathmsg[] =
676 nil, /* handled below */
677 "denormalized operand",
685 mathnote(ulong status, ulong pc)
687 char *msg, note[ERRMAX];
691 * Some attention should probably be paid here to the
692 * exception masks and error summary.
694 msg = "unknown exception";
695 for(i = 1; i <= 5; i++){
696 if(!((1<<i) & status))
704 msg = "stack overflow";
706 msg = "stack underflow";
708 msg = "invalid operation";
710 snprint(note, sizeof note, "sys: fp: %s fppc=0x%lux status=0x%lux",
712 postnote(up, 1, note, NDebug);
716 * math coprocessor error
719 matherror(Ureg*, void*)
722 * a write cycle to port 0xF0 clears the interrupt latch attached
723 * to the error# line from the 387
725 if(!(m->cpuiddx & Fpuonchip))
729 * get floating point state to check out error
732 mathnote(up->fpsave.status, up->fpsave.pc);
739 simderror(Ureg *ureg, void*)
742 up->fpstate = FPinactive;
743 mathnote(up->fpsave.mxcsr & 0x3f, ureg->pc);
747 * math coprocessor emulation fault
750 mathemu(Ureg *ureg, void*)
752 ulong status, control;
754 if(up->fpstate & FPillegal){
755 /* someone did floating point in a note handler */
756 postnote(up, 1, "sys: floating point in note handler", NDebug);
762 if(fpsave == fpssesave)
763 ldmxcsr(0); /* no simd exceptions on 386 */
764 up->fpstate = FPactive;
768 * Before restoring the state, check for any pending
769 * exceptions, there's no way to restore the state without
770 * generating an unmasked exception.
771 * More attention should probably be paid here to the
772 * exception masks and error summary.
774 status = up->fpsave.fsw;
775 control = up->fpsave.fcw;
776 if((status & ~control) & 0x07F){
777 mathnote(status, up->fpsave.fpuip);
780 fprestore(&up->fpsave);
781 up->fpstate = FPactive;
784 panic("math emu pid %ld %s pc 0x%lux",
785 up->pid, up->text, ureg->pc);
791 * math coprocessor segment overrun
794 mathover(Ureg*, void*)
796 pexit("math overrun", 0);
802 trapenable(VectorCERR, matherror, 0, "matherror");
803 if(X86FAMILY(m->cpuidax) == 3)
804 intrenable(IrqIRQ13, matherror, 0, BUSUNKNOWN, "matherror");
805 trapenable(VectorCNA, mathemu, 0, "mathemu");
806 trapenable(VectorCSO, mathover, 0, "mathover");
807 trapenable(VectorSIMD, simderror, 0, "simderror");
811 * set up floating point for a new process
820 p->pcycles = -p->kentry;
822 memset(p->gdt, 0, sizeof(p->gdt));
832 p->kentry = up->kentry;
833 p->pcycles = -p->kentry;
835 /* inherit user descriptors */
836 memmove(p->gdt, up->gdt, sizeof(p->gdt));
838 /* copy local descriptor table */
839 if(up->ldt != nil && up->nldt > 0){
840 p->ldt = smalloc(sizeof(Segdesc) * up->nldt);
841 memmove(p->ldt, up->ldt, sizeof(Segdesc) * up->nldt);
845 /* save floating point state */
847 switch(up->fpstate & ~FPillegal){
850 up->fpstate = FPinactive;
852 p->fpsave = up->fpsave;
853 p->fpstate = FPinactive;
872 * Save the mach dependent part of the process state.
883 if(p->fpstate == FPactive){
884 if(p->state == Moribund)
888 * Fpsave() stores without handling pending
889 * unmasked exeptions. Postnote() can't be called
890 * here as sleep() already has up->rlock, so
891 * the handling of pending exceptions is delayed
892 * until the process runs again and generates an
893 * emulation fault to activate the FPU.
897 p->fpstate = FPinactive;
901 * While this processor is in the scheduler, the process could run
902 * on another processor and exit, returning the page tables to
903 * the free list where they could be reallocated and overwritten.
904 * When this processor eventually has to get an entry from the
905 * trashed page tables it will crash.
907 * If there's only one processor, this can't happen.
908 * You might think it would be a win not to do this in that case,
909 * especially on VMware, but it turns out not to matter.
911 mmuflushtlb(PADDR(m->pdb));
915 shutdown(int ispanic)
921 active.ispanic = ispanic;
922 else if(m->machno == 0 && (active.machs & (1<<m->machno)) == 0)
924 once = active.machs & (1<<m->machno);
926 * setting exiting will make hzclock() on each processor call exit(0),
927 * which calls shutdown(0) and arch->reset(), which on mp systems is
928 * mpshutdown, from which there is no return: the processor is idled
929 * or initiates a reboot. clearing our bit in machs avoids calling
930 * exit(0) from hzclock() on this processor.
932 active.machs &= ~(1<<m->machno);
937 iprint("cpu%d: exiting\n", m->machno);
939 /* wait for any other processors to shutdown */
941 for(ms = 5*1000; ms > 0; ms -= TK2MS(2)){
943 if(active.machs == 0 && consactive() == 0)
951 if(getconf("*debug"))
959 reboot(void *entry, void *code, ulong size)
961 void (*f)(ulong, ulong, ulong);
967 * the boot processor is cpu0. execute this function on it
968 * so that the new kernel has the same cpu0. this only matters
969 * because the hardware has a notion of which processor was the
970 * boot processor and we look at it at start up.
972 if (m->machno != 0) {
978 iprint("shutting down...\n");
983 /* turn off buffered serial console */
986 /* shutdown devices */
991 * Modify the machine page table to directly map the low 4MB of memory
992 * This allows the reboot code to turn off the page mapping
995 pdb[PDX(0)] = pdb[PDX(KZERO)];
996 mmuflushtlb(PADDR(pdb));
998 /* setup reboot trampoline function */
999 f = (void*)REBOOTADDR;
1000 memmove(f, rebootcode, sizeof(rebootcode));
1002 /* off we go - never to return */
1004 (*f)((ulong)entry & ~0xF0000000UL, PADDR(code), size);