]> git.lizzy.rs Git - plan9front.git/blobdiff - sys/src/9/pc/main.c
kernel: cleanup the software mouse cursor mess
[plan9front.git] / sys / src / 9 / pc / main.c
index 857c918cbb8080b150c7a6378e8fea5896820a9c..01fa783adec5bc366e3f814884ca66e049a5404e 100644 (file)
 #include       "u.h"
+#include       "tos.h"
 #include       "../port/lib.h"
 #include       "mem.h"
 #include       "dat.h"
 #include       "fns.h"
 #include       "io.h"
-#include       "tos.h"
 #include       "ureg.h"
-#include       "init.h"
 #include       "pool.h"
-#include       "reboot.h"
+#include       "rebootcode.i"
 
 Mach *m;
-
-/*
- * Where configuration info is left for the loaded programme.
- * This will turn into a structure as more is done by the boot loader
- * (e.g. why parse the .ini file twice?).
- * There are 3584 bytes available at CONFADDR.
- */
-#define BOOTLINE       ((char*)CONFADDR)
-#define BOOTLINELEN    64
-#define BOOTARGS       ((char*)(CONFADDR+BOOTLINELEN))
-#define        BOOTARGSLEN     (4096-0x200-BOOTLINELEN)
-#define        MAXCONF         64
-
 Conf conf;
-char *confname[MAXCONF];
-char *confval[MAXCONF];
-int nconf;
-char *sp;      /* user stack of init proc */
+
 int delaylink;
 int idle_spin;
 
-static void
-multibootargs(void)
-{
-       char *cp, *ep;
-       ulong *m, l;
-
-       extern ulong *multiboot;
-
-       if(multiboot == nil)
-               return;
-
-       /* command line */
-       if((multiboot[0] & (1<<2)) != 0)
-               strncpy(BOOTLINE, KADDR(multiboot[4]), BOOTLINELEN-1);
-
-       cp = BOOTARGS;
-       ep = cp + BOOTARGSLEN-1;
-
-       /* memory map */
-       if((multiboot[0] & (1<<6)) != 0 && (l = multiboot[11]) >= 24){
-               cp = seprint(cp, ep, "*e820=");
-               m = KADDR(multiboot[12]);
-               while(m[0] >= 20 && m[0] <= l-4){
-                       uvlong base, size;
-                       m++;
-                       base = ((uvlong)m[0] | (uvlong)m[1]<<32);
-                       size = ((uvlong)m[2] | (uvlong)m[3]<<32);
-                       cp = seprint(cp, ep, "%.1lux %.16llux %.16llux ",
-                               m[4] & 0xF, base, base+size);
-                       l -= m[-1]+4;
-                       m = (ulong*)((ulong)m + m[-1]);
-               }
-               cp[-1] = '\n';
-       }
-
-       /* plan9.ini passed as the first module */
-       if((multiboot[0] & (1<<3)) != 0 && multiboot[5] > 0){
-               m = KADDR(multiboot[6]);
-               l = m[1] - m[0];
-               m = KADDR(m[0]);
-               if(cp+l > ep)
-                       l = ep - cp;
-               memmove(cp, m, l);
-               cp += l;
-       }
-       *cp = 0;
-}
-
-static void
-options(void)
-{
-       long i, n;
-       char *cp, *line[MAXCONF], *p, *q;
-
-       multibootargs();
-
-       /*
-        *  parse configuration args from dos file plan9.ini
-        */
-       cp = BOOTARGS;  /* where b.com leaves its config */
-       cp[BOOTARGSLEN-1] = 0;
-
-       /*
-        * Strip out '\r', change '\t' -> ' '.
-        */
-       p = cp;
-       for(q = cp; *q; q++){
-               if(*q == '\r')
-                       continue;
-               if(*q == '\t')
-                       *q = ' ';
-               *p++ = *q;
-       }
-       *p = 0;
-
-       n = getfields(cp, line, MAXCONF, 1, "\n");
-       for(i = 0; i < n; i++){
-               if(*line[i] == '#')
-                       continue;
-               cp = strchr(line[i], '=');
-               if(cp == nil)
-                       continue;
-               *cp++ = '\0';
-               confname[nconf] = line[i];
-               confval[nconf] = cp;
-               nconf++;
-       }
-}
-
-extern void mmuinit0(void);
 extern void (*i8237alloc)(void);
 extern void bootscreeninit(void);
