Port-xen archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

cleaning up NMI handling in trap()



Today I noticed that in trap() on amd64 and i386, the code for handling
NMI is a rat's nest of #ifdefs.  I've tried to clean it up by adding
some weak stub implementations for DDB and KGDB and letting the linker
instead of the preprocessor configure the kernel.  Please have a look at
the attached patch.  I'd like to commit something like it pretty soon.

Dave

-- 
David Young             OJC Technologies
dyoung%ojctech.com@localhost      Urbana, IL * (217) 344-0444 x24
Index: sys/arch/amd64/amd64/trap.c
===================================================================
RCS file: /cvsroot/src/sys/arch/amd64/amd64/trap.c,v
retrieving revision 1.65
diff -u -p -r1.65 trap.c
--- sys/arch/amd64/amd64/trap.c 20 Dec 2010 00:25:24 -0000      1.65
+++ sys/arch/amd64/amd64/trap.c 18 Mar 2011 19:35:48 -0000
@@ -98,9 +98,7 @@ __KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.6
 #include <machine/reg.h>
 #include <machine/trap.h>
 #include <machine/userret.h>
-#ifdef DDB
 #include <machine/db_machdep.h>
-#endif
 
 #include <x86/nmi.h>
 
@@ -108,9 +106,7 @@ __KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.6
 #include "isa.h"
 #endif
 
-#ifdef KGDB
 #include <sys/kgdb.h>
-#endif
 
 #ifdef KDTRACE_HOOKS
 #include <sys/dtrace_bsd.h>
@@ -283,24 +279,17 @@ trap(struct trapframe *frame)
                       " %lx cpl %x rsp %lx\n",
                    type, frame->tf_err, (u_long)frame->tf_rip, frame->tf_cs,
                    frame->tf_rflags, rcr2(), curcpu()->ci_ilevel, 
frame->tf_rsp);
-#ifdef DDB
                if (kdb_trap(type, 0, frame))
                        return;
-#endif
-#ifdef KGDB
                if (kgdb_trap(type, frame))
                        return;
-               else {
-                       /*
-                        * If this is a breakpoint, don't panic
-                        * if we're not connected.
-                        */
-                       if (type == T_BPTFLT) {
-                               printf("kgdb: ignored %s\n", trap_type[type]);
-                               return;
-                       }
+               /*
+                * If this is a breakpoint, don't panic if we're not connected.
+                */
+               if (type == T_BPTFLT && kgdb_disconnected()) {
+                       printf("kgdb: ignored %s\n", trap_type[type]);
+                       return;
                }
-#endif
                panic("trap");
                /*NOTREACHED*/
 
@@ -675,32 +664,17 @@ faultcommon:
                break;
 
        case T_NMI:
-#if !defined(XEN)
                if (nmi_dispatch(frame))
                        return;
-#endif /* !defined(XEN) */
-#if    NISA > 0
-#if defined(KGDB) || defined(DDB)
                /* NMI can be hooked up to a pushbutton for debugging */
-               printf ("NMI ... going to debugger\n");
-#ifdef KGDB
-
                if (kgdb_trap(type, frame))
                        return;
-#endif
-#ifdef DDB
                if (kdb_trap(type, 0, frame))
                        return;
-#endif
-#endif /* KGDB || DDB */
                /* machine/parity/power fail/"kitchen sink" faults */
 
-               if (x86_nmi() != 0)
-                       goto we_re_toast;
-               else
-                       return;
-#endif /* NISA > 0 */
-               ;       /* avoid a label at end of compound statement */
+               x86_nmi();
+               return;
        }
 
        if ((type & T_USER) == 0)
Index: sys/arch/i386/i386/trap.c
===================================================================
RCS file: /cvsroot/src/sys/arch/i386/i386/trap.c,v
retrieving revision 1.260
diff -u -p -r1.260 trap.c
--- sys/arch/i386/i386/trap.c   20 Dec 2010 00:25:35 -0000      1.260
+++ sys/arch/i386/i386/trap.c   18 Mar 2011 19:35:50 -0000
@@ -102,22 +102,16 @@ __KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.2
 #include <machine/reg.h>
 #include <machine/trap.h>
 #include <machine/userret.h>
-#ifdef DDB
 #include <machine/db_machdep.h>
-#endif
 
 #include "mca.h"
-#if NMCA > 0
 #include <machine/mca_machdep.h>
-#endif
 
 #include <x86/nmi.h>
 
 #include "isa.h"
 
-#ifdef KGDB
 #include <sys/kgdb.h>
-#endif
 
 #include "npx.h"
 
@@ -388,24 +382,17 @@ trap(struct trapframe *frame)
                        check_dr0();
                else
                        trap_print(type, frame);
-#ifdef DDB
                if (kdb_trap(type, 0, frame))
                        return;
