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 task_struct * task;
 245 
 246         clts();
 247 #ifdef __SMP__
 248         task = current;
 249 #else
 250         task = last_task_used_math;
 251         last_task_used_math = NULL;
 252         if (!task) {
 253                 __asm__("fnclex");
 254                 return;
 255         }
 256 #endif
 257         /*
 258          *      Save the info for the exception handler
 259          */
 260         __asm__ __volatile__("fnsave %0":"=m" (task->tss.i387.hard));
 261         task->flags&=~PF_USEDFPU;
 262 
 263         send_sig(SIGFPE, task, 1);
 264         task->tss.trap_no = 16;
 265         task->tss.error_code = 0;
 266 
 267         /*
 268          * Give the process a clean slate next time they use
 269          * the FPU (and if they haven't accepted the SIGFP before
 270          * that, it's their problem..)
 271          */
 272         stts();
 273         task->used_math = 0;
 274 }
 275 
 276 asmlinkage void do_coprocessor_error(struct pt_regs * regs, long error_code)
     /* [previous][next][first][last][top][bottom][index][help] */
 277 {
 278         ignore_irq13 = 1;
 279         math_error();
 280 }
 281 
 282 /*
 283  *  'math_state_restore()' saves the current math information in the
 284  * old math state array, and gets the new ones from the current task
 285  *
 286  * Careful.. There are problems with IBM-designed IRQ13 behaviour.
 287  * Don't touch unless you *really* know how it works.
 288  */
 289 asmlinkage void math_state_restore(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 290 {
 291         __asm__ __volatile__("clts");           /* Allow maths ops (or we recurse) */
 292 
 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 isn't used with SMP machines (thank god).
 300  */
 301 #ifndef __SMP__
 302         if (last_task_used_math == current)
 303                 return;
 304         if (last_task_used_math)
 305                 __asm__("fnsave %0":"=m" (last_task_used_math->tss.i387));
 306         else
 307                 __asm__("fnclex");
 308         last_task_used_math = current;
 309 #endif
 310 
 311         if(current->used_math)
 312                 __asm__("frstor %0": :"m" (current->tss.i387));
 313         else
 314         {
 315                 /*
 316                  *      Our first FPU usage, clean the chip.
 317                  */
 318                 __asm__("fninit");
 319                 current->used_math = 1;
 320         }
 321         current->flags|=PF_USEDFPU;             /* So we fnsave on switch_to() */
 322 }
 323 
 324 #ifndef CONFIG_MATH_EMULATION
 325 
 326 asmlinkage void math_emulate(long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 327 {
 328   printk("math-emulation not enabled and no coprocessor found.\n");
 329   printk("killing %s.\n",current->comm);
 330   send_sig(SIGFPE,current,1);
 331   schedule();
 332 }
 333 
 334 #endif /* CONFIG_MATH_EMULATION */
 335 
 336 void trap_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 337 {
 338         int i;
 339         struct desc_struct * p;
 340         static int smptrap=0;
 341         
 342         if(smptrap)
 343         {
 344                 __asm__("pushfl ; andl $0xffffbfff,(%esp) ; popfl");
 345                 load_ldt(0);
 346                 return;
 347         }
 348         smptrap++;
 349         if (strncmp((char*)0x0FFFD9, "EISA", 4) == 0)
 350                 EISA_bus = 1;
 351         set_call_gate(&default_ldt,lcall7);
 352         set_trap_gate(0,&divide_error);
 353         set_trap_gate(1,&debug);
 354         set_trap_gate(2,&nmi);
 355         set_system_gate(3,&int3);       /* int3-5 can be called from all */
 356         set_system_gate(4,&overflow);
 357         set_system_gate(5,&bounds);
 358         set_trap_gate(6,&invalid_op);
 359         set_trap_gate(7,&device_not_available);
 360         set_trap_gate(8,&double_fault);
 361         set_trap_gate(9,&coprocessor_segment_overrun);
 362         set_trap_gate(10,&invalid_TSS);
 363         set_trap_gate(11,&segment_not_present);
 364         set_trap_gate(12,&stack_segment);
 365         set_trap_gate(13,&general_protection);
 366         set_trap_gate(14,&page_fault);
 367         set_trap_gate(15,&reserved);
 368         set_trap_gate(16,&coprocessor_error);
 369         set_trap_gate(17,&alignment_check);
 370         for (i=18;i<48;i++)
 371                 set_trap_gate(i,&reserved);
 372         set_system_gate(0x80,&system_call);
 373 /* set up GDT task & ldt entries */
 374         p = gdt+FIRST_TSS_ENTRY;
 375         set_tss_desc(p, &init_task.tss);
 376         p++;
 377         set_ldt_desc(p, &default_ldt, 1);
 378         p++;
 379         for(i=1 ; i<NR_TASKS ; i++) {
 380                 p->a=p->b=0;
 381                 p++;
 382                 p->a=p->b=0;
 383                 p++;
 384         }
 385 /* Clear NT, so that we won't have troubles with that later on */
 386         __asm__("pushfl ; andl $0xffffbfff,(%esp) ; popfl");
 387         load_TR(0);
 388         load_ldt(0);
 389 }

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