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*MB;
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 * the dynamic allocation will balance the load properly,
446 * hopefully. be careful with 32-bit overflow.
448 imagmem->maxsize = mainmem->maxsize;
449 if(p = getconf("*imagemaxmb")){
450 imagmem->maxsize = strtol(p, nil, 0)*MB;
451 if(imagmem->maxsize > mainmem->maxsize)
452 imagmem->maxsize = mainmem->maxsize;
456 static char* mathmsg[] =
458 nil, /* handled below */
459 "denormalized operand",
471 char *msg, note[ERRMAX];
473 status = up->fpsave.status;
476 * Some attention should probably be paid here to the
477 * exception masks and error summary.
479 msg = "unknown exception";
480 for(i = 1; i <= 5; i++){
481 if(!((1<<i) & status))
489 msg = "stack overflow";
491 msg = "stack underflow";
493 msg = "invalid operation";
495 snprint(note, sizeof note, "sys: fp: %s fppc=0x%lux status=0x%lux",
496 msg, up->fpsave.pc, status);
497 postnote(up, 1, note, NDebug);
501 * math coprocessor error
504 matherror(Ureg *ur, void*)
507 * a write cycle to port 0xF0 clears the interrupt latch attached
508 * to the error# line from the 387
510 if(!(m->cpuiddx & 0x01))
514 * save floating point state to check out error
519 if((ur->pc & 0xf0000000) == KZERO)
520 panic("fp: status %ux fppc=0x%lux pc=0x%lux",
521 up->fpsave.status, up->fpsave.pc, ur->pc);
525 * math coprocessor emulation fault
528 mathemu(Ureg *ureg, void*)
530 if(up->fpstate & FPillegal){
531 /* someone did floating point in a note handler */
532 postnote(up, 1, "sys: floating point in note handler", NDebug);
538 up->fpstate = FPactive;
542 * Before restoring the state, check for any pending
543 * exceptions, there's no way to restore the state without
544 * generating an unmasked exception.
545 * More attention should probably be paid here to the
546 * exception masks and error summary.
548 if((up->fpsave.status & ~up->fpsave.control) & 0x07F){
552 fprestore(&up->fpsave);
553 up->fpstate = FPactive;
556 panic("math emu pid %ld %s pc 0x%lux",
557 up->pid, up->text, ureg->pc);
563 * math coprocessor segment overrun
566 mathover(Ureg*, void*)
568 pexit("math overrun", 0);
574 trapenable(VectorCERR, matherror, 0, "matherror");
575 if(X86FAMILY(m->cpuidax) == 3)
576 intrenable(IrqIRQ13, matherror, 0, BUSUNKNOWN, "matherror");
577 trapenable(VectorCNA, mathemu, 0, "mathemu");
578 trapenable(VectorCSO, mathover, 0, "mathover");
582 * set up floating point for a new process
591 p->pcycles = -p->kentry;
593 memset(p->gdt, 0, sizeof(p->gdt));
601 p->kentry = up->kentry;
602 p->pcycles = -p->kentry;
604 /* inherit user descriptors */
605 memmove(p->gdt, up->gdt, sizeof(p->gdt));
607 /* copy local descriptor table */
608 if(up->ldt != nil && up->nldt > 0){
609 p->ldt = smalloc(sizeof(Segdesc) * up->nldt);
610 memmove(p->ldt, up->ldt, sizeof(Segdesc) * up->nldt);
629 * Save the mach dependent part of the process state.
640 if(p->fpstate == FPactive){
641 if(p->state == Moribund)
645 * Fpsave() stores without handling pending
646 * unmasked exeptions. Postnote() can't be called
647 * here as sleep() already has up->rlock, so
648 * the handling of pending exceptions is delayed
649 * until the process runs again and generates an
650 * emulation fault to activate the FPU.
654 p->fpstate = FPinactive;
658 * While this processor is in the scheduler, the process could run
659 * on another processor and exit, returning the page tables to
660 * the free list where they could be reallocated and overwritten.
661 * When this processor eventually has to get an entry from the
662 * trashed page tables it will crash.
664 * If there's only one processor, this can't happen.
665 * You might think it would be a win not to do this in that case,
666 * especially on VMware, but it turns out not to matter.
668 mmuflushtlb(PADDR(m->pdb));
672 shutdown(int ispanic)
678 active.ispanic = ispanic;
679 else if(m->machno == 0 && (active.machs & (1<<m->machno)) == 0)
681 once = active.machs & (1<<m->machno);
683 * setting exiting will make hzclock() on each processor call exit(0),
684 * which calls shutdown(0) and arch->reset(), which on mp systems is
685 * mpshutdown, from which there is no return: the processor is idled
686 * or initiates a reboot. clearing our bit in machs avoids calling
687 * exit(0) from hzclock() on this processor.
689 active.machs &= ~(1<<m->machno);
694 iprint("cpu%d: exiting\n", m->machno);
696 /* wait for any other processors to shutdown */
698 for(ms = 5*1000; ms > 0; ms -= TK2MS(2)){
700 if(active.machs == 0 && consactive() == 0)
708 if(getconf("*debug"))
717 reboot(void *entry, void *code, ulong size)
719 void (*f)(ulong, ulong, ulong);
725 * the boot processor is cpu0. execute this function on it
726 * so that the new kernel has the same cpu0. this only matters
727 * because the hardware has a notion of which processor was the
728 * boot processor and we look at it at start up.
730 if (m->machno != 0) {
738 * should be the only processor running now
741 print("on cpu%d (not 0)!\n", m->machno);
743 print("still have active ap processors!\n");
745 print("shutting down...\n");
750 /* turn off buffered serial console */
753 /* shutdown devices */
758 * Modify the machine page table to directly map the low 4MB of memory
759 * This allows the reboot code to turn off the page mapping
762 pdb[PDX(0)] = pdb[PDX(KZERO)];
763 mmuflushtlb(PADDR(pdb));
765 /* setup reboot trampoline function */
766 f = (void*)REBOOTADDR;
767 memmove(f, rebootcode, sizeof(rebootcode));
769 print("rebooting...\n");
771 /* off we go - never to return */
773 (*f)(PADDR(entry), PADDR(code), size);
785 isaconfig(char *class, int ctlrno, ISAConf *isa)
790 snprint(cc, sizeof cc, "%s%d", class, ctlrno);
796 isa->nopt = tokenize(p, isa->opt, NISAOPT);
797 for(i = 0; i < isa->nopt; i++){
799 if(cistrncmp(p, "type=", 5) == 0)
801 else if(cistrncmp(p, "port=", 5) == 0)
802 isa->port = strtoul(p+5, &p, 0);
803 else if(cistrncmp(p, "irq=", 4) == 0)
804 isa->irq = strtoul(p+4, &p, 0);
805 else if(cistrncmp(p, "dma=", 4) == 0)
806 isa->dma = strtoul(p+4, &p, 0);
807 else if(cistrncmp(p, "mem=", 4) == 0)
808 isa->mem = strtoul(p+4, &p, 0);
809 else if(cistrncmp(p, "size=", 5) == 0)
810 isa->size = strtoul(p+5, &p, 0);
811 else if(cistrncmp(p, "freq=", 5) == 0)
812 isa->freq = strtoul(p+5, &p, 0);
818 cistrcmp(char *a, char *b)
826 if(ac >= 'A' && ac <= 'Z')
827 ac = 'a' + (ac - 'A');
828 if(bc >= 'A' && bc <= 'Z')
829 bc = 'a' + (bc - 'A');
840 cistrncmp(char *a, char *b, int n)
849 if(ac >= 'A' && ac <= 'Z')
850 ac = 'a' + (ac - 'A');
851 if(bc >= 'A' && bc <= 'Z')
852 bc = 'a' + (bc - 'A');
865 * put the processor in the halt state if we've no processes to run.
866 * an interrupt will get us going again.
875 else if(m->cpuidcx & Monitor)