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)
28 char *confname[MAXCONF];
29 char *confval[MAXCONF];
31 char *sp; /* user stack of init proc */
41 extern ulong *multiboot;
47 if((multiboot[0] & (1<<2)) != 0)
48 strncpy(BOOTLINE, KADDR(multiboot[4]), BOOTLINELEN-1);
51 ep = cp + BOOTARGSLEN-1;
54 if((multiboot[0] & (1<<6)) != 0 && (l = multiboot[11]) >= 24){
55 cp = seprint(cp, ep, "*e820=");
56 m = KADDR(multiboot[12]);
57 while(m[0] >= 20 && m[0]+4 <= l){
60 base = ((uvlong)m[0] | (uvlong)m[1]<<32);
61 size = ((uvlong)m[2] | (uvlong)m[3]<<32);
62 cp = seprint(cp, ep, "%.1lux %.16llux %.16llux ",
63 m[4] & 0xF, base, base+size);
65 m = (ulong*)((ulong)m + m[-1]);
70 /* plan9.ini passed as the first module */
71 if((multiboot[0] & (1<<3)) != 0 && multiboot[5] > 0){
72 m = KADDR(multiboot[6]);
87 char *cp, *line[MAXCONF], *p, *q;
92 * parse configuration args from dos file plan9.ini
94 cp = BOOTARGS; /* where b.com leaves its config */
95 cp[BOOTARGSLEN-1] = 0;
98 * Strip out '\r', change '\t' -> ' '.
101 for(q = cp; *q; q++){
110 n = getfields(cp, line, MAXCONF, 1, "\n");
111 for(i = 0; i < n; i++){
114 cp = strchr(line[i], '=');
118 confname[nconf] = line[i];
124 extern void (*i8237alloc)(void);
125 extern void bootscreeninit(void);
147 if(i8237alloc != nil)
153 if(arch->intrinit) /* launches other processors on an mp */
157 if(arch->clockenable)
178 MACHP(0) = (Mach*)CPU0MACH;
179 m->pdb = (ulong*)CPU0PDB;
180 m->gdt = (Segdesc*)CPU0GDT;
198 memset(m, 0, sizeof(Mach));
205 * For polled uart output at boot, need
206 * a default delay constant. 100000 should
207 * be enough for a while. Cpuidentify will
208 * calculate the real value later.
210 m->loopconst = 100000;
217 char buf[2*KNAMELEN];
224 * These are o.k. because rootinit is null.
225 * Then early kproc's will have a root and dot.
227 up->slash = namec("#/", Atodir, 0, 0);
228 pathclose(up->slash->path);
229 up->slash->path = newpath("/");
230 up->dot = cclone(up->slash);
235 snprint(buf, sizeof(buf), "%s %s", arch->id, conffile);
236 ksetenv("terminal", buf, 0);
237 ksetenv("cputype", "386", 0);
239 ksetenv("service", "cpu", 0);
241 ksetenv("service", "terminal", 0);
242 for(i = 0; i < nconf; i++){
243 if(confname[i][0] != '*')
244 ksetenv(confname[i], confval[i], 0);
245 ksetenv(confname[i], confval[i], 1);
249 kproc("alarm", alarmkproc, 0);
263 p->egrp = smalloc(sizeof(Egrp));
265 p->fgrp = dupfgrp(nil);
270 kstrdup(&p->text, "*init*");
271 kstrdup(&p->user, eve);
278 * N.B. make sure there's enough space for syscall to check
280 * 4 bytes for gotolabel's return PC
282 p->sched.pc = (ulong)init0;
283 p->sched.sp = (ulong)p->kstack+KSTACK-(sizeof(Sargs)+BY2WD);
288 * N.B. cannot call newpage() with clear=1, because pc kmap
289 * requires up != nil. use tmpmap instead.
291 s = newseg(SG_STACK, USTKTOP-USTKSIZE, USTKSIZE/BY2PG);
293 pg = newpage(0, 0, USTKTOP-BY2PG);
303 s = newseg(SG_TEXT, UTZERO, 1);
306 pg = newpage(0, 0, UTZERO);
311 memmove(v, initcode, sizeof initcode);
323 #define UA(ka) ((char*)(ka) + ((uintptr)(USTKTOP - BY2PG) - (uintptr)base))
324 sp = (char*)base + BY2PG - sizeof(Tos);
326 /* push boot command line onto the stack */
328 sp[BOOTLINELEN-1] = '\0';
329 memmove(sp, BOOTLINE, BOOTLINELEN-1);
331 /* parse boot command line */
332 argc = tokenize(sp, argv, nelem(argv));
339 /* 4 byte word align stack */
340 sp = (char*)((uintptr)sp & ~3);
342 /* build argv on stack */
343 sp -= (argc+1)*BY2WD;
344 for(i=0; i<argc; i++)
345 ((char**)sp)[i] = UA(argv[i]);
346 ((char**)sp)[i] = nil;
358 for(i = 0; i < nconf; i++)
359 if(cistrcmp(confname[i], name) == 0)
377 /* convert to name=value\n format */
386 error("kernel configuration too large");
387 memset(BOOTLINE, 0, BOOTLINELEN);
388 memmove(BOOTARGS, p, n);
400 if(p = getconf("service")){
401 if(strcmp(p, "cpu") == 0)
403 else if(strcmp(p,"terminal") == 0)
407 if(p = getconf("*kernelpercent"))
408 userpcnt = 100 - strtol(p, 0, 0);
413 for(i=0; i<nelem(conf.mem); i++)
414 conf.npage += conf.mem[i].npage;
416 conf.nproc = 100 + ((conf.npage*BY2PG)/MB)*5;
419 if(conf.nproc > 2000)
422 conf.nswap = conf.nproc*80;
428 kpages = conf.npage - (conf.npage*userpcnt)/100;
429 conf.nimage = conf.nproc;
432 * Hack for the big boys. Only good while physmem < 4GB.
433 * Give the kernel fixed max + enough to allocate the
435 * This is an overestimate as conf.upages < conf.npages.
436 * The patch of nimage is a band-aid, scanning the whole
437 * page list in imagereclaim just takes too long.
439 if(getconf("*imagemaxmb") == 0)
440 if(kpages > (64*MB + conf.npage*sizeof(Page))/BY2PG){
441 kpages = (64*MB + conf.npage*sizeof(Page))/BY2PG;
442 kpages += (conf.nproc*KSTACK)/BY2PG;
446 if(conf.npage*BY2PG < 16*MB)
451 kpages = conf.npage - (conf.npage*userpcnt)/100;
454 * Make sure terminals with low memory get at least
455 * 4MB on the first Image chunk allocation.
457 if(conf.npage*BY2PG < 16*MB)
458 imagmem->minarena = 4*MB;
462 * can't go past the end of virtual memory
463 * (ulong)-KZERO is 2^32 - KZERO
465 if(kpages > ((ulong)-KZERO)/BY2PG)
466 kpages = ((ulong)-KZERO)/BY2PG;
468 conf.upages = conf.npage - kpages;
469 conf.ialloc = (kpages/2)*BY2PG;
472 * Guess how much is taken by the large permanent
473 * datastructures. Mntcache and Mntrpc are not accounted for.
476 kpages -= conf.upages*sizeof(Page)
477 + conf.nproc*sizeof(Proc)
478 + conf.nimage*sizeof(Image)
480 + conf.nswppo*sizeof(Page*);
481 mainmem->maxsize = kpages;
484 * the dynamic allocation will balance the load properly,
485 * hopefully. be careful with 32-bit overflow.
487 imagmem->maxsize = kpages - (kpages/10);
488 if(p = getconf("*imagemaxmb")){
489 imagmem->maxsize = strtol(p, nil, 0)*MB;
490 if(imagmem->maxsize > mainmem->maxsize)
491 imagmem->maxsize = mainmem->maxsize;
496 * we keep FPsave structure in sse format emulating FXSAVE / FXRSTOR
497 * instructions for legacy x87 fpu.
499 * Note that fpx87restore() and fpxsserestore() do modify the FPsave
500 * data structure for conversion / realignment shuffeling. this means
501 * that p->fpsave is only valid when p->fpstate == FPinactive.
504 fpx87save(FPsave *fps)
511 * convert x87 tag word to fxsave tag byte:
512 * 00, 01, 10 -> 1, 11 -> 0
515 tag = (tag | (tag >> 1)) & 0x5555;
516 tag = (tag | (tag >> 1)) & 0x3333;
517 tag = (tag | (tag >> 2)) & 0x0F0F;
518 tag = (tag | (tag >> 4)) & 0x00FF;
520 /* NOP fps->fcw = fps->control; */
521 fps->fsw = fps->status;
523 fps->fop = fps->opcode;
524 fps->fpuip = fps->pc;
525 fps->cs = fps->selector;
526 fps->fpudp = fps->operand;
527 fps->ds = fps->oselector;
530 *((ushort*)(d+8)) = *((ushort*)(s+8)), \
531 *((ulong*)(d+4)) = *((ulong*)(s+4)), \
532 *((ulong*)(d)) = *((ulong*)(s))
534 MOVA(fps->xregs+0x70, fps->regs+70);
535 MOVA(fps->xregs+0x60, fps->regs+60);
536 MOVA(fps->xregs+0x50, fps->regs+50);
537 MOVA(fps->xregs+0x40, fps->regs+40);
538 MOVA(fps->xregs+0x30, fps->regs+30);
539 MOVA(fps->xregs+0x20, fps->regs+20);
540 MOVA(fps->xregs+0x10, fps->regs+10);
541 MOVA(fps->xregs+0x00, fps->regs+00);
546 *((ulong*)(d)) = 0, \
547 *((ushort*)(d+4)) = 0
549 CLR6(fps->xregs+0x70+10);
550 CLR6(fps->xregs+0x60+10);
551 CLR6(fps->xregs+0x50+10);
552 CLR6(fps->xregs+0x40+10);
553 CLR6(fps->xregs+0x30+10);
554 CLR6(fps->xregs+0x20+10);
555 CLR6(fps->xregs+0x10+10);
556 CLR6(fps->xregs+0x00+10);
560 fps->rsrvd1 = fps->rsrvd2 = fps->mxcsr = fps->mxcsr_mask = 0;
564 fpx87restore(FPsave *fps)
566 ushort msk, tos, tag, *reg;
568 /* convert fxsave tag byte to x87 tag word */
570 tos = 7 - ((fps->fsw >> 11) & 7);
571 for(msk = 0x80; msk != 0; tos--, msk >>= 1){
573 if((fps->ftw & msk) != 0){
574 reg = (ushort*)&fps->xregs[(tos & 7) << 4];
575 switch(reg[4] & 0x7fff){
577 if((reg[0] | reg[1] | reg[2] | reg[3]) == 0){
578 tag |= 1; /* 01 zero */
583 tag |= 2; /* 10 special */
586 if((reg[3] & 0x8000) == 0)
587 break; /* 00 valid */
588 tag |= 2; /* 10 special */
592 tag |= 3; /* 11 empty */
597 *((ulong*)(d)) = *((ulong*)(s)), \
598 *((ulong*)(d+4)) = *((ulong*)(s+4)), \
599 *((ushort*)(d+8)) = *((ushort*)(s+8))
601 MOVA(fps->regs+00, fps->xregs+0x00);
602 MOVA(fps->regs+10, fps->xregs+0x10);
603 MOVA(fps->regs+20, fps->xregs+0x20);
604 MOVA(fps->regs+30, fps->xregs+0x30);
605 MOVA(fps->regs+40, fps->xregs+0x40);
606 MOVA(fps->regs+50, fps->xregs+0x50);
607 MOVA(fps->regs+60, fps->xregs+0x60);
608 MOVA(fps->regs+70, fps->xregs+0x70);
612 fps->oselector = fps->ds;
613 fps->operand = fps->fpudp;
614 fps->opcode = fps->fop & 0x7ff;
615 fps->selector = fps->cs;
616 fps->pc = fps->fpuip;
618 fps->status = fps->fsw;
619 /* NOP fps->control = fps->fcw; */
621 fps->r1 = fps->r2 = fps->r3 = fps->r4 = 0;
627 * sse fp save and restore buffers have to be 16-byte (FPalign) aligned,
628 * so we shuffle the data up and down as needed or make copies.
631 fpssesave(FPsave *fps)
635 afps = (FPsave *)ROUND(((uintptr)fps), FPalign);
637 if(fps != afps) /* not aligned? shuffle down from aligned buffer */
638 memmove(fps, afps, sizeof(FPssestate) - FPalign);
642 fpsserestore(FPsave *fps)
646 afps = (FPsave *)ROUND(((uintptr)fps), FPalign);
647 if(fps != afps) /* shuffle up to make aligned */
648 memmove(afps, fps, sizeof(FPssestate) - FPalign);
652 static char* mathmsg[] =
654 nil, /* handled below */
655 "denormalized operand",
663 mathnote(ulong status, ulong pc)
665 char *msg, note[ERRMAX];
669 * Some attention should probably be paid here to the
670 * exception masks and error summary.
672 msg = "unknown exception";
673 for(i = 1; i <= 5; i++){
674 if(!((1<<i) & status))
682 msg = "stack overflow";
684 msg = "stack underflow";
686 msg = "invalid operation";
688 snprint(note, sizeof note, "sys: fp: %s fppc=0x%lux status=0x%lux",
690 postnote(up, 1, note, NDebug);
694 * math coprocessor error
697 matherror(Ureg*, void*)
700 * a write cycle to port 0xF0 clears the interrupt latch attached
701 * to the error# line from the 387
703 if(!(m->cpuiddx & Fpuonchip))
707 * get floating point state to check out error
710 up->fpstate = FPinactive;
711 mathnote(up->fpsave.fsw, up->fpsave.fpuip);
718 simderror(Ureg *ureg, void*)
721 up->fpstate = FPinactive;
722 mathnote(up->fpsave.mxcsr & 0x3f, ureg->pc);
726 * math coprocessor emulation fault
729 mathemu(Ureg *ureg, void*)
731 ulong status, control;
733 if(up->fpstate & FPillegal){
734 /* someone did floating point in a note handler */
735 postnote(up, 1, "sys: floating point in note handler", NDebug);
741 if(fpsave == fpssesave)
742 ldmxcsr(0); /* no simd exceptions on 386 */
743 up->fpstate = FPactive;
747 * Before restoring the state, check for any pending
748 * exceptions, there's no way to restore the state without
749 * generating an unmasked exception.
750 * More attention should probably be paid here to the
751 * exception masks and error summary.
753 status = up->fpsave.fsw;
754 control = up->fpsave.fcw;
755 if((status & ~control) & 0x07F){
756 mathnote(status, up->fpsave.fpuip);
759 fprestore(&up->fpsave);
760 up->fpstate = FPactive;
763 panic("math emu pid %ld %s pc 0x%lux",
764 up->pid, up->text, ureg->pc);
770 * math coprocessor segment overrun
773 mathover(Ureg*, void*)
775 pexit("math overrun", 0);
781 trapenable(VectorCERR, matherror, 0, "matherror");
782 if(X86FAMILY(m->cpuidax) == 3)
783 intrenable(IrqIRQ13, matherror, 0, BUSUNKNOWN, "matherror");
784 trapenable(VectorCNA, mathemu, 0, "mathemu");
785 trapenable(VectorCSO, mathover, 0, "mathover");
786 trapenable(VectorSIMD, simderror, 0, "simderror");
790 * set up floating point for a new process
799 p->pcycles = -p->kentry;
801 memset(p->gdt, 0, sizeof(p->gdt));
805 memset(p->dr, 0, sizeof(p->dr));
813 p->kentry = up->kentry;
814 p->pcycles = -p->kentry;
816 /* inherit user descriptors */
817 memmove(p->gdt, up->gdt, sizeof(p->gdt));
819 /* copy local descriptor table */
820 if(up->ldt != nil && up->nldt > 0){
821 p->ldt = smalloc(sizeof(Segdesc) * up->nldt);
822 memmove(p->ldt, up->ldt, sizeof(Segdesc) * up->nldt);
826 /* save floating point state */
828 switch(up->fpstate & ~FPillegal){
831 up->fpstate = FPinactive;
833 p->fpsave = up->fpsave;
834 p->fpstate = FPinactive;
837 /* clear debug registers */
838 memset(p->dr, 0, sizeof(p->dr));
861 * Save the mach dependent part of the process state.
868 /* we could just always putdr7(0) but accessing DR7 might be slow in a VM */
878 if(p->fpstate == FPactive){
879 if(p->state == Moribund)
883 * Fpsave() stores without handling pending
884 * unmasked exeptions. Postnote() can't be called
885 * here as sleep() already has up->rlock, so
886 * the handling of pending exceptions is delayed
887 * until the process runs again and generates an
888 * emulation fault to activate the FPU.
892 p->fpstate = FPinactive;
896 * While this processor is in the scheduler, the process could run
897 * on another processor and exit, returning the page tables to
898 * the free list where they could be reallocated and overwritten.
899 * When this processor eventually has to get an entry from the
900 * trashed page tables it will crash.
902 * If there's only one processor, this can't happen.
903 * You might think it would be a win not to do this in that case,
904 * especially on VMware, but it turns out not to matter.
906 mmuflushtlb(PADDR(m->pdb));
910 reboot(void *entry, void *code, ulong size)
912 void (*f)(ulong, ulong, ulong);
918 * the boot processor is cpu0. execute this function on it
919 * so that the new kernel has the same cpu0. this only matters
920 * because the hardware has a notion of which processor was the
921 * boot processor and we look at it at start up.
923 if (m->machno != 0) {
931 /* turn off buffered serial console */
934 /* shutdown devices */
939 * Modify the machine page table to directly map the low 4MB of memory
940 * This allows the reboot code to turn off the page mapping
943 pdb[PDX(0)] = pdb[PDX(KZERO)];
944 mmuflushtlb(PADDR(pdb));
946 /* setup reboot trampoline function */
947 f = (void*)REBOOTADDR;
948 memmove(f, rebootcode, sizeof(rebootcode));
950 /* off we go - never to return */
952 (*f)((ulong)entry & ~0xF0000000UL, PADDR(code), size);