Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Copyright (C) 1992, 1998 Linus Torvalds, Ingo Molnar |
| 4 | * |
| 5 | * This file contains the lowest level x86_64-specific interrupt |
| 6 | * entry and irq statistics code. All the remaining irq logic is |
| 7 | * done by the generic kernel/irq/ code and in the |
| 8 | * x86_64-specific irq controller code. (e.g. i8259.c and |
| 9 | * io_apic.c.) |
| 10 | */ |
| 11 | |
| 12 | #include <linux/kernel_stat.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/seq_file.h> |
Ashok Raj | 76e4f66 | 2005-06-25 14:55:00 -0700 | [diff] [blame] | 15 | #include <linux/delay.h> |
Frederic Weisbecker | bcbc4f2 | 2008-12-09 23:54:20 +0100 | [diff] [blame] | 16 | #include <linux/ftrace.h> |
Jaswinder Singh Rajput | 5f66b2a | 2009-01-04 16:25:19 +0530 | [diff] [blame] | 17 | #include <linux/uaccess.h> |
| 18 | #include <linux/smp.h> |
Ingo Molnar | 68db0cf | 2017-02-08 18:51:37 +0100 | [diff] [blame] | 19 | #include <linux/sched/task_stack.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <asm/io_apic.h> |
Brian Gerst | 3819cd4 | 2009-01-23 11:03:29 +0900 | [diff] [blame] | 21 | #include <asm/apic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Mitsuo Hayasaka | 55af779 | 2011-11-29 15:08:36 +0900 | [diff] [blame] | 23 | int sysctl_panic_on_stackoverflow; |
| 24 | |
Eric Sandeen | 4961f10 | 2006-06-26 14:00:05 +0200 | [diff] [blame] | 25 | /* |
| 26 | * Probabilistic stack overflow check: |
| 27 | * |
| 28 | * Only check the stack in process context, because everything else |
| 29 | * runs on the big interrupt stacks. Checking reliably is too expensive, |
| 30 | * so we just check from interrupts. |
| 31 | */ |
| 32 | static inline void stack_overflow_check(struct pt_regs *regs) |
| 33 | { |
Ingo Molnar | f377fa1 | 2008-11-23 09:02:26 +0100 | [diff] [blame] | 34 | #ifdef CONFIG_DEBUG_STACKOVERFLOW |
Mitsuo Hayasaka | d2db661 | 2011-12-07 17:29:10 +0900 | [diff] [blame] | 35 | #define STACK_TOP_MARGIN 128 |
Mitsuo Hayasaka | 37fe6a4 | 2011-11-29 15:08:29 +0900 | [diff] [blame] | 36 | struct orig_ist *oist; |
| 37 | u64 irq_stack_top, irq_stack_bottom; |
| 38 | u64 estack_top, estack_bottom; |
Roman Zippel | c9f4f06 | 2007-05-09 02:35:16 -0700 | [diff] [blame] | 39 | u64 curbase = (u64)task_stack_page(current); |
Eric Sandeen | 4961f10 | 2006-06-26 14:00:05 +0200 | [diff] [blame] | 40 | |
Andy Lutomirski | f39b6f0 | 2015-03-18 18:33:33 -0700 | [diff] [blame] | 41 | if (user_mode(regs)) |
Mitsuo Hayasaka | 69682b6 | 2011-11-29 15:08:21 +0900 | [diff] [blame] | 42 | return; |
| 43 | |
Andy Lutomirski | 15f4eae | 2016-09-13 14:29:25 -0700 | [diff] [blame] | 44 | if (regs->sp >= curbase + sizeof(struct pt_regs) + STACK_TOP_MARGIN && |
Mitsuo Hayasaka | 467e6b7 | 2011-11-29 15:08:45 +0900 | [diff] [blame] | 45 | regs->sp <= curbase + THREAD_SIZE) |
Mitsuo Hayasaka | 37fe6a4 | 2011-11-29 15:08:29 +0900 | [diff] [blame] | 46 | return; |
Ingo Molnar | f377fa1 | 2008-11-23 09:02:26 +0100 | [diff] [blame] | 47 | |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 48 | irq_stack_top = (u64)this_cpu_ptr(irq_stack_union.irq_stack) + |
Mitsuo Hayasaka | d2db661 | 2011-12-07 17:29:10 +0900 | [diff] [blame] | 49 | STACK_TOP_MARGIN; |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 50 | irq_stack_bottom = (u64)__this_cpu_read(irq_stack_ptr); |
Mitsuo Hayasaka | 37fe6a4 | 2011-11-29 15:08:29 +0900 | [diff] [blame] | 51 | if (regs->sp >= irq_stack_top && regs->sp <= irq_stack_bottom) |
| 52 | return; |
| 53 | |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 54 | oist = this_cpu_ptr(&orig_ist); |
Mitsuo Hayasaka | d2db661 | 2011-12-07 17:29:10 +0900 | [diff] [blame] | 55 | estack_top = (u64)oist->ist[0] - EXCEPTION_STKSZ + STACK_TOP_MARGIN; |
Mitsuo Hayasaka | 37fe6a4 | 2011-11-29 15:08:29 +0900 | [diff] [blame] | 56 | estack_bottom = (u64)oist->ist[N_EXCEPTION_STACKS - 1]; |
| 57 | if (regs->sp >= estack_top && regs->sp <= estack_bottom) |
| 58 | return; |
| 59 | |
Andy Lutomirski | 4f3789e7 | 2017-12-04 15:07:11 +0100 | [diff] [blame] | 60 | WARN_ONCE(1, "do_IRQ(): %s has overflown the kernel stack (cur:%Lx,sp:%lx,irq stk top-bottom:%Lx-%Lx,exception stk top-bottom:%Lx-%Lx,ip:%pF)\n", |
Mitsuo Hayasaka | 37fe6a4 | 2011-11-29 15:08:29 +0900 | [diff] [blame] | 61 | current->comm, curbase, regs->sp, |
| 62 | irq_stack_top, irq_stack_bottom, |
Andy Lutomirski | 4f3789e7 | 2017-12-04 15:07:11 +0100 | [diff] [blame] | 63 | estack_top, estack_bottom, (void *)regs->ip); |
Mitsuo Hayasaka | 55af779 | 2011-11-29 15:08:36 +0900 | [diff] [blame] | 64 | |
| 65 | if (sysctl_panic_on_stackoverflow) |
| 66 | panic("low stack detected by irq handler - check messages\n"); |
Eric Sandeen | 4961f10 | 2006-06-26 14:00:05 +0200 | [diff] [blame] | 67 | #endif |
Ingo Molnar | f377fa1 | 2008-11-23 09:02:26 +0100 | [diff] [blame] | 68 | } |
Eric Sandeen | 4961f10 | 2006-06-26 14:00:05 +0200 | [diff] [blame] | 69 | |
Thomas Gleixner | a782a7e | 2015-08-02 20:38:27 +0000 | [diff] [blame] | 70 | bool handle_irq(struct irq_desc *desc, struct pt_regs *regs) |
Jeremy Fitzhardinge | 9b2b76a | 2009-02-06 14:09:40 -0800 | [diff] [blame] | 71 | { |
Jeremy Fitzhardinge | 9b2b76a | 2009-02-06 14:09:40 -0800 | [diff] [blame] | 72 | stack_overflow_check(regs); |
| 73 | |
Geliang Tang | a7e705a | 2015-10-01 10:55:29 +0800 | [diff] [blame] | 74 | if (IS_ERR_OR_NULL(desc)) |
Jeremy Fitzhardinge | 9b2b76a | 2009-02-06 14:09:40 -0800 | [diff] [blame] | 75 | return false; |
| 76 | |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 77 | generic_handle_irq_desc(desc); |
Jeremy Fitzhardinge | 9b2b76a | 2009-02-06 14:09:40 -0800 | [diff] [blame] | 78 | return true; |
| 79 | } |