Debian Codegraph @

stable/mips64.c

raw
1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2005,2006 Christophe Fillot ([email protected])
4 *
5 * XXX TODO: proper context save/restore for CPUs.
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15 #include <assert.h>
16
17 #include "rbtree.h"
18 #include "cpu.h"
19 #include "mips64_mem.h"
20 #include "mips64_exec.h"
21 #include "mips64_jit.h"
22 #include "dynamips.h"
23 #include "memory.h"
24 #include "device.h"
25
26 /* MIPS general purpose registers names */
27 char *mips64_gpr_reg_names[MIPS64_GPR_NR] = {
28 "zr", "at", "v0", "v1", "a0", "a1", "a2", "a3",
29 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
30 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
31 "t8", "t9", "k0", "k1", "gp", "sp", "fp", "ra",
32 };
33
34 /* Cacheability and Coherency Attribute */
35 static int cca_cache_status[8] = {
36 1, 1, 0, 1, 0, 1, 0, 0,
37 };
38
39 /* Get register index given its name */
40 int mips64_get_reg_index(char *name)
41 {
42 int i;
43
44 for(i=0;i<MIPS64_GPR_NR;i++)
45 if (!strcmp(mips64_gpr_reg_names[i],name))
46 return(i);
47
48 return(-1);
49 }
50
51 /* Get cacheability info */
52 int mips64_cca_cached(m_uint8_t val)
53 {
54 return(cca_cache_status[val & 0x03]);
55 }
56
57 /* Reset a MIPS64 CPU */
58 int mips64_reset(cpu_mips_t *cpu)
59 {
60 cpu->pc = MIPS_ROM_PC;
61 cpu->gpr[MIPS_GPR_SP] = MIPS_ROM_SP;
62 cpu->cp0.reg[MIPS_CP0_STATUS] = MIPS_CP0_STATUS_BEV;
63 cpu->cp0.reg[MIPS_CP0_CAUSE] = 0;
64 cpu->cp0.reg[MIPS_CP0_CONFIG] = 0x00c08ff0ULL;
65
66 /* Clear the complete TLB */
67 memset(&cpu->cp0.tlb,0,MIPS64_TLB_MAX_ENTRIES*sizeof(tlb_entry_t));
68
69 /* Restart the MTS subsystem */
70 mips64_set_addr_mode(cpu,32/*64*/); /* zzz */
71 cpu->gen->mts_rebuild(cpu->gen);
72
73 /* Flush JIT structures */
74 mips64_jit_flush(cpu,0);
75 return(0);
76 }
77
78 /* Initialize a MIPS64 processor */
79 int mips64_init(cpu_mips_t *cpu)
80 {
81 cpu->addr_bus_mask = 0xFFFFFFFFFFFFFFFFULL;
82 cpu->cp0.reg[MIPS_CP0_PRID] = MIPS_PRID_R4600;
83 cpu->cp0.tlb_entries = MIPS64_TLB_STD_ENTRIES;
84
85 /* Initialize idle timer */
86 cpu->gen->idle_max = 500;
87 cpu->gen->idle_sleep_time = 30000;
88
89 /* Timer IRQ parameters (default frequency: 250 Hz <=> 4ms period) */
90 cpu->timer_irq_check_itv = 1000;
91 cpu->timer_irq_freq = 250;
92
93 /* Enable fast memory operations */
94 cpu->fast_memop = TRUE;
95
96 /* Enable/Disable direct block jump */
97 cpu->exec_blk_direct_jump = cpu->vm->exec_blk_direct_jump;
98
99 /* Create the IRQ lock (for non-jit architectures) */
100 pthread_mutex_init(&cpu->irq_lock,NULL);
101
102 /* Idle loop mutex and condition */
103 pthread_mutex_init(&cpu->gen->idle_mutex,NULL);
104 pthread_cond_init(&cpu->gen->idle_cond,NULL);
105
106 /* Set the CPU methods */
107 cpu->gen->reg_set = (void *)mips64_reg_set;
108 cpu->gen->reg_dump = (void *)mips64_dump_regs;
109 cpu->gen->mmu_dump = (void *)mips64_tlb_dump;
110 cpu->gen->mmu_raw_dump = (void *)mips64_tlb_raw_dump;
111 cpu->gen->add_breakpoint = (void *)mips64_add_breakpoint;
112 cpu->gen->remove_breakpoint = (void *)mips64_remove_breakpoint;
113 cpu->gen->set_idle_pc = (void *)mips64_set_idle_pc;
114 cpu->gen->get_idling_pc = (void *)mips64_get_idling_pc;
115
116 /* Set the startup parameters */
117 mips64_reset(cpu);
118 return(0);
119 }
120
121 /* Delete a MIPS64 processor */
122 void mips64_delete(cpu_mips_t *cpu)
123 {
124 if (cpu) {
125 mips64_mem_shutdown(cpu);
126 mips64_jit_shutdown(cpu);
127 }
128 }
129
130 /* Set the CPU PRID register */
131 void mips64_set_prid(cpu_mips_t *cpu,m_uint32_t prid)
132 {
133 cpu->cp0.reg[MIPS_CP0_PRID] = prid;
134
135 if ((prid == MIPS_PRID_R7000) || (prid == MIPS_PRID_BCM1250))
136 cpu->cp0.tlb_entries = MIPS64_TLB_MAX_ENTRIES;
137 }
138
139 /* Set idle PC value */
140 void mips64_set_idle_pc(cpu_gen_t *cpu,m_uint64_t addr)
141 {
142 CPU_MIPS64(cpu)->idle_pc = addr;
143 }
144
145 /* Timer IRQ */
146 void *mips64_timer_irq_run(cpu_mips_t *cpu)
147 {
148 pthread_mutex_t umutex = PTHREAD_MUTEX_INITIALIZER;
149 pthread_cond_t ucond = PTHREAD_COND_INITIALIZER;
150 struct timespec t_spc;
151 m_tmcnt_t expire;
152 u_int interval;
153 u_int threshold;
154
155 interval = 1000000 / cpu->timer_irq_freq;
156 threshold = cpu->timer_irq_freq * 10;
157 expire = m_gettime_usec() + interval;
158
159 while(cpu->gen->state != CPU_STATE_HALTED) {
160 pthread_mutex_lock(&umutex);
161 t_spc.tv_sec = expire / 1000000;
162 t_spc.tv_nsec = (expire % 1000000) * 1000;
163 pthread_cond_timedwait(&ucond,&umutex,&t_spc);
164 pthread_mutex_unlock(&umutex);
165
166 if (likely(!cpu->irq_disable) &&
167 likely(cpu->gen->state == CPU_STATE_RUNNING))
168 {
169 cpu->timer_irq_pending++;
170
171 if (unlikely(cpu->timer_irq_pending > threshold)) {
172 cpu->timer_irq_pending = 0;
173 cpu->timer_drift++;
174 #if 0
175 printf("Timer IRQ not accurate (%u pending IRQ): "
176 "reduce the \"--timer-irq-check-itv\" parameter "
177 "(current value: %u)\n",
178 cpu->timer_irq_pending,cpu->timer_irq_check_itv);
179 #endif
180 }
181 }
182
183 expire += interval;
184 }
185
186 return NULL;
187 }
188
189 #define IDLE_HASH_SIZE 8192
190
191 /* Idle PC hash item */
192 struct mips64_idle_pc_hash {
193 m_uint64_t pc;
194 u_int count;
195 struct mips64_idle_pc_hash *next;
196 };
197
198 /* Determine an "idling" PC */
199 int mips64_get_idling_pc(cpu_gen_t *cpu)
200 {
201 cpu_mips_t *mcpu = CPU_MIPS64(cpu);
202 struct mips64_idle_pc_hash **pc_hash,*p;
203 struct cpu_idle_pc *res;
204 u_int h_index;
205 m_uint64_t cur_pc;
206 int i;
207
208 cpu->idle_pc_prop_count = 0;
209
210 if (mcpu->idle_pc != 0) {
211 printf("\nYou already use an idle PC, using the calibration would give "
212 "incorrect results.\n");
213 return(-1);
214 }
215
216 printf("\nPlease wait while gathering statistics...\n");
217
218 pc_hash = calloc(IDLE_HASH_SIZE,sizeof(struct mips64_idle_pc_hash *));
219
220 /* Disable IRQ */
221 mcpu->irq_disable = TRUE;
222
223 /* Take 1000 measures, each mesure every 10ms */
224 for(i=0;i<1000;i++) {
225 cur_pc = mcpu->pc;
226 h_index = (cur_pc >> 2) & (IDLE_HASH_SIZE-1);
227
228 for(p=pc_hash[h_index];p;p=p->next)
229 if (p->pc == cur_pc) {
230 p->count++;
231 break;
232 }
233
234 if (!p) {
235 if ((p = malloc(sizeof(*p)))) {
236 p->pc = cur_pc;
237 p->count = 1;
238 p->next = pc_hash[h_index];
239 pc_hash[h_index] = p;
240 }
241 }
242
243 usleep(10000);
244 }
245
246 /* Select PCs */
247 for(i=0;i<IDLE_HASH_SIZE;i++) {
248 for(p=pc_hash[i];p;p=p->next)
249 if ((p->count >= 20) && (p->count <= 80)) {
250 res = &cpu->idle_pc_prop[cpu->idle_pc_prop_count++];
251
252 res->pc = p->pc;
253 res->count = p->count;
254
255 if (cpu->idle_pc_prop_count >= CPU_IDLE_PC_MAX_RES)
256 goto done;
257 }
258 }
259
260 done:
261 /* Set idle PC */
262 if (cpu->idle_pc_prop_count) {
263 printf("Done. Suggested idling PC:\n");
264
265 for(i=0;i<cpu->idle_pc_prop_count;i++) {
266 printf(" 0x%llx (count=%u)\n",
267 cpu->idle_pc_prop[i].pc,
268 cpu->idle_pc_prop[i].count);
269 }
270
271 printf("Restart the emulator with \"--idle-pc=0x%llx\" (for example)\n",
272 cpu->idle_pc_prop[0].pc);
273 } else {
274 printf("Done. No suggestion for idling PC\n");
275
276 for(i=0;i<IDLE_HASH_SIZE;i++)
277 for(p=pc_hash[i];p;p=p->next) {
278 printf(" 0x%16.16llx (%3u)\n",p->pc,p->count);
279
280 if (cpu->idle_pc_prop_count < CPU_IDLE_PC_MAX_RES) {
281 res = &cpu->idle_pc_prop[cpu->idle_pc_prop_count++];
282
283 res->pc = p->pc;
284 res->count = p->count;
285 }
286 }
287
288 printf("\n");
289 }
290
291 /* Re-enable IRQ */
292 mcpu->irq_disable = FALSE;
293 return(0);
294 }
295
296 /* Set an IRQ (VM IRQ standard routing) */
297 void mips64_vm_set_irq(vm_instance_t *vm,u_int irq)
298 {
299 cpu_mips_t *boot_cpu;
300
301 boot_cpu = CPU_MIPS64(vm->boot_cpu);
302
303 if (boot_cpu->irq_disable) {
304 boot_cpu->irq_pending = 0;
305 return;
306 }
307
308 mips64_set_irq(boot_cpu,irq);
309
310 if (boot_cpu->irq_idle_preempt[irq])
311 cpu_idle_break_wait(vm->boot_cpu);
312 }
313
314 /* Clear an IRQ (VM IRQ standard routing) */
315 void mips64_vm_clear_irq(vm_instance_t *vm,u_int irq)
316 {
317 cpu_mips_t *boot_cpu;
318
319 boot_cpu = CPU_MIPS64(vm->boot_cpu);
320 mips64_clear_irq(boot_cpu,irq);
321 }
322
323 /* Update the IRQ flag (inline) */
324 static forced_inline int mips64_update_irq_flag_fast(cpu_mips_t *cpu)
325 {
326 mips_cp0_t *cp0 = &cpu->cp0;
327 m_uint32_t imask,sreg_mask;
328 m_uint32_t cause;
329
330 cpu->irq_pending = FALSE;
331
332 cause = cp0->reg[MIPS_CP0_CAUSE] & ~MIPS_CP0_CAUSE_IMASK;
333 cp0->reg[MIPS_CP0_CAUSE] = cause | cpu->irq_cause;
334
335 sreg_mask = MIPS_CP0_STATUS_IE|MIPS_CP0_STATUS_EXL|MIPS_CP0_STATUS_ERL;
336
337 if ((cp0->reg[MIPS_CP0_STATUS] & sreg_mask) == MIPS_CP0_STATUS_IE) {
338 imask = cp0->reg[MIPS_CP0_STATUS] & MIPS_CP0_STATUS_IMASK;
339 if (unlikely(cp0->reg[MIPS_CP0_CAUSE] & imask)) {
340 cpu->irq_pending = TRUE;
341 return(TRUE);
342 }
343 }
344
345 return(FALSE);
346 }
347
348 /* Update the IRQ flag */
349 void mips64_update_irq_flag(cpu_mips_t *cpu)
350 {
351 mips64_update_irq_flag_fast(cpu);
352 }
353
354 /* Generate an exception */
355 void mips64_trigger_exception(cpu_mips_t *cpu,u_int exc_code,int bd_slot)
356 {
357 mips_cp0_t *cp0 = &cpu->cp0;
358 m_uint64_t cause,vector;
359
360 /* we don't set EPC if EXL is set */
361 if (!(cp0->reg[MIPS_CP0_STATUS] & MIPS_CP0_STATUS_EXL))
362 {
363 cp0->reg[MIPS_CP0_EPC] = cpu->pc;
364
365 /* keep IM, set exception code and bd slot */
366 cause = cp0->reg[MIPS_CP0_CAUSE] & MIPS_CP0_CAUSE_IMASK;
367
368 if (bd_slot)
369 cause |= MIPS_CP0_CAUSE_BD_SLOT;
370 else
371 cause &= ~MIPS_CP0_CAUSE_BD_SLOT;
372
373 cause |= (exc_code << MIPS_CP0_CAUSE_SHIFT);
374 cp0->reg[MIPS_CP0_CAUSE] = cause;
375
376 /* XXX properly set vector */
377 vector = 0x180ULL;
378 }
379 else
380 {
381 /* keep IM and set exception code */
382 cause = cp0->reg[MIPS_CP0_CAUSE] & MIPS_CP0_CAUSE_IMASK;
383 cause |= (exc_code << MIPS_CP0_CAUSE_SHIFT);
384 cp0->reg[MIPS_CP0_CAUSE] = cause;
385
386 /* set vector */
387 vector = 0x180ULL;
388 }
389
390 /* Set EXL bit in status register */
391 cp0->reg[MIPS_CP0_STATUS] |= MIPS_CP0_STATUS_EXL;
392
393 /* Use bootstrap vectors ? */
394 if (cp0->reg[MIPS_CP0_STATUS] & MIPS_CP0_STATUS_BEV)
395 cpu->pc = 0xffffffffbfc00200ULL + vector;
396 else
397 cpu->pc = 0xffffffff80000000ULL + vector;
398
399 /* Clear the pending IRQ flag */
400 cpu->irq_pending = 0;
401 }
402
403 /*
404 * Increment count register and trigger the timer IRQ if value in compare
405 * register is the same.
406 */
407 fastcall void mips64_exec_inc_cp0_cnt(cpu_mips_t *cpu)
408 {
409 cpu->cp0_virt_cnt_reg++;
410
411 #if 0 /* TIMER_IRQ */
412 mips_cp0_t *cp0 = &cpu->cp0;
413
414 if (unlikely((cpu->cp0_virt_cnt_reg == cpu->cp0_virt_cmp_reg))) {
415 cp0->reg[MIPS_CP0_COUNT] = (m_uint32_t)cp0->reg[MIPS_CP0_COMPARE];
416 mips64_set_irq(cpu,7);
417 mips64_update_irq_flag_fast(cpu);
418 }
419 #endif
420 }
421
422 /* Trigger the Timer IRQ */
423 fastcall void mips64_trigger_timer_irq(cpu_mips_t *cpu)
424 {
425 mips_cp0_t *cp0 = &cpu->cp0;
426
427 cpu->timer_irq_count++;
428
429 cp0->reg[MIPS_CP0_COUNT] = (m_uint32_t)cp0->reg[MIPS_CP0_COMPARE];
430 mips64_set_irq(cpu,7);
431 mips64_update_irq_flag_fast(cpu);
432 }
433
434 /* Execute ERET instruction */
435 fastcall void mips64_exec_eret(cpu_mips_t *cpu)
436 {
437 mips_cp0_t *cp0 = &cpu->cp0;
438
439 if (cp0->reg[MIPS_CP0_STATUS] & MIPS_CP0_STATUS_ERL) {
440 cp0->reg[MIPS_CP0_STATUS] &= ~MIPS_CP0_STATUS_ERL;
441 cpu->pc = cp0->reg[MIPS_CP0_ERR_EPC];
442 } else {
443 cp0->reg[MIPS_CP0_STATUS] &= ~MIPS_CP0_STATUS_EXL;
444 cpu->pc = cp0->reg[MIPS_CP0_EPC];
445 }
446
447 /* We have to clear the LLbit */
448 cpu->ll_bit = 0;
449
450 /* Update the pending IRQ flag */
451 mips64_update_irq_flag_fast(cpu);
452 }
453
454 /* Execute SYSCALL instruction */
455 fastcall void mips64_exec_syscall(cpu_mips_t *cpu)
456 {
457 #if DEBUG_SYSCALL
458 printf("MIPS64: SYSCALL at PC=0x%llx (RA=0x%llx)\n"
459 " a0=0x%llx, a1=0x%llx, a2=0x%llx, a3=0x%llx\n",
460 cpu->pc, cpu->gpr[MIPS_GPR_RA],
461 cpu->gpr[MIPS_GPR_A0], cpu->gpr[MIPS_GPR_A1],
462 cpu->gpr[MIPS_GPR_A2], cpu->gpr[MIPS_GPR_A3]);
463 #endif
464
465 /* XXX TODO: Branch Delay slot */
466 mips64_trigger_exception(cpu,MIPS_CP0_CAUSE_SYSCALL,0);
467 }
468
469 /* Execute BREAK instruction */
470 fastcall void mips64_exec_break(cpu_mips_t *cpu,u_int code)
471 {
472 printf("MIPS64: BREAK instruction (code=%u)\n",code);
473 mips64_dump_regs(cpu->gen);
474
475 /* XXX TODO: Branch Delay slot */
476 mips64_trigger_exception(cpu,MIPS_CP0_CAUSE_BP,0);
477 }
478
479 /* Trigger a Trap Exception */
480 fastcall void mips64_trigger_trap_exception(cpu_mips_t *cpu)
481 {
482 /* XXX TODO: Branch Delay slot */
483 printf("MIPS64: TRAP exception, CPU=%p\n",cpu);
484 mips64_trigger_exception(cpu,MIPS_CP0_CAUSE_TRAP,0);
485 }
486
487 /* Trigger IRQs */
488 fastcall void mips64_trigger_irq(cpu_mips_t *cpu)
489 {
490 if (unlikely(cpu->irq_disable)) {
491 cpu->irq_pending = 0;
492 return;
493 }
494
495 cpu->irq_count++;
496 if (mips64_update_irq_flag_fast(cpu))
497 mips64_trigger_exception(cpu,MIPS_CP0_CAUSE_INTERRUPT,0);
498 else
499 cpu->irq_fp_count++;
500 }
501
502 /* DMFC1 */
503 fastcall void mips64_exec_dmfc1(cpu_mips_t *cpu,u_int gp_reg,u_int cp1_reg)
504 {
505 cpu->gpr[gp_reg] = cpu->fpu.reg[cp1_reg];
506 }
507
508 /* DMTC1 */
509 fastcall void mips64_exec_dmtc1(cpu_mips_t *cpu,u_int gp_reg,u_int cp1_reg)
510 {
511 cpu->fpu.reg[cp1_reg] = cpu->gpr[gp_reg];
512 }
513
514 /* MFC1 */
515 fastcall void mips64_exec_mfc1(cpu_mips_t *cpu,u_int gp_reg,u_int cp1_reg)
516 {
517 m_int64_t val;
518
519 val = cpu->fpu.reg[cp1_reg] & 0xffffffff;
520 cpu->gpr[gp_reg] = sign_extend(val,32);
521 }
522
523 /* MTC1 */
524 fastcall void mips64_exec_mtc1(cpu_mips_t *cpu,u_int gp_reg,u_int cp1_reg)
525 {
526 cpu->fpu.reg[cp1_reg] = cpu->gpr[gp_reg] & 0xffffffff;
527 }
528
529 /* Virtual breakpoint */
530 fastcall void mips64_run_breakpoint(cpu_mips_t *cpu)
531 {
532 cpu_log(cpu->gen,"BREAKPOINT",
533 "Virtual breakpoint reached at PC=0x%llx\n",cpu->pc);
534
535 printf("[[[ Virtual Breakpoint reached at PC=0x%llx RA=0x%llx]]]\n",
536 cpu->pc,cpu->gpr[MIPS_GPR_RA]);
537
538 mips64_dump_regs(cpu->gen);
539 memlog_dump(cpu->gen);
540 }
541
542 /* Add a virtual breakpoint */
543 int mips64_add_breakpoint(cpu_gen_t *cpu,m_uint64_t pc)
544 {
545 cpu_mips_t *mcpu = CPU_MIPS64(cpu);
546 int i;
547
548 for(i=0;i<MIPS64_MAX_BREAKPOINTS;i++)
549 if (!mcpu->breakpoints[i])
550 break;
551
552 if (i == MIPS64_MAX_BREAKPOINTS)
553 return(-1);
554
555 mcpu->breakpoints[i] = pc;
556 mcpu->breakpoints_enabled = TRUE;
557 return(0);
558 }
559
560 /* Remove a virtual breakpoint */
561 void mips64_remove_breakpoint(cpu_gen_t *cpu,m_uint64_t pc)
562 {
563 cpu_mips_t *mcpu = CPU_MIPS64(cpu);
564 int i,j;
565
566 for(i=0;i<MIPS64_MAX_BREAKPOINTS;i++)
567 if (mcpu->breakpoints[i] == pc)
568 {
569 for(j=i;j<MIPS64_MAX_BREAKPOINTS-1;j++)
570 mcpu->breakpoints[j] = mcpu->breakpoints[j+1];
571
572 mcpu->breakpoints[MIPS64_MAX_BREAKPOINTS-1] = 0;
573 }
574
575 for(i=0;i<MIPS64_MAX_BREAKPOINTS;i++)
576 if (mcpu->breakpoints[i] != 0)
577 return;
578
579 mcpu->breakpoints_enabled = FALSE;
580 }
581
582 /* Debugging for register-jump to address 0 */
583 fastcall void mips64_debug_jr0(cpu_mips_t *cpu)
584 {
585 printf("MIPS64: cpu %p jumping to address 0...\n",cpu);
586 mips64_dump_regs(cpu->gen);
587 }
588
589 /* Set a register */
590 void mips64_reg_set(cpu_gen_t *cpu,u_int reg,m_uint64_t val)
591 {
592 if (reg < MIPS64_GPR_NR)
593 CPU_MIPS64(cpu)->gpr[reg] = val;
594 }
595
596 /* Dump registers of a MIPS64 processor */
597 void mips64_dump_regs(cpu_gen_t *cpu)
598 {
599 cpu_mips_t *mcpu = CPU_MIPS64(cpu);
600 mips_insn_t *ptr,insn;
601 char buffer[80];
602 int i;
603
604 printf("MIPS64 Registers:\n");
605
606 for(i=0;i<MIPS64_GPR_NR/2;i++) {
607 printf(" %s ($%2d) = 0x%16.16llx %s ($%2d) = 0x%16.16llx\n",
608 mips64_gpr_reg_names[i*2], i*2, mcpu->gpr[i*2],
609 mips64_gpr_reg_names[(i*2)+1], (i*2)+1, mcpu->gpr[(i*2)+1]);
610 }
611
612 printf(" lo = 0x%16.16llx, hi = 0x%16.16llx\n", mcpu->lo, mcpu->hi);
613 printf(" pc = 0x%16.16llx, ll_bit = %u\n", mcpu->pc, mcpu->ll_bit);
614
615 /* Fetch the current instruction */
616 ptr = mcpu->mem_op_lookup(mcpu,mcpu->pc);
617 if (ptr) {
618 insn = vmtoh32(*ptr);
619
620 if (mips64_dump_insn(buffer,sizeof(buffer),1,mcpu->pc,insn) != -1)
621 printf(" Instruction: %s\n",buffer);
622 }
623
624 printf("\nCP0 Registers:\n");
625
626 for(i=0;i<MIPS64_CP0_REG_NR/2;i++) {
627 printf(" %-10s ($%2d) = 0x%16.16llx %-10s ($%2d) = 0x%16.16llx\n",
628 mips64_cp0_reg_names[i*2], i*2,
629 mips64_cp0_get_reg(mcpu,i*2),
630 mips64_cp0_reg_names[(i*2)+1], (i*2)+1,
631 mips64_cp0_get_reg(mcpu,(i*2)+1));
632 }
633
634 printf("\n IRQ count: %llu, IRQ false positives: %llu, "
635 "IRQ Pending: %u\n",
636 mcpu->irq_count,mcpu->irq_fp_count,mcpu->irq_pending);
637
638 printf(" Timer IRQ count: %llu, pending: %u, timer drift: %u\n\n",
639 mcpu->timer_irq_count,mcpu->timer_irq_pending,mcpu->timer_drift);
640
641 printf(" Device access count: %llu\n",cpu->dev_access_counter);
642 printf("\n");
643 }
644
645 /* Dump a memory block */
646 void mips64_dump_memory(cpu_mips_t *cpu,m_uint64_t vaddr,u_int count)
647 {
648 void *haddr;
649 u_int i;
650
651 for(i=0;i<count;i++,vaddr+=4)
652 {
653 if ((i & 3) == 0)
654 printf("\n 0x%16.16llx: ",vaddr);
655
656 haddr = cpu->mem_op_lookup(cpu,vaddr);
657
658 if (haddr)
659 printf("0x%8.8x ",htovm32(*(m_uint32_t *)haddr));
660 else
661 printf("XXXXXXXXXX ");
662 }
663
664 printf("\n\n");
665 }
666
667 /* Dump the stack */
668 void mips64_dump_stack(cpu_mips_t *cpu,u_int count)
669 {
670 printf("MIPS Stack Dump at 0x%16.16llx:",cpu->gpr[MIPS_GPR_SP]);
671 mips64_dump_memory(cpu,cpu->gpr[MIPS_GPR_SP],count);
672 }
673
674 /* Save the CPU state into a file */
675 int mips64_save_state(cpu_mips_t *cpu,char *filename)
676 {
677 FILE *fd;
678 int i;
679
680 if (!(fd = fopen(filename,"w"))) {
681 perror("mips64_save_state: fopen");
682 return(-1);
683 }
684
685 /* pc, lo and hi */
686 fprintf(fd,"pc: %16.16llx\n",cpu->pc);
687 fprintf(fd,"lo: %16.16llx\n",cpu->lo);
688 fprintf(fd,"hi: %16.16llx\n",cpu->hi);
689
690 /* general purpose registers */
691 for(i=0;i<MIPS64_GPR_NR;i++)
692 fprintf(fd,"%s: %16.16llx\n",
693 mips64_gpr_reg_names[i],cpu->gpr[i]);
694
695 printf("\n");
696
697 /* cp0 registers */
698 for(i=0;i<MIPS64_CP0_REG_NR;i++)
699 fprintf(fd,"%s: %16.16llx\n",
700 mips64_cp0_reg_names[i],cpu->cp0.reg[i]);
701
702 printf("\n");
703
704 /* cp1 registers */
705 for(i=0;i<MIPS64_CP1_REG_NR;i++)
706 fprintf(fd,"fpu%d: %16.16llx\n",i,cpu->fpu.reg[i]);
707
708 printf("\n");
709
710 /* tlb entries */
711 for(i=0;i<cpu->cp0.tlb_entries;i++) {
712 fprintf(fd,"tlb%d_mask: %16.16llx\n",i,cpu->cp0.tlb[i].mask);
713 fprintf(fd,"tlb%d_hi: %16.16llx\n",i,cpu->cp0.tlb[i].hi);
714 fprintf(fd,"tlb%d_lo0: %16.16llx\n",i,cpu->cp0.tlb[i].lo0);
715 fprintf(fd,"tlb%d_lo1: %16.16llx\n",i,cpu->cp0.tlb[i].lo1);
716 }
717
718 fclose(fd);
719 return(0);
720 }
721
722 /* Read a 64-bit unsigned integer */
723 static m_uint64_t mips64_hex_u64(char *str,int *err)
724 {
725 m_uint64_t res = 0;
726 u_char c;
727
728 /* remove leading spaces */
729 while((*str == ' ') || (*str == '\t'))
730 str++;
731
732 while(*str) {
733 c = *str;
734
735 if ((c >= '0') && (c <= '9'))
736 res = (res << 4) + (c - '0');
737
738 if ((c >= 'a') && (c <= 'f'))
739 res = (res << 4) + ((c - 'a') + 10);
740
741 if ((c >= 'A') && (c <= 'F'))
742 res = (res << 4) + ((c - 'A') + 10);
743
744 str++;
745 }
746
747 return(res);
748 }
749
750 /* Restore the CPU state from a file */
751 int mips64_restore_state(cpu_mips_t *cpu,char *filename)
752 {
753 char buffer[4096],*sep,*value,*ep,*field;
754 size_t len;
755 FILE *fd;
756 int index;
757
758 if (!(fd = fopen(filename,"r"))) {
759 perror("mips64_restore_state: fopen");
760 return(-1);
761 }
762
763 while(!feof(fd))
764 {
765 *buffer = 0;
766 fgets(buffer,sizeof(buffer),fd);
767 len = strlen(buffer);
768
769 if (buffer[len-1] == '\n')
770 buffer[len-1] = 0;
771
772 sep = strchr(buffer,':');
773 if (!sep) continue;
774
775 value = sep + 1;
776 *sep = 0;
777
778 /* gpr ? */
779 if ((index = mips64_get_reg_index(buffer)) != -1) {
780 cpu->gpr[index] = mips64_hex_u64(value,NULL);
781 continue;
782 }
783
784 /* cp0 register ? */
785 if ((index = mips64_cp0_get_reg_index(buffer)) != -1) {
786 cpu->cp0.reg[index] = mips64_hex_u64(value,NULL);
787 continue;
788 }
789
790 /* cp1 register ? */
791 if ((len > 3) && (!strncmp(buffer,"fpu",3))) {
792 index = atoi(buffer+3);
793 cpu->fpu.reg[index] = mips64_hex_u64(value,NULL);
794 }
795
796 /* tlb entry ? */
797 if ((len > 3) && (!strncmp(buffer,"tlb",3))) {
798 ep = strchr(buffer,'_');
799
800 if (ep) {
801 index = atoi(buffer+3);
802 field = ep + 1;
803
804 if (!strcmp(field,"mask")) {
805 cpu->cp0.tlb[index].mask = mips64_hex_u64(value,NULL);
806 continue;
807 }
808
809 if (!strcmp(field,"hi")) {
810 cpu->cp0.tlb[index].hi = mips64_hex_u64(value,NULL);
811 continue;
812 }
813
814 if (!strcmp(field,"lo0")) {
815 cpu->cp0.tlb[index].lo0 = mips64_hex_u64(value,NULL);
816 continue;
817 }
818
819 if (!strcmp(field,"lo1")) {
820 cpu->cp0.tlb[index].lo1 = mips64_hex_u64(value,NULL);
821 continue;
822 }
823 }
824 }
825
826 /* pc, lo, hi ? */
827 if (!strcmp(buffer,"pc")) {
828 cpu->pc = mips64_hex_u64(value,NULL);
829 continue;
830 }
831
832 if (!strcmp(buffer,"lo")) {
833 cpu->lo = mips64_hex_u64(value,NULL);
834 continue;
835 }
836
837 if (!strcmp(buffer,"hi")) {
838 cpu->hi = mips64_hex_u64(value,NULL);
839 continue;
840 }
841 }
842
843 mips64_cp0_map_all_tlb_to_mts(cpu);
844
845 mips64_dump_regs(cpu->gen);
846 mips64_tlb_dump(cpu->gen);
847
848 fclose(fd);
849 return(0);
850 }
851
852 /* Load a raw image into the simulated memory */
853 int mips64_load_raw_image(cpu_mips_t *cpu,char *filename,m_uint64_t vaddr)
854 {
855 struct stat file_info;
856 size_t len,clen;
857 m_uint32_t remain;
858 void *haddr;
859 FILE *bfd;
860
861 if (!(bfd = fopen(filename,"r"))) {
862 perror("fopen");
863 return(-1);
864 }
865
866 if (fstat(fileno(bfd),&file_info) == -1) {
867 perror("stat");
868 return(-1);
869 }
870
871 len = file_info.st_size;
872
873 printf("Loading RAW file '%s' at virtual address 0x%llx (size=%lu)\n",
874 filename,vaddr,(u_long)len);
875
876 while(len > 0)
877 {
878 haddr = cpu->mem_op_lookup(cpu,vaddr);
879
880 if (!haddr) {
881 fprintf(stderr,"load_raw_image: invalid load address 0x%llx\n",
882 vaddr);
883 return(-1);
884 }
885
886 if (len > MIPS_MIN_PAGE_SIZE)
887 clen = MIPS_MIN_PAGE_SIZE;
888 else
889 clen = len;
890
891 remain = MIPS_MIN_PAGE_SIZE;
892 remain -= (vaddr - (vaddr & MIPS_MIN_PAGE_MASK));
893
894 clen = m_min(clen,remain);
895
896 if (fread((u_char *)haddr,clen,1,bfd) != 1)
897 break;
898
899 vaddr += clen;
900 len -= clen;
901 }
902
903 fclose(bfd);
904 return(0);
905 }
906
907 /* Load an ELF image into the simulated memory */
908 int mips64_load_elf_image(cpu_mips_t *cpu,char *filename,int skip_load,
909 m_uint32_t *entry_point)
910 {
911 m_uint64_t vaddr;
912 m_uint32_t remain;
913 void *haddr;
914 Elf32_Ehdr *ehdr;
915 Elf32_Shdr *shdr;
916 Elf_Scn *scn;
917 Elf *img_elf;
918 size_t len,clen;
919 _maybe_used char *name;
920 int i,fd;
921 FILE *bfd;
922
923 if (!filename)
924 return(-1);
925
926 #ifdef __CYGWIN__
927 fd = open(filename,O_RDONLY|O_BINARY);
928 #else
929 fd = open(filename,O_RDONLY);
930 #endif
931
932 if (fd == -1) {
933 perror("load_elf_image: open");
934 return(-1);
935 }
936
937 if (elf_version(EV_CURRENT) == EV_NONE) {
938 fprintf(stderr,"load_elf_image: library out of date\n");
939 return(-1);
940 }
941
942 if (!(img_elf = elf_begin(fd,ELF_C_READ,NULL))) {
943 fprintf(stderr,"load_elf_image: elf_begin: %s\n",
944 elf_errmsg(elf_errno()));
945 return(-1);
946 }
947
948 if (!(ehdr = elf32_getehdr(img_elf))) {
949 fprintf(stderr,"load_elf_image: invalid ELF file\n");
950 return(-1);
951 }
952
953 printf("Loading ELF file '%s'...\n",filename);
954 bfd = fdopen(fd,"rb");
955
956 if (!bfd) {
957 perror("load_elf_image: fdopen");
958 return(-1);
959 }
960
961 if (!skip_load) {
962 for(i=0;i<ehdr->e_shnum;i++) {
963 scn = elf_getscn(img_elf,i);
964
965 shdr = elf32_getshdr(scn);
966 name = elf_strptr(img_elf, ehdr->e_shstrndx, (size_t)shdr->sh_name);
967 len = shdr->sh_size;
968
969 if (!(shdr->sh_flags & SHF_ALLOC) || !len)
970 continue;
971
972 fseek(bfd,shdr->sh_offset,SEEK_SET);
973 vaddr = sign_extend(shdr->sh_addr,32);
974
975 if (cpu->vm->debug_level > 0) {
976 printf(" * Adding section at virtual address 0x%8.8llx "
977 "(len=0x%8.8lx)\n",vaddr & 0xFFFFFFFF,(u_long)len);
978 }
979
980 while(len > 0)
981 {
982 haddr = cpu->mem_op_lookup(cpu,vaddr);
983
984 if (!haddr) {
985 fprintf(stderr,"load_elf_image: invalid load address 0x%llx\n",
986 vaddr);
987 return(-1);
988 }
989
990 if (len > MIPS_MIN_PAGE_SIZE)
991 clen = MIPS_MIN_PAGE_SIZE;
992 else
993 clen = len;
994
995 remain = PPC32_MIN_PAGE_SIZE;
996 remain -= (vaddr - (vaddr & PPC32_MIN_PAGE_MASK));
997
998 clen = m_min(clen,remain);
999
1000 if (fread((u_char *)haddr,clen,1,bfd) < 1)
1001 break;
1002
1003 vaddr += clen;
1004 len -= clen;
1005 }
1006 }
1007 } else {
1008 printf("ELF loading skipped, using a ghost RAM file.\n");
1009 }
1010
1011 printf("ELF entry point: 0x%x\n",ehdr->e_entry);
1012
1013 if (entry_point)
1014 *entry_point = ehdr->e_entry;
1015
1016 elf_end(img_elf);
1017 fclose(bfd);
1018 return(0);
1019 }
1020
1021 /* Symbol lookup */
1022 struct symbol *mips64_sym_lookup(cpu_mips_t *cpu,m_uint64_t addr)
1023 {
1024 return(rbtree_lookup(cpu->sym_tree,&addr));
1025 }
1026
1027 /* Insert a new symbol */
1028 struct symbol *mips64_sym_insert(cpu_mips_t *cpu,char *name,m_uint64_t addr)
1029 {
1030 struct symbol *sym;
1031 size_t len;
1032
1033 if (!cpu->sym_tree)
1034 return NULL;
1035
1036 len = strlen(name);
1037
1038 if (!(sym = malloc(len+1+sizeof(*sym))))
1039 return NULL;
1040
1041 memcpy(sym->name,name,len+1);
1042 sym->addr = addr;
1043
1044 if (rbtree_insert(cpu->sym_tree,sym,sym) == -1) {
1045 free(sym);
1046 return NULL;
1047 }
1048
1049 return sym;
1050 }
1051
1052 /* Symbol comparison function */
1053 static int mips64_sym_compare(m_uint64_t *a1,struct symbol *sym)
1054 {
1055 if (*a1 > sym->addr)
1056 return(1);
1057
1058 if (*a1 < sym->addr)
1059 return(-1);
1060
1061 return(0);
1062 }
1063
1064 /* Create the symbol tree */
1065 int mips64_sym_create_tree(cpu_mips_t *cpu)
1066 {
1067 cpu->sym_tree = rbtree_create((tree_fcompare)mips64_sym_compare,NULL);
1068 return(cpu->sym_tree ? 0 : -1);
1069 }
1070
1071 /* Load a symbol file */
1072 int mips64_sym_load_file(cpu_mips_t *cpu,char *filename)
1073 {
1074 char buffer[4096],func_name[128];
1075 m_uint64_t addr;
1076 char sym_type;
1077 FILE *fd;
1078
1079 if (!cpu->sym_tree && (mips64_sym_create_tree(cpu) == -1)) {
1080 fprintf(stderr,"CPU%u: Unable to create symbol tree.\n",cpu->gen->id);
1081 return(-1);
1082 }
1083
1084 if (!(fd = fopen(filename,"r"))) {
1085 perror("load_sym_file: fopen");
1086 return(-1);
1087 }
1088
1089 while(!feof(fd)) {
1090 fgets(buffer,sizeof(buffer),fd);
1091
1092 if (sscanf(buffer,"%llx %c %s",&addr,&sym_type,func_name) == 3) {
1093 mips64_sym_insert(cpu,func_name,addr);
1094 }
1095 }
1096
1097 fclose(fd);
1098 return(0);
1099 }