]> git.lizzy.rs Git - plan9front.git/blobdiff - sys/src/9/pc/trap.c
kernel: cleanup the software mouse cursor mess
[plan9front.git] / sys / src / 9 / pc / trap.c
index a12b65b4dafe471c9c42688126fbb3fd2672d3d5..fadb64f42cb14df93a6a5bb59ad40270e1e03fd5 100644 (file)
@@ -13,6 +13,7 @@ static int trapinited;
 
 void   noted(Ureg*, ulong);
 
+static void debugexc(Ureg*, void*);
 static void debugbpt(Ureg*, void*);
 static void fault386(Ureg*, void*);
 static void doublefault(Ureg*, void*);
@@ -39,12 +40,16 @@ intrenable(int irq, void (*f)(Ureg*, void*), void* a, int tbdf, char *name)
                        irq, tbdf, name);
                return;
        }
-
-       if(tbdf != BUSUNKNOWN && (irq == 0xff || irq == 0)){
-               print("intrenable: got unassigned irq %d, tbdf 0x%uX for %s\n",
-                       irq, tbdf, name);
+       if(tbdf != BUSUNKNOWN && (irq == 0xff || irq == 0))
                irq = -1;
-       }
+
+       /*
+        * IRQ2 doesn't really exist, it's used to gang the interrupt
+        * controllers together. A device set to IRQ2 will appear on
+        * the second interrupt controller as IRQ9.
+        */
+       if(irq == 2)
+               irq = 9;
 
        if((v = xalloc(sizeof(Vctl))) == nil)
                panic("intrenable: out of memory");
@@ -76,75 +81,71 @@ intrenable(int irq, void (*f)(Ureg*, void*), void* a, int tbdf, char *name)
        iunlock(&vctllock);
 }
 
-int
+void
 intrdisable(int irq, void (*f)(Ureg *, void *), void *a, int tbdf, char *name)
 {
        Vctl **pv, *v;
        int vno;
 
-       /*
-        * For now, none of this will work with the APIC code,
-        * there is no mapping between irq and vector as the IRQ
-        * is pretty meaningless.
-        */
-       if(arch->intrvecno == nil)
-               return -1;
-       vno = arch->intrvecno(irq);
+       if(irq == 2)
+               irq = 9;
+       if(arch->intrvecno == nil || (tbdf != BUSUNKNOWN && (irq == 0xff || irq == 0))){
+               /*
+                * on APIC machine, irq is pretty meaningless
+                * and disabling a the vector is not implemented.
+                * however, we still want to remove the matching
+                * Vctl entry to prevent calling Vctl.f() with a
+                * stale Vctl.a pointer.
+                */
+               irq = -1;
+               vno = VectorPIC;
+       } else {
+               vno = arch->intrvecno(irq);
+       }
        ilock(&vctllock);
-       pv = &vctl[vno];
-       while (*pv &&
-                 ((*pv)->irq != irq || (*pv)->tbdf != tbdf || (*pv)->f != f || (*pv)->a != a ||
-                  strcmp((*pv)->name, name)))
-               pv = &((*pv)->next);
-       assert(*pv);
-
-       v = *pv;
-       *pv = (*pv)->next;      /* Link out the entry */
-
-       if(vctl[vno] == nil && arch->intrdisable != nil)
-               arch->intrdisable(irq);
+       do {
+               for(pv = &vctl[vno]; (v = *pv) != nil; pv = &v->next){
+                       if(v->isintr && (v->irq == irq || irq == -1)
+                       && v->tbdf == tbdf && v->f == f && v->a == a
+                       && strcmp(v->name, name) == 0)
+                               break;
+               }
+               if(v != nil){
+                       *pv = v->next;
+                       xfree(v);
+
+                       if(irq != -1 && vctl[vno] == nil && arch->intrdisable != nil)
+                               arch->intrdisable(irq);
+                       break;
+               }
+       } while(irq == -1 && ++vno <= MaxVectorAPIC);
        iunlock(&vctllock);
-       xfree(v);
-       return 0;
 }
 
 static long
-irqallocread(Chan*, void *vbuf, long n, vlong offset)
+irqallocread(Chan*, void *a, long n, vlong offset)
 {
-       char *buf, *p, str[2*(11+1)+KNAMELEN+1+1];
-       int m, vno;
-       long oldn;
+       char buf[2*(11+1)+KNAMELEN+1+1];
+       int vno, m;
        Vctl *v;
 
        if(n < 0 || offset < 0)
                error(Ebadarg);
 
-       oldn = n;
-       buf = vbuf;
        for(vno=0; vno<nelem(vctl); vno++){
                for(v=vctl[vno]; v; v=v->next){
-                       m = snprint(str, sizeof str, "%11d %11d %.*s\n", vno, v->irq, KNAMELEN, v->name);
-                       if(m <= offset) /* if do not want this, skip entry */
-                               offset -= m;
-                       else{
-                               /* skip offset bytes */
-                               m -= offset;
-                               p = str+offset;
-                               offset = 0;
-
-                               /* write at most max(n,m) bytes */
-                               if(m > n)
-                                       m = n;
-                               memmove(buf, p, m);
-                               n -= m;
-                               buf += m;
-
-                               if(n == 0)
-                                       return oldn;
-                       }
+                       m = snprint(buf, sizeof(buf), "%11d %11d %.*s\n", vno, v->irq, KNAMELEN, v->name);
+                       offset -= m;
+                       if(offset >= 0)
+                               continue;
+                       if(n > -offset)
+                               n = -offset;
+                       offset += m;
+                       memmove(a, buf+offset, n);
+                       return n;
                }
        }
-       return oldn - n;
+       return 0;
 }
 
 void
