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

📄 syscall.c

📁 一个很有名的硬件模拟器。可以模拟CPU
💻 C
📖 第 1 页 / 共 5 页
字号:
	  myfprintf(stderr, "ld_brk_point: 0x%012p\n", ld_brk_point);#if 0	/* check whether heap area has merged with stack area */	if (/* addr >= ld_brk_point && */ addr < regs->regs_R[MD_REG_SP])	  {	    regs->regs_R[MD_REG_A3] = 0;	    ld_brk_point = addr;	  }	else	  {	    /* out of address space, indicate error */	    regs->regs_R[MD_REG_A3] = -1;	  }#endif      }      break;    case OSF_SYS_obreak:      {        md_addr_t addr;        /* round the new heap pointer to the its page boundary */#if 0        addr = ROUND_UP(/*base*/regs->regs_R[MD_REG_A0], MD_PAGE_SIZE);#endif        addr = /*base*/regs->regs_R[MD_REG_A0];	if (verbose)	  myfprintf(stderr, "SYS_obreak: addr: 0x%012p\n", addr);	ld_brk_point = addr;	regs->regs_R[MD_REG_V0] = ld_brk_point;	regs->regs_R[MD_REG_A3] = 0;	if (verbose)	  myfprintf(stderr, "ld_brk_point: 0x%012p\n", ld_brk_point);      }      break;    case OSF_SYS_lseek:      /* seek into file */      regs->regs_R[MD_REG_V0] =	lseek(/*fd*/regs->regs_R[MD_REG_A0],	      /*off*/regs->regs_R[MD_REG_A1], /*dir*/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_getpid:      /* get the simulator process id */      /*result*/regs->regs_R[MD_REG_V0] = getpid();      /* 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_getuid:#ifdef _MSC_VER      warn("syscall getuid() not yet implemented for MSC...");      regs->regs_R[MD_REG_A3] = 0;#else /* !_MSC_VER */      /* get current user id */      /*first result*/regs->regs_R[MD_REG_V0] = getuid();      /*second result*/regs->regs_R[MD_REG_A4] = geteuid();      /* 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_access:      {	char buf[MAXBUFSIZE];	/* copy filename to host memory */	mem_strcpy(mem_fn, mem, Read, /*fName*/regs->regs_R[MD_REG_A0], buf);	/* check access on the file */	/*result*/regs->regs_R[MD_REG_V0] =	  access(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_stat:    case OSF_SYS_lstat:      {	char buf[MAXBUFSIZE];	struct osf_statbuf osf_sbuf;#ifdef _MSC_VER	struct _stat sbuf;#else /* !_MSC_VER */	struct stat sbuf;#endif /* _MSC_VER */	/* copy filename to host memory */	mem_strcpy(mem_fn, mem, Read, /*fName*/regs->regs_R[MD_REG_A0], buf);	/* stat() the file */	if (syscode == OSF_SYS_stat)	  /*result*/regs->regs_R[MD_REG_V0] = stat(buf, &sbuf);	else /* syscode == OSF_SYS_lstat */	  {#ifdef _MSC_VER            warn("syscall lstat() not yet implemented for MSC...");            regs->regs_R[MD_REG_A3] = 0;            break;#else /* !_MSC_VER */	    /*result*/regs->regs_R[MD_REG_V0] = lstat(buf, &sbuf);#endif /* _MSC_VER */	  }	/* 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;	  }	/* translate from host stat structure to target format */	osf_sbuf.osf_st_dev = MD_SWAPW(sbuf.st_dev);	osf_sbuf.osf_st_ino = MD_SWAPW(sbuf.st_ino);	osf_sbuf.osf_st_mode = MD_SWAPW(sbuf.st_mode);	osf_sbuf.osf_st_nlink = MD_SWAPH(sbuf.st_nlink);	osf_sbuf.osf_st_uid = MD_SWAPW(sbuf.st_uid);	osf_sbuf.osf_st_gid = MD_SWAPW(sbuf.st_gid);	osf_sbuf.osf_st_rdev = MD_SWAPW(sbuf.st_rdev);	osf_sbuf.osf_st_size = MD_SWAPQ(sbuf.st_size);	osf_sbuf.osf_st_atime = MD_SWAPW(sbuf.st_atime);	osf_sbuf.osf_st_mtime = MD_SWAPW(sbuf.st_mtime);	osf_sbuf.osf_st_ctime = MD_SWAPW(sbuf.st_ctime);#ifndef _MSC_VER	osf_sbuf.osf_st_blksize = MD_SWAPW(sbuf.st_blksize);	osf_sbuf.osf_st_blocks = MD_SWAPW(sbuf.st_blocks);#endif /* !_MSC_VER */	/* copy stat() results to simulator memory */	mem_bcopy(mem_fn, mem, Write, /*sbuf*/regs->regs_R[MD_REG_A1],		  &osf_sbuf, sizeof(struct osf_statbuf));      }      break;    case OSF_SYS_dup:      /* dup() the file descriptor */      /*fd*/regs->regs_R[MD_REG_V0] = dup(/*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_pipe:      {	int fd[2];	/* copy pipe descriptors to host memory */;	mem_bcopy(mem_fn, mem, Read, /*fd's*/regs->regs_R[MD_REG_A0],		  fd, sizeof(fd));	/* create a pipe */	/*result*/regs->regs_R[7] = pipe(fd);	/* copy descriptor results to result registers */	/*pipe1*/regs->regs_R[MD_REG_V0] = fd[0];	/*pipe 2*/regs->regs_R[3] = fd[1];	/* check for an error condition */	if (regs->regs_R[7] == (qword_t)-1)	  {	    regs->regs_R[MD_REG_V0] = errno;	    regs->regs_R[7] = 1;	  }      }      break;#endif    case OSF_SYS_getgid:#ifdef _MSC_VER      warn("syscall getgid() not yet implemented for MSC...");      regs->regs_R[MD_REG_A3] = 0;#else /* !_MSC_VER */      /* get current group id */      /*first result*/regs->regs_R[MD_REG_V0] = getgid();      /*second result*/regs->regs_R[MD_REG_A4] = getegid();      /* 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_ioctl:      switch (/* req */regs->regs_R[MD_REG_A1])	{#if !defined(TIOCGETP) && (defined(linux) || defined(__CYGWIN32__))	case OSF_TIOCGETP: /* <Out,TIOCGETP,6> */	  {	    struct termios lbuf;	    struct osf_sgttyb buf;	    /* result */regs->regs_R[MD_REG_V0] =			  tcgetattr(/* fd */(int)regs->regs_R[MD_REG_A0],				    &lbuf);	    /* translate results */	    buf.sg_ispeed = lbuf.c_ispeed;	    buf.sg_ospeed = lbuf.c_ospeed;	    buf.sg_erase = lbuf.c_cc[VERASE];	    buf.sg_kill = lbuf.c_cc[VKILL];	    buf.sg_flags = 0;	/* FIXME: this is wrong... */	    mem_bcopy(mem_fn, mem, Write,		      /* buf */regs->regs_R[MD_REG_A2], &buf,		      sizeof(struct osf_sgttyb));	    if (regs->regs_R[MD_REG_V0] != (qword_t)-1)	      regs->regs_R[MD_REG_A3] = 0;	    else /* probably not a typewriter, return details */	      {		regs->regs_R[MD_REG_A3] = -1;		regs->regs_R[MD_REG_V0] = errno;	      }	  }	  break;#endif#ifdef TIOCGETP	case OSF_TIOCGETP: /* <Out,TIOCGETP,6> */	  {	    struct sgttyb lbuf;	    struct osf_sgttyb buf;	    /* result */regs->regs_R[MD_REG_V0] =	      ioctl(/* fd */(int)regs->regs_R[MD_REG_A0],		    /* req */TIOCGETP,		    &lbuf);	    /* translate results */	    buf.sg_ispeed = lbuf.sg_ispeed;	    buf.sg_ospeed = lbuf.sg_ospeed;	    buf.sg_erase = lbuf.sg_erase;	    buf.sg_kill = lbuf.sg_kill;	    buf.sg_flags = lbuf.sg_flags;	    mem_bcopy(mem_fn, mem, Write,		      /* buf */regs->regs_R[MD_REG_A2], &buf,		      sizeof(struct osf_sgttyb));	    if (regs->regs_R[MD_REG_V0] != (qword_t)-1)	      regs->regs_R[MD_REG_A3] = 0;	    else /* not a typewriter, return details */	      {		regs->regs_R[MD_REG_A3] = -1;		regs->regs_R[MD_REG_V0] = errno;	      }	  }	  break;#endif#ifdef FIONREAD	case OSF_FIONREAD:	  {	    int nread;	    /* result */regs->regs_R[MD_REG_V0] =	      ioctl(/* fd */(int)regs->regs_R[MD_REG_A0],		    /* req */FIONREAD,		    /* arg */&nread);	    mem_bcopy(mem_fn, mem, Write,		      /* arg */regs->regs_R[MD_REG_A2],		      &nread, sizeof(nread));	    if (regs->regs_R[MD_REG_V0] != (qword_t)-1)	      regs->regs_R[MD_REG_A3] = 0;	    else /* not a typewriter, return details */	      {		regs->regs_R[MD_REG_A3] = -1;		regs->regs_R[MD_REG_V0] = errno;	      }	  }	  break;#endif#ifdef FIONBIO	case /*FIXME*/FIONBIO:	  {	    int arg = 0;	    if (regs->regs_R[MD_REG_A2])	      mem_bcopy(mem_fn, mem, Read,		      /* arg */regs->regs_R[MD_REG_A2],		      &arg, sizeof(arg));#ifdef NOTNOW	    fprintf(stderr, "FIONBIO: %d, %d\n",		    (int)regs->regs_R[MD_REG_A0],		    arg);#endif	    /* result */regs->regs_R[MD_REG_V0] =	      ioctl(/* fd */(int)regs->regs_R[MD_REG_A0],		    /* req */FIONBIO,		    /* arg */&arg);	    if (regs->regs_R[MD_REG_A2])	      mem_bcopy(mem_fn, mem, Write,		      /* arg */regs->regs_R[MD_REG_A2],		      &arg, sizeof(arg));	    if (regs->regs_R[MD_REG_V0] != (qword_t)-1)	      regs->regs_R[MD_REG_A3] = 0;	    else /* not a typewriter, return details */	      {		regs->regs_R[MD_REG_A3] = -1;		regs->regs_R[MD_REG_V0] = errno;	      }	  }	  break;#endif	default:	  warn("unsupported ioctl call: ioctl(%ld, ...)",	       regs->regs_R[MD_REG_A1]);	  regs->regs_R[MD_REG_A3] = 0;	  break;	}      break;#if 0      {	char buf[NUM_IOCTL_BYTES];	int local_req = 0;	/* convert target ioctl() request to host ioctl() request values */	switch (/*req*/regs->regs_R[MD_REG_A1]) {/* #if !defined(__CYGWIN32__) */	case SS_IOCTL_TIOCGETP:	  local_req = TIOCGETP;	  break;	case SS_IOCTL_TIOCSETP:	  local_req = TIOCSETP;	  break;	case SS_IOCTL_TCGETP:	  local_req = TIOCGETP;	  break;/* #endif */#ifdef TCGETA	case SS_IOCTL_TCGETA:	  local_req = TCGETA;	  break;#endif#ifdef TIOCGLTC	case SS_IOCTL_TIOCGLTC:	  local_req = TIOCGLTC;	  break;#endif#ifdef TIOCSLTC	case SS_IOCTL_TIOCSLTC:	  local_req = TIOCSLTC;	  break;#endif	case SS_IOCTL_TIOCGWINSZ:	  local_req = TIOCGWINSZ;	  break;#ifdef TCSETAW	case SS_IOCTL_TCSETAW:	  local_req = TCSETAW;	  break;#endif#ifdef TIOCGETC	case SS_IOCTL_TIOCGETC:	  local_req = TIOCGETC;	  break;#endif#ifdef TIOCSETC	case SS_IOCTL_TIOCSETC:	  local_req = TIOCSETC;	  break;#endif#ifdef TIOCLBIC	case SS_IOCTL_TIOCLBIC:	  local_req = TIOCLBIC;	  break;#endif#ifdef TIOCLBIS	case SS_IOCTL_TIOCLBIS:	  local_req = TIOCLBIS;	  break;#endif#ifdef TIOCLGET	case SS_IOCTL_TIOCLGET:	  local_req = TIOCLGET;	  break;#endif#ifdef TIOCLSET	case SS_IOCTL_TIOCLSET:	  local_req = TIOCLSET;	  break;#endif	}	if (!local_req)	  {	    /* FIXME: could not translate the ioctl() request, just warn user	       and ignore the request */	    warn("syscall: ioctl: ioctl code not supported d=%d, req=%d",		 regs->regs_R[MD_REG_A0], regs->regs_R[MD_REG_A1]);	    regs->regs_R[MD_REG_V0] = 0;	    regs->regs_R[7] = 0;	  }	else	  {	    /* ioctl() code was successfully translated to a host code */	    /* if arg ptr exists, copy NUM_IOCTL_BYTES bytes to host mem */	    if (/*argp*/regs->regs_R[MD_REG_A2] != 0)	      mem_bcopy(mem_fn, mem, Read, /*argp*/regs->regs_R[MD_REG_A2],			buf, NUM_IOCTL_BYTES);	    /* perform the ioctl() call */	    /*result*/regs->regs_R[MD_REG_V0] =	      ioctl(/*fd*/regs->regs_R[MD_REG_A0], local_req, buf);	    /* if arg ptr exists, copy NUM_IOCTL_BYTES bytes from host mem */	    if (/*argp*/regs->regs_R[MD_REG_A2] != 0)	      mem_bcopy(mem_fn, mem, Write, regs->regs_R[MD_REG_A2],			buf, NUM_IOCTL_BYTES);	    /* check for an error condition */	    if (regs->regs_R[MD_REG_V0] != (qword_t)-1)	      regs->regs_R[7] = 0;	    else	      {			/* got an error, return details */

⌨️ 快捷键说明

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