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;
41 extern ulong *multiboot;
46 if((*multiboot & 8) != 0 && multiboot[5] > 0){
47 m = KADDR(multiboot[6]);
58 * parse configuration args from dos file plan9.ini
60 cp = BOOTARGS; /* where b.com leaves its config */
61 cp[BOOTARGSLEN-1] = 0;
64 * Strip out '\r', change '\t' -> ' '.
76 n = getfields(cp, line, MAXCONF, 1, "\n");
77 for(i = 0; i < n; i++){
80 cp = strchr(line[i], '=');
84 confname[nconf] = line[i];
90 extern void mmuinit0(void);
91 extern void (*i8237alloc)(void);
115 if(i8237alloc != nil)
121 if(arch->intrinit) /* launches other processors on an mp */
126 if(arch->clockenable)
140 active.thunderbirdsarego = 1;
148 MACHP(0) = (Mach*)CPU0MACH;
149 m->pdb = (ulong*)CPU0PDB;
150 m->gdt = (Segdesc*)CPU0GDT;
168 memset(m, 0, sizeof(Mach));
175 * For polled uart output at boot, need
176 * a default delay constant. 100000 should
177 * be enough for a while. Cpuidentify will
178 * calculate the real value later.
180 m->loopconst = 100000;
187 char buf[2*KNAMELEN];
194 * These are o.k. because rootinit is null.
195 * Then early kproc's will have a root and dot.
197 up->slash = namec("#/", Atodir, 0, 0);
198 pathclose(up->slash->path);
199 up->slash->path = newpath("/");
200 up->dot = cclone(up->slash);
205 snprint(buf, sizeof(buf), "%s %s", arch->id, conffile);
206 ksetenv("terminal", buf, 0);
207 ksetenv("cputype", "386", 0);
209 ksetenv("service", "cpu", 0);
211 ksetenv("service", "terminal", 0);
212 for(i = 0; i < nconf; i++){
213 if(confname[i][0] != '*')
214 ksetenv(confname[i], confval[i], 0);
215 ksetenv(confname[i], confval[i], 1);
219 kproc("alarm", alarmkproc, 0);
233 p->egrp = smalloc(sizeof(Egrp));
235 p->fgrp = dupfgrp(nil);
240 kstrdup(&p->text, "*init*");
241 kstrdup(&p->user, eve);
248 * N.B. make sure there's enough space for syscall to check
250 * 4 bytes for gotolabel's return PC
252 p->sched.pc = (ulong)init0;
253 p->sched.sp = (ulong)p->kstack+KSTACK-(sizeof(Sargs)+BY2WD);
258 * N.B. cannot call newpage() with clear=1, because pc kmap
259 * requires up != nil. use tmpmap instead.
261 s = newseg(SG_STACK, USTKTOP-USTKSIZE, USTKSIZE/BY2PG);
263 pg = newpage(0, 0, USTKTOP-BY2PG);
273 s = newseg(SG_TEXT, UTZERO, 1);
276 pg = newpage(0, 0, UTZERO);
277 memset(pg->cachectl, PG_TXTFLUSH, sizeof(pg->cachectl));
281 memmove(v, initcode, sizeof initcode);
307 sp = (uchar*)base + BY2PG - sizeof(Tos);
310 av[ac++] = pusharg("boot");
312 /* when boot is changed to only use rc, this code can go away */
313 cp[BOOTLINELEN-1] = 0;
315 if(strncmp(cp, "fd", 2) == 0){
316 sprint(buf, "local!#f/fd%lddisk", strtol(cp+2, 0, 0));
317 av[ac++] = pusharg(buf);
318 } else if(strncmp(cp, "sd", 2) == 0){
319 sprint(buf, "local!#S/sd%c%c/fs", *(cp+2), *(cp+3));
320 av[ac++] = pusharg(buf);
321 } else if(strncmp(cp, "ether", 5) == 0)
322 av[ac++] = pusharg("-n");
324 /* 4 byte word align stack */
325 sp = (uchar*)((ulong)sp & ~3);
327 /* build argc, argv on stack */
328 sp -= (ac+1)*sizeof(sp);
330 for(i = 0; i < ac; i++)
331 lsp[i] = av[i] + ((USTKTOP - BY2PG) - (ulong)base);
333 sp += (USTKTOP - BY2PG) - (ulong)base;
342 for(i = 0; i < nconf; i++)
343 if(cistrcmp(confname[i], name) == 0)
361 /* convert to name=value\n format */
370 error("kernel configuration too large");
371 memset(BOOTLINE, 0, BOOTLINELEN);
372 memmove(BOOTARGS, p, n);
384 if(p = getconf("*kernelpercent"))
385 userpcnt = 100 - strtol(p, 0, 0);
390 for(i=0; i<nelem(conf.mem); i++)
391 conf.npage += conf.mem[i].npage;
393 conf.nproc = 100 + ((conf.npage*BY2PG)/MB)*5;
396 if(conf.nproc > 2000)
399 conf.nswap = conf.nproc*80;
405 kpages = conf.npage - (conf.npage*userpcnt)/100;
408 * Hack for the big boys. Only good while physmem < 4GB.
409 * Give the kernel fixed max + enough to allocate the
411 * This is an overestimate as conf.upages < conf.npages.
412 * The patch of nimage is a band-aid, scanning the whole
413 * page list in imagereclaim just takes too long.
415 if(kpages > (64*MB + conf.npage*sizeof(Page))/BY2PG){
416 kpages = (64*MB + conf.npage*sizeof(Page))/BY2PG;
418 kpages += (conf.nproc*KSTACK)/BY2PG;
422 if(conf.npage*BY2PG < 16*MB)
427 kpages = conf.npage - (conf.npage*userpcnt)/100;
430 * Make sure terminals with low memory get at least
431 * 4MB on the first Image chunk allocation.
433 if(conf.npage*BY2PG < 16*MB)
434 imagmem->minarena = 4*MB;
438 * can't go past the end of virtual memory
439 * (ulong)-KZERO is 2^32 - KZERO
441 if(kpages > ((ulong)-KZERO)/BY2PG)
442 kpages = ((ulong)-KZERO)/BY2PG;
444 conf.upages = conf.npage - kpages;
445 conf.ialloc = (kpages/2)*BY2PG;
448 * Guess how much is taken by the large permanent
449 * datastructures. Mntcache and Mntrpc are not accounted for
453 kpages -= conf.upages*sizeof(Page)
454 + conf.nproc*sizeof(Proc)
455 + conf.nimage*sizeof(Image)
457 + conf.nswppo*sizeof(Page*);
458 mainmem->maxsize = kpages;
461 * the dynamic allocation will balance the load properly,
462 * hopefully. be careful with 32-bit overflow.
464 imagmem->maxsize = kpages - (kpages/10);
465 if(p = getconf("*imagemaxmb")){
466 imagmem->maxsize = strtol(p, nil, 0)*MB;
467 if(imagmem->maxsize > mainmem->maxsize)
468 imagmem->maxsize = mainmem->maxsize;
472 static char* mathmsg[] =
474 nil, /* handled below */
475 "denormalized operand",
487 char *msg, note[ERRMAX];
489 status = up->fpsave.status;
492 * Some attention should probably be paid here to the
493 * exception masks and error summary.
495 msg = "unknown exception";
496 for(i = 1; i <= 5; i++){
497 if(!((1<<i) & status))
505 msg = "stack overflow";
507 msg = "stack underflow";
509 msg = "invalid operation";
511 snprint(note, sizeof note, "sys: fp: %s fppc=0x%lux status=0x%lux",
512 msg, up->fpsave.pc, status);
513 postnote(up, 1, note, NDebug);
517 * math coprocessor error
520 matherror(Ureg *ur, void*)
523 * a write cycle to port 0xF0 clears the interrupt latch attached
524 * to the error# line from the 387
526 if(!(m->cpuiddx & Fpuonchip))
530 * save floating point state to check out error
535 if((ur->pc & 0xf0000000) == KZERO)
536 panic("fp: status %ux fppc=0x%lux pc=0x%lux",
537 up->fpsave.status, up->fpsave.pc, ur->pc);
541 * math coprocessor emulation fault
544 mathemu(Ureg *ureg, void*)
546 if(up->fpstate & FPillegal){
547 /* someone did floating point in a note handler */
548 postnote(up, 1, "sys: floating point in note handler", NDebug);
554 up->fpstate = FPactive;
558 * Before restoring the state, check for any pending
559 * exceptions, there's no way to restore the state without
560 * generating an unmasked exception.
561 * More attention should probably be paid here to the
562 * exception masks and error summary.
564 if((up->fpsave.status & ~up->fpsave.control) & 0x07F){
568 fprestore(&up->fpsave);
569 up->fpstate = FPactive;
572 panic("math emu pid %ld %s pc 0x%lux",
573 up->pid, up->text, ureg->pc);
579 * math coprocessor segment overrun
582 mathover(Ureg*, void*)
584 pexit("math overrun", 0);
590 trapenable(VectorCERR, matherror, 0, "matherror");
591 if(X86FAMILY(m->cpuidax) == 3)
592 intrenable(IrqIRQ13, matherror, 0, BUSUNKNOWN, "matherror");
593 trapenable(VectorCNA, mathemu, 0, "mathemu");
594 trapenable(VectorCSO, mathover, 0, "mathover");
598 * set up floating point for a new process
607 p->pcycles = -p->kentry;
609 memset(p->gdt, 0, sizeof(p->gdt));
617 p->kentry = up->kentry;
618 p->pcycles = -p->kentry;
620 /* inherit user descriptors */
621 memmove(p->gdt, up->gdt, sizeof(p->gdt));
623 /* copy local descriptor table */
624 if(up->ldt != nil && up->nldt > 0){
625 p->ldt = smalloc(sizeof(Segdesc) * up->nldt);
626 memmove(p->ldt, up->ldt, sizeof(Segdesc) * up->nldt);
645 * Save the mach dependent part of the process state.
656 if(p->fpstate == FPactive){
657 if(p->state == Moribund)
661 * Fpsave() stores without handling pending
662 * unmasked exeptions. Postnote() can't be called
663 * here as sleep() already has up->rlock, so
664 * the handling of pending exceptions is delayed
665 * until the process runs again and generates an
666 * emulation fault to activate the FPU.
670 p->fpstate = FPinactive;
674 * While this processor is in the scheduler, the process could run
675 * on another processor and exit, returning the page tables to
676 * the free list where they could be reallocated and overwritten.
677 * When this processor eventually has to get an entry from the
678 * trashed page tables it will crash.
680 * If there's only one processor, this can't happen.
681 * You might think it would be a win not to do this in that case,
682 * especially on VMware, but it turns out not to matter.
684 mmuflushtlb(PADDR(m->pdb));
688 shutdown(int ispanic)
694 active.ispanic = ispanic;
695 else if(m->machno == 0 && (active.machs & (1<<m->machno)) == 0)
697 once = active.machs & (1<<m->machno);
699 * setting exiting will make hzclock() on each processor call exit(0),
700 * which calls shutdown(0) and arch->reset(), which on mp systems is
701 * mpshutdown, from which there is no return: the processor is idled
702 * or initiates a reboot. clearing our bit in machs avoids calling
703 * exit(0) from hzclock() on this processor.
705 active.machs &= ~(1<<m->machno);
710 iprint("cpu%d: exiting\n", m->machno);
712 /* wait for any other processors to shutdown */
714 for(ms = 5*1000; ms > 0; ms -= TK2MS(2)){
716 if(active.machs == 0 && consactive() == 0)
724 if(getconf("*debug"))
733 reboot(void *entry, void *code, ulong size)
735 void (*f)(ulong, ulong, ulong);
741 * the boot processor is cpu0. execute this function on it
742 * so that the new kernel has the same cpu0. this only matters
743 * because the hardware has a notion of which processor was the
744 * boot processor and we look at it at start up.
746 if (m->machno != 0) {
754 * should be the only processor running now
757 print("on cpu%d (not 0)!\n", m->machno);
759 print("still have active ap processors!\n");
761 print("shutting down...\n");
766 /* turn off buffered serial console */
769 /* shutdown devices */
774 * Modify the machine page table to directly map the low 4MB of memory
775 * This allows the reboot code to turn off the page mapping
778 pdb[PDX(0)] = pdb[PDX(KZERO)];
779 mmuflushtlb(PADDR(pdb));
781 /* setup reboot trampoline function */
782 f = (void*)REBOOTADDR;
783 memmove(f, rebootcode, sizeof(rebootcode));
785 print("rebooting...\n");
787 /* off we go - never to return */
789 (*f)(PADDR(entry), PADDR(code), size);
801 isaconfig(char *class, int ctlrno, ISAConf *isa)
806 snprint(cc, sizeof cc, "%s%d", class, ctlrno);
812 isa->nopt = tokenize(p, isa->opt, NISAOPT);
813 for(i = 0; i < isa->nopt; i++){
815 if(cistrncmp(p, "type=", 5) == 0)
817 else if(cistrncmp(p, "port=", 5) == 0)
818 isa->port = strtoul(p+5, &p, 0);
819 else if(cistrncmp(p, "irq=", 4) == 0)
820 isa->irq = strtoul(p+4, &p, 0);
821 else if(cistrncmp(p, "dma=", 4) == 0)
822 isa->dma = strtoul(p+4, &p, 0);
823 else if(cistrncmp(p, "mem=", 4) == 0)
824 isa->mem = strtoul(p+4, &p, 0);
825 else if(cistrncmp(p, "size=", 5) == 0)
826 isa->size = strtoul(p+5, &p, 0);
827 else if(cistrncmp(p, "freq=", 5) == 0)
828 isa->freq = strtoul(p+5, &p, 0);
834 cistrcmp(char *a, char *b)
842 if(ac >= 'A' && ac <= 'Z')
843 ac = 'a' + (ac - 'A');
844 if(bc >= 'A' && bc <= 'Z')
845 bc = 'a' + (bc - 'A');
856 cistrncmp(char *a, char *b, int n)
865 if(ac >= 'A' && ac <= 'Z')
866 ac = 'a' + (ac - 'A');
867 if(bc >= 'A' && bc <= 'Z')
868 bc = 'a' + (bc - 'A');
881 * put the processor in the halt state if we've no processes to run.
882 * an interrupt will get us going again.
891 else if(m->cpuidcx & Monitor)