+extern void multibootdebug(void);
 
 void
 main(void)
 {
        mach0init();
-       options();
+       bootargsinit();
        ioinit();
        i8250console();
        quotefmtinstall();
        screeninit();
 
        print("\nPlan 9\n");
-
+       
        trapinit0();
-       mmuinit0();
-
-       kbdinit();
        i8253init();
        cpuidentify();
+       meminit0();
+       archinit();
        meminit();
+       ramdiskinit();
        confinit();
        xinit();
-       archinit();
        bootscreeninit();
        if(i8237alloc != nil)
                i8237alloc();
@@ -158,7 +51,6 @@ main(void)
                arch->intrinit();
        timersinit();
        mathinit();
-       kbdenable();
        if(arch->clockenable)
                arch->clockenable();
        procinit0();
@@ -168,12 +60,9 @@ main(void)
                pcimatch(0, 0, 0);
        }else
                links();
-       conf.monitor = 1;
        chandevreset();
        pageinit();
-       swapinit();
        userinit();
-       active.thunderbirdsarego = 1;
        schedinit();
 }
 
@@ -187,7 +76,7 @@ mach0init(void)
 
        machinit();
 
-       active.machs = 1;
+       active.machs[0] = 1;
        active.exiting = 0;
 }
 
@@ -219,21 +108,7 @@ machinit(void)
 void
 init0(void)
 {
-       int i;
-       char buf[2*KNAMELEN];
-
-       up->nerrlab = 0;
-
-       spllo();
-
-       /*
-        * These are o.k. because rootinit is null.
-        * Then early kproc's will have a root and dot.
-        */
-       up->slash = namec("#/", Atodir, 0, 0);
-       pathclose(up->slash->path);
-       up->slash->path = newpath("/");
-       up->dot = cclone(up->slash);
+       char buf[2*KNAMELEN], **sp;
 
        chandevinit();
 
@@ -245,155 +120,16 @@ init0(void)
                        ksetenv("service", "cpu", 0);
                else
                        ksetenv("service", "terminal", 0);
-               for(i = 0; i < nconf; i++){
-                       if(confname[i][0] != '*')
-                               ksetenv(confname[i], confval[i], 0);
-                       ksetenv(confname[i], confval[i], 1);
-               }
+               setconfenv();
                poperror();
        }
        kproc("alarm", alarmkproc, 0);
-       touser(sp);
-}
-
-void
-userinit(void)
-{
-       void *v;
-       Proc *p;
-       Segment *s;
-       Page *pg;
 
-       p = newproc();
-       p->pgrp = newpgrp();
-       p->egrp = smalloc(sizeof(Egrp));
-       p->egrp->ref = 1;
-       p->fgrp = dupfgrp(nil);
-       p->rgrp = newrgrp();
-       p->procmode = 0640;
-
-       kstrdup(&eve, "");
-       kstrdup(&p->text, "*init*");
-       kstrdup(&p->user, eve);
-
-       procsetup(p);
-
-       /*
-        * Kernel Stack
-        *
-        * N.B. make sure there's enough space for syscall to check
-        *      for valid args and 
-        *      4 bytes for gotolabel's return PC
-        */
-       p->sched.pc = (ulong)init0;
-       p->sched.sp = (ulong)p->kstack+KSTACK-(sizeof(Sargs)+BY2WD);
-
-       /*
-        * User Stack
-        *
-        * N.B. cannot call newpage() with clear=1, because pc kmap
-        * requires up != nil.  use tmpmap instead.
-        */
-       s = newseg(SG_STACK, USTKTOP-USTKSIZE, USTKSIZE/BY2PG);
-       p->seg[SSEG] = s;
-       pg = newpage(0, 0, USTKTOP-BY2PG);
-       v = tmpmap(pg);
-       memset(v, 0, BY2PG);
-       segpage(s, pg);
-       bootargs(v);
-       tmpunmap(v);
-
-       /*
-        * Text
-        */
-       s = newseg(SG_TEXT, UTZERO, 1);
-       s->flushme++;
-       p->seg[TSEG] = s;
-       pg = newpage(0, 0, UTZERO);
-       memset(pg->cachectl, PG_TXTFLUSH, sizeof(pg->cachectl));
-       segpage(s, pg);
-       v = tmpmap(pg);
-       memset(v, 0, BY2PG);
-       memmove(v, initcode, sizeof initcode);
-       tmpunmap(v);
-
-       ready(p);
-}
-
-void
-bootargs(void *base)
-{
-       char *argv[8];
-       int i, argc;
-
-#define UA(ka) ((char*)(ka) + ((uintptr)(USTKTOP - BY2PG) - (uintptr)base))
-       sp = (char*)base + BY2PG - sizeof(Tos);
-
-       /* push boot command line onto the stack */
-       sp -= BOOTLINELEN;
-       sp[BOOTLINELEN-1] = '\0';
-       memmove(sp, BOOTLINE, BOOTLINELEN-1);
-
-       /* parse boot command line */
-       argc = tokenize(sp, argv, nelem(argv));
-       if(argc < 1){
-               strcpy(sp, "boot");
-               argc = 0;
-               argv[argc++] = sp;
-       }
-
-       /* 4 byte word align stack */
-       sp = (char*)((uintptr)sp & ~3);
-
-       /* build argv on stack */
-       sp -= (argc+1)*BY2WD;
-       for(i=0; i<argc; i++)
-               ((char**)sp)[i] = UA(argv[i]);
-       ((char**)sp)[i] = nil;
-
-       sp = UA(sp);
-#undef UA
-       sp -= BY2WD;
-}
-
-char*
-getconf(char *name)
-{
-       int i;
-
-       for(i = 0; i < nconf; i++)
-               if(cistrcmp(confname[i], name) == 0)
-                       return confval[i];
-       return 0;
-}
-
-static void
-writeconf(void)
-{
-       char *p, *q;
-       int n;
-
-       p = getconfenv();
-
-       if(waserror()) {
-               free(p);
-               nexterror();
-       }
-
-       /* convert to name=value\n format */
-       for(q=p; *q; q++) {
-               q += strlen(q);
-               *q = '=';
-               q += strlen(q);
-               *q = '\n';
-       }
-       n = q - p + 1;
-       if(n >= BOOTARGSLEN)
-               error("kernel configuration too large");
-       memset(BOOTLINE, 0, BOOTLINELEN);
-       memmove(BOOTARGS, p, n);
-       poperror();
-       free(p);
+       sp = (char**)(USTKTOP - sizeof(Tos) - 8 - sizeof(sp[0])*4);
+       sp[3] = sp[2] = nil;
+       strcpy(sp[1] = (char*)&sp[4], "boot");
+       sp[0] = nil;
+       touser(sp);
 }
 
 void
