1 // arch/x86/misc.cc -- Misc. arch-specific stuff
3 // This software is copyright (c) 2006 Scott Wood <scott@buserror.net>.
5 // This software is provided 'as-is', without any express or implied warranty.
6 // In no event will the authors or contributors be held liable for any damages
7 // arising from the use of this software.
9 // Permission is hereby granted to everyone, free of charge, to use, copy,
10 // modify, prepare derivative works of, publish, distribute, perform,
11 // sublicense, and/or sell copies of the Software, provided that the above
12 // copyright notice and disclaimer of warranty be included in all copies or
13 // substantial portions of this software.
15 #include <kern/types.h>
16 #include <kern/libc.h>
17 #include <kern/arch.h>
18 #include <kern/i8259.h>
19 #include <kern/time.h>
20 #include <kern/thread.h>
23 #include <arch/addrs.h>
24 #include <arch/multiboot.h>
26 extern u32 x86_init_ptbl_l2[1024];
29 u8 stack[4096 - ::Threads::thread_size];
30 ::Threads::Thread thread;
31 } __attribute__((aligned(4096))) x86_init_stack;
37 void show_regs(u32 *stack) {
38 printf("eax: 0x%08x ecx: 0x%08x edx: 0x%08x ebx: 0x%08x\n"
39 "esp: 0x%08x ebp: 0x%08x esi: 0x%08x edi: 0x%08x\n"
45 stack[9] & 3 ? stack[11] : (u32)stack + 11 * 4,
51 printf("Stack trace: ");
52 u32 *frame = (u32 *)stack[5];
54 for (int i = 2; i < 32; i++) {
55 u32 stackptr = frame[1];
56 frame = (u32 *)frame[0];
58 if ((u32)frame < 0x80000000UL)
64 printf("0x%08x ", stackptr);
68 struct TimerInt : public IRQ::Interrupt {
71 Time::monotonic_timers->run();
80 ::Threads::Thread *init_thread;
84 init_thread = &x86_init_stack.thread;
85 Priv::early_adjust_mappings();
87 Priv::MultiBoot::process_info();
90 u32 tss_addr = reinterpret_cast<u32>(&Priv::tss);
91 x86_gdt[3].base_low = tss_addr & 0xffff;
92 x86_gdt[3].base_mid = (tss_addr & 0xff0000) >> 16;
93 x86_gdt[3].base_high = (tss_addr & 0xff000000) >> 24;
96 asm volatile("ltr %w0" : : "r" (0x18) : "memory");
97 init_thread->addr_space = new Mem::AddrSpace(x86_init_ptbl_l2);
98 init_thread->active_addr_space = init_thread->addr_space;
103 IRQ::InterruptSlot *timer = i8259.get_slot(0);
104 i8259.request_int(timer, &Priv::timer_int);
107 void ArchThread::init(void *entry, void *arg)
109 void **stack = reinterpret_cast<void **>(this);
120 using Arch::Priv::show_regs;
122 extern "C" void x86_do_diverr(u32 *stack)
124 printf("Division error at 0x%04x:0x%08x\n", stack[9], stack[8]);
129 extern "C" void x86_do_debug(u32 *stack)
131 printf("Debug exception at 0x%04x:0x%08x\n", stack[9], stack[8]);
136 extern "C" void x86_do_breakpoint(u32 *stack)
138 printf("Breakpoint at 0x%04x:0x%08x\n", stack[9], stack[8]);
143 extern "C" void x86_do_invalid_insn(u32 *stack)
145 printf("Invalid instruction at 0x%04x:0x%08x\n", stack[9], stack[8]);
150 extern "C" void x86_do_page_fault(u32 *stack, u32 fault_addr, u32 error_code)
157 // A reserved bit was set in the PTE; this is always a bug.
161 // Don't try to fix up a page fault if interrupts were disabled. It is an
162 // error to access non-locked pages with interrupts disabled. Trying to
163 // fix it up in the case of an access that would be legitimate if interrupts
164 // were enabled would simply mask the loss of atomicity, and trying to grab
165 // locks to look up the address if it is a completely bad reference won't
166 // accomplish much other than decreasing the odds that the fault message
169 if (!(stack[10] & 0x200))
172 // Don't allow fault-ins using a borrowed addr-space.
173 as = curthread->addr_space;
175 if (!as || curthread == Arch::init_thread)
181 if (as->handle_fault(fault_addr, error_code & 2,
182 false /* error_code & 16 */, error_code & 4))
185 // FIXME: throw exception to user
191 printf("Page fault at 0x%04x:0x%08x for 0x%08x, error code: 0x%04x\n",
192 stack[9], stack[8], fault_addr, error_code);
199 extern "C" void x86_do_gpf(u32 *stack, u32 error_code)
206 printf("General protection fault at 0x%04x:0x%08x, error code: 0x%04x\n",
207 stack[9], stack[8], error_code);
214 extern "C" void x86_do_irq(int irq)
216 IRQ::i8259.handle_irq(irq - 0x20);