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)
348 reboot(void *entry, void *code, ulong size)
350 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
395 * There are opportunities to be lazier about saving and
396 * restoring the state and allocating the storage needed.
398 extern void _clts(void);
399 extern void _fldcw(u16int);
400 extern void _fnclex(void);
401 extern void _fninit(void);
402 extern void _fxrstor(Fxsave*);
403 extern void _fxsave(Fxsave*);
404 extern void _fwait(void);
405 extern void _ldmxcsr(u32int);
406 extern void _stts(void);
409 * not used, AMD64 mandated SSE
416 fpx87restore(FPsave*)
421 fpssesave(FPsave *fps)
423 Fxsave *fx = (Fxsave*)ROUND(((uintptr)fps), FPalign);
427 if(fx != (Fxsave*)fps)
428 memmove((Fxsave*)fps, fx, sizeof(Fxsave));
431 fpsserestore(FPsave *fps)
433 Fxsave *fx = (Fxsave*)ROUND(((uintptr)fps), FPalign);
435 if(fx != (Fxsave*)fps)
436 memmove(fx, (Fxsave*)fps, sizeof(Fxsave));
441 static char* mathmsg[] =
443 nil, /* handled below */
444 "denormalized operand",
452 mathnote(ulong status, uintptr pc)
454 char *msg, note[ERRMAX];
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=%#p status=0x%lux",
479 postnote(up, 1, note, NDebug);
483 * math coprocessor error
486 matherror(Ureg*, void*)
489 * Save FPU state to check out the error.
492 up->fpstate = FPinactive;
493 mathnote(up->fpsave.fsw, up->fpsave.rip);
500 simderror(Ureg *ureg, void*)
503 up->fpstate = FPinactive;
504 mathnote(up->fpsave.mxcsr & 0x3f, ureg->pc);
511 * A process tries to use the FPU for the
512 * first time and generates a 'device not available'
514 * Turn the FPU on and initialise it for use.
515 * Set the precision and mask the exceptions
516 * we don't care about from the generic Mach value.
526 * math coprocessor emulation fault
529 mathemu(Ureg *ureg, void*)
531 ulong status, control;
533 if(up->fpstate & FPillegal){
534 /* someone did floating point in a note handler */
535 postnote(up, 1, "sys: floating point in note handler", NDebug);
541 up->fpstate = FPactive;
545 * Before restoring the state, check for any pending
546 * exceptions, there's no way to restore the state without
547 * generating an unmasked exception.
548 * More attention should probably be paid here to the
549 * exception masks and error summary.
551 status = up->fpsave.fsw;
552 control = up->fpsave.fcw;
553 if((status & ~control) & 0x07F){
554 mathnote(status, up->fpsave.rip);
557 fprestore(&up->fpsave);
558 up->fpstate = FPactive;
561 panic("math emu pid %ld %s pc %#p",
562 up->pid, up->text, ureg->pc);
568 * math coprocessor segment overrun
571 mathover(Ureg*, void*)
573 pexit("math overrun", 0);
579 trapenable(VectorCERR, matherror, 0, "matherror");
580 if(X86FAMILY(m->cpuidax) == 3)
581 intrenable(IrqIRQ13, matherror, 0, BUSUNKNOWN, "matherror");
582 trapenable(VectorCNA, mathemu, 0, "mathemu");
583 trapenable(VectorCSO, mathover, 0, "mathover");
584 trapenable(VectorSIMD, simderror, 0, "simderror");
593 p->pcycles = -p->kentry;
601 p->kentry = up->kentry;
602 p->pcycles = -p->kentry;
604 /* save floating point state */
606 switch(up->fpstate & ~FPillegal){
609 up->fpstate = FPinactive;
611 p->fpsave = up->fpsave;
612 p->fpstate = FPinactive;
653 if(p->fpstate == FPactive){
654 if(p->state == Moribund){
661 * Fpsave() stores without handling pending
662 * unmasked exeptions. Postnote() can't be called
663 * here as sleep() already has up->rlock, so
664 * the handling of pending exceptions is delayed
665 * until the process runs again and generates an
666 * emulation fault to activate the FPU.
670 p->fpstate = FPinactive;
674 * While this processor is in the scheduler, the process could run
675 * on another processor and exit, returning the page tables to
676 * the free list where they could be reallocated and overwritten.
677 * When this processor eventually has to get an entry from the
678 * trashed page tables it will crash.
680 * If there's only one processor, this can't happen.
681 * You might think it would be a win not to do this in that case,
682 * especially on VMware, but it turns out not to matter.