2 #include "../port/lib.h"
19 extern void (*i8237alloc)(void);
20 extern void bootscreeninit(void);
21 extern void multibootdebug(void);
49 if(arch->intrinit) /* launches other processors on an mp */
74 MACHP(0) = (Mach*)CPU0MACH;
75 m->pdb = (ulong*)CPU0PDB;
76 m->gdt = (Segdesc*)CPU0GDT;
94 memset(m, 0, sizeof(Mach));
101 * For polled uart output at boot, need
102 * a default delay constant. 100000 should
103 * be enough for a while. Cpuidentify will
104 * calculate the real value later.
106 m->loopconst = 100000;
112 char buf[2*KNAMELEN], **sp;
119 * These are o.k. because rootinit is null.
120 * Then early kproc's will have a root and dot.
122 up->slash = namec("#/", Atodir, 0, 0);
123 pathclose(up->slash->path);
124 up->slash->path = newpath("/");
125 up->dot = cclone(up->slash);
130 snprint(buf, sizeof(buf), "%s %s", arch->id, conffile);
131 ksetenv("terminal", buf, 0);
132 ksetenv("cputype", "386", 0);
134 ksetenv("service", "cpu", 0);
136 ksetenv("service", "terminal", 0);
140 kproc("alarm", alarmkproc, 0);
142 sp = (char**)(USTKTOP - sizeof(Tos) - 8 - sizeof(sp[0])*4);
144 strcpy(sp[1] = (char*)&sp[4], "boot");
159 p->egrp = smalloc(sizeof(Egrp));
161 p->fgrp = dupfgrp(nil);
166 kstrdup(&p->text, "*init*");
167 kstrdup(&p->user, eve);
174 * N.B. make sure there's enough space for syscall to check
176 * 4 bytes for gotolabel's return PC
178 p->sched.pc = (ulong)init0;
179 p->sched.sp = (ulong)p->kstack+KSTACK-(sizeof(Sargs)+BY2WD);
184 * N.B. cannot call newpage() with clear=1, because pc kmap
185 * requires up != nil. use tmpmap instead.
187 s = newseg(SG_STACK, USTKTOP-USTKSIZE, USTKSIZE/BY2PG);
189 pg = newpage(0, 0, USTKTOP-BY2PG);
198 s = newseg(SG_TEXT, UTZERO, 1);
201 pg = newpage(0, 0, UTZERO);
206 memmove(v, initcode, sizeof initcode);
219 if(p = getconf("service")){
220 if(strcmp(p, "cpu") == 0)
222 else if(strcmp(p,"terminal") == 0)
226 if(p = getconf("*kernelpercent"))
227 userpcnt = 100 - strtol(p, 0, 0);
232 for(i=0; i<nelem(conf.mem); i++)
233 conf.npage += conf.mem[i].npage;
235 conf.nproc = 100 + ((conf.npage*BY2PG)/MB)*5;
238 if(conf.nproc > 2000)
241 conf.nswap = conf.nproc*80;
247 kpages = conf.npage - (conf.npage*userpcnt)/100;
248 conf.nimage = conf.nproc;
251 * Hack for the big boys. Only good while physmem < 4GB.
252 * Give the kernel fixed max + enough to allocate the
254 * This is an overestimate as conf.upages < conf.npages.
255 * The patch of nimage is a band-aid, scanning the whole
256 * page list in imagereclaim just takes too long.
258 if(getconf("*imagemaxmb") == 0)
259 if(kpages > (64*MB + conf.npage*sizeof(Page))/BY2PG){
260 kpages = (64*MB + conf.npage*sizeof(Page))/BY2PG;
261 kpages += (conf.nproc*KSTACK)/BY2PG;
265 if(conf.npage*BY2PG < 16*MB)
270 kpages = conf.npage - (conf.npage*userpcnt)/100;
273 * Make sure terminals with low memory get at least
274 * 4MB on the first Image chunk allocation.
276 if(conf.npage*BY2PG < 16*MB)
277 imagmem->minarena = 4*MB;
281 * can't go past the end of virtual memory
282 * (ulong)-KZERO is 2^32 - KZERO
284 if(kpages > ((ulong)-KZERO)/BY2PG)
285 kpages = ((ulong)-KZERO)/BY2PG;
287 conf.upages = conf.npage - kpages;
288 conf.ialloc = (kpages/2)*BY2PG;
291 * Guess how much is taken by the large permanent
292 * datastructures. Mntcache and Mntrpc are not accounted for.
295 kpages -= conf.upages*sizeof(Page)
296 + conf.nproc*sizeof(Proc)
297 + conf.nimage*sizeof(Image)
299 + conf.nswppo*sizeof(Page*);
300 mainmem->maxsize = kpages;
303 * the dynamic allocation will balance the load properly,
304 * hopefully. be careful with 32-bit overflow.
306 imagmem->maxsize = kpages - (kpages/10);
307 if(p = getconf("*imagemaxmb")){
308 imagmem->maxsize = strtol(p, nil, 0)*MB;
309 if(imagmem->maxsize > mainmem->maxsize)
310 imagmem->maxsize = mainmem->maxsize;
315 * we keep FPsave structure in sse format emulating FXSAVE / FXRSTOR
316 * instructions for legacy x87 fpu.
318 * Note that fpx87restore() and fpxsserestore() do modify the FPsave
319 * data structure for conversion / realignment shuffeling. this means
320 * that p->fpsave is only valid when p->fpstate == FPinactive.
323 fpx87save(FPsave *fps)
330 * convert x87 tag word to fxsave tag byte:
331 * 00, 01, 10 -> 1, 11 -> 0
334 tag = (tag | (tag >> 1)) & 0x5555;
335 tag = (tag | (tag >> 1)) & 0x3333;
336 tag = (tag | (tag >> 2)) & 0x0F0F;
337 tag = (tag | (tag >> 4)) & 0x00FF;
339 /* NOP fps->fcw = fps->control; */
340 fps->fsw = fps->status;
342 fps->fop = fps->opcode;
343 fps->fpuip = fps->pc;
344 fps->cs = fps->selector;
345 fps->fpudp = fps->operand;
346 fps->ds = fps->oselector;
349 *((ushort*)(d+8)) = *((ushort*)(s+8)), \
350 *((ulong*)(d+4)) = *((ulong*)(s+4)), \
351 *((ulong*)(d)) = *((ulong*)(s))
353 MOVA(fps->xregs+0x70, fps->regs+70);
354 MOVA(fps->xregs+0x60, fps->regs+60);
355 MOVA(fps->xregs+0x50, fps->regs+50);
356 MOVA(fps->xregs+0x40, fps->regs+40);
357 MOVA(fps->xregs+0x30, fps->regs+30);
358 MOVA(fps->xregs+0x20, fps->regs+20);
359 MOVA(fps->xregs+0x10, fps->regs+10);
360 MOVA(fps->xregs+0x00, fps->regs+00);
365 *((ulong*)(d)) = 0, \
366 *((ushort*)(d+4)) = 0
368 CLR6(fps->xregs+0x70+10);
369 CLR6(fps->xregs+0x60+10);
370 CLR6(fps->xregs+0x50+10);
371 CLR6(fps->xregs+0x40+10);
372 CLR6(fps->xregs+0x30+10);
373 CLR6(fps->xregs+0x20+10);
374 CLR6(fps->xregs+0x10+10);
375 CLR6(fps->xregs+0x00+10);
379 fps->rsrvd1 = fps->rsrvd2 = fps->mxcsr = fps->mxcsr_mask = 0;
383 fpx87restore(FPsave *fps)
385 ushort msk, tos, tag, *reg;
387 /* convert fxsave tag byte to x87 tag word */
389 tos = 7 - ((fps->fsw >> 11) & 7);
390 for(msk = 0x80; msk != 0; tos--, msk >>= 1){
392 if((fps->ftw & msk) != 0){
393 reg = (ushort*)&fps->xregs[(tos & 7) << 4];
394 switch(reg[4] & 0x7fff){
396 if((reg[0] | reg[1] | reg[2] | reg[3]) == 0){
397 tag |= 1; /* 01 zero */
402 tag |= 2; /* 10 special */
405 if((reg[3] & 0x8000) == 0)
406 break; /* 00 valid */
407 tag |= 2; /* 10 special */
411 tag |= 3; /* 11 empty */
416 *((ulong*)(d)) = *((ulong*)(s)), \
417 *((ulong*)(d+4)) = *((ulong*)(s+4)), \
418 *((ushort*)(d+8)) = *((ushort*)(s+8))
420 MOVA(fps->regs+00, fps->xregs+0x00);
421 MOVA(fps->regs+10, fps->xregs+0x10);
422 MOVA(fps->regs+20, fps->xregs+0x20);
423 MOVA(fps->regs+30, fps->xregs+0x30);
424 MOVA(fps->regs+40, fps->xregs+0x40);
425 MOVA(fps->regs+50, fps->xregs+0x50);
426 MOVA(fps->regs+60, fps->xregs+0x60);
427 MOVA(fps->regs+70, fps->xregs+0x70);
431 fps->oselector = fps->ds;
432 fps->operand = fps->fpudp;
433 fps->opcode = fps->fop & 0x7ff;
434 fps->selector = fps->cs;
435 fps->pc = fps->fpuip;
437 fps->status = fps->fsw;
438 /* NOP fps->control = fps->fcw; */
440 fps->r1 = fps->r2 = fps->r3 = fps->r4 = 0;
446 * sse fp save and restore buffers have to be 16-byte (FPalign) aligned,
447 * so we shuffle the data up and down as needed or make copies.
450 fpssesave(FPsave *fps)
454 afps = (FPsave *)ROUND(((uintptr)fps), FPalign);
456 if(fps != afps) /* not aligned? shuffle down from aligned buffer */
457 memmove(fps, afps, sizeof(FPssestate) - FPalign);
461 fpsserestore(FPsave *fps)
465 afps = (FPsave *)ROUND(((uintptr)fps), FPalign);
466 if(fps != afps) /* shuffle up to make aligned */
467 memmove(afps, fps, sizeof(FPssestate) - FPalign);
471 static char* mathmsg[] =
473 nil, /* handled below */
474 "denormalized operand",
482 mathnote(ulong status, ulong pc)
484 char *msg, note[ERRMAX];
488 * Some attention should probably be paid here to the
489 * exception masks and error summary.
491 msg = "unknown exception";
492 for(i = 1; i <= 5; i++){
493 if(!((1<<i) & status))
501 msg = "stack overflow";
503 msg = "stack underflow";
505 msg = "invalid operation";
507 snprint(note, sizeof note, "sys: fp: %s fppc=0x%lux status=0x%lux",
509 postnote(up, 1, note, NDebug);
513 * math coprocessor error
516 matherror(Ureg*, void*)
519 * a write cycle to port 0xF0 clears the interrupt latch attached
520 * to the error# line from the 387
522 if(!(m->cpuiddx & Fpuonchip))
526 * get floating point state to check out error
529 up->fpstate = FPinactive;
530 mathnote(up->fpsave.fsw, up->fpsave.fpuip);
537 simderror(Ureg *ureg, void*)
540 up->fpstate = FPinactive;
541 mathnote(up->fpsave.mxcsr & 0x3f, ureg->pc);
545 * math coprocessor emulation fault
548 mathemu(Ureg *ureg, void*)
550 ulong status, control;
552 if(up->fpstate & FPillegal){
553 /* someone did floating point in a note handler */
554 postnote(up, 1, "sys: floating point in note handler", NDebug);
560 if(fpsave == fpssesave)
561 ldmxcsr(0); /* no simd exceptions on 386 */
562 up->fpstate = FPactive;
566 * Before restoring the state, check for any pending
567 * exceptions, there's no way to restore the state without
568 * generating an unmasked exception.
569 * More attention should probably be paid here to the
570 * exception masks and error summary.
572 status = up->fpsave.fsw;
573 control = up->fpsave.fcw;
574 if((status & ~control) & 0x07F){
575 mathnote(status, up->fpsave.fpuip);
578 fprestore(&up->fpsave);
579 up->fpstate = FPactive;
582 panic("math emu pid %ld %s pc 0x%lux",
583 up->pid, up->text, ureg->pc);
589 * math coprocessor segment overrun
592 mathover(Ureg*, void*)
594 pexit("math overrun", 0);
600 trapenable(VectorCERR, matherror, 0, "matherror");
601 if(X86FAMILY(m->cpuidax) == 3)
602 intrenable(IrqIRQ13, matherror, 0, BUSUNKNOWN, "matherror");
603 trapenable(VectorCNA, mathemu, 0, "mathemu");
604 trapenable(VectorCSO, mathover, 0, "mathover");
605 trapenable(VectorSIMD, simderror, 0, "simderror");
609 * set up floating point for a new process
618 p->pcycles = -p->kentry;
620 memset(p->gdt, 0, sizeof(p->gdt));
624 memset(p->dr, 0, sizeof(p->dr));
632 p->kentry = up->kentry;
633 p->pcycles = -p->kentry;
635 /* inherit user descriptors */
636 memmove(p->gdt, up->gdt, sizeof(p->gdt));
638 /* copy local descriptor table */
639 if(up->ldt != nil && up->nldt > 0){
640 p->ldt = smalloc(sizeof(Segdesc) * up->nldt);
641 memmove(p->ldt, up->ldt, sizeof(Segdesc) * up->nldt);
645 /* save floating point state */
647 switch(up->fpstate & ~FPillegal){
650 up->fpstate = FPinactive;
652 p->fpsave = up->fpsave;
653 p->fpstate = FPinactive;
656 /* clear debug registers */
657 memset(p->dr, 0, sizeof(p->dr));
683 * Save the mach dependent part of the process state.
690 /* we could just always putdr7(0) but accessing DR7 might be slow in a VM */
700 if(p->fpstate == FPactive){
701 if(p->state == Moribund)
705 * Fpsave() stores without handling pending
706 * unmasked exeptions. Postnote() can't be called
707 * here as sleep() already has up->rlock, so
708 * the handling of pending exceptions is delayed
709 * until the process runs again and generates an
710 * emulation fault to activate the FPU.
714 p->fpstate = FPinactive;
718 * While this processor is in the scheduler, the process could run
719 * on another processor and exit, returning the page tables to
720 * the free list where they could be reallocated and overwritten.
721 * When this processor eventually has to get an entry from the
722 * trashed page tables it will crash.
724 * If there's only one processor, this can't happen.
725 * You might think it would be a win not to do this in that case,
726 * especially on VMware, but it turns out not to matter.
728 mmuflushtlb(PADDR(m->pdb));
732 reboot(void *entry, void *code, ulong size)
734 void (*f)(ulong, ulong, ulong);
740 * the boot processor is cpu0. execute this function on it
741 * so that the new kernel has the same cpu0. this only matters
742 * because the hardware has a notion of which processor was the
743 * boot processor and we look at it at start up.
745 if (m->machno != 0) {
753 /* turn off buffered serial console */
756 /* shutdown devices */
761 * Modify the machine page table to directly map the low 4MB of memory
762 * This allows the reboot code to turn off the page mapping
765 pdb[PDX(0)] = pdb[PDX(KZERO)];
766 mmuflushtlb(PADDR(pdb));
768 /* setup reboot trampoline function */
769 f = (void*)REBOOTADDR;
770 memmove(f, rebootcode, sizeof(rebootcode));
772 /* off we go - never to return */
774 (*f)((ulong)entry & ~0xF0000000UL, PADDR(code), size);