1 /*
2 * Copyright (c) 2015 Travis Geiselbrecht
3 *
4 * Use of this source code is governed by a MIT-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/MIT
7 */
8 #include <lk/trace.h>
9 #include <lk/debug.h>
10 #include <assert.h>
11 #include <stdint.h>
12 #include <lk/bits.h>
13 #include <kernel/thread.h>
14 #include <kernel/debug.h>
15 #include <arch/mips.h>
16
17 #define LOCAL_TRACE 0
18
19 extern enum handler_return platform_irq(struct mips_iframe *iframe, uint num);
20
21 void mips_gen_exception(struct mips_iframe *iframe);
mips_gen_exception(struct mips_iframe * iframe)22 void mips_gen_exception(struct mips_iframe *iframe) {
23 uint32_t excode = BITS_SHIFT(iframe->cause, 6, 2);
24 if (excode == 0x8) {
25 LTRACEF("SYSCALL, EPC 0x%x\n", iframe->epc);
26 iframe->epc += 4;
27 } else {
28 LTRACEF("status 0x%x\n", iframe->status);
29 LTRACEF("cause 0x%x\n", iframe->cause);
30 LTRACEF("\texcode 0x%x\n", excode);
31 LTRACEF("epc 0x%x\n", iframe->epc);
32 for (;;);
33 }
34 }
35
36 void mips_irq(struct mips_iframe *iframe, uint num);
mips_irq(struct mips_iframe * iframe,uint num)37 void mips_irq(struct mips_iframe *iframe, uint num) {
38 // unset IE and clear EXL
39 mips_write_c0_status(mips_read_c0_status() & ~(3<<0));
40
41 THREAD_STATS_INC(interrupts);
42 KEVLOG_IRQ_ENTER(num);
43
44 LTRACEF("IRQ %u, EPC 0x%x, old status 0x%x, status 0x%x\n",
45 num, iframe->epc, iframe->status, mips_read_c0_status());
46
47 enum handler_return ret = INT_NO_RESCHEDULE;
48
49 // figure out which interrupt the timer is set to
50 uint32_t ipti = BITS_SHIFT(mips_read_c0_intctl(), 31, 29);
51 if (ipti >= 2 && ipti == num) {
52 // builtin timer
53 ret = mips_timer_irq();
54 #if PLATFORM_QEMU_MIPS
55 } else if (num == 2) {
56 ret = platform_irq(iframe, num);
57 #endif
58 } else {
59 panic("mips: unhandled irq\n");
60 }
61
62 KEVLOG_IRQ_EXIT(num);
63
64 if (ret != INT_NO_RESCHEDULE)
65 thread_preempt();
66 }
67
68