root/kernel/traps.c

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

DEFINITIONS

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

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