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