diff options
author | Steven Rostedt <srostedt@redhat.com> | 2012-06-07 11:54:37 -0400 |
---|---|---|
committer | Steven Rostedt <rostedt@goodmis.org> | 2012-06-08 18:51:12 -0400 |
commit | 70fb74a5420f9caa3e001d65004e4b669124283e (patch) | |
tree | a099c86b2e63dc5d6246b8efe07a5b3e91b1301e | |
parent | c7d65a78fc18ed70353baeb7497ec71a7c775ac5 (diff) |
x86: Save cr2 in NMI in case NMIs take a page fault (for i386)
Avi Kivity reported that page faults in NMIs could cause havic if
the NMI preempted another page fault handler:
The recent changes to NMI allow exceptions to take place in NMI
handlers, but I think that a #PF (say, due to access to vmalloc space)
is still problematic. Consider the sequence
#PF (cr2 set by processor)
NMI
...
#PF (cr2 clobbered)
do_page_fault()
IRET
...
IRET
do_page_fault()
address = read_cr2()
The last line reads the overwritten cr2 value.
This is the i386 version, which has the luxury of doing the work
in C code.
Link: http://lkml.kernel.org/r/4FBB8C40.6080304@redhat.com
Reported-by: Avi Kivity <avi@redhat.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
-rw-r--r-- | arch/x86/kernel/nmi.c | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/arch/x86/kernel/nmi.c b/arch/x86/kernel/nmi.c index a15a88800661..f84f5c57de35 100644 --- a/arch/x86/kernel/nmi.c +++ b/arch/x86/kernel/nmi.c | |||
@@ -395,6 +395,14 @@ static __kprobes void default_do_nmi(struct pt_regs *regs) | |||
395 | * thus there is no race between the first check of state for NOT_RUNNING | 395 | * thus there is no race between the first check of state for NOT_RUNNING |
396 | * and setting it to NMI_EXECUTING. The HW will prevent nested NMIs | 396 | * and setting it to NMI_EXECUTING. The HW will prevent nested NMIs |
397 | * at this point. | 397 | * at this point. |
398 | * | ||
399 | * In case the NMI takes a page fault, we need to save off the CR2 | ||
400 | * because the NMI could have preempted another page fault and corrupt | ||
401 | * the CR2 that is about to be read. As nested NMIs must be restarted | ||
402 | * and they can not take breakpoints or page faults, the update of the | ||
403 | * CR2 must be done before converting the nmi state back to NOT_RUNNING. | ||
404 | * Otherwise, there would be a race of another nested NMI coming in | ||
405 | * after setting state to NOT_RUNNING but before updating the nmi_cr2. | ||
398 | */ | 406 | */ |
399 | enum nmi_states { | 407 | enum nmi_states { |
400 | NMI_NOT_RUNNING = 0, | 408 | NMI_NOT_RUNNING = 0, |
@@ -402,6 +410,7 @@ enum nmi_states { | |||
402 | NMI_LATCHED, | 410 | NMI_LATCHED, |
403 | }; | 411 | }; |
404 | static DEFINE_PER_CPU(enum nmi_states, nmi_state); | 412 | static DEFINE_PER_CPU(enum nmi_states, nmi_state); |
413 | static DEFINE_PER_CPU(unsigned long, nmi_cr2); | ||
405 | 414 | ||
406 | #define nmi_nesting_preprocess(regs) \ | 415 | #define nmi_nesting_preprocess(regs) \ |
407 | do { \ | 416 | do { \ |
@@ -410,11 +419,14 @@ static DEFINE_PER_CPU(enum nmi_states, nmi_state); | |||
410 | return; \ | 419 | return; \ |
411 | } \ | 420 | } \ |
412 | this_cpu_write(nmi_state, NMI_EXECUTING); \ | 421 | this_cpu_write(nmi_state, NMI_EXECUTING); \ |
422 | this_cpu_write(nmi_cr2, read_cr2()); \ | ||
413 | } while (0); \ | 423 | } while (0); \ |
414 | nmi_restart: | 424 | nmi_restart: |
415 | 425 | ||
416 | #define nmi_nesting_postprocess() \ | 426 | #define nmi_nesting_postprocess() \ |
417 | do { \ | 427 | do { \ |
428 | if (unlikely(this_cpu_read(nmi_cr2) != read_cr2())) \ | ||
429 | write_cr2(this_cpu_read(nmi_cr2)); \ | ||
418 | if (this_cpu_dec_return(nmi_state)) \ | 430 | if (this_cpu_dec_return(nmi_state)) \ |
419 | goto nmi_restart; \ | 431 | goto nmi_restart; \ |
420 | } while (0) | 432 | } while (0) |