root/arch/i386/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. 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/config.h>
  14 #include <linux/head.h>
  15 #include <linux/sched.h>
  16 #include <linux/kernel.h>
  17 #include <linux/string.h>
  18 #include <linux/errno.h>
  19 #include <linux/ptrace.h>
  20 #include <linux/config.h>
  21 #include <linux/timer.h>
  22 #include <linux/mm.h>
  23 
  24 #include <asm/system.h>
  25 #include <asm/segment.h>
  26 #include <asm/io.h>
  27 
  28 asmlinkage int system_call(void);
  29 asmlinkage void lcall7(void);
  30 struct desc_struct default_ldt = { 0, 0 };
  31 
  32 static inline void console_verbose(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  33 {
  34         extern int console_loglevel;
  35         console_loglevel = 15;
  36 }
  37 
  38 #define DO_ERROR(trapnr, signr, str, name, tsk) \
  39 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
  40 { \
  41         tsk->tss.error_code = error_code; \
  42         tsk->tss.trap_no = trapnr; \
  43         if (signr == SIGTRAP && current->flags & PF_PTRACED) \
  44                 current->blocked &= ~(1 << (SIGTRAP-1)); \
  45         send_sig(signr, tsk, 1); \
  46         die_if_kernel(str,regs,error_code); \
  47 }
  48 
  49 #define get_seg_byte(seg,addr) ({ \
  50 register unsigned char __res; \
  51 __asm__("push %%fs;mov %%ax,%%fs;movb %%fs:%2,%%al;pop %%fs" \
  52         :"=a" (__res):"0" (seg),"m" (*(addr))); \
  53 __res;})
  54 
  55 #define get_seg_long(seg,addr) ({ \
  56 register unsigned long __res; \
  57 __asm__("push %%fs;mov %%ax,%%fs;movl %%fs:%2,%%eax;pop %%fs" \
  58         :"=a" (__res):"0" (seg),"m" (*(addr))); \
  59 __res;})
  60 
  61 #define _fs() ({ \
  62 register unsigned short __res; \
  63 __asm__("mov %%fs,%%ax":"=a" (__res):); \
  64 __res;})
  65 
  66 void page_exception(void);
  67 
  68 asmlinkage void divide_error(void);
  69 asmlinkage void debug(void);
  70 asmlinkage void nmi(void);
  71 asmlinkage void int3(void);
  72 asmlinkage void overflow(void);
  73 asmlinkage void bounds(void);
  74 asmlinkage void invalid_op(void);
  75 asmlinkage void device_not_available(void);
  76 asmlinkage void double_fault(void);
  77 asmlinkage void coprocessor_segment_overrun(void);
  78 asmlinkage void invalid_TSS(void);
  79 asmlinkage void segment_not_present(void);
  80 asmlinkage void stack_segment(void);
  81 asmlinkage void general_protection(void);
  82 asmlinkage void page_fault(void);
  83 asmlinkage void coprocessor_error(void);
  84 asmlinkage void reserved(void);
  85 asmlinkage void alignment_check(void);
  86 
  87 int kstack_depth_to_print = 24;
  88 
  89 /*
  90  * These constants are for searching for possible module text
  91  * segments.  VMALLOC_OFFSET comes from mm/vmalloc.c; MODULE_RANGE is
  92  * a guess of how much space is likely to be vmalloced.
  93  */
  94 #define VMALLOC_OFFSET (8*1024*1024)
  95 #define MODULE_RANGE (8*1024*1024)
  96 
  97 /*static*/ void die_if_kernel(const char * str, struct pt_regs * regs, long err)
     /* [previous][next][first][last][top][bottom][index][help] */
  98 {
  99         int i;
 100         unsigned long esp;
 101         unsigned short ss;
 102         unsigned long *stack, addr, module_start, module_end;
 103         extern char start_kernel, _etext;
 104 
 105         esp = (unsigned long) &regs->esp;
 106         ss = KERNEL_DS;
 107         if ((regs->eflags & VM_MASK) || (3 & regs->cs) == 3)
 108                 return;
 109         if (regs->cs & 3) {
 110                 esp = regs->esp;
 111                 ss = regs->ss;
 112         }
 113         console_verbose();
 114         printk("%s: %04lx\n", str, err & 0xffff);
 115         printk("CPU:    %d\n", smp_processor_id());
 116         printk("EIP:    %04x:[<%08lx>]\nEFLAGS: %08lx\n", 0xffff & regs->cs,regs->eip,regs->eflags);
 117         printk("eax: %08lx   ebx: %08lx   ecx: %08lx   edx: %08lx\n",
 118                 regs->eax, regs->ebx, regs->ecx, regs->edx);
 119         printk("esi: %08lx   edi: %08lx   ebp: %08lx   esp: %08lx\n",
 120                 regs->esi, regs->edi, regs->ebp, esp);
 121         printk("ds: %04x   es: %04x   fs: %04x   gs: %04x   ss: %04x\n",
 122                 regs->ds, regs->es, regs->fs, regs->gs, ss);
 123         store_TR(i);
 124         if (STACK_MAGIC != *(unsigned long *)current->kernel_stack_page)
 125                 printk("Corrupted stack page\n");
 126         printk("Process %s (pid: %d, process nr: %d, stackpage=%08lx)\nStack: ",
 127                 current->comm, current->pid, 0xffff & i, current->kernel_stack_page);
 128         stack = (unsigned long *) esp;
 129         for(i=0; i < kstack_depth_to_print; i++) {
 130                 if (((long) stack & 4095) == 0)
 131                         break;
 132                 if (i && ((i % 8) == 0))
 133                         printk("\n       ");
 134                 printk("%08lx ", get_seg_long(ss,stack++));
 135         }
 136         printk("\nCall Trace: ");
 137         stack = (unsigned long *) esp;
 138         i = 1;
 139         module_start = ((high_memory + VMALLOC_OFFSET) & ~(VMALLOC_OFFSET-1));
 140         module_end = module_start + MODULE_RANGE;
 141         while (((long) stack & 4095) != 0) {
 142                 addr = get_seg_long(ss, stack++);
 143                 /*
 144                  * If the address is either in the text segment of the
 145                  * kernel, or in the region which contains vmalloc'ed
 146                  * memory, it *may* be the address of a calling
 147                  * routine; if so, print it so that someone tracing
 148                  * down the cause of the crash will be able to figure
 149                  * out the call path that was taken.
 150                  */
 151                 if (((addr >= (unsigned long) &start_kernel) &&
 152                      (addr <= (unsigned long) &_etext)) ||
 153                     ((addr >= module_start) && (addr <= module_end))) {
 154                         if (i && ((i % 8) == 0))
 155                                 printk("\n       ");
 156                         printk("[<%08lx>] ", addr);
 157                         i++;
 158                 }
 159         }
 160         printk("\nCode: ");
 161         for(i=0;i<20;i++)
 162                 printk("%02x ",0xff & get_seg_byte(regs->cs,(i+(char *)regs->eip)));
 163         printk("\n");
 164         do_exit(SIGSEGV);
 165 }
 166 
 167 DO_ERROR( 0, SIGFPE,  "divide error", divide_error, current)
     /* [previous][next][first][last][top][bottom][index][help] */
 168 DO_ERROR( 3, SIGTRAP, "int3", int3, current)
 169 DO_ERROR( 4, SIGSEGV, "overflow", overflow, current)
 170 DO_ERROR( 5, SIGSEGV, "bounds", bounds, current)
 171 DO_ERROR( 6, SIGILL,  "invalid operand", invalid_op, current)
 172 DO_ERROR( 7, SIGSEGV, "device not available", device_not_available, current)
 173 DO_ERROR( 8, SIGSEGV, "double fault", double_fault, current)
 174 DO_ERROR( 9, SIGFPE,  "coprocessor segment overrun", coprocessor_segment_overrun, last_task_used_math)
 175 DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS, current)
 176 DO_ERROR(11, SIGBUS,  "segment not present", segment_not_present, current)
 177 DO_ERROR(12, SIGBUS,  "stack segment", stack_segment, current)
 178 DO_ERROR(15, SIGSEGV, "reserved", reserved, current)
 179 DO_ERROR(17, SIGSEGV, "alignment check", alignment_check, current)
 180 
 181 asmlinkage void do_general_protection(struct pt_regs * regs, long error_code)
 182 {
 183         if (regs->eflags & VM_MASK) {
 184                 handle_vm86_fault((struct vm86_regs *) regs, error_code);
 185                 return;
 186         }
 187         die_if_kernel("general protection",regs,error_code);
 188         current->tss.error_code = error_code;
 189         current->tss.trap_no = 13;
 190         send_sig(SIGSEGV, current, 1);  
 191 }
 192 
 193 asmlinkage void do_nmi(struct pt_regs * regs, long error_code)
     /* [previous][next][first][last][top][bottom][index][help] */
 194 {
 195 #ifdef CONFIG_SMP_NMI_INVAL
 196         smp_flush_tlb_rcv();
 197 #else
 198 #ifndef CONFIG_IGNORE_NMI
 199         printk("Uhhuh. NMI received. Dazed and confused, but trying to continue\n");
 200         printk("You probably have a hardware problem with your RAM chips or a\n");
 201         printk("power saving mode enabled.\n");
 202 #endif  
 203 #endif
 204 }
 205 
 206 asmlinkage void do_debug(struct pt_regs * regs, long error_code)
     /* [previous][next][first][last][top][bottom][index][help] */
 207 {
 208         if (regs->eflags & VM_MASK) {
 209                 handle_vm86_debug((struct vm86_regs *) regs, error_code);
 210                 return;
 211         }
 212         if (current->flags & PF_PTRACED)
 213                 current->blocked &= ~(1 << (SIGTRAP-1));
 214         send_sig(SIGTRAP, current, 1);
 215         current->tss.trap_no = 1;
 216         current->tss.error_code = error_code;
 217         if ((regs->cs & 3) == 0) {
 218                 /* If this is a kernel mode trap, then reset db7 and allow us to continue */
 219                 __asm__("movl %0,%%db7"
 220                         : /* no output */
 221                         : "r" (0));
 222                 return;
 223         }
 224         die_if_kernel("debug",regs,error_code);
 225 }
 226 
 227 /*
 228  * Allow the process which triggered the interrupt to recover the error
 229  * condition.
 230  *  - the status word is saved in the cs selector.
 231  *  - the tag word is saved in the operand selector.
 232  *  - the status word is then cleared and the tags all set to Empty.
 233  *
 234  * This will give sufficient information for complete recovery provided that
 235  * the affected process knows or can deduce the code and data segments
 236  * which were in force when the exception condition arose.
 237  *
 238  * Note that we play around with the 'TS' bit to hopefully get
 239  * the correct behaviour even in the presence of the asynchronous
 240  * IRQ13 behaviour
 241  */
 242 void math_error(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 243 {
 244         struct i387_hard_struct * env;
 245 #ifdef __SMP__
 246         env=&current->tss.i387.hard;
 247         send_sig(SIGFPE, current, 1);
 248         /*
 249          *      Save the info for the exception handler
 250          */
 251         __asm__ __volatile__("fnsave %0":"=m" (*env));
 252         current->flags&=~PF_USEDFPU;
 253         /*
 254          *      Cause a trap if they use the FPU again.
 255          */
 256         stts();
 257 #else
 258         clts();
 259         if (!last_task_used_math) {
 260                 __asm__("fnclex");
 261                 return;
 262         }
 263         env = &last_task_used_math->tss.i387.hard;
 264         send_sig(SIGFPE, last_task_used_math, 1);
 265         last_task_used_math->tss.trap_no = 16;
 266         last_task_used_math->tss.error_code = 0;
 267         __asm__ __volatile__("fnsave %0":"=m" (*env));
 268         last_task_used_math = NULL;
 269         stts();
 270         env->fcs = (env->swd & 0x0000ffff) | (env->fcs & 0xffff0000);
 271         env->fos = env->twd;
 272         env->swd &= 0xffff3800;
 273         env->twd = 0xffffffff;
 274 #endif  
 275 }
 276 
 277 asmlinkage void do_coprocessor_error(struct pt_regs * regs, long error_code)
     /* [previous][next][first][last][top][bottom][index][help] */
 278 {
 279         ignore_irq13 = 1;
 280         math_error();
 281 }
 282 
 283 /*
 284  *  'math_state_restore()' saves the current math information in the
 285  * old math state array, and gets the new ones from the current task
 286  *
 287  * Careful.. There are problems with IBM-designed IRQ13 behaviour.
 288  * Don't touch unless you *really* know how it works.
 289  */
 290 asmlinkage void math_state_restore(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 291 {
 292 #ifdef __SMP__
 293 /*
 294  *      SMP is actually simpler than uniprocessor for once. Because
 295  *      we can't pull the delayed FPU switching trick Linus does
 296  *      we simply have to do the restore each context switch and
 297  *      set the flag. switch_to() will always save the state in
 298  *      case we swap processors. We also don't use the coprocessor
 299  *      timer - IRQ 13 mode isnt used with SMP machines (thank god).
 300  *
 301  *      If this actually works it will be a miracle however
 302  */
 303         __asm__ __volatile__("clts");           /* Allow maths ops (or we recurse) */
 304         if(current->used_math)
 305                 __asm__("frstor %0": :"m" (current->tss.i387));
 306         else
 307         {
 308                 /*
 309                  *      Our first FPU usage, clean the chip.
 310                  */
 311                 __asm__("fninit");
 312                 current->used_math = 1;
 313         }
 314         current->flags|=PF_USEDFPU;             /* So we fnsave on switch_to() */
 315 #else
 316         __asm__ __volatile__("clts");
 317         if (last_task_used_math == current)
 318                 return;
 319         timer_table[COPRO_TIMER].expires = jiffies+50;
 320         timer_active |= 1<<COPRO_TIMER; 
 321         if (last_task_used_math)
 322                 __asm__("fnsave %0":"=m" (last_task_used_math->tss.i387));
 323         else
 324                 __asm__("fnclex");
 325         last_task_used_math = current;
 326         if (current->used_math) {
 327                 __asm__("frstor %0": :"m" (current->tss.i387));
 328         } else {
 329                 __asm__("fninit");
 330                 current->used_math=1;
 331         }
 332         timer_active &= ~(1<<COPRO_TIMER);
 333 #endif  
 334 }
 335 
 336 #ifndef CONFIG_MATH_EMULATION
 337 
 338 asmlinkage void math_emulate(long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 339 {
 340   printk("math-emulation not enabled and no coprocessor found.\n");
 341   printk("killing %s.\n",current->comm);
 342   send_sig(SIGFPE,current,1);
 343   schedule();
 344 }
 345 
 346 #endif /* CONFIG_MATH_EMULATION */
 347 
 348 void trap_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 349 {
 350         int i;
 351         struct desc_struct * p;
 352         static int smptrap=0;
 353         
 354         if(smptrap)
 355         {
 356                 __asm__("pushfl ; andl $0xffffbfff,(%esp) ; popfl");
 357                 load_ldt(0);
 358                 return;
 359         }
 360         smptrap++;
 361         if (strncmp((char*)0x0FFFD9, "EISA", 4) == 0)
 362                 EISA_bus = 1;
 363         set_call_gate(&default_ldt,lcall7);
 364         set_trap_gate(0,&divide_error);
 365         set_trap_gate(1,&debug);
 366         set_trap_gate(2,&nmi);
 367         set_system_gate(3,&int3);       /* int3-5 can be called from all */
 368         set_system_gate(4,&overflow);
 369         set_system_gate(5,&bounds);
 370         set_trap_gate(6,&invalid_op);
 371         set_trap_gate(7,&device_not_available);
 372         set_trap_gate(8,&double_fault);
 373         set_trap_gate(9,&coprocessor_segment_overrun);
 374         set_trap_gate(10,&invalid_TSS);
 375         set_trap_gate(11,&segment_not_present);
 376         set_trap_gate(12,&stack_segment);
 377         set_trap_gate(13,&general_protection);
 378         set_trap_gate(14,&page_fault);
 379         set_trap_gate(15,&reserved);
 380         set_trap_gate(16,&coprocessor_error);
 381         set_trap_gate(17,&alignment_check);
 382         for (i=18;i<48;i++)
 383                 set_trap_gate(i,&reserved);
 384         set_system_gate(0x80,&system_call);
 385 /* set up GDT task & ldt entries */
 386         p = gdt+FIRST_TSS_ENTRY;
 387         set_tss_desc(p, &init_task.tss);
 388         p++;
 389         set_ldt_desc(p, &default_ldt, 1);
 390         p++;
 391         for(i=1 ; i<NR_TASKS ; i++) {
 392                 p->a=p->b=0;
 393                 p++;
 394                 p->a=p->b=0;
 395                 p++;
 396         }
 397 /* Clear NT, so that we won't have troubles with that later on */
 398         __asm__("pushfl ; andl $0xffffbfff,(%esp) ; popfl");
 399         load_TR(0);
 400         load_ldt(0);
 401 }

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