xref: /haiku/src/system/kernel/arch/ppc/arch_int.cpp (revision 0d452c8f34013b611a54c746a71c05e28796eae2)
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 				iframe->srr1 & (1 << 14), // was the system in user or supervisor
168 				&newip);
169 			if (newip != 0) {
170 				// the page fault handler wants us to modify the iframe to set the
171 				// IP the cpu will return to to be this ip
172 				iframe->srr0 = newip;
173 			}
174  			break;
175 		}
176 
177 		case 0x500: // external interrupt
178 		{
179 			if (!sPIC) {
180 				panic("ppc_exception_entry(): external interrupt although we "
181 					"don't have a PIC driver!");
182 				break;
183 			}
184 
185 dprintf("handling I/O interrupts...\n");
186 			int irq;
187 			while ((irq = sPIC->acknowledge_io_interrupt(sPICCookie)) >= 0) {
188 // TODO: correctly pass level-triggered vs. edge-triggered to the handler!
189 				int_io_interrupt_handler(irq, true);
190 			}
191 dprintf("handling I/O interrupts done\n");
192 			break;
193 		}
194 
195 		case 0x600: // alignment exception
196 			panic("alignment exception: unimplemented\n");
197 			break;
198 		case 0x700: // program exception
199 			panic("program exception: unimplemented\n");
200 			break;
201 		case 0x800: // FP unavailable exception
202 			panic("FP unavailable exception: unimplemented\n");
203 			break;
204 		case 0x900: // decrementer exception
205 			timer_interrupt();
206 			break;
207 		case 0xc00: // system call
208 			panic("system call exception: unimplemented\n");
209 			break;
210 		case 0xd00: // trace exception
211 			panic("trace exception: unimplemented\n");
212 			break;
213 		case 0xe00: // FP assist exception
214 			panic("FP assist exception: unimplemented\n");
215 			break;
216 		case 0xf00: // performance monitor exception
217 			panic("performance monitor exception: unimplemented\n");
218 			break;
219 		case 0xf20: // altivec unavailable exception
220 			panic("alitivec unavailable exception: unimplemented\n");
221 			break;
222 		case 0x1000:
223 		case 0x1100:
224 		case 0x1200:
225 			panic("TLB miss exception: unimplemented\n");
226 			break;
227 		case 0x1300: // instruction address exception
228 			panic("instruction address exception: unimplemented\n");
229 			break;
230 		case 0x1400: // system management exception
231 			panic("system management exception: unimplemented\n");
232 			break;
233 		case 0x1600: // altivec assist exception
234 			panic("altivec assist exception: unimplemented\n");
235 			break;
236 		case 0x1700: // thermal management exception
237 			panic("thermal management exception: unimplemented\n");
238 			break;
239 		default:
240 			dprintf("unhandled exception type 0x%x\n", vector);
241 			print_iframe(iframe);
242 			panic("unhandled exception type\n");
243 	}
244 
245 	cpu_status state = disable_interrupts();
246 	if (thread->cpu->invoke_scheduler) {
247 		SpinLocker schedulerLocker(gSchedulerLock);
248 		scheduler_reschedule();
249 		schedulerLocker.Unlock();
250 		restore_interrupts(state);
251 	} else if (thread->post_interrupt_callback != NULL) {
252 		void (*callback)(void*) = thread->post_interrupt_callback;
253 		void* data = thread->post_interrupt_data;
254 
255 		thread->post_interrupt_callback = NULL;
256 		thread->post_interrupt_data = NULL;
257 
258 		restore_interrupts(state);
259 
260 		callback(data);
261 	}
262 
263 	// pop iframe
264 	if (thread)
265 		ppc_pop_iframe(&thread->arch_info.iframes);
266 	else
267 		ppc_pop_iframe(&gBootFrameStack);
268 }
269 
270 
271 status_t
272 arch_int_init(kernel_args *args)
273 {
274 	return B_OK;
275 }
276 
277 
278 status_t
279 arch_int_init_post_vm(kernel_args *args)
280 {
281 	void *handlers = (void *)args->arch_args.exception_handlers.start;
282 
283 	// We may need to remap the exception handler area into the kernel address
284 	// space.
285 	if (!IS_KERNEL_ADDRESS(handlers)) {
286 		addr_t address = (addr_t)handlers;
287 		status_t error = ppc_remap_address_range(&address,
288 			args->arch_args.exception_handlers.size, true);
289 		if (error != B_OK) {
290 			panic("arch_int_init_post_vm(): Failed to remap the exception "
291 				"handler area!");
292 			return error;
293 		}
294 		handlers = (void*)(address);
295 	}
296 
297 	// create a region to map the irq vector code into (physical address 0x0)
298 	area_id exceptionArea = create_area("exception_handlers",
299 		&handlers, B_EXACT_ADDRESS, args->arch_args.exception_handlers.size,
300 		B_ALREADY_WIRED, B_KERNEL_READ_AREA | B_KERNEL_WRITE_AREA);
301 	if (exceptionArea < B_OK)
302 		panic("arch_int_init2: could not create exception handler region\n");
303 
304 	dprintf("exception handlers at %p\n", handlers);
305 
306 	// copy the handlers into this area
307 	memcpy(handlers, &__irqvec_start, args->arch_args.exception_handlers.size);
308 	arch_cpu_sync_icache(handlers, args->arch_args.exception_handlers.size);
309 
310 	// init the CPU exception contexts
311 	int cpuCount = smp_get_num_cpus();
312 	for (int i = 0; i < cpuCount; i++) {
313 		ppc_cpu_exception_context *context = ppc_get_cpu_exception_context(i);
314 		context->kernel_handle_exception = (void*)&ppc_exception_tail;
315 		context->exception_context = context;
316 		// kernel_stack is set when the current thread changes. At this point
317 		// we don't have threads yet.
318 	}
319 
320 	// set the exception context for this CPU
321 	ppc_set_current_cpu_exception_context(ppc_get_cpu_exception_context(0));
322 
323 	return B_OK;
324 }
325 
326 
327 status_t
328 arch_int_init_io(kernel_args* args)
329 {
330 	return B_OK;
331 }
332 
333 
334 template<typename ModuleInfo>
335 struct Module : DoublyLinkedListLinkImpl<Module<ModuleInfo> > {
336 	Module(ModuleInfo *module)
337 		: module(module)
338 	{
339 	}
340 
341 	~Module()
342 	{
343 		if (module)
344 			put_module(((module_info*)module)->name);
345 	}
346 
347 	ModuleInfo	*module;
348 };
349 
350 typedef Module<interrupt_controller_module_info> PICModule;
351 
352 struct PICModuleList : DoublyLinkedList<PICModule> {
353 	~PICModuleList()
354 	{
355 		while (PICModule *module = First()) {
356 			Remove(module);
357 			delete module;
358 		}
359 	}
360 };
361 
362 
363 class DeviceTreeIterator {
364 public:
365 	DeviceTreeIterator(device_manager_info *deviceManager)
366 		: fDeviceManager(deviceManager),
367 		  fNode(NULL),
368 		  fParent(NULL)
369 	{
370 		Rewind();
371 	}
372 
373 	~DeviceTreeIterator()
374 	{
375 		if (fParent != NULL)
376 			fDeviceManager->put_node(fParent);
377 		if (fNode != NULL)
378 			fDeviceManager->put_node(fNode);
379 	}
380 
381 	void Rewind()
382 	{
383 		fNode = fDeviceManager->get_root_node();
384 	}
385 
386 	bool HasNext() const
387 	{
388 		return (fNode != NULL);
389 	}
390 
391 	device_node *Next()
392 	{
393 		if (fNode == NULL)
394 			return NULL;
395 
396 		device_node *foundNode = fNode;
397 
398 		// get first child
399 		device_node *child = NULL;
400 		if (fDeviceManager->get_next_child_node(fNode, NULL, &child)
401 				== B_OK) {
402 			// move to the child node
403 			if (fParent != NULL)
404 				fDeviceManager->put_node(fParent);
405 			fParent = fNode;
406 			fNode = child;
407 
408 		// no more children; backtrack to find the next sibling
409 		} else {
410 			while (fParent != NULL) {
411 				if (fDeviceManager->get_next_child_node(fParent, NULL, &fNode)
412 						== B_OK) {
413 						// get_next_child_node() always puts the node
414 					break;
415 				}
416 				fNode = fParent;
417 				fParent = fDeviceManager->get_parent_node(fNode);
418 			}
419 
420 			// if we hit the root node again, we're done
421 			if (fParent == NULL) {
422 				fDeviceManager->put_node(fNode);
423 				fNode = NULL;
424 			}
425 		}
426 
427 		return foundNode;
428 	}
429 
430 private:
431 	device_manager_info *fDeviceManager;
432 	device_node	*fNode;
433 	device_node	*fParent;
434 };
435 
436 
437 static void
438 get_interrupt_controller_modules(PICModuleList &list)
439 {
440 	const char *namePrefix = "interrupt_controllers/";
441 	size_t namePrefixLen = strlen(namePrefix);
442 
443 	char name[B_PATH_NAME_LENGTH];
444 	size_t length;
445 	uint32 cookie = 0;
446 	while (get_next_loaded_module_name(&cookie, name, &(length = sizeof(name)))
447 			== B_OK) {
448 		// an interrupt controller module?
449 		if (length <= namePrefixLen
450 			|| strncmp(name, namePrefix, namePrefixLen) != 0) {
451 			continue;
452 		}
453 
454 		// get the module
455 		interrupt_controller_module_info *moduleInfo;
456 		if (get_module(name, (module_info**)&moduleInfo) != B_OK)
457 			continue;
458 
459 		// add it to the list
460 		PICModule *module = new(nothrow) PICModule(moduleInfo);
461 		if (!module) {
462 			put_module(((module_info*)moduleInfo)->name);
463 			continue;
464 		}
465 		list.Add(module);
466 	}
467 }
468 
469 
470 static bool
471 probe_pic_device(device_node *node, PICModuleList &picModules)
472 {
473 	for (PICModule *module = picModules.Head();
474 		 module;
475 		 module = picModules.GetNext(module)) {
476 		if (module->module->info.supports_device(node) > 0) {
477 			if (module->module->info.register_device(node) == B_OK)
478 				return true;
479 		}
480 	}
481 
482 	return false;
483 }
484 
485 
486 status_t
487 arch_int_init_post_device_manager(struct kernel_args *args)
488 {
489 	// get the interrupt controller driver modules
490 	PICModuleList picModules;
491 	get_interrupt_controller_modules(picModules);
492 	if (picModules.IsEmpty()) {
493 		panic("arch_int_init_post_device_manager(): Found no PIC modules!");
494 		return B_ENTRY_NOT_FOUND;
495 	}
496 
497 	// get the device manager module
498 	device_manager_info *deviceManager;
499 	status_t error = get_module(B_DEVICE_MANAGER_MODULE_NAME,
500 		(module_info**)&deviceManager);
501 	if (error != B_OK) {
502 		panic("arch_int_init_post_device_manager(): Failed to get device "
503 			"manager: %s", strerror(error));
504 		return error;
505 	}
506 	Module<device_manager_info> _deviceManager(deviceManager);	// auto put
507 
508 	// iterate through the device tree and probe the interrupt controllers
509 	DeviceTreeIterator iterator(deviceManager);
510 	while (device_node *node = iterator.Next())
511 		probe_pic_device(node, picModules);
512 
513 	// iterate through the tree again and get an interrupt controller node
514 	iterator.Rewind();
515 	while (device_node *node = iterator.Next()) {
516 		const char *deviceType;
517 		if (deviceManager->get_attr_string(node, B_DEVICE_TYPE,
518 				&deviceType, false) == B_OK) {
519 			bool isPIC = false;
520 
521 			/*
522 			bool isPIC
523 				= (strcmp(deviceType, B_INTERRUPT_CONTROLLER_DRIVER_TYPE) == 0);
524 			free(deviceType);
525 			*/
526 
527 			if (isPIC) {
528 				driver_module_info *driver;
529 				void *driverCookie;
530 
531 				deviceManager->get_driver(node, (driver_module_info **)&driver, (void **)&driverCookie);
532 
533 				sPIC = (interrupt_controller_module_info *)driver;
534 				sPICCookie = driverCookie;
535 				return B_OK;
536 			}
537 		}
538 	}
539 
540 	// no PIC found
541 	panic("arch_int_init_post_device_manager(): Found no supported PIC!");
542 
543 	return B_ENTRY_NOT_FOUND;
544 }
545 
546 
547 // #pragma mark -
548 
549 struct ppc_cpu_exception_context *
550 ppc_get_cpu_exception_context(int cpu)
551 {
552 	return sCPUExceptionContexts + cpu;
553 }
554 
555 
556 void
557 ppc_set_current_cpu_exception_context(struct ppc_cpu_exception_context *context)
558 {
559 	// translate to physical address
560 	phys_addr_t physicalPage;
561 	addr_t inPageOffset = (addr_t)context & (B_PAGE_SIZE - 1);
562 	status_t error = vm_get_page_mapping(VMAddressSpace::KernelID(),
563 		(addr_t)context - inPageOffset, &physicalPage);
564 	if (error != B_OK) {
565 		panic("ppc_set_current_cpu_exception_context(): Failed to get physical "
566 			"address!");
567 		return;
568 	}
569 
570 	asm volatile("mtsprg0 %0" : : "r"(physicalPage + inPageOffset));
571 }
572 
573