公开学习文档

公开学习文档


nmi中断

<h2>概述</h2> <h2>处理流程</h2> <pre><code class="language-c">// file: arch/x86/kernel/nmi.c dotraplinkage notrace void do_nmi(struct pt_regs *regs, long error_code) { if (this_cpu_read(nmi_state) != NMI_NOT_RUNNING) { this_cpu_write(nmi_state, NMI_LATCHED); return; } this_cpu_write(nmi_state, NMI_EXECUTING); this_cpu_write(nmi_cr2, read_cr2()); nmi_restart: #ifdef CONFIG_X86_64 /* * If we interrupted a breakpoint, it is possible that * the nmi handler will have breakpoints too. We need to * change the IDT such that breakpoints that happen here * continue to use the NMI stack. */ if (unlikely(is_debug_stack(regs-&amp;gt;sp))) { debug_stack_set_zero(); this_cpu_write(update_debug_stack, 1); } #endif nmi_enter(); inc_irq_stat(__nmi_count); if (!ignore_nmis) default_do_nmi(regs); // 继续 nmi_exit(); #ifdef CONFIG_X86_64 if (unlikely(this_cpu_read(update_debug_stack))) { debug_stack_reset(); this_cpu_write(update_debug_stack, 0); } #endif if (unlikely(this_cpu_read(nmi_cr2) != read_cr2())) write_cr2(this_cpu_read(nmi_cr2)); if (this_cpu_dec_return(nmi_state)) goto nmi_restart; if (user_mode(regs)) mds_user_clear_cpu_buffers(); } NOKPROBE_SYMBOL(do_nmi); static DEFINE_PER_CPU(bool, swallow_nmi); static DEFINE_PER_CPU(unsigned long, last_nmi_rip); static void default_do_nmi(struct pt_regs *regs) { unsigned char reason = 0; int handled; bool b2b = false; /* * CPU-specific NMI must be processed before non-CPU-specific * NMI, otherwise we may lose it, because the CPU-specific * NMI can not be detected/processed on other CPUs. */ /* * Back-to-back NMIs are interesting because they can either * be two NMI or more than two NMIs (any thing over two is dropped * due to NMI being edge-triggered). If this is the second half * of the back-to-back NMI, assume we dropped things and process * more handlers. Otherwise reset the 'swallow' NMI behaviour */ if (regs-&amp;gt;ip == __this_cpu_read(last_nmi_rip)) b2b = true; else __this_cpu_write(swallow_nmi, false); __this_cpu_write(last_nmi_rip, regs-&amp;gt;ip); handled = nmi_handle(NMI_LOCAL, regs); // 继续 __this_cpu_add(nmi_stats.normal, handled); if (handled) { /* * There are cases when a NMI handler handles multiple * events in the current NMI. One of these events may * be queued for in the next NMI. Because the event is * already handled, the next NMI will result in an unknown * NMI. Instead lets flag this for a potential NMI to * swallow. */ if (handled &amp;gt; 1) __this_cpu_write(swallow_nmi, true); return; } /* * Non-CPU-specific NMI: NMI sources can be processed on any CPU. * * Another CPU may be processing panic routines while holding * nmi_reason_lock. Check if the CPU issued the IPI for crash dumping, * and if so, call its callback directly. If there is no CPU preparing * crash dump, we simply loop here. */ while (!raw_spin_trylock(&amp;amp;nmi_reason_lock)) { run_crash_ipi_callback(regs); cpu_relax(); } reason = x86_platform.get_nmi_reason(); if (reason &amp;amp; NMI_REASON_MASK) { if (reason &amp;amp; NMI_REASON_SERR) pci_serr_error(reason, regs); else if (reason &amp;amp; NMI_REASON_IOCHK) io_check_error(reason, regs); #ifdef CONFIG_X86_32 /* * Reassert NMI in case it became active * meanwhile as it's edge-triggered: */ reassert_nmi(); #endif __this_cpu_add(nmi_stats.external, 1); raw_spin_unlock(&amp;amp;nmi_reason_lock); return; } raw_spin_unlock(&amp;amp;nmi_reason_lock); /* * Only one NMI can be latched at a time. To handle * this we may process multiple nmi handlers at once to * cover the case where an NMI is dropped. The downside * to this approach is we may process an NMI prematurely, * while its real NMI is sitting latched. This will cause * an unknown NMI on the next run of the NMI processing. * * We tried to flag that condition above, by setting the * swallow_nmi flag when we process more than one event. * This condition is also only present on the second half * of a back-to-back NMI, so we flag that condition too. * * If both are true, we assume we already processed this * NMI previously and we swallow it. Otherwise we reset * the logic. * * There are scenarios where we may accidentally swallow * a 'real' unknown NMI. For example, while processing * a perf NMI another perf NMI comes in along with a * 'real' unknown NMI. These two NMIs get combined into * one (as descibed above). When the next NMI gets * processed, it will be flagged by perf as handled, but * noone will know that there was a 'real' unknown NMI sent * also. As a result it gets swallowed. Or if the first * perf NMI returns two events handled then the second * NMI will get eaten by the logic below, again losing a * 'real' unknown NMI. But this is the best we can do * for now. */ if (b2b &amp;amp;&amp;amp; __this_cpu_read(swallow_nmi)) __this_cpu_add(nmi_stats.swallow, 1); else unknown_nmi_error(reason, regs); } NOKPROBE_SYMBOL(default_do_nmi); static int nmi_handle(unsigned int type, struct pt_regs *regs) { struct nmi_desc *desc = nmi_to_desc(type); struct nmiaction *a; int handled=0; rcu_read_lock(); /* * NMIs are edge-triggered, which means if you have enough * of them concurrently, you can lose some because only one * can be latched at any given time. Walk the whole list * to handle those situations. */ list_for_each_entry_rcu(a, &amp;amp;desc-&amp;gt;head, list) { int thishandled; u64 delta; delta = sched_clock(); thishandled = a-&amp;gt;handler(type, regs); handled += thishandled; delta = sched_clock() - delta; trace_nmi_handler(a-&amp;gt;handler, (int)delta, thishandled); nmi_check_duration(a, delta); } rcu_read_unlock(); /* return total number of NMI events handled */ return handled; } NOKPROBE_SYMBOL(nmi_handle); </code></pre>

页面列表

ITEM_HTML