2 #include "../port/lib.h"
19 extern void (*i8237alloc)(void);
20 extern void bootscreeninit(void);
21 extern void multibootdebug(void);
50 if(arch->intrinit) /* launches other processors on an mp */
73 MACHP(0) = (Mach*)CPU0MACH;
74 m->pdb = (ulong*)CPU0PDB;
75 m->gdt = (Segdesc*)CPU0GDT;
93 memset(m, 0, sizeof(Mach));
100 * For polled uart output at boot, need
101 * a default delay constant. 100000 should
102 * be enough for a while. Cpuidentify will
103 * calculate the real value later.
105 m->loopconst = 100000;
111 char buf[2*KNAMELEN], **sp;
118 * These are o.k. because rootinit is null.
119 * Then early kproc's will have a root and dot.
121 up->slash = namec("#/", Atodir, 0, 0);
122 pathclose(up->slash->path);
123 up->slash->path = newpath("/");
124 up->dot = cclone(up->slash);
129 snprint(buf, sizeof(buf), "%s %s", arch->id, conffile);
130 ksetenv("terminal", buf, 0);
131 ksetenv("cputype", "386", 0);
133 ksetenv("service", "cpu", 0);
135 ksetenv("service", "terminal", 0);
139 kproc("alarm", alarmkproc, 0);
141 sp = (char**)(USTKTOP - sizeof(Tos) - 8 - sizeof(sp[0])*4);
143 strcpy(sp[1] = (char*)&sp[4], "boot");
158 p->egrp = smalloc(sizeof(Egrp));
160 p->fgrp = dupfgrp(nil);
165 kstrdup(&p->text, "*init*");
166 kstrdup(&p->user, eve);
173 * N.B. make sure there's enough space for syscall to check
175 * 4 bytes for gotolabel's return PC
177 p->sched.pc = (ulong)init0;
178 p->sched.sp = (ulong)p->kstack+KSTACK-(sizeof(Sargs)+BY2WD);
183 * N.B. cannot call newpage() with clear=1, because pc kmap
184 * requires up != nil. use tmpmap instead.
186 s = newseg(SG_STACK, USTKTOP-USTKSIZE, USTKSIZE/BY2PG);
188 pg = newpage(0, 0, USTKTOP-BY2PG);
197 s = newseg(SG_TEXT, UTZERO, 1);
200 pg = newpage(0, 0, UTZERO);
205 memmove(v, initcode, sizeof initcode);
218 if(p = getconf("service")){
219 if(strcmp(p, "cpu") == 0)
221 else if(strcmp(p,"terminal") == 0)
225 if(p = getconf("*kernelpercent"))
226 userpcnt = 100 - strtol(p, 0, 0);
231 for(i=0; i<nelem(conf.mem); i++)
232 conf.npage += conf.mem[i].npage;
234 conf.nproc = 100 + ((conf.npage*BY2PG)/MB)*5;
237 if(conf.nproc > 2000)
240 conf.nswap = conf.nproc*80;
246 kpages = conf.npage - (conf.npage*userpcnt)/100;
247 conf.nimage = conf.nproc;
250 * Hack for the big boys. Only good while physmem < 4GB.
251 * Give the kernel fixed max + enough to allocate the
253 * This is an overestimate as conf.upages < conf.npages.
254 * The patch of nimage is a band-aid, scanning the whole
255 * page list in imagereclaim just takes too long.
257 if(getconf("*imagemaxmb") == 0)
258 if(kpages > (64*MB + conf.npage*sizeof(Page))/BY2PG){
259 kpages = (64*MB + conf.npage*sizeof(Page))/BY2PG;
260 kpages += (conf.nproc*KSTACK)/BY2PG;
264 if(conf.npage*BY2PG < 16*MB)
269 kpages = conf.npage - (conf.npage*userpcnt)/100;
272 * Make sure terminals with low memory get at least
273 * 4MB on the first Image chunk allocation.
275 if(conf.npage*BY2PG < 16*MB)
276 imagmem->minarena = 4*MB;
280 * can't go past the end of virtual memory
281 * (ulong)-KZERO is 2^32 - KZERO
283 if(kpages > ((ulong)-KZERO)/BY2PG)
284 kpages = ((ulong)-KZERO)/BY2PG;
286 conf.upages = conf.npage - kpages;
287 conf.ialloc = (kpages/2)*BY2PG;
290 * Guess how much is taken by the large permanent
291 * datastructures. Mntcache and Mntrpc are not accounted for.
294 kpages -= conf.upages*sizeof(Page)
295 + conf.nproc*sizeof(Proc)
296 + conf.nimage*sizeof(Image)
298 + conf.nswppo*sizeof(Page*);
299 mainmem->maxsize = kpages;
302 * the dynamic allocation will balance the load properly,
303 * hopefully. be careful with 32-bit overflow.
305 imagmem->maxsize = kpages - (kpages/10);
306 if(p = getconf("*imagemaxmb")){
307 imagmem->maxsize = strtol(p, nil, 0)*MB;
308 if(imagmem->maxsize > mainmem->maxsize)
309 imagmem->maxsize = mainmem->maxsize;
314 * we keep FPsave structure in SSE format emulating FXSAVE / FXRSTOR
315 * instructions for legacy x87 fpu.
318 fpx87save(FPsave *fps)
325 * convert x87 tag word to fxsave tag byte:
326 * 00, 01, 10 -> 1, 11 -> 0
329 tag = (tag | (tag >> 1)) & 0x5555;
330 tag = (tag | (tag >> 1)) & 0x3333;
331 tag = (tag | (tag >> 2)) & 0x0F0F;
332 tag = (tag | (tag >> 4)) & 0x00FF;
334 /* NOP fps->fcw = fps->control; */
335 fps->fsw = fps->status;
337 fps->fop = fps->opcode;
338 fps->fpuip = fps->pc;
339 fps->cs = fps->selector;
340 fps->fpudp = fps->operand;
341 fps->ds = fps->oselector;
344 *((ushort*)(d+8)) = *((ushort*)(s+8)), \
345 *((ulong*)(d+4)) = *((ulong*)(s+4)), \
346 *((ulong*)(d)) = *((ulong*)(s))
348 MOVA(fps->xregs+0x70, fps->regs+70);
349 MOVA(fps->xregs+0x60, fps->regs+60);
350 MOVA(fps->xregs+0x50, fps->regs+50);
351 MOVA(fps->xregs+0x40, fps->regs+40);
352 MOVA(fps->xregs+0x30, fps->regs+30);
353 MOVA(fps->xregs+0x20, fps->regs+20);
354 MOVA(fps->xregs+0x10, fps->regs+10);
355 MOVA(fps->xregs+0x00, fps->regs+00);
360 *((ulong*)(d)) = 0, \
361 *((ushort*)(d+4)) = 0
363 CLR6(fps->xregs+0x70+10);
364 CLR6(fps->xregs+0x60+10);
365 CLR6(fps->xregs+0x50+10);
366 CLR6(fps->xregs+0x40+10);
367 CLR6(fps->xregs+0x30+10);
368 CLR6(fps->xregs+0x20+10);
369 CLR6(fps->xregs+0x10+10);
370 CLR6(fps->xregs+0x00+10);
374 fps->rsrvd1 = fps->rsrvd2 = fps->mxcsr = fps->mxcsr_mask = 0;
378 fpx87restore(FPsave *fps)
380 ushort msk, tos, tag, *reg;
382 /* convert fxsave tag byte to x87 tag word */
384 tos = 7 - ((fps->fsw >> 11) & 7);
385 for(msk = 0x80; msk != 0; tos--, msk >>= 1){
387 if((fps->ftw & msk) != 0){
388 reg = (ushort*)&fps->xregs[(tos & 7) << 4];
389 switch(reg[4] & 0x7fff){
391 if((reg[0] | reg[1] | reg[2] | reg[3]) == 0){
392 tag |= 1; /* 01 zero */
397 tag |= 2; /* 10 special */
400 if((reg[3] & 0x8000) == 0)
401 break; /* 00 valid */
402 tag |= 2; /* 10 special */
406 tag |= 3; /* 11 empty */
411 *((ulong*)(d)) = *((ulong*)(s)), \
412 *((ulong*)(d+4)) = *((ulong*)(s+4)), \
413 *((ushort*)(d+8)) = *((ushort*)(s+8))
415 MOVA(fps->regs+00, fps->xregs+0x00);
416 MOVA(fps->regs+10, fps->xregs+0x10);
417 MOVA(fps->regs+20, fps->xregs+0x20);
418 MOVA(fps->regs+30, fps->xregs+0x30);
419 MOVA(fps->regs+40, fps->xregs+0x40);
420 MOVA(fps->regs+50, fps->xregs+0x50);
421 MOVA(fps->regs+60, fps->xregs+0x60);
422 MOVA(fps->regs+70, fps->xregs+0x70);
426 fps->oselector = fps->ds;
427 fps->operand = fps->fpudp;
428 fps->opcode = fps->fop & 0x7ff;
429 fps->selector = fps->cs;
430 fps->pc = fps->fpuip;
432 fps->status = fps->fsw;
433 /* NOP fps->control = fps->fcw; */
435 fps->r1 = fps->r2 = fps->r3 = fps->r4 = 0;
440 static char* mathmsg[] =
442 nil, /* handled below */
443 "denormalized operand",
451 mathnote(ulong status, ulong pc)
453 char *msg, note[ERRMAX];
457 * Some attention should probably be paid here to the
458 * exception masks and error summary.
460 msg = "unknown exception";
461 for(i = 1; i <= 5; i++){
462 if(!((1<<i) & status))
470 msg = "stack overflow";
472 msg = "stack underflow";
474 msg = "invalid operation";
476 snprint(note, sizeof note, "sys: fp: %s fppc=0x%lux status=0x%lux",
478 postnote(up, 1, note, NDebug);
482 * math coprocessor error
485 matherror(Ureg*, void*)
488 * a write cycle to port 0xF0 clears the interrupt latch attached
489 * to the error# line from the 387
491 if(!(m->cpuiddx & Fpuonchip))
495 * get floating point state to check out error
498 up->fpstate = FPinactive;
499 mathnote(up->fpsave->fsw, up->fpsave->fpuip);
506 simderror(Ureg *ureg, void*)
509 up->fpstate = FPinactive;
510 mathnote(up->fpsave->mxcsr & 0x3f, ureg->pc);
514 * math coprocessor emulation fault
517 mathemu(Ureg *ureg, void*)
519 ulong status, control;
521 if(up->fpstate & FPillegal){
522 /* someone did floating point in a note handler */
523 postnote(up, 1, "sys: floating point in note handler", NDebug);
529 if(fpsave == fpssesave)
530 ldmxcsr(0x1f80); /* no simd exceptions on 386 */
531 while(up->fpsave == nil)
532 up->fpsave = mallocalign(sizeof(FPsave), FPalign, 0, 0);
533 up->fpstate = FPactive;
537 * Before restoring the state, check for any pending
538 * exceptions, there's no way to restore the state without
539 * generating an unmasked exception.
540 * More attention should probably be paid here to the
541 * exception masks and error summary.
543 status = up->fpsave->fsw;
544 control = up->fpsave->fcw;
545 if((status & ~control) & 0x07F){
546 mathnote(status, up->fpsave->fpuip);
549 fprestore(up->fpsave);
550 up->fpstate = FPactive;
553 panic("math emu pid %ld %s pc 0x%lux",
554 up->pid, up->text, ureg->pc);
560 * math coprocessor segment overrun
563 mathover(Ureg*, void*)
565 pexit("math overrun", 0);
571 trapenable(VectorCERR, matherror, 0, "matherror");
572 if(m->cpuidfamily == 3)
573 intrenable(IrqIRQ13, matherror, 0, BUSUNKNOWN, "matherror");
574 trapenable(VectorCNA, mathemu, 0, "mathemu");
575 trapenable(VectorCSO, mathover, 0, "mathover");
576 trapenable(VectorSIMD, simderror, 0, "simderror");
580 * set up floating point for a new process
589 p->pcycles = -p->kentry;
591 memset(p->gdt, 0, sizeof(p->gdt));
595 memset(p->dr, 0, sizeof(p->dr));
603 p->kentry = up->kentry;
604 p->pcycles = -p->kentry;
606 /* inherit user descriptors */
607 memmove(p->gdt, up->gdt, sizeof(p->gdt));
609 /* copy local descriptor table */
610 if(up->ldt != nil && up->nldt > 0){
611 p->ldt = smalloc(sizeof(Segdesc) * up->nldt);
612 memmove(p->ldt, up->ldt, sizeof(Segdesc) * up->nldt);
616 /* save floating point state */
618 switch(up->fpstate & ~FPillegal){
621 up->fpstate = FPinactive;
623 while(p->fpsave == nil)
624 p->fpsave = mallocalign(sizeof(FPsave), FPalign, 0, 0);
625 memmove(p->fpsave, up->fpsave, sizeof(FPsave));
626 p->fpstate = FPinactive;
629 /* clear debug registers */
630 memset(p->dr, 0, sizeof(p->dr));
656 * Save the mach dependent part of the process state.
663 /* we could just always putdr7(0) but accessing DR7 might be slow in a VM */
673 if(p->fpstate == FPactive){
674 if(p->state == Moribund)
678 * Fpsave() stores without handling pending
679 * unmasked exeptions. Postnote() can't be called
680 * here as sleep() already has up->rlock, so
681 * the handling of pending exceptions is delayed
682 * until the process runs again and generates an
683 * emulation fault to activate the FPU.
687 p->fpstate = FPinactive;
691 * While this processor is in the scheduler, the process could run
692 * on another processor and exit, returning the page tables to
693 * the free list where they could be reallocated and overwritten.
694 * When this processor eventually has to get an entry from the
695 * trashed page tables it will crash.
697 * If there's only one processor, this can't happen.
698 * You might think it would be a win not to do this in that case,
699 * especially on VMware, but it turns out not to matter.
701 mmuflushtlb(PADDR(m->pdb));
705 reboot(void *entry, void *code, ulong size)
707 void (*f)(ulong, ulong, ulong);
714 * the boot processor is cpu0. execute this function on it
715 * so that the new kernel has the same cpu0. this only matters
716 * because the hardware has a notion of which processor was the
717 * boot processor and we look at it at start up.
719 if (m->machno != 0) {
727 /* turn off buffered serial console */
730 /* shutdown devices */
735 * Modify the machine page table to directly map the low 4MB of memory
736 * This allows the reboot code to turn off the page mapping
739 pdb[PDX(0)] = pdb[PDX(KZERO)];
740 mmuflushtlb(PADDR(pdb));
742 /* setup reboot trampoline function */
743 f = (void*)REBOOTADDR;
744 memmove(f, rebootcode, sizeof(rebootcode));
746 /* off we go - never to return */
748 (*f)((ulong)entry & ~0xF0000000UL, PADDR(code), size);