2 #include "../port/lib.h"
16 * Where configuration info is left for the loaded programme.
17 * This will turn into a structure as more is done by the boot loader
18 * (e.g. why parse the .ini file twice?).
19 * There are 3584 bytes available at CONFADDR.
21 #define BOOTLINE ((char*)CONFADDR)
22 #define BOOTLINELEN 64
23 #define BOOTARGS ((char*)(CONFADDR+BOOTLINELEN))
24 #define BOOTARGSLEN (4096-0x200-BOOTLINELEN)
27 char bootdisk[KNAMELEN];
29 char *confname[MAXCONF];
30 char *confval[MAXCONF];
32 uchar *sp; /* user stack of init proc */
39 char *cp, *line[MAXCONF], *p, *q;
42 * parse configuration args from dos file plan9.ini
44 cp = BOOTARGS; /* where b.com leaves its config */
45 cp[BOOTARGSLEN-1] = 0;
48 * Strip out '\r', change '\t' -> ' '.
60 n = getfields(cp, line, MAXCONF, 1, "\n");
61 for(i = 0; i < n; i++){
64 cp = strchr(line[i], '=');
68 confname[nconf] = line[i];
74 extern void mmuinit0(void);
75 extern void (*i8237alloc)(void);
105 if(arch->intrinit) /* launches other processors on an mp */
110 if(arch->clockenable)
124 active.thunderbirdsarego = 1;
132 MACHP(0) = (Mach*)CPU0MACH;
133 m->pdb = (ulong*)CPU0PDB;
134 m->gdt = (Segdesc*)CPU0GDT;
152 memset(m, 0, sizeof(Mach));
159 * For polled uart output at boot, need
160 * a default delay constant. 100000 should
161 * be enough for a while. Cpuidentify will
162 * calculate the real value later.
164 m->loopconst = 100000;
171 char buf[2*KNAMELEN];
178 * These are o.k. because rootinit is null.
179 * Then early kproc's will have a root and dot.
181 up->slash = namec("#/", Atodir, 0, 0);
182 pathclose(up->slash->path);
183 up->slash->path = newpath("/");
184 up->dot = cclone(up->slash);
189 snprint(buf, sizeof(buf), "%s %s", arch->id, conffile);
190 ksetenv("terminal", buf, 0);
191 ksetenv("cputype", "386", 0);
193 ksetenv("service", "cpu", 0);
195 ksetenv("service", "terminal", 0);
196 for(i = 0; i < nconf; i++){
197 if(confname[i][0] != '*')
198 ksetenv(confname[i], confval[i], 0);
199 ksetenv(confname[i], confval[i], 1);
203 kproc("alarm", alarmkproc, 0);
217 p->egrp = smalloc(sizeof(Egrp));
219 p->fgrp = dupfgrp(nil);
224 kstrdup(&p->text, "*init*");
225 kstrdup(&p->user, eve);
232 * N.B. make sure there's enough space for syscall to check
234 * 4 bytes for gotolabel's return PC
236 p->sched.pc = (ulong)init0;
237 p->sched.sp = (ulong)p->kstack+KSTACK-(sizeof(Sargs)+BY2WD);
242 * N.B. cannot call newpage() with clear=1, because pc kmap
243 * requires up != nil. use tmpmap instead.
245 s = newseg(SG_STACK, USTKTOP-USTKSIZE, USTKSIZE/BY2PG);
247 pg = newpage(0, 0, USTKTOP-BY2PG);
257 s = newseg(SG_TEXT, UTZERO, 1);
260 pg = newpage(0, 0, UTZERO);
261 memset(pg->cachectl, PG_TXTFLUSH, sizeof(pg->cachectl));
265 memmove(v, initcode, sizeof initcode);
291 sp = (uchar*)base + BY2PG - sizeof(Tos);
294 av[ac++] = pusharg("boot");
296 /* when boot is changed to only use rc, this code can go away */
297 cp[BOOTLINELEN-1] = 0;
299 if(strncmp(cp, "fd", 2) == 0){
300 sprint(buf, "local!#f/fd%lddisk", strtol(cp+2, 0, 0));
301 av[ac++] = pusharg(buf);
302 } else if(strncmp(cp, "sd", 2) == 0){
303 sprint(buf, "local!#S/sd%c%c/fs", *(cp+2), *(cp+3));
304 av[ac++] = pusharg(buf);
305 } else if(strncmp(cp, "ether", 5) == 0)
306 av[ac++] = pusharg("-n");
308 /* 4 byte word align stack */
309 sp = (uchar*)((ulong)sp & ~3);
311 /* build argc, argv on stack */
312 sp -= (ac+1)*sizeof(sp);
314 for(i = 0; i < ac; i++)
315 lsp[i] = av[i] + ((USTKTOP - BY2PG) - (ulong)base);
317 sp += (USTKTOP - BY2PG) - (ulong)base;
326 for(i = 0; i < nconf; i++)
327 if(cistrcmp(confname[i], name) == 0)
345 /* convert to name=value\n format */
354 error("kernel configuration too large");
355 memset(BOOTLINE, 0, BOOTLINELEN);
356 memmove(BOOTARGS, p, n);
368 if(p = getconf("*kernelpercent"))
369 userpcnt = 100 - strtol(p, 0, 0);
374 for(i=0; i<nelem(conf.mem); i++)
375 conf.npage += conf.mem[i].npage;
377 conf.nproc = 100 + ((conf.npage*BY2PG)/MB)*5;
380 if(conf.nproc > 2000)
383 conf.nswap = conf.nproc*80;
389 kpages = conf.npage - (conf.npage*userpcnt)/100;
392 * Hack for the big boys. Only good while physmem < 4GB.
393 * Give the kernel fixed max + enough to allocate the
395 * This is an overestimate as conf.upages < conf.npages.
396 * The patch of nimage is a band-aid, scanning the whole
397 * page list in imagereclaim just takes too long.
399 if(kpages > (64*MB + conf.npage*sizeof(Page))/BY2PG){
400 kpages = (64*MB + conf.npage*sizeof(Page))/BY2PG;
402 kpages += (conf.nproc*KSTACK)/BY2PG;
406 if(conf.npage*BY2PG < 16*MB)
411 kpages = conf.npage - (conf.npage*userpcnt)/100;
414 * Make sure terminals with low memory get at least
415 * 4MB on the first Image chunk allocation.
417 if(conf.npage*BY2PG < 16*MB)
418 imagmem->minarena = 4*1024*1024;
422 * can't go past the end of virtual memory
423 * (ulong)-KZERO is 2^32 - KZERO
425 if(kpages > ((ulong)-KZERO)/BY2PG)
426 kpages = ((ulong)-KZERO)/BY2PG;
428 conf.upages = conf.npage - kpages;
429 conf.ialloc = (kpages/2)*BY2PG;
432 * Guess how much is taken by the large permanent
433 * datastructures. Mntcache and Mntrpc are not accounted for
437 kpages -= conf.upages*sizeof(Page)
438 + conf.nproc*sizeof(Proc)
439 + conf.nimage*sizeof(Image)
441 + conf.nswppo*sizeof(Page);
442 mainmem->maxsize = kpages;
445 * give terminals lots of image memory, too; the dynamic
446 * allocation will balance the load properly, hopefully.
447 * be careful with 32-bit overflow.
449 imagmem->maxsize = kpages;
453 static char* mathmsg[] =
455 nil, /* handled below */
456 "denormalized operand",
468 char *msg, note[ERRMAX];
470 status = up->fpsave.status;
473 * Some attention should probably be paid here to the
474 * exception masks and error summary.
476 msg = "unknown exception";
477 for(i = 1; i <= 5; i++){
478 if(!((1<<i) & status))
486 msg = "stack overflow";
488 msg = "stack underflow";
490 msg = "invalid operation";
492 snprint(note, sizeof note, "sys: fp: %s fppc=0x%lux status=0x%lux",
493 msg, up->fpsave.pc, status);
494 postnote(up, 1, note, NDebug);
498 * math coprocessor error
501 matherror(Ureg *ur, void*)
504 * a write cycle to port 0xF0 clears the interrupt latch attached
505 * to the error# line from the 387
507 if(!(m->cpuiddx & 0x01))
511 * save floating point state to check out error
516 if((ur->pc & 0xf0000000) == KZERO)
517 panic("fp: status %ux fppc=0x%lux pc=0x%lux",
518 up->fpsave.status, up->fpsave.pc, ur->pc);
522 * math coprocessor emulation fault
525 mathemu(Ureg *ureg, void*)
527 if(up->fpstate & FPillegal){
528 /* someone did floating point in a note handler */
529 postnote(up, 1, "sys: floating point in note handler", NDebug);
535 up->fpstate = FPactive;
539 * Before restoring the state, check for any pending
540 * exceptions, there's no way to restore the state without
541 * generating an unmasked exception.
542 * More attention should probably be paid here to the
543 * exception masks and error summary.
545 if((up->fpsave.status & ~up->fpsave.control) & 0x07F){
549 fprestore(&up->fpsave);
550 up->fpstate = FPactive;
553 panic("math emu pid %ld %s pc 0x%lux",
554 up->pid, up->text, ureg->pc);
560 * math coprocessor segment overrun
563 mathover(Ureg*, void*)
565 pexit("math overrun", 0);
571 trapenable(VectorCERR, matherror, 0, "matherror");
572 if(X86FAMILY(m->cpuidax) == 3)
573 intrenable(IrqIRQ13, matherror, 0, BUSUNKNOWN, "matherror");
574 trapenable(VectorCNA, mathemu, 0, "mathemu");
575 trapenable(VectorCSO, mathover, 0, "mathover");
579 * set up floating point for a new process
588 p->pcycles = -p->kentry;
590 memset(p->gdt, 0, sizeof(p->gdt));
598 p->kentry = up->kentry;
599 p->pcycles = -p->kentry;
601 /* inherit user descriptors */
602 memmove(p->gdt, up->gdt, sizeof(p->gdt));
604 /* copy local descriptor table */
605 if(up->ldt != nil && up->nldt > 0){
606 p->ldt = malloc(sizeof(Segdesc) * up->nldt);
607 memmove(p->ldt, up->ldt, sizeof(Segdesc) * up->nldt);
626 * Save the mach dependent part of the process state.
637 if(p->fpstate == FPactive){
638 if(p->state == Moribund)
642 * Fpsave() stores without handling pending
643 * unmasked exeptions. Postnote() can't be called
644 * here as sleep() already has up->rlock, so
645 * the handling of pending exceptions is delayed
646 * until the process runs again and generates an
647 * emulation fault to activate the FPU.
651 p->fpstate = FPinactive;
655 * While this processor is in the scheduler, the process could run
656 * on another processor and exit, returning the page tables to
657 * the free list where they could be reallocated and overwritten.
658 * When this processor eventually has to get an entry from the
659 * trashed page tables it will crash.
661 * If there's only one processor, this can't happen.
662 * You might think it would be a win not to do this in that case,
663 * especially on VMware, but it turns out not to matter.
665 mmuflushtlb(PADDR(m->pdb));
669 shutdown(int ispanic)
675 active.ispanic = ispanic;
676 else if(m->machno == 0 && (active.machs & (1<<m->machno)) == 0)
678 once = active.machs & (1<<m->machno);
680 * setting exiting will make hzclock() on each processor call exit(0),
681 * which calls shutdown(0) and arch->reset(), which on mp systems is
682 * mpshutdown, from which there is no return: the processor is idled
683 * or initiates a reboot. clearing our bit in machs avoids calling
684 * exit(0) from hzclock() on this processor.
686 active.machs &= ~(1<<m->machno);
691 iprint("cpu%d: exiting\n", m->machno);
693 /* wait for any other processors to shutdown */
695 for(ms = 5*1000; ms > 0; ms -= TK2MS(2)){
697 if(active.machs == 0 && consactive() == 0)
705 if(getconf("*debug"))
714 reboot(void *entry, void *code, ulong size)
716 void (*f)(ulong, ulong, ulong);
722 * the boot processor is cpu0. execute this function on it
723 * so that the new kernel has the same cpu0. this only matters
724 * because the hardware has a notion of which processor was the
725 * boot processor and we look at it at start up.
727 if (m->machno != 0) {
735 * should be the only processor running now
738 print("on cpu%d (not 0)!\n", m->machno);
740 print("still have active ap processors!\n");
742 print("shutting down...\n");
747 /* turn off buffered serial console */
750 /* shutdown devices */
755 * Modify the machine page table to directly map the low 4MB of memory
756 * This allows the reboot code to turn off the page mapping
759 pdb[PDX(0)] = pdb[PDX(KZERO)];
760 mmuflushtlb(PADDR(pdb));
762 /* setup reboot trampoline function */
763 f = (void*)REBOOTADDR;
764 memmove(f, rebootcode, sizeof(rebootcode));
766 print("rebooting...\n");
768 /* off we go - never to return */
770 (*f)(PADDR(entry), PADDR(code), size);
782 isaconfig(char *class, int ctlrno, ISAConf *isa)
787 snprint(cc, sizeof cc, "%s%d", class, ctlrno);
793 isa->nopt = tokenize(p, isa->opt, NISAOPT);
794 for(i = 0; i < isa->nopt; i++){
796 if(cistrncmp(p, "type=", 5) == 0)
798 else if(cistrncmp(p, "port=", 5) == 0)
799 isa->port = strtoul(p+5, &p, 0);
800 else if(cistrncmp(p, "irq=", 4) == 0)
801 isa->irq = strtoul(p+4, &p, 0);
802 else if(cistrncmp(p, "dma=", 4) == 0)
803 isa->dma = strtoul(p+4, &p, 0);
804 else if(cistrncmp(p, "mem=", 4) == 0)
805 isa->mem = strtoul(p+4, &p, 0);
806 else if(cistrncmp(p, "size=", 5) == 0)
807 isa->size = strtoul(p+5, &p, 0);
808 else if(cistrncmp(p, "freq=", 5) == 0)
809 isa->freq = strtoul(p+5, &p, 0);
815 cistrcmp(char *a, char *b)
823 if(ac >= 'A' && ac <= 'Z')
824 ac = 'a' + (ac - 'A');
825 if(bc >= 'A' && bc <= 'Z')
826 bc = 'a' + (bc - 'A');
837 cistrncmp(char *a, char *b, int n)
846 if(ac >= 'A' && ac <= 'Z')
847 ac = 'a' + (ac - 'A');
848 if(bc >= 'A' && bc <= 'Z')
849 bc = 'a' + (bc - 'A');
862 * put the processor in the halt state if we've no processes to run.
863 * an interrupt will get us going again.