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]+4 <= l){
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)
533 reboot(void *entry, void *code, ulong size)
535 void (*f)(uintptr, uintptr, ulong);
540 * the boot processor is cpu0. execute this function on it
541 * so that the new kernel has the same cpu0. this only matters
542 * because the hardware has a notion of which processor was the
543 * boot processor and we look at it at start up.
545 if (m->machno != 0) {
553 /* turn off buffered serial console */
556 /* shutdown devices */
561 * This allows the reboot code to turn off the page mapping
563 *mmuwalk(m->pml4, 0, 3, 0) = *mmuwalk(m->pml4, KZERO, 3, 0);
564 *mmuwalk(m->pml4, 0, 2, 0) = *mmuwalk(m->pml4, KZERO, 2, 0);
567 /* setup reboot trampoline function */
568 f = (void*)REBOOTADDR;
569 memmove(f, rebootcode, sizeof(rebootcode));
571 /* off we go - never to return */
573 (*f)((uintptr)entry & ~0xF0000000UL, (uintptr)PADDR(code), size);
577 * SIMD Floating Point.
578 * Assembler support to get at the individual instructions
580 * There are opportunities to be lazier about saving and
581 * restoring the state and allocating the storage needed.
583 extern void _clts(void);
584 extern void _fldcw(u16int);
585 extern void _fnclex(void);
586 extern void _fninit(void);
587 extern void _fxrstor(Fxsave*);
588 extern void _fxsave(Fxsave*);
589 extern void _fwait(void);
590 extern void _ldmxcsr(u32int);
591 extern void _stts(void);
594 * not used, AMD64 mandated SSE
601 fpx87restore(FPsave*)
606 fpssesave(FPsave *fps)
608 Fxsave *fx = (Fxsave*)ROUND(((uintptr)fps), FPalign);
612 if(fx != (Fxsave*)fps)
613 memmove((Fxsave*)fps, fx, sizeof(Fxsave));
616 fpsserestore(FPsave *fps)
618 Fxsave *fx = (Fxsave*)ROUND(((uintptr)fps), FPalign);
620 if(fx != (Fxsave*)fps)
621 memmove(fx, (Fxsave*)fps, sizeof(Fxsave));
626 static char* mathmsg[] =
628 nil, /* handled below */
629 "denormalized operand",
637 mathnote(ulong status, uintptr pc)
639 char *msg, note[ERRMAX];
643 * Some attention should probably be paid here to the
644 * exception masks and error summary.
646 msg = "unknown exception";
647 for(i = 1; i <= 5; i++){
648 if(!((1<<i) & status))
656 msg = "stack overflow";
658 msg = "stack underflow";
660 msg = "invalid operation";
662 snprint(note, sizeof note, "sys: fp: %s fppc=%#p status=0x%lux",
664 postnote(up, 1, note, NDebug);
668 * math coprocessor error
671 matherror(Ureg*, void*)
674 * Save FPU state to check out the error.
677 up->fpstate = FPinactive;
678 mathnote(up->fpsave.fsw, up->fpsave.rip);
685 simderror(Ureg *ureg, void*)
688 up->fpstate = FPinactive;
689 mathnote(up->fpsave.mxcsr & 0x3f, ureg->pc);
696 * A process tries to use the FPU for the
697 * first time and generates a 'device not available'
699 * Turn the FPU on and initialise it for use.
700 * Set the precision and mask the exceptions
701 * we don't care about from the generic Mach value.
711 * math coprocessor emulation fault
714 mathemu(Ureg *ureg, void*)
716 ulong status, control;
718 if(up->fpstate & FPillegal){
719 /* someone did floating point in a note handler */
720 postnote(up, 1, "sys: floating point in note handler", NDebug);
726 up->fpstate = FPactive;
730 * Before restoring the state, check for any pending
731 * exceptions, there's no way to restore the state without
732 * generating an unmasked exception.
733 * More attention should probably be paid here to the
734 * exception masks and error summary.
736 status = up->fpsave.fsw;
737 control = up->fpsave.fcw;
738 if((status & ~control) & 0x07F){
739 mathnote(status, up->fpsave.rip);
742 fprestore(&up->fpsave);
743 up->fpstate = FPactive;
746 panic("math emu pid %ld %s pc %#p",
747 up->pid, up->text, ureg->pc);
753 * math coprocessor segment overrun
756 mathover(Ureg*, void*)
758 pexit("math overrun", 0);
764 trapenable(VectorCERR, matherror, 0, "matherror");
765 if(X86FAMILY(m->cpuidax) == 3)
766 intrenable(IrqIRQ13, matherror, 0, BUSUNKNOWN, "matherror");
767 trapenable(VectorCNA, mathemu, 0, "mathemu");
768 trapenable(VectorCSO, mathover, 0, "mathover");
769 trapenable(VectorSIMD, simderror, 0, "simderror");
778 p->pcycles = -p->kentry;
786 p->kentry = up->kentry;
787 p->pcycles = -p->kentry;
789 /* save floating point state */
791 switch(up->fpstate & ~FPillegal){
794 up->fpstate = FPinactive;
796 p->fpsave = up->fpsave;
797 p->fpstate = FPinactive;
835 if(p->fpstate == FPactive){
836 if(p->state == Moribund){
843 * Fpsave() stores without handling pending
844 * unmasked exeptions. Postnote() can't be called
845 * here as sleep() already has up->rlock, so
846 * the handling of pending exceptions is delayed
847 * until the process runs again and generates an
848 * emulation fault to activate the FPU.
852 p->fpstate = FPinactive;
856 * While this processor is in the scheduler, the process could run
857 * on another processor and exit, returning the page tables to
858 * the free list where they could be reallocated and overwritten.
859 * When this processor eventually has to get an entry from the
860 * trashed page tables it will crash.
862 * If there's only one processor, this can't happen.
863 * You might think it would be a win not to do this in that case,
864 * especially on VMware, but it turns out not to matter.