⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 syscall.c

📁 一个很有名的硬件模拟器。可以模拟CPU
💻 C
📖 第 1 页 / 共 5 页
字号:
	    regs->regs_R[i] = sc.sc_regs[i];	  for (i=0; i < 32; ++i)	    regs->regs_F.q[i] = sc.sc_fpregs[i];	  regs->regs_C.fpcr = sc.sc_fpcr;	}      /* fini... */      return;    }  /* no, OK execute the live system call... */  switch (syscode)    {    case OSF_SYS_exit:      /* exit jumps to the target set in main() */      longjmp(sim_exit_buf,	      /* exitcode + fudge */(regs->regs_R[MD_REG_A0] & 0xff) + 1);      break;    case OSF_SYS_read:      {	char *buf;	/* allocate same-sized input buffer in host memory */	if (!(buf =	      (char *)calloc(/*nbytes*/regs->regs_R[MD_REG_A2], sizeof(char))))	  fatal("out of memory in SYS_read");	/* read data from file */	do {	  /*nread*/regs->regs_R[MD_REG_V0] =	    read(/*fd*/regs->regs_R[MD_REG_A0], buf,	         /*nbytes*/regs->regs_R[MD_REG_A2]);	} while (/*nread*/regs->regs_R[MD_REG_V0] == -1	         && errno == EAGAIN);	/* check for error condition */	if (regs->regs_R[MD_REG_V0] != (qword_t)-1)	  regs->regs_R[MD_REG_A3] = 0;	else /* got an error, return details */	  {	    regs->regs_R[MD_REG_A3] = -1;	    regs->regs_R[MD_REG_V0] = errno;	  }	/* copy results back into host memory */	mem_bcopy(mem_fn, mem, Write,		  /*buf*/regs->regs_R[MD_REG_A1], buf, /*nread*/regs->regs_R[MD_REG_A2]);	/* done with input buffer */	free(buf);      }      break;    case OSF_SYS_write:      {	char *buf;	/* allocate same-sized output buffer in host memory */	if (!(buf =	      (char *)calloc(/*nbytes*/regs->regs_R[MD_REG_A2], sizeof(char))))	  fatal("out of memory in SYS_write");	/* copy inputs into host memory */	mem_bcopy(mem_fn, mem, Read, /*buf*/regs->regs_R[MD_REG_A1], buf,		  /*nbytes*/regs->regs_R[MD_REG_A2]);	/* write data to file */	if (sim_progfd && MD_OUTPUT_SYSCALL(regs))	  {	    /* redirect program output to file */	    /*nwritten*/regs->regs_R[MD_REG_V0] =	      fwrite(buf, 1, /*nbytes*/regs->regs_R[MD_REG_A2], sim_progfd);	  }	else	  {	    /* perform program output request */	    do {	      /*nwritten*/regs->regs_R[MD_REG_V0] =	        write(/*fd*/regs->regs_R[MD_REG_A0],		      buf, /*nbytes*/regs->regs_R[MD_REG_A2]);	    } while (/*nwritten*/regs->regs_R[MD_REG_V0] == -1		     && errno == EAGAIN);	  }	/* check for an error condition */	if (regs->regs_R[MD_REG_V0] == regs->regs_R[MD_REG_A2])	  regs->regs_R[MD_REG_A3] = 0;	else /* got an error, return details */	  {	    regs->regs_R[MD_REG_A3] = -1;	    regs->regs_R[MD_REG_V0] = errno;	  }	/* done with output buffer */	free(buf);      }      break;#if !defined(MIN_SYSCALL_MODE)      /* ADDED BY CALDER 10/27/99 */    case OSF_SYS_getdomainname:      /* get program scheduling priority */      {	char *buf;	buf = malloc(/* len */(size_t)regs->regs_R[MD_REG_A1]);	if (!buf)	  fatal("out of virtual memory in gethostname()");	/* result */regs->regs_R[MD_REG_V0] =	  getdomainname(/* name */buf,		      /* len */(size_t)regs->regs_R[MD_REG_A1]);	/* check for an error condition */	if (regs->regs_R[MD_REG_V0] != (qword_t)-1)	  regs->regs_R[MD_REG_A3] = 0;	else /* got an error, return details */	  {	    regs->regs_R[MD_REG_A3] = -1;	    regs->regs_R[MD_REG_V0] = errno;	  }	/* copy string back to simulated memory */	mem_bcopy(mem_fn, mem, Write,		  /* name */regs->regs_R[MD_REG_A0],		  buf, /* len */regs->regs_R[MD_REG_A1]);      }      break;#endif#if !defined(MIN_SYSCALL_MODE)      /* ADDED BY CALDER 10/27/99 */    case OSF_SYS_flock:      /* get flock() information on the file */      {	regs->regs_R[MD_REG_V0] =	  flock(/*fd*/(int)regs->regs_R[MD_REG_A0],		/*cmd*/(int)regs->regs_R[MD_REG_A1]);	/* check for an error condition */	if (regs->regs_R[MD_REG_V0] != (qword_t)-1)	  regs->regs_R[MD_REG_A3] = 0;	else /* got an error, return details */	  {	    regs->regs_R[MD_REG_A3] = -1;	    regs->regs_R[MD_REG_V0] = errno;	  }      }      break;#endif#if !defined(MIN_SYSCALL_MODE)      /* ADDED BY CALDER 10/27/99 */    case OSF_SYS_bind:      {	const struct sockaddr a_sock;	mem_bcopy(mem_fn, mem, Read, /* serv_addr */regs->regs_R[MD_REG_A1],		  (void *)&a_sock, /* addrlen */(int)regs->regs_R[MD_REG_A2]);      regs->regs_R[MD_REG_V0] =	bind((int) regs->regs_R[MD_REG_A0],	     &a_sock,(int) regs->regs_R[MD_REG_A2]);      /* check for an error condition */      if (regs->regs_R[MD_REG_V0] != (qword_t)-1)	regs->regs_R[MD_REG_A3] = 0;      else /* got an error, return details */	{	  regs->regs_R[MD_REG_A3] = -1;	  regs->regs_R[MD_REG_V0] = errno;	}      }      break;#endif#if !defined(MIN_SYSCALL_MODE)      /* ADDED BY CALDER 10/27/99 */    case OSF_SYS_sendto:      {	char *buf = NULL;	struct sockaddr d_sock;	int buf_len = 0;	buf_len = regs->regs_R[MD_REG_A2];	if (buf_len > 0)	  buf = (char *) malloc(buf_len*sizeof(char));	mem_bcopy(mem_fn, mem, Read, /* serv_addr */regs->regs_R[MD_REG_A1],		  buf, /* addrlen */(int)regs->regs_R[MD_REG_A2]);	if (regs->regs_R[MD_REG_A5] > 0) 	  mem_bcopy(mem_fn, mem, Read, regs->regs_R[MD_REG_A4],		    &d_sock, (int)regs->regs_R[MD_REG_A5]);	regs->regs_R[MD_REG_V0] =	  sendto((int) regs->regs_R[MD_REG_A0],		 buf,(int) regs->regs_R[MD_REG_A2],		 (int) regs->regs_R[MD_REG_A3],		 &d_sock,(int) regs->regs_R[MD_REG_A5]);	mem_bcopy(mem_fn, mem, Write, /* serv_addr */regs->regs_R[MD_REG_A1],		  buf, /* addrlen */(int)regs->regs_R[MD_REG_A2]);	/* maybe copy back whole size of sockaddr */	if (regs->regs_R[MD_REG_A5] > 0)	  mem_bcopy(mem_fn, mem, Write, regs->regs_R[MD_REG_A4],		    &d_sock, (int)regs->regs_R[MD_REG_A5]);	/* check for an error condition */	if (regs->regs_R[MD_REG_V0] != (qword_t)-1)	  regs->regs_R[MD_REG_A3] = 0;	else /* got an error, return details */	  {	    regs->regs_R[MD_REG_A3] = -1;	    regs->regs_R[MD_REG_V0] = errno;	  }	if (buf != NULL) 	  free(buf);      }      break;#endif#if !defined(MIN_SYSCALL_MODE)      /* ADDED BY CALDER 10/27/99 */    case OSF_SYS_old_recvfrom:    case OSF_SYS_recvfrom:      {	int addr_len;	char *buf;	struct sockaddr *a_sock;      	buf = (char *) malloc(sizeof(char)*regs->regs_R[MD_REG_A2]);	mem_bcopy(mem_fn, mem, Read, /* serv_addr */regs->regs_R[MD_REG_A1],		  buf, /* addrlen */(int)regs->regs_R[MD_REG_A2]);	mem_bcopy(mem_fn, mem, Read, /* serv_addr */regs->regs_R[MD_REG_A5],		  &addr_len, sizeof(int));	a_sock = (struct sockaddr *)malloc(addr_len);	mem_bcopy(mem_fn, mem, Read, regs->regs_R[MD_REG_A4],		  a_sock, addr_len);	regs->regs_R[MD_REG_V0] =	  recvfrom((int) regs->regs_R[MD_REG_A0],		   buf,(int) regs->regs_R[MD_REG_A2],		   (int) regs->regs_R[MD_REG_A3], a_sock,&addr_len);	mem_bcopy(mem_fn, mem, Write, regs->regs_R[MD_REG_A1],		  buf, (int) regs->regs_R[MD_REG_V0]);	mem_bcopy(mem_fn, mem, Write, /* serv_addr */regs->regs_R[MD_REG_A5],		  &addr_len, sizeof(int));	mem_bcopy(mem_fn, mem, Write, regs->regs_R[MD_REG_A4],		  a_sock, addr_len);	/* check for an error condition */	if (regs->regs_R[MD_REG_V0] != (qword_t)-1)	  regs->regs_R[MD_REG_A3] = 0;	else /* got an error, return details */	  {	    regs->regs_R[MD_REG_A3] = -1;	    regs->regs_R[MD_REG_V0] = errno;	  }	if (buf != NULL)	  free(buf);      }      break;#endif    case OSF_SYS_open:      {	char buf[MAXBUFSIZE];	unsigned int i;	int osf_flags = regs->regs_R[MD_REG_A1], local_flags = 0;	/* translate open(2) flags */	for (i=0; i < OSF_NFLAGS; i++)	  {	    if (osf_flags & osf_flag_table[i].osf_flag)	      {		osf_flags &= ~osf_flag_table[i].osf_flag;		local_flags |= osf_flag_table[i].local_flag;	      }	  }	/* any target flags left? */	if (osf_flags != 0)	  fatal("syscall: open: cannot decode flags: 0x%08x", osf_flags);	/* copy filename to host memory */	mem_strcpy(mem_fn, mem, Read, /*fname*/regs->regs_R[MD_REG_A0], buf);	/* open the file */	/*fd*/regs->regs_R[MD_REG_V0] =	  open(buf, local_flags, /*mode*/regs->regs_R[MD_REG_A2]);		/* check for an error condition */	if (regs->regs_R[MD_REG_V0] != (qword_t)-1)	  regs->regs_R[MD_REG_A3] = 0;	else /* got an error, return details */	  {	    regs->regs_R[MD_REG_A3] = -1;	    regs->regs_R[MD_REG_V0] = errno;	  }      }      break;    case OSF_SYS_close:      /* don't close stdin, stdout, or stderr as this messes up sim logs */      if (/*fd*/regs->regs_R[MD_REG_A0] == 0	  || /*fd*/regs->regs_R[MD_REG_A0] == 1	  || /*fd*/regs->regs_R[MD_REG_A0] == 2)	{	  regs->regs_R[MD_REG_A3] = 0;	  break;	}      /* close the file */      regs->regs_R[MD_REG_V0] = close(/*fd*/regs->regs_R[MD_REG_A0]);      /* check for an error condition */      if (regs->regs_R[MD_REG_V0] != (qword_t)-1)	regs->regs_R[MD_REG_A3] = 0;      else /* got an error, return details */	{	  regs->regs_R[MD_REG_A3] = -1;	  regs->regs_R[MD_REG_V0] = errno;	}      break;#if 0    case OSF_SYS_creat:      {	char buf[MAXBUFSIZE];	/* copy filename to host memory */	mem_strcpy(mem_fn, Read, /*fname*/regs->regs_R[MD_REG_A0], buf);	/* create the file */	/*fd*/regs->regs_R[MD_REG_V0] =	  creat(buf, /*mode*/regs->regs_R[MD_REG_A1]);	/* check for an error condition */	if (regs->regs_R[MD_REG_V0] != (qword_t)-1)	  regs->regs_R[MD_REG_A3] = 0;	else /* got an error, return details */	  {	    regs->regs_R[MD_REG_A3] = -1;	    regs->regs_R[MD_REG_V0] = errno;	  }      }      break;#endif    case OSF_SYS_unlink:      {	char buf[MAXBUFSIZE];	/* copy filename to host memory */	mem_strcpy(mem_fn, mem, Read, /*fname*/regs->regs_R[MD_REG_A0], buf);	/* delete the file */	/*result*/regs->regs_R[MD_REG_V0] = unlink(buf);	/* check for an error condition */	if (regs->regs_R[MD_REG_V0] != (qword_t)-1)	  regs->regs_R[MD_REG_A3] = 0;	else /* got an error, return details */	  {	    regs->regs_R[MD_REG_A3] = -1;	    regs->regs_R[MD_REG_V0] = errno;	  }      }      break;    case OSF_SYS_chdir:      {	char buf[MAXBUFSIZE];	/* copy filename to host memory */	mem_strcpy(mem_fn, mem, Read, /*fname*/regs->regs_R[MD_REG_A0], buf);	/* change the working directory */	/*result*/regs->regs_R[MD_REG_V0] = chdir(buf);	/* check for an error condition */	if (regs->regs_R[MD_REG_V0] != (qword_t)-1)	  regs->regs_R[MD_REG_A3] = 0;	else /* got an error, return details */	  {	    regs->regs_R[MD_REG_A3] = -1;	    regs->regs_R[MD_REG_V0] = errno;	  }      }      break;    case OSF_SYS_chmod:      {	char buf[MAXBUFSIZE];	/* copy filename to host memory */	mem_strcpy(mem_fn, mem, Read, /*fname*/regs->regs_R[MD_REG_A0], buf);	/* chmod the file */	/*result*/regs->regs_R[MD_REG_V0] =	  chmod(buf, /*mode*/regs->regs_R[MD_REG_A1]);	/* check for an error condition */	if (regs->regs_R[MD_REG_V0] != (qword_t)-1)	  regs->regs_R[MD_REG_A3] = 0;	else /* got an error, return details */	  {	    regs->regs_R[MD_REG_A3] = -1;	    regs->regs_R[MD_REG_V0] = errno;	  }      }      break;    case OSF_SYS_chown:#ifdef _MSC_VER      warn("syscall chown() not yet implemented for MSC...");      regs->regs_R[MD_REG_A3] = 0;#else /* !_MSC_VER */      {	char buf[MAXBUFSIZE];	/* copy filename to host memory */	mem_strcpy(mem_fn, mem,Read, /*fname*/regs->regs_R[MD_REG_A0], buf);	/* chown the file */	/*result*/regs->regs_R[MD_REG_V0] =	  chown(buf, /*owner*/regs->regs_R[MD_REG_A1],		/*group*/regs->regs_R[MD_REG_A2]);	/* check for an error condition */	if (regs->regs_R[MD_REG_V0] != (qword_t)-1)	  regs->regs_R[MD_REG_A3] = 0;	else /* got an error, return details */	  {	    regs->regs_R[MD_REG_A3] = -1;	    regs->regs_R[MD_REG_V0] = errno;	  }      }#endif /* _MSC_VER */      break;    case OSF_SYS_sbrk:      {	sqword_t delta;	md_addr_t addr;	delta = regs->regs_R[MD_REG_A0];	addr = ld_brk_point + delta;	if (verbose)	  myfprintf(stderr, "SYS_sbrk: delta: 0x%012p (%ld)\n", delta, delta);	ld_brk_point = addr;	regs->regs_R[MD_REG_V0] = ld_brk_point;	regs->regs_R[MD_REG_A3] = 0;	if (verbose)

⌨️ 快捷键说明

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