2 #include "../port/lib.h"
17 extern void (*i8237alloc)(void);
18 extern void bootscreeninit(void);
27 if(p = getconf("service")){
28 if(strcmp(p, "cpu") == 0)
30 else if(strcmp(p,"terminal") == 0)
34 if(p = getconf("*kernelpercent"))
35 userpcnt = 100 - strtol(p, 0, 0);
40 for(i=0; i<nelem(conf.mem); i++)
41 conf.npage += conf.mem[i].npage;
43 conf.nproc = 100 + ((conf.npage*BY2PG)/MB)*5;
49 conf.nswap = conf.nproc*80;
55 kpages = conf.npage - (conf.npage*userpcnt)/100;
56 conf.nimage = conf.nproc;
59 if(conf.npage*BY2PG < 16*MB)
64 kpages = conf.npage - (conf.npage*userpcnt)/100;
67 * Make sure terminals with low memory get at least
68 * 4MB on the first Image chunk allocation.
70 if(conf.npage*BY2PG < 16*MB)
71 imagmem->minarena = 4*MB;
75 * can't go past the end of virtual memory.
77 if(kpages > ((uintptr)-KZERO)/BY2PG)
78 kpages = ((uintptr)-KZERO)/BY2PG;
80 conf.upages = conf.npage - kpages;
81 conf.ialloc = (kpages/2)*BY2PG;
84 * Guess how much is taken by the large permanent
85 * datastructures. Mntcache and Mntrpc are not accounted for.
88 kpages -= conf.nproc*sizeof(Proc)
89 + conf.nimage*sizeof(Image)
91 + conf.nswppo*sizeof(Page*);
92 mainmem->maxsize = kpages;
95 * the dynamic allocation will balance the load properly,
96 * hopefully. be careful with 32-bit overflow.
98 imagmem->maxsize = kpages - (kpages/10);
99 if(p = getconf("*imagemaxmb")){
100 imagmem->maxsize = strtol(p, nil, 0)*MB;
101 if(imagmem->maxsize > mainmem->maxsize)
102 imagmem->maxsize = mainmem->maxsize;
107 * The palloc.pages array can be a large chunk out of the 2GB
108 * window above KZERO, so we allocate the array from
109 * upages and map in the VMAP window before pageinit()
115 uintptr va, base, top;
121 for(i=0; i<nelem(palloc.mem); i++){
125 size = (uvlong)np * BY2PG;
126 size += sizeof(Page) + BY2PG; /* round up */
127 size = (size / (sizeof(Page) + BY2PG)) * sizeof(Page);
128 size = ROUND(size, PGLSZ(1));
130 for(i=0; i<nelem(palloc.mem); i++){
132 base = ROUND(pm->base, PGLSZ(1));
133 top = pm->base + (uvlong)pm->npage * BY2PG;
134 if((base + size) <= VMAPSIZE && (vlong)(top - base) >= size){
136 pmap(m->pml4, base | PTEGLOBAL|PTEWRITE|PTEVALID, va, size);
137 palloc.pages = (Page*)va;
138 pm->base = base + size;
139 pm->npage = (top - pm->base)/BY2PG;
155 memset(m, 0, sizeof(Mach));
162 * For polled uart output at boot, need
163 * a default delay constant. 100000 should
164 * be enough for a while. Cpuidentify will
165 * calculate the real value later.
167 m->loopconst = 100000;
175 MACHP(0) = (Mach*)CPU0MACH;
178 m->pml4 = (u64int*)CPU0PML4;
179 m->gdt = (Segdesc*)CPU0GDT;
190 char buf[2*KNAMELEN], **sp;
197 * These are o.k. because rootinit is null.
198 * Then early kproc's will have a root and dot.
200 up->slash = namec("#/", Atodir, 0, 0);
201 pathclose(up->slash->path);
202 up->slash->path = newpath("/");
203 up->dot = cclone(up->slash);
208 snprint(buf, sizeof(buf), "%s %s", arch->id, conffile);
209 ksetenv("terminal", buf, 0);
210 ksetenv("cputype", "amd64", 0);
212 ksetenv("service", "cpu", 0);
214 ksetenv("service", "terminal", 0);
218 kproc("alarm", alarmkproc, 0);
220 sp = (char**)(USTKTOP - sizeof(Tos) - 8 - sizeof(sp[0])*4);
222 strcpy(sp[1] = (char*)&sp[4], "boot");
237 p->egrp = smalloc(sizeof(Egrp));
239 p->fgrp = dupfgrp(nil);
244 kstrdup(&p->text, "*init*");
245 kstrdup(&p->user, eve);
252 * N.B. make sure there's enough space for syscall to check
254 * 8 bytes for gotolabel's return PC
256 p->sched.pc = (uintptr)init0;
257 p->sched.sp = (uintptr)p->kstack+KSTACK-(sizeof(Sargs)+BY2WD);
259 /* temporarily set up for kmap() */
265 s = newseg(SG_STACK, USTKTOP-USTKSIZE, USTKSIZE/BY2PG);
267 pg = newpage(0, 0, USTKTOP-BY2PG);
276 s = newseg(SG_TEXT, UTZERO, 1);
279 pg = newpage(0, 0, UTZERO);
284 memmove(v, initcode, sizeof initcode);
312 if(i8237alloc != nil)
322 if(arch->clockenable)
347 reboot(void *entry, void *code, ulong size)
349 void (*f)(uintptr, uintptr, ulong);
355 * the boot processor is cpu0. execute this function on it
356 * so that the new kernel has the same cpu0. this only matters
357 * because the hardware has a notion of which processor was the
358 * boot processor and we look at it at start up.
360 if (m->machno != 0) {
368 /* turn off buffered serial console */
371 /* shutdown devices */
376 * This allows the reboot code to turn off the page mapping
378 *mmuwalk(m->pml4, 0, 3, 0) = *mmuwalk(m->pml4, KZERO, 3, 0);
379 *mmuwalk(m->pml4, 0, 2, 0) = *mmuwalk(m->pml4, KZERO, 2, 0);
382 /* setup reboot trampoline function */
383 f = (void*)REBOOTADDR;
384 memmove(f, rebootcode, sizeof(rebootcode));
386 /* off we go - never to return */
388 (*f)((uintptr)entry & ~0xF0000000UL, (uintptr)PADDR(code), size);
392 * SIMD Floating Point.
393 * Assembler support to get at the individual instructions
396 extern void _clts(void);
397 extern void _fldcw(u16int);
398 extern void _fnclex(void);
399 extern void _fninit(void);
400 extern void _fxrstor(void*);
401 extern void _fxsave(void*);
402 extern void _fwait(void);
403 extern void _ldmxcsr(u32int);
404 extern void _stts(void);
407 * not used, AMD64 mandated SSE
414 fpx87restore(FPsave*)
425 fpsserestore(FPsave *s)
431 static char* mathmsg[] =
433 nil, /* handled below */
434 "denormalized operand",
442 mathnote(ulong status, uintptr pc)
444 char *msg, note[ERRMAX];
448 * Some attention should probably be paid here to the
449 * exception masks and error summary.
451 msg = "unknown exception";
452 for(i = 1; i <= 5; i++){
453 if(!((1<<i) & status))
461 msg = "stack overflow";
463 msg = "stack underflow";
465 msg = "invalid operation";
467 snprint(note, sizeof note, "sys: fp: %s fppc=%#p status=0x%lux",
469 postnote(up, 1, note, NDebug);
473 * math coprocessor error
476 matherror(Ureg *, void*)
479 * Save FPU state to check out the error.
482 up->fpstate = FPinactive | (up->fpstate & (FPnouser|FPkernel|FPindexm));
483 mathnote(up->fpsave->fsw, up->fpsave->rip);
490 simderror(Ureg *ureg, void*)
493 up->fpstate = FPinactive | (up->fpstate & (FPnouser|FPkernel|FPindexm));
494 mathnote(up->fpsave->mxcsr & 0x3f, ureg->pc);
501 * A process tries to use the FPU for the
502 * first time and generates a 'device not available'
504 * Turn the FPU on and initialise it for use.
505 * Set the precision and mask the exceptions
506 * we don't care about from the generic Mach value.
516 * math coprocessor emulation fault
519 mathemu(Ureg *ureg, void*)
521 ulong status, control;
524 if(up->fpstate & FPillegal){
525 /* someone did floating point in a note handler */
526 postnote(up, 1, "sys: floating point in note handler", NDebug);
529 switch(up->fpstate & ~(FPnouser|FPkernel|FPindexm)){
530 case FPactive | FPpush:
533 case FPinactive | FPpush:
534 up->fpstate += FPindex1;
535 case FPinit | FPpush:
538 index = up->fpstate >> FPindexs;
539 if(index < 0 || index > FPindexm)
540 panic("fpslot index overflow: %d", index);
543 panic("fpslot index %d != 0 for user", index);
546 up->fpstate |= FPnouser;
547 up->fpstate |= FPkernel;
549 while(up->fpslot[index] == nil)
550 up->fpslot[index] = mallocalign(sizeof(FPsave), FPalign, 0, 0);
551 up->fpsave = up->fpslot[index];
552 up->fpstate = FPactive | (up->fpstate & (FPnouser|FPkernel|FPindexm));
556 * Before restoring the state, check for any pending
557 * exceptions, there's no way to restore the state without
558 * generating an unmasked exception.
559 * More attention should probably be paid here to the
560 * exception masks and error summary.
562 status = up->fpsave->fsw;
563 control = up->fpsave->fcw;
564 if((status & ~control) & 0x07F){
565 mathnote(status, up->fpsave->rip);
568 fprestore(up->fpsave);
569 up->fpstate = FPactive | (up->fpstate & (FPnouser|FPkernel|FPindexm));
572 panic("math emu pid %ld %s pc %#p",
573 up->pid, up->text, ureg->pc);
579 * math coprocessor segment overrun
582 mathover(Ureg*, void*)
584 pexit("math overrun", 0);
590 trapenable(VectorCERR, matherror, 0, "matherror");
591 if(X86FAMILY(m->cpuidax) == 3)
592 intrenable(IrqIRQ13, matherror, 0, BUSUNKNOWN, "matherror");
593 trapenable(VectorCNA, mathemu, 0, "mathemu");
594 trapenable(VectorCSO, mathover, 0, "mathover");
595 trapenable(VectorSIMD, simderror, 0, "simderror");
604 p->pcycles = -p->kentry;
612 p->kentry = up->kentry;
613 p->pcycles = -p->kentry;
615 /* save floating point state */
617 switch(up->fpstate & ~FPillegal){
618 case FPactive | FPpush:
622 up->fpstate = FPinactive | (up->fpstate & FPpush);
623 case FPactive | FPkernel:
624 case FPinactive | FPkernel:
625 case FPinactive | FPpush:
627 while(p->fpslot[0] == nil)
628 p->fpslot[0] = mallocalign(sizeof(FPsave), FPalign, 0, 0);
629 memmove(p->fpsave = p->fpslot[0], up->fpslot[0], sizeof(FPsave));
630 p->fpstate = FPinactive;
670 switch(p->fpstate & ~(FPnouser|FPkernel|FPindexm)){
671 case FPactive | FPpush:
674 if(p->state == Moribund){
680 * Fpsave() stores without handling pending
681 * unmasked exeptions. Postnote() can't be called
682 * here as sleep() already has up->rlock, so
683 * the handling of pending exceptions is delayed
684 * until the process runs again and generates an
685 * emulation fault to activate the FPU.
688 p->fpstate = FPinactive | (p->fpstate & (FPpush|FPnouser|FPkernel|FPindexm));
693 * While this processor is in the scheduler, the process could run
694 * on another processor and exit, returning the page tables to
695 * the free list where they could be reallocated and overwritten.
696 * When this processor eventually has to get an entry from the
697 * trashed page tables it will crash.
699 * If there's only one processor, this can't happen.
700 * You might think it would be a win not to do this in that case,
701 * especially on VMware, but it turns out not to matter.
707 * Fpusave and fpurestore lazily save and restore FPU state across
708 * system calls and the pagefault handler so that we can take
709 * advantage of SSE instructions such as AES-NI in the kernel.
714 int ostate = up->fpstate;
715 if((ostate & ~(FPnouser|FPkernel|FPindexm)) == FPactive)
717 up->fpstate = FPpush | (ostate & ~FPillegal);
721 fpurestore(int ostate)
723 int astate = up->fpstate;
724 if(astate == (FPpush | (ostate & ~FPillegal))){
725 if((ostate & ~(FPnouser|FPkernel|FPindexm)) == FPactive)
728 if(astate == FPinit) /* don't restore on procexec()/procsetup() */
730 if((astate & ~(FPnouser|FPkernel|FPindexm)) == FPactive)
732 up->fpsave = up->fpslot[ostate>>FPindexs];
733 ostate = FPinactive | (ostate & (FPillegal|FPpush|FPnouser|FPkernel|FPindexm));
735 up->fpstate = ostate;