eio.c

来自「一个很有名的硬件模拟器。可以模拟CPU」· C语言 代码 · 共 841 行 · 第 1/2 页

C
841
字号
/* eio.c - external interfaces to external I/O f\iles *//* SimpleScalar(TM) Tool Suite * Copyright (C) 1994-2003 by Todd M. Austin, Ph.D. and SimpleScalar, LLC. * All Rights Reserved.  *  * THIS IS A LEGAL DOCUMENT, BY USING SIMPLESCALAR, * YOU ARE AGREEING TO THESE TERMS AND CONDITIONS. *  * No portion of this work may be used by any commercial entity, or for any * commercial purpose, without the prior, written permission of SimpleScalar, * LLC (info@simplescalar.com). Nonprofit and noncommercial use is permitted * as described below. *  * 1. SimpleScalar is provided AS IS, with no warranty of any kind, express * or implied. The user of the program accepts full responsibility for the * application of the program and the use of any results. *  * 2. Nonprofit and noncommercial use is encouraged. SimpleScalar may be * downloaded, compiled, executed, copied, and modified solely for nonprofit, * educational, noncommercial research, and noncommercial scholarship * purposes provided that this notice in its entirety accompanies all copies. * Copies of the modified software can be delivered to persons who use it * solely for nonprofit, educational, noncommercial research, and * noncommercial scholarship purposes provided that this notice in its * entirety accompanies all copies. *  * 3. ALL COMMERCIAL USE, AND ALL USE BY FOR PROFIT ENTITIES, IS EXPRESSLY * PROHIBITED WITHOUT A LICENSE FROM SIMPLESCALAR, LLC (info@simplescalar.com). *  * 4. No nonprofit user may place any restrictions on the use of this software, * including as modified by the user, by any other authorized user. *  * 5. Noncommercial and nonprofit users may distribute copies of SimpleScalar * in compiled or executable form as set forth in Section 2, provided that * either: (A) it is accompanied by the corresponding machine-readable source * code, or (B) it is accompanied by a written offer, with no time limit, to * give anyone a machine-readable copy of the corresponding source code in * return for reimbursement of the cost of distribution. This written offer * must permit verbatim duplication by anyone, or (C) it is distributed by * someone who received only the executable form, and is accompanied by a * copy of the written offer of source code. *  * 6. SimpleScalar was developed by Todd M. Austin, Ph.D. The tool suite is * currently maintained by SimpleScalar LLC (info@simplescalar.com). US Mail: * 2395 Timbercrest Court, Ann Arbor, MI 48105. *  * Copyright (C) 1994-2003 by Todd M. Austin, Ph.D. and SimpleScalar, LLC. */#include <stdio.h>#include <stdlib.h>#ifdef _MSC_VER#include <io.h>#else /* !_MSC_VER */#include <unistd.h>#endif#include "host.h"#include "misc.h"#include "machine.h"#include "regs.h"#include "memory.h"#include "loader.h"#include "libexo/libexo.h"#include "syscall.h"#include "sim.h"#include "endian.h"#include "eio.h"#ifdef _MSC_VER#define write		_write#endif#define EIO_FILE_HEADER							\  "/* This is a SimpleScalar EIO file - DO NOT MOVE OR EDIT THIS LINE! */\n"/*   EIO transaction format:   (inst_count, pc,    ... reg inputs ...    [r2, r3, r4, r5, r6, r7],    ... mem inputs ...    ((addr, size, blob), ...)    ... reg outputs ...    [r2, r3, r4, r5, r6, r7],    ... mem outputs ...    ((addr, size, blob), ...)   )*//* EIO transaction count, i.e., number of last transaction completed */static counter_t eio_trans_icnt = -1;FILE *eio_create(char *fname){  FILE *fd;  struct exo_term_t *exo;  int target_big_endian;  target_big_endian = (endian_host_byte_order() == endian_big);  fd = gzopen(fname, "w");  if (!fd)    fatal("unable to create EIO file `%s'", fname);  /* emit EIO file header */  fprintf(fd, "%s\n", EIO_FILE_HEADER);  fprintf(fd, "/* file_format: %d, file_version: %d, big_endian: %d */\n", 	  MD_EIO_FILE_FORMAT, EIO_FILE_VERSION, ld_target_big_endian);  exo = exo_new(ec_list,		exo_new(ec_integer, (exo_integer_t)MD_EIO_FILE_FORMAT),		exo_new(ec_integer, (exo_integer_t)EIO_FILE_VERSION),		exo_new(ec_integer, (exo_integer_t)target_big_endian),		NULL);  exo_print(exo, fd);  exo_delete(exo);  fprintf(fd, "\n\n");  return fd;}FILE *eio_open(char *fname){  FILE *fd;  struct exo_term_t *exo;  int file_format, file_version, big_endian, target_big_endian;  target_big_endian = (endian_host_byte_order() == endian_big);  fd = gzopen(fname, "r");  if (!fd)    fatal("unable to open EIO file `%s'", fname);  /* read and check EIO file header */  exo = exo_read(fd);  if (!exo      || exo->ec != ec_list      || !exo->as_list.head      || exo->as_list.head->ec != ec_integer      || !exo->as_list.head->next      || exo->as_list.head->next->ec != ec_integer      || !exo->as_list.head->next->next      || exo->as_list.head->next->next->ec != ec_integer      || exo->as_list.head->next->next->next != NULL)    fatal("could not read EIO file header");  file_format = exo->as_list.head->as_integer.val;  file_version = exo->as_list.head->next->as_integer.val;  big_endian = exo->as_list.head->next->next->as_integer.val;  exo_delete(exo);  if (file_format != MD_EIO_FILE_FORMAT)    fatal("EIO file `%s' has incompatible format", fname);  if (file_version != EIO_FILE_VERSION)    fatal("EIO file `%s' has incompatible version", fname);  if (!!big_endian != !!target_big_endian)    {      warn("endian of `%s' does not match host", fname);      warn("running with experimental cross-endian execution support");      warn("****************************************");      warn("**>> please check results carefully <<**");      warn("****************************************");    }  return fd;}/* returns non-zero if file FNAME has a valid EIO header */inteio_valid(char *fname){  FILE *fd;  char buf[512];  /* open possible EIO file */  fd = gzopen(fname, "r");  if (!fd)    return FALSE;  /* read and check EIO file header */  fgets(buf, 512, fd);  /* check the header */  if (strcmp(buf, EIO_FILE_HEADER))    return FALSE;  /* all done, close up file */  gzclose(fd);  /* else, has a valid header, go with it... */  return TRUE;}voideio_close(FILE *fd){  gzclose(fd);}/* check point current architected state to stream FD, returns   EIO transaction count (an EIO file pointer) */counter_teio_write_chkpt(struct regs_t *regs,		/* regs to dump */		struct mem_t *mem,		/* memory to dump */		FILE *fd)			/* stream to write to */{  int i;  struct exo_term_t *exo;  struct mem_pte_t *pte;  myfprintf(fd, "/* ** start checkpoint @ %n... */\n\n", eio_trans_icnt);  myfprintf(fd, "/* EIO file pointer: %n... */\n", eio_trans_icnt);  exo = exo_new(ec_integer, (exo_integer_t)eio_trans_icnt);  exo_print(exo, fd);  fprintf(fd, "\n\n");  exo_delete(exo);  /* dump misc regs: icnt, PC, NPC, etc... */  fprintf(fd, "/* misc regs icnt, PC, NPC, etc... */\n");  exo = MD_MISC_REGS_TO_EXO(regs);  exo_print(exo, fd);  fprintf(fd, "\n\n");  exo_delete(exo);  /* dump integer registers */  fprintf(fd, "/* integer regs */\n");  exo = exo_new(ec_list, NULL);  for (i=0; i < MD_NUM_IREGS; i++)    exo->as_list.head = exo_chain(exo->as_list.head, MD_IREG_TO_EXO(regs, i));  exo_print(exo, fd);  fprintf(fd, "\n\n");  exo_delete(exo);  /* dump FP registers */  fprintf(fd, "/* FP regs (integer format) */\n");  exo = exo_new(ec_list, NULL);  for (i=0; i < MD_NUM_FREGS; i++)    exo->as_list.head = exo_chain(exo->as_list.head, MD_FREG_TO_EXO(regs, i));  exo_print(exo, fd);  fprintf(fd, "\n\n");  exo_delete(exo);  fprintf(fd, "/* writing `%d' memory pages... */\n", (int)mem->page_count);  exo = exo_new(ec_list,		exo_new(ec_integer, (exo_integer_t)mem->page_count),		exo_new(ec_address, (exo_integer_t)ld_brk_point),		exo_new(ec_address, (exo_integer_t)ld_stack_min),		NULL);  exo_print(exo, fd);  fprintf(fd, "\n\n");  exo_delete(exo);  fprintf(fd, "/* text segment specifiers (base & size) */\n");  exo = exo_new(ec_list,		exo_new(ec_address, (exo_integer_t)ld_text_base),		exo_new(ec_integer, (exo_integer_t)ld_text_size),		NULL);  exo_print(exo, fd);  fprintf(fd, "\n\n");  exo_delete(exo);  fprintf(fd, "/* data segment specifiers (base & size) */\n");  exo = exo_new(ec_list,		exo_new(ec_address, (exo_integer_t)ld_data_base),		exo_new(ec_integer, (exo_integer_t)ld_data_size),		NULL);  exo_print(exo, fd);  fprintf(fd, "\n\n");  exo_delete(exo);  fprintf(fd, "/* stack segment specifiers (base & size) */\n");  exo = exo_new(ec_list,		exo_new(ec_address, (exo_integer_t)ld_stack_base),		exo_new(ec_integer, (exo_integer_t)ld_stack_size),		NULL);  exo_print(exo, fd);  fprintf(fd, "\n\n");  exo_delete(exo);  /* visit all active memory pages, and dump them to the checkpoint file */  MEM_FORALL(mem, i, pte)    {      /* dump this page... */      exo = exo_new(ec_list,		    exo_new(ec_address, (exo_integer_t)MEM_PTE_ADDR(pte, i)),		    exo_new(ec_blob, MD_PAGE_SIZE, pte->page),		    NULL);      exo_print(exo, fd);      fprintf(fd, "\n\n");      exo_delete(exo);    }  myfprintf(fd, "/* ** end checkpoint @ %n... */\n\n", eio_trans_icnt);  return eio_trans_icnt;}/* read check point of architected state from stream FD, returns   EIO transaction count (an EIO file pointer) */counter_teio_read_chkpt(struct regs_t *regs,		/* regs to dump */		struct mem_t *mem,		/* memory to dump */		FILE *fd)			/* stream to read */{  int i, page_count;  counter_t trans_icnt;  struct exo_term_t *exo, *elt;  /* read the EIO file pointer */  exo = exo_read(fd);  if (!exo      || exo->ec != ec_integer)    fatal("could not read EIO file pointer");  trans_icnt = exo->as_integer.val;  exo_delete(exo);  /* read misc regs: icnt, PC, NPC, HI, LO, FCC */  exo = exo_read(fd);  MD_EXO_TO_MISC_REGS(exo, sim_num_insn, regs);  exo_delete(exo);  /* read integer registers */  exo = exo_read(fd);  if (!exo      || exo->ec != ec_list)    fatal("could not read EIO integer regs");  elt = exo->as_list.head;  for (i=0; i < MD_NUM_IREGS; i++)    {      if (!elt)	fatal("could not read EIO integer regs (too few)");      if (elt->ec != ec_address)	fatal("could not read EIO integer regs (bad value)");      MD_EXO_TO_IREG(elt, regs, i);      elt = elt->next;    }  if (elt != NULL)    fatal("could not read EIO integer regs (too many)");  exo_delete(exo);  /* read FP registers */  exo = exo_read(fd);  if (!exo      || exo->ec != ec_list)    fatal("could not read EIO FP regs");  elt = exo->as_list.head;  for (i=0; i < MD_NUM_FREGS; i++)    {      if (!elt)	fatal("could not read EIO FP regs (too few)");      if (elt->ec != ec_address)	fatal("could not read EIO FP regs (bad value)");      MD_EXO_TO_FREG(elt, regs, i);      elt = elt->next;    }  if (elt != NULL)    fatal("could not read EIO FP regs (too many)");  exo_delete(exo);  /* read the number of page defs, and memory config */  exo = exo_read(fd);  if (!exo      || exo->ec != ec_list      || !exo->as_list.head      || exo->as_list.head->ec != ec_integer      || !exo->as_list.head->next      || exo->as_list.head->next->ec != ec_address      || !exo->as_list.head->next->next      || exo->as_list.head->next->next->ec != ec_address      || exo->as_list.head->next->next->next != NULL)    fatal("could not read EIO memory page count");  page_count = exo->as_list.head->as_integer.val;  ld_brk_point = (md_addr_t)exo->as_list.head->next->as_address.val;  ld_stack_min = (md_addr_t)exo->as_list.head->next->next->as_address.val;  exo_delete(exo);  /* read text segment specifiers */  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_integer      || exo->as_list.head->next->next != NULL)    fatal("count not read EIO text segment specifiers");  ld_text_base = (md_addr_t)exo->as_list.head->as_address.val;  ld_text_size = (unsigned int)exo->as_list.head->next->as_integer.val;  exo_delete(exo);  /* read data segment specifiers */  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_integer      || exo->as_list.head->next->next != NULL)    fatal("count not read EIO data segment specifiers");  ld_data_base = (md_addr_t)exo->as_list.head->as_address.val;  ld_data_size = (unsigned int)exo->as_list.head->next->as_integer.val;  exo_delete(exo);  /* read stack segment specifiers */  exo = exo_read(fd);  if (!exo      || exo->ec != ec_list      || !exo->as_list.head      || exo->as_list.head->ec != ec_address

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?