@@ -432,6 +168,7 @@ confinit(void)
                if(userpcnt < 10)
                        userpcnt = 70;
                kpages = conf.npage - (conf.npage*userpcnt)/100;
+               conf.nimage = conf.nproc;
 
                /*
                 * Hack for the big boys. Only good while physmem < 4GB.
@@ -444,7 +181,6 @@ confinit(void)
                if(getconf("*imagemaxmb") == 0)
                if(kpages > (64*MB + conf.npage*sizeof(Page))/BY2PG){
                        kpages = (64*MB + conf.npage*sizeof(Page))/BY2PG;
-                       conf.nimage = 2000;
                        kpages += (conf.nproc*KSTACK)/BY2PG;
                }
        } else {
@@ -499,12 +235,8 @@ confinit(void)
 }
 
 /*
- * we keep FPsave structure in sse format emulating FXSAVE / FXRSTOR
+ * we keep FPsave structure in SSE format emulating FXSAVE / FXRSTOR
  * instructions for legacy x87 fpu.
- *
- * Note that fpx87restore() and fpxsserestore() do modify the FPsave
- * data structure for conversion / realignment shuffeling. this means
- * that p->fpsave is only valid when p->fpstate == FPinactive.
  */
 void
 fpx87save(FPsave *fps)
@@ -629,32 +361,6 @@ fpx87restore(FPsave *fps)
        fpx87restore0(fps);
 }
 
