Debian Codegraph @

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