1 /* 2 * Copyright 2003-2012, 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 * François Revol <revol@free.fr> 9 * Ithamar R. Adema <ithamar@upgrade-android.com> 10 * 11 * Copyright 2001, Travis Geiselbrecht. All rights reserved. 12 * Distributed under the terms of the NewOS License. 13 */ 14 15 16 #include <int.h> 17 18 #include <arch/smp.h> 19 #include <boot/kernel_args.h> 20 #include <device_manager.h> 21 #include <kscheduler.h> 22 #include <interrupt_controller.h> 23 #include <smp.h> 24 #include <thread.h> 25 #include <timer.h> 26 #include <util/DoublyLinkedList.h> 27 #include <util/kernel_cpp.h> 28 #include <vm/vm.h> 29 #include <vm/vm_priv.h> 30 #include <vm/VMAddressSpace.h> 31 #include <string.h> 32 33 #include <drivers/bus/FDT.h> 34 #include "soc.h" 35 36 #include "soc_pxa.h" 37 #include "soc_omap3.h" 38 39 #define TRACE_ARCH_INT 40 #ifdef TRACE_ARCH_INT 41 # define TRACE(x) dprintf x 42 #else 43 # define TRACE(x) ; 44 #endif 45 46 #define VECTORPAGE_SIZE 64 47 #define USER_VECTOR_ADDR_LOW 0x00000000 48 #define USER_VECTOR_ADDR_HIGH 0xffff0000 49 50 extern int _vectors_start; 51 extern int _vectors_end; 52 53 static area_id sVectorPageArea; 54 static void *sVectorPageAddress; 55 static area_id sUserVectorPageArea; 56 static void *sUserVectorPageAddress; 57 static fdt_module_info *sFdtModule; 58 59 // An iframe stack used in the early boot process when we don't have 60 // threads yet. 61 struct iframe_stack gBootFrameStack; 62 63 64 void 65 arch_int_enable_io_interrupt(int irq) 66 { 67 TRACE(("arch_int_enable_io_interrupt(%d)\n", irq)); 68 InterruptController *ic = InterruptController::Get(); 69 if (ic != NULL) 70 ic->EnableInterrupt(irq); 71 } 72 73 74 void 75 arch_int_disable_io_interrupt(int irq) 76 { 77 TRACE(("arch_int_disable_io_interrupt(%d)\n", irq)); 78 InterruptController *ic = InterruptController::Get(); 79 if (ic != NULL) 80 ic->DisableInterrupt(irq); 81 } 82 83 84 /* arch_int_*_interrupts() and friends are in arch_asm.S */ 85 86 void 87 arch_int_assign_to_cpu(int32 irq, int32 cpu) 88 { 89 // intentionally left blank; no SMP support (yet) 90 } 91 92 static void 93 print_iframe(const char *event, struct iframe *frame) 94 { 95 if (event) 96 dprintf("Exception: %s\n", event); 97 98 dprintf("R00=%08lx R01=%08lx R02=%08lx R03=%08lx\n" 99 "R04=%08lx R05=%08lx R06=%08lx R07=%08lx\n", 100 frame->r0, frame->r1, frame->r2, frame->r3, 101 frame->r4, frame->r5, frame->r6, frame->r7); 102 dprintf("R08=%08lx R09=%08lx R10=%08lx R11=%08lx\n" 103 "R12=%08lx SP=%08lx LR=%08lx PC=%08lx CPSR=%08lx\n", 104 frame->r8, frame->r9, frame->r10, frame->r11, 105 frame->r12, frame->svc_sp, frame->svc_lr, frame->pc, frame->spsr); 106 } 107 108 109 status_t 110 arch_int_init(kernel_args *args) 111 { 112 return B_OK; 113 } 114 115 116 extern "C" void arm_vector_init(void); 117 118 119 static struct fdt_device_info intc_table[] = { 120 { 121 .compatible = "marvell,pxa-intc", 122 .init = PXAInterruptController::Init, 123 }, { 124 .compatible = "ti,omap3-intc", 125 .init = OMAP3InterruptController::Init, 126 } 127 }; 128 static int intc_count = sizeof(intc_table) / sizeof(struct fdt_device_info); 129 130 131 status_t 132 arch_int_init_post_vm(kernel_args *args) 133 { 134 // create a read/write kernel area 135 sVectorPageArea = create_area("vectorpage", (void **)&sVectorPageAddress, 136 B_ANY_ADDRESS, VECTORPAGE_SIZE, B_FULL_LOCK, 137 B_KERNEL_WRITE_AREA | B_KERNEL_READ_AREA); 138 if (sVectorPageArea < 0) 139 panic("vector page could not be created!"); 140 141 // clone it at a fixed address with user read/only permissions 142 sUserVectorPageAddress = (addr_t*)USER_VECTOR_ADDR_HIGH; 143 sUserVectorPageArea = clone_area("user_vectorpage", 144 (void **)&sUserVectorPageAddress, B_EXACT_ADDRESS, 145 B_READ_AREA | B_EXECUTE_AREA, sVectorPageArea); 146 147 if (sUserVectorPageArea < 0) 148 panic("user vector page @ %p could not be created (%lx)!", sVectorPageAddress, sUserVectorPageArea); 149 150 // copy vectors into the newly created area 151 memcpy(sVectorPageAddress, &_vectors_start, VECTORPAGE_SIZE); 152 153 arm_vector_init(); 154 155 // see if high vectors are enabled 156 if ((mmu_read_c1() & (1 << 13)) != 0) 157 dprintf("High vectors already enabled\n"); 158 else { 159 mmu_write_c1(mmu_read_c1() | (1 << 13)); 160 161 if ((mmu_read_c1() & (1 << 13)) == 0) 162 dprintf("Unable to enable high vectors!\n"); 163 else 164 dprintf("Enabled high vectors\n"); 165 } 166 167 status_t rc = get_module(B_FDT_MODULE_NAME, (module_info**)&sFdtModule); 168 if (rc != B_OK) 169 panic("Unable to get FDT module: %08lx!\n", rc); 170 171 rc = sFdtModule->setup_devices(intc_table, intc_count, NULL); 172 if (rc != B_OK) 173 panic("No interrupt controllers found!\n"); 174 175 return B_OK; 176 } 177 178 179 status_t 180 arch_int_init_io(kernel_args* args) 181 { 182 return B_OK; 183 } 184 185 186 status_t 187 arch_int_init_post_device_manager(struct kernel_args *args) 188 { 189 return B_ENTRY_NOT_FOUND; 190 } 191 192 193 // Little helper class for handling the 194 // iframe stack as used by KDL. 195 class IFrameScope { 196 public: 197 IFrameScope(struct iframe *iframe) { 198 fThread = thread_get_current_thread(); 199 if (fThread) 200 arm_push_iframe(&fThread->arch_info.iframes, iframe); 201 else 202 arm_push_iframe(&gBootFrameStack, iframe); 203 } 204 205 virtual ~IFrameScope() { 206 // pop iframe 207 if (fThread) 208 arm_pop_iframe(&fThread->arch_info.iframes); 209 else 210 arm_pop_iframe(&gBootFrameStack); 211 } 212 private: 213 Thread* fThread; 214 }; 215 216 217 extern "C" void 218 arch_arm_undefined(struct iframe *iframe) 219 { 220 print_iframe("Undefined Instruction", iframe); 221 IFrameScope scope(iframe); // push/pop iframe 222 223 panic("not handled!"); 224 } 225 226 227 extern "C" void 228 arch_arm_syscall(struct iframe *iframe) 229 { 230 print_iframe("Software interrupt", iframe); 231 IFrameScope scope(iframe); // push/pop iframe 232 } 233 234 235 extern "C" void 236 arch_arm_data_abort(struct iframe *frame) 237 { 238 Thread *thread = thread_get_current_thread(); 239 bool isUser = (frame->spsr & 0x1f) == 0x10; 240 addr_t far = arm_get_far(); 241 bool isWrite = true; 242 addr_t newip = 0; 243 244 #ifdef TRACE_ARCH_INT 245 print_iframe("Data Abort", frame); 246 dprintf("FAR: %08lx, thread: %s\n", far, thread->name); 247 #endif 248 249 IFrameScope scope(frame); 250 251 if (debug_debugger_running()) { 252 // If this CPU or this thread has a fault handler, we're allowed to be 253 // here. 254 if (thread != NULL) { 255 cpu_ent* cpu = &gCPU[smp_get_current_cpu()]; 256 257 if (cpu->fault_handler != 0) { 258 debug_set_page_fault_info(far, frame->pc, 259 isWrite ? DEBUG_PAGE_FAULT_WRITE : 0); 260 frame->svc_sp = cpu->fault_handler_stack_pointer; 261 frame->pc = cpu->fault_handler; 262 return; 263 } 264 265 if (thread->fault_handler != 0) { 266 kprintf("ERROR: thread::fault_handler used in kernel " 267 "debugger!\n"); 268 debug_set_page_fault_info(far, frame->pc, 269 isWrite ? DEBUG_PAGE_FAULT_WRITE : 0); 270 frame->pc = reinterpret_cast<uintptr_t>(thread->fault_handler); 271 return; 272 } 273 } 274 275 // otherwise, not really 276 panic("page fault in debugger without fault handler! Touching " 277 "address %p from pc %p\n", (void *)far, (void *)frame->pc); 278 return; 279 } else if ((frame->spsr & (1 << 7)) != 0) { 280 // interrupts disabled 281 282 // If a page fault handler is installed, we're allowed to be here. 283 // TODO: Now we are generally allowing user_memcpy() with interrupts 284 // disabled, which in most cases is a bug. We should add some thread 285 // flag allowing to explicitly indicate that this handling is desired. 286 uintptr_t handler = reinterpret_cast<uintptr_t>(thread->fault_handler); 287 if (thread && thread->fault_handler != 0) { 288 if (frame->pc != handler) { 289 frame->pc = handler; 290 return; 291 } 292 293 // The fault happened at the fault handler address. This is a 294 // certain infinite loop. 295 panic("page fault, interrupts disabled, fault handler loop. " 296 "Touching address %p from pc %p\n", (void*)far, 297 (void*)frame->pc); 298 } 299 300 // If we are not running the kernel startup the page fault was not 301 // allowed to happen and we must panic. 302 panic("page fault, but interrupts were disabled. Touching address " 303 "%p from pc %p\n", (void *)far, (void *)frame->pc); 304 return; 305 } else if (thread != NULL && thread->page_faults_allowed < 1) { 306 panic("page fault not allowed at this place. Touching address " 307 "%p from pc %p\n", (void *)far, (void *)frame->pc); 308 return; 309 } 310 311 enable_interrupts(); 312 313 vm_page_fault(far, frame->pc, isWrite, false, isUser, &newip); 314 315 if (newip != 0) { 316 // the page fault handler wants us to modify the iframe to set the 317 // IP the cpu will return to to be this ip 318 frame->pc = newip; 319 } 320 } 321 322 323 extern "C" void 324 arch_arm_prefetch_abort(struct iframe *iframe) 325 { 326 print_iframe("Prefetch Abort", iframe); 327 IFrameScope scope(iframe); 328 329 panic("not handled!"); 330 } 331 332 333 extern "C" void 334 arch_arm_irq(struct iframe *iframe) 335 { 336 IFrameScope scope(iframe); 337 338 InterruptController *ic = InterruptController::Get(); 339 if (ic != NULL) 340 ic->HandleInterrupt(); 341 } 342 343 344 extern "C" void 345 arch_arm_fiq(struct iframe *iframe) 346 { 347 IFrameScope scope(iframe); 348 349 panic("FIQ not implemented yet!"); 350 } 351