1 |
33 |
lampret |
/* except.c -- Simulation of OR1K exceptions
|
2 |
|
|
Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
|
3 |
|
|
|
4 |
|
|
This file is part of OpenRISC 1000 Architectural Simulator.
|
5 |
|
|
|
6 |
|
|
This program is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2 of the License, or
|
9 |
|
|
(at your option) any later version.
|
10 |
|
|
|
11 |
|
|
This program is distributed in the hope that it will be useful,
|
12 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
|
|
GNU General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with this program; if not, write to the Free Software
|
18 |
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
19 |
|
|
|
20 |
|
|
#include <stdlib.h>
|
21 |
|
|
#include <stdio.h>
|
22 |
|
|
#include <string.h>
|
23 |
|
|
|
24 |
1350 |
nogj |
#include "config.h"
|
25 |
|
|
|
26 |
|
|
#ifdef HAVE_INTTYPES_H
|
27 |
|
|
#include <inttypes.h>
|
28 |
|
|
#endif
|
29 |
|
|
|
30 |
|
|
#include "port.h"
|
31 |
|
|
#include "arch.h"
|
32 |
33 |
lampret |
#include "abstract.h"
|
33 |
|
|
#include "except.h"
|
34 |
344 |
markom |
#include "sim-config.h"
|
35 |
1308 |
phoenix |
#include "debug_unit.h"
|
36 |
1432 |
nogj |
#include "opcode/or32.h"
|
37 |
|
|
#include "spr_defs.h"
|
38 |
1350 |
nogj |
#include "execute.h"
|
39 |
1432 |
nogj |
#include "sprs.h"
|
40 |
1510 |
nogj |
#include "debug.h"
|
41 |
33 |
lampret |
|
42 |
1452 |
nogj |
#if DYNAMIC_EXECUTION
|
43 |
|
|
#include "sched.h"
|
44 |
|
|
#include "rec_i386.h"
|
45 |
|
|
#include "op_support.h"
|
46 |
|
|
#endif
|
47 |
|
|
|
48 |
1510 |
nogj |
DEFAULT_DEBUG_CHANNEL(except);
|
49 |
82 |
lampret |
|
50 |
1386 |
nogj |
int except_pending = 0;
|
51 |
139 |
chris |
|
52 |
1386 |
nogj |
static const char *except_names[] = {
|
53 |
|
|
NULL,
|
54 |
|
|
"Reset",
|
55 |
|
|
"Bus Error",
|
56 |
|
|
"Data Page Fault",
|
57 |
|
|
"Insn Page Fault",
|
58 |
|
|
"Tick timer",
|
59 |
|
|
"Alignment",
|
60 |
|
|
"Illegal instruction",
|
61 |
|
|
"Interrupt",
|
62 |
|
|
"Data TLB Miss",
|
63 |
|
|
"Insn TLB Miss",
|
64 |
|
|
"Range",
|
65 |
|
|
"System Call",
|
66 |
1585 |
phoenix |
"Floating Point",
|
67 |
1386 |
nogj |
"Trap" };
|
68 |
|
|
|
69 |
1452 |
nogj |
const char *except_name(oraddr_t except)
|
70 |
139 |
chris |
{
|
71 |
1386 |
nogj |
return except_names[except >> 8];
|
72 |
139 |
chris |
}
|
73 |
|
|
|
74 |
1452 |
nogj |
#if DYNAMIC_EXECUTION
|
75 |
|
|
/* FIXME: Remove the need for this */
|
76 |
|
|
/* This is needed because immu_translate can be called from do_rfe and do_jump
|
77 |
|
|
* in which case the scheduler does not need to get run. immu_translate can also
|
78 |
|
|
* be called from mtspr in which case the exceptions that it generates happen
|
79 |
|
|
* during an instruction and the scheduler needs to get run. */
|
80 |
|
|
int immu_ex_from_insn = 0;
|
81 |
|
|
#endif
|
82 |
|
|
|
83 |
479 |
markom |
/* Asserts OR1K exception. */
|
84 |
1473 |
nogj |
/* WARNING: Don't excpect except_handle to return. Sometimes it _may_ return at
|
85 |
|
|
* other times it may not. */
|
86 |
1350 |
nogj |
void except_handle(oraddr_t except, oraddr_t ea)
|
87 |
33 |
lampret |
{
|
88 |
1452 |
nogj |
oraddr_t except_vector;
|
89 |
|
|
|
90 |
1386 |
nogj |
if(debug_ignore_exception (except))
|
91 |
|
|
return;
|
92 |
139 |
chris |
|
93 |
1452 |
nogj |
#if !(DYNAMIC_EXECUTION)
|
94 |
|
|
/* In the dynamic recompiler, this function never returns, so this is not
|
95 |
|
|
* needed. Ofcourse we could set it anyway, but then all code that checks
|
96 |
|
|
* this variable would break, since it is never reset */
|
97 |
1386 |
nogj |
except_pending = 1;
|
98 |
1452 |
nogj |
#endif
|
99 |
51 |
lampret |
|
100 |
1510 |
nogj |
TRACE("Exception 0x%"PRIxADDR" (%s) at 0x%"PRIxADDR", EA: 0x%"PRIxADDR
|
101 |
|
|
", cycles %lld, #%lld\n",
|
102 |
|
|
except, except_name(except), cpu_state.pc, ea, runtime.sim.cycles,
|
103 |
|
|
runtime.cpu.instructions);
|
104 |
1386 |
nogj |
|
105 |
1506 |
nogj |
except_vector = except + (cpu_state.sprs[SPR_SR] & SPR_SR_EPH ? 0xf0000000 : 0x00000000);
|
106 |
1386 |
nogj |
|
107 |
1452 |
nogj |
#if !(DYNAMIC_EXECUTION)
|
108 |
|
|
pcnext = except_vector;
|
109 |
|
|
#endif
|
110 |
|
|
|
111 |
1442 |
nogj |
cpu_state.sprs[SPR_EEAR_BASE] = ea;
|
112 |
|
|
cpu_state.sprs[SPR_ESR_BASE] = cpu_state.sprs[SPR_SR];
|
113 |
|
|
|
114 |
|
|
cpu_state.sprs[SPR_SR] &= ~SPR_SR_OVE; /* Disable overflow flag exception. */
|
115 |
|
|
|
116 |
|
|
cpu_state.sprs[SPR_SR] |= SPR_SR_SM; /* SUPV mode */
|
117 |
|
|
cpu_state.sprs[SPR_SR] &= ~(SPR_SR_IEE | SPR_SR_TEE); /* Disable interrupts. */
|
118 |
|
|
|
119 |
|
|
/* Address translation is always disabled when starting exception. */
|
120 |
|
|
cpu_state.sprs[SPR_SR] &= ~SPR_SR_DME;
|
121 |
|
|
|
122 |
1452 |
nogj |
#if DYNAMIC_EXECUTION
|
123 |
|
|
/* If we were called from do_scheduler and there were more jobs scheduled to
|
124 |
|
|
* run after this, they won't run unless the following call is made since this
|
125 |
|
|
* function never returns. (If we weren't called from do_scheduler, then the
|
126 |
|
|
* job at the head of the queue will still have some time remaining) */
|
127 |
|
|
if(scheduler.job_queue->time <= 0)
|
128 |
|
|
do_scheduler();
|
129 |
|
|
#endif
|
130 |
|
|
|
131 |
1386 |
nogj |
switch(except) {
|
132 |
|
|
/* EPCR is irrelevent */
|
133 |
|
|
case EXCEPT_RESET:
|
134 |
|
|
break;
|
135 |
|
|
/* EPCR is loaded with address of instruction that caused the exception */
|
136 |
1452 |
nogj |
case EXCEPT_ITLBMISS:
|
137 |
|
|
case EXCEPT_IPF:
|
138 |
|
|
#if DYNAMIC_EXECUTION
|
139 |
|
|
/* In immu_translate except_handle is called with except_handle(..., virtaddr) */
|
140 |
|
|
/* Add the immu miss delay to the cycle counter */
|
141 |
1481 |
nogj |
if(!immu_ex_from_insn) {
|
142 |
1508 |
nogj |
cpu_state.sprs[SPR_EPCR_BASE] = get_pc() - (cpu_state.delay_insn ? 4 : 0);
|
143 |
1481 |
nogj |
} else
|
144 |
1452 |
nogj |
/* This exception came from an l.mtspr instruction in which case the pc
|
145 |
|
|
* points to the l.mtspr instruction when in acutal fact, it is the next
|
146 |
|
|
* instruction that would have faulted/missed. ea is used instead of
|
147 |
|
|
* cpu_state.pc + 4 because in the event that the l.mtspr instruction is
|
148 |
|
|
* in the delay slot of a page local jump the fault must happen on the
|
149 |
|
|
* instruction that was jumped to. This is handled in recheck_immu. */
|
150 |
1508 |
nogj |
cpu_state.sprs[SPR_EPCR_BASE] = ea;
|
151 |
1481 |
nogj |
run_sched_out_of_line(immu_ex_from_insn);
|
152 |
|
|
/* Save the registers that are in the temporaries */
|
153 |
|
|
if(!cpu_state.ts_current)
|
154 |
|
|
upd_reg_from_t(cpu_state.pc, !immu_ex_from_insn);
|
155 |
|
|
immu_ex_from_insn = 0;
|
156 |
|
|
break;
|
157 |
1452 |
nogj |
#endif
|
158 |
1386 |
nogj |
/* All these exceptions happen during a simulated instruction */
|
159 |
|
|
case EXCEPT_BUSERR:
|
160 |
|
|
case EXCEPT_DPF:
|
161 |
|
|
case EXCEPT_ALIGN:
|
162 |
|
|
case EXCEPT_ILLEGAL:
|
163 |
|
|
case EXCEPT_DTLBMISS:
|
164 |
|
|
case EXCEPT_RANGE:
|
165 |
|
|
case EXCEPT_TRAP:
|
166 |
1452 |
nogj |
#if DYNAMIC_EXECUTION
|
167 |
|
|
/* Since these exceptions happen during a simulated instruction and this
|
168 |
|
|
* function jumps out to the exception vector the scheduler would never have
|
169 |
|
|
* a chance to run, therefore run it now */
|
170 |
|
|
run_sched_out_of_line(1);
|
171 |
1481 |
nogj |
/* Save the registers that are in the temporaries */
|
172 |
|
|
if(!cpu_state.ts_current) {
|
173 |
|
|
if(cpu_state.delay_insn &&
|
174 |
1525 |
nogj |
(IADDR_PAGE(cpu_state.pc) == IADDR_PAGE(cpu_state.pc - 4)))
|
175 |
1481 |
nogj |
upd_reg_from_t(cpu_state.pc - 4, 0);
|
176 |
|
|
else
|
177 |
|
|
upd_reg_from_t(cpu_state.pc, 0);
|
178 |
|
|
}
|
179 |
1452 |
nogj |
#endif
|
180 |
1508 |
nogj |
cpu_state.sprs[SPR_EPCR_BASE] = cpu_state.pc - (cpu_state.delay_insn ? 4 : 0);
|
181 |
1386 |
nogj |
break;
|
182 |
|
|
/* EPCR is loaded with address of next not-yet-executed instruction */
|
183 |
|
|
case EXCEPT_SYSCALL:
|
184 |
1508 |
nogj |
cpu_state.sprs[SPR_EPCR_BASE] = (cpu_state.pc + 4) - (cpu_state.delay_insn ? 4 : 0);
|
185 |
1386 |
nogj |
break;
|
186 |
|
|
/* These exceptions happen AFTER (or before) an instruction has been
|
187 |
|
|
* simulated, therefore the pc already points to the *next* instruction */
|
188 |
|
|
case EXCEPT_TICK:
|
189 |
|
|
case EXCEPT_INT:
|
190 |
1508 |
nogj |
cpu_state.sprs[SPR_EPCR_BASE] = cpu_state.pc - (cpu_state.delay_insn ? 4 : 0);
|
191 |
1452 |
nogj |
#if !(DYNAMIC_EXECUTION)
|
192 |
1386 |
nogj |
/* If we don't update the pc now, then it will only happen *after* the next
|
193 |
|
|
* instruction (There would be serious problems if the next instruction just
|
194 |
|
|
* happens to be a branch), when it should happen NOW. */
|
195 |
1432 |
nogj |
cpu_state.pc = pcnext;
|
196 |
1386 |
nogj |
pcnext += 4;
|
197 |
1452 |
nogj |
#else
|
198 |
|
|
/* except_handle() mucks around with the temporaries, which are in the state
|
199 |
|
|
* of the last instruction executed and not the next one, to which the pc
|
200 |
|
|
* now points to */
|
201 |
|
|
cpu_state.pc -= 4;
|
202 |
1481 |
nogj |
|
203 |
|
|
/* Save the registers that are in the temporaries */
|
204 |
|
|
if(!cpu_state.ts_current)
|
205 |
|
|
upd_reg_from_t(cpu_state.pc, 1);
|
206 |
1452 |
nogj |
#endif
|
207 |
1386 |
nogj |
break;
|
208 |
479 |
markom |
}
|
209 |
693 |
markom |
|
210 |
1452 |
nogj |
/* Address trnaslation is here because run_sched_out_of_line calls
|
211 |
|
|
* eval_insn_direct which checks out the immu for the address translation but
|
212 |
|
|
* if it would be disabled above then there would be not much point... */
|
213 |
|
|
cpu_state.sprs[SPR_SR] &= ~SPR_SR_IME;
|
214 |
|
|
|
215 |
|
|
/* Complex/simple execution strictly don't need this because of the
|
216 |
|
|
* next_delay_insn thingy but in the dynamic execution modell that doesn't
|
217 |
1481 |
nogj |
* exist and thus cpu_state.delay_insn would stick in the exception handler
|
218 |
1452 |
nogj |
* causeing grief if the first instruction of the exception handler is also in
|
219 |
|
|
* the delay slot of the previous instruction */
|
220 |
1432 |
nogj |
cpu_state.delay_insn = 0;
|
221 |
1452 |
nogj |
|
222 |
|
|
#if DYNAMIC_EXECUTION
|
223 |
|
|
cpu_state.pc = except_vector;
|
224 |
|
|
cpu_state.ts_current = 0;
|
225 |
|
|
jump_dyn_code(except_vector);
|
226 |
|
|
#endif
|
227 |
33 |
lampret |
}
|