-#endif
-#ifdef KGDB
                if (kgdb_trap(type, frame))
                        return;
-               else {
-                       /*
-                        * If this is a breakpoint, don't panic
-                        * if we're not connected.
-                        */
-                       if (type == T_BPTFLT) {
-                               printf("kgdb: ignored %s\n", trap_type[type]);
-                               return;
-                       }
+               /*
+                * If this is a breakpoint, don't panic if we're not connected.
+                */
+               if (type == T_BPTFLT && kgdb_disconnected()) {
+                       printf("kgdb: ignored %s\n", trap_type[type]);
+                       return;
                }
-#endif
                panic("trap");
                /*NOTREACHED*/
 
@@ -806,40 +793,16 @@ faultcommon:
                break;
 
        case T_NMI:
-#if !defined(XEN)
                if (nmi_dispatch(frame))
                        return;
-#if (NISA > 0 || NMCA > 0)
-#if defined(KGDB) || defined(DDB)
                /* NMI can be hooked up to a pushbutton for debugging */
-               printf ("NMI ... going to debugger\n");
-#ifdef KGDB
-
                if (kgdb_trap(type, frame))
                        return;
-#endif
-#ifdef DDB
                if (kdb_trap(type, 0, frame))
                        return;
-#endif
-#endif /* KGDB || DDB */
                /* machine/parity/power fail/"kitchen sink" faults */
-
-#if NMCA > 0
-               /* mca_nmi() takes care to call x86_nmi() if appropriate */
-               if (mca_nmi() != 0)
-                       goto we_re_toast;
-               else
-                       return;
-#else /* NISA > 0 */
-               if (x86_nmi() != 0)
-                       goto we_re_toast;
-               else
-                       return;
-#endif /* NMCA > 0 */
-#endif /* (NISA > 0 || NMCA > 0) */
-#endif /* !defined(XEN) */
-               ;       /* avoid a label at end of compound statement */
+               mca_nmi();
+               x86_nmi();
        }
 
        if ((type & T_USER) == 0)
Index: sys/arch/i386/include/mca_machdep.h
===================================================================
RCS file: /cvsroot/src/sys/arch/i386/include/mca_machdep.h,v
retrieving revision 1.13
diff -u -p -r1.13 mca_machdep.h
--- sys/arch/i386/include/mca_machdep.h 4 May 2009 12:19:29 -0000       1.13
+++ sys/arch/i386/include/mca_machdep.h 18 Mar 2011 19:35:50 -0000
@@ -42,7 +42,7 @@ extern struct x86_bus_dma_tag mca_bus_dm
 /* set to 1 if MCA bus is detected */
 extern int MCA_system;
 
-int    mca_nmi(void);
+void   mca_nmi(void);
 
 /*
  * Types provided to machine-independent MCA code.
Index: sys/arch/i386/mca/mca_machdep.c
===================================================================
RCS file: /cvsroot/src/sys/arch/i386/mca/mca_machdep.c,v
retrieving revision 1.39
diff -u -p -r1.39 mca_machdep.c
--- sys/arch/i386/mca/mca_machdep.c     23 Mar 2010 21:18:23 -0000      1.39
+++ sys/arch/i386/mca/mca_machdep.c     18 Mar 2011 19:35:50 -0000
@@ -236,7 +236,7 @@ mca_intr_disestablish(mca_chipset_tag_t 
  * Handle a NMI.
  * return true to panic system, false to ignore.
  */
