irq_32.c 5.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 *	Copyright (C) 1992, 1998 Linus Torvalds, Ingo Molnar
 *
 * This file contains the lowest level x86-specific interrupt
 * entry, irq-stacks and irq statistics code. All the remaining
 * irq logic is done by the generic kernel/irq/ code and
 * by the x86-specific irq controller code. (e.g. i8259.c and
 * io_apic.c.)
 */

#include <linux/module.h>
#include <linux/seq_file.h>
#include <linux/interrupt.h>
#include <linux/kernel_stat.h>
Z
Zwane Mwaikambo 已提交
15 16 17
#include <linux/notifier.h>
#include <linux/cpu.h>
#include <linux/delay.h>
18
#include <linux/uaccess.h>
19
#include <linux/percpu.h>
20
#include <linux/mm.h>
L
Linus Torvalds 已提交
21

22 23
#include <asm/apic.h>

24
DEFINE_PER_CPU_SHARED_ALIGNED(irq_cpustat_t, irq_stat);
L
Linus Torvalds 已提交
25 26
EXPORT_PER_CPU_SYMBOL(irq_stat);

27 28 29
DEFINE_PER_CPU(struct pt_regs *, irq_regs);
EXPORT_PER_CPU_SYMBOL(irq_regs);

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#ifdef CONFIG_DEBUG_STACKOVERFLOW
/* Debugging check for stack overflow: is there less than 1KB free? */
static int check_stack_overflow(void)
{
	long sp;

	__asm__ __volatile__("andl %%esp,%0" :
			     "=r" (sp) : "0" (THREAD_SIZE - 1));

	return sp < (sizeof(struct thread_info) + STACK_WARN);
}

static void print_stack_overflow(void)
{
	printk(KERN_WARNING "low stack detected by irq handler\n");
	dump_stack();
}

#else
static inline int check_stack_overflow(void) { return 0; }
static inline void print_stack_overflow(void) { }
#endif

L
Linus Torvalds 已提交
53 54 55 56 57 58
/*
 * per-CPU IRQ handling contexts (thread information and stack)
 */
union irq_ctx {
	struct thread_info      tinfo;
	u32                     stack[THREAD_SIZE/sizeof(u32)];
59
} __attribute__((aligned(THREAD_SIZE)));
L
Linus Torvalds 已提交
60

61 62
static DEFINE_PER_CPU(union irq_ctx *, hardirq_ctx);
static DEFINE_PER_CPU(union irq_ctx *, softirq_ctx);
L
Linus Torvalds 已提交
63

64
static void call_on_stack(void *func, void *stack)
65
{
66 67 68 69 70 71 72
	asm volatile("xchgl	%%ebx,%%esp	\n"
		     "call	*%%edi		\n"
		     "movl	%%ebx,%%esp	\n"
		     : "=b" (stack)
		     : "0" (stack),
		       "D"(func)
		     : "memory", "cc", "edx", "ecx", "eax");
73
}
L
Linus Torvalds 已提交
74

75 76 77 78
static inline int
execute_on_irq_stack(int overflow, struct irq_desc *desc, int irq)
{
	union irq_ctx *curctx, *irqctx;
79
	u32 *isp, arg1, arg2;
L
Linus Torvalds 已提交
80 81

	curctx = (union irq_ctx *) current_thread_info();
T
Tejun Heo 已提交
82
	irqctx = __this_cpu_read(hardirq_ctx);
L
Linus Torvalds 已提交
83 84 85 86 87 88 89

	/*
	 * this is where we switch to the IRQ stack. However, if we are
	 * already using the IRQ stack (because we interrupted a hardirq
	 * handler) we can't do that and just have to keep using the
	 * current stack (which is the irq stack already after all)
	 */
90 91
	if (unlikely(curctx == irqctx))
		return 0;
L
Linus Torvalds 已提交
92

93
	/* build the stack frame on the IRQ stack */
94
	isp = (u32 *) ((char *)irqctx + sizeof(*irqctx));
95 96
	irqctx->tinfo.task = curctx->tinfo.task;
	irqctx->tinfo.previous_esp = current_stack_pointer;
L
Linus Torvalds 已提交
97

98 99 100 101 102 103 104 105 106
	/*
	 * Copy the softirq bits in preempt_count so that the
	 * softirq checks work in the hardirq context.
	 */
	irqctx->tinfo.preempt_count =
		(irqctx->tinfo.preempt_count & ~SOFTIRQ_MASK) |
		(curctx->tinfo.preempt_count & SOFTIRQ_MASK);

	if (unlikely(overflow))
107 108 109 110 111 112 113 114 115
		call_on_stack(print_stack_overflow, isp);

	asm volatile("xchgl	%%ebx,%%esp	\n"
		     "call	*%%edi		\n"
		     "movl	%%ebx,%%esp	\n"
		     : "=a" (arg1), "=d" (arg2), "=b" (isp)
		     :  "0" (irq),   "1" (desc),  "2" (isp),
			"D" (desc->handle_irq)
		     : "memory", "cc", "ecx");
L
Linus Torvalds 已提交
116 117 118 119 120 121
	return 1;
}

