1 |
dpavlin |
2 |
/* |
2 |
dpavlin |
22 |
* Copyright (C) 2005-2006 Anders Gavare. All rights reserved. |
3 |
dpavlin |
2 |
* |
4 |
|
|
* Redistribution and use in source and binary forms, with or without |
5 |
|
|
* modification, are permitted provided that the following conditions are met: |
6 |
|
|
* |
7 |
|
|
* 1. Redistributions of source code must retain the above copyright |
8 |
|
|
* notice, this list of conditions and the following disclaimer. |
9 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
10 |
|
|
* notice, this list of conditions and the following disclaimer in the |
11 |
|
|
* documentation and/or other materials provided with the distribution. |
12 |
|
|
* 3. The name of the author may not be used to endorse or promote products |
13 |
|
|
* derived from this software without specific prior written permission. |
14 |
|
|
* |
15 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
16 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
19 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
20 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
21 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
22 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
23 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
24 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
25 |
|
|
* SUCH DAMAGE. |
26 |
|
|
* |
27 |
|
|
* |
28 |
dpavlin |
32 |
* $Id: cpu.c,v 1.361 2006/10/25 09:24:05 debug Exp $ |
29 |
dpavlin |
2 |
* |
30 |
|
|
* Common routines for CPU emulation. (Not specific to any CPU type.) |
31 |
|
|
*/ |
32 |
|
|
|
33 |
|
|
#include <stdio.h> |
34 |
|
|
#include <stdlib.h> |
35 |
|
|
#include <sys/types.h> |
36 |
dpavlin |
22 |
#include <sys/mman.h> |
37 |
dpavlin |
2 |
#include <string.h> |
38 |
|
|
|
39 |
|
|
#include "cpu.h" |
40 |
|
|
#include "machine.h" |
41 |
dpavlin |
12 |
#include "memory.h" |
42 |
dpavlin |
2 |
#include "misc.h" |
43 |
dpavlin |
32 |
#include "settings.h" |
44 |
dpavlin |
2 |
|
45 |
|
|
|
46 |
|
|
static struct cpu_family *first_cpu_family = NULL; |
47 |
|
|
|
48 |
|
|
|
49 |
|
|
/* |
50 |
|
|
* cpu_new(): |
51 |
|
|
* |
52 |
|
|
* Create a new cpu object. Each family is tried in sequence until a |
53 |
|
|
* CPU family recognizes the cpu_type_name. |
54 |
dpavlin |
32 |
* |
55 |
|
|
* If there was no match, NULL is returned. Otherwise, a pointer to an |
56 |
|
|
* initialized cpu struct is returned. |
57 |
dpavlin |
2 |
*/ |
58 |
|
|
struct cpu *cpu_new(struct memory *mem, struct machine *machine, |
59 |
|
|
int cpu_id, char *name) |
60 |
|
|
{ |
61 |
dpavlin |
10 |
struct cpu *cpu; |
62 |
dpavlin |
2 |
struct cpu_family *fp; |
63 |
|
|
char *cpu_type_name; |
64 |
dpavlin |
32 |
char tmpstr[30]; |
65 |
dpavlin |
2 |
|
66 |
|
|
if (name == NULL) { |
67 |
|
|
fprintf(stderr, "cpu_new(): cpu name = NULL?\n"); |
68 |
|
|
exit(1); |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
cpu_type_name = strdup(name); |
72 |
|
|
if (cpu_type_name == NULL) { |
73 |
|
|
fprintf(stderr, "cpu_new(): out of memory\n"); |
74 |
|
|
exit(1); |
75 |
|
|
} |
76 |
|
|
|
77 |
dpavlin |
12 |
cpu = zeroed_alloc(sizeof(struct cpu)); |
78 |
dpavlin |
10 |
|
79 |
dpavlin |
32 |
cpu->memory_rw = NULL; |
80 |
|
|
cpu->name = cpu_type_name; |
81 |
|
|
cpu->mem = mem; |
82 |
|
|
cpu->machine = machine; |
83 |
|
|
cpu->cpu_id = cpu_id; |
84 |
|
|
cpu->byte_order = EMUL_UNDEFINED_ENDIAN; |
85 |
|
|
cpu->running = 0; |
86 |
dpavlin |
10 |
|
87 |
dpavlin |
32 |
/* Create settings, and attach to the machine: */ |
88 |
|
|
cpu->settings = settings_new(); |
89 |
|
|
snprintf(tmpstr, sizeof(tmpstr), "cpu[%i]", cpu_id); |
90 |
|
|
settings_add(machine->settings, tmpstr, 1, |
91 |
|
|
SETTINGS_TYPE_SUBSETTINGS, 0, cpu->settings); |
92 |
|
|
|
93 |
|
|
settings_add(cpu->settings, "name", 0, SETTINGS_TYPE_STRING, |
94 |
|
|
SETTINGS_FORMAT_STRING, (void *) &cpu->name); |
95 |
|
|
settings_add(cpu->settings, "running", 0, SETTINGS_TYPE_INT, |
96 |
|
|
SETTINGS_FORMAT_YESNO, (void *) &cpu->running); |
97 |
|
|
|
98 |
dpavlin |
12 |
cpu_create_or_reset_tc(cpu); |
99 |
|
|
|
100 |
dpavlin |
2 |
fp = first_cpu_family; |
101 |
|
|
|
102 |
|
|
while (fp != NULL) { |
103 |
|
|
if (fp->cpu_new != NULL) { |
104 |
dpavlin |
10 |
if (fp->cpu_new(cpu, mem, machine, cpu_id, |
105 |
|
|
cpu_type_name)) { |
106 |
|
|
/* Sanity check: */ |
107 |
|
|
if (cpu->memory_rw == NULL) { |
108 |
|
|
fatal("\ncpu_new(): memory_rw == " |
109 |
|
|
"NULL\n"); |
110 |
dpavlin |
2 |
exit(1); |
111 |
|
|
} |
112 |
dpavlin |
26 |
break; |
113 |
dpavlin |
2 |
} |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
fp = fp->next; |
117 |
|
|
} |
118 |
|
|
|
119 |
dpavlin |
26 |
if (fp == NULL) { |
120 |
|
|
fatal("\ncpu_new(): unknown cpu type '%s'\n", cpu_type_name); |
121 |
|
|
return NULL; |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
fp->init_tables(cpu); |
125 |
|
|
|
126 |
dpavlin |
32 |
if (cpu->byte_order == EMUL_UNDEFINED_ENDIAN) { |
127 |
|
|
fatal("\ncpu_new(): Internal bug: Endianness not set.\n"); |
128 |
|
|
exit(1); |
129 |
|
|
} |
130 |
|
|
|
131 |
dpavlin |
26 |
return cpu; |
132 |
dpavlin |
2 |
} |
133 |
|
|
|
134 |
|
|
|
135 |
|
|
/* |
136 |
dpavlin |
32 |
* cpu_destroy(): |
137 |
|
|
* |
138 |
|
|
* Destroy a cpu object. |
139 |
|
|
*/ |
140 |
|
|
void cpu_destroy(struct cpu *cpu) |
141 |
|
|
{ |
142 |
|
|
settings_remove(cpu->settings, "name"); |
143 |
|
|
settings_remove(cpu->settings, "running"); |
144 |
|
|
|
145 |
|
|
/* Remove any remaining level-1 settings: */ |
146 |
|
|
settings_remove_all(cpu->settings); |
147 |
|
|
|
148 |
|
|
settings_destroy(cpu->settings); |
149 |
|
|
|
150 |
|
|
/* TODO: This assumes that zeroed_alloc() actually succeeded |
151 |
|
|
with using mmap(), and not malloc()! */ |
152 |
|
|
munmap((void *)cpu, sizeof(struct cpu)); |
153 |
|
|
} |
154 |
|
|
|
155 |
|
|
|
156 |
|
|
/* |
157 |
dpavlin |
2 |
* cpu_tlbdump(): |
158 |
|
|
* |
159 |
|
|
* Called from the debugger to dump the TLB in a readable format. |
160 |
|
|
* x is the cpu number to dump, or -1 to dump all CPUs. |
161 |
|
|
* |
162 |
|
|
* If rawflag is nonzero, then the TLB contents isn't formated nicely, |
163 |
|
|
* just dumped. |
164 |
|
|
*/ |
165 |
|
|
void cpu_tlbdump(struct machine *m, int x, int rawflag) |
166 |
|
|
{ |
167 |
|
|
if (m->cpu_family == NULL || m->cpu_family->tlbdump == NULL) |
168 |
|
|
fatal("cpu_tlbdump(): NULL\n"); |
169 |
|
|
else |
170 |
|
|
m->cpu_family->tlbdump(m, x, rawflag); |
171 |
|
|
} |
172 |
|
|
|
173 |
|
|
|
174 |
|
|
/* |
175 |
|
|
* cpu_disassemble_instr(): |
176 |
|
|
* |
177 |
|
|
* Convert an instruction word into human readable format, for instruction |
178 |
|
|
* tracing. |
179 |
|
|
*/ |
180 |
|
|
int cpu_disassemble_instr(struct machine *m, struct cpu *cpu, |
181 |
dpavlin |
24 |
unsigned char *instr, int running, uint64_t addr) |
182 |
dpavlin |
2 |
{ |
183 |
|
|
if (m->cpu_family == NULL || m->cpu_family->disassemble_instr == NULL) { |
184 |
|
|
fatal("cpu_disassemble_instr(): NULL\n"); |
185 |
|
|
return 0; |
186 |
|
|
} else |
187 |
|
|
return m->cpu_family->disassemble_instr(cpu, instr, |
188 |
dpavlin |
24 |
running, addr); |
189 |
dpavlin |
2 |
} |
190 |
|
|
|
191 |
|
|
|
192 |
|
|
/* |
193 |
|
|
* cpu_register_dump(): |
194 |
|
|
* |
195 |
|
|
* Dump cpu registers in a relatively readable format. |
196 |
|
|
* |
197 |
dpavlin |
18 |
* gprs: set to non-zero to dump GPRs. (CPU dependent.) |
198 |
|
|
* coprocs: set bit 0..x to dump registers in coproc 0..x. (CPU dependent.) |
199 |
dpavlin |
2 |
*/ |
200 |
|
|
void cpu_register_dump(struct machine *m, struct cpu *cpu, |
201 |
|
|
int gprs, int coprocs) |
202 |
|
|
{ |
203 |
|
|
if (m->cpu_family == NULL || m->cpu_family->register_dump == NULL) |
204 |
|
|
fatal("cpu_register_dump(): NULL\n"); |
205 |
|
|
else |
206 |
|
|
m->cpu_family->register_dump(cpu, gprs, coprocs); |
207 |
|
|
} |
208 |
|
|
|
209 |
|
|
|
210 |
|
|
/* |
211 |
dpavlin |
24 |
* cpu_gdb_stub(): |
212 |
|
|
* |
213 |
|
|
* Execute a "remote GDB" command. Return value is a pointer to a newly |
214 |
|
|
* allocated response string, if the command was successfully executed. If |
215 |
|
|
* there was an error, NULL is returned. |
216 |
|
|
*/ |
217 |
|
|
char *cpu_gdb_stub(struct cpu *cpu, char *cmd) |
218 |
|
|
{ |
219 |
|
|
if (cpu->machine->cpu_family == NULL || |
220 |
|
|
cpu->machine->cpu_family->gdb_stub == NULL) { |
221 |
|
|
fatal("cpu_gdb_stub(): NULL\n"); |
222 |
|
|
return NULL; |
223 |
|
|
} else |
224 |
|
|
return cpu->machine->cpu_family->gdb_stub(cpu, cmd); |
225 |
|
|
} |
226 |
|
|
|
227 |
|
|
|
228 |
|
|
/* |
229 |
dpavlin |
2 |
* cpu_interrupt(): |
230 |
|
|
* |
231 |
|
|
* Assert an interrupt. |
232 |
|
|
* Return value is 1 if the interrupt was asserted, 0 otherwise. |
233 |
|
|
*/ |
234 |
|
|
int cpu_interrupt(struct cpu *cpu, uint64_t irq_nr) |
235 |
|
|
{ |
236 |
|
|
if (cpu->machine->cpu_family == NULL || |
237 |
|
|
cpu->machine->cpu_family->interrupt == NULL) { |
238 |
|
|
fatal("cpu_interrupt(): NULL\n"); |
239 |
|
|
return 0; |
240 |
|
|
} else |
241 |
|
|
return cpu->machine->cpu_family->interrupt(cpu, irq_nr); |
242 |
|
|
} |
243 |
|
|
|
244 |
|
|
|
245 |
|
|
/* |
246 |
|
|
* cpu_interrupt_ack(): |
247 |
|
|
* |
248 |
|
|
* Acknowledge an interrupt. |
249 |
|
|
* Return value is 1 if the interrupt was deasserted, 0 otherwise. |
250 |
|
|
*/ |
251 |
|
|
int cpu_interrupt_ack(struct cpu *cpu, uint64_t irq_nr) |
252 |
|
|
{ |
253 |
|
|
if (cpu->machine->cpu_family == NULL || |
254 |
|
|
cpu->machine->cpu_family->interrupt_ack == NULL) { |
255 |
|
|
/* debug("cpu_interrupt_ack(): NULL\n"); */ |
256 |
|
|
return 0; |
257 |
|
|
} else |
258 |
|
|
return cpu->machine->cpu_family->interrupt_ack(cpu, irq_nr); |
259 |
|
|
} |
260 |
|
|
|
261 |
|
|
|
262 |
|
|
/* |
263 |
dpavlin |
12 |
* cpu_functioncall_trace(): |
264 |
|
|
* |
265 |
|
|
* This function should be called if machine->show_trace_tree is enabled, and |
266 |
|
|
* a function call is being made. f contains the address of the function. |
267 |
|
|
*/ |
268 |
|
|
void cpu_functioncall_trace(struct cpu *cpu, uint64_t f) |
269 |
|
|
{ |
270 |
|
|
int i, n_args = -1; |
271 |
|
|
char *symbol; |
272 |
|
|
uint64_t offset; |
273 |
|
|
|
274 |
|
|
if (cpu->machine->ncpus > 1) |
275 |
|
|
fatal("cpu%i:\t", cpu->cpu_id); |
276 |
|
|
|
277 |
|
|
cpu->trace_tree_depth ++; |
278 |
dpavlin |
14 |
if (cpu->trace_tree_depth > 100) |
279 |
|
|
cpu->trace_tree_depth = 100; |
280 |
dpavlin |
12 |
for (i=0; i<cpu->trace_tree_depth; i++) |
281 |
|
|
fatal(" "); |
282 |
|
|
|
283 |
|
|
fatal("<"); |
284 |
|
|
symbol = get_symbol_name_and_n_args(&cpu->machine->symbol_context, |
285 |
|
|
f, &offset, &n_args); |
286 |
|
|
if (symbol != NULL) |
287 |
|
|
fatal("%s", symbol); |
288 |
|
|
else { |
289 |
|
|
if (cpu->is_32bit) |
290 |
dpavlin |
24 |
fatal("0x%"PRIx32, (uint32_t) f); |
291 |
dpavlin |
12 |
else |
292 |
dpavlin |
24 |
fatal("0x%"PRIx64, (uint64_t) f); |
293 |
dpavlin |
12 |
} |
294 |
|
|
fatal("("); |
295 |
|
|
|
296 |
|
|
if (cpu->machine->cpu_family->functioncall_trace != NULL) |
297 |
|
|
cpu->machine->cpu_family->functioncall_trace(cpu, f, n_args); |
298 |
|
|
|
299 |
|
|
fatal(")>\n"); |
300 |
dpavlin |
24 |
|
301 |
|
|
#ifdef PRINT_MEMORY_CHECKSUM |
302 |
|
|
/* Temporary hack for finding bugs: */ |
303 |
|
|
fatal("call chksum=%016"PRIx64"\n", memory_checksum(cpu->mem)); |
304 |
|
|
#endif |
305 |
dpavlin |
12 |
} |
306 |
|
|
|
307 |
|
|
|
308 |
|
|
/* |
309 |
|
|
* cpu_functioncall_trace_return(): |
310 |
|
|
* |
311 |
|
|
* This function should be called if machine->show_trace_tree is enabled, and |
312 |
|
|
* a function is being returned from. |
313 |
|
|
* |
314 |
|
|
* TODO: Print return value? This could be implemented similar to the |
315 |
|
|
* cpu->functioncall_trace function call above. |
316 |
|
|
*/ |
317 |
|
|
void cpu_functioncall_trace_return(struct cpu *cpu) |
318 |
|
|
{ |
319 |
|
|
cpu->trace_tree_depth --; |
320 |
|
|
if (cpu->trace_tree_depth < 0) |
321 |
|
|
cpu->trace_tree_depth = 0; |
322 |
|
|
} |
323 |
|
|
|
324 |
|
|
|
325 |
|
|
/* |
326 |
|
|
* cpu_create_or_reset_tc(): |
327 |
|
|
* |
328 |
|
|
* Create the translation cache in memory (ie allocate memory for it), if |
329 |
|
|
* necessary, and then reset it to an initial state. |
330 |
|
|
*/ |
331 |
|
|
void cpu_create_or_reset_tc(struct cpu *cpu) |
332 |
|
|
{ |
333 |
dpavlin |
22 |
size_t s = DYNTRANS_CACHE_SIZE + DYNTRANS_CACHE_MARGIN; |
334 |
dpavlin |
12 |
|
335 |
dpavlin |
24 |
if (cpu->translation_cache == NULL) |
336 |
|
|
cpu->translation_cache = zeroed_alloc(s); |
337 |
dpavlin |
22 |
|
338 |
dpavlin |
12 |
/* Create an empty table at the beginning of the translation cache: */ |
339 |
|
|
memset(cpu->translation_cache, 0, sizeof(uint32_t) |
340 |
|
|
* N_BASE_TABLE_ENTRIES); |
341 |
|
|
|
342 |
|
|
cpu->translation_cache_cur_ofs = |
343 |
|
|
N_BASE_TABLE_ENTRIES * sizeof(uint32_t); |
344 |
|
|
|
345 |
|
|
/* |
346 |
|
|
* There might be other translation pointers that still point to |
347 |
|
|
* within the translation_cache region. Let's invalidate those too: |
348 |
|
|
*/ |
349 |
dpavlin |
14 |
if (cpu->invalidate_code_translation != NULL) |
350 |
|
|
cpu->invalidate_code_translation(cpu, 0, INVALIDATE_ALL); |
351 |
dpavlin |
12 |
} |
352 |
|
|
|
353 |
|
|
|
354 |
|
|
/* |
355 |
dpavlin |
2 |
* cpu_dumpinfo(): |
356 |
|
|
* |
357 |
|
|
* Dumps info about a CPU using debug(). "cpu0: CPUNAME, running" (or similar) |
358 |
dpavlin |
18 |
* is outputed, and it is up to CPU dependent code to complete the line. |
359 |
dpavlin |
2 |
*/ |
360 |
|
|
void cpu_dumpinfo(struct machine *m, struct cpu *cpu) |
361 |
|
|
{ |
362 |
|
|
debug("cpu%i: %s, %s", cpu->cpu_id, cpu->name, |
363 |
|
|
cpu->running? "running" : "stopped"); |
364 |
|
|
|
365 |
|
|
if (m->cpu_family == NULL || m->cpu_family->dumpinfo == NULL) |
366 |
|
|
fatal("cpu_dumpinfo(): NULL\n"); |
367 |
|
|
else |
368 |
|
|
m->cpu_family->dumpinfo(cpu); |
369 |
|
|
} |
370 |
|
|
|
371 |
|
|
|
372 |
|
|
/* |
373 |
|
|
* cpu_list_available_types(): |
374 |
|
|
* |
375 |
|
|
* Print a list of available CPU types for each cpu family. |
376 |
|
|
*/ |
377 |
|
|
void cpu_list_available_types(void) |
378 |
|
|
{ |
379 |
|
|
struct cpu_family *fp; |
380 |
dpavlin |
22 |
int iadd = DEBUG_INDENTATION; |
381 |
dpavlin |
2 |
|
382 |
|
|
fp = first_cpu_family; |
383 |
|
|
|
384 |
|
|
if (fp == NULL) { |
385 |
|
|
debug("No CPUs defined!\n"); |
386 |
|
|
return; |
387 |
|
|
} |
388 |
|
|
|
389 |
|
|
while (fp != NULL) { |
390 |
|
|
debug("%s:\n", fp->name); |
391 |
|
|
debug_indentation(iadd); |
392 |
|
|
if (fp->list_available_types != NULL) |
393 |
|
|
fp->list_available_types(); |
394 |
|
|
else |
395 |
|
|
debug("(internal error: list_available_types" |
396 |
|
|
" = NULL)\n"); |
397 |
|
|
debug_indentation(-iadd); |
398 |
|
|
|
399 |
|
|
fp = fp->next; |
400 |
|
|
} |
401 |
|
|
} |
402 |
|
|
|
403 |
|
|
|
404 |
|
|
/* |
405 |
|
|
* cpu_run_deinit(): |
406 |
|
|
* |
407 |
|
|
* Shuts down all CPUs in a machine when ending a simulation. (This function |
408 |
|
|
* should only need to be called once for each machine.) |
409 |
|
|
*/ |
410 |
dpavlin |
12 |
void cpu_run_deinit(struct machine *machine) |
411 |
dpavlin |
2 |
{ |
412 |
|
|
int te; |
413 |
|
|
|
414 |
|
|
/* |
415 |
dpavlin |
28 |
* Two last ticks of every hardware device. This will allow e.g. |
416 |
|
|
* framebuffers to draw the last updates to the screen before halting. |
417 |
|
|
* |
418 |
|
|
* TODO: This should be refactored when redesigning the mainbus |
419 |
|
|
* concepts! |
420 |
dpavlin |
2 |
*/ |
421 |
|
|
for (te=0; te<machine->n_tick_entries; te++) { |
422 |
|
|
machine->tick_func[te](machine->cpus[0], |
423 |
|
|
machine->tick_extra[te]); |
424 |
|
|
machine->tick_func[te](machine->cpus[0], |
425 |
|
|
machine->tick_extra[te]); |
426 |
|
|
} |
427 |
|
|
|
428 |
dpavlin |
28 |
if (machine->show_nr_of_instructions) |
429 |
dpavlin |
10 |
cpu_show_cycles(machine, 1); |
430 |
dpavlin |
2 |
|
431 |
|
|
fflush(stdout); |
432 |
|
|
} |
433 |
|
|
|
434 |
|
|
|
435 |
|
|
/* |
436 |
|
|
* cpu_show_cycles(): |
437 |
|
|
* |
438 |
dpavlin |
32 |
* If show_nr_of_instructions is on, then print a line to stdout about how |
439 |
|
|
* many instructions/cycles have been executed so far. |
440 |
dpavlin |
2 |
*/ |
441 |
dpavlin |
10 |
void cpu_show_cycles(struct machine *machine, int forced) |
442 |
dpavlin |
2 |
{ |
443 |
|
|
uint64_t offset, pc; |
444 |
|
|
char *symbol; |
445 |
dpavlin |
12 |
int64_t mseconds, ninstrs, is, avg; |
446 |
dpavlin |
2 |
struct timeval tv; |
447 |
dpavlin |
32 |
struct cpu *cpu = machine->cpus[machine->bootstrap_cpu]; |
448 |
dpavlin |
2 |
|
449 |
|
|
static int64_t mseconds_last = 0; |
450 |
|
|
static int64_t ninstrs_last = -1; |
451 |
|
|
|
452 |
dpavlin |
32 |
pc = cpu->pc; |
453 |
dpavlin |
2 |
|
454 |
|
|
gettimeofday(&tv, NULL); |
455 |
dpavlin |
10 |
mseconds = (tv.tv_sec - machine->starttime.tv_sec) * 1000 |
456 |
|
|
+ (tv.tv_usec - machine->starttime.tv_usec) / 1000; |
457 |
dpavlin |
2 |
|
458 |
|
|
if (mseconds == 0) |
459 |
|
|
mseconds = 1; |
460 |
|
|
|
461 |
|
|
if (mseconds - mseconds_last == 0) |
462 |
|
|
mseconds ++; |
463 |
|
|
|
464 |
dpavlin |
28 |
ninstrs = machine->ninstrs_since_gettimeofday; |
465 |
dpavlin |
2 |
|
466 |
|
|
/* RETURN here, unless show_nr_of_instructions (-N) is turned on: */ |
467 |
|
|
if (!machine->show_nr_of_instructions && !forced) |
468 |
|
|
goto do_return; |
469 |
|
|
|
470 |
dpavlin |
28 |
printf("[ %"PRIi64" instrs", (int64_t)machine->ninstrs); |
471 |
dpavlin |
2 |
|
472 |
|
|
/* Instructions per second, and average so far: */ |
473 |
dpavlin |
12 |
is = 1000 * (ninstrs-ninstrs_last) / (mseconds-mseconds_last); |
474 |
|
|
avg = (long long)1000 * ninstrs / mseconds; |
475 |
|
|
if (is < 0) |
476 |
|
|
is = 0; |
477 |
|
|
if (avg < 0) |
478 |
|
|
avg = 0; |
479 |
dpavlin |
2 |
|
480 |
dpavlin |
32 |
if (cpu->has_been_idling) { |
481 |
|
|
printf("; idling"); |
482 |
|
|
cpu->has_been_idling = 0; |
483 |
|
|
} else |
484 |
|
|
printf("; i/s=%"PRIi64" avg=%"PRIi64, is, avg); |
485 |
|
|
|
486 |
dpavlin |
2 |
symbol = get_symbol_name(&machine->symbol_context, pc, &offset); |
487 |
|
|
|
488 |
dpavlin |
12 |
if (machine->ncpus == 1) { |
489 |
dpavlin |
32 |
if (cpu->is_32bit) |
490 |
dpavlin |
24 |
printf("; pc=0x%08"PRIx32, (uint32_t) pc); |
491 |
dpavlin |
12 |
else |
492 |
dpavlin |
24 |
printf("; pc=0x%016"PRIx64, (uint64_t) pc); |
493 |
dpavlin |
12 |
} |
494 |
dpavlin |
2 |
|
495 |
dpavlin |
10 |
if (symbol != NULL) |
496 |
|
|
printf(" <%s>", symbol); |
497 |
|
|
printf(" ]\n"); |
498 |
dpavlin |
2 |
|
499 |
|
|
do_return: |
500 |
|
|
ninstrs_last = ninstrs; |
501 |
|
|
mseconds_last = mseconds; |
502 |
|
|
} |
503 |
|
|
|
504 |
|
|
|
505 |
|
|
/* |
506 |
|
|
* cpu_run_init(): |
507 |
|
|
* |
508 |
|
|
* Prepare to run instructions on all CPUs in this machine. (This function |
509 |
|
|
* should only need to be called once for each machine.) |
510 |
|
|
*/ |
511 |
dpavlin |
12 |
void cpu_run_init(struct machine *machine) |
512 |
dpavlin |
2 |
{ |
513 |
dpavlin |
28 |
machine->ninstrs_flush = 0; |
514 |
|
|
machine->ninstrs = 0; |
515 |
|
|
machine->ninstrs_show = 0; |
516 |
dpavlin |
2 |
|
517 |
|
|
/* For performance measurement: */ |
518 |
|
|
gettimeofday(&machine->starttime, NULL); |
519 |
dpavlin |
28 |
machine->ninstrs_since_gettimeofday = 0; |
520 |
dpavlin |
2 |
} |
521 |
|
|
|
522 |
|
|
|
523 |
|
|
/* |
524 |
|
|
* add_cpu_family(): |
525 |
|
|
* |
526 |
|
|
* Allocates a cpu_family struct and calls an init function for the |
527 |
|
|
* family to fill in reasonable data and pointers. |
528 |
|
|
*/ |
529 |
|
|
static void add_cpu_family(int (*family_init)(struct cpu_family *), int arch) |
530 |
|
|
{ |
531 |
|
|
struct cpu_family *fp, *tmp; |
532 |
|
|
int res; |
533 |
|
|
|
534 |
|
|
fp = malloc(sizeof(struct cpu_family)); |
535 |
|
|
if (fp == NULL) { |
536 |
|
|
fprintf(stderr, "add_cpu_family(): out of memory\n"); |
537 |
|
|
exit(1); |
538 |
|
|
} |
539 |
|
|
memset(fp, 0, sizeof(struct cpu_family)); |
540 |
|
|
|
541 |
|
|
/* |
542 |
|
|
* family_init() returns 1 if the struct has been filled with |
543 |
|
|
* valid data, 0 if suppor for the cpu family isn't compiled |
544 |
|
|
* into the emulator. |
545 |
|
|
*/ |
546 |
|
|
res = family_init(fp); |
547 |
|
|
if (!res) { |
548 |
|
|
free(fp); |
549 |
|
|
return; |
550 |
|
|
} |
551 |
|
|
fp->arch = arch; |
552 |
|
|
fp->next = NULL; |
553 |
|
|
|
554 |
|
|
/* Add last in family chain: */ |
555 |
|
|
tmp = first_cpu_family; |
556 |
|
|
if (tmp == NULL) { |
557 |
|
|
first_cpu_family = fp; |
558 |
|
|
} else { |
559 |
|
|
while (tmp->next != NULL) |
560 |
|
|
tmp = tmp->next; |
561 |
|
|
tmp->next = fp; |
562 |
|
|
} |
563 |
|
|
} |
564 |
|
|
|
565 |
|
|
|
566 |
|
|
/* |
567 |
|
|
* cpu_family_ptr_by_number(): |
568 |
|
|
* |
569 |
|
|
* Returns a pointer to a CPU family based on the ARCH_* integers. |
570 |
|
|
*/ |
571 |
|
|
struct cpu_family *cpu_family_ptr_by_number(int arch) |
572 |
|
|
{ |
573 |
|
|
struct cpu_family *fp; |
574 |
|
|
fp = first_cpu_family; |
575 |
|
|
|
576 |
|
|
/* YUCK! This is too hardcoded! TODO */ |
577 |
|
|
|
578 |
|
|
while (fp != NULL) { |
579 |
|
|
if (arch == fp->arch) |
580 |
|
|
return fp; |
581 |
|
|
fp = fp->next; |
582 |
|
|
} |
583 |
|
|
|
584 |
|
|
return NULL; |
585 |
|
|
} |
586 |
|
|
|
587 |
|
|
|
588 |
|
|
/* |
589 |
|
|
* cpu_init(): |
590 |
|
|
* |
591 |
|
|
* Should be called before any other cpu_*() function. |
592 |
dpavlin |
32 |
* |
593 |
|
|
* TODO: Make this nicer by moving out the conditional stuff to |
594 |
|
|
* an automagically generated file? Or a define in config.h? |
595 |
dpavlin |
2 |
*/ |
596 |
|
|
void cpu_init(void) |
597 |
|
|
{ |
598 |
|
|
/* Note: These are registered in alphabetic order. */ |
599 |
dpavlin |
12 |
|
600 |
|
|
#ifdef ENABLE_ALPHA |
601 |
|
|
add_cpu_family(alpha_cpu_family_init, ARCH_ALPHA); |
602 |
|
|
#endif |
603 |
|
|
|
604 |
|
|
#ifdef ENABLE_ARM |
605 |
dpavlin |
6 |
add_cpu_family(arm_cpu_family_init, ARCH_ARM); |
606 |
dpavlin |
12 |
#endif |
607 |
|
|
|
608 |
dpavlin |
14 |
#ifdef ENABLE_AVR |
609 |
|
|
add_cpu_family(avr_cpu_family_init, ARCH_AVR); |
610 |
|
|
#endif |
611 |
|
|
|
612 |
dpavlin |
32 |
#ifdef ENABLE_AVR32 |
613 |
|
|
add_cpu_family(avr32_cpu_family_init, ARCH_AVR32); |
614 |
|
|
#endif |
615 |
|
|
|
616 |
|
|
#ifdef ENABLE_RCA180X |
617 |
|
|
add_cpu_family(rca180x_cpu_family_init, ARCH_RCA180X); |
618 |
|
|
#endif |
619 |
|
|
|
620 |
dpavlin |
14 |
#ifdef ENABLE_HPPA |
621 |
|
|
add_cpu_family(hppa_cpu_family_init, ARCH_HPPA); |
622 |
|
|
#endif |
623 |
|
|
|
624 |
|
|
#ifdef ENABLE_I960 |
625 |
|
|
add_cpu_family(i960_cpu_family_init, ARCH_I960); |
626 |
|
|
#endif |
627 |
|
|
|
628 |
dpavlin |
12 |
#ifdef ENABLE_IA64 |
629 |
|
|
add_cpu_family(ia64_cpu_family_init, ARCH_IA64); |
630 |
|
|
#endif |
631 |
|
|
|
632 |
|
|
#ifdef ENABLE_M68K |
633 |
|
|
add_cpu_family(m68k_cpu_family_init, ARCH_M68K); |
634 |
|
|
#endif |
635 |
|
|
|
636 |
|
|
#ifdef ENABLE_MIPS |
637 |
dpavlin |
2 |
add_cpu_family(mips_cpu_family_init, ARCH_MIPS); |
638 |
dpavlin |
12 |
#endif |
639 |
|
|
|
640 |
|
|
#ifdef ENABLE_PPC |
641 |
dpavlin |
2 |
add_cpu_family(ppc_cpu_family_init, ARCH_PPC); |
642 |
dpavlin |
12 |
#endif |
643 |
|
|
|
644 |
dpavlin |
14 |
#ifdef ENABLE_SH |
645 |
|
|
add_cpu_family(sh_cpu_family_init, ARCH_SH); |
646 |
|
|
#endif |
647 |
|
|
|
648 |
dpavlin |
12 |
#ifdef ENABLE_SPARC |
649 |
|
|
add_cpu_family(sparc_cpu_family_init, ARCH_SPARC); |
650 |
|
|
#endif |
651 |
|
|
|
652 |
dpavlin |
28 |
#ifdef ENABLE_TRANSPUTER |
653 |
|
|
add_cpu_family(transputer_cpu_family_init, ARCH_TRANSPUTER); |
654 |
|
|
#endif |
655 |
|
|
|
656 |
dpavlin |
12 |
#ifdef ENABLE_X86 |
657 |
dpavlin |
4 |
add_cpu_family(x86_cpu_family_init, ARCH_X86); |
658 |
dpavlin |
12 |
#endif |
659 |
dpavlin |
2 |
} |
660 |
|
|
|