@@ -181,7 +182,7 @@ nmienable(void)
        outb(0x70, 0);
 
        x = inb(0x61) & 0x07;           /* Enable NMI */
-       outb(0x61, 0x08|x);
+       outb(0x61, 0x0C|x);
        outb(0x61, x);
 }
 
@@ -228,6 +229,7 @@ trapinit(void)
         * Special traps.
         * Syscall() is called directly without going through trap().
         */
+       trapenable(VectorDE, debugexc, 0, "debugexc");
        trapenable(VectorBPT, debugbpt, 0, "debugpt");
        trapenable(VectorPF, fault386, 0, "fault386");
        trapenable(Vector2F, doublefault, 0, "doublefault");
@@ -406,8 +408,8 @@ trap(Ureg* ureg)
                if(0)print("cpu%d: spurious interrupt %d, last %d\n",
                        m->machno, vno, m->lastintr);
                if(0)if(conf.nmach > 1){
-                       for(i = 0; i < 32; i++){
-                               if(!(active.machs & (1<<i)))
+                       for(i = 0; i < MAXMACH; i++){
+                               if(active.machs[i] == 0)
                                        continue;
                                mach = MACHP(i);
                                if(m->machno == mach->machno)
@@ -428,7 +430,8 @@ trap(Ureg* ureg)
                         * Don't re-enable, it confuses the crash dumps.
                        nmienable();
                         */
-                       iprint("cpu%d: PC %#8.8lux\n", m->machno, ureg->pc);
+                       iprint("cpu%d: nmi PC %#8.8lux, status %ux\n",
+                               m->machno, ureg->pc, inb(0x61));
                        while(m->machno != 0)
                                ;
                }
@@ -444,6 +447,7 @@ trap(Ureg* ureg)
                        extern void _forkretiret(void);
                        extern void _rdmsrinst(void);
                        extern void _wrmsrinst(void);
+                       extern void _peekinst(void);
 
                        extern void load_fs(ulong);
                        extern void load_gs(ulong);
@@ -472,6 +476,11 @@ trap(Ureg* ureg)
                                        ureg->pc += 2;
                                        return;
                                }
+                       } else if(pc == _peekinst){
+                               if(vno == VectorGPF){
+                                       ureg->pc += 2;
+                                       return;
+                               }
                        }
                }
 
@@ -631,6 +640,40 @@ dumpstack(void)
        callwithureg(_dumpstack);
 }
 
+static void
+debugexc(Ureg *ureg, void *)
+{
+       u32int dr6, m;
+       char buf[ERRMAX];
+       char *p, *e;
+       int i;
+
+       dr6 = getdr6();
+       if(up == nil)
+               panic("kernel debug exception dr6=%#.8ux", dr6);
+       putdr6(up->dr[6]);
+       if(userureg(ureg))
+               qlock(&up->debug);
+       else if(!canqlock(&up->debug))
+               return;
+       m = up->dr[7];
+       m = (m >> 4 | m >> 3) & 8 | (m >> 3 | m >> 2) & 4 | (m >> 2 | m >> 1) & 2 | (m >> 1 | m) & 1;
+       m &= dr6;
+       if(m == 0){
+               sprint(buf, "sys: debug exception dr6=%#.8ux", dr6);
+               postnote(up, 0, buf, NDebug);
+       }else{
+               p = buf;
+               e = buf + sizeof(buf);
+               p = seprint(p, e, "sys: watchpoint ");
+               for(i = 0; i < 4; i++)
+                       if((m & 1<<i) != 0)
+                               p = seprint(p, e, "%d%s", i, (m >> i + 1 != 0) ? "," : "");
+               postnote(up, 0, buf, NDebug);
+       }
+       qunlock(&up->debug);
+}
+
 static void
 debugbpt(Ureg* ureg, void*)
 {
@@ -672,6 +715,13 @@ fault386(Ureg* ureg, void*)
        if(!user){
                if(vmapsync(addr))
                        return;
+               {
+                       extern void _peekinst(void);
+                       if((void(*)(void))ureg->pc == _peekinst){
+                               ureg->pc += 2;
+                               return;
+                       }
+               }
                if(addr >= USTKTOP)
                        panic("kernel fault: bad address pc=0x%.8lux addr=0x%.8lux", ureg->pc, addr);
                if(up == nil)
@@ -682,7 +732,7 @@ fault386(Ureg* ureg, void*)
 
        insyscall = up->insyscall;
        up->insyscall = 1;
-       n = fault(addr, read);
+       n = fault(addr, ureg->pc, read);
        if(n < 0){
                if(!user){
                        dumpregs(ureg);
@@ -823,7 +873,7 @@ notify(Ureg* ureg)
                return 0;
 
        if(up->fpstate == FPactive){
-               fpsave(&up->fpsave);
+               fpsave(up->fpsave);
                up->fpstate = FPinactive;
        }
        up->fpstate |= FPillegal;
@@ -1054,10 +1104,6 @@ forkchild(Proc *p, Ureg *ureg)
        memmove(cureg, ureg, sizeof(Ureg));
        /* return value of syscall in child */
        cureg->ax = 0;
-
-       /* Things from bottom of syscall which were never executed */
-       p->psstate = 0;
-       p->insyscall = 0;
 }
 
 /* Give enough context in the ureg to produce a kernel stack for