Debian Codegraph @

stable/ppc32.c

raw
1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2005,2006 Christophe Fillot ([email protected])
4 *
5 * PowerPC (32-bit) generic routines.
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 "dynamips.h"
20 #include "memory.h"
21 #include "device.h"
22 #include "ppc32_mem.h"
23 #include "ppc32_exec.h"
24 #include "ppc32_jit.h"
25
26 /* Reset a PowerPC CPU */
27 int ppc32_reset(cpu_ppc_t *cpu)
28 {
29 cpu->ia = PPC32_ROM_START;
30 cpu->gpr[1] = PPC32_ROM_SP;
31 cpu->msr = PPC32_MSR_IP;
32
33 /* Restart the MTS subsystem */
34 ppc32_mem_restart(cpu);
35
36 /* Flush JIT structures */
37 ppc32_jit_flush(cpu,0);
38 return(0);
39 }
40
41 /* Initialize a PowerPC processor */
42 int ppc32_init(cpu_ppc_t *cpu)
43 {
44 /* Initialize JIT operations */
45 jit_op_init_cpu(cpu->gen);
46
47 /* Initialize idle timer */
48 cpu->gen->idle_max = 500;
49 cpu->gen->idle_sleep_time = 30000;
50
51 /* Timer IRQ parameters (default frequency: 250 Hz <=> 4ms period) */
52 cpu->timer_irq_check_itv = 1000;
53 cpu->timer_irq_freq = 250;
54
55 /* Enable/disable direct block jump */
56 cpu->exec_blk_direct_jump = cpu->vm->exec_blk_direct_jump;
57
58 /* Idle loop mutex and condition */
59 pthread_mutex_init(&cpu->gen->idle_mutex,NULL);
60 pthread_cond_init(&cpu->gen->idle_cond,NULL);
61
62 /* Set the CPU methods */
63 cpu->gen->reg_set = (void *)ppc32_reg_set;
64 cpu->gen->reg_dump = (void *)ppc32_dump_regs;
65 cpu->gen->mmu_dump = (void *)ppc32_dump_mmu;
66 cpu->gen->mmu_raw_dump = (void *)ppc32_dump_mmu;
67 cpu->gen->add_breakpoint = (void *)ppc32_add_breakpoint;
68 cpu->gen->remove_breakpoint = (void *)ppc32_remove_breakpoint;
69 cpu->gen->set_idle_pc = (void *)ppc32_set_idle_pc;
70 cpu->gen->get_idling_pc = (void *)ppc32_get_idling_pc;
71
72 /* zzz */
73 memset(cpu->vtlb,0xFF,sizeof(cpu->vtlb));
74
75 /* Set the startup parameters */
76 ppc32_reset(cpu);
77 return(0);
78 }
79
80 /* Delete a PowerPC processor */
81 void ppc32_delete(cpu_ppc_t *cpu)
82 {
83 if (cpu) {
84 ppc32_mem_shutdown(cpu);
85 ppc32_jit_shutdown(cpu);
86 }
87 }
88
89 /* Set the processor version register (PVR) */
90 void ppc32_set_pvr(cpu_ppc_t *cpu,m_uint32_t pvr)
91 {
92 cpu->pvr = pvr;
93 ppc32_mem_restart(cpu);
94 }
95
96 /* Set idle PC value */
97 void ppc32_set_idle_pc(cpu_gen_t *cpu,m_uint64_t addr)
98 {
99 CPU_PPC32(cpu)->idle_pc = (m_uint32_t)addr;
100 }
101
102 /* Timer IRQ */
103 void *ppc32_timer_irq_run(cpu_ppc_t *cpu)
104 {
105 pthread_mutex_t umutex = PTHREAD_MUTEX_INITIALIZER;
106 pthread_cond_t ucond = PTHREAD_COND_INITIALIZER;
107 struct timespec t_spc;
108 m_tmcnt_t expire;
109 u_int interval;
110 u_int threshold;
111
112 #if 0
113 while(!cpu->timer_irq_armed)
114 sleep(1);
115 #endif
116
117 interval = 1000000 / cpu->timer_irq_freq;
118 threshold = cpu->timer_irq_freq * 10;
119 expire = m_gettime_usec() + interval;
120
121 while(cpu->gen->state != CPU_STATE_HALTED) {
122 pthread_mutex_lock(&umutex);
123 t_spc.tv_sec = expire / 1000000;
124 t_spc.tv_nsec = (expire % 1000000) * 1000;
125 pthread_cond_timedwait(&ucond,&umutex,&t_spc);
126 pthread_mutex_unlock(&umutex);
127
128 if (likely(!cpu->irq_disable) &&
129 likely(cpu->gen->state == CPU_STATE_RUNNING) &&
130 likely(cpu->msr & PPC32_MSR_EE))
131 {
132 cpu->timer_irq_pending++;
133
134 if (unlikely(cpu->timer_irq_pending > threshold)) {
135 cpu->timer_irq_pending = 0;
136 cpu->timer_drift++;
137 #if 0
138 printf("Timer IRQ not accurate (%u pending IRQ): "
139 "reduce the \"--timer-irq-check-itv\" parameter "
140 "(current value: %u)\n",
141 cpu->timer_irq_pending,cpu->timer_irq_check_itv);
142 #endif
143 }
144 }
145
146 expire += interval;
147 }
148
149 return NULL;
150 }
151
152 #define IDLE_HASH_SIZE 8192
153
154 /* Idle PC hash item */
155 struct ppc32_idle_pc_hash {
156 m_uint32_t ia;
157 u_int count;
158 struct ppc32_idle_pc_hash *next;
159 };
160
161 /* Determine an "idling" PC */
162 int ppc32_get_idling_pc(cpu_gen_t *cpu)
163 {
164 cpu_ppc_t *pcpu = CPU_PPC32(cpu);
165 struct ppc32_idle_pc_hash **pc_hash,*p;
166 struct cpu_idle_pc *res;
167 u_int h_index;
168 m_uint32_t cur_ia;
169 int i;
170
171 cpu->idle_pc_prop_count = 0;
172
173 if (pcpu->idle_pc != 0) {
174 printf("\nYou already use an idle PC, using the calibration would give "
175 "incorrect results.\n");
176 return(-1);
177 }
178
179 printf("\nPlease wait while gathering statistics...\n");
180
181 pc_hash = calloc(IDLE_HASH_SIZE,sizeof(struct ppc32_idle_pc_hash *));
182
183 /* Disable IRQ */
184 pcpu->irq_disable = TRUE;
185
186 /* Take 1000 measures, each mesure every 10ms */
187 for(i=0;i<1000;i++) {
188 cur_ia = pcpu->ia;
189 h_index = (cur_ia >> 2) & (IDLE_HASH_SIZE-1);
190
191 for(p=pc_hash[h_index];p;p=p->next)
192 if (p->ia == cur_ia) {
193 p->count++;
194 break;
195 }
196
197 if (!p) {
198 if ((p = malloc(sizeof(*p)))) {
199 p->ia = cur_ia;
200 p->count = 1;
201 p->next = pc_hash[h_index];
202 pc_hash[h_index] = p;
203 }
204 }
205
206 usleep(10000);
207 }
208
209 /* Select PCs */
210 for(i=0;i<IDLE_HASH_SIZE;i++) {
211 for(p=pc_hash[i];p;p=p->next)
212 if ((p->count >= 20) && (p->count <= 80)) {
213 res = &cpu->idle_pc_prop[cpu->idle_pc_prop_count++];
214
215 res->pc = p->ia;
216 res->count = p->count;
217
218 if (cpu->idle_pc_prop_count >= CPU_IDLE_PC_MAX_RES)
219 goto done;
220 }
221 }
222
223 done:
224 /* Set idle PC */
225 if (cpu->idle_pc_prop_count) {
226 printf("Done. Suggested idling PC:\n");
227
228 for(i=0;i<cpu->idle_pc_prop_count;i++) {
229 printf(" 0x%llx (count=%u)\n",
230 cpu->idle_pc_prop[i].pc,
231 cpu->idle_pc_prop[i].count);
232 }
233
234 printf("Restart the emulator with \"--idle-pc=0x%llx\" (for example)\n",
235 cpu->idle_pc_prop[0].pc);
236 } else {
237 printf("Done. No suggestion for idling PC, dumping the full table:\n");
238
239 for(i=0;i<IDLE_HASH_SIZE;i++)
240 for(p=pc_hash[i];p;p=p->next) {
241 printf(" 0x%8.8x (%3u)\n",p->ia,p->count);
242
243 if (cpu->idle_pc_prop_count < CPU_IDLE_PC_MAX_RES) {
244 res = &cpu->idle_pc_prop[cpu->idle_pc_prop_count++];
245
246 res->pc = p->ia;
247 res->count = p->count;
248 }
249 }
250
251 printf("\n");
252 }
253
254 /* Re-enable IRQ */
255 pcpu->irq_disable = FALSE;
256 return(0);
257 }
258
259 #if 0
260 /* Set an IRQ (VM IRQ standard routing) */
261 void ppc32_vm_set_irq(vm_instance_t *vm,u_int irq)
262 {
263 cpu_ppc_t *boot_cpu;
264
265 boot_cpu = CPU_PPC32(vm->boot_cpu);
266
267 if (boot_cpu->irq_disable) {
268 boot_cpu->irq_pending = 0;
269 return;
270 }
271
272 ppc32_set_irq(boot_cpu,irq);
273
274 if (boot_cpu->irq_idle_preempt[irq])
275 cpu_idle_break_wait(vm->boot_cpu);
276 }
277
278 /* Clear an IRQ (VM IRQ standard routing) */
279 void ppc32_vm_clear_irq(vm_instance_t *vm,u_int irq)
280 {
281 cpu_ppc_t *boot_cpu;
282
283 boot_cpu = CPU_PPC32(vm->boot_cpu);
284 ppc32_clear_irq(boot_cpu,irq);
285 }
286 #endif
287
288 /* Generate an exception */
289 void ppc32_trigger_exception(cpu_ppc_t *cpu,u_int exc_vector)
290 {
291 //printf("TRIGGER_EXCEPTION: saving cpu->ia=0x%8.8x, msr=0x%8.8x\n",
292 // cpu->ia,cpu->msr);
293
294 /* Save the return instruction address */
295 cpu->srr0 = cpu->ia;
296
297 if (exc_vector == PPC32_EXC_SYSCALL)
298 cpu->srr0 += sizeof(ppc_insn_t);
299
300 //printf("SRR0 = 0x%8.8x\n",cpu->srr0);
301
302 /* Save Machine State Register (MSR) */
303 cpu->srr1 = cpu->msr & PPC32_EXC_SRR1_MASK;
304
305 //printf("SRR1 = 0x%8.8x\n",cpu->srr1);
306
307 /* Set the new SRR value */
308 cpu->msr &= ~PPC32_EXC_MSR_MASK;
309 cpu->irq_check = FALSE;
310
311 //printf("MSR = 0x%8.8x\n",cpu->msr);
312
313 /* Use bootstrap vectors ? */
314 if (cpu->msr & PPC32_MSR_IP)
315 cpu->ia = 0xFFF00000 + exc_vector;
316 else
317 cpu->ia = exc_vector;
318 }
319
320 /* Trigger IRQs */
321 fastcall void ppc32_trigger_irq(cpu_ppc_t *cpu)
322 {
323 if (unlikely(cpu->irq_disable)) {
324 cpu->irq_pending = FALSE;
325 cpu->irq_check = FALSE;
326 return;
327 }
328
329 /* Clear the IRQ check flag */
330 cpu->irq_check = FALSE;
331
332 if (cpu->irq_pending && (cpu->msr & PPC32_MSR_EE)) {
333 cpu->irq_count++;
334 cpu->irq_pending = FALSE;
335 ppc32_trigger_exception(cpu,PPC32_EXC_EXT);
336 }
337 }
338
339 /* Trigger the decrementer exception */
340 void ppc32_trigger_timer_irq(cpu_ppc_t *cpu)
341 {
342 cpu->timer_irq_count++;
343
344 if (cpu->msr & PPC32_MSR_EE)
345 ppc32_trigger_exception(cpu,PPC32_EXC_DEC);
346 }
347
348 /* Virtual breakpoint */
349 fastcall void ppc32_run_breakpoint(cpu_ppc_t *cpu)
350 {
351 cpu_log(cpu->gen,"BREAKPOINT",
352 "Virtual breakpoint reached at IA=0x%8.8x\n",cpu->ia);
353
354 printf("[[[ Virtual Breakpoint reached at IA=0x%8.8x LR=0x%8.8x]]]\n",
355 cpu->ia,cpu->lr);
356
357 ppc32_dump_regs(cpu->gen);
358 }
359
360 /* Add a virtual breakpoint */
361 int ppc32_add_breakpoint(cpu_gen_t *cpu,m_uint64_t ia)
362 {
363 cpu_ppc_t *pcpu = CPU_PPC32(cpu);
364 int i;
365
366 for(i=0;i<PPC32_MAX_BREAKPOINTS;i++)
367 if (!pcpu->breakpoints[i])
368 break;
369
370 if (i == PPC32_MAX_BREAKPOINTS)
371 return(-1);
372
373 pcpu->breakpoints[i] = ia;
374 pcpu->breakpoints_enabled = TRUE;
375 return(0);
376 }
377
378 /* Remove a virtual breakpoint */
379 void ppc32_remove_breakpoint(cpu_gen_t *cpu,m_uint64_t ia)
380 {
381 cpu_ppc_t *pcpu = CPU_PPC32(cpu);
382 int i,j;
383
384 for(i=0;i<PPC32_MAX_BREAKPOINTS;i++)
385 if (pcpu->breakpoints[i] == ia)
386 {
387 for(j=i;j<PPC32_MAX_BREAKPOINTS-1;j++)
388 pcpu->breakpoints[j] = pcpu->breakpoints[j+1];
389
390 pcpu->breakpoints[PPC32_MAX_BREAKPOINTS-1] = 0;
391 }
392
393 for(i=0;i<PPC32_MAX_BREAKPOINTS;i++)
394 if (pcpu->breakpoints[i] != 0)
395 return;
396
397 pcpu->breakpoints_enabled = FALSE;
398 }
399
400 /* Set a register */
401 void ppc32_reg_set(cpu_gen_t *cpu,u_int reg,m_uint64_t val)
402 {
403 if (reg < PPC32_GPR_NR)
404 CPU_PPC32(cpu)->gpr[reg] = (m_uint32_t)val;
405 }
406
407 /* Dump registers of a PowerPC processor */
408 void ppc32_dump_regs(cpu_gen_t *cpu)
409 {
410 cpu_ppc_t *pcpu = CPU_PPC32(cpu);
411 int i;
412
413 printf("PowerPC Registers:\n");
414
415 for(i=0;i<PPC32_GPR_NR/4;i++) {
416 printf(" $%2d = 0x%8.8x $%2d = 0x%8.8x"
417 " $%2d = 0x%8.8x $%2d = 0x%8.8x\n",
418 i*4, pcpu->gpr[i*4], (i*4)+1, pcpu->gpr[(i*4)+1],
419 (i*4)+2, pcpu->gpr[(i*4)+2], (i*4)+3, pcpu->gpr[(i*4)+3]);
420 }
421
422 printf("\n");
423 printf(" ia = 0x%8.8x, lr = 0x%8.8x\n", pcpu->ia, pcpu->lr);
424 printf(" cr = 0x%8.8x, msr = 0x%8.8x, xer = 0x%8.8x, dec = 0x%8.8x\n",
425 ppc32_get_cr(pcpu), pcpu->msr,
426 pcpu->xer | (pcpu->xer_ca << PPC32_XER_CA_BIT),
427 pcpu->dec);
428
429 printf(" sprg[0] = 0x%8.8x, sprg[1] = 0x%8.8x\n",
430 pcpu->sprg[0],pcpu->sprg[1]);
431
432 printf(" sprg[2] = 0x%8.8x, sprg[3] = 0x%8.8x\n",
433 pcpu->sprg[2],pcpu->sprg[3]);
434
435 printf("\n IRQ count: %llu, IRQ false positives: %llu, "
436 "IRQ Pending: %u, IRQ Check: %s\n",
437 pcpu->irq_count,pcpu->irq_fp_count,pcpu->irq_pending,
438 pcpu->irq_check ? "yes" : "no");
439
440 printf(" Timer IRQ count: %llu, pending: %u, timer drift: %u\n\n",
441 pcpu->timer_irq_count,pcpu->timer_irq_pending,pcpu->timer_drift);
442
443 printf(" Device access count: %llu\n",cpu->dev_access_counter);
444
445 printf("\n");
446 }
447
448 /* Dump BAT registers */
449 static void ppc32_dump_bat(cpu_ppc_t *cpu,int index)
450 {
451 int i;
452
453 for(i=0;i<PPC32_BAT_NR;i++)
454 printf(" BAT[%d] = 0x%8.8x 0x%8.8x\n",
455 i,cpu->bat[index][i].reg[0],cpu->bat[index][i].reg[1]);
456 }
457
458 /* Dump MMU registers */
459 void ppc32_dump_mmu(cpu_gen_t *cpu)
460 {
461 cpu_ppc_t *pcpu = CPU_PPC32(cpu);
462 int i;
463
464 printf("PowerPC MMU Registers:\n");
465
466 printf(" - IBAT Registers:\n");
467 ppc32_dump_bat(pcpu,PPC32_IBAT_IDX);
468
469 printf(" - DBAT Registers:\n");
470 ppc32_dump_bat(pcpu,PPC32_DBAT_IDX);
471
472 printf(" - Segment Registers:\n");
473 for(i=0;i<PPC32_SR_NR;i++)
474 printf(" SR[%d] = 0x%8.8x\n",i,pcpu->sr[i]);
475
476 printf(" - SDR1: 0x%8.8x\n",pcpu->sdr1);
477 }
478
479 /* Load a raw image into the simulated memory */
480 int ppc32_load_raw_image(cpu_ppc_t *cpu,char *filename,m_uint32_t vaddr)
481 {
482 struct stat file_info;
483 size_t len,clen;
484 m_uint32_t remain;
485 void *haddr;
486 FILE *bfd;
487
488 if (!(bfd = fopen(filename,"r"))) {
489 perror("fopen");
490 return(-1);
491 }
492
493 if (fstat(fileno(bfd),&file_info) == -1) {
494 perror("stat");
495 return(-1);
496 }
497
498 len = file_info.st_size;
499
500 printf("Loading RAW file '%s' at virtual address 0x%8.8x (size=%lu)\n",
501 filename,vaddr,(u_long)len);
502
503 while(len > 0)
504 {
505 haddr = cpu->mem_op_lookup(cpu,vaddr,PPC32_MTS_DCACHE);
506
507 if (!haddr) {
508 fprintf(stderr,"load_raw_image: invalid load address 0x%8.8x\n",
509 vaddr);
510 return(-1);
511 }
512
513 if (len > PPC32_MIN_PAGE_SIZE)
514 clen = PPC32_MIN_PAGE_SIZE;
515 else
516 clen = len;
517
518 remain = MIPS_MIN_PAGE_SIZE;
519 remain -= (vaddr - (vaddr & MIPS_MIN_PAGE_MASK));
520
521 clen = m_min(clen,remain);
522
523 if (fread((u_char *)haddr,clen,1,bfd) != 1)
524 break;
525
526 vaddr += clen;
527 len -= clen;
528 }
529
530 fclose(bfd);
531 return(0);
532 }
533
534 /* Load an ELF image into the simulated memory */
535 int ppc32_load_elf_image(cpu_ppc_t *cpu,char *filename,int skip_load,
536 m_uint32_t *entry_point)
537 {
538 m_uint32_t vaddr,remain;
539 void *haddr;
540 Elf32_Ehdr *ehdr;
541 Elf32_Shdr *shdr;
542 Elf_Scn *scn;
543 Elf *img_elf;
544 size_t len,clen;
545 _maybe_used char *name;
546 int i,fd;
547 FILE *bfd;
548
549 if (!filename)
550 return(-1);
551
552 #ifdef __CYGWIN__
553 fd = open(filename,O_RDONLY|O_BINARY);
554 #else
555 fd = open(filename,O_RDONLY);
556 #endif
557
558 if (fd == -1) {
559 perror("load_elf_image: open");
560 return(-1);
561 }
562
563 if (elf_version(EV_CURRENT) == EV_NONE) {
564 fprintf(stderr,"load_elf_image: library out of date\n");
565 return(-1);
566 }
567
568 if (!(img_elf = elf_begin(fd,ELF_C_READ,NULL))) {
569 fprintf(stderr,"load_elf_image: elf_begin: %s\n",
570 elf_errmsg(elf_errno()));
571 return(-1);
572 }
573
574 if (!(ehdr = elf32_getehdr(img_elf))) {
575 fprintf(stderr,"load_elf_image: invalid ELF file\n");
576 return(-1);
577 }
578
579 printf("Loading ELF file '%s'...\n",filename);
580 bfd = fdopen(fd,"rb");
581
582 if (!bfd) {
583 perror("load_elf_image: fdopen");
584 return(-1);
585 }
586
587 if (!skip_load) {
588 for(i=0;i<ehdr->e_shnum;i++) {
589 scn = elf_getscn(img_elf,i);
590
591 shdr = elf32_getshdr(scn);
592 name = elf_strptr(img_elf, ehdr->e_shstrndx, (size_t)shdr->sh_name);
593 len = shdr->sh_size;
594
595 if (!(shdr->sh_flags & SHF_ALLOC) || !len)
596 continue;
597
598 fseek(bfd,shdr->sh_offset,SEEK_SET);
599 vaddr = shdr->sh_addr;
600
601 if (cpu->vm->debug_level > 0) {
602 printf(" * Adding section at virtual address 0x%8.8x "
603 "(len=0x%8.8lx)\n",vaddr,(u_long)len);
604 }
605
606 while(len > 0)
607 {
608 haddr = cpu->mem_op_lookup(cpu,vaddr,PPC32_MTS_DCACHE);
609
610 if (!haddr) {
611 fprintf(stderr,"load_elf_image: invalid load address 0x%x\n",
612 vaddr);
613 return(-1);
614 }
615
616 if (len > PPC32_MIN_PAGE_SIZE)
617 clen = PPC32_MIN_PAGE_SIZE;
618 else
619 clen = len;
620
621 remain = PPC32_MIN_PAGE_SIZE;
622 remain -= (vaddr - (vaddr & PPC32_MIN_PAGE_MASK));
623
624 clen = m_min(clen,remain);
625
626 if (fread((u_char *)haddr,clen,1,bfd) < 1)
627 break;
628
629 vaddr += clen;
630 len -= clen;
631 }
632 }
633 } else {
634 printf("ELF loading skipped, using a ghost RAM file.\n");
635 }
636
637 printf("ELF entry point: 0x%x\n",ehdr->e_entry);
638
639 if (entry_point)
640 *entry_point = ehdr->e_entry;
641
642 elf_end(img_elf);
643 fclose(bfd);
644 return(0);
645 }