-/*
- * sse fp save and restore buffers have to be 16-byte (FPalign) aligned,
- * so we shuffle the data up and down as needed or make copies.
- */
-void
-fpssesave(FPsave *fps)
-{
-       FPsave *afps;
-
-       afps = (FPsave *)ROUND(((uintptr)fps), FPalign);
-       fpssesave0(afps);
-       if(fps != afps)  /* not aligned? shuffle down from aligned buffer */
-               memmove(fps, afps, sizeof(FPssestate) - FPalign);
-}
-
-void
-fpsserestore(FPsave *fps)
-{
-       FPsave *afps;
-
-       afps = (FPsave *)ROUND(((uintptr)fps), FPalign);
-       if(fps != afps)  /* shuffle up to make aligned */
-               memmove(afps, fps, sizeof(FPssestate) - FPalign);
-       fpsserestore0(afps);
-}
-
 static char* mathmsg[] =
 {
        nil,    /* handled below */
@@ -712,8 +418,9 @@ matherror(Ureg*, void*)
        /*
         *  get floating point state to check out error
         */
-       fpenv(&up->fpsave);
-       mathnote(up->fpsave.status, up->fpsave.pc);
+       fpsave(up->fpsave);
+       up->fpstate = FPinactive;
+       mathnote(up->fpsave->fsw, up->fpsave->fpuip);
 }
 
 /*
@@ -722,9 +429,9 @@ matherror(Ureg*, void*)
 static void
 simderror(Ureg *ureg, void*)
 {
-       fpsave(&up->fpsave);
+       fpsave(up->fpsave);
        up->fpstate = FPinactive;
-       mathnote(up->fpsave.mxcsr & 0x3f, ureg->pc);
+       mathnote(up->fpsave->mxcsr & 0x3f, ureg->pc);
 }
 
 /*
@@ -744,7 +451,9 @@ mathemu(Ureg *ureg, void*)
        case FPinit:
                fpinit();
                if(fpsave == fpssesave)
-                       ldmxcsr(0);     /* no simd exceptions on 386 */
+                       ldmxcsr(0x1f80);        /* no simd exceptions on 386 */
+               while(up->fpsave == nil)
+                       up->fpsave = mallocalign(sizeof(FPsave), FPalign, 0, 0);
                up->fpstate = FPactive;
                break;
        case FPinactive:
@@ -755,13 +464,13 @@ mathemu(Ureg *ureg, void*)
                 * More attention should probably be paid here to the
                 * exception masks and error summary.
                 */
-               status = up->fpsave.fsw;
-               control = up->fpsave.fcw;
+               status = up->fpsave->fsw;
+               control = up->fpsave->fcw;
                if((status & ~control) & 0x07F){
-                       mathnote(status, up->fpsave.fpuip);
+                       mathnote(status, up->fpsave->fpuip);
                        break;
                }
-               fprestore(&up->fpsave);
+               fprestore(up->fpsave);
                up->fpstate = FPactive;
                break;
        case FPactive:
@@ -784,7 +493,7 @@ void
 mathinit(void)
 {
        trapenable(VectorCERR, matherror, 0, "matherror");
-       if(X86FAMILY(m->cpuidax) == 3)
+       if(m->cpuidfamily == 3)
                intrenable(IrqIRQ13, matherror, 0, BUSUNKNOWN, "matherror");
        trapenable(VectorCNA, mathemu, 0, "mathemu");
        trapenable(VectorCSO, mathover, 0, "mathover");
@@ -800,12 +509,18 @@ procsetup(Proc *p)
        p->fpstate = FPinit;
        fpoff();
 
-       cycles(&p->kentry);
-       p->pcycles = -p->kentry;
-
        memset(p->gdt, 0, sizeof(p->gdt));
-       p->ldt = nil;
        p->nldt = 0;
+       
+       /* clear debug registers */
+       memset(p->dr, 0, sizeof(p->dr));
+       if(m->dr7 != 0){
+               m->dr7 = 0;
+               putdr7(0);
+       }
+
+       cycles(&p->kentry);
+       p->pcycles = -p->kentry;
 }
 
 void
@@ -830,10 +545,12 @@ procfork(Proc *p)
        s = splhi();
        switch(up->fpstate & ~FPillegal){
        case FPactive:
-               fpsave(&up->fpsave);
+               fpsave(up->fpsave);
                up->fpstate = FPinactive;
        case FPinactive:
-               p->fpsave = up->fpsave;
+               while(p->fpsave == nil)
+                       p->fpsave = mallocalign(sizeof(FPsave), FPalign, 0, 0);
+               memmove(p->fpsave, up->fpsave, sizeof(FPsave));
                p->fpstate = FPinactive;
        }
        splx(s);
