eio.c
来自「一个很有名的硬件模拟器。可以模拟CPU」· C语言 代码 · 共 841 行 · 第 1/2 页
C
841 行
|| !exo->as_list.head->next || exo->as_list.head->next->ec != ec_integer || exo->as_list.head->next->next != NULL) fatal("count not read EIO stack segment specifiers"); ld_stack_base = (md_addr_t)exo->as_list.head->as_address.val; ld_stack_size = (unsigned int)exo->as_list.head->next->as_integer.val; exo_delete(exo); for (i=0; i < page_count; i++) { int j; md_addr_t page_addr; struct exo_term_t *blob; /* read the page */ exo = exo_read(fd); if (!exo || exo->ec != ec_list || !exo->as_list.head || exo->as_list.head->ec != ec_address || !exo->as_list.head->next || exo->as_list.head->next->ec != ec_blob || exo->as_list.head->next->next != NULL) fatal("could not read EIO memory page"); page_addr = (md_addr_t)exo->as_list.head->as_address.val; blob = exo->as_list.head->next; /* write data to simulator memory */ for (j=0; j < blob->as_blob.size; j++) { byte_t val; val = blob->as_blob.data[j]; /* unchecked access... */ MEM_WRITE_BYTE(mem, page_addr, val); page_addr++; } exo_delete(exo); } return trans_icnt;}struct mem_rec_t { md_addr_t addr; unsigned size, maxsize; struct exo_term_t *exo; struct exo_term_t *blob;};/* reg recs */static struct exo_term_t *input_regs;static struct exo_term_t *output_regs;/* input memory recs */static struct exo_term_t *input_mem;static struct mem_rec_t input_mem_rec;/* output memory recs */static struct exo_term_t *output_mem;static struct mem_rec_t output_mem_rec;static int seen_write;static mem_access_fn local_mem_fn;/* size of padding that can be filled on the end of a blob tail */#define BLOB_TAIL_SIZE 256/* tracing memory access function */static enum md_fault_typemy_mem_fn(struct mem_t *mem, /* memory space to access */ enum mem_cmd cmd, /* Read (from sim mem) or Write */ md_addr_t addr, /* target address to access */ void *vp, /* host memory address to access */ int nbytes) /* number of bytes to access */{ int i; unsigned char *p = vp; struct mem_rec_t *mem_rec = NULL; struct exo_term_t *mem_list = NULL; enum md_fault_type fault = md_fault_none; if (cmd == Read && seen_write) fatal("Read after Write in eio_syscall()"); if (cmd == Write) seen_write = TRUE; /* record the memory access */ if (cmd == Read) { mem_rec = &input_mem_rec; mem_list = input_mem; } else if (cmd == Write) { mem_rec = &output_mem_rec; mem_list = output_mem; } else panic("bogus memory access command"); /* perform the memory access, Read's first so we can probe *p for data */ if (cmd == Read /* simulator memory */) fault = (*local_mem_fn)(mem, cmd, addr, p, nbytes); /* the following freakish code simply coalesces subsequent reads and writes to memory into the same EXO blob structure, this greatly reduces the size of the EIO output files... */ if (mem_rec->exo != NULL && (mem_rec->addr + mem_rec->size == addr) && (mem_rec->size + nbytes < mem_rec->maxsize)) { /* add to last blob */ for (i=0; i < nbytes; i++) mem_rec->blob->as_blob.data[mem_rec->size + i] = p[i]; mem_rec->size += nbytes; mem_rec->blob->as_blob.size = mem_rec->size; } else { /* add to a new blob */ mem_list->as_list.head = exo_chain(mem_list->as_list.head, (mem_rec->exo = exo_new(ec_list, exo_new(ec_address, (exo_integer_t)addr), (mem_rec->blob = exo_new(ec_blob, nbytes + BLOB_TAIL_SIZE, NULL)), NULL))); for (i=0; i < nbytes; i++) mem_rec->blob->as_blob.data[i] = p[i]; mem_rec->addr = addr; mem_rec->size = nbytes; mem_rec->maxsize = nbytes + BLOB_TAIL_SIZE; mem_rec->blob->as_blob.size = mem_rec->size; } /* perform the memory access */ if (cmd == Write /* simulator memory */) fault = (*local_mem_fn)(mem, cmd, addr, p, nbytes); return fault;}/* syscall proxy handler, with EIO tracing support, architect registers and memory are assumed to be precise when this function is called, register and memory are updated with the results of the sustem call */voideio_write_trace(FILE *eio_fd, /* EIO stream file desc */ counter_t icnt, /* instruction count */ struct regs_t *regs, /* registers to update */ mem_access_fn mem_fn, /* generic memory accessor */ struct mem_t *mem, /* memory to update */ md_inst_t inst) /* system call inst */{ int i; struct exo_term_t *exo; /* write syscall register inputs ($r2..$r7) */ input_regs = exo_new(ec_list, NULL); for (i=MD_FIRST_IN_REG; i <= MD_LAST_IN_REG; i++) { input_regs->as_list.head = exo_chain(input_regs->as_list.head, MD_IREG_TO_EXO(regs, i)); } /* initialize memory inputs */ input_mem = exo_new(ec_list, NULL); input_mem_rec.exo = NULL; output_mem = exo_new(ec_list, NULL); output_mem_rec.exo = NULL; /* perform the system call, record inputs and outputs */ seen_write = FALSE; local_mem_fn = mem_fn; if (sim_eio_fd != NULL) eio_read_trace(sim_eio_fd, icnt, regs, my_mem_fn, mem, inst); else { sys_syscall(regs, my_mem_fn, mem, inst, FALSE); } /* write syscall breakpoint and register outputs ($r2..$r7) */ output_regs = exo_new(ec_list, NULL); output_regs->as_list.head = exo_chain(output_regs->as_list.head, exo_new(ec_address, (exo_integer_t)ld_brk_point)); for (i=MD_FIRST_OUT_REG; i <= MD_LAST_OUT_REG; i++) { output_regs->as_list.head = exo_chain(output_regs->as_list.head, MD_IREG_TO_EXO(regs, i)); } /* write the whole enchalada to output stream */ exo = exo_new(ec_list, /* icnt */exo_new(ec_integer, (exo_integer_t)icnt), /* PC */exo_new(ec_address, (exo_integer_t)regs->regs_PC), input_regs, input_mem, output_regs, output_mem, NULL); exo_print(exo, eio_fd); fprintf(eio_fd, "\n\n"); /* release input storage */ exo_delete(exo); /* one more transaction processed */ eio_trans_icnt = icnt;}/* syscall proxy handler from an EIO trace, architect registers and memory are assumed to be precise when this function is called, register and memory are updated with the results of the sustem call */voideio_read_trace(FILE *eio_fd, /* EIO stream file desc */ counter_t icnt, /* instruction count */ struct regs_t *regs, /* registers to update */ mem_access_fn mem_fn, /* generic memory accessor */ struct mem_t *mem, /* memory to update */ md_inst_t inst) /* system call inst */{ int i; struct exo_term_t *exo, *exo_icnt, *exo_pc; struct exo_term_t *exo_inregs, *exo_inmem, *exo_outregs, *exo_outmem; struct exo_term_t *brkrec, *regrec, *memrec; /* exit() system calls get executed for real... */ if (MD_EXIT_SYSCALL(regs)) { sys_syscall(regs, mem_fn, mem, inst, FALSE); panic("returned from exit() system call"); } /* else, read the external I/O (EIO) transaction */ exo = exo_read(eio_fd); /* one more transaction processed */ eio_trans_icnt = icnt; /* pull apart the EIO transaction (EXO format) */ if (!exo || exo->ec != ec_list || !(exo_icnt = exo->as_list.head) || exo_icnt->ec != ec_integer || !(exo_pc = exo_icnt->next) || exo_pc->ec != ec_address || !(exo_inregs = exo_pc->next) || exo_inregs->ec != ec_list || !(exo_inmem = exo_inregs->next) || exo_inmem->ec != ec_list || !(exo_outregs = exo_inmem->next) || exo_outregs->ec != ec_list || !(exo_outmem = exo_outregs->next) || exo_outmem->ec != ec_list || exo_outmem->next != NULL) fatal("cannot read EIO transaction"); /* * check the system call inputs */ /* check ICNT input */ if (icnt != (counter_t)exo_icnt->as_integer.val) fatal("EIO trace inconsistency: ICNT mismatch"); /* check PC input */ if (regs->regs_PC != (md_addr_t)exo_pc->as_integer.val) fatal("EIO trace inconsistency: PC mismatch"); /* check integer register inputs */ for (i=MD_FIRST_IN_REG, regrec=exo_inregs->as_list.head; i <= MD_LAST_IN_REG; i++, regrec=regrec->next) { if (!regrec || regrec->ec != ec_address) fatal("EIO trace inconsistency: missing input reg"); if (MD_EXO_CMP_IREG(regrec, regs, i)) fatal("EIO trace inconsistency: R[%d] input mismatch", i);#ifdef VERBOSE fprintf(stderr, "** R[%d] checks out...\n", i);#endif /* VERBOSE */ } if (regrec != NULL) fatal("EIO trace inconsistency: too many input regs"); /* check memory inputs */ for (memrec=exo_inmem->as_list.head; memrec != NULL; memrec=memrec->next) { md_addr_t loc; struct exo_term_t *addr, *blob; /* check the mem transaction format */ if (!memrec || memrec->ec != ec_list || !(addr = memrec->as_list.head) || addr->ec != ec_address || !(blob = addr->next) || blob->ec != ec_blob || blob->next != NULL) fatal("EIO trace inconsistency: bad memory transaction"); for (loc=addr->as_integer.val, i=0; i < blob->as_blob.size; loc++,i++) { unsigned char val; /* was: val = MEM_READ_BYTE(loc); */ (*mem_fn)(mem, Read, loc, &val, sizeof(unsigned char)); if (val != blob->as_blob.data[i]) fatal("EIO trace inconsistency: addr 0x%08p input mismatch", loc);#ifdef VERBOSE myfprintf(stderr, "** 0x%08p checks out...\n", loc);#endif /* VERBOSE */ } /* simulate view'able I/O */ if (MD_OUTPUT_SYSCALL(regs)) { if (sim_progfd) { /* redirect program output to file */ fwrite(blob->as_blob.data, 1, blob->as_blob.size, sim_progfd); } else { /* write the output to stdout/stderr */ write(MD_STREAM_FILENO(regs), blob->as_blob.data, blob->as_blob.size); } } } /* * write system call outputs */ /* adjust breakpoint */ brkrec = exo_outregs->as_list.head; if (!brkrec || brkrec->ec != ec_address) fatal("EIO trace inconsistency: missing memory breakpoint"); ld_brk_point = (md_addr_t)brkrec->as_integer.val; /* write integer register outputs */ for (i=MD_FIRST_OUT_REG, regrec=exo_outregs->as_list.head->next; i <= MD_LAST_OUT_REG; i++, regrec=regrec->next) { if (!regrec || regrec->ec != ec_address) fatal("EIO trace inconsistency: missing output reg"); MD_EXO_TO_IREG(regrec, regs, i);#ifdef VERBOSE fprintf(stderr, "** R[%d] written...\n", i);#endif /* VERBOSE */ } if (regrec != NULL) fatal("EIO trace inconsistency: too many output regs"); /* write memory outputs */ for (memrec=exo_outmem->as_list.head; memrec != NULL; memrec=memrec->next) { md_addr_t loc; struct exo_term_t *addr, *blob; /* check the mem transaction format */ if (!memrec || memrec->ec != ec_list || !(addr = memrec->as_list.head) || addr->ec != ec_address || !(blob = addr->next) || blob->ec != ec_blob || blob->next != NULL) fatal("EIO trace icnonsistency: bad memory transaction"); for (loc=addr->as_integer.val, i=0; i < blob->as_blob.size; loc++,i++) { /* was: MEM_WRITE_BYTE(loc, blob->as_blob.data[i]); */ (*mem_fn)(mem, Write, loc, &blob->as_blob.data[i], sizeof(unsigned char));#ifdef VERBOSE fprintf(stderr, "** 0x%08p written...\n", loc);#endif /* VERBOSE */ } } /* release the EIO EXO node */ exo_delete(exo);}/* fast forward EIO trace EIO_FD to the transaction just after ICNT */voideio_fast_forward(FILE *eio_fd, counter_t icnt){ struct exo_term_t *exo, *exo_icnt; do { /* read the next external I/O (EIO) transaction */ exo = exo_read(eio_fd); if (!exo) fatal("could not fast forward to EIO checkpoint"); /* one more transaction processed */ eio_trans_icnt = icnt; /* pull apart the EIO transaction (EXO format) */ if (!exo || exo->ec != ec_list || !(exo_icnt = exo->as_list.head) || exo_icnt->ec != ec_integer) fatal("cannot read EIO transaction (during fast forward)"); } while ((counter_t)exo_icnt->as_integer.val != icnt); /* found it! */}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?