Race betwen the NMI handler and the RTC clock in practially all kernels
I had a customer on x86 notice that sometimes offset 0xf in the CMOS RAM was getting set to invalid values. Their BIOS used this for information about how to boot, and this caused the BIOS to lock up.
They traced it down to the following code in arch/kernel/traps.c (now in include/asm-i386/mach-default/mach_traps.c):
outb(0x8f, 0x70); inb(0x71); /* dummy */ outb(0x0f, 0x70); inb(0x71); /* dummy */
This code is done at the end of NMI handling and is done without locks. 0x70 is the CMOS RAM index register, 0x71 is the data register. The RTC driver uses the same registers to set the RTC. Obviously, this is bad. I'm not exactly how this code does its function, but it is definately conflicting with the RTC driver.
Fixing it is not so easy since you can't claim a lock inside the NMI code that is also claimed by non-NMI code. However, it is fixable, it just requires some special handling (and support of compare-and-exchange). I have attached a first shot at fixing it, relative to 2.6.9. This creates a special lock that is owned by a CPU that also stores the index register being read/written. This is moderatly ugly, I guess, but everything dealing with NMIs is ugly and I couldn't think of a better solution.
-Corey
arch/i386/kernel/time.c +8 -0
@@ -82,6 +82,14 @@spinlock_t rtc_lock =SPIN_LOCK_UNLOCKED ; +/* +* This is a special lock that is owned by the CPU and holds the index +* register we are working with. It is required for NMI access to the +* CMOS/RTC registers. See include/asm-i386/mc146818rtc.h for details. +*/ +volatile unsigned long cmos_lock =0 ; +EXPORT_SYMBOL (cmos_lock); +spinlock_t i8253_lock =SPIN_LOCK_UNLOCKED ;EXPORT_SYMBOL (i8253_lock);
include/asm-i386/mach-default/mach_traps.h +12 -0
@@ -7,6 +7,8 @@#ifndef _MACH_TRAPS_H#define _MACH_TRAPS_H +#include <asm/mc146818rtc.h> +static inline void clear_mem_error (unsigned char reason) { reason = (reason &0xf ) |4 ;
@@ -20,10 +22,20 @@static inline void reassert_nmi (void ) { +int old_reg =-1 ; + +if (do_i_have_lock_cmos ()) + old_reg =current_lock_cmos_reg (); +else +lock_cmos (0 );/* register doesn't matter here */ outb (0x8f ,0x70 );inb (0x71 );/* dummy */ outb (0x0f ,0x70 );inb (0x71 );/* dummy */ +if (old_reg >=0 ) +outb (old_reg,0x70 ); +else +unlock_cmos (); }#endif /* !_MACH_TRAPS_H */
include/asm-i386/mc146818rtc.h +86 -8
110 lines not on this page · raw message
There was a misplaced "+1" in my previous version of this patch (in lock_cmos()).
Here is a corrected version...
arch/i386/kernel/time.c +8 -0
@@ -82,6 +82,14 @@spinlock_t rtc_lock =SPIN_LOCK_UNLOCKED ; +/* +* This is a special lock that is owned by the CPU and holds the index +* register we are working with. It is required for NMI access to the +* CMOS/RTC registers. See include/asm-i386/mc146818rtc.h for details. +*/ +volatile unsigned long cmos_lock =0 ; +EXPORT_SYMBOL (cmos_lock); +spinlock_t i8253_lock =SPIN_LOCK_UNLOCKED ;EXPORT_SYMBOL (i8253_lock);
include/asm-i386/mach-default/mach_traps.h +12 -0
@@ -7,6 +7,8 @@#ifndef _MACH_TRAPS_H#define _MACH_TRAPS_H +#include <asm/mc146818rtc.h> +static inline void clear_mem_error (unsigned char reason) { reason = (reason &0xf ) |4 ;
@@ -20,10 +22,20 @@static inline void reassert_nmi (void ) { +int old_reg =-1 ; + +if (do_i_have_lock_cmos ()) + old_reg =current_lock_cmos_reg (); +else +lock_cmos (0 );/* register doesn't matter here */ outb (0x8f ,0x70 );inb (0x71 );/* dummy */ outb (0x0f ,0x70 );inb (0x71 );/* dummy */ +if (old_reg >=0 ) +outb (old_reg,0x70 ); +else +unlock_cmos (); }#endif /* !_MACH_TRAPS_H */
include/asm-i386/mc146818rtc.h +86 -8
110 lines not on this page · raw message