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 uchar *sp; /* user stack of init proc */
34 extern void (*i8237alloc)(void);
39 extern ulong multibootptr;
47 multiboot = (ulong*)KADDR(multibootptr);
49 if((multiboot[0] & (1<<2)) != 0)
50 strncpy(BOOTLINE, KADDR(multiboot[4]), BOOTLINELEN-1);
53 ep = cp + BOOTARGSLEN-1;
56 if((multiboot[0] & (1<<6)) != 0 && (l = multiboot[11]) >= 24){
57 cp = seprint(cp, ep, "*e820=");
58 m = KADDR(multiboot[12]);
59 while(m[0] >= 20 && m[0] <= l-4){
62 base = ((uvlong)m[0] | (uvlong)m[1]<<32);
63 size = ((uvlong)m[2] | (uvlong)m[3]<<32);
64 cp = seprint(cp, ep, "%.1lux %.16llux %.16llux ",
65 m[4] & 0xF, base, base+size);
67 m = (ulong*)((uintptr)m + m[-1]);
72 /* plan9.ini passed as the first module */
73 if((multiboot[0] & (1<<3)) != 0 && multiboot[5] > 0){
74 m = KADDR(multiboot[6]);
89 char *cp, *line[MAXCONF], *p, *q;
94 * parse configuration args from dos file plan9.ini
96 cp = BOOTARGS; /* where b.com leaves its config */
97 cp[BOOTARGSLEN-1] = 0;
100 * Strip out '\r', change '\t' -> ' '.
103 for(q = cp; *q; q++){
112 n = getfields(cp, line, MAXCONF, 1, "\n");
113 for(i = 0; i < n; i++){
116 cp = strchr(line[i], '=');
120 confname[nconf] = line[i];
131 for(i = 0; i < nconf; i++)
132 if(cistrcmp(confname[i], name) == 0)
150 /* convert to name=value\n format */
159 error("kernel configuration too large");
160 memset(BOOTLINE, 0, BOOTLINELEN);
161 memmove(BOOTARGS, p, n);
173 if(p = getconf("service")){
174 if(strcmp(p, "cpu") == 0)
176 else if(strcmp(p,"terminal") == 0)
180 if(p = getconf("*kernelpercent"))
181 userpcnt = 100 - strtol(p, 0, 0);
186 for(i=0; i<nelem(conf.mem); i++)
187 conf.npage += conf.mem[i].npage;
189 conf.nproc = 100 + ((conf.npage*BY2PG)/MB)*5;
192 if(conf.nproc > 2000)
195 conf.nswap = conf.nproc*80;
201 kpages = conf.npage - (conf.npage*userpcnt)/100;
204 * Hack for the big boys. Only good while physmem < 4GB.
205 * Give the kernel fixed max + enough to allocate the
207 * This is an overestimate as conf.upages < conf.npages.
208 * The patch of nimage is a band-aid, scanning the whole
209 * page list in imagereclaim just takes too long.
211 if(getconf("*imagemaxmb") == 0)
212 if(kpages > (64*MB + conf.npage*sizeof(Page))/BY2PG){
213 kpages = (64*MB + conf.npage*sizeof(Page))/BY2PG;
215 kpages += (conf.nproc*KSTACK)/BY2PG;
219 if(conf.npage*BY2PG < 16*MB)
224 kpages = conf.npage - (conf.npage*userpcnt)/100;
227 * Make sure terminals with low memory get at least
228 * 4MB on the first Image chunk allocation.
230 if(conf.npage*BY2PG < 16*MB)
231 imagmem->minarena = 4*MB;
235 * can't go past the end of virtual memory.
237 if(kpages > ((uintptr)-KZERO)/BY2PG)
238 kpages = ((uintptr)-KZERO)/BY2PG;
240 conf.upages = conf.npage - kpages;
241 conf.ialloc = (kpages/2)*BY2PG;
244 * Guess how much is taken by the large permanent
245 * datastructures. Mntcache and Mntrpc are not accounted for
249 kpages -= conf.upages*sizeof(Page)
250 + conf.nproc*sizeof(Proc)
251 + conf.nimage*sizeof(Image)
253 + conf.nswppo*sizeof(Page*);
254 mainmem->maxsize = kpages;
257 * the dynamic allocation will balance the load properly,
258 * hopefully. be careful with 32-bit overflow.
260 imagmem->maxsize = kpages - (kpages/10);
261 if(p = getconf("*imagemaxmb")){
262 imagmem->maxsize = strtol(p, nil, 0)*MB;
263 if(imagmem->maxsize > mainmem->maxsize)
264 imagmem->maxsize = mainmem->maxsize;
279 memset(m, 0, sizeof(Mach));
286 * For polled uart output at boot, need
287 * a default delay constant. 100000 should
288 * be enough for a while. Cpuidentify will
289 * calculate the real value later.
291 m->loopconst = 100000;
299 MACHP(0) = (Mach*)CPU0MACH;
302 m->pml4 = (u64int*)CPU0PML4;
303 m->gdt = (Segdesc*)CPU0GDT;
309 active.rebooting = 0;
333 sp = (uchar*)base + BY2PG - sizeof(Tos);
336 av[ac++] = pusharg("boot");
338 /* when boot is changed to only use rc, this code can go away */
339 cp[BOOTLINELEN-1] = 0;
341 if(strncmp(cp, "fd", 2) == 0){
342 sprint(buf, "local!#f/fd%lddisk", strtol(cp+2, 0, 0));
343 av[ac++] = pusharg(buf);
344 } else if(strncmp(cp, "sd", 2) == 0){
345 sprint(buf, "local!#S/sd%c%c/fs", *(cp+2), *(cp+3));
346 av[ac++] = pusharg(buf);
347 } else if(strncmp(cp, "ether", 5) == 0)
348 av[ac++] = pusharg("-n");
350 /* 8 byte word align stack */
351 sp = (uchar*)((uintptr)sp & ~7);
353 /* build argc, argv on stack */
354 sp -= (ac+1)*sizeof(sp);
356 for(i = 0; i < ac; i++)
357 lsp[i] = av[i] + ((uintptr)(USTKTOP - BY2PG) - (uintptr)base);
359 sp += (uintptr)(USTKTOP - BY2PG) - (uintptr)base;
367 char buf[2*KNAMELEN];
374 * These are o.k. because rootinit is null.
375 * Then early kproc's will have a root and dot.
377 up->slash = namec("#/", Atodir, 0, 0);
378 pathclose(up->slash->path);
379 up->slash->path = newpath("/");
380 up->dot = cclone(up->slash);
385 snprint(buf, sizeof(buf), "%s %s", arch->id, conffile);
386 ksetenv("terminal", buf, 0);
387 ksetenv("cputype", "amd64", 0);
389 ksetenv("service", "cpu", 0);
391 ksetenv("service", "terminal", 0);
392 for(i = 0; i < nconf; i++){
393 if(confname[i][0] != '*')
394 ksetenv(confname[i], confval[i], 0);
395 ksetenv(confname[i], confval[i], 1);
399 kproc("alarm", alarmkproc, 0);
414 p->egrp = smalloc(sizeof(Egrp));
416 p->fgrp = dupfgrp(nil);
421 kstrdup(&p->text, "*init*");
422 kstrdup(&p->user, eve);
429 * N.B. make sure there's enough space for syscall to check
431 * 8 bytes for gotolabel's return PC
433 p->sched.pc = (uintptr)init0;
434 p->sched.sp = (uintptr)p->kstack+KSTACK-(sizeof(Sargs)+BY2WD);
436 /* temporarily set up for kmap() */
442 s = newseg(SG_STACK, USTKTOP-USTKSIZE, USTKSIZE/BY2PG);
444 pg = newpage(0, 0, USTKTOP-BY2PG);
454 s = newseg(SG_TEXT, UTZERO, 1);
457 pg = newpage(0, 0, UTZERO);
458 memset(pg->cachectl, PG_TXTFLUSH, sizeof(pg->cachectl));
462 memmove(v, initcode, sizeof initcode);
489 if(i8237alloc != nil)
500 if(arch->clockenable)
514 active.thunderbirdsarego = 1;
519 shutdown(int ispanic)
525 active.ispanic = ispanic;
526 else if(m->machno == 0 && (active.machs & (1<<m->machno)) == 0)
528 once = active.machs & (1<<m->machno);
530 * setting exiting will make hzclock() on each processor call exit(0),
531 * which calls shutdown(0) and arch->reset(), which on mp systems is
532 * mpshutdown, from which there is no return: the processor is idled
533 * or initiates a reboot. clearing our bit in machs avoids calling
534 * exit(0) from hzclock() on this processor.
536 active.machs &= ~(1<<m->machno);
541 iprint("cpu%d: exiting\n", m->machno);
543 /* wait for any other processors to shutdown */
545 for(ms = 5*1000; ms > 0; ms -= TK2MS(2)){
547 if(active.machs == 0 && consactive() == 0)
555 if(getconf("*debug"))
570 reboot(void *entry, void *code, ulong size)
572 void (*f)(uintptr, uintptr, ulong);
577 * the boot processor is cpu0. execute this function on it
578 * so that the new kernel has the same cpu0. this only matters
579 * because the hardware has a notion of which processor was the
580 * boot processor and we look at it at start up.
582 if (m->machno != 0) {
588 active.rebooting = 1;
594 * should be the only processor running now
597 iprint("on cpu%d (not 0)!\n", m->machno);
599 iprint("still have active ap processors!\n");
601 iprint("shutting down...\n");
606 /* turn off buffered serial console */
609 /* shutdown devices */
614 * This allows the reboot code to turn off the page mapping
616 *mmuwalk(m->pml4, 0, 3, 0) = *mmuwalk(m->pml4, KZERO, 3, 0);
617 *mmuwalk(m->pml4, 0, 2, 0) = *mmuwalk(m->pml4, KZERO, 2, 0);
620 /* setup reboot trampoline function */
621 f = (void*)REBOOTADDR;
622 memmove(f, rebootcode, sizeof(rebootcode));
624 /* off we go - never to return */
626 (*f)((uintptr)entry & ~0xF0000000UL, (uintptr)PADDR(code), size);
630 * SIMD Floating Point.
631 * Assembler support to get at the individual instructions
633 * There are opportunities to be lazier about saving and
634 * restoring the state and allocating the storage needed.
636 extern void _clts(void);
637 extern void _fldcw(u16int);
638 extern void _fnclex(void);
639 extern void _fninit(void);
640 extern void _fxrstor(Fxsave*);
641 extern void _fxsave(Fxsave*);
642 extern void _fwait(void);
643 extern void _ldmxcsr(u32int);
644 extern void _stts(void);
647 * not used, AMD64 mandated SSE
654 fpx87restore(FPsave*)
659 fpssesave(FPsave *fps)
661 Fxsave *fx = (Fxsave*)ROUND(((uintptr)fps), FPalign);
665 if(fx != (Fxsave*)fps)
666 memmove((Fxsave*)fps, fx, sizeof(Fxsave));
669 fpsserestore(FPsave *fps)
671 Fxsave *fx = (Fxsave*)ROUND(((uintptr)fps), FPalign);
673 if(fx != (Fxsave*)fps)
674 memmove(fx, (Fxsave*)fps, sizeof(Fxsave));
679 static char* mathmsg[] =
681 nil, /* handled below */
682 "denormalized operand",
690 mathnote(ulong status, uintptr pc)
692 char *msg, note[ERRMAX];
696 * Some attention should probably be paid here to the
697 * exception masks and error summary.
699 msg = "unknown exception";
700 for(i = 1; i <= 5; i++){
701 if(!((1<<i) & status))
709 msg = "stack overflow";
711 msg = "stack underflow";
713 msg = "invalid operation";
715 snprint(note, sizeof note, "sys: fp: %s fppc=%#p status=0x%lux",
717 postnote(up, 1, note, NDebug);
721 * math coprocessor error
724 matherror(Ureg*, void*)
727 * Save FPU state to check out the error.
730 up->fpstate = FPinactive;
731 mathnote(up->fpsave.fsw, up->fpsave.rip);
735 * math coprocessor emulation fault
738 mathemu(Ureg *ureg, void*)
740 ulong status, control;
742 if(up->fpstate & FPillegal){
743 /* someone did floating point in a note handler */
744 postnote(up, 1, "sys: floating point in note handler", NDebug);
750 * A process tries to use the FPU for the
751 * first time and generates a 'device not available'
753 * Turn the FPU on and initialise it for use.
754 * Set the precision and mask the exceptions
755 * we don't care about from the generic Mach value.
762 * TODO: sse exceptions
763 * _ldmxcsr(m->mxcsr);
766 up->fpstate = FPactive;
770 * Before restoring the state, check for any pending
771 * exceptions, there's no way to restore the state without
772 * generating an unmasked exception.
773 * More attention should probably be paid here to the
774 * exception masks and error summary.
776 status = up->fpsave.fsw;
777 control = up->fpsave.fcw;
778 if((status & ~control) & 0x07F){
779 mathnote(status, up->fpsave.rip);
782 fprestore(&up->fpsave);
783 up->fpstate = FPactive;
786 panic("math emu pid %ld %s pc %#p",
787 up->pid, up->text, ureg->pc);
793 * math coprocessor segment overrun
796 mathover(Ureg*, void*)
798 pexit("math overrun", 0);
804 trapenable(VectorCERR, matherror, 0, "matherror");
805 if(X86FAMILY(m->cpuidax) == 3)
806 intrenable(IrqIRQ13, matherror, 0, BUSUNKNOWN, "matherror");
807 trapenable(VectorCNA, mathemu, 0, "mathemu");
808 trapenable(VectorCSO, mathover, 0, "mathover");
817 p->pcycles = -p->kentry;
825 p->kentry = up->kentry;
826 p->pcycles = -p->kentry;
828 /* save floating point state */
830 switch(up->fpstate & ~FPillegal){
833 up->fpstate = FPinactive;
835 p->fpsave = up->fpsave;
836 p->fpstate = FPinactive;
864 if(p->fpstate == FPactive){
865 if(p->state == Moribund){
872 * Fpsave() stores without handling pending
873 * unmasked exeptions. Postnote() can't be called
874 * here as sleep() already has up->rlock, so
875 * the handling of pending exceptions is delayed
876 * until the process runs again and generates an
877 * emulation fault to activate the FPU.
881 p->fpstate = FPinactive;
885 * While this processor is in the scheduler, the process could run
886 * on another processor and exit, returning the page tables to
887 * the free list where they could be reallocated and overwritten.
888 * When this processor eventually has to get an entry from the
889 * trashed page tables it will crash.
891 * If there's only one processor, this can't happen.
892 * You might think it would be a win not to do this in that case,
893 * especially on VMware, but it turns out not to matter.