1fcf5ef2aSThomas Huth /*
2fcf5ef2aSThomas Huth * OpenRISC interrupt.
3fcf5ef2aSThomas Huth *
4fcf5ef2aSThomas Huth * Copyright (c) 2011-2012 Jia Liu <proljc@gmail.com>
5fcf5ef2aSThomas Huth *
6fcf5ef2aSThomas Huth * This library is free software; you can redistribute it and/or
7fcf5ef2aSThomas Huth * modify it under the terms of the GNU Lesser General Public
8fcf5ef2aSThomas Huth * License as published by the Free Software Foundation; either
9198a2d21SThomas Huth * version 2.1 of the License, or (at your option) any later version.
10fcf5ef2aSThomas Huth *
11fcf5ef2aSThomas Huth * This library is distributed in the hope that it will be useful,
12fcf5ef2aSThomas Huth * but WITHOUT ANY WARRANTY; without even the implied warranty of
13fcf5ef2aSThomas Huth * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14fcf5ef2aSThomas Huth * Lesser General Public License for more details.
15fcf5ef2aSThomas Huth *
16fcf5ef2aSThomas Huth * You should have received a copy of the GNU Lesser General Public
17fcf5ef2aSThomas Huth * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18fcf5ef2aSThomas Huth */
19fcf5ef2aSThomas Huth
20fcf5ef2aSThomas Huth #include "qemu/osdep.h"
21cd617484SPhilippe Mathieu-Daudé #include "qemu/log.h"
22fcf5ef2aSThomas Huth #include "cpu.h"
23fcf5ef2aSThomas Huth #include "exec/exec-all.h"
244ea5fe99SAlex Bennée #include "gdbstub/helpers.h"
25fcf5ef2aSThomas Huth #include "qemu/host-utils.h"
26fcf5ef2aSThomas Huth #ifndef CONFIG_USER_ONLY
27fcf5ef2aSThomas Huth #include "hw/loader.h"
28fcf5ef2aSThomas Huth #endif
29fcf5ef2aSThomas Huth
openrisc_cpu_do_interrupt(CPUState * cs)30fcf5ef2aSThomas Huth void openrisc_cpu_do_interrupt(CPUState *cs)
31fcf5ef2aSThomas Huth {
32*074bd799SPhilippe Mathieu-Daudé CPUOpenRISCState *env = cpu_env(cs);
33378cd36fSRichard Henderson int exception = cs->exception_index;
34fcf5ef2aSThomas Huth
35fcf5ef2aSThomas Huth env->epcr = env->pc;
36765fdc1eSStafford Horne
37c56e3b86SStafford Horne /* When we have an illegal instruction the error effective address
38c56e3b86SStafford Horne shall be set to the illegal instruction address. */
39378cd36fSRichard Henderson if (exception == EXCP_ILLEGAL) {
40c56e3b86SStafford Horne env->eear = env->pc;
41c56e3b86SStafford Horne }
42fcf5ef2aSThomas Huth
439f6e8afaSStafford Horne /* During exceptions esr is populared with the pre-exception sr. */
4484775c43SRichard Henderson env->esr = cpu_get_sr(env);
459f6e8afaSStafford Horne /* In parallel sr is updated to disable mmu, interrupts, timers and
469f6e8afaSStafford Horne set the delay slot exception flag. */
47fcf5ef2aSThomas Huth env->sr &= ~SR_DME;
48fcf5ef2aSThomas Huth env->sr &= ~SR_IME;
49fcf5ef2aSThomas Huth env->sr |= SR_SM;
50fcf5ef2aSThomas Huth env->sr &= ~SR_IEE;
51fcf5ef2aSThomas Huth env->sr &= ~SR_TEE;
52f4d1414aSStafford Horne env->pmr &= ~PMR_DME;
53f4d1414aSStafford Horne env->pmr &= ~PMR_SME;
54930c3d00SRichard Henderson env->lock_addr = -1;
55fcf5ef2aSThomas Huth
569f6e8afaSStafford Horne /* Set/clear dsx to indicate if we are in a delay slot exception. */
579f6e8afaSStafford Horne if (env->dflag) {
589f6e8afaSStafford Horne env->dflag = 0;
599f6e8afaSStafford Horne env->sr |= SR_DSX;
609f6e8afaSStafford Horne env->epcr -= 4;
619f6e8afaSStafford Horne } else {
629f6e8afaSStafford Horne env->sr &= ~SR_DSX;
63765fdc1eSStafford Horne if (exception == EXCP_SYSCALL || exception == EXCP_FPE) {
64765fdc1eSStafford Horne env->epcr += 4;
65765fdc1eSStafford Horne }
669f6e8afaSStafford Horne }
679f6e8afaSStafford Horne
68378cd36fSRichard Henderson if (exception > 0 && exception < EXCP_NR) {
69378cd36fSRichard Henderson static const char * const int_name[EXCP_NR] = {
70378cd36fSRichard Henderson [EXCP_RESET] = "RESET",
71378cd36fSRichard Henderson [EXCP_BUSERR] = "BUSERR (bus error)",
72378cd36fSRichard Henderson [EXCP_DPF] = "DFP (data protection fault)",
73378cd36fSRichard Henderson [EXCP_IPF] = "IPF (code protection fault)",
74378cd36fSRichard Henderson [EXCP_TICK] = "TICK (timer interrupt)",
75378cd36fSRichard Henderson [EXCP_ALIGN] = "ALIGN",
76378cd36fSRichard Henderson [EXCP_ILLEGAL] = "ILLEGAL",
77378cd36fSRichard Henderson [EXCP_INT] = "INT (device interrupt)",
78378cd36fSRichard Henderson [EXCP_DTLBMISS] = "DTLBMISS (data tlb miss)",
79378cd36fSRichard Henderson [EXCP_ITLBMISS] = "ITLBMISS (code tlb miss)",
80378cd36fSRichard Henderson [EXCP_RANGE] = "RANGE",
81378cd36fSRichard Henderson [EXCP_SYSCALL] = "SYSCALL",
82378cd36fSRichard Henderson [EXCP_FPE] = "FPE",
83378cd36fSRichard Henderson [EXCP_TRAP] = "TRAP",
84378cd36fSRichard Henderson };
85378cd36fSRichard Henderson
86bbe6855eSStafford Horne qemu_log_mask(CPU_LOG_INT, "CPU: %d INT: %s\n",
87bbe6855eSStafford Horne cs->cpu_index,
88bbe6855eSStafford Horne int_name[exception]);
89378cd36fSRichard Henderson
90378cd36fSRichard Henderson hwaddr vect_pc = exception << 8;
91356a2db3STim 'mithro' Ansell if (env->cpucfgr & CPUCFGR_EVBARP) {
92356a2db3STim 'mithro' Ansell vect_pc |= env->evbar;
93356a2db3STim 'mithro' Ansell }
943fee028dSTim 'mithro' Ansell if (env->sr & SR_EPH) {
953fee028dSTim 'mithro' Ansell vect_pc |= 0xf0000000;
963fee028dSTim 'mithro' Ansell }
97356a2db3STim 'mithro' Ansell env->pc = vect_pc;
98fcf5ef2aSThomas Huth } else {
99378cd36fSRichard Henderson cpu_abort(cs, "Unhandled exception 0x%x\n", exception);
100fcf5ef2aSThomas Huth }
101fcf5ef2aSThomas Huth
102fcf5ef2aSThomas Huth cs->exception_index = -1;
103fcf5ef2aSThomas Huth }
104fcf5ef2aSThomas Huth
openrisc_cpu_exec_interrupt(CPUState * cs,int interrupt_request)105fcf5ef2aSThomas Huth bool openrisc_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
106fcf5ef2aSThomas Huth {
107*074bd799SPhilippe Mathieu-Daudé CPUOpenRISCState *env = cpu_env(cs);
108fcf5ef2aSThomas Huth int idx = -1;
109fcf5ef2aSThomas Huth
110fcf5ef2aSThomas Huth if ((interrupt_request & CPU_INTERRUPT_HARD) && (env->sr & SR_IEE)) {
111fcf5ef2aSThomas Huth idx = EXCP_INT;
112fcf5ef2aSThomas Huth }
113fcf5ef2aSThomas Huth if ((interrupt_request & CPU_INTERRUPT_TIMER) && (env->sr & SR_TEE)) {
114fcf5ef2aSThomas Huth idx = EXCP_TICK;
115fcf5ef2aSThomas Huth }
116fcf5ef2aSThomas Huth if (idx >= 0) {
117fcf5ef2aSThomas Huth cs->exception_index = idx;
118fcf5ef2aSThomas Huth openrisc_cpu_do_interrupt(cs);
119fcf5ef2aSThomas Huth return true;
120fcf5ef2aSThomas Huth }
121fcf5ef2aSThomas Huth return false;
122fcf5ef2aSThomas Huth }
123