@@ -843,6 +560,14 @@ void
 procrestore(Proc *p)
 {
        uvlong t;
+       
+       if(p->dr[7] != 0){
+               m->dr7 = p->dr[7];
+               putdr(p->dr);
+       }
+       
+       if(p->vmx != nil)
+               vmxprocrestore(p);
 
        if(p->kp)
                return;
@@ -859,11 +584,19 @@ void
 procsave(Proc *p)
 {
        uvlong t;
-
+       
        cycles(&t);
        p->kentry -= t;
        p->pcycles += t;
 
+       /* we could just always putdr7(0) but accessing DR7 might be slow in a VM */
+       if(m->dr7 != 0){
+               m->dr7 = 0;
+               putdr7(0);
+       }
+       if(p->state == Moribund)
+               p->dr[7] = 0;
+
        if(p->fpstate == FPactive){
                if(p->state == Moribund)
                        fpclear();
@@ -876,7 +609,7 @@ procsave(Proc *p)
                         * until the process runs again and generates an
                         * emulation fault to activate the FPU.
                         */
-                       fpsave(&p->fpsave);
+                       fpsave(p->fpsave);
                }
                p->fpstate = FPinactive;
        }
@@ -896,56 +629,47 @@ procsave(Proc *p)
 }
 
 static void
-shutdown(int ispanic)
+rebootjump(uintptr entry, uintptr code, ulong size)
 {
-       int ms, once;
-
-       lock(&active);
-       if(ispanic)
-               active.ispanic = ispanic;
-       else if(m->machno == 0 && (active.machs & (1<<m->machno)) == 0)
-               active.ispanic = 0;
-       once = active.machs & (1<<m->machno);
+       void (*f)(uintptr, uintptr, ulong);
+       ulong *pdb;
+
+       splhi();
+       arch->introff();
+
        /*
-        * setting exiting will make hzclock() on each processor call exit(0),
-        * which calls shutdown(0) and arch->reset(), which on mp systems is
-        * mpshutdown, from which there is no return: the processor is idled
-        * or initiates a reboot.  clearing our bit in machs avoids calling
-        * exit(0) from hzclock() on this processor.
+        * Modify the machine page table to directly map the low 4MB of memory
+        * This allows the reboot code to turn off the page mapping
         */
-       active.machs &= ~(1<<m->machno);
-       active.exiting = 1;
-       unlock(&active);
-
-       if(once)
-               iprint("cpu%d: exiting\n", m->machno);
-
-       /* wait for any other processors to shutdown */
-       spllo();
-       for(ms = 5*1000; ms > 0; ms -= TK2MS(2)){
-               delay(TK2MS(2));
-               if(active.machs == 0 && consactive() == 0)
-                       break;
-       }
+       pdb = m->pdb;
+       pdb[PDX(0)] = pdb[PDX(KZERO)];
+       mmuflushtlb(PADDR(pdb));
 
-       if(active.ispanic){
-               if(!cpuserver)
-                       for(;;)
-                               halt();
-               if(getconf("*debug"))
-                       delay(5*60*1000);
-               else
-                       delay(10000);
-       }
+       /* setup reboot trampoline function */
+       f = (void*)REBOOTADDR;
+       memmove(f, rebootcode, sizeof(rebootcode));
+
+       /* off we go - never to return */
+       coherence();
+       (*f)(entry, code, size);
+       for(;;);
+}
+
+
+void
+exit(int)
+{
+       cpushutdown();
+       if(m->machno)
+               rebootjump(0, 0, 0);
+       arch->reset();
 }
 
 void
 reboot(void *entry, void *code, ulong size)
 {
-       void (*f)(ulong, ulong, ulong);
-       ulong *pdb;
-
        writeconf();
+       vmxshutdown();
 
        /*
         * the boot processor is cpu0.  execute this function on it
@@ -957,11 +681,8 @@ reboot(void *entry, void *code, ulong size)
                procwired(up, 0);
                sched();
        }
-       shutdown(0);
-
-       iprint("shutting down...\n");
-       delay(200);
-
+       cpushutdown();
+       delay(1000);
        splhi();
 
        /* turn off buffered serial console */
@@ -969,29 +690,6 @@ reboot(void *entry, void *code, ulong size)
 
        /* shutdown devices */
        chandevshutdown();
-       arch->introff();
 
-       /*
-        * Modify the machine page table to directly map the low 4MB of memory
-        * This allows the reboot code to turn off the page mapping
-        */
-       pdb = m->pdb;
-       pdb[PDX(0)] = pdb[PDX(KZERO)];
-       mmuflushtlb(PADDR(pdb));
-
-       /* setup reboot trampoline function */
-       f = (void*)REBOOTADDR;
-       memmove(f, rebootcode, sizeof(rebootcode));
-
-       /* off we go - never to return */
-       coherence();
-       (*f)((ulong)entry & ~0xF0000000UL, PADDR(code), size);
-}
-
-
-void
-exit(int ispanic)
-{
-       shutdown(ispanic);
-       arch->reset();
+       rebootjump((ulong)entry & ~0xF0000000UL, PADDR(code), size);
 }