1 /* 2 * Copyright 2003-2011, Haiku Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Axel Dörfler <axeld@pinc-software.de> 7 * Ingo Weinhold <bonefish@cs.tu-berlin.de> 8 * 9 * Copyright 2001, Travis Geiselbrecht. All rights reserved. 10 * Distributed under the terms of the NewOS License. 11 */ 12 13 14 #include <int.h> 15 16 #include <arch/smp.h> 17 #include <boot/kernel_args.h> 18 #include <device_manager.h> 19 #include <kscheduler.h> 20 #include <interrupt_controller.h> 21 #include <smp.h> 22 #include <thread.h> 23 #include <timer.h> 24 #include <util/AutoLock.h> 25 #include <util/DoublyLinkedList.h> 26 #include <util/kernel_cpp.h> 27 #include <vm/vm.h> 28 #include <vm/vm_priv.h> 29 #include <vm/VMAddressSpace.h> 30 31 #include <string.h> 32 33 34 // defined in arch_exceptions.S 35 extern int __irqvec_start; 36 extern int __irqvec_end; 37 38 extern"C" void ppc_exception_tail(void); 39 40 41 // the exception contexts for all CPUs 42 static ppc_cpu_exception_context sCPUExceptionContexts[SMP_MAX_CPUS]; 43 44 45 // An iframe stack used in the early boot process when we don't have 46 // threads yet. 47 struct iframe_stack gBootFrameStack; 48 49 // interrupt controller interface (initialized 50 // in arch_int_init_post_device_manager()) 51 static struct interrupt_controller_module_info *sPIC; 52 static void *sPICCookie; 53 54 55 void 56 arch_int_enable_io_interrupt(int irq) 57 { 58 if (!sPIC) 59 return; 60 61 // TODO: I have no idea, what IRQ type is appropriate. 62 sPIC->enable_io_interrupt(sPICCookie, irq, IRQ_TYPE_LEVEL); 63 } 64 65 66 void 67 arch_int_disable_io_interrupt(int irq) 68 { 69 if (!sPIC) 70 return; 71 72 sPIC->disable_io_interrupt(sPICCookie, irq); 73 } 74 75 76 /* arch_int_*_interrupts() and friends are in arch_asm.S */ 77 78 79 static void 80 print_iframe(struct iframe *frame) 81 { 82 dprintf("iframe at %p:\n", frame); 83 dprintf("r0-r3: 0x%08lx 0x%08lx 0x%08lx 0x%08lx\n", frame->r0, frame->r1, frame->r2, frame->r3); 84 dprintf("r4-r7: 0x%08lx 0x%08lx 0x%08lx 0x%08lx\n", frame->r4, frame->r5, frame->r6, frame->r7); 85 dprintf("r8-r11: 0x%08lx 0x%08lx 0x%08lx 0x%08lx\n", frame->r8, frame->r9, frame->r10, frame->r11); 86 dprintf("r12-r15: 0x%08lx 0x%08lx 0x%08lx 0x%08lx\n", frame->r12, frame->r13, frame->r14, frame->r15); 87 dprintf("r16-r19: 0x%08lx 0x%08lx 0x%08lx 0x%08lx\n", frame->r16, frame->r17, frame->r18, frame->r19); 88 dprintf("r20-r23: 0x%08lx 0x%08lx 0x%08lx 0x%08lx\n", frame->r20, frame->r21, frame->r22, frame->r23); 89 dprintf("r24-r27: 0x%08lx 0x%08lx 0x%08lx 0x%08lx\n", frame->r24, frame->r25, frame->r26, frame->r27); 90 dprintf("r28-r31: 0x%08lx 0x%08lx 0x%08lx 0x%08lx\n", frame->r28, frame->r29, frame->r30, frame->r31); 91 dprintf(" ctr 0x%08lx xer 0x%08lx\n", frame->ctr, frame->xer); 92 dprintf(" cr 0x%08lx lr 0x%08lx\n", frame->cr, frame->lr); 93 dprintf(" dsisr 0x%08lx dar 0x%08lx\n", frame->dsisr, frame->dar); 94 dprintf(" srr1 0x%08lx srr0 0x%08lx\n", frame->srr1, frame->srr0); 95 } 96 97 98 extern "C" void ppc_exception_entry(int vector, struct iframe *iframe); 99 void 100 ppc_exception_entry(int vector, struct iframe *iframe) 101 { 102 if (vector != 0x900) { 103 dprintf("ppc_exception_entry: time %lld vector 0x%x, iframe %p, " 104 "srr0: %p\n", system_time(), vector, iframe, (void*)iframe->srr0); 105 } 106 107 Thread *thread = thread_get_current_thread(); 108 109 // push iframe 110 if (thread) 111 ppc_push_iframe(&thread->arch_info.iframes, iframe); 112 else 113 ppc_push_iframe(&gBootFrameStack, iframe); 114 115 switch (vector) { 116 case 0x100: // system reset 117 panic("system reset exception\n"); 118 break; 119 case 0x200: // machine check 120 panic("machine check exception\n"); 121 break; 122 case 0x300: // DSI 123 case 0x400: // ISI 124 { 125 bool kernelDebugger = debug_debugger_running(); 126 127 if (kernelDebugger) { 128 // if this CPU or this thread has a fault handler, 129 // we're allowed to be here 130 cpu_ent* cpu = &gCPU[smp_get_current_cpu()]; 131 if (cpu->fault_handler != 0) { 132 iframe->srr0 = cpu->fault_handler; 133 iframe->r1 = cpu->fault_handler_stack_pointer; 134 break; 135 } 136 Thread *thread = thread_get_current_thread(); 137 if (thread && thread->fault_handler != 0) { 138 iframe->srr0 = thread->fault_handler; 139 break; 140 } 141 142 // otherwise, not really 143 panic("page fault in debugger without fault handler! Touching " 144 "address %p from ip %p\n", (void *)iframe->dar, 145 (void *)iframe->srr0); 146 break; 147 } else if ((iframe->srr1 & MSR_EXCEPTIONS_ENABLED) == 0) { 148 // if the interrupts were disabled, and we are not running the 149 // kernel startup the page fault was not allowed to happen and 150 // we must panic 151 panic("page fault, but interrupts were disabled. Touching " 152 "address %p from ip %p\n", (void *)iframe->dar, 153 (void *)iframe->srr0); 154 break; 155 } else if (thread != NULL && thread->page_faults_allowed < 1) { 156 panic("page fault not allowed at this place. Touching address " 157 "%p from ip %p\n", (void *)iframe->dar, 158 (void *)iframe->srr0); 159 } 160 161 enable_interrupts(); 162 163 addr_t newip; 164 165 vm_page_fault(iframe->dar, iframe->srr0, 166 iframe->dsisr & (1 << 25), // store or load 167 false, 168 iframe->srr1 & (1 << 14), // was the system in user or supervisor 169 &newip); 170 if (newip != 0) { 171 // the page fault handler wants us to modify the iframe to set the 172 // IP the cpu will return to to be this ip 173 iframe->srr0 = newip; 174 } 175 break; 176 } 177 178 case 0x500: // external interrupt 179 { 180 if (!sPIC) { 181 panic("ppc_exception_entry(): external interrupt although we " 182 "don't have a PIC driver!"); 183 break; 184 } 185 186 dprintf("handling I/O interrupts...\n"); 187 int irq; 188 while ((irq = sPIC->acknowledge_io_interrupt(sPICCookie)) >= 0) { 189 // TODO: correctly pass level-triggered vs. edge-triggered to the handler! 190 int_io_interrupt_handler(irq, true); 191 } 192 dprintf("handling I/O interrupts done\n"); 193 break; 194 } 195 196 case 0x600: // alignment exception 197 panic("alignment exception: unimplemented\n"); 198 break; 199 case 0x700: // program exception 200 panic("program exception: unimplemented\n"); 201 break; 202 case 0x800: // FP unavailable exception 203 panic("FP unavailable exception: unimplemented\n"); 204 break; 205 case 0x900: // decrementer exception 206 timer_interrupt(); 207 break; 208 case 0xc00: // system call 209 panic("system call exception: unimplemented\n"); 210 break; 211 case 0xd00: // trace exception 212 panic("trace exception: unimplemented\n"); 213 break; 214 case 0xe00: // FP assist exception 215 panic("FP assist exception: unimplemented\n"); 216 break; 217 case 0xf00: // performance monitor exception 218 panic("performance monitor exception: unimplemented\n"); 219 break; 220 case 0xf20: // altivec unavailable exception 221 panic("alitivec unavailable exception: unimplemented\n"); 222 break; 223 case 0x1000: 224 case 0x1100: 225 case 0x1200: 226 panic("TLB miss exception: unimplemented\n"); 227 break; 228 case 0x1300: // instruction address exception 229 panic("instruction address exception: unimplemented\n"); 230 break; 231 case 0x1400: // system management exception 232 panic("system management exception: unimplemented\n"); 233 break; 234 case 0x1600: // altivec assist exception 235 panic("altivec assist exception: unimplemented\n"); 236 break; 237 case 0x1700: // thermal management exception 238 panic("thermal management exception: unimplemented\n"); 239 break; 240 default: 241 dprintf("unhandled exception type 0x%x\n", vector); 242 print_iframe(iframe); 243 panic("unhandled exception type\n"); 244 } 245 246 cpu_status state = disable_interrupts(); 247 if (thread->cpu->invoke_scheduler) { 248 SpinLocker schedulerLocker(gSchedulerLock); 249 scheduler_reschedule(); 250 schedulerLocker.Unlock(); 251 restore_interrupts(state); 252 } else if (thread->post_interrupt_callback != NULL) { 253 void (*callback)(void*) = thread->post_interrupt_callback; 254 void* data = thread->post_interrupt_data; 255 256 thread->post_interrupt_callback = NULL; 257 thread->post_interrupt_data = NULL; 258 259 restore_interrupts(state); 260 261 callback(data); 262 } 263 264 // pop iframe 265 if (thread) 266 ppc_pop_iframe(&thread->arch_info.iframes); 267 else 268 ppc_pop_iframe(&gBootFrameStack); 269 } 270 271 272 status_t 273 arch_int_init(kernel_args *args) 274 { 275 return B_OK; 276 } 277 278 279 status_t 280 arch_int_init_post_vm(kernel_args *args) 281 { 282 void *handlers = (void *)args->arch_args.exception_handlers.start; 283 284 // We may need to remap the exception handler area into the kernel address 285 // space. 286 if (!IS_KERNEL_ADDRESS(handlers)) { 287 addr_t address = (addr_t)handlers; 288 status_t error = ppc_remap_address_range(&address, 289 args->arch_args.exception_handlers.size, true); 290 if (error != B_OK) { 291 panic("arch_int_init_post_vm(): Failed to remap the exception " 292 "handler area!"); 293 return error; 294 } 295 handlers = (void*)(address); 296 } 297 298 // create a region to map the irq vector code into (physical address 0x0) 299 area_id exceptionArea = create_area("exception_handlers", 300 &handlers, B_EXACT_ADDRESS, args->arch_args.exception_handlers.size, 301 B_ALREADY_WIRED, B_KERNEL_READ_AREA | B_KERNEL_WRITE_AREA); 302 if (exceptionArea < B_OK) 303 panic("arch_int_init2: could not create exception handler region\n"); 304 305 dprintf("exception handlers at %p\n", handlers); 306 307 // copy the handlers into this area 308 memcpy(handlers, &__irqvec_start, args->arch_args.exception_handlers.size); 309 arch_cpu_sync_icache(handlers, args->arch_args.exception_handlers.size); 310 311 // init the CPU exception contexts 312 int cpuCount = smp_get_num_cpus(); 313 for (int i = 0; i < cpuCount; i++) { 314 ppc_cpu_exception_context *context = ppc_get_cpu_exception_context(i); 315 context->kernel_handle_exception = (void*)&ppc_exception_tail; 316 context->exception_context = context; 317 // kernel_stack is set when the current thread changes. At this point 318 // we don't have threads yet. 319 } 320 321 // set the exception context for this CPU 322 ppc_set_current_cpu_exception_context(ppc_get_cpu_exception_context(0)); 323 324 return B_OK; 325 } 326 327 328 status_t 329 arch_int_init_io(kernel_args* args) 330 { 331 return B_OK; 332 } 333 334 335 template<typename ModuleInfo> 336 struct Module : DoublyLinkedListLinkImpl<Module<ModuleInfo> > { 337 Module(ModuleInfo *module) 338 : module(module) 339 { 340 } 341 342 ~Module() 343 { 344 if (module) 345 put_module(((module_info*)module)->name); 346 } 347 348 ModuleInfo *module; 349 }; 350 351 typedef Module<interrupt_controller_module_info> PICModule; 352 353 struct PICModuleList : DoublyLinkedList<PICModule> { 354 ~PICModuleList() 355 { 356 while (PICModule *module = First()) { 357 Remove(module); 358 delete module; 359 } 360 } 361 }; 362 363 364 class DeviceTreeIterator { 365 public: 366 DeviceTreeIterator(device_manager_info *deviceManager) 367 : fDeviceManager(deviceManager), 368 fNode(NULL), 369 fParent(NULL) 370 { 371 Rewind(); 372 } 373 374 ~DeviceTreeIterator() 375 { 376 if (fParent != NULL) 377 fDeviceManager->put_node(fParent); 378 if (fNode != NULL) 379 fDeviceManager->put_node(fNode); 380 } 381 382 void Rewind() 383 { 384 fNode = fDeviceManager->get_root_node(); 385 } 386 387 bool HasNext() const 388 { 389 return (fNode != NULL); 390 } 391 392 device_node *Next() 393 { 394 if (fNode == NULL) 395 return NULL; 396 397 device_node *foundNode = fNode; 398 399 // get first child 400 device_node *child = NULL; 401 if (fDeviceManager->get_next_child_node(fNode, NULL, &child) 402 == B_OK) { 403 // move to the child node 404 if (fParent != NULL) 405 fDeviceManager->put_node(fParent); 406 fParent = fNode; 407 fNode = child; 408 409 // no more children; backtrack to find the next sibling 410 } else { 411 while (fParent != NULL) { 412 if (fDeviceManager->get_next_child_node(fParent, NULL, &fNode) 413 == B_OK) { 414 // get_next_child_node() always puts the node 415 break; 416 } 417 fNode = fParent; 418 fParent = fDeviceManager->get_parent_node(fNode); 419 } 420 421 // if we hit the root node again, we're done 422 if (fParent == NULL) { 423 fDeviceManager->put_node(fNode); 424 fNode = NULL; 425 } 426 } 427 428 return foundNode; 429 } 430 431 private: 432 device_manager_info *fDeviceManager; 433 device_node *fNode; 434 device_node *fParent; 435 }; 436 437 438 static void 439 get_interrupt_controller_modules(PICModuleList &list) 440 { 441 const char *namePrefix = "interrupt_controllers/"; 442 size_t namePrefixLen = strlen(namePrefix); 443 444 char name[B_PATH_NAME_LENGTH]; 445 size_t length; 446 uint32 cookie = 0; 447 while (get_next_loaded_module_name(&cookie, name, &(length = sizeof(name))) 448 == B_OK) { 449 // an interrupt controller module? 450 if (length <= namePrefixLen 451 || strncmp(name, namePrefix, namePrefixLen) != 0) { 452 continue; 453 } 454 455 // get the module 456 interrupt_controller_module_info *moduleInfo; 457 if (get_module(name, (module_info**)&moduleInfo) != B_OK) 458 continue; 459 460 // add it to the list 461 PICModule *module = new(nothrow) PICModule(moduleInfo); 462 if (!module) { 463 put_module(((module_info*)moduleInfo)->name); 464 continue; 465 } 466 list.Add(module); 467 } 468 } 469 470 471 static bool 472 probe_pic_device(device_node *node, PICModuleList &picModules) 473 { 474 for (PICModule *module = picModules.Head(); 475 module; 476 module = picModules.GetNext(module)) { 477 if (module->module->info.supports_device(node) > 0) { 478 if (module->module->info.register_device(node) == B_OK) 479 return true; 480 } 481 } 482 483 return false; 484 } 485 486 487 status_t 488 arch_int_init_post_device_manager(struct kernel_args *args) 489 { 490 // get the interrupt controller driver modules 491 PICModuleList picModules; 492 get_interrupt_controller_modules(picModules); 493 if (picModules.IsEmpty()) { 494 panic("arch_int_init_post_device_manager(): Found no PIC modules!"); 495 return B_ENTRY_NOT_FOUND; 496 } 497 498 // get the device manager module 499 device_manager_info *deviceManager; 500 status_t error = get_module(B_DEVICE_MANAGER_MODULE_NAME, 501 (module_info**)&deviceManager); 502 if (error != B_OK) { 503 panic("arch_int_init_post_device_manager(): Failed to get device " 504 "manager: %s", strerror(error)); 505 return error; 506 } 507 Module<device_manager_info> _deviceManager(deviceManager); // auto put 508 509 // iterate through the device tree and probe the interrupt controllers 510 DeviceTreeIterator iterator(deviceManager); 511 while (device_node *node = iterator.Next()) 512 probe_pic_device(node, picModules); 513 514 // iterate through the tree again and get an interrupt controller node 515 iterator.Rewind(); 516 while (device_node *node = iterator.Next()) { 517 const char *deviceType; 518 if (deviceManager->get_attr_string(node, B_DEVICE_TYPE, 519 &deviceType, false) == B_OK) { 520 bool isPIC = false; 521 522 /* 523 bool isPIC 524 = (strcmp(deviceType, B_INTERRUPT_CONTROLLER_DRIVER_TYPE) == 0); 525 free(deviceType); 526 */ 527 528 if (isPIC) { 529 driver_module_info *driver; 530 void *driverCookie; 531 532 deviceManager->get_driver(node, (driver_module_info **)&driver, (void **)&driverCookie); 533 534 sPIC = (interrupt_controller_module_info *)driver; 535 sPICCookie = driverCookie; 536 return B_OK; 537 } 538 } 539 } 540 541 // no PIC found 542 panic("arch_int_init_post_device_manager(): Found no supported PIC!"); 543 544 return B_ENTRY_NOT_FOUND; 545 } 546 547 548 // #pragma mark - 549 550 struct ppc_cpu_exception_context * 551 ppc_get_cpu_exception_context(int cpu) 552 { 553 return sCPUExceptionContexts + cpu; 554 } 555 556 557 void 558 ppc_set_current_cpu_exception_context(struct ppc_cpu_exception_context *context) 559 { 560 // translate to physical address 561 phys_addr_t physicalPage; 562 addr_t inPageOffset = (addr_t)context & (B_PAGE_SIZE - 1); 563 status_t error = vm_get_page_mapping(VMAddressSpace::KernelID(), 564 (addr_t)context - inPageOffset, &physicalPage); 565 if (error != B_OK) { 566 panic("ppc_set_current_cpu_exception_context(): Failed to get physical " 567 "address!"); 568 return; 569 } 570 571 asm volatile("mtsprg0 %0" : : "r"(physicalPage + inPageOffset)); 572 } 573 574