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

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