2 * linux/arch/i386/traps.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
8 * 'Traps.c' handles hardware traps and faults after we have saved some
11 #include <linux/config.h>
12 #include <linux/sched.h>
13 #include <linux/kernel.h>
14 #include <linux/string.h>
15 #include <linux/errno.h>
16 #include <linux/ptrace.h>
17 #include <linux/timer.h>
19 #include <linux/smp.h>
20 #include <linux/smp_lock.h>
21 #include <linux/init.h>
22 #include <linux/delay.h>
23 #include <linux/spinlock.h>
26 #include <linux/mca.h>
27 #include <asm/processor.h>
30 #include <asm/system.h>
31 #include <asm/uaccess.h>
33 #include <asm/atomic.h>
34 #include <asm/debugreg.h>
39 #ifdef CONFIG_X86_VISWS_APIC
40 #include <asm/fixmap.h>
41 #include <asm/cobalt.h>
42 #include <asm/lithium.h>
45 #include <linux/irq.h>
47 asmlinkage
int system_call(void);
48 asmlinkage
void lcall7(void);
49 asmlinkage
void lcall27(void);
51 struct desc_struct default_ldt
[] = { { 0, 0 }, { 0, 0 }, { 0, 0 },
55 * The IDT has to be page-aligned to simplify the Pentium
56 * F0 0F bug workaround.. We have a special link segment
59 struct desc_struct idt_table
[256] __attribute__((__section__(".data.idt"))) = { {0, 0}, };
61 static inline void console_verbose(void)
63 extern int console_loglevel
;
64 console_loglevel
= 15;
67 #define DO_ERROR(trapnr, signr, str, name, tsk) \
68 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
70 tsk->thread.error_code = error_code; \
71 tsk->thread.trap_no = trapnr; \
72 die_if_no_fixup(str,regs,error_code); \
73 force_sig(signr, tsk); \
76 #define DO_VM86_ERROR(trapnr, signr, str, name, tsk) \
77 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
80 if (regs->eflags & VM_MASK) { \
81 if (!handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, trapnr)) \
83 /* else fall through */ \
85 tsk->thread.error_code = error_code; \
86 tsk->thread.trap_no = trapnr; \
87 force_sig(signr, tsk); \
88 die_if_kernel(str,regs,error_code); \
93 void page_exception(void);
95 asmlinkage
void divide_error(void);
96 asmlinkage
void debug(void);
97 asmlinkage
void nmi(void);
98 asmlinkage
void int3(void);
99 asmlinkage
void overflow(void);
100 asmlinkage
void bounds(void);
101 asmlinkage
void invalid_op(void);
102 asmlinkage
void device_not_available(void);
103 asmlinkage
void double_fault(void);
104 asmlinkage
void coprocessor_segment_overrun(void);
105 asmlinkage
void invalid_TSS(void);
106 asmlinkage
void segment_not_present(void);
107 asmlinkage
void stack_segment(void);
108 asmlinkage
void general_protection(void);
109 asmlinkage
void page_fault(void);
110 asmlinkage
void coprocessor_error(void);
111 asmlinkage
void reserved(void);
112 asmlinkage
void alignment_check(void);
113 asmlinkage
void spurious_interrupt_bug(void);
115 int kstack_depth_to_print
= 24;
118 * These constants are for searching for possible module text
119 * segments. VMALLOC_OFFSET comes from mm/vmalloc.c; MODULE_RANGE is
120 * a guess of how much space is likely to be vmalloced.
122 #define VMALLOC_OFFSET (8*1024*1024)
123 #define MODULE_RANGE (8*1024*1024)
125 static void show_registers(struct pt_regs
*regs
)
131 unsigned long *stack
, addr
, module_start
, module_end
;
133 esp
= (unsigned long) (1+regs
);
138 ss
= regs
->xss
& 0xffff;
140 printk("CPU: %d\nEIP: %04x:[<%08lx>]\nEFLAGS: %08lx\n",
141 smp_processor_id(), 0xffff & regs
->xcs
, regs
->eip
, regs
->eflags
);
142 printk("eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n",
143 regs
->eax
, regs
->ebx
, regs
->ecx
, regs
->edx
);
144 printk("esi: %08lx edi: %08lx ebp: %08lx esp: %08lx\n",
145 regs
->esi
, regs
->edi
, regs
->ebp
, esp
);
146 printk("ds: %04x es: %04x ss: %04x\n",
147 regs
->xds
& 0xffff, regs
->xes
& 0xffff, ss
);
148 printk("Process %s (pid: %d, stackpage=%08lx)",
149 current
->comm
, current
->pid
, 4096+(unsigned long)current
);
151 * When in-kernel, we also print out the stack and code at the
152 * time of the fault..
156 stack
= (unsigned long *) esp
;
157 for(i
=0; i
< kstack_depth_to_print
; i
++) {
158 if (((long) stack
& 4095) == 0)
160 if (i
&& ((i
% 8) == 0))
162 printk("%08lx ", *stack
++);
164 printk("\nCall Trace: ");
165 stack
= (unsigned long *) esp
;
167 module_start
= PAGE_OFFSET
+ (max_mapnr
<< PAGE_SHIFT
);
168 module_start
= ((module_start
+ VMALLOC_OFFSET
) & ~(VMALLOC_OFFSET
-1));
169 module_end
= module_start
+ MODULE_RANGE
;
170 while (((long) stack
& 4095) != 0) {
173 * If the address is either in the text segment of the
174 * kernel, or in the region which contains vmalloc'ed
175 * memory, it *may* be the address of a calling
176 * routine; if so, print it so that someone tracing
177 * down the cause of the crash will be able to figure
178 * out the call path that was taken.
180 if (((addr
>= (unsigned long) &_stext
) &&
181 (addr
<= (unsigned long) &_etext
)) ||
182 ((addr
>= module_start
) && (addr
<= module_end
))) {
183 if (i
&& ((i
% 8) == 0))
185 printk("[<%08lx>] ", addr
);
191 printk("%02x ", ((unsigned char *)regs
->eip
)[i
]);
198 void die(const char * str
, struct pt_regs
* regs
, long err
)
201 spin_lock_irq(&die_lock
);
202 printk("%s: %04lx\n", str
, err
& 0xffff);
203 show_registers(regs
);
205 spin_lock_irq(&die_lock
);
207 spin_unlock_irq(&die_lock
);
211 static inline void die_if_kernel(const char * str
, struct pt_regs
* regs
, long err
)
213 if (!(regs
->eflags
& VM_MASK
) && !(3 & regs
->xcs
))
217 static void die_if_no_fixup(const char * str
, struct pt_regs
* regs
, long err
)
219 if (!(regs
->eflags
& VM_MASK
) && !(3 & regs
->xcs
))
222 fixup
= search_exception_table(regs
->eip
);
231 DO_VM86_ERROR( 0, SIGFPE
, "divide error", divide_error
, current
)
232 DO_VM86_ERROR( 3, SIGTRAP
, "int3", int3
, current
)
233 DO_VM86_ERROR( 4, SIGSEGV
, "overflow", overflow
, current
)
234 DO_VM86_ERROR( 5, SIGSEGV
, "bounds", bounds
, current
)
235 DO_ERROR( 6, SIGILL
, "invalid operand", invalid_op
, current
)
236 DO_VM86_ERROR( 7, SIGSEGV
, "device not available", device_not_available
, current
)
237 DO_ERROR( 8, SIGSEGV
, "double fault", double_fault
, current
)
238 DO_ERROR( 9, SIGFPE
, "coprocessor segment overrun", coprocessor_segment_overrun
, current
)
239 DO_ERROR(10, SIGSEGV
, "invalid TSS", invalid_TSS
, current
)
240 DO_ERROR(11, SIGBUS
, "segment not present", segment_not_present
, current
)
241 DO_ERROR(12, SIGBUS
, "stack segment", stack_segment
, current
)
242 DO_ERROR(17, SIGSEGV
, "alignment check", alignment_check
, current
)
243 DO_ERROR(18, SIGSEGV
, "reserved", reserved
, current
)
244 /* I don't have documents for this but it does seem to cover the cache
245 flush from user space exception some people get. */
246 DO_ERROR(19, SIGSEGV
, "cache flush denied", cache_flush_denied
, current
)
248 asmlinkage
void cache_flush_denied(struct pt_regs
* regs
, long error_code
)
250 if (regs
->eflags
& VM_MASK
) {
251 handle_vm86_fault((struct kernel_vm86_regs
*) regs
, error_code
);
254 die_if_kernel("cache flush denied",regs
,error_code
);
255 current
->thread
.error_code
= error_code
;
256 current
->thread
.trap_no
= 19;
257 force_sig(SIGSEGV
, current
);
260 asmlinkage
void do_general_protection(struct pt_regs
* regs
, long error_code
)
262 if (regs
->eflags
& VM_MASK
)
265 if (!(regs
->xcs
& 3))
268 current
->thread
.error_code
= error_code
;
269 current
->thread
.trap_no
= 13;
270 force_sig(SIGSEGV
, current
);
275 handle_vm86_fault((struct kernel_vm86_regs
*) regs
, error_code
);
282 fixup
= search_exception_table(regs
->eip
);
287 die("general protection fault", regs
, error_code
);
291 static void mem_parity_error(unsigned char reason
, struct pt_regs
* regs
)
293 printk("Uhhuh. NMI received. Dazed and confused, but trying to continue\n");
294 printk("You probably have a hardware problem with your RAM chips\n");
297 static void io_check_error(unsigned char reason
, struct pt_regs
* regs
)
301 printk("NMI: IOCK error (debug interrupt?)\n");
302 show_registers(regs
);
304 /* Re-enable the IOCK line, wait for a few seconds */
308 while (--i
) udelay(1000);
313 static void unknown_nmi_error(unsigned char reason
, struct pt_regs
* regs
)
316 /* Might actually be able to figure out what the guilty party
323 printk("Uhhuh. NMI received for unknown reason %02x.\n", reason
);
324 printk("Dazed and confused, but trying to continue\n");
325 printk("Do you have a strange power saving mode enabled?\n");
328 asmlinkage
void do_nmi(struct pt_regs
* regs
, long error_code
)
330 unsigned char reason
= inb(0x61);
331 extern atomic_t nmi_counter
;
333 atomic_inc(&nmi_counter
);
335 mem_parity_error(reason
, regs
);
337 io_check_error(reason
, regs
);
338 if (!(reason
& 0xc0))
339 unknown_nmi_error(reason
, regs
);
343 * Careful - we must not do a lock-kernel until we have checked that the
344 * debug fault happened in user mode. Getting debug exceptions while
345 * in the kernel has to be handled without locking, to avoid deadlocks..
347 * Being careful here means that we don't have to be as careful in a
348 * lot of more complicated places (task switching can be a bit lazy
349 * about restoring all the debug state, and ptrace doesn't have to
350 * find every occurrence of the TF bit that could be saved away even
351 * by user code - and we don't have to be careful about what values
352 * can be written to the debug registers because there are no really
355 asmlinkage
void do_debug(struct pt_regs
* regs
, long error_code
)
357 unsigned int condition
;
358 struct task_struct
*tsk
= current
;
360 __asm__
__volatile__("movl %%db6,%0" : "=r" (condition
));
362 /* Mask out spurious debug traps due to lazy DR7 setting */
363 if (condition
& (DR_TRAP0
|DR_TRAP1
|DR_TRAP2
|DR_TRAP3
)) {
364 if (!tsk
->thread
.debugreg
[7])
368 if (regs
->eflags
& VM_MASK
)
371 /* Mask out spurious TF errors due to lazy TF clearing */
372 if (condition
& DR_STEP
) {
374 * The TF error should be masked out only if the current
375 * process is not traced and if the TRAP flag has been set
376 * previously by a tracing process (condition detected by
377 * the PF_DTRACE flag); remember that the i386 TRAP flag
378 * can be modified by the process itself in user mode,
379 * allowing programs to debug themselves without the ptrace()
382 if ((tsk
->flags
& (PF_DTRACE
|PF_PTRACED
)) == PF_DTRACE
)
386 /* If this is a kernel mode trap, we need to reset db7 to allow us to continue sanely */
387 if ((regs
->xcs
& 3) == 0)
390 /* Ok, finally something we can handle */
391 tsk
->thread
.trap_no
= 1;
392 tsk
->thread
.error_code
= error_code
;
393 force_sig(SIGTRAP
, tsk
);
398 handle_vm86_trap((struct kernel_vm86_regs
*) regs
, error_code
, 1);
403 __asm__("movl %0,%%db7"
409 regs
->eflags
&= ~TF_MASK
;
414 * Note that we play around with the 'TS' bit in an attempt to get
415 * the correct behaviour even in the presence of the asynchronous
418 void math_error(void)
420 struct task_struct
* task
;
423 * Save the info for the exception handler
424 * (this will also clear the error)
428 task
->thread
.trap_no
= 16;
429 task
->thread
.error_code
= 0;
430 force_sig(SIGFPE
, task
);
433 asmlinkage
void do_coprocessor_error(struct pt_regs
* regs
, long error_code
)
439 asmlinkage
void do_spurious_interrupt_bug(struct pt_regs
* regs
,
443 /* No need to warn about this any longer. */
444 printk("Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
449 * 'math_state_restore()' saves the current math information in the
450 * old math state array, and gets the new ones from the current task
452 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
453 * Don't touch unless you *really* know how it works.
455 asmlinkage
void math_state_restore(struct pt_regs regs
)
457 __asm__
__volatile__("clts"); /* Allow maths ops (or we recurse) */
458 if(current
->used_math
)
459 __asm__("frstor %0": :"m" (current
->thread
.i387
));
463 * Our first FPU usage, clean the chip.
466 current
->used_math
= 1;
468 current
->flags
|=PF_USEDFPU
; /* So we fnsave on switch_to() */
471 #ifndef CONFIG_MATH_EMULATION
473 asmlinkage
void math_emulate(long arg
)
476 printk("math-emulation not enabled and no coprocessor found.\n");
477 printk("killing %s.\n",current
->comm
);
478 force_sig(SIGFPE
,current
);
483 #endif /* CONFIG_MATH_EMULATION */
485 void __init
trap_init_f00f_bug(void)
494 * Allocate a new page in virtual address space,
495 * move the IDT into it and write protect this page.
497 page
= (unsigned long) vmalloc(PAGE_SIZE
);
498 pgd
= pgd_offset(&init_mm
, page
);
499 pmd
= pmd_offset(pgd
, page
);
500 pte
= pte_offset(pmd
, page
);
501 free_page(pte_page(*pte
));
502 *pte
= mk_pte(&idt_table
, PAGE_KERNEL_RO
);
506 * "idt" is magic - it overlaps the idt_descr
507 * variable so that updating idt will automatically
508 * update the idt descriptor..
510 idt
= (struct desc_struct
*)page
;
511 __asm__
__volatile__("lidt %0": "=m" (idt_descr
));
514 #define _set_gate(gate_addr,type,dpl,addr) \
517 __asm__ __volatile__ ("movw %%dx,%%ax\n\t" \
519 "movl %%eax,%0\n\t" \
521 :"=m" (*((long *) (gate_addr))), \
522 "=m" (*(1+(long *) (gate_addr))), "=&a" (__d0), "=&d" (__d1) \
523 :"i" ((short) (0x8000+(dpl<<13)+(type<<8))), \
524 "3" ((char *) (addr)),"2" (__KERNEL_CS << 16)); \
529 * This needs to use 'idt_table' rather than 'idt', and
530 * thus use the _nonmapped_ version of the IDT, as the
531 * Pentium F0 0F bugfix can have resulted in the mapped
532 * IDT being write-protected.
534 void set_intr_gate(unsigned int n
, void *addr
)
536 _set_gate(idt_table
+n
,14,0,addr
);
539 static void __init
set_trap_gate(unsigned int n
, void *addr
)
541 _set_gate(idt_table
+n
,15,0,addr
);
544 static void __init
set_system_gate(unsigned int n
, void *addr
)
546 _set_gate(idt_table
+n
,15,3,addr
);
549 static void __init
set_call_gate(void *a
, void *addr
)
551 _set_gate(a
,12,3,addr
);
554 #define _set_seg_desc(gate_addr,type,dpl,base,limit) {\
555 *((gate_addr)+1) = ((base) & 0xff000000) | \
556 (((base) & 0x00ff0000)>>16) | \
557 ((limit) & 0xf0000) | \
561 *(gate_addr) = (((base) & 0x0000ffff)<<16) | \
562 ((limit) & 0x0ffff); }
564 #define _set_tssldt_desc(n,addr,limit,type) \
565 __asm__ __volatile__ ("movw %3,0(%2)\n\t" \
566 "movw %%ax,2(%2)\n\t" \
567 "rorl $16,%%eax\n\t" \
568 "movb %%al,4(%2)\n\t" \
569 "movb %4,5(%2)\n\t" \
570 "movb $0,6(%2)\n\t" \
571 "movb %%ah,7(%2)\n\t" \
573 : "=m"(*(n)) : "a" (addr), "r"(n), "ir"(limit), "i"(type))
575 void set_tss_desc(unsigned int n
, void *addr
)
577 _set_tssldt_desc(gdt_table
+__TSS(n
), (int)addr
, 235, 0x89);
580 void set_ldt_desc(unsigned int n
, void *addr
, unsigned int size
)
582 _set_tssldt_desc(gdt_table
+__LDT(n
), (int)addr
, ((size
<< 3)-1), 0x82);
585 #ifdef CONFIG_X86_VISWS_APIC
588 * On Rev 005 motherboards legacy device interrupt lines are wired directly
589 * to Lithium from the 307. But the PROM leaves the interrupt type of each
590 * 307 logical device set appropriate for the 8259. Later we'll actually use
591 * the 8259, but for now we have to flip the interrupt types to
592 * level triggered, active lo as required by Lithium.
595 #define REG 0x2e /* The register to read/write */
596 #define DEV 0x07 /* Register: Logical device select */
597 #define VAL 0x2f /* The value to read/write */
600 superio_outb(int dev
, int reg
, int val
)
608 static int __attribute__ ((unused
))
609 superio_inb(int dev
, int reg
)
617 #define FLOP 3 /* floppy logical device */
618 #define PPORT 4 /* parallel logical device */
619 #define UART5 5 /* uart2 logical device (not wired up) */
620 #define UART6 6 /* uart1 logical device (THIS is the serial port!) */
621 #define IDEST 0x70 /* int. destination (which 307 IRQ line) reg. */
622 #define ITYPE 0x71 /* interrupt type register */
624 /* interrupt type bits */
625 #define LEVEL 0x01 /* bit 0, 0 == edge triggered */
626 #define ACTHI 0x02 /* bit 1, 0 == active lo */
631 if (visws_board_type
== VISWS_320
&& visws_board_rev
== 5) {
632 superio_outb(UART6
, IDEST
, 0); /* 0 means no intr propagated */
633 printk("SGI 320 rev 5: disabling 307 uart1 interrupt\n");
640 set_fixmap(FIX_LI_PCIA
, LI_PCI_A_PHYS
);
641 printk("Lithium PCI Bridge A, Bus Number: %d\n",
642 li_pcia_read16(LI_PCI_BUSNUM
) & 0xff);
643 set_fixmap(FIX_LI_PCIB
, LI_PCI_B_PHYS
);
644 printk("Lithium PCI Bridge B (PIIX4), Bus Number: %d\n",
645 li_pcib_read16(LI_PCI_BUSNUM
) & 0xff);
647 /* XXX blindly enables all interrupts */
648 li_pcia_write16(LI_PCI_INTEN
, 0xffff);
649 li_pcib_write16(LI_PCI_INTEN
, 0xffff);
656 * On normal SMP PC this is used only with SMP, but we have to
657 * use it and set it up here to start the Cobalt clock
659 set_fixmap(FIX_APIC_BASE
, APIC_PHYS_BASE
);
660 printk("Local APIC ID %lx\n", apic_read(APIC_ID
));
661 printk("Local APIC Version %lx\n", apic_read(APIC_VERSION
));
663 set_fixmap(FIX_CO_CPU
, CO_CPU_PHYS
);
664 printk("Cobalt Revision %lx\n", co_cpu_read(CO_CPU_REV
));
666 set_fixmap(FIX_CO_APIC
, CO_APIC_PHYS
);
667 printk("Cobalt APIC ID %lx\n", co_apic_read(CO_APIC_ID
));
669 /* Enable Cobalt APIC being careful to NOT change the ID! */
670 co_apic_write(CO_APIC_ID
, co_apic_read(CO_APIC_ID
)|CO_APIC_ENABLE
);
672 printk("Cobalt APIC enabled: ID reg %lx\n", co_apic_read(CO_APIC_ID
));
675 void __init
trap_init(void)
677 if (readl(0x0FFFD9) == 'E' + ('I'<<8) + ('S'<<16) + ('A'<<24))
680 set_trap_gate(0,÷_error
);
681 set_trap_gate(1,&debug
);
682 set_trap_gate(2,&nmi
);
683 set_system_gate(3,&int3
); /* int3-5 can be called from all */
684 set_system_gate(4,&overflow
);
685 set_system_gate(5,&bounds
);
686 set_trap_gate(6,&invalid_op
);
687 set_trap_gate(7,&device_not_available
);
688 set_trap_gate(8,&double_fault
);
689 set_trap_gate(9,&coprocessor_segment_overrun
);
690 set_trap_gate(10,&invalid_TSS
);
691 set_trap_gate(11,&segment_not_present
);
692 set_trap_gate(12,&stack_segment
);
693 set_trap_gate(13,&general_protection
);
694 set_trap_gate(14,&page_fault
);
695 set_trap_gate(15,&spurious_interrupt_bug
);
696 set_trap_gate(16,&coprocessor_error
);
697 set_trap_gate(17,&alignment_check
);
698 set_system_gate(SYSCALL_VECTOR
,&system_call
);
701 * default LDT is a single-entry callgate to lcall7 for iBCS
702 * and a callgate to lcall27 for Solaris/x86 binaries
704 set_call_gate(&default_ldt
[0],lcall7
);
705 set_call_gate(&default_ldt
[4],lcall27
);
708 * on SMP we do not yet know which CPU is on which TSS,
709 * so we delay this until smp_init(). (the CPU is already
710 * in a reasonable state, otherwise we wouldnt have gotten so far :)
716 #ifdef CONFIG_X86_VISWS_APIC