root/kernel/traps.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. die_if_kernel
  2. do_double_fault
  3. do_general_protection
  4. do_alignment_check
  5. do_divide_error
  6. do_int3
  7. do_nmi
  8. do_debug
  9. do_overflow
  10. do_bounds
  11. do_invalid_op
  12. do_device_not_available
  13. do_coprocessor_segment_overrun
  14. do_invalid_TSS
  15. do_segment_not_present
  16. do_stack_segment
  17. math_error
  18. do_coprocessor_error
  19. do_reserved
  20. trap_init

   1 /*
   2  *  linux/kernel/traps.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 /*
   8  * 'Traps.c' handles hardware traps and faults after we have saved some
   9  * state in 'asm.s'. Currently mostly a debugging-aid, will be extended
  10  * to mainly kill the offending process (probably by giving it a signal,
  11  * but possibly by killing it outright if necessary).
  12  */
  13 #include <linux/head.h>
  14 #include <linux/sched.h>
  15 #include <linux/kernel.h>
  16 #include <linux/string.h>
  17 #include <linux/errno.h>
  18 #include <linux/segment.h>
  19 #include <linux/ptrace.h>
  20 
  21 #include <asm/system.h>
  22 #include <asm/segment.h>
  23 #include <asm/io.h>
  24 
  25 #define get_seg_byte(seg,addr) ({ \
  26 register char __res; \
  27 __asm__("push %%fs;mov %%ax,%%fs;movb %%fs:%2,%%al;pop %%fs" \
  28         :"=a" (__res):"0" (seg),"m" (*(addr))); \
  29 __res;})
  30 
  31 #define get_seg_long(seg,addr) ({ \
  32 register unsigned long __res; \
  33 __asm__("push %%fs;mov %%ax,%%fs;movl %%fs:%2,%%eax;pop %%fs" \
  34         :"=a" (__res):"0" (seg),"m" (*(addr))); \
  35 __res;})
  36 
  37 #define _fs() ({ \
  38 register unsigned short __res; \
  39 __asm__("mov %%fs,%%ax":"=a" (__res):); \
  40 __res;})
  41 
  42 void page_exception(void);
  43 
  44 asmlinkage void divide_error(void);
  45 asmlinkage void debug(void);
  46 asmlinkage void nmi(void);
  47 asmlinkage void int3(void);
  48 asmlinkage void overflow(void);
  49 asmlinkage void bounds(void);
  50 asmlinkage void invalid_op(void);
  51 asmlinkage void device_not_available(void);
  52 asmlinkage void double_fault(void);
  53 asmlinkage void coprocessor_segment_overrun(void);
  54 asmlinkage void invalid_TSS(void);
  55 asmlinkage void segment_not_present(void);
  56 asmlinkage void stack_segment(void);
  57 asmlinkage void general_protection(void);
  58 asmlinkage void page_fault(void);
  59 asmlinkage void coprocessor_error(void);
  60 asmlinkage void reserved(void);
  61 asmlinkage void alignment_check(void);
  62 
  63 /*static*/ void die_if_kernel(char * str, struct pt_regs * regs, long err)
     /* [previous][next][first][last][top][bottom][index][help] */
  64 {
  65         int i;
  66 
  67         if ((regs->eflags & VM_MASK) || (3 & regs->cs) == 3)
  68                 return;
  69 
  70         printk("%s: %04x\n", str, err & 0xffff);
  71         printk("EIP:    %04x:%08x\nEFLAGS: %08x\n", 0xffff & regs->cs,regs->eip,regs->eflags);
  72         printk("eax: %08x   ebx: %08x   ecx: %08x   edx: %08x\n",
  73                 regs->eax, regs->ebx, regs->ecx, regs->edx);
  74         printk("esi: %08x   edi: %08x   ebp: %08x\n",
  75                 regs->esi, regs->edi, regs->ebp);
  76         printk("ds: %04x   es: %04x   fs: %04x   gs: %04x\n",
  77                 regs->ds, regs->es, regs->fs, regs->gs);
  78         store_TR(i);
  79         printk("Pid: %d, process nr: %d\n", current->pid, 0xffff & i);
  80         for(i=0;i<10;i++)
  81                 printk("%02x ",0xff & get_seg_byte(regs->cs,(i+(char *)regs->eip)));
  82         printk("\n");
  83         do_exit(SIGSEGV);
  84 }
  85 
  86 asmlinkage void do_double_fault(struct pt_regs * regs, long error_code)
     /* [previous][next][first][last][top][bottom][index][help] */
  87 {
  88         send_sig(SIGSEGV, current, 1);
  89         die_if_kernel("double fault",regs,error_code);
  90 }
  91 
  92 asmlinkage void do_general_protection(struct pt_regs * regs, long error_code)
     /* [previous][next][first][last][top][bottom][index][help] */
  93 {
  94         send_sig(SIGSEGV, current, 1);
  95         die_if_kernel("general protection",regs,error_code);
  96 }
  97 
  98 asmlinkage void do_alignment_check(struct pt_regs * regs, long error_code)
     /* [previous][next][first][last][top][bottom][index][help] */
  99 {
 100         send_sig(SIGSEGV, current, 1);
 101         die_if_kernel("alignment check",regs,error_code);
 102 }
 103 
 104 asmlinkage void do_divide_error(struct pt_regs * regs, long error_code)
     /* [previous][next][first][last][top][bottom][index][help] */
 105 {
 106         send_sig(SIGFPE, current, 1);
 107         die_if_kernel("divide error",regs,error_code);
 108 }
 109 
 110 asmlinkage void do_int3(struct pt_regs * regs, long error_code)
     /* [previous][next][first][last][top][bottom][index][help] */
 111 {
 112         if (current->flags & PF_PTRACED)
 113                 current->blocked &= ~(1 << (SIGTRAP-1));
 114         send_sig(SIGTRAP, current, 1);
 115         die_if_kernel("int3",regs,error_code);
 116 }
 117 
 118 asmlinkage void do_nmi(struct pt_regs * regs, long error_code)
     /* [previous][next][first][last][top][bottom][index][help] */
 119 {
 120         printk("Uhhuh. NMI received. Dazed and confused, but trying to continue\n");
 121 }
 122 
 123 asmlinkage void do_debug(struct pt_regs * regs, long error_code)
     /* [previous][next][first][last][top][bottom][index][help] */
 124 {
 125         if (current->flags & PF_PTRACED)
 126                 current->blocked &= ~(1 << (SIGTRAP-1));
 127         send_sig(SIGTRAP, current, 1);
 128         die_if_kernel("debug",regs,error_code);
 129 }
 130 
 131 asmlinkage void do_overflow(struct pt_regs * regs, long error_code)
     /* [previous][next][first][last][top][bottom][index][help] */
 132 {
 133         send_sig(SIGSEGV, current, 1);
 134         die_if_kernel("overflow",regs,error_code);
 135 }
 136 
 137 asmlinkage void do_bounds(struct pt_regs * regs, long error_code)
     /* [previous][next][first][last][top][bottom][index][help] */
 138 {
 139         send_sig(SIGSEGV, current, 1);
 140         die_if_kernel("bounds",regs,error_code);
 141 }
 142 
 143 asmlinkage void do_invalid_op(struct pt_regs * regs, long error_code)
     /* [previous][next][first][last][top][bottom][index][help] */
 144 {
 145         send_sig(SIGILL, current, 1);
 146         die_if_kernel("invalid operand",regs,error_code);
 147 }
 148 
 149 asmlinkage void do_device_not_available(struct pt_regs * regs, long error_code)
     /* [previous][next][first][last][top][bottom][index][help] */
 150 {
 151         send_sig(SIGSEGV, current, 1);
 152         die_if_kernel("device not available",regs,error_code);
 153 }
 154 
 155 asmlinkage void do_coprocessor_segment_overrun(struct pt_regs * regs, long error_code)
     /* [previous][next][first][last][top][bottom][index][help] */
 156 {
 157         send_sig(SIGFPE, last_task_used_math, 1);
 158         die_if_kernel("coprocessor segment overrun",regs,error_code);
 159 }
 160 
 161 asmlinkage void do_invalid_TSS(struct pt_regs * regs,long error_code)
     /* [previous][next][first][last][top][bottom][index][help] */
 162 {
 163         send_sig(SIGSEGV, current, 1);
 164         die_if_kernel("invalid TSS",regs,error_code);
 165 }
 166 
 167 asmlinkage void do_segment_not_present(struct pt_regs * regs,long error_code)
     /* [previous][next][first][last][top][bottom][index][help] */
 168 {
 169         send_sig(SIGSEGV, current, 1);
 170         die_if_kernel("segment not present",regs,error_code);
 171 }
 172 
 173 asmlinkage void do_stack_segment(struct pt_regs * regs,long error_code)
     /* [previous][next][first][last][top][bottom][index][help] */
 174 {
 175         send_sig(SIGSEGV, current, 1);
 176         die_if_kernel("stack segment",regs,error_code);
 177 }
 178 
 179 /*
 180  * Allow the process which triggered the interrupt to recover the error
 181  * condition.
 182  *  - the status word is saved in the cs selector.
 183  *  - the tag word is saved in the operand selector.
 184  *  - the status word is then cleared and the tags all set to Empty.
 185  *
 186  * This will give sufficient information for complete recovery provided that
 187  * the affected process knows or can deduce the code and data segments
 188  * which were in force when the exception condition arose.
 189  *
 190  * Note that we play around with the 'TS' bit to hopefully get
 191  * the correct behaviour even in the presense of the asynchronous
 192  * IRQ13 behaviour
 193  */
 194 void math_error(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 195 {
 196         struct i387_hard_struct * env;
 197 
 198         clts();
 199         if (!last_task_used_math) {
 200                 __asm__("fnclex");
 201                 return;
 202         }
 203         env = &last_task_used_math->tss.i387.hard;
 204         send_sig(SIGFPE, last_task_used_math, 1);
 205         __asm__ __volatile__("fnsave %0":"=m" (*env));
 206         last_task_used_math = NULL;
 207         stts();
 208         env->fcs = (env->swd & 0x0000ffff) | (env->fcs & 0xffff0000);
 209         env->fos = env->twd;
 210         env->swd &= 0xffff0000;
 211         env->twd = 0xffffffff;
 212 }
 213 
 214 asmlinkage void do_coprocessor_error(struct pt_regs * regs, long error_code)
     /* [previous][next][first][last][top][bottom][index][help] */
 215 {
 216         ignore_irq13 = 1;
 217         math_error();
 218 }
 219 
 220 asmlinkage void do_reserved(struct pt_regs * regs, long error_code)
     /* [previous][next][first][last][top][bottom][index][help] */
 221 {
 222         send_sig(SIGSEGV, current, 1);
 223         die_if_kernel("reserved (15,17-47) error",regs,error_code);
 224 }
 225 
 226 void trap_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 227 {
 228         int i;
 229 
 230         set_trap_gate(0,&divide_error);
 231         set_trap_gate(1,&debug);
 232         set_trap_gate(2,&nmi);
 233         set_system_gate(3,&int3);       /* int3-5 can be called from all */
 234         set_system_gate(4,&overflow);
 235         set_system_gate(5,&bounds);
 236         set_trap_gate(6,&invalid_op);
 237         set_trap_gate(7,&device_not_available);
 238         set_trap_gate(8,&double_fault);
 239         set_trap_gate(9,&coprocessor_segment_overrun);
 240         set_trap_gate(10,&invalid_TSS);
 241         set_trap_gate(11,&segment_not_present);
 242         set_trap_gate(12,&stack_segment);
 243         set_trap_gate(13,&general_protection);
 244         set_trap_gate(14,&page_fault);
 245         set_trap_gate(15,&reserved);
 246         set_trap_gate(16,&coprocessor_error);
 247         set_trap_gate(17,&alignment_check);
 248         for (i=18;i<48;i++)
 249                 set_trap_gate(i,&reserved);
 250 }

/* [previous][next][first][last][top][bottom][index][help] */