root/arch/i386/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. math_state_restore
  9. math_emulate
  10. trap_init

   1 /*
   2  *  linux/arch/i386/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 #include <linux/config.h>
  20 #include <linux/timer.h>
  21 
  22 #include <asm/system.h>
  23 #include <asm/segment.h>
  24 #include <asm/io.h>
  25 
  26 asmlinkage int system_call(void);
  27 asmlinkage void lcall7(void);
  28 struct desc_struct default_ldt;
  29 
  30 static inline void console_verbose(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  31 {
  32         extern int console_loglevel;
  33         console_loglevel = 15;
  34 }
  35 
  36 #define DO_ERROR(trapnr, signr, str, name, tsk) \
  37 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
  38 { \
  39         tsk->tss.error_code = error_code; \
  40         tsk->tss.trap_no = trapnr; \
  41         if (signr == SIGTRAP && current->flags & PF_PTRACED) \
  42                 current->blocked &= ~(1 << (SIGTRAP-1)); \
  43         send_sig(signr, tsk, 1); \
  44         die_if_kernel(str,regs,error_code); \
  45 }
  46 
  47 #define get_seg_byte(seg,addr) ({ \
  48 register unsigned char __res; \
  49 __asm__("push %%fs;mov %%ax,%%fs;movb %%fs:%2,%%al;pop %%fs" \
  50         :"=a" (__res):"0" (seg),"m" (*(addr))); \
  51 __res;})
  52 
  53 #define get_seg_long(seg,addr) ({ \
  54 register unsigned long __res; \
  55 __asm__("push %%fs;mov %%ax,%%fs;movl %%fs:%2,%%eax;pop %%fs" \
  56         :"=a" (__res):"0" (seg),"m" (*(addr))); \
  57 __res;})
  58 
  59 #define _fs() ({ \
  60 register unsigned short __res; \
  61 __asm__("mov %%fs,%%ax":"=a" (__res):); \
  62 __res;})
  63 
  64 void page_exception(void);
  65 
  66 asmlinkage void divide_error(void);
  67 asmlinkage void debug(void);
  68 asmlinkage void nmi(void);
  69 asmlinkage void int3(void);
  70 asmlinkage void overflow(void);
  71 asmlinkage void bounds(void);
  72 asmlinkage void invalid_op(void);
  73 asmlinkage void device_not_available(void);
  74 asmlinkage void double_fault(void);
  75 asmlinkage void coprocessor_segment_overrun(void);
  76 asmlinkage void invalid_TSS(void);
  77 asmlinkage void segment_not_present(void);
  78 asmlinkage void stack_segment(void);
  79 asmlinkage void general_protection(void);
  80 asmlinkage void page_fault(void);
  81 asmlinkage void coprocessor_error(void);
  82 asmlinkage void reserved(void);
  83 asmlinkage void alignment_check(void);
  84 
  85 /*static*/ void die_if_kernel(char * str, struct pt_regs * regs, long err)
     /* [previous][next][first][last][top][bottom][index][help] */
  86 {
  87         int i;
  88         unsigned long esp;
  89         unsigned short ss;
  90 
  91         esp = (unsigned long) &regs->esp;
  92         ss = KERNEL_DS;
  93         if ((regs->eflags & VM_MASK) || (3 & regs->cs) == 3)
  94                 return;
  95         if (regs->cs & 3) {
  96                 esp = regs->esp;
  97                 ss = regs->ss;
  98         }
  99         console_verbose();
 100         printk("%s: %04lx\n", str, err & 0xffff);
 101         printk("EIP:    %04x:%08lx\nEFLAGS: %08lx\n", 0xffff & regs->cs,regs->eip,regs->eflags);
 102         printk("eax: %08lx   ebx: %08lx   ecx: %08lx   edx: %08lx\n",
 103                 regs->eax, regs->ebx, regs->ecx, regs->edx);
 104         printk("esi: %08lx   edi: %08lx   ebp: %08lx   esp: %08lx\n",
 105                 regs->esi, regs->edi, regs->ebp, esp);
 106         printk("ds: %04x   es: %04x   fs: %04x   gs: %04x   ss: %04x\n",
 107                 regs->ds, regs->es, regs->fs, regs->gs, ss);
 108         store_TR(i);
 109         if (STACK_MAGIC != *(unsigned long *)current->kernel_stack_page)
 110                 printk("Corrupted stack page\n");
 111         printk("Process %s (pid: %d, process nr: %d, stackpage=%08lx)\nStack: ",
 112                 current->comm, current->pid, 0xffff & i, current->kernel_stack_page);
 113         for(i=0;i<5;i++)
 114                 printk("%08lx ", get_seg_long(ss,(i+(unsigned long *)esp)));
 115         printk("\nCode: ");
 116         for(i=0;i<20;i++)
 117                 printk("%02x ",0xff & get_seg_byte(regs->cs,(i+(char *)regs->eip)));
 118         printk("\n");
 119         do_exit(SIGSEGV);
 120 }
 121 
 122 DO_ERROR( 0, SIGFPE,  "divide error", divide_error, current)
     /* [previous][next][first][last][top][bottom][index][help] */
 123 DO_ERROR( 3, SIGTRAP, "int3", int3, current)
 124 DO_ERROR( 4, SIGSEGV, "overflow", overflow, current)
 125 DO_ERROR( 5, SIGSEGV, "bounds", bounds, current)
 126 DO_ERROR( 6, SIGILL,  "invalid operand", invalid_op, current)
 127 DO_ERROR( 7, SIGSEGV, "device not available", device_not_available, current)
 128 DO_ERROR( 8, SIGSEGV, "double fault", double_fault, current)
 129 DO_ERROR( 9, SIGFPE,  "coprocessor segment overrun", coprocessor_segment_overrun, last_task_used_math)
 130 DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS, current)
 131 DO_ERROR(11, SIGBUS,  "segment not present", segment_not_present, current)
 132 DO_ERROR(12, SIGBUS,  "stack segment", stack_segment, current)
 133 DO_ERROR(15, SIGSEGV, "reserved", reserved, current)
 134 DO_ERROR(17, SIGSEGV, "alignment check", alignment_check, current)
 135 
 136 asmlinkage void do_general_protection(struct pt_regs * regs, long error_code)
 137 {
 138         int signr = SIGSEGV;
 139 
 140         if (regs->eflags & VM_MASK) {
 141                 handle_vm86_fault((struct vm86_regs *) regs, error_code);
 142                 return;
 143         }
 144         die_if_kernel("general protection",regs,error_code);
 145         switch (get_seg_byte(regs->cs, (char *)regs->eip)) {
 146                 case 0xCD: /* INT */
 147                 case 0xF4: /* HLT */
 148                 case 0xFA: /* CLI */
 149                 case 0xFB: /* STI */
 150                         signr = SIGILL;
 151         }
 152         current->tss.error_code = error_code;
 153         current->tss.trap_no = 13;
 154         send_sig(signr, current, 1);    
 155 }
 156 
 157 asmlinkage void do_nmi(struct pt_regs * regs, long error_code)
     /* [previous][next][first][last][top][bottom][index][help] */
 158 {
 159         printk("Uhhuh. NMI received. Dazed and confused, but trying to continue\n");
 160         printk("You probably have a hardware problem with your RAM chips\n");
 161 }
 162 
 163 asmlinkage void do_debug(struct pt_regs * regs, long error_code)
     /* [previous][next][first][last][top][bottom][index][help] */
 164 {
 165         if (regs->eflags & VM_MASK) {
 166                 handle_vm86_debug((struct vm86_regs *) regs, error_code);
 167                 return;
 168         }
 169         if (current->flags & PF_PTRACED)
 170                 current->blocked &= ~(1 << (SIGTRAP-1));
 171         send_sig(SIGTRAP, current, 1);
 172         current->tss.trap_no = 1;
 173         current->tss.error_code = error_code;
 174         if ((regs->cs & 3) == 0) {
 175                 /* If this is a kernel mode trap, then reset db7 and allow us to continue */
 176                 __asm__("movl %0,%%db7"
 177                         : /* no output */
 178                         : "r" (0));
 179                 return;
 180         }
 181         die_if_kernel("debug",regs,error_code);
 182 }
 183 
 184 /*
 185  * Allow the process which triggered the interrupt to recover the error
 186  * condition.
 187  *  - the status word is saved in the cs selector.
 188  *  - the tag word is saved in the operand selector.
 189  *  - the status word is then cleared and the tags all set to Empty.
 190  *
 191  * This will give sufficient information for complete recovery provided that
 192  * the affected process knows or can deduce the code and data segments
 193  * which were in force when the exception condition arose.
 194  *
 195  * Note that we play around with the 'TS' bit to hopefully get
 196  * the correct behaviour even in the presence of the asynchronous
 197  * IRQ13 behaviour
 198  */
 199 void math_error(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 200 {
 201         struct i387_hard_struct * env;
 202 
 203         clts();
 204         if (!last_task_used_math) {
 205                 __asm__("fnclex");
 206                 return;
 207         }
 208         env = &last_task_used_math->tss.i387.hard;
 209         send_sig(SIGFPE, last_task_used_math, 1);
 210         last_task_used_math->tss.trap_no = 16;
 211         last_task_used_math->tss.error_code = 0;
 212         __asm__ __volatile__("fnsave %0":"=m" (*env));
 213         last_task_used_math = NULL;
 214         stts();
 215         env->fcs = (env->swd & 0x0000ffff) | (env->fcs & 0xffff0000);
 216         env->fos = env->twd;
 217         env->swd &= 0xffff3800;
 218         env->twd = 0xffffffff;
 219 }
 220 
 221 asmlinkage void do_coprocessor_error(struct pt_regs * regs, long error_code)
     /* [previous][next][first][last][top][bottom][index][help] */
 222 {
 223         ignore_irq13 = 1;
 224         math_error();
 225 }
 226 
 227 /*
 228  *  'math_state_restore()' saves the current math information in the
 229  * old math state array, and gets the new ones from the current task
 230  *
 231  * Careful.. There are problems with IBM-designed IRQ13 behaviour.
 232  * Don't touch unless you *really* know how it works.
 233  */
 234 asmlinkage void math_state_restore(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 235 {
 236         __asm__ __volatile__("clts");
 237         if (last_task_used_math == current)
 238                 return;
 239         timer_table[COPRO_TIMER].expires = jiffies+50;
 240         timer_active |= 1<<COPRO_TIMER; 
 241         if (last_task_used_math)
 242                 __asm__("fnsave %0":"=m" (last_task_used_math->tss.i387));
 243         else
 244                 __asm__("fnclex");
 245         last_task_used_math = current;
 246         if (current->used_math) {
 247                 __asm__("frstor %0": :"m" (current->tss.i387));
 248         } else {
 249                 __asm__("fninit");
 250                 current->used_math=1;
 251         }
 252         timer_active &= ~(1<<COPRO_TIMER);
 253 }
 254 
 255 #ifndef CONFIG_MATH_EMULATION
 256 
 257 asmlinkage void math_emulate(long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 258 {
 259   printk("math-emulation not enabled and no coprocessor found.\n");
 260   printk("killing %s.\n",current->comm);
 261   send_sig(SIGFPE,current,1);
 262   schedule();
 263 }
 264 
 265 #endif /* CONFIG_MATH_EMULATION */
 266 
 267 void trap_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 268 {
 269         int i;
 270         struct desc_struct * p;
 271 
 272         set_call_gate(&default_ldt,lcall7);
 273         set_trap_gate(0,&divide_error);
 274         set_trap_gate(1,&debug);
 275         set_trap_gate(2,&nmi);
 276         set_system_gate(3,&int3);       /* int3-5 can be called from all */
 277         set_system_gate(4,&overflow);
 278         set_system_gate(5,&bounds);
 279         set_trap_gate(6,&invalid_op);
 280         set_trap_gate(7,&device_not_available);
 281         set_trap_gate(8,&double_fault);
 282         set_trap_gate(9,&coprocessor_segment_overrun);
 283         set_trap_gate(10,&invalid_TSS);
 284         set_trap_gate(11,&segment_not_present);
 285         set_trap_gate(12,&stack_segment);
 286         set_trap_gate(13,&general_protection);
 287         set_trap_gate(14,&page_fault);
 288         set_trap_gate(15,&reserved);
 289         set_trap_gate(16,&coprocessor_error);
 290         set_trap_gate(17,&alignment_check);
 291         for (i=18;i<48;i++)
 292                 set_trap_gate(i,&reserved);
 293         set_system_gate(0x80,&system_call);
 294 /* set up GDT task & ldt entries */
 295         p = gdt+FIRST_TSS_ENTRY;
 296         set_tss_desc(p, &init_task.tss);
 297         p++;
 298         set_ldt_desc(p, &default_ldt, 1);
 299         p++;
 300         for(i=1 ; i<NR_TASKS ; i++) {
 301                 p->a=p->b=0;
 302                 p++;
 303                 p->a=p->b=0;
 304                 p++;
 305         }
 306 /* Clear NT, so that we won't have troubles with that later on */
 307         __asm__("pushfl ; andl $0xffffbfff,(%esp) ; popfl");
 308         load_TR(0);
 309         load_ldt(0);
 310 }

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