-int
+void
 mca_nmi(void)
 {
        /*
@@ -272,9 +272,8 @@ mca_nmi(void)
    out:
        if (!mcanmi) {
                /* no CHCK bits asserted, assume ISA NMI */
-               return (x86_nmi());
-       } else
-               return(0);
+               x86_nmi();
+       }
 }
 
 /*
Index: sys/arch/x86/conf/files.x86
===================================================================
RCS file: /cvsroot/src/sys/arch/x86/conf/files.x86,v
retrieving revision 1.57
diff -u -p -r1.57 files.x86
--- sys/arch/x86/conf/files.x86 5 Feb 2011 14:00:34 -0000       1.57
+++ sys/arch/x86/conf/files.x86 18 Mar 2011 19:35:53 -0000
@@ -112,6 +112,9 @@ file        arch/x86/x86/coretemp.c         intel_core
 # VIA C7 Temperature sensor
 file   arch/x86/x86/viac7temp.c        via_c7temp
 
+# Stubs for x86 routines not included in the system
+file   arch/x86/x86/x86_stub.c
+
 # IPMI device
 device ipmi: sysmon_envsys, sysmon_wdog
 attach ipmi at ipmibus
Index: sys/arch/x86/include/intr.h
===================================================================
RCS file: /cvsroot/src/sys/arch/x86/include/intr.h,v
retrieving revision 1.41
diff -u -p -r1.41 intr.h
--- sys/arch/x86/include/intr.h 2 May 2010 18:03:31 -0000       1.41
+++ sys/arch/x86/include/intr.h 18 Mar 2011 19:35:53 -0000
@@ -171,7 +171,7 @@ struct cpu_info;
 struct pcibus_attach_args;
 
 void intr_default_setup(void);
-int x86_nmi(void);
+void x86_nmi(void);
 void *intr_establish(int, struct pic *, int, int, int, int (*)(void *), void 
*, bool);
 void intr_disestablish(struct intrhand *);
 void intr_add_pcibus(struct pcibus_attach_args *);
Index: sys/arch/x86/x86/cpu.c
===================================================================
RCS file: /cvsroot/src/sys/arch/x86/x86/cpu.c,v
retrieving revision 1.80
diff -u -p -r1.80 cpu.c
--- sys/arch/x86/x86/cpu.c      2 Feb 2011 12:26:42 -0000       1.80
+++ sys/arch/x86/x86/cpu.c      18 Mar 2011 19:35:53 -0000
@@ -1071,6 +1071,12 @@ cpu_resume(device_t dv, const pmf_qual_t
 static bool
 cpu_shutdown(device_t dv, int how)
 {
+       struct cpu_softc *sc = device_private(dv);
+       struct cpu_info *ci = sc->sc_info;
+
+       if (ci->ci_flags & CPUF_BSP)
+               return false;
+
        return cpu_suspend(dv, NULL);
 }
 
Index: sys/arch/x86/x86/intr.c
===================================================================
RCS file: /cvsroot/src/sys/arch/x86/x86/intr.c,v
retrieving revision 1.70
diff -u -p -r1.70 intr.c
--- sys/arch/x86/x86/intr.c     22 Jan 2011 14:01:27 -0000      1.70
+++ sys/arch/x86/x86/intr.c     18 Mar 2011 19:35:54 -0000
@@ -222,12 +222,11 @@ intr_default_setup(void)
  * Handle a NMI, possibly a machine check.
  * return true to panic system, false to ignore.
  */
-int
+void
 x86_nmi(void)
 {
 
        log(LOG_CRIT, "NMI port 61 %x, port 70 %x\n", inb(0x61), inb(0x70));
-       return(0);
 }
 
 /*
Index: sys/kern/kgdb_stub.c
===================================================================
RCS file: /cvsroot/src/sys/kern/kgdb_stub.c,v
retrieving revision 1.23
diff -u -p -r1.23 kgdb_stub.c
--- sys/kern/kgdb_stub.c        11 Jan 2009 10:20:53 -0000      1.23
+++ sys/kern/kgdb_stub.c        18 Mar 2011 19:36:03 -0000
@@ -527,3 +527,9 @@ kgdb_trap(int type, db_regs_t *regs)
        kgdb_recover = 0;
        return (1);
 }
+
+int
+kgdb_disconnected(void)
+{
+       return 1;
+}
Index: sys/sys/kgdb.h
===================================================================
RCS file: /cvsroot/src/sys/sys/kgdb.h,v
retrieving revision 1.11
diff -u -p -r1.11 kgdb.h
--- sys/sys/kgdb.h      11 Jan 2009 10:20:53 -0000      1.11
+++ sys/sys/kgdb.h      18 Mar 2011 19:36:13 -0000
@@ -89,6 +89,7 @@ void kgdb_attach(int (*)(void *), void (
 void kgdb_connect(int);
 void kgdb_panic(void);
 int kgdb_trap(int, db_regs_t *);
+int kgdb_disconnected(void);
 
 /*
  * Machine dependent functions needed by kgdb_stub.c
Index: conf/files.xen
===================================================================
RCS file: /cvsroot/src/sys/arch/xen/conf/files.xen,v
retrieving revision 1.110
diff -u -p -r1.110 files.xen
--- conf/files.xen      2 Dec 2010 23:12:30 -0000       1.110
+++ conf/files.xen      18 Mar 2011 20:26:13 -0000
@@ -270,6 +270,9 @@ file        arch/x86/x86/mpacpi.c           acpi
 file   arch/x86/x86/acpi_machdep.c     acpi
 file   arch/x86/x86/i8259.c
 
+# Stubs for x86 routines not included in the system
+file   arch/x86/x86/x86_stub.c
+
 # MP configuration using Intel SMP specification 1.4
 file   arch/x86/x86/mpbios.c           mpbios
 
@@ -280,6 +283,7 @@ file        arch/x86/pci/pci_bus_fixup.c    pci_bu
 file   arch/x86/pci/pci_addr_fixup.c   pci_addr_fixup
 
 file   arch/x86/x86/apic.c             ioapic
+file   arch/x86/x86/nmi.c
 
 device ioapic
 attach ioapic at ioapicbus


Home | Main Index | Thread Index | Old Index