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

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