/*
 * allocate per-cpu stacks for hardirq and for softirq processing
 */
122
void __cpuinit irq_ctx_init(int cpu)
L
Linus Torvalds 已提交
123 124 125
{
	union irq_ctx *irqctx;

126
	if (per_cpu(hardirq_ctx, cpu))
L
Linus Torvalds 已提交
127 128
		return;

129 130 131
	irqctx = page_address(alloc_pages_node(cpu_to_node(cpu),
					       THREAD_FLAGS,
					       THREAD_ORDER));
B
Brian Gerst 已提交
132
	memset(&irqctx->tinfo, 0, sizeof(struct thread_info));
133 134 135
	irqctx->tinfo.cpu		= cpu;
	irqctx->tinfo.preempt_count	= HARDIRQ_OFFSET;
	irqctx->tinfo.addr_limit	= MAKE_MM_SEG(0);
L
Linus Torvalds 已提交
136

137
	per_cpu(hardirq_ctx, cpu) = irqctx;
L
Linus Torvalds 已提交
138

139 140 141
	irqctx = page_address(alloc_pages_node(cpu_to_node(cpu),
					       THREAD_FLAGS,
					       THREAD_ORDER));
B
Brian Gerst 已提交
142
	memset(&irqctx->tinfo, 0, sizeof(struct thread_info));
143 144
	irqctx->tinfo.cpu		= cpu;
	irqctx->tinfo.addr_limit	= MAKE_MM_SEG(0);
L
Linus Torvalds 已提交
145

146
	per_cpu(softirq_ctx, cpu) = irqctx;
L
Linus Torvalds 已提交
147

148
	printk(KERN_DEBUG "CPU %u irqstacks, hard=%p soft=%p\n",
149
	       cpu, per_cpu(hardirq_ctx, cpu),  per_cpu(softirq_ctx, cpu));
L
Linus Torvalds 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
}

asmlinkage void do_softirq(void)
{
	unsigned long flags;
	struct thread_info *curctx;
	union irq_ctx *irqctx;
	u32 *isp;

	if (in_interrupt())
		return;

	local_irq_save(flags);

	if (local_softirq_pending()) {
		curctx = current_thread_info();
T
Tejun Heo 已提交
166
		irqctx = __this_cpu_read(softirq_ctx);
L
Linus Torvalds 已提交
167 168 169 170
		irqctx->tinfo.task = curctx->task;
		irqctx->tinfo.previous_esp = current_stack_pointer;

		/* build the stack frame on the softirq stack */
171
		isp = (u32 *) ((char *)irqctx + sizeof(*irqctx));
L
Linus Torvalds 已提交
172

173
		call_on_stack(__do_softirq, isp);
174
		/*
L
Lucas De Marchi 已提交
175
		 * Shouldn't happen, we returned above if in_interrupt():
176
		 */
177
		WARN_ON_ONCE(softirq_count());
L
Linus Torvalds 已提交
178 179 180 181
	}

	local_irq_restore(flags);
}
182

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
bool handle_irq(unsigned irq, struct pt_regs *regs)
{
	struct irq_desc *desc;
	int overflow;

	overflow = check_stack_overflow();

	desc = irq_to_desc(irq);
	if (unlikely(!desc))
		return false;

	if (!execute_on_irq_stack(overflow, desc, irq)) {
		if (unlikely(overflow))
			print_stack_overflow();
		desc->handle_irq(irq, desc);
	}

	return true;
}