1 |
1452 |
nogj |
/* op_support.c -- Support routines for micro operations
|
2 |
|
|
Copyright (C) 2005 György `nog' Jeney, nog@sdf.lonestar.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 |
|
|
|
21 |
|
|
#include <stdlib.h>
|
22 |
|
|
|
23 |
|
|
#include "config.h"
|
24 |
|
|
|
25 |
|
|
#ifdef HAVE_INTTYPES_H
|
26 |
|
|
#include <inttypes.h>
|
27 |
|
|
#endif
|
28 |
|
|
|
29 |
|
|
#include "port.h"
|
30 |
|
|
#include "arch.h"
|
31 |
|
|
#include "opcode/or32.h"
|
32 |
|
|
#include "sim-config.h"
|
33 |
|
|
#include "spr_defs.h"
|
34 |
|
|
#include "except.h"
|
35 |
|
|
#include "immu.h"
|
36 |
|
|
#include "abstract.h"
|
37 |
|
|
#include "execute.h"
|
38 |
|
|
#include "sched.h"
|
39 |
|
|
|
40 |
|
|
#include "i386_regs.h"
|
41 |
|
|
|
42 |
|
|
#include "dyn_rec.h"
|
43 |
|
|
#include "op_support.h"
|
44 |
|
|
|
45 |
|
|
#include "rec_i386.h"
|
46 |
|
|
|
47 |
|
|
/* Stuff that is really a `micro' operation but is rather big (or for some other
|
48 |
|
|
* reason (like calling exit()) */
|
49 |
|
|
|
50 |
|
|
void upd_reg_from_t(oraddr_t pc)
|
51 |
|
|
{
|
52 |
|
|
int reg;
|
53 |
|
|
|
54 |
|
|
reg = cpu_state.curr_page->ts[(pc & (PAGE_SIZE - 1)) / 2];
|
55 |
|
|
|
56 |
|
|
if(reg & 0x1f)
|
57 |
|
|
cpu_state.reg[reg & 0x1f] = cpu_state.t0;
|
58 |
|
|
|
59 |
|
|
if((reg >> 5) & 0x1f)
|
60 |
|
|
cpu_state.reg[(reg >> 5) & 0x1f] = cpu_state.t1;
|
61 |
|
|
|
62 |
|
|
if((reg >> 10) & 0x1f)
|
63 |
|
|
cpu_state.reg[(reg >> 10) & 0x1f] = cpu_state.t2;
|
64 |
|
|
}
|
65 |
|
|
|
66 |
|
|
void op_support_nop_exit(void)
|
67 |
|
|
{
|
68 |
|
|
upd_reg_from_t(get_pc());
|
69 |
|
|
PRINTF("exit(%"PRIdREG")\n", cpu_state.reg[3]);
|
70 |
|
|
fprintf(stderr, "@reset : cycles %lld, insn #%lld\n",
|
71 |
|
|
runtime.sim.reset_cycles, runtime.cpu.reset_instructions);
|
72 |
|
|
fprintf(stderr, "@exit : cycles %lld, insn #%lld\n", runtime.sim.cycles,
|
73 |
|
|
runtime.cpu.instructions);
|
74 |
|
|
fprintf(stderr, " diff : cycles %lld, insn #%lld\n",
|
75 |
|
|
runtime.sim.cycles - runtime.sim.reset_cycles,
|
76 |
|
|
runtime.cpu.instructions - runtime.cpu.reset_instructions);
|
77 |
|
|
/* FIXME: Implement emulation of a stalled cpu
|
78 |
|
|
if (config.debug.gdb_enabled)
|
79 |
|
|
set_stall_state (1);
|
80 |
|
|
else
|
81 |
|
|
runtime.sim.cont_run = 0;
|
82 |
|
|
*/
|
83 |
|
|
exit(0);
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
void op_support_nop_reset(void)
|
87 |
|
|
{
|
88 |
|
|
PRINTF("****************** counters reset ******************\n");
|
89 |
|
|
PRINTF("cycles %lld, insn #%lld\n", runtime.sim.cycles, runtime.cpu.instructions);
|
90 |
|
|
PRINTF("****************** counters reset ******************\n");
|
91 |
|
|
runtime.sim.reset_cycles = runtime.sim.cycles;
|
92 |
|
|
runtime.cpu.reset_instructions = runtime.cpu.instructions;
|
93 |
|
|
}
|
94 |
|
|
|
95 |
|
|
void op_support_nop_printf(void)
|
96 |
|
|
{
|
97 |
|
|
upd_reg_from_t(get_pc());
|
98 |
|
|
simprintf(cpu_state.reg[4], cpu_state.reg[3]);
|
99 |
|
|
}
|
100 |
|
|
|
101 |
|
|
void op_support_nop_report(void)
|
102 |
|
|
{
|
103 |
|
|
upd_reg_from_t(get_pc());
|
104 |
|
|
PRINTF("report(0x%"PRIxREG");\n", cpu_state.reg[3]);
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
void op_support_nop_report_imm(int imm)
|
108 |
|
|
{
|
109 |
|
|
upd_reg_from_t(get_pc());
|
110 |
|
|
PRINTF("report %i (0x%"PRIxREG");\n", imm, cpu_state.reg[3]);
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
/* Handles a jump */
|
114 |
|
|
/* addr is a VIRTUAL address */
|
115 |
|
|
/* NOTE: We can't use env since this code is compiled like the rest of the
|
116 |
|
|
* simulator (most likely without -fomit-frame-pointer) and thus env will point
|
117 |
|
|
* to some bogus value. */
|
118 |
|
|
void do_jump(oraddr_t addr)
|
119 |
|
|
{
|
120 |
|
|
struct dyn_page *target_dp;
|
121 |
|
|
struct x_ref *xref;
|
122 |
|
|
oraddr_t phys_page;
|
123 |
|
|
|
124 |
|
|
/* The pc is set to the location of the jump in op_set_pc_preemt(_check) and
|
125 |
|
|
* then it is incermented by 4 when the scheduler is run. If a scheduled job
|
126 |
|
|
* so happens to raise an exception cpu_state.delay_insn will still be set and
|
127 |
|
|
* so except_handle will do its pc adjusting magic (ie. -4 from it) and every-
|
128 |
|
|
* thing ends up just working right, except when a scheduled job does not
|
129 |
|
|
* raise an exeception. In that case we set the pc here explicitly */
|
130 |
|
|
set_pc(addr);
|
131 |
|
|
|
132 |
|
|
/* immu_translate must be called after set_pc. If it would be called before
|
133 |
|
|
* it and it issued an ITLB miss then it would appear that the instruction
|
134 |
|
|
* that faulted was the instruction in the delay slot which is incorrect */
|
135 |
|
|
phys_page = immu_translate(addr);
|
136 |
|
|
|
137 |
|
|
/* do_jump is called from the delay slot, which is the jump instruction
|
138 |
|
|
* address + 4. */
|
139 |
|
|
/*
|
140 |
|
|
printf("Recompiled code jumping out to %"PRIxADDR" from %"PRIxADDR"\n",
|
141 |
|
|
phys_page, cpu_state.sprs[SPR_PPC] - 4);
|
142 |
|
|
*/
|
143 |
|
|
|
144 |
|
|
/* immu_translate() adds the hit delay to runtime.sim.mem_cycles but we add it
|
145 |
|
|
* to the cycles when the instruction is executed so if we don't reset it now
|
146 |
|
|
* it will produce wrong results */
|
147 |
|
|
runtime.sim.mem_cycles = 0;
|
148 |
|
|
|
149 |
|
|
target_dp = find_dynd_page(phys_page);
|
150 |
|
|
|
151 |
|
|
if(!target_dp)
|
152 |
|
|
target_dp = new_dp(phys_page);
|
153 |
|
|
|
154 |
|
|
/* Since writes to the 0x0-0xff range do not dirtyfy a page recompile the 0x0
|
155 |
|
|
* page if the jump is to that location */
|
156 |
|
|
if(phys_page < 0x100)
|
157 |
|
|
target_dp->dirty = 1;
|
158 |
|
|
|
159 |
|
|
/* Check if this location is cross-referenced */
|
160 |
|
|
if(!(xref = find_host_x_ref(target_dp->xrefs, phys_page))) {
|
161 |
|
|
target_dp->dirty = 1;
|
162 |
|
|
xref = add_to_xrefs(target_dp, phys_page);
|
163 |
|
|
if(cpu_state.curr_page)
|
164 |
|
|
add_to_held_xrefs(cpu_state.curr_page, xref);
|
165 |
|
|
} else {
|
166 |
|
|
/* Only increment reference count if this page didn't already */
|
167 |
|
|
if(cpu_state.curr_page && !find_held_x_ref(cpu_state.curr_page->held_xrefs,
|
168 |
|
|
phys_page)) {
|
169 |
|
|
xref->ref++;
|
170 |
|
|
add_to_held_xrefs(cpu_state.curr_page, xref);
|
171 |
|
|
}
|
172 |
|
|
}
|
173 |
|
|
|
174 |
|
|
if(target_dp->dirty)
|
175 |
|
|
recompile_page(target_dp);
|
176 |
|
|
|
177 |
|
|
cpu_state.curr_page = target_dp;
|
178 |
|
|
|
179 |
|
|
/* FIXME: If the page is backed by more than one type of memory, this will
|
180 |
|
|
* produce wrong results */
|
181 |
|
|
if(cpu_state.sprs[SPR_SR] & SPR_SR_IME)
|
182 |
|
|
/* Add the mmu hit delay to the cycle counter */
|
183 |
|
|
upd_cycles_dec(target_dp->delayr - config.immu.hitdelay);
|
184 |
|
|
else
|
185 |
|
|
upd_cycles_dec(target_dp->delayr);
|
186 |
|
|
|
187 |
|
|
cpu_state.ts_current = 0;
|
188 |
|
|
|
189 |
|
|
/* Initially this (and do_rfe/handle_except) returned the address that we
|
190 |
|
|
* should jump to and then the recompiled code performed the jump. This was
|
191 |
|
|
* no problem if the jump was trully an interpage jump or if the location
|
192 |
|
|
* didn't need recompileation. If the jump is page local and the page needs
|
193 |
|
|
* recompileation there is a very high probability that the page will move in
|
194 |
|
|
* memory and then the return address that is on the stack will point to
|
195 |
|
|
* memory that has already been freed, sometimes leading to crashes */
|
196 |
|
|
/* This looks like it could really be simpler, but no it can't. The only
|
197 |
|
|
* issue here is the stack: it has to be unwound. This function is called
|
198 |
|
|
* from except_handle, which generally ends up quite high on the stack... */
|
199 |
|
|
or_longjmp(xref->dyn_addr);
|
200 |
|
|
}
|
201 |
|
|
|
202 |
|
|
/* l.rfe is a hard instruction to emulate. One could just call
|
203 |
|
|
* do_jump(cpu_state.sprs[SPR_EPCR_BASE]), but then the location that we jump to
|
204 |
|
|
* will get cross referenced and because the page that contains the exception
|
205 |
|
|
* handlers is very rearly marked as dirty it will accumulate alot of held
|
206 |
|
|
* cross references over time. */
|
207 |
|
|
void do_rfe(void)
|
208 |
|
|
{
|
209 |
|
|
struct dyn_page *target_dp;
|
210 |
|
|
struct x_ref *xref;
|
211 |
|
|
oraddr_t phys_page;
|
212 |
|
|
int already_held = 0;
|
213 |
|
|
|
214 |
|
|
set_pc(cpu_state.sprs[SPR_EPCR_BASE]);
|
215 |
|
|
|
216 |
|
|
phys_page = immu_translate(cpu_state.sprs[SPR_EPCR_BASE]);
|
217 |
|
|
|
218 |
|
|
/* Same reason as in do_jump() */
|
219 |
|
|
runtime.sim.mem_cycles = 0;
|
220 |
|
|
|
221 |
|
|
/* op_do_sched has run by the time this is run, which makes the pc point to
|
222 |
|
|
* the instruction after l.rfe. */
|
223 |
|
|
printf("Returning from exception to %"PRIxADDR" from %"PRIxADDR"\n",
|
224 |
|
|
phys_page, cpu_state.sprs[SPR_PPC]);
|
225 |
|
|
|
226 |
|
|
target_dp = find_dynd_page(phys_page);
|
227 |
|
|
|
228 |
|
|
if(!target_dp)
|
229 |
|
|
target_dp = new_dp(phys_page);
|
230 |
|
|
|
231 |
|
|
/* Since writes to the 0x0-0xff range do not dirtyfy a page recompile the 0x0
|
232 |
|
|
* page if the jump is to that location */
|
233 |
|
|
if(phys_page < 0x100)
|
234 |
|
|
target_dp->dirty = 1;
|
235 |
|
|
|
236 |
|
|
/* Check if this location is cross-referenced */
|
237 |
|
|
if(!(xref = find_host_x_ref(target_dp->xrefs, phys_page))) {
|
238 |
|
|
xref = add_to_xrefs(target_dp, phys_page);
|
239 |
|
|
/* Calling dirtyfy_page is real tempting but if we get to the situation were
|
240 |
|
|
* the l.rfe instruction and the location to which it returns to are on the
|
241 |
|
|
* same page then all the exception cross references will get removed and
|
242 |
|
|
* this will result in excessive recompileations of this page */
|
243 |
|
|
target_dp->dirty = 1;
|
244 |
|
|
|
245 |
|
|
/* There is alot of code (especially in linux) that do loops like this:
|
246 |
|
|
* int a;
|
247 |
|
|
* // Stuff such that b gets on another page than a
|
248 |
|
|
* int b;
|
249 |
|
|
* for(i = 0; i < (some big value); i++) {
|
250 |
|
|
* a = b;
|
251 |
|
|
* // Some more stuff
|
252 |
|
|
* }
|
253 |
|
|
* Here a DTLB miss will happen on every acess to a and b and l.rfe will
|
254 |
|
|
* always return to the same locations but since the previous l.rfe to this
|
255 |
|
|
* page was to a different location the page will get recompiled each time a
|
256 |
|
|
* or b is acessed. This is why the last NUM_RFE_HELD returns are `cached'.
|
257 |
|
|
*/
|
258 |
|
|
if(++cpu_state.rfe_held_xref_pos == NUM_RFE_HELD)
|
259 |
|
|
cpu_state.rfe_held_xref_pos = 0;
|
260 |
|
|
|
261 |
|
|
if(cpu_state.rfe_held_xrefs[cpu_state.rfe_held_xref_pos])
|
262 |
|
|
cpu_state.rfe_held_xrefs[cpu_state.rfe_held_xref_pos]->ref--;
|
263 |
|
|
|
264 |
|
|
cpu_state.rfe_held_xrefs[cpu_state.rfe_held_xref_pos] = xref;
|
265 |
|
|
} else {
|
266 |
|
|
/* Make sure we increase this cross reference's reference count, since it is
|
267 |
|
|
* decremented below. */
|
268 |
|
|
xref->ref++;
|
269 |
|
|
already_held = 1;
|
270 |
|
|
}
|
271 |
|
|
|
272 |
|
|
if(target_dp->dirty)
|
273 |
|
|
recompile_page(target_dp);
|
274 |
|
|
|
275 |
|
|
if(already_held)
|
276 |
|
|
xref->ref--;
|
277 |
|
|
|
278 |
|
|
cpu_state.curr_page = target_dp;
|
279 |
|
|
|
280 |
|
|
/* FIXME: If the page is backed by more than one type of memory, this will
|
281 |
|
|
* produce wrong results */
|
282 |
|
|
if(cpu_state.sprs[SPR_SR] & SPR_SR_IME)
|
283 |
|
|
/* Add the mmu hit delay to the cycle counter */
|
284 |
|
|
upd_cycles_dec(target_dp->delayr - config.immu.hitdelay);
|
285 |
|
|
else
|
286 |
|
|
upd_cycles_dec(target_dp->delayr);
|
287 |
|
|
|
288 |
|
|
cpu_state.ts_current = 0;
|
289 |
|
|
|
290 |
|
|
/* See the comment at the end of do_jump */
|
291 |
|
|
or_longjmp(xref->dyn_addr);
|
292 |
|
|
}
|
293 |
|
|
|
294 |
|
|
/* Handles an exception. */
|
295 |
|
|
void handle_except(oraddr_t except)
|
296 |
|
|
{
|
297 |
|
|
struct dyn_page *target_dp;
|
298 |
|
|
struct x_ref *xref;
|
299 |
|
|
|
300 |
|
|
/* NOTE: It is known when this code will be run. It is therefore not
|
301 |
|
|
* necessary to have to plough through cpu_state.curr_page->ts to store the
|
302 |
|
|
* temporaries. On the other hand, except_handle is also called from the
|
303 |
|
|
* scheduler, therefore we don't know when it is called and we can't move the
|
304 |
|
|
* temporaries to their permanent storeage in the recompiled code. */
|
305 |
|
|
|
306 |
|
|
/* op_do_sched has run by the time we run this, which makes the pc point to
|
307 |
|
|
* the next instruction. */
|
308 |
|
|
printf("Exception %"PRIxADDR" (%s) from %"PRIxADDR"\n", except,
|
309 |
|
|
except_name(except), get_pc() - 4);
|
310 |
|
|
|
311 |
|
|
set_pc(except);
|
312 |
|
|
|
313 |
|
|
target_dp = find_dynd_page(except);
|
314 |
|
|
|
315 |
|
|
if(!target_dp)
|
316 |
|
|
target_dp = new_dp(except);
|
317 |
|
|
|
318 |
|
|
/* Check if this location is cross-referenced */
|
319 |
|
|
if(!(xref = find_host_x_ref(target_dp->xrefs, except))) {
|
320 |
|
|
/* See the comment in do_rfe for why dirtyfy page is not called */
|
321 |
|
|
target_dp->dirty = 1;
|
322 |
|
|
xref = add_to_xrefs(target_dp, except);
|
323 |
|
|
} else {
|
324 |
|
|
/* If this cross reference is scheduled for removal increment its reference
|
325 |
|
|
* count */
|
326 |
|
|
if(!xref->ref)
|
327 |
|
|
xref->ref++;
|
328 |
|
|
}
|
329 |
|
|
|
330 |
|
|
if(target_dp->dirty)
|
331 |
|
|
recompile_page(target_dp);
|
332 |
|
|
|
333 |
|
|
cpu_state.curr_page = target_dp;
|
334 |
|
|
|
335 |
|
|
/* FIXME: If the page is backed by more than one type of memory, this will
|
336 |
|
|
* produce wrong results */
|
337 |
|
|
/* Address translation is disabled above (no need to add hitdelay) */
|
338 |
|
|
upd_cycles_dec(target_dp->delayr);
|
339 |
|
|
|
340 |
|
|
cpu_state.ts_current = 0;
|
341 |
|
|
|
342 |
|
|
/* See the comment at the end of do_jump */
|
343 |
|
|
or_longjmp(xref->dyn_addr);
|
344 |
|
|
}
|
345 |
|
|
|