1 /* 2 * Copyright 2008-2011, Michael Lotz, mmlr@mlotz.ch. 3 * Copyright 2010, Clemens Zeidler, haiku@clemens-zeidler.de. 4 * Copyright 2009-2011, Ingo Weinhold, ingo_weinhold@gmx.de. 5 * Copyright 2002-2010, Axel Dörfler, axeld@pinc-software.de. 6 * Distributed under the terms of the MIT License. 7 * 8 * Copyright 2001, Travis Geiselbrecht. All rights reserved. 9 * Distributed under the terms of the NewOS License. 10 */ 11 12 13 #include <cpu.h> 14 #include <int.h> 15 #include <kscheduler.h> 16 #include <team.h> 17 #include <thread.h> 18 #include <util/AutoLock.h> 19 #include <vm/vm.h> 20 #include <vm/vm_priv.h> 21 22 #include <arch/cpu.h> 23 #include <arch/int.h> 24 25 #include <arch/x86/apic.h> 26 #include <arch/x86/descriptors.h> 27 #include <arch/x86/msi.h> 28 #include <arch/x86/msi_priv.h> 29 30 #include <stdio.h> 31 32 // interrupt controllers 33 #include <arch/x86/ioapic.h> 34 #include <arch/x86/pic.h> 35 36 37 //#define TRACE_ARCH_INT 38 #ifdef TRACE_ARCH_INT 39 # define TRACE(x) dprintf x 40 #else 41 # define TRACE(x) ; 42 #endif 43 44 45 static irq_source sVectorSources[NUM_IO_VECTORS]; 46 47 static const char *kInterruptNames[] = { 48 /* 0 */ "Divide Error Exception", 49 /* 1 */ "Debug Exception", 50 /* 2 */ "NMI Interrupt", 51 /* 3 */ "Breakpoint Exception", 52 /* 4 */ "Overflow Exception", 53 /* 5 */ "BOUND Range Exceeded Exception", 54 /* 6 */ "Invalid Opcode Exception", 55 /* 7 */ "Device Not Available Exception", 56 /* 8 */ "Double Fault Exception", 57 /* 9 */ "Coprocessor Segment Overrun", 58 /* 10 */ "Invalid TSS Exception", 59 /* 11 */ "Segment Not Present", 60 /* 12 */ "Stack Fault Exception", 61 /* 13 */ "General Protection Exception", 62 /* 14 */ "Page-Fault Exception", 63 /* 15 */ "-", 64 /* 16 */ "x87 FPU Floating-Point Error", 65 /* 17 */ "Alignment Check Exception", 66 /* 18 */ "Machine-Check Exception", 67 /* 19 */ "SIMD Floating-Point Exception", 68 }; 69 static const int kInterruptNameCount = 20; 70 71 static const interrupt_controller* sCurrentPIC = NULL; 72 73 74 static const char* 75 exception_name(int number, char* buffer, int32 bufferSize) 76 { 77 if (number >= 0 && number < kInterruptNameCount) 78 return kInterruptNames[number]; 79 80 snprintf(buffer, bufferSize, "exception %d", number); 81 return buffer; 82 } 83 84 85 void 86 x86_invalid_exception(iframe* frame) 87 { 88 Thread* thread = thread_get_current_thread(); 89 char name[32]; 90 panic("unhandled trap 0x%lx (%s) at ip 0x%lx, thread %" B_PRId32 "!\n", 91 frame->vector, exception_name(frame->vector, name, sizeof(name)), 92 frame->ip, thread ? thread->id : -1); 93 } 94 95 96 void 97 x86_fatal_exception(iframe* frame) 98 { 99 char name[32]; 100 panic("Fatal exception \"%s\" occurred! Error code: 0x%lx\n", 101 exception_name(frame->vector, name, sizeof(name)), frame->error_code); 102 } 103 104 105 void 106 x86_unexpected_exception(iframe* frame) 107 { 108 debug_exception_type type; 109 uint32 signalNumber; 110 int32 signalCode; 111 addr_t signalAddress = 0; 112 int32 signalError = B_ERROR; 113 114 switch (frame->vector) { 115 case 0: // Divide Error Exception (#DE) 116 type = B_DIVIDE_ERROR; 117 signalNumber = SIGFPE; 118 signalCode = FPE_INTDIV; 119 signalAddress = frame->ip; 120 break; 121 122 case 4: // Overflow Exception (#OF) 123 type = B_OVERFLOW_EXCEPTION; 124 signalNumber = SIGFPE; 125 signalCode = FPE_INTOVF; 126 signalAddress = frame->ip; 127 break; 128 129 case 5: // BOUND Range Exceeded Exception (#BR) 130 type = B_BOUNDS_CHECK_EXCEPTION; 131 signalNumber = SIGTRAP; 132 signalCode = SI_USER; 133 break; 134 135 case 6: // Invalid Opcode Exception (#UD) 136 type = B_INVALID_OPCODE_EXCEPTION; 137 signalNumber = SIGILL; 138 signalCode = ILL_ILLOPC; 139 signalAddress = frame->ip; 140 break; 141 142 case 13: // General Protection Exception (#GP) 143 type = B_GENERAL_PROTECTION_FAULT; 144 signalNumber = SIGILL; 145 signalCode = ILL_PRVOPC; // or ILL_PRVREG 146 signalAddress = frame->ip; 147 break; 148 149 case 16: // x87 FPU Floating-Point Error (#MF) 150 type = B_FLOATING_POINT_EXCEPTION; 151 signalNumber = SIGFPE; 152 signalCode = FPE_FLTDIV; 153 // TODO: Determine the correct cause via the FPU status 154 // register! 155 signalAddress = frame->ip; 156 break; 157 158 case 17: // Alignment Check Exception (#AC) 159 type = B_ALIGNMENT_EXCEPTION; 160 signalNumber = SIGBUS; 161 signalCode = BUS_ADRALN; 162 // TODO: Also get the address (from where?). Since we don't enable 163 // alignment checking this exception should never happen, though. 164 signalError = EFAULT; 165 break; 166 167 case 19: // SIMD Floating-Point Exception (#XF) 168 type = B_FLOATING_POINT_EXCEPTION; 169 signalNumber = SIGFPE; 170 signalCode = FPE_FLTDIV; 171 // TODO: Determine the correct cause via the MXCSR register! 172 signalAddress = frame->ip; 173 break; 174 175 default: 176 x86_invalid_exception(frame); 177 return; 178 } 179 180 if (IFRAME_IS_USER(frame)) { 181 struct sigaction action; 182 Thread* thread = thread_get_current_thread(); 183 184 enable_interrupts(); 185 186 // If the thread has a signal handler for the signal, we simply send it 187 // the signal. Otherwise we notify the user debugger first. 188 if ((sigaction(signalNumber, NULL, &action) == 0 189 && action.sa_handler != SIG_DFL 190 && action.sa_handler != SIG_IGN) 191 || user_debug_exception_occurred(type, signalNumber)) { 192 Signal signal(signalNumber, signalCode, signalError, 193 thread->team->id); 194 signal.SetAddress((void*)signalAddress); 195 send_signal_to_thread(thread, signal, 0); 196 } 197 } else { 198 char name[32]; 199 panic("Unexpected exception \"%s\" occurred in kernel mode! " 200 "Error code: 0x%lx\n", 201 exception_name(frame->vector, name, sizeof(name)), 202 frame->error_code); 203 } 204 } 205 206 207 void 208 x86_hardware_interrupt(struct iframe* frame) 209 { 210 int32 vector = frame->vector - ARCH_INTERRUPT_BASE; 211 bool levelTriggered = false; 212 Thread* thread = thread_get_current_thread(); 213 214 if (sCurrentPIC->is_spurious_interrupt(vector)) { 215 TRACE(("got spurious interrupt at vector %ld\n", vector)); 216 return; 217 } 218 219 levelTriggered = sCurrentPIC->is_level_triggered_interrupt(vector); 220 221 if (!levelTriggered) { 222 // if it's not handled by the current pic then it's an apic generated 223 // interrupt like local interrupts, msi or ipi. 224 if (!sCurrentPIC->end_of_interrupt(vector)) 225 apic_end_of_interrupt(); 226 } 227 228 int_io_interrupt_handler(vector, levelTriggered); 229 230 if (levelTriggered) { 231 if (!sCurrentPIC->end_of_interrupt(vector)) 232 apic_end_of_interrupt(); 233 } 234 235 cpu_status state = disable_interrupts(); 236 if (thread->cpu->invoke_scheduler) { 237 SpinLocker schedulerLocker(thread->scheduler_lock); 238 scheduler_reschedule(B_THREAD_READY); 239 schedulerLocker.Unlock(); 240 restore_interrupts(state); 241 } else if (thread->post_interrupt_callback != NULL) { 242 void (*callback)(void*) = thread->post_interrupt_callback; 243 void* data = thread->post_interrupt_data; 244 245 thread->post_interrupt_callback = NULL; 246 thread->post_interrupt_data = NULL; 247 248 restore_interrupts(state); 249 250 callback(data); 251 } 252 } 253 254 255 void 256 x86_page_fault_exception(struct iframe* frame) 257 { 258 Thread* thread = thread_get_current_thread(); 259 addr_t cr2 = x86_read_cr2(); 260 addr_t newip; 261 262 if (debug_debugger_running()) { 263 // If this CPU or this thread has a fault handler, we're allowed to be 264 // here. 265 if (thread != NULL) { 266 cpu_ent* cpu = &gCPU[smp_get_current_cpu()]; 267 if (cpu->fault_handler != 0) { 268 debug_set_page_fault_info(cr2, frame->ip, 269 (frame->error_code & 0x2) != 0 270 ? DEBUG_PAGE_FAULT_WRITE : 0); 271 frame->ip = cpu->fault_handler; 272 frame->bp = cpu->fault_handler_stack_pointer; 273 return; 274 } 275 276 if (thread->fault_handler != 0) { 277 kprintf("ERROR: thread::fault_handler used in kernel " 278 "debugger!\n"); 279 debug_set_page_fault_info(cr2, frame->ip, 280 (frame->error_code & 0x2) != 0 281 ? DEBUG_PAGE_FAULT_WRITE : 0); 282 frame->ip = reinterpret_cast<uintptr_t>(thread->fault_handler); 283 return; 284 } 285 } 286 287 // otherwise, not really 288 panic("page fault in debugger without fault handler! Touching " 289 "address %p from ip %p\n", (void*)cr2, (void*)frame->ip); 290 return; 291 } else if ((frame->flags & 0x200) == 0) { 292 // interrupts disabled 293 294 // If a page fault handler is installed, we're allowed to be here. 295 // TODO: Now we are generally allowing user_memcpy() with interrupts 296 // disabled, which in most cases is a bug. We should add some thread 297 // flag allowing to explicitly indicate that this handling is desired. 298 if (thread != NULL && thread->fault_handler != 0) { 299 uintptr_t handler 300 = reinterpret_cast<uintptr_t>(thread->fault_handler); 301 if (frame->ip != handler) { 302 frame->ip = handler; 303 return; 304 } 305 306 // The fault happened at the fault handler address. This is a 307 // certain infinite loop. 308 panic("page fault, interrupts disabled, fault handler loop. " 309 "Touching address %p from ip %p\n", (void*)cr2, 310 (void*)frame->ip); 311 } 312 313 // If we are not running the kernel startup the page fault was not 314 // allowed to happen and we must panic. 315 panic("page fault, but interrupts were disabled. Touching address " 316 "%p from ip %p\n", (void*)cr2, (void*)frame->ip); 317 return; 318 } else if (thread != NULL && thread->page_faults_allowed < 1) { 319 panic("page fault not allowed at this place. Touching address " 320 "%p from ip %p\n", (void*)cr2, (void*)frame->ip); 321 return; 322 } 323 324 enable_interrupts(); 325 326 vm_page_fault(cr2, frame->ip, 327 (frame->error_code & 0x2)!= 0, // write access 328 (frame->error_code & 0x10) != 0, // instruction fetch 329 (frame->error_code & 0x4) != 0, // userland 330 &newip); 331 if (newip != 0) { 332 // the page fault handler wants us to modify the iframe to set the 333 // IP the cpu will return to this ip 334 frame->ip = newip; 335 } 336 } 337 338 339 void 340 x86_set_irq_source(int irq, irq_source source) 341 { 342 sVectorSources[irq] = source; 343 } 344 345 346 // #pragma mark - 347 348 349 void 350 arch_int_enable_io_interrupt(int irq) 351 { 352 sCurrentPIC->enable_io_interrupt(irq); 353 } 354 355 356 void 357 arch_int_disable_io_interrupt(int irq) 358 { 359 sCurrentPIC->disable_io_interrupt(irq); 360 } 361 362 363 void 364 arch_int_configure_io_interrupt(int irq, uint32 config) 365 { 366 sCurrentPIC->configure_io_interrupt(irq, config); 367 } 368 369 370 #undef arch_int_enable_interrupts 371 #undef arch_int_disable_interrupts 372 #undef arch_int_restore_interrupts 373 #undef arch_int_are_interrupts_enabled 374 375 376 void 377 arch_int_enable_interrupts(void) 378 { 379 arch_int_enable_interrupts_inline(); 380 } 381 382 383 int 384 arch_int_disable_interrupts(void) 385 { 386 return arch_int_disable_interrupts_inline(); 387 } 388 389 390 void 391 arch_int_restore_interrupts(int oldState) 392 { 393 arch_int_restore_interrupts_inline(oldState); 394 } 395 396 397 bool 398 arch_int_are_interrupts_enabled(void) 399 { 400 return arch_int_are_interrupts_enabled_inline(); 401 } 402 403 404 void 405 arch_int_assign_to_cpu(int32 irq, int32 cpu) 406 { 407 switch (sVectorSources[irq]) { 408 case IRQ_SOURCE_IOAPIC: 409 if (sCurrentPIC->assign_interrupt_to_cpu != NULL) 410 sCurrentPIC->assign_interrupt_to_cpu(irq, cpu); 411 break; 412 413 case IRQ_SOURCE_MSI: 414 msi_assign_interrupt_to_cpu(irq, cpu); 415 break; 416 417 default: 418 break; 419 } 420 } 421 422 423 status_t 424 arch_int_init(kernel_args* args) 425 { 426 // setup the standard programmable interrupt controller 427 pic_init(); 428 return B_OK; 429 } 430 431 432 status_t 433 arch_int_init_post_vm(kernel_args* args) 434 { 435 // Always init the local apic as it can be used for timers even if we 436 // don't end up using the io apic 437 apic_init(args); 438 return B_OK; 439 } 440 441 442 status_t 443 arch_int_init_io(kernel_args* args) 444 { 445 msi_init(args); 446 ioapic_init(args); 447 return B_OK; 448 } 449 450 451 status_t 452 arch_int_init_post_device_manager(kernel_args* args) 453 { 454 return B_OK; 455 } 456 457 458 void 459 arch_int_set_interrupt_controller(const interrupt_controller& controller) 460 { 461 sCurrentPIC = &controller; 462 } 463