2
0

m68k-sim.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <errno.h>
  22. #include <fcntl.h>
  23. #include <unistd.h>
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <time.h>
  27. #include "qemu.h"
  28. #define SYS_EXIT 1
  29. #define SYS_READ 3
  30. #define SYS_WRITE 4
  31. #define SYS_OPEN 5
  32. #define SYS_CLOSE 6
  33. #define SYS_BRK 17
  34. #define SYS_FSTAT 28
  35. #define SYS_ISATTY 29
  36. #define SYS_LSEEK 199
  37. struct m68k_sim_stat {
  38. uint16_t sim_st_dev;
  39. uint16_t sim_st_ino;
  40. uint32_t sim_st_mode;
  41. uint16_t sim_st_nlink;
  42. uint16_t sim_st_uid;
  43. uint16_t sim_st_gid;
  44. uint16_t sim_st_rdev;
  45. uint32_t sim_st_size;
  46. uint32_t sim_st_atime;
  47. uint32_t sim_st_mtime;
  48. uint32_t sim_st_ctime;
  49. uint32_t sim_st_blksize;
  50. uint32_t sim_st_blocks;
  51. };
  52. static inline uint32_t check_err(CPUM68KState *env, uint32_t code)
  53. {
  54. env->dregs[0] = code;
  55. if (code == (uint32_t)-1) {
  56. env->dregs[1] = errno;
  57. } else {
  58. env->dregs[1] = 0;
  59. }
  60. return code;
  61. }
  62. #define SIM_O_APPEND 0x0008
  63. #define SIM_O_CREAT 0x0200
  64. #define SIM_O_TRUNC 0x0400
  65. #define SIM_O_EXCL 0x0800
  66. #define SIM_O_NONBLOCK 0x4000
  67. #define SIM_O_NOCTTY 0x8000
  68. #define SIM_O_SYNC 0x2000
  69. static int translate_openflags(int flags)
  70. {
  71. int hf;
  72. switch (flags & 3) {
  73. case 0: hf = O_RDONLY; break;
  74. case 1: hf = O_WRONLY; break;
  75. case 2: hf = O_RDWR; break;
  76. default: hf = O_RDWR; break;
  77. }
  78. if (flags & SIM_O_APPEND) hf |= O_APPEND;
  79. if (flags & SIM_O_CREAT) hf |= O_CREAT;
  80. if (flags & SIM_O_TRUNC) hf |= O_TRUNC;
  81. if (flags & SIM_O_EXCL) hf |= O_EXCL;
  82. if (flags & SIM_O_NONBLOCK) hf |= O_NONBLOCK;
  83. if (flags & SIM_O_NOCTTY) hf |= O_NOCTTY;
  84. if (flags & SIM_O_SYNC) hf |= O_SYNC;
  85. return hf;
  86. }
  87. #define ARG(x) tswap32(args[x])
  88. void do_m68k_simcall(CPUM68KState *env, int nr)
  89. {
  90. M68kCPU *cpu = m68k_env_get_cpu(env);
  91. uint32_t *args;
  92. args = (uint32_t *)(unsigned long)(env->aregs[7] + 4);
  93. switch (nr) {
  94. case SYS_EXIT:
  95. exit(ARG(0));
  96. case SYS_READ:
  97. check_err(env, read(ARG(0), (void *)(unsigned long)ARG(1), ARG(2)));
  98. break;
  99. case SYS_WRITE:
  100. check_err(env, write(ARG(0), (void *)(unsigned long)ARG(1), ARG(2)));
  101. break;
  102. case SYS_OPEN:
  103. check_err(env, open((char *)(unsigned long)ARG(0),
  104. translate_openflags(ARG(1)), ARG(2)));
  105. break;
  106. case SYS_CLOSE:
  107. {
  108. /* Ignore attempts to close stdin/out/err. */
  109. int fd = ARG(0);
  110. if (fd > 2)
  111. check_err(env, close(fd));
  112. else
  113. check_err(env, 0);
  114. break;
  115. }
  116. case SYS_BRK:
  117. {
  118. int32_t ret;
  119. ret = do_brk((abi_ulong)ARG(0));
  120. if (ret == -ENOMEM)
  121. ret = -1;
  122. check_err(env, ret);
  123. }
  124. break;
  125. case SYS_FSTAT:
  126. {
  127. struct stat s;
  128. int rc;
  129. struct m68k_sim_stat *p;
  130. rc = check_err(env, fstat(ARG(0), &s));
  131. if (rc == 0) {
  132. p = (struct m68k_sim_stat *)(unsigned long)ARG(1);
  133. p->sim_st_dev = tswap16(s.st_dev);
  134. p->sim_st_ino = tswap16(s.st_ino);
  135. p->sim_st_mode = tswap32(s.st_mode);
  136. p->sim_st_nlink = tswap16(s.st_nlink);
  137. p->sim_st_uid = tswap16(s.st_uid);
  138. p->sim_st_gid = tswap16(s.st_gid);
  139. p->sim_st_rdev = tswap16(s.st_rdev);
  140. p->sim_st_size = tswap32(s.st_size);
  141. p->sim_st_atime = tswap32(s.st_atime);
  142. p->sim_st_mtime = tswap32(s.st_mtime);
  143. p->sim_st_ctime = tswap32(s.st_ctime);
  144. p->sim_st_blksize = tswap32(s.st_blksize);
  145. p->sim_st_blocks = tswap32(s.st_blocks);
  146. }
  147. }
  148. break;
  149. case SYS_ISATTY:
  150. check_err(env, isatty(ARG(0)));
  151. break;
  152. case SYS_LSEEK:
  153. check_err(env, lseek(ARG(0), (int32_t)ARG(1), ARG(2)));
  154. break;
  155. default:
  156. cpu_abort(CPU(cpu), "Unsupported m68k sim syscall %d\n", nr);
  157. }
  158. }