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 uintptr_t handler = reinterpret_cast<uintptr_t>(thread->fault_handler); 299 if (thread && thread->fault_handler != 0) { 300 if (frame->ip != handler) { 301 frame->ip = handler; 302 return; 303 } 304 305 // The fault happened at the fault handler address. This is a 306 // certain infinite loop. 307 panic("page fault, interrupts disabled, fault handler loop. " 308 "Touching address %p from ip %p\n", (void*)cr2, 309 (void*)frame->ip); 310 } 311 312 // If we are not running the kernel startup the page fault was not 313 // allowed to happen and we must panic. 314 panic("page fault, but interrupts were disabled. Touching address " 315 "%p from ip %p\n", (void*)cr2, (void*)frame->ip); 316 return; 317 } else if (thread != NULL && thread->page_faults_allowed < 1) { 318 panic("page fault not allowed at this place. Touching address " 319 "%p from ip %p\n", (void*)cr2, (void*)frame->ip); 320 return; 321 } 322 323 enable_interrupts(); 324 325 vm_page_fault(cr2, frame->ip, 326 (frame->error_code & 0x2)!= 0, // write access 327 (frame->error_code & 0x10) != 0, // instruction fetch 328 (frame->error_code & 0x4) != 0, // userland 329 &newip); 330 if (newip != 0) { 331 // the page fault handler wants us to modify the iframe to set the 332 // IP the cpu will return to this ip 333 frame->ip = newip; 334 } 335 } 336 337 338 void 339 x86_set_irq_source(int irq, irq_source source) 340 { 341 sVectorSources[irq] = source; 342 } 343 344 345 // #pragma mark - 346 347 348 void 349 arch_int_enable_io_interrupt(int irq) 350 { 351 sCurrentPIC->enable_io_interrupt(irq); 352 } 353 354 355 void 356 arch_int_disable_io_interrupt(int irq) 357 { 358 sCurrentPIC->disable_io_interrupt(irq); 359 } 360 361 362 void 363 arch_int_configure_io_interrupt(int irq, uint32 config) 364 { 365 sCurrentPIC->configure_io_interrupt(irq, config); 366 } 367 368 369 #undef arch_int_enable_interrupts 370 #undef arch_int_disable_interrupts 371 #undef arch_int_restore_interrupts 372 #undef arch_int_are_interrupts_enabled 373 374 375 void 376 arch_int_enable_interrupts(void) 377 { 378 arch_int_enable_interrupts_inline(); 379 } 380 381 382 int 383 arch_int_disable_interrupts(void) 384 { 385 return arch_int_disable_interrupts_inline(); 386 } 387 388 389 void 390 arch_int_restore_interrupts(int oldState) 391 { 392 arch_int_restore_interrupts_inline(oldState); 393 } 394 395 396 bool 397 arch_int_are_interrupts_enabled(void) 398 { 399 return arch_int_are_interrupts_enabled_inline(); 400 } 401 402 403 void 404 arch_int_assign_to_cpu(int32 irq, int32 cpu) 405 { 406 switch (sVectorSources[irq]) { 407 case IRQ_SOURCE_IOAPIC: 408 if (sCurrentPIC->assign_interrupt_to_cpu != NULL) 409 sCurrentPIC->assign_interrupt_to_cpu(irq, cpu); 410 break; 411 412 case IRQ_SOURCE_MSI: 413 msi_assign_interrupt_to_cpu(irq, cpu); 414 break; 415 416 default: 417 break; 418 } 419 } 420 421 422 status_t 423 arch_int_init(kernel_args* args) 424 { 425 // setup the standard programmable interrupt controller 426 pic_init(); 427 return B_OK; 428 } 429 430 431 status_t 432 arch_int_init_post_vm(kernel_args* args) 433 { 434 // Always init the local apic as it can be used for timers even if we 435 // don't end up using the io apic 436 apic_init(args); 437 return B_OK; 438 } 439 440 441 status_t 442 arch_int_init_io(kernel_args* args) 443 { 444 msi_init(args); 445 ioapic_init(args); 446 return B_OK; 447 } 448 449 450 status_t 451 arch_int_init_post_device_manager(kernel_args* args) 452 { 453 return B_OK; 454 } 455 456 457 void 458 arch_int_set_interrupt_controller(const interrupt_controller& controller) 459 { 460 sCurrentPIC = &controller; 461 } 462