2 #include "../port/lib.h"
13 * Where configuration info is left for the loaded programme.
14 * This will turn into a structure as more is done by the boot loader
15 * (e.g. why parse the .ini file twice?).
16 * There are 3584 bytes available at CONFADDR.
18 #define BOOTLINE ((char*)CONFADDR)
19 #define BOOTLINELEN 64
20 #define BOOTARGS ((char*)(CONFADDR+BOOTLINELEN))
21 #define BOOTARGSLEN (4096-0x200-BOOTLINELEN)
25 char *confname[MAXCONF];
26 char *confval[MAXCONF];
31 uchar *sp; /* user stack of init proc */
33 extern void (*i8237alloc)(void);
38 extern ulong multibootptr;
46 multiboot = (ulong*)KADDR(multibootptr);
48 if((multiboot[0] & (1<<2)) != 0)
49 strncpy(BOOTLINE, KADDR(multiboot[4]), BOOTLINELEN-1);
52 ep = cp + BOOTARGSLEN-1;
55 if((multiboot[0] & (1<<6)) != 0 && (l = multiboot[11]) >= 24){
56 cp = seprint(cp, ep, "*e820=");
57 m = KADDR(multiboot[12]);
58 while(m[0] >= 20 && m[0] <= l-4){
61 base = ((uvlong)m[0] | (uvlong)m[1]<<32);
62 size = ((uvlong)m[2] | (uvlong)m[3]<<32);
63 cp = seprint(cp, ep, "%.1lux %.16llux %.16llux ",
64 m[4] & 0xF, base, base+size);
66 m = (ulong*)((uintptr)m + m[-1]);
71 /* plan9.ini passed as the first module */
72 if((multiboot[0] & (1<<3)) != 0 && multiboot[5] > 0){
73 m = KADDR(multiboot[6]);
88 char *cp, *line[MAXCONF], *p, *q;
93 * parse configuration args from dos file plan9.ini
95 cp = BOOTARGS; /* where b.com leaves its config */
96 cp[BOOTARGSLEN-1] = 0;
99 * Strip out '\r', change '\t' -> ' '.
102 for(q = cp; *q; q++){
111 n = getfields(cp, line, MAXCONF, 1, "\n");
112 for(i = 0; i < n; i++){
115 cp = strchr(line[i], '=');
119 confname[nconf] = line[i];
130 for(i = 0; i < nconf; i++)
131 if(cistrcmp(confname[i], name) == 0)
143 if(p = getconf("*kernelpercent"))
144 userpcnt = 100 - strtol(p, 0, 0);
149 for(i=0; i<nelem(conf.mem); i++)
150 conf.npage += conf.mem[i].npage;
152 conf.nproc = 100 + ((conf.npage*BY2PG)/MB)*5;
155 if(conf.nproc > 2000)
158 conf.nswap = conf.nproc*80;
164 kpages = conf.npage - (conf.npage*userpcnt)/100;
167 * Hack for the big boys. Only good while physmem < 4GB.
168 * Give the kernel fixed max + enough to allocate the
170 * This is an overestimate as conf.upages < conf.npages.
171 * The patch of nimage is a band-aid, scanning the whole
172 * page list in imagereclaim just takes too long.
174 if(getconf("*imagemaxmb") == 0)
175 if(kpages > (64*MB + conf.npage*sizeof(Page))/BY2PG){
176 kpages = (64*MB + conf.npage*sizeof(Page))/BY2PG;
178 kpages += (conf.nproc*KSTACK)/BY2PG;
182 if(conf.npage*BY2PG < 16*MB)
187 kpages = conf.npage - (conf.npage*userpcnt)/100;
190 * Make sure terminals with low memory get at least
191 * 4MB on the first Image chunk allocation.
193 if(conf.npage*BY2PG < 16*MB)
194 imagmem->minarena = 4*MB;
198 * can't go past the end of virtual memory.
200 if(kpages > ((uintptr)-KZERO)/BY2PG)
201 kpages = ((uintptr)-KZERO)/BY2PG;
203 conf.upages = conf.npage - kpages;
204 conf.ialloc = (kpages/2)*BY2PG;
207 * Guess how much is taken by the large permanent
208 * datastructures. Mntcache and Mntrpc are not accounted for
212 kpages -= conf.upages*sizeof(Page)
213 + conf.nproc*sizeof(Proc)
214 + conf.nimage*sizeof(Image)
216 + conf.nswppo*sizeof(Page*);
217 mainmem->maxsize = kpages;
220 * the dynamic allocation will balance the load properly,
221 * hopefully. be careful with 32-bit overflow.
223 imagmem->maxsize = kpages - (kpages/10);
224 if(p = getconf("*imagemaxmb")){
225 imagmem->maxsize = strtol(p, nil, 0)*MB;
226 if(imagmem->maxsize > mainmem->maxsize)
227 imagmem->maxsize = mainmem->maxsize;
242 memset(m, 0, sizeof(Mach));
249 * For polled uart output at boot, need
250 * a default delay constant. 100000 should
251 * be enough for a while. Cpuidentify will
252 * calculate the real value later.
254 m->loopconst = 100000;
262 MACHP(0) = (Mach*)CPU0MACH;
265 m->pml4 = (u64int*)CPU0PML4;
266 m->gdt = (Segdesc*)CPU0GDT;
295 sp = (uchar*)base + BY2PG - sizeof(Tos);
298 av[ac++] = pusharg("boot");
300 /* when boot is changed to only use rc, this code can go away */
301 cp[BOOTLINELEN-1] = 0;
303 if(strncmp(cp, "fd", 2) == 0){
304 sprint(buf, "local!#f/fd%lddisk", strtol(cp+2, 0, 0));
305 av[ac++] = pusharg(buf);
306 } else if(strncmp(cp, "sd", 2) == 0){
307 sprint(buf, "local!#S/sd%c%c/fs", *(cp+2), *(cp+3));
308 av[ac++] = pusharg(buf);
309 } else if(strncmp(cp, "ether", 5) == 0)
310 av[ac++] = pusharg("-n");
312 /* 8 byte word align stack */
313 sp = (uchar*)((uintptr)sp & ~7);
315 /* build argc, argv on stack */
316 sp -= (ac+1)*sizeof(sp);
318 for(i = 0; i < ac; i++)
319 lsp[i] = av[i] + ((uintptr)(USTKTOP - BY2PG) - (uintptr)base);
321 sp += (uintptr)(USTKTOP - BY2PG) - (uintptr)base;
329 char buf[2*KNAMELEN];
336 * These are o.k. because rootinit is null.
337 * Then early kproc's will have a root and dot.
339 up->slash = namec("#/", Atodir, 0, 0);
340 pathclose(up->slash->path);
341 up->slash->path = newpath("/");
342 up->dot = cclone(up->slash);
347 snprint(buf, sizeof(buf), "%s %s", arch->id, conffile);
348 ksetenv("terminal", buf, 0);
349 ksetenv("cputype", "amd64", 0);
351 ksetenv("service", "cpu", 0);
353 ksetenv("service", "terminal", 0);
354 for(i = 0; i < nconf; i++){
355 if(confname[i][0] != '*')
356 ksetenv(confname[i], confval[i], 0);
357 ksetenv(confname[i], confval[i], 1);
361 kproc("alarm", alarmkproc, 0);
376 p->egrp = smalloc(sizeof(Egrp));
378 p->fgrp = dupfgrp(nil);
383 kstrdup(&p->text, "*init*");
384 kstrdup(&p->user, eve);
391 * N.B. make sure there's enough space for syscall to check
393 * 8 bytes for gotolabel's return PC
395 p->sched.pc = (uintptr)init0;
396 p->sched.sp = (uintptr)p->kstack+KSTACK-(sizeof(Sargs)+BY2WD);
398 /* temporarily set up for kmap() */
404 s = newseg(SG_STACK, USTKTOP-USTKSIZE, USTKSIZE/BY2PG);
406 pg = newpage(0, 0, USTKTOP-BY2PG);
416 s = newseg(SG_TEXT, UTZERO, 1);
419 pg = newpage(0, 0, UTZERO);
420 memset(pg->cachectl, PG_TXTFLUSH, sizeof(pg->cachectl));
424 memmove(v, initcode, sizeof initcode);
451 if(i8237alloc != nil)
462 if(arch->clockenable)
476 active.thunderbirdsarego = 1;
481 shutdown(int ispanic)
487 active.ispanic = ispanic;
488 else if(m->machno == 0 && (active.machs & (1<<m->machno)) == 0)
490 once = active.machs & (1<<m->machno);
492 * setting exiting will make hzclock() on each processor call exit(0),
493 * which calls shutdown(0) and arch->reset(), which on mp systems is
494 * mpshutdown, from which there is no return: the processor is idled
495 * or initiates a reboot. clearing our bit in machs avoids calling
496 * exit(0) from hzclock() on this processor.
498 active.machs &= ~(1<<m->machno);
503 iprint("cpu%d: exiting\n", m->machno);
505 /* wait for any other processors to shutdown */
507 for(ms = 5*1000; ms > 0; ms -= TK2MS(2)){
509 if(active.machs == 0 && consactive() == 0)
517 if(getconf("*debug"))
533 reboot(void*, void*, ulong)
539 * SIMD Floating Point.
540 * Assembler support to get at the individual instructions
542 * There are opportunities to be lazier about saving and
543 * restoring the state and allocating the storage needed.
545 extern void _clts(void);
546 extern void _fldcw(u16int);
547 extern void _fnclex(void);
548 extern void _fninit(void);
549 extern void _fxrstor(Fxsave*);
550 extern void _fxsave(Fxsave*);
551 extern void _fwait(void);
552 extern void _ldmxcsr(u32int);
553 extern void _stts(void);
556 * not used, AMD64 mandated SSE
563 fpx87restore(FPsave*)
568 fpssesave(FPsave *fps)
570 Fxsave *fx = (Fxsave*)ROUND(((uintptr)fps), FPalign);
574 if(fx != (Fxsave*)fps)
575 memmove((Fxsave*)fps, fx, sizeof(Fxsave));
578 fpsserestore(FPsave *fps)
580 Fxsave *fx = (Fxsave*)ROUND(((uintptr)fps), FPalign);
582 if(fx != (Fxsave*)fps)
583 memmove(fx, (Fxsave*)fps, sizeof(Fxsave));
588 static char* mathmsg[] =
590 nil, /* handled below */
591 "denormalized operand",
599 mathnote(ulong status, uintptr pc)
601 char *msg, note[ERRMAX];
605 * Some attention should probably be paid here to the
606 * exception masks and error summary.
608 msg = "unknown exception";
609 for(i = 1; i <= 5; i++){
610 if(!((1<<i) & status))
618 msg = "stack overflow";
620 msg = "stack underflow";
622 msg = "invalid operation";
624 snprint(note, sizeof note, "sys: fp: %s fppc=%#p status=0x%lux",
626 postnote(up, 1, note, NDebug);
630 * math coprocessor error
633 matherror(Ureg*, void*)
636 * Save FPU state to check out the error.
639 up->fpstate = FPinactive;
640 mathnote(up->fpsave.fsw, up->fpsave.rip);
644 * math coprocessor emulation fault
647 mathemu(Ureg *ureg, void*)
649 ulong status, control;
651 if(up->fpstate & FPillegal){
652 /* someone did floating point in a note handler */
653 postnote(up, 1, "sys: floating point in note handler", NDebug);
659 * A process tries to use the FPU for the
660 * first time and generates a 'device not available'
662 * Turn the FPU on and initialise it for use.
663 * Set the precision and mask the exceptions
664 * we don't care about from the generic Mach value.
671 * TODO: sse exceptions
672 * _ldmxcsr(m->mxcsr);
675 up->fpstate = FPactive;
679 * Before restoring the state, check for any pending
680 * exceptions, there's no way to restore the state without
681 * generating an unmasked exception.
682 * More attention should probably be paid here to the
683 * exception masks and error summary.
685 status = up->fpsave.fsw;
686 control = up->fpsave.fcw;
687 if((status & ~control) & 0x07F){
688 mathnote(status, up->fpsave.rip);
691 fprestore(&up->fpsave);
692 up->fpstate = FPactive;
695 panic("math emu pid %ld %s pc %#p",
696 up->pid, up->text, ureg->pc);
702 * math coprocessor segment overrun
705 mathover(Ureg*, void*)
707 pexit("math overrun", 0);
713 trapenable(VectorCERR, matherror, 0, "matherror");
714 if(X86FAMILY(m->cpuidax) == 3)
715 intrenable(IrqIRQ13, matherror, 0, BUSUNKNOWN, "matherror");
716 trapenable(VectorCNA, mathemu, 0, "mathemu");
717 trapenable(VectorCSO, mathover, 0, "mathover");
726 p->pcycles = -p->kentry;
734 p->kentry = up->kentry;
735 p->pcycles = -p->kentry;
737 /* save floating point state */
739 switch(up->fpstate & ~FPillegal){
742 up->fpstate = FPinactive;
744 p->fpsave = up->fpsave;
745 p->fpstate = FPinactive;
773 if(p->fpstate == FPactive){
774 if(p->state == Moribund){
781 * Fpsave() stores without handling pending
782 * unmasked exeptions. Postnote() can't be called
783 * here as sleep() already has up->rlock, so
784 * the handling of pending exceptions is delayed
785 * until the process runs again and generates an
786 * emulation fault to activate the FPU.
790 p->fpstate = FPinactive;
794 * While this processor is in the scheduler, the process could run
795 * on another processor and exit, returning the page tables to
796 * the free list where they could be reallocated and overwritten.
797 * When this processor eventually has to get an entry from the
798 * trashed page tables it will crash.
800 * If there's only one processor, this can't happen.
801 * You might think it would be a win not to do this in that case,
802 * especially on VMware, but it turns out not to matter.