Import 2.4.0-test6pre4
[davej-history.git] / arch / i386 / kernel / traps.c
blob7bd8a04d052385cd106bae57937eb309a41af3db
1 /*
2 * linux/arch/i386/traps.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * Pentium III FXSR, SSE support
7 * Gareth Hughes <gareth@valinux.com>, May 2000
8 */
11 * 'Traps.c' handles hardware traps and faults after we have saved some
12 * state in 'asm.s'.
14 #include <linux/config.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/timer.h>
21 #include <linux/mm.h>
22 #include <linux/init.h>
23 #include <linux/delay.h>
24 #include <linux/spinlock.h>
25 #include <linux/interrupt.h>
27 #ifdef CONFIG_MCA
28 #include <linux/mca.h>
29 #include <asm/processor.h>
30 #endif
32 #include <asm/system.h>
33 #include <asm/uaccess.h>
34 #include <asm/io.h>
35 #include <asm/atomic.h>
36 #include <asm/debugreg.h>
37 #include <asm/desc.h>
38 #include <asm/i387.h>
40 #include <asm/smp.h>
41 #include <asm/pgalloc.h>
43 #ifdef CONFIG_X86_VISWS_APIC
44 #include <asm/fixmap.h>
45 #include <asm/cobalt.h>
46 #include <asm/lithium.h>
47 #endif
49 #include <linux/irq.h>
51 asmlinkage int system_call(void);
52 asmlinkage void lcall7(void);
53 asmlinkage void lcall27(void);
55 struct desc_struct default_ldt[] = { { 0, 0 }, { 0, 0 }, { 0, 0 },
56 { 0, 0 }, { 0, 0 } };
59 * The IDT has to be page-aligned to simplify the Pentium
60 * F0 0F bug workaround.. We have a special link segment
61 * for this.
63 struct desc_struct idt_table[256] __attribute__((__section__(".data.idt"))) = { {0, 0}, };
65 extern int console_loglevel;
67 static inline void console_silent(void)
69 console_loglevel = 0;
72 static inline void console_verbose(void)
74 if (console_loglevel)
75 console_loglevel = 15;
78 asmlinkage void divide_error(void);
79 asmlinkage void debug(void);
80 asmlinkage void nmi(void);
81 asmlinkage void int3(void);
82 asmlinkage void overflow(void);
83 asmlinkage void bounds(void);
84 asmlinkage void invalid_op(void);
85 asmlinkage void device_not_available(void);
86 asmlinkage void double_fault(void);
87 asmlinkage void coprocessor_segment_overrun(void);
88 asmlinkage void invalid_TSS(void);
89 asmlinkage void segment_not_present(void);
90 asmlinkage void stack_segment(void);
91 asmlinkage void general_protection(void);
92 asmlinkage void page_fault(void);
93 asmlinkage void coprocessor_error(void);
94 asmlinkage void simd_coprocessor_error(void);
95 asmlinkage void reserved(void);
96 asmlinkage void alignment_check(void);
97 asmlinkage void spurious_interrupt_bug(void);
99 int kstack_depth_to_print = 24;
102 * These constants are for searching for possible module text
103 * segments. MODULE_RANGE is a guess of how much space is likely
104 * to be vmalloced.
106 #define MODULE_RANGE (8*1024*1024)
108 void show_stack(unsigned long * esp)
110 unsigned long *stack, addr, module_start, module_end;
111 int i;
113 // debugging aid: "show_stack(NULL);" prints the
114 // back trace for this cpu.
116 if(esp==NULL)
117 esp=(unsigned long*)&esp;
119 stack = esp;
120 for(i=0; i < kstack_depth_to_print; i++) {
121 if (((long) stack & (THREAD_SIZE-1)) == 0)
122 break;
123 if (i && ((i % 8) == 0))
124 printk("\n ");
125 printk("%08lx ", *stack++);
128 printk("\nCall Trace: ");
129 stack = esp;
130 i = 1;
131 module_start = VMALLOC_START;
132 module_end = VMALLOC_END;
133 while (((long) stack & (THREAD_SIZE-1)) != 0) {
134 addr = *stack++;
136 * If the address is either in the text segment of the
137 * kernel, or in the region which contains vmalloc'ed
138 * memory, it *may* be the address of a calling
139 * routine; if so, print it so that someone tracing
140 * down the cause of the crash will be able to figure
141 * out the call path that was taken.
143 if (((addr >= (unsigned long) &_stext) &&
144 (addr <= (unsigned long) &_etext)) ||
145 ((addr >= module_start) && (addr <= module_end))) {
146 if (i && ((i % 8) == 0))
147 printk("\n ");
148 printk("[<%08lx>] ", addr);
149 i++;
154 static void show_registers(struct pt_regs *regs)
156 int i;
157 int in_kernel = 1;
158 unsigned long esp;
159 unsigned short ss;
161 esp = (unsigned long) (&regs->esp);
162 ss = __KERNEL_DS;
163 if (regs->xcs & 3) {
164 in_kernel = 0;
165 esp = regs->esp;
166 ss = regs->xss & 0xffff;
168 printk("CPU: %d\nEIP: %04x:[<%08lx>]\nEFLAGS: %08lx\n",
169 smp_processor_id(), 0xffff & regs->xcs, regs->eip, regs->eflags);
170 printk("eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n",
171 regs->eax, regs->ebx, regs->ecx, regs->edx);
172 printk("esi: %08lx edi: %08lx ebp: %08lx esp: %08lx\n",
173 regs->esi, regs->edi, regs->ebp, esp);
174 printk("ds: %04x es: %04x ss: %04x\n",
175 regs->xds & 0xffff, regs->xes & 0xffff, ss);
176 printk("Process %s (pid: %d, stackpage=%08lx)",
177 current->comm, current->pid, 4096+(unsigned long)current);
179 * When in-kernel, we also print out the stack and code at the
180 * time of the fault..
182 if (in_kernel) {
184 printk("\nStack: ");
185 show_stack((unsigned long*)esp);
187 printk("\nCode: ");
188 if(regs->eip < PAGE_OFFSET)
189 goto bad;
191 for(i=0;i<20;i++)
193 unsigned char c;
194 if(__get_user(c, &((unsigned char*)regs->eip)[i])) {
195 bad:
196 printk(" Bad EIP value.");
197 break;
199 printk("%02x ", c);
202 printk("\n");
205 spinlock_t die_lock = SPIN_LOCK_UNLOCKED;
207 void die(const char * str, struct pt_regs * regs, long err)
209 console_verbose();
210 spin_lock_irq(&die_lock);
211 printk("%s: %04lx\n", str, err & 0xffff);
212 show_registers(regs);
214 spin_unlock_irq(&die_lock);
215 do_exit(SIGSEGV);
218 static inline void die_if_kernel(const char * str, struct pt_regs * regs, long err)
220 if (!(regs->eflags & VM_MASK) && !(3 & regs->xcs))
221 die(str, regs, err);
224 static inline unsigned long get_cr2(void)
226 unsigned long address;
228 /* get the address */
229 __asm__("movl %%cr2,%0":"=r" (address));
230 return address;
233 static void inline do_trap(int trapnr, int signr, char *str, int vm86,
234 struct pt_regs * regs, long error_code, siginfo_t *info)
236 if (vm86 && regs->eflags & VM_MASK)
237 goto vm86_trap;
238 if (!(regs->xcs & 3))
239 goto kernel_trap;
241 trap_signal: {
242 struct task_struct *tsk = current;
243 tsk->thread.error_code = error_code;
244 tsk->thread.trap_no = trapnr;
245 if (info)
246 force_sig_info(signr, info, tsk);
247 else
248 force_sig(signr, tsk);
249 return;
252 kernel_trap: {
253 unsigned long fixup = search_exception_table(regs->eip);
254 if (fixup)
255 regs->eip = fixup;
256 else
257 die(str, regs, error_code);
258 return;
261 vm86_trap: {
262 int ret = handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, trapnr);
263 if (ret) goto trap_signal;
264 return;
268 #define DO_ERROR(trapnr, signr, str, name) \
269 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
271 do_trap(trapnr, signr, str, 0, regs, error_code, NULL); \
274 #define DO_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
275 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
277 siginfo_t info; \
278 info.si_signo = signr; \
279 info.si_errno = 0; \
280 info.si_code = sicode; \
281 info.si_addr = (void *)siaddr; \
282 do_trap(trapnr, signr, str, 0, regs, error_code, &info); \
285 #define DO_VM86_ERROR(trapnr, signr, str, name) \
286 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
288 do_trap(trapnr, signr, str, 1, regs, error_code, NULL); \
291 #define DO_VM86_ERROR_INFO(trapnr, signr, str, name, sicode, siaddr) \
292 asmlinkage void do_##name(struct pt_regs * regs, long error_code) \
294 siginfo_t info; \
295 info.si_signo = signr; \
296 info.si_errno = 0; \
297 info.si_code = sicode; \
298 info.si_addr = (void *)siaddr; \
299 do_trap(trapnr, signr, str, 1, regs, error_code, &info); \
302 DO_VM86_ERROR_INFO( 0, SIGFPE, "divide error", divide_error, FPE_INTDIV, regs->eip)
303 DO_VM86_ERROR( 3, SIGTRAP, "int3", int3)
304 DO_VM86_ERROR( 4, SIGSEGV, "overflow", overflow)
305 DO_VM86_ERROR( 5, SIGSEGV, "bounds", bounds)
306 DO_ERROR_INFO( 6, SIGILL, "invalid operand", invalid_op, ILL_ILLOPN, regs->eip)
307 DO_VM86_ERROR( 7, SIGSEGV, "device not available", device_not_available)
308 DO_ERROR( 8, SIGSEGV, "double fault", double_fault)
309 DO_ERROR( 9, SIGFPE, "coprocessor segment overrun", coprocessor_segment_overrun)
310 DO_ERROR(10, SIGSEGV, "invalid TSS", invalid_TSS)
311 DO_ERROR(11, SIGBUS, "segment not present", segment_not_present)
312 DO_ERROR(12, SIGBUS, "stack segment", stack_segment)
313 DO_ERROR_INFO(17, SIGBUS, "alignment check", alignment_check, BUS_ADRALN, get_cr2())
314 DO_ERROR(18, SIGSEGV, "reserved", reserved)
316 asmlinkage void do_general_protection(struct pt_regs * regs, long error_code)
318 if (regs->eflags & VM_MASK)
319 goto gp_in_vm86;
321 if (!(regs->xcs & 3))
322 goto gp_in_kernel;
324 current->thread.error_code = error_code;
325 current->thread.trap_no = 13;
326 force_sig(SIGSEGV, current);
327 return;
329 gp_in_vm86:
330 handle_vm86_fault((struct kernel_vm86_regs *) regs, error_code);
331 return;
333 gp_in_kernel:
335 unsigned long fixup;
336 fixup = search_exception_table(regs->eip);
337 if (fixup) {
338 regs->eip = fixup;
339 return;
341 die("general protection fault", regs, error_code);
345 static void mem_parity_error(unsigned char reason, struct pt_regs * regs)
347 printk("Uhhuh. NMI received. Dazed and confused, but trying to continue\n");
348 printk("You probably have a hardware problem with your RAM chips\n");
350 /* Clear and disable the memory parity error line. */
351 reason = (reason & 0xf) | 4;
352 outb(reason, 0x61);
355 static void io_check_error(unsigned char reason, struct pt_regs * regs)
357 unsigned long i;
359 printk("NMI: IOCK error (debug interrupt?)\n");
360 show_registers(regs);
362 /* Re-enable the IOCK line, wait for a few seconds */
363 reason = (reason & 0xf) | 8;
364 outb(reason, 0x61);
365 i = 2000;
366 while (--i) udelay(1000);
367 reason &= ~8;
368 outb(reason, 0x61);
371 static void unknown_nmi_error(unsigned char reason, struct pt_regs * regs)
373 #ifdef CONFIG_MCA
374 /* Might actually be able to figure out what the guilty party
375 * is. */
376 if( MCA_bus ) {
377 mca_handle_nmi();
378 return;
380 #endif
381 printk("Uhhuh. NMI received for unknown reason %02x.\n", reason);
382 printk("Dazed and confused, but trying to continue\n");
383 printk("Do you have a strange power saving mode enabled?\n");
386 #if CONFIG_X86_IO_APIC
388 int nmi_watchdog = 1;
390 static int __init setup_nmi_watchdog(char *str)
392 get_option(&str, &nmi_watchdog);
393 return 1;
396 __setup("nmi_watchdog=", setup_nmi_watchdog);
398 extern spinlock_t console_lock;
399 static spinlock_t nmi_print_lock = SPIN_LOCK_UNLOCKED;
401 inline void nmi_watchdog_tick(struct pt_regs * regs)
404 * the best way to detect wether a CPU has a 'hard lockup' problem
405 * is to check it's local APIC timer IRQ counts. If they are not
406 * changing then that CPU has some problem.
408 * as these watchdog NMI IRQs are broadcasted to every CPU, here
409 * we only have to check the current processor.
411 * since NMIs dont listen to _any_ locks, we have to be extremely
412 * careful not to rely on unsafe variables. The printk might lock
413 * up though, so we have to break up console_lock first ...
414 * [when there will be more tty-related locks, break them up
415 * here too!]
418 static unsigned int last_irq_sums [NR_CPUS] = { 0, },
419 alert_counter [NR_CPUS] = { 0, };
422 * Since current-> is always on the stack, and we always switch
423 * the stack NMI-atomically, it's safe to use smp_processor_id().
425 int sum, cpu = smp_processor_id();
427 sum = apic_timer_irqs[cpu];
429 if (last_irq_sums[cpu] == sum) {
431 * Ayiee, looks like this CPU is stuck ...
432 * wait a few IRQs (5 seconds) before doing the oops ...
434 alert_counter[cpu]++;
435 if (alert_counter[cpu] == 5*HZ) {
436 spin_lock(&nmi_print_lock);
438 * We are in trouble anyway, lets at least try
439 * to get a message out.
441 spin_trylock(&console_lock);
442 spin_unlock(&console_lock);
443 printk("NMI Watchdog detected LOCKUP on CPU%d, registers:\n", cpu);
444 show_registers(regs);
445 printk("console shuts up ...\n");
446 console_silent();
447 spin_unlock(&nmi_print_lock);
448 do_exit(SIGSEGV);
450 } else {
451 last_irq_sums[cpu] = sum;
452 alert_counter[cpu] = 0;
455 #endif
457 asmlinkage void do_nmi(struct pt_regs * regs, long error_code)
459 unsigned char reason = inb(0x61);
462 ++nmi_count(smp_processor_id());
463 if (!(reason & 0xc0)) {
464 #if CONFIG_X86_IO_APIC
466 * Ok, so this is none of the documented NMI sources,
467 * so it must be the NMI watchdog.
469 if (nmi_watchdog) {
470 nmi_watchdog_tick(regs);
471 return;
472 } else
473 unknown_nmi_error(reason, regs);
474 #else
475 unknown_nmi_error(reason, regs);
476 #endif
477 return;
479 if (reason & 0x80)
480 mem_parity_error(reason, regs);
481 if (reason & 0x40)
482 io_check_error(reason, regs);
484 * Reassert NMI in case it became active meanwhile
485 * as it's edge-triggered.
487 outb(0x8f, 0x70);
488 inb(0x71); /* dummy */
489 outb(0x0f, 0x70);
490 inb(0x71); /* dummy */
494 * Careful - we must not do a lock-kernel until we have checked that the
495 * debug fault happened in user mode. Getting debug exceptions while
496 * in the kernel has to be handled without locking, to avoid deadlocks..
498 * Being careful here means that we don't have to be as careful in a
499 * lot of more complicated places (task switching can be a bit lazy
500 * about restoring all the debug state, and ptrace doesn't have to
501 * find every occurrence of the TF bit that could be saved away even
502 * by user code - and we don't have to be careful about what values
503 * can be written to the debug registers because there are no really
504 * bad cases).
506 asmlinkage void do_debug(struct pt_regs * regs, long error_code)
508 unsigned int condition;
509 struct task_struct *tsk = current;
510 siginfo_t info;
512 __asm__ __volatile__("movl %%db6,%0" : "=r" (condition));
514 /* Mask out spurious debug traps due to lazy DR7 setting */
515 if (condition & (DR_TRAP0|DR_TRAP1|DR_TRAP2|DR_TRAP3)) {
516 if (!tsk->thread.debugreg[7])
517 goto clear_dr7;
520 if (regs->eflags & VM_MASK)
521 goto debug_vm86;
523 /* Mask out spurious TF errors due to lazy TF clearing */
524 if (condition & DR_STEP) {
526 * The TF error should be masked out only if the current
527 * process is not traced and if the TRAP flag has been set
528 * previously by a tracing process (condition detected by
529 * the PT_DTRACE flag); remember that the i386 TRAP flag
530 * can be modified by the process itself in user mode,
531 * allowing programs to debug themselves without the ptrace()
532 * interface.
534 if ((tsk->ptrace & (PT_DTRACE|PT_PTRACED)) == PT_DTRACE)
535 goto clear_TF;
538 /* If this is a kernel mode trap, we need to reset db7 to allow us to continue sanely */
539 if ((regs->xcs & 3) == 0)
540 goto clear_dr7;
542 /* Ok, finally something we can handle */
543 tsk->thread.trap_no = 1;
544 tsk->thread.error_code = error_code;
545 info.si_signo = SIGTRAP;
546 info.si_errno = 0;
547 info.si_code = TRAP_BRKPT;
548 info.si_addr = (void *)regs->eip;
549 force_sig_info(SIGTRAP, &info, tsk);
550 return;
552 debug_vm86:
553 handle_vm86_trap((struct kernel_vm86_regs *) regs, error_code, 1);
554 return;
556 clear_dr7:
557 __asm__("movl %0,%%db7"
558 : /* no output */
559 : "r" (0));
560 return;
562 clear_TF:
563 regs->eflags &= ~TF_MASK;
564 return;
568 * Note that we play around with the 'TS' bit in an attempt to get
569 * the correct behaviour even in the presence of the asynchronous
570 * IRQ13 behaviour
572 void math_error(void *eip)
574 struct task_struct * task;
575 siginfo_t info;
576 unsigned short cwd, swd;
579 * Save the info for the exception handler and clear the error.
581 task = current;
582 save_init_fpu(task);
583 task->thread.trap_no = 16;
584 task->thread.error_code = 0;
585 info.si_signo = SIGFPE;
586 info.si_errno = 0;
587 info.si_code = __SI_FAULT;
588 info.si_addr = eip;
590 * (~cwd & swd) will mask out exceptions that are not set to unmasked
591 * status. 0x3f is the exception bits in these regs, 0x200 is the
592 * C1 reg you need in case of a stack fault, 0x040 is the stack
593 * fault bit. We should only be taking one exception at a time,
594 * so if this combination doesn't produce any single exception,
595 * then we have a bad program that isn't syncronizing its FPU usage
596 * and it will suffer the consequences since we won't be able to
597 * fully reproduce the context of the exception
599 cwd = get_fpu_cwd(task);
600 swd = get_fpu_swd(task);
601 switch (((~cwd) & swd & 0x3f) | (swd & 0x240)) {
602 case 0x000:
603 default:
604 break;
605 case 0x001: /* Invalid Op */
606 case 0x040: /* Stack Fault */
607 case 0x240: /* Stack Fault | Direction */
608 info.si_code = FPE_FLTINV;
609 break;
610 case 0x002: /* Denormalize */
611 case 0x010: /* Underflow */
612 info.si_code = FPE_FLTUND;
613 break;
614 case 0x004: /* Zero Divide */
615 info.si_code = FPE_FLTDIV;
616 break;
617 case 0x008: /* Overflow */
618 info.si_code = FPE_FLTOVF;
619 break;
620 case 0x020: /* Precision */
621 info.si_code = FPE_FLTRES;
622 break;
624 force_sig_info(SIGFPE, &info, task);
627 asmlinkage void do_coprocessor_error(struct pt_regs * regs, long error_code)
629 ignore_irq13 = 1;
630 math_error((void *)regs->eip);
633 void simd_math_error(void *eip)
635 struct task_struct * task;
636 siginfo_t info;
637 unsigned short mxcsr;
640 * Save the info for the exception handler and clear the error.
642 task = current;
643 save_init_fpu(task);
644 load_mxcsr(0x1f80);
645 task->thread.trap_no = 19;
646 task->thread.error_code = 0;
647 info.si_signo = SIGFPE;
648 info.si_errno = 0;
649 info.si_code = __SI_FAULT;
650 info.si_addr = eip;
652 * The SIMD FPU exceptions are handled a little differently, as there
653 * is only a single status/control register. Thus, to determine which
654 * unmasked exception was caught we must mask the exception mask bits
655 * at 0x1f80, and then use these to mask the exception bits at 0x3f.
657 mxcsr = get_fpu_mxcsr(task);
658 switch (~((mxcsr & 0x1f80) >> 7) & (mxcsr & 0x3f)) {
659 case 0x000:
660 default:
661 break;
662 case 0x001: /* Invalid Op */
663 info.si_code = FPE_FLTINV;
664 break;
665 case 0x002: /* Denormalize */
666 case 0x010: /* Underflow */
667 info.si_code = FPE_FLTUND;
668 break;
669 case 0x004: /* Zero Divide */
670 info.si_code = FPE_FLTDIV;
671 break;
672 case 0x008: /* Overflow */
673 info.si_code = FPE_FLTOVF;
674 break;
675 case 0x020: /* Precision */
676 info.si_code = FPE_FLTRES;
677 break;
679 force_sig_info(SIGFPE, &info, task);
682 asmlinkage void do_simd_coprocessor_error(struct pt_regs * regs,
683 long error_code)
685 if (cpu_has_xmm) {
686 /* Handle SIMD FPU exceptions on PIII+ processors. */
687 ignore_irq13 = 1;
688 simd_math_error((void *)regs->eip);
689 } else {
691 * Handle strange cache flush from user space exception
692 * in all other cases. This is undocumented behaviour.
694 if (regs->eflags & VM_MASK) {
695 handle_vm86_fault((struct kernel_vm86_regs *)regs,
696 error_code);
697 return;
699 die_if_kernel("cache flush denied", regs, error_code);
700 current->thread.trap_no = 19;
701 current->thread.error_code = error_code;
702 force_sig(SIGSEGV, current);
706 asmlinkage void do_spurious_interrupt_bug(struct pt_regs * regs,
707 long error_code)
709 #if 0
710 /* No need to warn about this any longer. */
711 printk("Ignoring P6 Local APIC Spurious Interrupt Bug...\n");
712 #endif
716 * 'math_state_restore()' saves the current math information in the
717 * old math state array, and gets the new ones from the current task
719 * Careful.. There are problems with IBM-designed IRQ13 behaviour.
720 * Don't touch unless you *really* know how it works.
722 asmlinkage void math_state_restore(struct pt_regs regs)
724 __asm__ __volatile__("clts"); /* Allow maths ops (or we recurse) */
726 if (current->used_math) {
727 restore_fpu(current);
728 } else {
730 * Our first FPU usage, clean the chip.
732 __asm__("fninit");
733 current->used_math = 1;
735 current->flags |= PF_USEDFPU; /* So we fnsave on switch_to() */
738 #ifndef CONFIG_MATH_EMULATION
740 asmlinkage void math_emulate(long arg)
742 printk("math-emulation not enabled and no coprocessor found.\n");
743 printk("killing %s.\n",current->comm);
744 force_sig(SIGFPE,current);
745 schedule();
748 #endif /* CONFIG_MATH_EMULATION */
750 #ifndef CONFIG_M686
751 void __init trap_init_f00f_bug(void)
753 unsigned long page;
754 pgd_t * pgd;
755 pmd_t * pmd;
756 pte_t * pte;
759 * Allocate a new page in virtual address space,
760 * move the IDT into it and write protect this page.
762 page = (unsigned long) vmalloc(PAGE_SIZE);
763 pgd = pgd_offset(&init_mm, page);
764 pmd = pmd_offset(pgd, page);
765 pte = pte_offset(pmd, page);
766 __free_page(pte_page(*pte));
767 *pte = mk_pte_phys(__pa(&idt_table), PAGE_KERNEL_RO);
769 * Not that any PGE-capable kernel should have the f00f bug ...
771 __flush_tlb_all();
774 * "idt" is magic - it overlaps the idt_descr
775 * variable so that updating idt will automatically
776 * update the idt descriptor..
778 idt = (struct desc_struct *)page;
779 __asm__ __volatile__("lidt %0": "=m" (idt_descr));
781 #endif
783 #define _set_gate(gate_addr,type,dpl,addr) \
784 do { \
785 int __d0, __d1; \
786 __asm__ __volatile__ ("movw %%dx,%%ax\n\t" \
787 "movw %4,%%dx\n\t" \
788 "movl %%eax,%0\n\t" \
789 "movl %%edx,%1" \
790 :"=m" (*((long *) (gate_addr))), \
791 "=m" (*(1+(long *) (gate_addr))), "=&a" (__d0), "=&d" (__d1) \
792 :"i" ((short) (0x8000+(dpl<<13)+(type<<8))), \
793 "3" ((char *) (addr)),"2" (__KERNEL_CS << 16)); \
794 } while (0)
798 * This needs to use 'idt_table' rather than 'idt', and
799 * thus use the _nonmapped_ version of the IDT, as the
800 * Pentium F0 0F bugfix can have resulted in the mapped
801 * IDT being write-protected.
803 void set_intr_gate(unsigned int n, void *addr)
805 _set_gate(idt_table+n,14,0,addr);
808 static void __init set_trap_gate(unsigned int n, void *addr)
810 _set_gate(idt_table+n,15,0,addr);
813 static void __init set_system_gate(unsigned int n, void *addr)
815 _set_gate(idt_table+n,15,3,addr);
818 static void __init set_call_gate(void *a, void *addr)
820 _set_gate(a,12,3,addr);
823 #define _set_seg_desc(gate_addr,type,dpl,base,limit) {\
824 *((gate_addr)+1) = ((base) & 0xff000000) | \
825 (((base) & 0x00ff0000)>>16) | \
826 ((limit) & 0xf0000) | \
827 ((dpl)<<13) | \
828 (0x00408000) | \
829 ((type)<<8); \
830 *(gate_addr) = (((base) & 0x0000ffff)<<16) | \
831 ((limit) & 0x0ffff); }
833 #define _set_tssldt_desc(n,addr,limit,type) \
834 __asm__ __volatile__ ("movw %w3,0(%2)\n\t" \
835 "movw %%ax,2(%2)\n\t" \
836 "rorl $16,%%eax\n\t" \
837 "movb %%al,4(%2)\n\t" \
838 "movb %4,5(%2)\n\t" \
839 "movb $0,6(%2)\n\t" \
840 "movb %%ah,7(%2)\n\t" \
841 "rorl $16,%%eax" \
842 : "=m"(*(n)) : "a" (addr), "r"(n), "ir"(limit), "i"(type))
844 void set_tss_desc(unsigned int n, void *addr)
846 _set_tssldt_desc(gdt_table+__TSS(n), (int)addr, 235, 0x89);
849 void set_ldt_desc(unsigned int n, void *addr, unsigned int size)
851 _set_tssldt_desc(gdt_table+__LDT(n), (int)addr, ((size << 3)-1), 0x82);
854 #ifdef CONFIG_X86_VISWS_APIC
857 * On Rev 005 motherboards legacy device interrupt lines are wired directly
858 * to Lithium from the 307. But the PROM leaves the interrupt type of each
859 * 307 logical device set appropriate for the 8259. Later we'll actually use
860 * the 8259, but for now we have to flip the interrupt types to
861 * level triggered, active lo as required by Lithium.
864 #define REG 0x2e /* The register to read/write */
865 #define DEV 0x07 /* Register: Logical device select */
866 #define VAL 0x2f /* The value to read/write */
868 static void
869 superio_outb(int dev, int reg, int val)
871 outb(DEV, REG);
872 outb(dev, VAL);
873 outb(reg, REG);
874 outb(val, VAL);
877 static int __attribute__ ((unused))
878 superio_inb(int dev, int reg)
880 outb(DEV, REG);
881 outb(dev, VAL);
882 outb(reg, REG);
883 return inb(VAL);
886 #define FLOP 3 /* floppy logical device */
887 #define PPORT 4 /* parallel logical device */
888 #define UART5 5 /* uart2 logical device (not wired up) */
889 #define UART6 6 /* uart1 logical device (THIS is the serial port!) */
890 #define IDEST 0x70 /* int. destination (which 307 IRQ line) reg. */
891 #define ITYPE 0x71 /* interrupt type register */
893 /* interrupt type bits */
894 #define LEVEL 0x01 /* bit 0, 0 == edge triggered */
895 #define ACTHI 0x02 /* bit 1, 0 == active lo */
897 static void
898 superio_init(void)
900 if (visws_board_type == VISWS_320 && visws_board_rev == 5) {
901 superio_outb(UART6, IDEST, 0); /* 0 means no intr propagated */
902 printk("SGI 320 rev 5: disabling 307 uart1 interrupt\n");
906 static void
907 lithium_init(void)
909 set_fixmap(FIX_LI_PCIA, LI_PCI_A_PHYS);
910 printk("Lithium PCI Bridge A, Bus Number: %d\n",
911 li_pcia_read16(LI_PCI_BUSNUM) & 0xff);
912 set_fixmap(FIX_LI_PCIB, LI_PCI_B_PHYS);
913 printk("Lithium PCI Bridge B (PIIX4), Bus Number: %d\n",
914 li_pcib_read16(LI_PCI_BUSNUM) & 0xff);
916 /* XXX blindly enables all interrupts */
917 li_pcia_write16(LI_PCI_INTEN, 0xffff);
918 li_pcib_write16(LI_PCI_INTEN, 0xffff);
921 static void
922 cobalt_init(void)
925 * On normal SMP PC this is used only with SMP, but we have to
926 * use it and set it up here to start the Cobalt clock
928 set_fixmap(FIX_APIC_BASE, APIC_DEFAULT_PHYS_BASE);
929 printk("Local APIC ID %lx\n", apic_read(APIC_ID));
930 printk("Local APIC Version %lx\n", apic_read(APIC_LVR));
932 set_fixmap(FIX_CO_CPU, CO_CPU_PHYS);
933 printk("Cobalt Revision %lx\n", co_cpu_read(CO_CPU_REV));
935 set_fixmap(FIX_CO_APIC, CO_APIC_PHYS);
936 printk("Cobalt APIC ID %lx\n", co_apic_read(CO_APIC_ID));
938 /* Enable Cobalt APIC being careful to NOT change the ID! */
939 co_apic_write(CO_APIC_ID, co_apic_read(CO_APIC_ID)|CO_APIC_ENABLE);
941 printk("Cobalt APIC enabled: ID reg %lx\n", co_apic_read(CO_APIC_ID));
943 #endif
944 void __init trap_init(void)
946 if (isa_readl(0x0FFFD9) == 'E'+('I'<<8)+('S'<<16)+('A'<<24))
947 EISA_bus = 1;
949 set_trap_gate(0,&divide_error);
950 set_trap_gate(1,&debug);
951 set_intr_gate(2,&nmi);
952 set_system_gate(3,&int3); /* int3-5 can be called from all */
953 set_system_gate(4,&overflow);
954 set_system_gate(5,&bounds);
955 set_trap_gate(6,&invalid_op);
956 set_trap_gate(7,&device_not_available);
957 set_trap_gate(8,&double_fault);
958 set_trap_gate(9,&coprocessor_segment_overrun);
959 set_trap_gate(10,&invalid_TSS);
960 set_trap_gate(11,&segment_not_present);
961 set_trap_gate(12,&stack_segment);
962 set_trap_gate(13,&general_protection);
963 set_trap_gate(14,&page_fault);
964 set_trap_gate(15,&spurious_interrupt_bug);
965 set_trap_gate(16,&coprocessor_error);
966 set_trap_gate(17,&alignment_check);
967 set_trap_gate(19,&simd_coprocessor_error);
969 set_system_gate(SYSCALL_VECTOR,&system_call);
972 * default LDT is a single-entry callgate to lcall7 for iBCS
973 * and a callgate to lcall27 for Solaris/x86 binaries
975 set_call_gate(&default_ldt[0],lcall7);
976 set_call_gate(&default_ldt[4],lcall27);
979 * Should be a barrier for any external CPU state.
981 cpu_init();
983 #ifdef CONFIG_X86_VISWS_APIC
984 superio_init();
985 lithium_init();
986 cobalt_init();
987 #endif