xref: /haiku/src/system/kernel/arch/m68k/arch_int.cpp (revision 60b11851d49669095452371787cdf21357d9733a)
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  * 		François Revol <revol@free.fr>
9  * Distributed under the terms of the MIT License.
10  *
11  *
12  * Copyright 2001, Travis Geiselbrecht. All rights reserved.
13  * Distributed under the terms of the NewOS License.
14  */
15 
16 #include <int.h>
17 
18 #include <arch_platform.h>
19 #include <arch/smp.h>
20 #include <boot/kernel_args.h>
21 #include <device_manager.h>
22 #include <kscheduler.h>
23 #include <interrupt_controller.h>
24 #include <smp.h>
25 #include <thread.h>
26 #include <timer.h>
27 #include <util/DoublyLinkedList.h>
28 #include <util/kernel_cpp.h>
29 #include <vm.h>
30 #include <vm_address_space.h>
31 #include <vm_priv.h>
32 #include <string.h>
33 
34 #warning M68K: writeme!
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 typedef void (*m68k_exception_handler)(void);
45 #define M68K_EXCEPTION_VECTOR_COUNT 256
46 #warning M68K: align on 4 ?
47 //m68k_exception_handler gExceptionVectors[M68K_EXCEPTION_VECTOR_COUNT];
48 m68k_exception_handler *gExceptionVectors;
49 
50 // defined in arch_exceptions.S
51 extern "C" void __m68k_exception_noop(void);
52 extern "C" void __m68k_exception_common(void);
53 
54 extern int __irqvec_start;
55 extern int __irqvec_end;
56 
57 extern"C" void m68k_exception_tail(void);
58 
59 // current fault handler
60 addr_t gFaultHandler;
61 
62 // An iframe stack used in the early boot process when we don't have
63 // threads yet.
64 struct iframe_stack gBootFrameStack;
65 
66 // interrupt controller interface (initialized
67 // in arch_int_init_post_device_manager())
68 //static struct interrupt_controller_module_info *sPIC;
69 //static void *sPICCookie;
70 
71 
72 void
73 arch_int_enable_io_interrupt(int irq)
74 {
75 	//if (!sPIC)
76 	//	return;
77 
78 	// TODO: I have no idea, what IRQ type is appropriate.
79 	//sPIC->enable_io_interrupt(sPICCookie, irq, IRQ_TYPE_LEVEL);
80 	M68KPlatform::Default()->EnableIOInterrupt(irq);
81 }
82 
83 
84 void
85 arch_int_disable_io_interrupt(int irq)
86 {
87 	//if (!sPIC)
88 	//	return;
89 
90 	//sPIC->disable_io_interrupt(sPICCookie, irq);
91 	M68KPlatform::Default()->DisableIOInterrupt(irq);
92 }
93 
94 
95 /* arch_int_*_interrupts() and friends are in arch_asm.S */
96 
97 
98 static void
99 print_iframe(struct iframe *frame)
100 {
101 	dprintf("iframe at %p:\n", frame);
102 	dprintf("   d0 0x%08lx    d1 0x%08lx    d2 0x%08lx    d3 0x%08lx\n",
103 				frame->d[0], frame->d[1], frame->d[2], frame->d[3]);
104 			kprintf("   d4 0x%08lx    d5 0x%08lx    d6 0x%08lx    d7 0x%08lx\n",
105 				frame->d[4], frame->d[5], frame->d[6], frame->d[7]);
106 			kprintf("   a0 0x%08lx    a1 0x%08lx    a2 0x%08lx    a3 0x%08lx\n",
107 				frame->a[0], frame->a[1], frame->a[2], frame->a[3]);
108 			kprintf("   a4 0x%08lx    a5 0x%08lx    a6 0x%08lx    "/*"a7 0x%08lx (sp)"*/"\n",
109 				frame->a[4], frame->a[5], frame->a[6]/*, frame->a[7]*/);
110 
111 			/*kprintf("   pc 0x%08lx   ccr 0x%02x\n",
112 			  frame->pc, frame->ccr);*/
113 			kprintf("   pc 0x%08lx        sr 0x%04x\n",
114 				frame->cpu.pc, frame->cpu.sr);
115 #if 0
116 	dprintf("r0-r3:   0x%08lx 0x%08lx 0x%08lx 0x%08lx\n", frame->d0, frame->d1, frame->d2, frame->d3);
117 	dprintf("r4-r7:   0x%08lx 0x%08lx 0x%08lx 0x%08lx\n", frame->d4, frame->d5, frame->d6, frame->d7);
118 	dprintf("r8-r11:  0x%08lx 0x%08lx 0x%08lx 0x%08lx\n", frame->a0, frame->a1, frame->a2, frame->a3);
119 	dprintf("r12-r15: 0x%08lx 0x%08lx 0x%08lx 0x%08lx\n", frame->a4, frame->a5, frame->a6, frame->a7);
120 	dprintf("      pc 0x%08lx         sr 0x%08lx\n", frame->pc, frame->sr);
121 #endif
122 }
123 
124 static addr_t
125 fault_address(struct iframe *iframe)
126 {
127 	switch (iframe->cpu.type) {
128 		case 0x0:
129 		case 0x1:
130 			return 0;
131 		case 0x2:
132 			return iframe->cpu.type_2.instruction_address;
133 		case 0x3:
134 			return iframe->cpu.type_3.effective_address;
135 		case 0x7:
136 			return iframe->cpu.type_7.effective_address;
137 		case 0x9:
138 			return iframe->cpu.type_9.instruction_address;
139 		case 0xa:
140 			return iframe->cpu.type_a.fault_address;
141 		case 0xb:
142 			return iframe->cpu.type_b.fault_address;
143 		default:
144 			return 0;
145 	}
146 }
147 
148 static bool
149 fault_was_write(struct iframe *iframe)
150 {
151 	switch (iframe->cpu.type) {
152 		case 0x7:
153 			return !iframe->cpu.type_7.ssw.rw;
154 		case 0xa:
155 			return !iframe->cpu.type_a.ssw.rw;
156 		case 0xb:
157 			return !iframe->cpu.type_b.ssw.rw;
158 		default:
159 			panic("can't determine r/w from iframe type %d\n",
160 				iframe->cpu.type);
161 			return false;
162 	}
163 }
164 
165 extern "C" void m68k_exception_entry(struct iframe *iframe);
166 void
167 m68k_exception_entry(struct iframe *iframe)
168 {
169 	int vector = iframe->cpu.vector >> 2;
170 	int ret = B_HANDLED_INTERRUPT;
171 
172 	if (vector != -1) {
173 		dprintf("m68k_exception_entry: time %lld vector 0x%x, iframe %p, "
174 			"pc: %p\n", system_time(), vector, iframe, (void*)iframe->cpu.pc);
175 	}
176 
177 	struct thread *thread = thread_get_current_thread();
178 
179 	// push iframe
180 	if (thread)
181 		m68k_push_iframe(&thread->arch_info.iframes, iframe);
182 	else
183 		m68k_push_iframe(&gBootFrameStack, iframe);
184 
185 	switch (vector) {
186 		case 0: // system reset
187 			panic("system reset exception\n");
188 			break;
189 		case 2: // bus error
190 		case 3: // address error
191 		{
192 			bool kernelDebugger = debug_debugger_running();
193 
194 			if (kernelDebugger) {
195 				// if this thread has a fault handler, we're allowed to be here
196 				struct thread *thread = thread_get_current_thread();
197 				if (thread && thread->fault_handler != 0) {
198 					iframe->cpu.pc = thread->fault_handler;
199 					break;
200 				}
201 
202 
203 				// otherwise, not really
204 				panic("page fault in debugger without fault handler! Touching "
205 					"address %p from ip %p\n", (void *)fault_address(iframe),
206 					(void *)iframe->cpu.pc);
207 				break;
208 			} else if ((iframe->cpu.sr & SR_IP_MASK) != 0) {
209 				// if the interrupts were disabled, and we are not running the
210 				// kernel startup the page fault was not allowed to happen and
211 				// we must panic
212 				panic("page fault, but interrupts were disabled. Touching "
213 					"address %p from ip %p\n", (void *)fault_address(iframe),
214 					(void *)iframe->cpu.pc);
215 				break;
216 			} else if (thread != NULL && thread->page_faults_allowed < 1) {
217 				panic("page fault not allowed at this place. Touching address "
218 					"%p from ip %p\n", (void *)fault_address(iframe),
219 					(void *)iframe->cpu.pc);
220 			}
221 
222 			enable_interrupts();
223 
224 			addr_t newip;
225 
226 			ret = vm_page_fault(fault_address(iframe), iframe->cpu.pc,
227 				fault_was_write(iframe), // store or load
228 				iframe->cpu.sr & SR_S, // was the system in user or supervisor
229 				&newip);
230 			if (newip != 0) {
231 				// the page fault handler wants us to modify the iframe to set the
232 				// IP the cpu will return to to be this ip
233 				iframe->cpu.pc = newip;
234 			}
235  			break;
236 		}
237 
238 		case 24: // spurious interrupt
239 			dprintf("spurious interrupt\n");
240 			break;
241 		case 25: // autovector interrupt
242 		case 26: // autovector interrupt
243 		case 27: // autovector interrupt
244 		case 28: // autovector interrupt
245 		case 29: // autovector interrupt
246 		case 30: // autovector interrupt
247 		case 31: // autovector interrupt
248 		{
249 #if 0
250 			if (!sPIC) {
251 				panic("m68k_exception_entry(): external interrupt although we "
252 					"don't have a PIC driver!");
253 				ret = B_HANDLED_INTERRUPT;
254 				break;
255 			}
256 #endif
257 			M68KPlatform::Default()->AcknowledgeIOInterrupt(vector);
258 
259 dprintf("handling I/O interrupts...\n");
260 			ret = int_io_interrupt_handler(vector, true);
261 #if 0
262 			while ((irq = sPIC->acknowledge_io_interrupt(sPICCookie)) >= 0) {
263 // TODO: correctly pass level-triggered vs. edge-triggered to the handler!
264 				ret = int_io_interrupt_handler(irq, true);
265 			}
266 #endif
267 dprintf("handling I/O interrupts done\n");
268 			break;
269 		}
270 
271 		case 9: // trace
272 		default:
273 			// vectors >= 64 are user defined vectors, used for IRQ
274 			if (vector >= 64) {
275 				M68KPlatform::Default()->AcknowledgeIOInterrupt(irq);
276 				ret = int_io_interrupt_handler(vector, true);
277 				break;
278 			}
279 			dprintf("unhandled exception type 0x%x\n", vector);
280 			print_iframe(iframe);
281 			panic("unhandled exception type\n");
282 	}
283 
284 	if (ret == B_INVOKE_SCHEDULER) {
285 		int state = disable_interrupts();
286 		GRAB_THREAD_LOCK();
287 		scheduler_reschedule();
288 		RELEASE_THREAD_LOCK();
289 		restore_interrupts(state);
290 	}
291 
292 	// pop iframe
293 	if (thread)
294 		m68k_pop_iframe(&thread->arch_info.iframes);
295 	else
296 		m68k_pop_iframe(&gBootFrameStack);
297 }
298 
299 
300 status_t
301 arch_int_init(kernel_args *args)
302 {
303 	status_t err;
304 	addr_t vbr;
305 	int i;
306 
307 	gExceptionVectors = (m68k_exception_handler *)args->arch_args.vir_vbr;
308 
309 	/* fill in the vector table */
310 	for (i = 0; i < M68K_EXCEPTION_VECTOR_COUNT; i++)
311 		gExceptionVectors[i] = &__m68k_exception_common;
312 
313 	vbr = args->arch_args.phys_vbr;
314 	/* point VBR to the new table */
315 	asm volatile  ("movec %0,%%vbr" : : "r"(vbr):);
316 	return B_OK;
317 }
318 
319 
320 status_t
321 arch_int_init_post_vm(kernel_args *args)
322 {
323 	return B_OK;
324 }
325 
326 
327 #if 0 /* PIC modules */
328 template<typename ModuleInfo>
329 struct Module : DoublyLinkedListLinkImpl<Module<ModuleInfo> > {
330 	Module(ModuleInfo *module)
331 		: module(module)
332 	{
333 	}
334 
335 	~Module()
336 	{
337 		if (module)
338 			put_module(((module_info*)module)->name);
339 	}
340 
341 	ModuleInfo	*module;
342 };
343 
344 typedef Module<interrupt_controller_module_info> PICModule;
345 
346 struct PICModuleList : DoublyLinkedList<PICModule> {
347 	~PICModuleList()
348 	{
349 		while (PICModule *module = First()) {
350 			Remove(module);
351 			delete module;
352 		}
353 	}
354 };
355 
356 
357 class DeviceTreeIterator {
358 public:
359 	DeviceTreeIterator(device_manager_info *deviceManager)
360 		: fDeviceManager(deviceManager),
361 		  fNode(NULL),
362 		  fParent(NULL)
363 	{
364 		Rewind();
365 	}
366 
367 	~DeviceTreeIterator()
368 	{
369 		if (fParent != NULL)
370 			fDeviceManager->put_device_node(fParent);
371 		if (fNode != NULL)
372 			fDeviceManager->put_device_node(fNode);
373 	}
374 
375 	void Rewind()
376 	{
377 		fNode = fDeviceManager->get_root();
378 	}
379 
380 	bool HasNext() const
381 	{
382 		return (fNode != NULL);
383 	}
384 
385 	device_node_handle Next()
386 	{
387 		if (fNode == NULL)
388 			return NULL;
389 
390 		device_node_handle foundNode = fNode;
391 
392 		// get first child
393 		device_node_handle child = NULL;
394 		if (fDeviceManager->get_next_child_device(fNode, &child, NULL)
395 				== B_OK) {
396 			// move to the child node
397 			if (fParent != NULL)
398 				fDeviceManager->put_device_node(fParent);
399 			fParent = fNode;
400 			fNode = child;
401 
402 		// no more children; backtrack to find the next sibling
403 		} else {
404 			while (fParent != NULL) {
405 				if (fDeviceManager->get_next_child_device(fParent, &fNode, NULL)
406 						== B_OK) {
407 						// get_next_child_device() always puts the node
408 					break;
409 				}
410 				fNode = fParent;
411 				fParent = fDeviceManager->get_parent(fNode);
412 			}
413 
414 			// if we hit the root node again, we're done
415 			if (fParent == NULL) {
416 				fDeviceManager->put_device_node(fNode);
417 				fNode = NULL;
418 			}
419 		}
420 
421 		return foundNode;
422 	}
423 
424 private:
425 	device_manager_info *fDeviceManager;
426 	device_node_handle	fNode;
427 	device_node_handle	fParent;
428 };
429 
430 
431 static void
432 get_interrupt_controller_modules(PICModuleList &list)
433 {
434 	const char *namePrefix = "interrupt_controllers/";
435 	size_t namePrefixLen = strlen(namePrefix);
436 
437 	char name[B_PATH_NAME_LENGTH];
438 	size_t length;
439 	uint32 cookie = 0;
440 	while (get_next_loaded_module_name(&cookie, name, &(length = sizeof(name)))
441 			== B_OK) {
442 		// an interrupt controller module?
443 		if (length <= namePrefixLen
444 			|| strncmp(name, namePrefix, namePrefixLen) != 0) {
445 			continue;
446 		}
447 
448 		// get the module
449 		interrupt_controller_module_info *moduleInfo;
450 		if (get_module(name, (module_info**)&moduleInfo) != B_OK)
451 			continue;
452 
453 		// add it to the list
454 		PICModule *module = new(nothrow) PICModule(moduleInfo);
455 		if (!module) {
456 			put_module(((module_info*)moduleInfo)->name);
457 			continue;
458 		}
459 		list.Add(module);
460 	}
461 }
462 
463 
464 static bool
465 probe_pic_device(device_node_handle node, PICModuleList &picModules)
466 {
467 	for (PICModule *module = picModules.Head();
468 		 module;
469 		 module = picModules.GetNext(module)) {
470 		bool noConnection;
471 		if (module->module->info.supports_device(node, &noConnection) > 0) {
472 			if (module->module->info.register_device(node) == B_OK)
473 				return true;
474 		}
475 	}
476 
477 	return false;
478 }
479 #endif /* PIC modules */
480 
481 status_t
482 arch_int_init_post_device_manager(struct kernel_args *args)
483 {
484 	status_t err;
485 	err = M68KPlatform::Default()->InitPIC(args);
486 	return err;
487 #if 0 /* PIC modules */
488 	// get the interrupt controller driver modules
489 	PICModuleList picModules;
490 	get_interrupt_controller_modules(picModules);
491 	if (picModules.IsEmpty()) {
492 		panic("arch_int_init_post_device_manager(): Found no PIC modules!");
493 		return B_ENTRY_NOT_FOUND;
494 	}
495 
496 	// get the device manager module
497 	device_manager_info *deviceManager;
498 	status_t error = get_module(B_DEVICE_MANAGER_MODULE_NAME,
499 		(module_info**)&deviceManager);
500 	if (error != B_OK) {
501 		panic("arch_int_init_post_device_manager(): Failed to get device "
502 			"manager: %s", strerror(error));
503 		return error;
504 	}
505 	Module<device_manager_info> _deviceManager(deviceManager);	// auto put
506 
507 	// iterate through the device tree and probe the interrupt controllers
508 	DeviceTreeIterator iterator(deviceManager);
509 	while (device_node_handle node = iterator.Next())
510 		probe_pic_device(node, picModules);
511 
512 	// iterate through the tree again and get an interrupt controller node
513 	iterator.Rewind();
514 	while (device_node_handle node = iterator.Next()) {
515 		char *deviceType;
516 		if (deviceManager->get_attr_string(node, B_DRIVER_DEVICE_TYPE,
517 				&deviceType, false) == B_OK) {
518 			bool isPIC
519 				= (strcmp(deviceType, B_INTERRUPT_CONTROLLER_DRIVER_TYPE) == 0);
520 			free(deviceType);
521 
522 			if (isPIC) {
523 				driver_module_info *driver;
524 				void *driverCookie;
525 				error = deviceManager->init_driver(node, NULL, &driver,
526 					&driverCookie);
527 				if (error == B_OK) {
528 					sPIC = (interrupt_controller_module_info *)driver;
529 					sPICCookie = driverCookie;
530 					return B_OK;
531 				}
532 			}
533 		}
534 	}
535 
536 #endif /* PIC modules */
537 
538 	// no PIC found
539 	panic("arch_int_init_post_device_manager(): Found no supported PIC!");
540 
541 	return B_ENTRY_NOT_FOUND;
542 }
543 
544 
545 #if 0//PPC
546 // #pragma mark -
547 
548 struct m68k_cpu_exception_context *
549 m68k_get_cpu_exception_context(int cpu)
550 {
551 	return sCPUExceptionContexts + cpu;
552 }
553 
554 
555 void
556 m68k_set_current_cpu_exception_context(struct m68k_cpu_exception_context *context)
557 {
558 	// translate to physical address
559 	addr_t physicalPage;
560 	addr_t inPageOffset = (addr_t)context & (B_PAGE_SIZE - 1);
561 	status_t error = vm_get_page_mapping(vm_kernel_address_space_id(),
562 		(addr_t)context - inPageOffset, &physicalPage);
563 	if (error != B_OK) {
564 		panic("m68k_set_current_cpu_exception_context(): Failed to get physical "
565 			"address!");
566 		return;
567 	}
568 
569 	asm volatile("mtsprg0 %0" : : "r"(physicalPage + inPageOffset));
570 }
571 
572 #endif
573