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 */
72 MACHP(0) = (Mach*)CPU0MACH;
73 m->pdb = (ulong*)CPU0PDB;
74 m->gdt = (Segdesc*)CPU0GDT;
92 memset(m, 0, sizeof(Mach));
99 * For polled uart output at boot, need
100 * a default delay constant. 100000 should
101 * be enough for a while. Cpuidentify will
102 * calculate the real value later.
104 m->loopconst = 100000;
110 char buf[2*KNAMELEN], **sp;
117 * These are o.k. because rootinit is null.
118 * Then early kproc's will have a root and dot.
120 up->slash = namec("#/", Atodir, 0, 0);
121 pathclose(up->slash->path);
122 up->slash->path = newpath("/");
123 up->dot = cclone(up->slash);
128 snprint(buf, sizeof(buf), "%s %s", arch->id, conffile);
129 ksetenv("terminal", buf, 0);
130 ksetenv("cputype", "386", 0);
132 ksetenv("service", "cpu", 0);
134 ksetenv("service", "terminal", 0);
138 kproc("alarm", alarmkproc, 0);
140 sp = (char**)(USTKTOP - sizeof(Tos) - 8 - sizeof(sp[0])*4);
142 strcpy(sp[1] = (char*)&sp[4], "boot");
157 p->egrp = smalloc(sizeof(Egrp));
159 p->fgrp = dupfgrp(nil);
164 kstrdup(&p->text, "*init*");
165 kstrdup(&p->user, eve);
172 * N.B. make sure there's enough space for syscall to check
174 * 4 bytes for gotolabel's return PC
176 p->sched.pc = (ulong)init0;
177 p->sched.sp = (ulong)p->kstack+KSTACK-(sizeof(Sargs)+BY2WD);
182 * N.B. cannot call newpage() with clear=1, because pc kmap
183 * requires up != nil. use tmpmap instead.
185 s = newseg(SG_STACK, USTKTOP-USTKSIZE, USTKSIZE/BY2PG);
187 pg = newpage(0, 0, USTKTOP-BY2PG);
196 s = newseg(SG_TEXT, UTZERO, 1);
199 pg = newpage(0, 0, UTZERO);
204 memmove(v, initcode, sizeof initcode);
217 if(p = getconf("service")){
218 if(strcmp(p, "cpu") == 0)
220 else if(strcmp(p,"terminal") == 0)
224 if(p = getconf("*kernelpercent"))
225 userpcnt = 100 - strtol(p, 0, 0);
230 for(i=0; i<nelem(conf.mem); i++)
231 conf.npage += conf.mem[i].npage;
233 conf.nproc = 100 + ((conf.npage*BY2PG)/MB)*5;
236 if(conf.nproc > 2000)
239 conf.nswap = conf.nproc*80;
245 kpages = conf.npage - (conf.npage*userpcnt)/100;
246 conf.nimage = conf.nproc;
249 * Hack for the big boys. Only good while physmem < 4GB.
250 * Give the kernel fixed max + enough to allocate the
252 * This is an overestimate as conf.upages < conf.npages.
253 * The patch of nimage is a band-aid, scanning the whole
254 * page list in imagereclaim just takes too long.
256 if(getconf("*imagemaxmb") == 0)
257 if(kpages > (64*MB + conf.npage*sizeof(Page))/BY2PG){
258 kpages = (64*MB + conf.npage*sizeof(Page))/BY2PG;
259 kpages += (conf.nproc*KSTACK)/BY2PG;
263 if(conf.npage*BY2PG < 16*MB)
268 kpages = conf.npage - (conf.npage*userpcnt)/100;
271 * Make sure terminals with low memory get at least
272 * 4MB on the first Image chunk allocation.
274 if(conf.npage*BY2PG < 16*MB)
275 imagmem->minarena = 4*MB;
279 * can't go past the end of virtual memory
280 * (ulong)-KZERO is 2^32 - KZERO
282 if(kpages > ((ulong)-KZERO)/BY2PG)
283 kpages = ((ulong)-KZERO)/BY2PG;
285 conf.upages = conf.npage - kpages;
286 conf.ialloc = (kpages/2)*BY2PG;
289 * Guess how much is taken by the large permanent
290 * datastructures. Mntcache and Mntrpc are not accounted for.
293 kpages -= conf.upages*sizeof(Page)
294 + conf.nproc*sizeof(Proc)
295 + conf.nimage*sizeof(Image)
297 + conf.nswppo*sizeof(Page*);
298 mainmem->maxsize = kpages;
301 * the dynamic allocation will balance the load properly,
302 * hopefully. be careful with 32-bit overflow.
304 imagmem->maxsize = kpages - (kpages/10);
305 if(p = getconf("*imagemaxmb")){
306 imagmem->maxsize = strtol(p, nil, 0)*MB;
307 if(imagmem->maxsize > mainmem->maxsize)
308 imagmem->maxsize = mainmem->maxsize;
313 * we keep FPsave structure in SSE format emulating FXSAVE / FXRSTOR
314 * instructions for legacy x87 fpu.
317 fpx87save(FPsave *fps)
324 * convert x87 tag word to fxsave tag byte:
325 * 00, 01, 10 -> 1, 11 -> 0
328 tag = (tag | (tag >> 1)) & 0x5555;
329 tag = (tag | (tag >> 1)) & 0x3333;
330 tag = (tag | (tag >> 2)) & 0x0F0F;
331 tag = (tag | (tag >> 4)) & 0x00FF;
333 /* NOP fps->fcw = fps->control; */
334 fps->fsw = fps->status;
336 fps->fop = fps->opcode;
337 fps->fpuip = fps->pc;
338 fps->cs = fps->selector;
339 fps->fpudp = fps->operand;
340 fps->ds = fps->oselector;
343 *((ushort*)(d+8)) = *((ushort*)(s+8)), \
344 *((ulong*)(d+4)) = *((ulong*)(s+4)), \
345 *((ulong*)(d)) = *((ulong*)(s))
347 MOVA(fps->xregs+0x70, fps->regs+70);
348 MOVA(fps->xregs+0x60, fps->regs+60);
349 MOVA(fps->xregs+0x50, fps->regs+50);
350 MOVA(fps->xregs+0x40, fps->regs+40);
351 MOVA(fps->xregs+0x30, fps->regs+30);
352 MOVA(fps->xregs+0x20, fps->regs+20);
353 MOVA(fps->xregs+0x10, fps->regs+10);
354 MOVA(fps->xregs+0x00, fps->regs+00);
359 *((ulong*)(d)) = 0, \
360 *((ushort*)(d+4)) = 0
362 CLR6(fps->xregs+0x70+10);
363 CLR6(fps->xregs+0x60+10);
364 CLR6(fps->xregs+0x50+10);
365 CLR6(fps->xregs+0x40+10);
366 CLR6(fps->xregs+0x30+10);
367 CLR6(fps->xregs+0x20+10);
368 CLR6(fps->xregs+0x10+10);
369 CLR6(fps->xregs+0x00+10);
373 fps->rsrvd1 = fps->rsrvd2 = fps->mxcsr = fps->mxcsr_mask = 0;
377 fpx87restore(FPsave *fps)
379 ushort msk, tos, tag, *reg;
381 /* convert fxsave tag byte to x87 tag word */
383 tos = 7 - ((fps->fsw >> 11) & 7);
384 for(msk = 0x80; msk != 0; tos--, msk >>= 1){
386 if((fps->ftw & msk) != 0){
387 reg = (ushort*)&fps->xregs[(tos & 7) << 4];
388 switch(reg[4] & 0x7fff){
390 if((reg[0] | reg[1] | reg[2] | reg[3]) == 0){
391 tag |= 1; /* 01 zero */
396 tag |= 2; /* 10 special */
399 if((reg[3] & 0x8000) == 0)
400 break; /* 00 valid */
401 tag |= 2; /* 10 special */
405 tag |= 3; /* 11 empty */
410 *((ulong*)(d)) = *((ulong*)(s)), \
411 *((ulong*)(d+4)) = *((ulong*)(s+4)), \
412 *((ushort*)(d+8)) = *((ushort*)(s+8))
414 MOVA(fps->regs+00, fps->xregs+0x00);
415 MOVA(fps->regs+10, fps->xregs+0x10);
416 MOVA(fps->regs+20, fps->xregs+0x20);
417 MOVA(fps->regs+30, fps->xregs+0x30);
418 MOVA(fps->regs+40, fps->xregs+0x40);
419 MOVA(fps->regs+50, fps->xregs+0x50);
420 MOVA(fps->regs+60, fps->xregs+0x60);
421 MOVA(fps->regs+70, fps->xregs+0x70);
425 fps->oselector = fps->ds;
426 fps->operand = fps->fpudp;
427 fps->opcode = fps->fop & 0x7ff;
428 fps->selector = fps->cs;
429 fps->pc = fps->fpuip;
431 fps->status = fps->fsw;
432 /* NOP fps->control = fps->fcw; */
434 fps->r1 = fps->r2 = fps->r3 = fps->r4 = 0;
439 static char* mathmsg[] =
441 nil, /* handled below */
442 "denormalized operand",
450 mathnote(ulong status, ulong pc)
452 char *msg, note[ERRMAX];
456 * Some attention should probably be paid here to the
457 * exception masks and error summary.
459 msg = "unknown exception";
460 for(i = 1; i <= 5; i++){
461 if(!((1<<i) & status))
469 msg = "stack overflow";
471 msg = "stack underflow";
473 msg = "invalid operation";
475 snprint(note, sizeof note, "sys: fp: %s fppc=0x%lux status=0x%lux",
477 postnote(up, 1, note, NDebug);
481 * math coprocessor error
484 matherror(Ureg*, void*)
487 * a write cycle to port 0xF0 clears the interrupt latch attached
488 * to the error# line from the 387
490 if(!(m->cpuiddx & Fpuonchip))
494 * get floating point state to check out error
497 up->fpstate = FPinactive;
498 mathnote(up->fpsave->fsw, up->fpsave->fpuip);
505 simderror(Ureg *ureg, void*)
508 up->fpstate = FPinactive;
509 mathnote(up->fpsave->mxcsr & 0x3f, ureg->pc);
513 * math coprocessor emulation fault
516 mathemu(Ureg *ureg, void*)
518 ulong status, control;
520 if(up->fpstate & FPillegal){
521 /* someone did floating point in a note handler */
522 postnote(up, 1, "sys: floating point in note handler", NDebug);
528 if(fpsave == fpssesave)
529 ldmxcsr(0x1f80); /* no simd exceptions on 386 */
530 while(up->fpsave == nil)
531 up->fpsave = mallocalign(sizeof(FPsave), FPalign, 0, 0);
532 up->fpstate = FPactive;
536 * Before restoring the state, check for any pending
537 * exceptions, there's no way to restore the state without
538 * generating an unmasked exception.
539 * More attention should probably be paid here to the
540 * exception masks and error summary.
542 status = up->fpsave->fsw;
543 control = up->fpsave->fcw;
544 if((status & ~control) & 0x07F){
545 mathnote(status, up->fpsave->fpuip);
548 fprestore(up->fpsave);
549 up->fpstate = FPactive;
552 panic("math emu pid %ld %s pc 0x%lux",
553 up->pid, up->text, ureg->pc);
559 * math coprocessor segment overrun
562 mathover(Ureg*, void*)
564 pexit("math overrun", 0);
570 trapenable(VectorCERR, matherror, 0, "matherror");
571 if(X86FAMILY(m->cpuidax) == 3)
572 intrenable(IrqIRQ13, matherror, 0, BUSUNKNOWN, "matherror");
573 trapenable(VectorCNA, mathemu, 0, "mathemu");
574 trapenable(VectorCSO, mathover, 0, "mathover");
575 trapenable(VectorSIMD, simderror, 0, "simderror");
579 * set up floating point for a new process
588 p->pcycles = -p->kentry;
590 memset(p->gdt, 0, sizeof(p->gdt));
594 memset(p->dr, 0, sizeof(p->dr));
602 p->kentry = up->kentry;
603 p->pcycles = -p->kentry;
605 /* inherit user descriptors */
606 memmove(p->gdt, up->gdt, sizeof(p->gdt));
608 /* copy local descriptor table */
609 if(up->ldt != nil && up->nldt > 0){
610 p->ldt = smalloc(sizeof(Segdesc) * up->nldt);
611 memmove(p->ldt, up->ldt, sizeof(Segdesc) * up->nldt);
615 /* save floating point state */
617 switch(up->fpstate & ~FPillegal){
620 up->fpstate = FPinactive;
622 while(p->fpsave == nil)
623 p->fpsave = mallocalign(sizeof(FPsave), FPalign, 0, 0);
624 memmove(p->fpsave, up->fpsave, sizeof(FPsave));
625 p->fpstate = FPinactive;
628 /* clear debug registers */
629 memset(p->dr, 0, sizeof(p->dr));
655 * Save the mach dependent part of the process state.
662 /* we could just always putdr7(0) but accessing DR7 might be slow in a VM */
672 if(p->fpstate == FPactive){
673 if(p->state == Moribund)
677 * Fpsave() stores without handling pending
678 * unmasked exeptions. Postnote() can't be called
679 * here as sleep() already has up->rlock, so
680 * the handling of pending exceptions is delayed
681 * until the process runs again and generates an
682 * emulation fault to activate the FPU.
686 p->fpstate = FPinactive;
690 * While this processor is in the scheduler, the process could run
691 * on another processor and exit, returning the page tables to
692 * the free list where they could be reallocated and overwritten.
693 * When this processor eventually has to get an entry from the
694 * trashed page tables it will crash.
696 * If there's only one processor, this can't happen.
697 * You might think it would be a win not to do this in that case,
698 * especially on VMware, but it turns out not to matter.
700 mmuflushtlb(PADDR(m->pdb));
704 reboot(void *entry, void *code, ulong size)
706 void (*f)(ulong, ulong, ulong);
713 * the boot processor is cpu0. execute this function on it
714 * so that the new kernel has the same cpu0. this only matters
715 * because the hardware has a notion of which processor was the
716 * boot processor and we look at it at start up.
718 if (m->machno != 0) {
726 /* turn off buffered serial console */
729 /* shutdown devices */
734 * Modify the machine page table to directly map the low 4MB of memory
735 * This allows the reboot code to turn off the page mapping
738 pdb[PDX(0)] = pdb[PDX(KZERO)];
739 mmuflushtlb(PADDR(pdb));
741 /* setup reboot trampoline function */
742 f = (void*)REBOOTADDR;
743 memmove(f, rebootcode, sizeof(rebootcode));
745 /* off we go - never to return */
747 (*f)((ulong)entry & ~0xF0000000UL, PADDR(code), size);