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 */
118 MACHP(0) = (Mach*)CPU0MACH;
119 m->pdb = (ulong*)xenstart->pt_base;
135 memset(m, 0, sizeof(Mach));
141 * For polled uart output at boot, need
142 * a default delay constant. 100000 should
143 * be enough for a while. Cpuidentify will
144 * calculate the real value later.
146 m->loopconst = 100000;
147 m->cpumhz = 1000; // XXX!
149 HYPERVISOR_shared_info = (shared_info_t*)mmumapframe(XENSHARED, (xenstart->shared_info)>>PGSHIFT);
151 // XXX m->shared = &HYPERVISOR_shared_info->vcpu_data[m->machno];
158 char buf[2*KNAMELEN];
165 * These are o.k. because rootinit is null.
166 * Then early kproc's will have a root and dot.
168 up->slash = namec("#/", Atodir, 0, 0);
169 pathclose(up->slash->path);
170 up->slash->path = newpath("/");
171 up->dot = cclone(up->slash);
176 snprint(buf, sizeof(buf), "%s %s", arch->id, conffile);
177 ksetenv("terminal", buf, 0);
178 ksetenv("cputype", "386", 0);
180 ksetenv("service", "cpu", 0);
182 ksetenv("service", "terminal", 0);
183 ksetenv("readparts", "1", 0);
184 for(i = 0; i < nconf; i++){
185 if(confname[i][0] != '*')
186 ksetenv(confname[i], confval[i], 0);
187 ksetenv(confname[i], confval[i], 1);
192 kproc("alarm", alarmkproc, 0);
206 p->egrp = smalloc(sizeof(Egrp));
208 p->fgrp = dupfgrp(nil);
213 kstrdup(&p->text, "*init*");
214 kstrdup(&p->user, eve);
222 * N.B. make sure there's enough space for syscall to check
224 * 4 bytes for gotolabel's return PC
226 p->sched.pc = (ulong)init0;
227 p->sched.sp = (ulong)p->kstack+KSTACK-(sizeof(Sargs)+BY2WD);
232 s = newseg(SG_STACK, USTKTOP-USTKSIZE, USTKSIZE/BY2PG);
234 pg = newpage(1, 0, USTKTOP-BY2PG);
243 s = newseg(SG_TEXT, UTZERO, 1);
246 pg = newpage(1, 0, UTZERO);
249 k = kmap(s->map[0]->pages[0]);
250 memmove((ulong*)VA(k), initcode, sizeof initcode);
273 sp = (uchar*)base + BY2PG - Ustkheadroom;
276 av[ac++] = pusharg("boot");
277 av[ac++] = pusharg("-D");
279 /* 4 byte word align stack */
280 sp = (uchar*)((ulong)sp & ~3);
282 /* build argc, argv on stack */
283 sp -= (ac+1)*sizeof(sp);
285 for(i = 0; i < ac; i++)
286 *lsp++ = av[i] + ((USTKTOP - BY2PG) - base);
288 sp += (USTKTOP - BY2PG) - base - sizeof(ulong);
296 for(i = 0; i < nconf; i++)
297 if(cistrcmp(confname[i], name) == 0)
315 /* convert to name=value\n format */
324 error("kernel configuration too large");
325 memmove(BOOTARGS, p, n);
337 for(i = 0; i < nconf; i++)
338 print("%s=%s\n", confname[i], confval[i]);
340 * all ram above xentop is free, but must be mappable
341 * to virt addrs less than VIRT_START.
343 kpages = PADDR(hypervisor_virt_start)>>PGSHIFT;
344 if(xenstart->nr_pages <= kpages)
345 kpages = xenstart->nr_pages;
347 print("Warning: Plan 9 / Xen limitation - "
348 "using only %lud of %lud available RAM pages\n",
349 kpages, xenstart->nr_pages);
350 xentop = PGROUND(PADDR(xentop));
351 conf.mem[0].npage = kpages - (xentop>>PGSHIFT);
352 conf.mem[0].base = xentop;
354 if(p = getconf("*kernelpercent"))
355 userpcnt = 100 - strtol(p, 0, 0);
360 for(i=0; i<nelem(conf.mem); i++)
361 conf.npage += conf.mem[i].npage;
363 conf.nproc = 100 + ((conf.npage*BY2PG)/MB)*5;
366 if(conf.nproc > 2000)
369 conf.nswap = conf.nproc*80;
375 kpages = conf.npage - (conf.npage*userpcnt)/100;
378 * Hack for the big boys. Only good while physmem < 4GB.
379 * Give the kernel fixed max + enough to allocate the
381 * This is an overestimate as conf.upages < conf.npages.
382 * The patch of nimage is a band-aid, scanning the whole
383 * page list in imagereclaim just takes too long.
385 if(kpages > (64*MB + conf.npage*sizeof(Page))/BY2PG){
386 kpages = (64*MB + conf.npage*sizeof(Page))/BY2PG;
388 kpages += (conf.nproc*KSTACK)/BY2PG;
392 if(conf.npage*BY2PG < 16*MB)
397 kpages = conf.npage - (conf.npage*userpcnt)/100;
400 * Make sure terminals with low memory get at least
401 * 4MB on the first Image chunk allocation.
403 if(conf.npage*BY2PG < 16*MB)
404 imagmem->minarena = 4*1024*1024;
408 * can't go past the end of virtual memory
409 * (ulong)-KZERO is 2^32 - KZERO
411 if(kpages > ((ulong)-KZERO)/BY2PG)
412 kpages = ((ulong)-KZERO)/BY2PG;
414 conf.upages = conf.npage - kpages;
415 conf.ialloc = (kpages/2)*BY2PG;
418 * Guess how much is taken by the large permanent
419 * datastructures. Mntcache and Mntrpc are not accounted for.
422 kpages -= conf.upages*sizeof(Page)
423 + conf.nproc*sizeof(Proc)
424 + conf.nimage*sizeof(Image)
426 + conf.nswppo*sizeof(Page*);
427 mainmem->maxsize = kpages;
430 * give terminals lots of image memory, too; the dynamic
431 * allocation will balance the load properly, hopefully.
432 * be careful with 32-bit overflow.
434 imagmem->maxsize = kpages;
438 static char* mathmsg[] =
440 nil, /* handled below */
441 "denormalized operand",
453 char *msg, note[ERRMAX];
455 status = up->fpsave.status;
458 * Some attention should probably be paid here to the
459 * exception masks and error summary.
461 msg = "unknown exception";
462 for(i = 1; i <= 5; i++){
463 if(!((1<<i) & status))
471 msg = "stack overflow";
473 msg = "stack underflow";
475 msg = "invalid operation";
477 snprint(note, sizeof note, "sys: fp: %s fppc=0x%lux status=0x%lux",
478 msg, up->fpsave.pc, status);
479 postnote(up, 1, note, NDebug);
483 * math coprocessor error
486 matherror(Ureg *ur, void*)
489 * a write cycle to port 0xF0 clears the interrupt latch attached
490 * to the error# line from the 387
492 if(!(m->cpuiddx & 0x01))
496 * save floating point state to check out error
502 panic("fp: status %ux fppc=0x%lux pc=0x%lux",
503 up->fpsave.status, up->fpsave.pc, ur->pc);
507 * math coprocessor emulation fault
510 mathemu(Ureg *ureg, void*)
512 if(up->fpstate & FPillegal){
513 /* someone did floating point in a note handler */
514 postnote(up, 1, "sys: floating point in note handler", NDebug);
520 up->fpstate = FPactive;
524 * Before restoring the state, check for any pending
525 * exceptions, there's no way to restore the state without
526 * generating an unmasked exception.
527 * More attention should probably be paid here to the
528 * exception masks and error summary.
530 if((up->fpsave.status & ~up->fpsave.control) & 0x07F){
534 fprestore(&up->fpsave);
535 up->fpstate = FPactive;
538 panic("math emu pid %ld %s pc 0x%lux",
539 up->pid, up->text, ureg->pc);
545 * math coprocessor segment overrun
548 mathover(Ureg*, void*)
550 pexit("math overrun", 0);
556 trapenable(VectorCERR, matherror, 0, "matherror");
557 //if(X86FAMILY(m->cpuidax) == 3)
558 // intrenable(IrqIRQ13, matherror, 0, BUSUNKNOWN, "matherror");
559 trapenable(VectorCNA, mathemu, 0, "mathemu");
560 trapenable(VectorCSO, mathover, 0, "mathover");
564 * set up floating point for a new process
578 p->kentry = up->kentry;
579 p->pcycles = -p->kentry;
581 /* save floating point state */
583 switch(up->fpstate & ~FPillegal){
586 up->fpstate = FPinactive;
588 p->fpsave = up->fpsave;
589 p->fpstate = FPinactive;
606 * Save the mach dependent part of the process state.
615 if(p->fpstate == FPactive){
616 if(p->state == Moribund)
620 * Fpsave() stores without handling pending
621 * unmasked exeptions. Postnote() can't be called
622 * here as sleep() already has up->rlock, so
623 * the handling of pending exceptions is delayed
624 * until the process runs again and generates an
625 * emulation fault to activate the FPU.
629 p->fpstate = FPinactive;
633 * While this processor is in the scheduler, the process could run
634 * on another processor and exit, returning the page tables to
635 * the free list where they could be reallocated and overwritten.
636 * When this processor eventually has to get an entry from the
637 * trashed page tables it will crash.
639 * If there's only one processor, this can't happen.
640 * You might think it would be a win not to do this in that case,
641 * especially on VMware, but it turns out not to matter.
647 reboot(void *entry, void *code, ulong size)
649 void (*f)(ulong, ulong, ulong);
656 /* turn off buffered serial console */
659 /* shutdown devices */
662 /* reboot(0, ...) on Xen causes domU shutdown */
664 HYPERVISOR_shutdown(0);
668 /* setup reboot trampoline function */
669 f = (void*)REBOOTADDR;
670 memmove(f, rebootcode, sizeof(rebootcode));
672 /* off we go - never to return */
673 (*f)(PADDR(entry), PADDR(code), size);