syscalls.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* syscalls.c --- implement system calls for the RX simulator.
  2. Copyright (C) 2005-2022 Free Software Foundation, Inc.
  3. Contributed by Red Hat, Inc.
  4. This file is part of the GNU simulators.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /* This must come before any other includes. */
  16. #include "defs.h"
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <fcntl.h>
  20. #include <unistd.h>
  21. #include <sys/time.h>
  22. #include "sim/callback.h"
  23. #include "cpu.h"
  24. #include "mem.h"
  25. #include "syscalls.h"
  26. #include "target-newlib-syscall.h"
  27. /* The current syscall callbacks we're using. */
  28. static struct host_callback_struct *callbacks;
  29. void
  30. set_callbacks (struct host_callback_struct *cb)
  31. {
  32. callbacks = cb;
  33. }
  34. struct host_callback_struct *
  35. get_callbacks (void)
  36. {
  37. return callbacks;
  38. }
  39. /* Arguments 1..4 are in R1..R4, remainder on stack.
  40. Return value in R1..R4 as needed.
  41. structs bigger than 16 bytes: pointer pushed on stack last
  42. We only support arguments that fit in general registers.
  43. The system call number is in R5. We expect ssycalls to look like
  44. this in libgloss:
  45. _exit:
  46. mov #SYS_exit, r5
  47. int #255
  48. rts
  49. */
  50. int argp, stackp;
  51. static int
  52. arg (void)
  53. {
  54. int rv = 0;
  55. argp++;
  56. if (argp < 4)
  57. return get_reg (argp);
  58. rv = mem_get_si (get_reg (sp) + stackp);
  59. stackp += 4;
  60. return rv;
  61. }
  62. static void
  63. read_target (char *buffer, int address, int count, int asciiz)
  64. {
  65. char byte;
  66. while (count > 0)
  67. {
  68. byte = mem_get_qi (address++);
  69. *buffer++ = byte;
  70. if (asciiz && (byte == 0))
  71. return;
  72. count--;
  73. }
  74. }
  75. static void
  76. write_target (char *buffer, int address, int count, int asciiz)
  77. {
  78. char byte;
  79. while (count > 0)
  80. {
  81. byte = *buffer++;
  82. mem_put_qi (address++, byte);
  83. if (asciiz && (byte == 0))
  84. return;
  85. count--;
  86. }
  87. }
  88. #define PTRSZ (A16 ? 2 : 3)
  89. static char *callnames[] = {
  90. "SYS_zero",
  91. "SYS_exit",
  92. "SYS_open",
  93. "SYS_close",
  94. "SYS_read",
  95. "SYS_write",
  96. "SYS_lseek",
  97. "SYS_unlink",
  98. "SYS_getpid",
  99. "SYS_kill",
  100. "SYS_fstat",
  101. "SYS_sbrk",
  102. "SYS_argvlen",
  103. "SYS_argv",
  104. "SYS_chdir",
  105. "SYS_stat",
  106. "SYS_chmod",
  107. "SYS_utime",
  108. "SYS_time",
  109. "SYS_gettimeofday",
  110. "SYS_times",
  111. "SYS_link"
  112. };
  113. int
  114. rx_syscall (int id)
  115. {
  116. static char buf[256];
  117. int rv;
  118. argp = 0;
  119. stackp = 4;
  120. if (trace)
  121. printf ("\033[31m/* SYSCALL(%d) = %s */\033[0m\n", id, id <= TARGET_NEWLIB_SYS_link ? callnames[id] : "unknown");
  122. switch (id)
  123. {
  124. case TARGET_NEWLIB_SYS_exit:
  125. {
  126. int ec = arg ();
  127. if (verbose)
  128. printf ("[exit %d]\n", ec);
  129. return RX_MAKE_EXITED (ec);
  130. }
  131. break;
  132. case TARGET_NEWLIB_SYS_open:
  133. {
  134. int oflags, cflags;
  135. int path = arg ();
  136. /* The open function is defined as taking a variable number of arguments
  137. because the third parameter to it is optional:
  138. open (const char * filename, int flags, ...);
  139. Hence the oflags and cflags arguments will be on the stack and we need
  140. to skip the (empty) argument registers r3 and r4. */
  141. argp = 4;
  142. oflags = arg ();
  143. cflags = arg ();
  144. read_target (buf, path, 256, 1);
  145. if (trace)
  146. printf ("open(\"%s\",0x%x,%#o) = ", buf, oflags, cflags);
  147. if (callbacks)
  148. /* The callback vector ignores CFLAGS. */
  149. rv = callbacks->open (callbacks, buf, oflags);
  150. else
  151. {
  152. int h_oflags = 0;
  153. if (oflags & 0x0001)
  154. h_oflags |= O_WRONLY;
  155. if (oflags & 0x0002)
  156. h_oflags |= O_RDWR;
  157. if (oflags & 0x0200)
  158. h_oflags |= O_CREAT;
  159. if (oflags & 0x0008)
  160. h_oflags |= O_APPEND;
  161. if (oflags & 0x0400)
  162. h_oflags |= O_TRUNC;
  163. rv = open (buf, h_oflags, cflags);
  164. }
  165. if (trace)
  166. printf ("%d\n", rv);
  167. put_reg (1, rv);
  168. }
  169. break;
  170. case TARGET_NEWLIB_SYS_close:
  171. {
  172. int fd = arg ();
  173. if (callbacks)
  174. rv = callbacks->close (callbacks, fd);
  175. else if (fd > 2)
  176. rv = close (fd);
  177. else
  178. rv = 0;
  179. if (trace)
  180. printf ("close(%d) = %d\n", fd, rv);
  181. put_reg (1, rv);
  182. }
  183. break;
  184. case TARGET_NEWLIB_SYS_read:
  185. {
  186. int fd = arg ();
  187. int addr = arg ();
  188. int count = arg ();
  189. if (count > sizeof (buf))
  190. count = sizeof (buf);
  191. if (callbacks)
  192. rv = callbacks->read (callbacks, fd, buf, count);
  193. else
  194. rv = read (fd, buf, count);
  195. if (trace)
  196. printf ("read(%d,%d) = %d\n", fd, count, rv);
  197. if (rv > 0)
  198. write_target (buf, addr, rv, 0);
  199. put_reg (1, rv);
  200. }
  201. break;
  202. case TARGET_NEWLIB_SYS_write:
  203. {
  204. int fd = arg ();
  205. int addr = arg ();
  206. int count = arg ();
  207. if (count > sizeof (buf))
  208. count = sizeof (buf);
  209. if (trace)
  210. printf ("write(%d,0x%x,%d)\n", fd, addr, count);
  211. read_target (buf, addr, count, 0);
  212. if (trace)
  213. fflush (stdout);
  214. if (callbacks)
  215. rv = callbacks->write (callbacks, fd, buf, count);
  216. else
  217. rv = write (fd, buf, count);
  218. if (trace)
  219. printf ("write(%d,%d) = %d\n", fd, count, rv);
  220. put_reg (1, rv);
  221. }
  222. break;
  223. case TARGET_NEWLIB_SYS_getpid:
  224. put_reg (1, 42);
  225. break;
  226. case TARGET_NEWLIB_SYS_gettimeofday:
  227. {
  228. int tvaddr = arg ();
  229. struct timeval tv;
  230. rv = gettimeofday (&tv, 0);
  231. if (trace)
  232. printf ("gettimeofday: %ld sec %ld usec to 0x%x\n", tv.tv_sec,
  233. tv.tv_usec, tvaddr);
  234. mem_put_si (tvaddr, tv.tv_sec);
  235. mem_put_si (tvaddr + 4, tv.tv_usec);
  236. put_reg (1, rv);
  237. }
  238. break;
  239. case TARGET_NEWLIB_SYS_kill:
  240. {
  241. int pid = arg ();
  242. int sig = arg ();
  243. if (pid == 42)
  244. {
  245. if (verbose)
  246. printf ("[signal %d]\n", sig);
  247. return RX_MAKE_STOPPED (sig);
  248. }
  249. }
  250. break;
  251. case 11:
  252. {
  253. int heaptop_arg = arg ();
  254. if (trace)
  255. printf ("sbrk: heap top set to %x\n", heaptop_arg);
  256. heaptop = heaptop_arg;
  257. if (heapbottom == 0)
  258. heapbottom = heaptop_arg;
  259. }
  260. break;
  261. case 255:
  262. {
  263. int addr = arg ();
  264. mem_put_si (addr, rx_cycles + mem_usage_cycles());
  265. }
  266. break;
  267. }
  268. return RX_MAKE_STEPPED ();
  269. }