2 #include "../port/lib.h"
15 #define BOOTARGS (xenstart->cmd_line)
16 #define BOOTARGSLEN (sizeof xenstart->cmd_line)
20 /* space for syscall args, return PC, top-of-stack struct */
21 Ustkheadroom = sizeof(Sargs) + sizeof(uintptr) + sizeof(Tos),
25 char *confname[MAXCONF];
26 char *confval[MAXCONF];
28 uchar *sp; /* user stack of init proc */
35 char *cp, *line[MAXCONF], *p, *q;
38 * parse configuration args from dos file plan9.ini
40 cp = BOOTARGS; /* where b.com leaves its config */
41 cp[BOOTARGSLEN-1] = 0;
44 * Strip out '\r', change '\t' -> ' '.
56 n = getfields(cp, line, MAXCONF, 1, "\n");
57 for(i = 0; i < n; i++){
60 cp = strchr(line[i], '=');
64 confname[nconf] = line[i];
80 print("\nPlan 9 (%s)\n", xenstart->magic);
83 // meminit() is not for us
91 if(arch->intrinit) /* launches other processors on an mp */
109 active.thunderbirdsarego = 1;
119 MACHP(0) = (Mach*)CPU0MACH;
120 m->pdb = (ulong*)xenstart->pt_base;
136 memset(m, 0, sizeof(Mach));
142 * For polled uart output at boot, need
143 * a default delay constant. 100000 should
144 * be enough for a while. Cpuidentify will
145 * calculate the real value later.
147 m->loopconst = 100000;
148 m->cpumhz = 1000; // XXX!
150 HYPERVISOR_shared_info = (shared_info_t*)mmumapframe(XENSHARED, (xenstart->shared_info)>>PGSHIFT);
152 // XXX m->shared = &HYPERVISOR_shared_info->vcpu_data[m->machno];
159 char buf[2*KNAMELEN];
166 * These are o.k. because rootinit is null.
167 * Then early kproc's will have a root and dot.
169 up->slash = namec("#/", Atodir, 0, 0);
170 pathclose(up->slash->path);
171 up->slash->path = newpath("/");
172 up->dot = cclone(up->slash);
177 snprint(buf, sizeof(buf), "%s %s", arch->id, conffile);
178 ksetenv("terminal", buf, 0);
179 ksetenv("cputype", "386", 0);
181 ksetenv("service", "cpu", 0);
183 ksetenv("service", "terminal", 0);
184 ksetenv("readparts", "1", 0);
185 for(i = 0; i < nconf; i++){
186 if(confname[i][0] != '*')
187 ksetenv(confname[i], confval[i], 0);
188 ksetenv(confname[i], confval[i], 1);
193 kproc("alarm", alarmkproc, 0);
207 p->egrp = smalloc(sizeof(Egrp));
209 p->fgrp = dupfgrp(nil);
214 kstrdup(&p->text, "*init*");
215 kstrdup(&p->user, eve);
223 * N.B. make sure there's enough space for syscall to check
225 * 4 bytes for gotolabel's return PC
227 p->sched.pc = (ulong)init0;
228 p->sched.sp = (ulong)p->kstack+KSTACK-(sizeof(Sargs)+BY2WD);
233 s = newseg(SG_STACK, USTKTOP-USTKSIZE, USTKSIZE/BY2PG);
235 pg = newpage(1, 0, USTKTOP-BY2PG);
244 s = newseg(SG_TEXT, UTZERO, 1);
247 pg = newpage(1, 0, UTZERO);
248 memset(pg->cachectl, PG_TXTFLUSH, sizeof(pg->cachectl));
250 k = kmap(s->map[0]->pages[0]);
251 memmove((ulong*)VA(k), initcode, sizeof initcode);
274 sp = (uchar*)base + BY2PG - Ustkheadroom;
277 av[ac++] = pusharg("boot");
278 av[ac++] = pusharg("-D");
280 /* 4 byte word align stack */
281 sp = (uchar*)((ulong)sp & ~3);
283 /* build argc, argv on stack */
284 sp -= (ac+1)*sizeof(sp);
286 for(i = 0; i < ac; i++)
287 *lsp++ = av[i] + ((USTKTOP - BY2PG) - base);
289 sp += (USTKTOP - BY2PG) - base - sizeof(ulong);
297 for(i = 0; i < nconf; i++)
298 if(cistrcmp(confname[i], name) == 0)
316 /* convert to name=value\n format */
325 error("kernel configuration too large");
326 memmove(BOOTARGS, p, n);
338 for(i = 0; i < nconf; i++)
339 print("%s=%s\n", confname[i], confval[i]);
341 * all ram above xentop is free, but must be mappable
342 * to virt addrs less than VIRT_START.
344 kpages = PADDR(hypervisor_virt_start)>>PGSHIFT;
345 if(xenstart->nr_pages <= kpages)
346 kpages = xenstart->nr_pages;
348 print("Warning: Plan 9 / Xen limitation - "
349 "using only %lud of %lud available RAM pages\n",
350 kpages, xenstart->nr_pages);
351 xentop = PGROUND(PADDR(xentop));
352 conf.mem[0].npage = kpages - (xentop>>PGSHIFT);
353 conf.mem[0].base = xentop;
355 if(p = getconf("*kernelpercent"))
356 userpcnt = 100 - strtol(p, 0, 0);
361 for(i=0; i<nelem(conf.mem); i++)
362 conf.npage += conf.mem[i].npage;
364 conf.nproc = 100 + ((conf.npage*BY2PG)/MB)*5;
367 if(conf.nproc > 2000)
370 conf.nswap = conf.nproc*80;
376 kpages = conf.npage - (conf.npage*userpcnt)/100;
379 * Hack for the big boys. Only good while physmem < 4GB.
380 * Give the kernel fixed max + enough to allocate the
382 * This is an overestimate as conf.upages < conf.npages.
383 * The patch of nimage is a band-aid, scanning the whole
384 * page list in imagereclaim just takes too long.
386 if(kpages > (64*MB + conf.npage*sizeof(Page))/BY2PG){
387 kpages = (64*MB + conf.npage*sizeof(Page))/BY2PG;
389 kpages += (conf.nproc*KSTACK)/BY2PG;
393 if(conf.npage*BY2PG < 16*MB)
398 kpages = conf.npage - (conf.npage*userpcnt)/100;
401 * Make sure terminals with low memory get at least
402 * 4MB on the first Image chunk allocation.
404 if(conf.npage*BY2PG < 16*MB)
405 imagmem->minarena = 4*1024*1024;
409 * can't go past the end of virtual memory
410 * (ulong)-KZERO is 2^32 - KZERO
412 if(kpages > ((ulong)-KZERO)/BY2PG)
413 kpages = ((ulong)-KZERO)/BY2PG;
415 conf.upages = conf.npage - kpages;
416 conf.ialloc = (kpages/2)*BY2PG;
419 * Guess how much is taken by the large permanent
420 * datastructures. Mntcache and Mntrpc are not accounted for.
423 kpages -= conf.upages*sizeof(Page)
424 + conf.nproc*sizeof(Proc)
425 + conf.nimage*sizeof(Image)
427 + conf.nswppo*sizeof(Page*);
428 mainmem->maxsize = kpages;
431 * give terminals lots of image memory, too; the dynamic
432 * allocation will balance the load properly, hopefully.
433 * be careful with 32-bit overflow.
435 imagmem->maxsize = kpages;
439 static char* mathmsg[] =
441 nil, /* handled below */
442 "denormalized operand",
454 char *msg, note[ERRMAX];
456 status = up->fpsave.status;
459 * Some attention should probably be paid here to the
460 * exception masks and error summary.
462 msg = "unknown exception";
463 for(i = 1; i <= 5; i++){
464 if(!((1<<i) & status))
472 msg = "stack overflow";
474 msg = "stack underflow";
476 msg = "invalid operation";
478 snprint(note, sizeof note, "sys: fp: %s fppc=0x%lux status=0x%lux",
479 msg, up->fpsave.pc, status);
480 postnote(up, 1, note, NDebug);
484 * math coprocessor error
487 matherror(Ureg *ur, void*)
490 * a write cycle to port 0xF0 clears the interrupt latch attached
491 * to the error# line from the 387
493 if(!(m->cpuiddx & 0x01))
497 * save floating point state to check out error
503 panic("fp: status %ux fppc=0x%lux pc=0x%lux",
504 up->fpsave.status, up->fpsave.pc, ur->pc);
508 * math coprocessor emulation fault
511 mathemu(Ureg *ureg, void*)
513 if(up->fpstate & FPillegal){
514 /* someone did floating point in a note handler */
515 postnote(up, 1, "sys: floating point in note handler", NDebug);
521 up->fpstate = FPactive;
525 * Before restoring the state, check for any pending
526 * exceptions, there's no way to restore the state without
527 * generating an unmasked exception.
528 * More attention should probably be paid here to the
529 * exception masks and error summary.
531 if((up->fpsave.status & ~up->fpsave.control) & 0x07F){
535 fprestore(&up->fpsave);
536 up->fpstate = FPactive;
539 panic("math emu pid %ld %s pc 0x%lux",
540 up->pid, up->text, ureg->pc);
546 * math coprocessor segment overrun
549 mathover(Ureg*, void*)
551 pexit("math overrun", 0);
557 trapenable(VectorCERR, matherror, 0, "matherror");
558 //if(X86FAMILY(m->cpuidax) == 3)
559 // intrenable(IrqIRQ13, matherror, 0, BUSUNKNOWN, "matherror");
560 trapenable(VectorCNA, mathemu, 0, "mathemu");
561 trapenable(VectorCSO, mathover, 0, "mathover");
565 * set up floating point for a new process
579 p->kentry = up->kentry;
580 p->pcycles = -p->kentry;
582 /* save floating point state */
584 switch(up->fpstate & ~FPillegal){
587 up->fpstate = FPinactive;
589 p->fpsave = up->fpsave;
590 p->fpstate = FPinactive;
607 * Save the mach dependent part of the process state.
616 if(p->fpstate == FPactive){
617 if(p->state == Moribund)
621 * Fpsave() stores without handling pending
622 * unmasked exeptions. Postnote() can't be called
623 * here as sleep() already has up->rlock, so
624 * the handling of pending exceptions is delayed
625 * until the process runs again and generates an
626 * emulation fault to activate the FPU.
630 p->fpstate = FPinactive;
634 * While this processor is in the scheduler, the process could run
635 * on another processor and exit, returning the page tables to
636 * the free list where they could be reallocated and overwritten.
637 * When this processor eventually has to get an entry from the
638 * trashed page tables it will crash.
640 * If there's only one processor, this can't happen.
641 * You might think it would be a win not to do this in that case,
642 * especially on VMware, but it turns out not to matter.
648 shutdown(int ispanic)
654 active.ispanic = ispanic;
655 else if(m->machno == 0 && (active.machs & (1<<m->machno)) == 0)
657 once = active.machs & (1<<m->machno);
658 active.machs &= ~(1<<m->machno);
663 print("cpu%d: exiting\n", m->machno);
665 for(ms = 5*1000; ms > 0; ms -= TK2MS(2)){
667 if(active.machs == 0 && consactive() == 0)
671 if(getconf("*debug"))
684 reboot(void *entry, void *code, ulong size)
686 void (*f)(ulong, ulong, ulong);
694 * should be the only processor running now
697 print("shutting down...\n");
702 /* turn off buffered serial console */
705 /* shutdown devices */
708 /* reboot(0, ...) on Xen causes domU shutdown */
710 HYPERVISOR_shutdown(0);
713 * Modify the machine page table to directly map the low 4MB of memory
714 * This allows the reboot code to turn off the page mapping
717 //pdb[PDX(0)] = pdb[PDX(KZERO)];
720 /* setup reboot trampoline function */
721 f = (void*)REBOOTADDR;
722 memmove(f, rebootcode, sizeof(rebootcode));
724 print("rebooting...\n");
726 /* off we go - never to return */
727 (*f)(PADDR(entry), PADDR(code), size);