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