2 #include "../port/lib.h"
14 * Where configuration info is left for the loaded programme.
15 * This will turn into a structure as more is done by the boot loader
16 * (e.g. why parse the .ini file twice?).
17 * There are 3584 bytes available at CONFADDR.
19 #define BOOTLINE ((char*)CONFADDR)
20 #define BOOTLINELEN 64
21 #define BOOTARGS ((char*)(CONFADDR+BOOTLINELEN))
22 #define BOOTARGSLEN (4096-0x200-BOOTLINELEN)
26 char *confname[MAXCONF];
27 char *confval[MAXCONF];
32 char *sp; /* user stack of init proc */
34 extern void (*i8237alloc)(void);
35 extern void bootscreeninit(void);
40 extern ulong multibootptr;
48 multiboot = (ulong*)KADDR(multibootptr);
50 if((multiboot[0] & (1<<2)) != 0)
51 strncpy(BOOTLINE, KADDR(multiboot[4]), BOOTLINELEN-1);
54 ep = cp + BOOTARGSLEN-1;
57 if((multiboot[0] & (1<<6)) != 0 && (l = multiboot[11]) >= 24){
58 cp = seprint(cp, ep, "*e820=");
59 m = KADDR(multiboot[12]);
60 while(m[0] >= 20 && m[0] <= l-4){
63 base = ((uvlong)m[0] | (uvlong)m[1]<<32);
64 size = ((uvlong)m[2] | (uvlong)m[3]<<32);
65 cp = seprint(cp, ep, "%.1lux %.16llux %.16llux ",
66 m[4] & 0xF, base, base+size);
68 m = (ulong*)((uintptr)m + m[-1]);
73 /* plan9.ini passed as the first module */
74 if((multiboot[0] & (1<<3)) != 0 && multiboot[5] > 0){
75 m = KADDR(multiboot[6]);
90 char *cp, *line[MAXCONF], *p, *q;
95 * parse configuration args from dos file plan9.ini
97 cp = BOOTARGS; /* where b.com leaves its config */
98 cp[BOOTARGSLEN-1] = 0;
101 * Strip out '\r', change '\t' -> ' '.
104 for(q = cp; *q; q++){
113 n = getfields(cp, line, MAXCONF, 1, "\n");
114 for(i = 0; i < n; i++){
117 cp = strchr(line[i], '=');
121 confname[nconf] = line[i];
132 for(i = 0; i < nconf; i++)
133 if(cistrcmp(confname[i], name) == 0)
151 /* convert to name=value\n format */
160 error("kernel configuration too large");
161 memset(BOOTLINE, 0, BOOTLINELEN);
162 memmove(BOOTARGS, p, n);
174 if(p = getconf("service")){
175 if(strcmp(p, "cpu") == 0)
177 else if(strcmp(p,"terminal") == 0)
181 if(p = getconf("*kernelpercent"))
182 userpcnt = 100 - strtol(p, 0, 0);
187 for(i=0; i<nelem(conf.mem); i++)
188 conf.npage += conf.mem[i].npage;
190 conf.nproc = 100 + ((conf.npage*BY2PG)/MB)*5;
193 if(conf.nproc > 2000)
196 conf.nswap = conf.nproc*80;
202 kpages = conf.npage - (conf.npage*userpcnt)/100;
203 conf.nimage = conf.nproc;
206 if(conf.npage*BY2PG < 16*MB)
211 kpages = conf.npage - (conf.npage*userpcnt)/100;
214 * Make sure terminals with low memory get at least
215 * 4MB on the first Image chunk allocation.
217 if(conf.npage*BY2PG < 16*MB)
218 imagmem->minarena = 4*MB;
222 * can't go past the end of virtual memory.
224 if(kpages > ((uintptr)-KZERO)/BY2PG)
225 kpages = ((uintptr)-KZERO)/BY2PG;
227 conf.upages = conf.npage - kpages;
228 conf.ialloc = (kpages/2)*BY2PG;
231 * Guess how much is taken by the large permanent
232 * datastructures. Mntcache and Mntrpc are not accounted for.
235 kpages -= conf.nproc*sizeof(Proc)
236 + conf.nimage*sizeof(Image)
238 + conf.nswppo*sizeof(Page*);
239 mainmem->maxsize = kpages;
242 * the dynamic allocation will balance the load properly,
243 * hopefully. be careful with 32-bit overflow.
245 imagmem->maxsize = kpages - (kpages/10);
246 if(p = getconf("*imagemaxmb")){
247 imagmem->maxsize = strtol(p, nil, 0)*MB;
248 if(imagmem->maxsize > mainmem->maxsize)
249 imagmem->maxsize = mainmem->maxsize;
254 * The palloc.pages array can be a large chunk out of the 2GB
255 * window above KZERO, so we allocate the array from
256 * upages and map in the VMAP window before pageinit()
262 uintptr va, base, top;
268 for(i=0; i<nelem(palloc.mem); i++){
272 size = (uvlong)np * BY2PG;
273 size += sizeof(Page) + BY2PG; /* round up */
274 size = (size / (sizeof(Page) + BY2PG)) * sizeof(Page);
275 size = ROUND(size, PGLSZ(1));
277 for(i=0; i<nelem(palloc.mem); i++){
279 base = ROUND(pm->base, PGLSZ(1));
280 top = pm->base + (uvlong)pm->npage * BY2PG;
281 if((base + size) <= VMAPSIZE && (vlong)(top - base) >= size){
283 pmap(m->pml4, base | PTEGLOBAL|PTEWRITE|PTEVALID, va, size);
284 palloc.pages = (Page*)va;
285 pm->base = base + size;
286 pm->npage = (top - pm->base)/BY2PG;
302 memset(m, 0, sizeof(Mach));
309 * For polled uart output at boot, need
310 * a default delay constant. 100000 should
311 * be enough for a while. Cpuidentify will
312 * calculate the real value later.
314 m->loopconst = 100000;
322 MACHP(0) = (Mach*)CPU0MACH;
325 m->pml4 = (u64int*)CPU0PML4;
326 m->gdt = (Segdesc*)CPU0GDT;
340 #define UA(ka) ((char*)(ka) + ((uintptr)(USTKTOP - BY2PG) - (uintptr)base))
341 sp = (char*)base + BY2PG - sizeof(Tos);
343 /* push boot command line onto the stack */
345 sp[BOOTLINELEN-1] = '\0';
346 memmove(sp, BOOTLINE, BOOTLINELEN-1);
348 /* parse boot command line */
349 argc = tokenize(sp, argv, nelem(argv));
356 /* 8 byte word align stack */
357 sp = (char*)((uintptr)sp & ~7);
359 /* build argv on stack */
360 sp -= (argc+1)*BY2WD;
361 for(i=0; i<argc; i++)
362 ((char**)sp)[i] = UA(argv[i]);
363 ((char**)sp)[i] = nil;
374 char buf[2*KNAMELEN];
381 * These are o.k. because rootinit is null.
382 * Then early kproc's will have a root and dot.
384 up->slash = namec("#/", Atodir, 0, 0);
385 pathclose(up->slash->path);
386 up->slash->path = newpath("/");
387 up->dot = cclone(up->slash);
392 snprint(buf, sizeof(buf), "%s %s", arch->id, conffile);
393 ksetenv("terminal", buf, 0);
394 ksetenv("cputype", "amd64", 0);
396 ksetenv("service", "cpu", 0);
398 ksetenv("service", "terminal", 0);
399 for(i = 0; i < nconf; i++){
400 if(confname[i][0] != '*')
401 ksetenv(confname[i], confval[i], 0);
402 ksetenv(confname[i], confval[i], 1);
406 kproc("alarm", alarmkproc, 0);
421 p->egrp = smalloc(sizeof(Egrp));
423 p->fgrp = dupfgrp(nil);
428 kstrdup(&p->text, "*init*");
429 kstrdup(&p->user, eve);
436 * N.B. make sure there's enough space for syscall to check
438 * 8 bytes for gotolabel's return PC
440 p->sched.pc = (uintptr)init0;
441 p->sched.sp = (uintptr)p->kstack+KSTACK-(sizeof(Sargs)+BY2WD);
443 /* temporarily set up for kmap() */
449 s = newseg(SG_STACK, USTKTOP-USTKSIZE, USTKSIZE/BY2PG);
451 pg = newpage(0, 0, USTKTOP-BY2PG);
461 s = newseg(SG_TEXT, UTZERO, 1);
464 pg = newpage(0, 0, UTZERO);
469 memmove(v, initcode, sizeof initcode);
497 if(i8237alloc != nil)
507 if(arch->clockenable)
532 reboot(void *entry, void *code, ulong size)
534 void (*f)(uintptr, uintptr, ulong);
539 * the boot processor is cpu0. execute this function on it
540 * so that the new kernel has the same cpu0. this only matters
541 * because the hardware has a notion of which processor was the
542 * boot processor and we look at it at start up.
544 if (m->machno != 0) {
552 /* turn off buffered serial console */
555 /* shutdown devices */
560 * This allows the reboot code to turn off the page mapping
562 *mmuwalk(m->pml4, 0, 3, 0) = *mmuwalk(m->pml4, KZERO, 3, 0);
563 *mmuwalk(m->pml4, 0, 2, 0) = *mmuwalk(m->pml4, KZERO, 2, 0);
566 /* setup reboot trampoline function */
567 f = (void*)REBOOTADDR;
568 memmove(f, rebootcode, sizeof(rebootcode));
570 /* off we go - never to return */
572 (*f)((uintptr)entry & ~0xF0000000UL, (uintptr)PADDR(code), size);
576 * SIMD Floating Point.
577 * Assembler support to get at the individual instructions
579 * There are opportunities to be lazier about saving and
580 * restoring the state and allocating the storage needed.
582 extern void _clts(void);
583 extern void _fldcw(u16int);
584 extern void _fnclex(void);
585 extern void _fninit(void);
586 extern void _fxrstor(Fxsave*);
587 extern void _fxsave(Fxsave*);
588 extern void _fwait(void);
589 extern void _ldmxcsr(u32int);
590 extern void _stts(void);
593 * not used, AMD64 mandated SSE
600 fpx87restore(FPsave*)
605 fpssesave(FPsave *fps)
607 Fxsave *fx = (Fxsave*)ROUND(((uintptr)fps), FPalign);
611 if(fx != (Fxsave*)fps)
612 memmove((Fxsave*)fps, fx, sizeof(Fxsave));
615 fpsserestore(FPsave *fps)
617 Fxsave *fx = (Fxsave*)ROUND(((uintptr)fps), FPalign);
619 if(fx != (Fxsave*)fps)
620 memmove(fx, (Fxsave*)fps, sizeof(Fxsave));
625 static char* mathmsg[] =
627 nil, /* handled below */
628 "denormalized operand",
636 mathnote(ulong status, uintptr pc)
638 char *msg, note[ERRMAX];
642 * Some attention should probably be paid here to the
643 * exception masks and error summary.
645 msg = "unknown exception";
646 for(i = 1; i <= 5; i++){
647 if(!((1<<i) & status))
655 msg = "stack overflow";
657 msg = "stack underflow";
659 msg = "invalid operation";
661 snprint(note, sizeof note, "sys: fp: %s fppc=%#p status=0x%lux",
663 postnote(up, 1, note, NDebug);
667 * math coprocessor error
670 matherror(Ureg*, void*)
673 * Save FPU state to check out the error.
676 up->fpstate = FPinactive;
677 mathnote(up->fpsave.fsw, up->fpsave.rip);
684 simderror(Ureg *ureg, void*)
687 up->fpstate = FPinactive;
688 mathnote(up->fpsave.mxcsr & 0x3f, ureg->pc);
692 * math coprocessor emulation fault
695 mathemu(Ureg *ureg, void*)
697 ulong status, control;
699 if(up->fpstate & FPillegal){
700 /* someone did floating point in a note handler */
701 postnote(up, 1, "sys: floating point in note handler", NDebug);
707 * A process tries to use the FPU for the
708 * first time and generates a 'device not available'
710 * Turn the FPU on and initialise it for use.
711 * Set the precision and mask the exceptions
712 * we don't care about from the generic Mach value.
719 up->fpstate = FPactive;
723 * Before restoring the state, check for any pending
724 * exceptions, there's no way to restore the state without
725 * generating an unmasked exception.
726 * More attention should probably be paid here to the
727 * exception masks and error summary.
729 status = up->fpsave.fsw;
730 control = up->fpsave.fcw;
731 if((status & ~control) & 0x07F){
732 mathnote(status, up->fpsave.rip);
735 fprestore(&up->fpsave);
736 up->fpstate = FPactive;
739 panic("math emu pid %ld %s pc %#p",
740 up->pid, up->text, ureg->pc);
746 * math coprocessor segment overrun
749 mathover(Ureg*, void*)
751 pexit("math overrun", 0);
757 trapenable(VectorCERR, matherror, 0, "matherror");
758 if(X86FAMILY(m->cpuidax) == 3)
759 intrenable(IrqIRQ13, matherror, 0, BUSUNKNOWN, "matherror");
760 trapenable(VectorCNA, mathemu, 0, "mathemu");
761 trapenable(VectorCSO, mathover, 0, "mathover");
762 trapenable(VectorSIMD, simderror, 0, "simderror");
771 p->pcycles = -p->kentry;
779 p->kentry = up->kentry;
780 p->pcycles = -p->kentry;
782 /* save floating point state */
784 switch(up->fpstate & ~FPillegal){
787 up->fpstate = FPinactive;
789 p->fpsave = up->fpsave;
790 p->fpstate = FPinactive;
818 if(p->fpstate == FPactive){
819 if(p->state == Moribund){
826 * Fpsave() stores without handling pending
827 * unmasked exeptions. Postnote() can't be called
828 * here as sleep() already has up->rlock, so
829 * the handling of pending exceptions is delayed
830 * until the process runs again and generates an
831 * emulation fault to activate the FPU.
835 p->fpstate = FPinactive;
839 * While this processor is in the scheduler, the process could run
840 * on another processor and exit, returning the page tables to
841 * the free list where they could be reallocated and overwritten.
842 * When this processor eventually has to get an entry from the
843 * trashed page tables it will crash.
845 * If there's only one processor, this can't happen.
846 * You might think it would be a win not to do this in that case,
847 * especially on VMware, but it turns out not to matter.