2
0

m68k-sim.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * m68k simulator syscall interface
  3. *
  4. * Copyright (c) 2005 CodeSourcery, LLC. Written by Paul Brook.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  19. * MA 02110-1301, USA.
  20. */
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <errno.h>
  24. #include <fcntl.h>
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <time.h>
  29. #include "qemu.h"
  30. #define SYS_EXIT 1
  31. #define SYS_READ 3
  32. #define SYS_WRITE 4
  33. #define SYS_OPEN 5
  34. #define SYS_CLOSE 6
  35. #define SYS_BRK 17
  36. #define SYS_FSTAT 28
  37. #define SYS_ISATTY 29
  38. #define SYS_LSEEK 199
  39. struct m86k_sim_stat {
  40. uint16_t sim_st_dev;
  41. uint16_t sim_st_ino;
  42. uint32_t sim_st_mode;
  43. uint16_t sim_st_nlink;
  44. uint16_t sim_st_uid;
  45. uint16_t sim_st_gid;
  46. uint16_t sim_st_rdev;
  47. uint32_t sim_st_size;
  48. uint32_t sim_st_atime;
  49. uint32_t sim_st_mtime;
  50. uint32_t sim_st_ctime;
  51. uint32_t sim_st_blksize;
  52. uint32_t sim_st_blocks;
  53. };
  54. static inline uint32_t check_err(CPUM68KState *env, uint32_t code)
  55. {
  56. env->dregs[0] = code;
  57. if (code == (uint32_t)-1) {
  58. env->dregs[1] = errno;
  59. } else {
  60. env->dregs[1] = 0;
  61. }
  62. return code;
  63. }
  64. #define SIM_O_APPEND 0x0008
  65. #define SIM_O_CREAT 0x0200
  66. #define SIM_O_TRUNC 0x0400
  67. #define SIM_O_EXCL 0x0800
  68. #define SIM_O_NONBLOCK 0x4000
  69. #define SIM_O_NOCTTY 0x8000
  70. #define SIM_O_SYNC 0x2000
  71. static int translate_openflags(int flags)
  72. {
  73. int hf;
  74. switch (flags & 3) {
  75. case 0: hf = O_RDONLY; break;
  76. case 1: hf = O_WRONLY; break;
  77. case 2: hf = O_RDWR; break;
  78. default: hf = O_RDWR; break;
  79. }
  80. if (flags & SIM_O_APPEND) hf |= O_APPEND;
  81. if (flags & SIM_O_CREAT) hf |= O_CREAT;
  82. if (flags & SIM_O_TRUNC) hf |= O_TRUNC;
  83. if (flags & SIM_O_EXCL) hf |= O_EXCL;
  84. if (flags & SIM_O_NONBLOCK) hf |= O_NONBLOCK;
  85. if (flags & SIM_O_NOCTTY) hf |= O_NOCTTY;
  86. if (flags & SIM_O_SYNC) hf |= O_SYNC;
  87. return hf;
  88. }
  89. #define ARG(x) tswap32(args[x])
  90. void do_m68k_simcall(CPUM68KState *env, int nr)
  91. {
  92. uint32_t *args;
  93. args = (uint32_t *)(unsigned long)(env->aregs[7] + 4);
  94. switch (nr) {
  95. case SYS_EXIT:
  96. exit(ARG(0));
  97. case SYS_READ:
  98. check_err(env, read(ARG(0), (void *)(unsigned long)ARG(1), ARG(2)));
  99. break;
  100. case SYS_WRITE:
  101. check_err(env, write(ARG(0), (void *)(unsigned long)ARG(1), ARG(2)));
  102. break;
  103. case SYS_OPEN:
  104. check_err(env, open((char *)(unsigned long)ARG(0),
  105. translate_openflags(ARG(1)), ARG(2)));
  106. break;
  107. case SYS_CLOSE:
  108. {
  109. /* Ignore attempts to close stdin/out/err. */
  110. int fd = ARG(0);
  111. if (fd > 2)
  112. check_err(env, close(fd));
  113. else
  114. check_err(env, 0);
  115. break;
  116. }
  117. case SYS_BRK:
  118. {
  119. int32_t ret;
  120. ret = do_brk((abi_ulong)ARG(0));
  121. if (ret == -ENOMEM)
  122. ret = -1;
  123. check_err(env, ret);
  124. }
  125. break;
  126. case SYS_FSTAT:
  127. {
  128. struct stat s;
  129. int rc;
  130. struct m86k_sim_stat *p;
  131. rc = check_err(env, fstat(ARG(0), &s));
  132. if (rc == 0) {
  133. p = (struct m86k_sim_stat *)(unsigned long)ARG(1);
  134. p->sim_st_dev = tswap16(s.st_dev);
  135. p->sim_st_ino = tswap16(s.st_ino);
  136. p->sim_st_mode = tswap32(s.st_mode);
  137. p->sim_st_nlink = tswap16(s.st_nlink);
  138. p->sim_st_uid = tswap16(s.st_uid);
  139. p->sim_st_gid = tswap16(s.st_gid);
  140. p->sim_st_rdev = tswap16(s.st_rdev);
  141. p->sim_st_size = tswap32(s.st_size);
  142. p->sim_st_atime = tswap32(s.st_atime);
  143. p->sim_st_mtime = tswap32(s.st_mtime);
  144. p->sim_st_ctime = tswap32(s.st_ctime);
  145. p->sim_st_blksize = tswap32(s.st_blksize);
  146. p->sim_st_blocks = tswap32(s.st_blocks);
  147. }
  148. }
  149. break;
  150. case SYS_ISATTY:
  151. check_err(env, isatty(ARG(0)));
  152. break;
  153. case SYS_LSEEK:
  154. check_err(env, lseek(ARG(0), (int32_t)ARG(1), ARG(2)));
  155. break;
  156. default:
  157. cpu_abort(env, "Unsupported m68k sim syscall %d\n", nr);
  158. }
  159. }