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;
205 if(conf.npage*BY2PG < 16*MB)
210 kpages = conf.npage - (conf.npage*userpcnt)/100;
213 * Make sure terminals with low memory get at least
214 * 4MB on the first Image chunk allocation.
216 if(conf.npage*BY2PG < 16*MB)
217 imagmem->minarena = 4*MB;
221 * can't go past the end of virtual memory.
223 if(kpages > ((uintptr)-KZERO)/BY2PG)
224 kpages = ((uintptr)-KZERO)/BY2PG;
226 conf.upages = conf.npage - kpages;
227 conf.ialloc = (kpages/2)*BY2PG;
230 * Guess how much is taken by the large permanent
231 * datastructures. Mntcache and Mntrpc are not accounted for.
234 kpages -= conf.nproc*sizeof(Proc)
235 + conf.nimage*sizeof(Image)
237 + conf.nswppo*sizeof(Page*);
238 mainmem->maxsize = kpages;
241 * the dynamic allocation will balance the load properly,
242 * hopefully. be careful with 32-bit overflow.
244 imagmem->maxsize = kpages - (kpages/10);
245 if(p = getconf("*imagemaxmb")){
246 imagmem->maxsize = strtol(p, nil, 0)*MB;
247 if(imagmem->maxsize > mainmem->maxsize)
248 imagmem->maxsize = mainmem->maxsize;
253 * The palloc.pages array can be a large chunk out of the 2GB
254 * window above KZERO, so we allocate the array from
255 * upages and map in the VMAP window before pageinit()
261 uintptr va, base, top;
267 for(i=0; i<nelem(palloc.mem); i++){
271 size = (uvlong)np * BY2PG;
272 size += sizeof(Page) + BY2PG; /* round up */
273 size = (size / (sizeof(Page) + BY2PG)) * sizeof(Page);
274 size = ROUND(size, PGLSZ(1));
276 for(i=0; i<nelem(palloc.mem); i++){
278 base = ROUND(pm->base, PGLSZ(1));
279 top = pm->base + (uvlong)pm->npage * BY2PG;
280 if((base + size) <= VMAPSIZE && (vlong)(top - base) >= size){
282 pmap(m->pml4, base | PTEGLOBAL|PTEWRITE|PTEVALID, va, size);
283 palloc.pages = (Page*)va;
284 pm->base = base + size;
285 pm->npage = (top - pm->base)/BY2PG;
301 memset(m, 0, sizeof(Mach));
308 * For polled uart output at boot, need
309 * a default delay constant. 100000 should
310 * be enough for a while. Cpuidentify will
311 * calculate the real value later.
313 m->loopconst = 100000;
321 MACHP(0) = (Mach*)CPU0MACH;
324 m->pml4 = (u64int*)CPU0PML4;
325 m->gdt = (Segdesc*)CPU0GDT;
339 #define UA(ka) ((char*)(ka) + ((uintptr)(USTKTOP - BY2PG) - (uintptr)base))
340 sp = (char*)base + BY2PG - sizeof(Tos);
342 /* push boot command line onto the stack */
344 sp[BOOTLINELEN-1] = '\0';
345 memmove(sp, BOOTLINE, BOOTLINELEN-1);
347 /* parse boot command line */
348 argc = tokenize(sp, argv, nelem(argv));
355 /* 8 byte word align stack */
356 sp = (char*)((uintptr)sp & ~7);
358 /* build argv on stack */
359 sp -= (argc+1)*BY2WD;
360 for(i=0; i<argc; i++)
361 ((char**)sp)[i] = UA(argv[i]);
362 ((char**)sp)[i] = nil;
373 char buf[2*KNAMELEN];
380 * These are o.k. because rootinit is null.
381 * Then early kproc's will have a root and dot.
383 up->slash = namec("#/", Atodir, 0, 0);
384 pathclose(up->slash->path);
385 up->slash->path = newpath("/");
386 up->dot = cclone(up->slash);
391 snprint(buf, sizeof(buf), "%s %s", arch->id, conffile);
392 ksetenv("terminal", buf, 0);
393 ksetenv("cputype", "amd64", 0);
395 ksetenv("service", "cpu", 0);
397 ksetenv("service", "terminal", 0);
398 for(i = 0; i < nconf; i++){
399 if(confname[i][0] != '*')
400 ksetenv(confname[i], confval[i], 0);
401 ksetenv(confname[i], confval[i], 1);
405 kproc("alarm", alarmkproc, 0);
420 p->egrp = smalloc(sizeof(Egrp));
422 p->fgrp = dupfgrp(nil);
427 kstrdup(&p->text, "*init*");
428 kstrdup(&p->user, eve);
435 * N.B. make sure there's enough space for syscall to check
437 * 8 bytes for gotolabel's return PC
439 p->sched.pc = (uintptr)init0;
440 p->sched.sp = (uintptr)p->kstack+KSTACK-(sizeof(Sargs)+BY2WD);
442 /* temporarily set up for kmap() */
448 s = newseg(SG_STACK, USTKTOP-USTKSIZE, USTKSIZE/BY2PG);
450 pg = newpage(0, 0, USTKTOP-BY2PG);
460 s = newseg(SG_TEXT, UTZERO, 1);
463 pg = newpage(0, 0, UTZERO);
468 memmove(v, initcode, sizeof initcode);
497 if(i8237alloc != nil)
508 if(arch->clockenable)
523 active.thunderbirdsarego = 1;
528 shutdown(int ispanic)
534 active.ispanic = ispanic;
535 else if(m->machno == 0 && (active.machs & (1<<m->machno)) == 0)
537 once = active.machs & (1<<m->machno);
539 * setting exiting will make hzclock() on each processor call exit(0),
540 * which calls shutdown(0) and arch->reset(), which on mp systems calls
541 * mpshutdown(), from which there is no return: the processor is idled
542 * or initiates a reboot. clearing our bit in machs avoids calling
543 * exit(0) from hzclock() on this processor.
545 active.machs &= ~(1<<m->machno);
550 iprint("cpu%d: exiting\n", m->machno);
552 /* wait for any other processors to shutdown */
554 for(ms = 5*1000; ms > 0; ms -= TK2MS(2)){
556 if(active.machs == 0 && consactive() == 0)
564 if(getconf("*debug"))
579 reboot(void *entry, void *code, ulong size)
581 void (*f)(uintptr, uintptr, ulong);
586 * the boot processor is cpu0. execute this function on it
587 * so that the new kernel has the same cpu0. this only matters
588 * because the hardware has a notion of which processor was the
589 * boot processor and we look at it at start up.
591 if (m->machno != 0) {
597 iprint("shutting down...\n");
602 /* turn off buffered serial console */
605 /* shutdown devices */
610 * This allows the reboot code to turn off the page mapping
612 *mmuwalk(m->pml4, 0, 3, 0) = *mmuwalk(m->pml4, KZERO, 3, 0);
613 *mmuwalk(m->pml4, 0, 2, 0) = *mmuwalk(m->pml4, KZERO, 2, 0);
616 /* setup reboot trampoline function */
617 f = (void*)REBOOTADDR;
618 memmove(f, rebootcode, sizeof(rebootcode));
620 /* off we go - never to return */
622 (*f)((uintptr)entry & ~0xF0000000UL, (uintptr)PADDR(code), size);
626 * SIMD Floating Point.
627 * Assembler support to get at the individual instructions
629 * There are opportunities to be lazier about saving and
630 * restoring the state and allocating the storage needed.
632 extern void _clts(void);
633 extern void _fldcw(u16int);
634 extern void _fnclex(void);
635 extern void _fninit(void);
636 extern void _fxrstor(Fxsave*);
637 extern void _fxsave(Fxsave*);
638 extern void _fwait(void);
639 extern void _ldmxcsr(u32int);
640 extern void _stts(void);
643 * not used, AMD64 mandated SSE
650 fpx87restore(FPsave*)
655 fpssesave(FPsave *fps)
657 Fxsave *fx = (Fxsave*)ROUND(((uintptr)fps), FPalign);
661 if(fx != (Fxsave*)fps)
662 memmove((Fxsave*)fps, fx, sizeof(Fxsave));
665 fpsserestore(FPsave *fps)
667 Fxsave *fx = (Fxsave*)ROUND(((uintptr)fps), FPalign);
669 if(fx != (Fxsave*)fps)
670 memmove(fx, (Fxsave*)fps, sizeof(Fxsave));
675 static char* mathmsg[] =
677 nil, /* handled below */
678 "denormalized operand",
686 mathnote(ulong status, uintptr pc)
688 char *msg, note[ERRMAX];
692 * Some attention should probably be paid here to the
693 * exception masks and error summary.
695 msg = "unknown exception";
696 for(i = 1; i <= 5; i++){
697 if(!((1<<i) & status))
705 msg = "stack overflow";
707 msg = "stack underflow";
709 msg = "invalid operation";
711 snprint(note, sizeof note, "sys: fp: %s fppc=%#p status=0x%lux",
713 postnote(up, 1, note, NDebug);
717 * math coprocessor error
720 matherror(Ureg*, void*)
723 * Save FPU state to check out the error.
726 up->fpstate = FPinactive;
727 mathnote(up->fpsave.fsw, up->fpsave.rip);
734 simderror(Ureg *ureg, void*)
737 up->fpstate = FPinactive;
738 mathnote(up->fpsave.mxcsr & 0x3f, ureg->pc);
742 * math coprocessor emulation fault
745 mathemu(Ureg *ureg, void*)
747 ulong status, control;
749 if(up->fpstate & FPillegal){
750 /* someone did floating point in a note handler */
751 postnote(up, 1, "sys: floating point in note handler", NDebug);
757 * A process tries to use the FPU for the
758 * first time and generates a 'device not available'
760 * Turn the FPU on and initialise it for use.
761 * Set the precision and mask the exceptions
762 * we don't care about from the generic Mach value.
769 up->fpstate = FPactive;
773 * Before restoring the state, check for any pending
774 * exceptions, there's no way to restore the state without
775 * generating an unmasked exception.
776 * More attention should probably be paid here to the
777 * exception masks and error summary.
779 status = up->fpsave.fsw;
780 control = up->fpsave.fcw;
781 if((status & ~control) & 0x07F){
782 mathnote(status, up->fpsave.rip);
785 fprestore(&up->fpsave);
786 up->fpstate = FPactive;
789 panic("math emu pid %ld %s pc %#p",
790 up->pid, up->text, ureg->pc);
796 * math coprocessor segment overrun
799 mathover(Ureg*, void*)
801 pexit("math overrun", 0);
807 trapenable(VectorCERR, matherror, 0, "matherror");
808 if(X86FAMILY(m->cpuidax) == 3)
809 intrenable(IrqIRQ13, matherror, 0, BUSUNKNOWN, "matherror");
810 trapenable(VectorCNA, mathemu, 0, "mathemu");
811 trapenable(VectorCSO, mathover, 0, "mathover");
812 trapenable(VectorSIMD, simderror, 0, "simderror");
821 p->pcycles = -p->kentry;
829 p->kentry = up->kentry;
830 p->pcycles = -p->kentry;
832 /* save floating point state */
834 switch(up->fpstate & ~FPillegal){
837 up->fpstate = FPinactive;
839 p->fpsave = up->fpsave;
840 p->fpstate = FPinactive;
868 if(p->fpstate == FPactive){
869 if(p->state == Moribund){
876 * Fpsave() stores without handling pending
877 * unmasked exeptions. Postnote() can't be called
878 * here as sleep() already has up->rlock, so
879 * the handling of pending exceptions is delayed
880 * until the process runs again and generates an
881 * emulation fault to activate the FPU.
885 p->fpstate = FPinactive;
889 * While this processor is in the scheduler, the process could run
890 * on another processor and exit, returning the page tables to
891 * the free list where they could be reallocated and overwritten.
892 * When this processor eventually has to get an entry from the
893 * trashed page tables it will crash.
895 * If there's only one processor, this can't happen.
896 * You might think it would be a win not to do this in that case,
897 * especially on VMware, but it turns out not to matter.