syscalls.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* syscalls.c --- implement system calls for the M32C simulator.
  2. Copyright (C) 2005-2015 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. #include "config.h"
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <fcntl.h>
  19. #include <unistd.h>
  20. #include <sys/time.h>
  21. #include "gdb/callback.h"
  22. #include "cpu.h"
  23. #include "mem.h"
  24. #include "syscalls.h"
  25. #include "syscall.h"
  26. /* The current syscall callbacks we're using. */
  27. static struct host_callback_struct *callbacks;
  28. void
  29. set_callbacks (struct host_callback_struct *cb)
  30. {
  31. callbacks = cb;
  32. }
  33. /* A16 ABI: arg1 in r1l (QI) or r1 (HI) or stack
  34. arg2 in r2 (HI) or stack
  35. arg3..N on stack
  36. padding: none
  37. A24 ABI: arg1 in r0l (QI) or r0 (HI) or stack
  38. arg2..N on stack
  39. padding: qi->hi
  40. return value in r0l (QI) r0 (HI) r2r0 (SI)
  41. structs: pointer pushed on stack last
  42. */
  43. int argp, stackp;
  44. static int
  45. arg (int bytes)
  46. {
  47. int rv = 0;
  48. argp++;
  49. if (A16)
  50. {
  51. switch (argp)
  52. {
  53. case 1:
  54. if (bytes == 1)
  55. return get_reg (r1l);
  56. if (bytes == 2)
  57. return get_reg (r1);
  58. break;
  59. case 2:
  60. if (bytes == 2)
  61. return get_reg (r2);
  62. break;
  63. }
  64. }
  65. else
  66. {
  67. switch (argp)
  68. {
  69. case 1:
  70. if (bytes == 1)
  71. return get_reg (r0l);
  72. if (bytes == 2)
  73. return get_reg (r0);
  74. break;
  75. }
  76. }
  77. if (bytes == 0)
  78. bytes = 2;
  79. switch (bytes)
  80. {
  81. case 1:
  82. rv = mem_get_qi (get_reg (sp) + stackp);
  83. if (A24)
  84. stackp++;
  85. break;
  86. case 2:
  87. rv = mem_get_hi (get_reg (sp) + stackp);
  88. break;
  89. case 3:
  90. rv = mem_get_psi (get_reg (sp) + stackp);
  91. if (A24)
  92. stackp++;
  93. break;
  94. case 4:
  95. rv = mem_get_si (get_reg (sp) + stackp);
  96. break;
  97. }
  98. stackp += bytes;
  99. return rv;
  100. }
  101. static void
  102. read_target (char *buffer, int address, int count, int asciiz)
  103. {
  104. char byte;
  105. while (count > 0)
  106. {
  107. byte = mem_get_qi (address++);
  108. *buffer++ = byte;
  109. if (asciiz && (byte == 0))
  110. return;
  111. count--;
  112. }
  113. }
  114. static void
  115. write_target (char *buffer, int address, int count, int asciiz)
  116. {
  117. char byte;
  118. while (count > 0)
  119. {
  120. byte = *buffer++;
  121. mem_put_qi (address++, byte);
  122. if (asciiz && (byte == 0))
  123. return;
  124. count--;
  125. }
  126. }
  127. #define PTRSZ (A16 ? 2 : 3)
  128. static char *callnames[] = {
  129. "SYS_zero",
  130. "SYS_exit",
  131. "SYS_open",
  132. "SYS_close",
  133. "SYS_read",
  134. "SYS_write",
  135. "SYS_lseek",
  136. "SYS_unlink",
  137. "SYS_getpid",
  138. "SYS_kill",
  139. "SYS_fstat",
  140. "SYS_sbrk",
  141. "SYS_argvlen",
  142. "SYS_argv",
  143. "SYS_chdir",
  144. "SYS_stat",
  145. "SYS_chmod",
  146. "SYS_utime",
  147. "SYS_time",
  148. "SYS_gettimeofday",
  149. "SYS_times",
  150. "SYS_link"
  151. };
  152. void
  153. m32c_syscall (int id)
  154. {
  155. static char buf[256];
  156. int rv;
  157. argp = 0;
  158. stackp = A16 ? 3 : 4;
  159. if (trace)
  160. printf ("\033[31m/* SYSCALL(%d) = %s */\033[0m\n", id, callnames[id]);
  161. switch (id)
  162. {
  163. case SYS_exit:
  164. {
  165. int ec = arg (2);
  166. if (verbose)
  167. printf ("[exit %d]\n", ec);
  168. step_result = M32C_MAKE_EXITED (ec);
  169. }
  170. break;
  171. case SYS_open:
  172. {
  173. int path = arg (PTRSZ);
  174. int oflags = arg (2);
  175. int cflags = arg (2);
  176. read_target (buf, path, 256, 1);
  177. if (trace)
  178. printf ("open(\"%s\",0x%x,%#o) = ", buf, oflags, cflags);
  179. if (callbacks)
  180. /* The callback vector ignores CFLAGS. */
  181. rv = callbacks->open (callbacks, buf, oflags);
  182. else
  183. {
  184. int h_oflags = 0;
  185. if (oflags & 0x0001)
  186. h_oflags |= O_WRONLY;
  187. if (oflags & 0x0002)
  188. h_oflags |= O_RDWR;
  189. if (oflags & 0x0200)
  190. h_oflags |= O_CREAT;
  191. if (oflags & 0x0008)
  192. h_oflags |= O_APPEND;
  193. if (oflags & 0x0400)
  194. h_oflags |= O_TRUNC;
  195. rv = open (buf, h_oflags, cflags);
  196. }
  197. if (trace)
  198. printf ("%d\n", rv);
  199. put_reg (r0, rv);
  200. }
  201. break;
  202. case SYS_close:
  203. {
  204. int fd = arg (2);
  205. if (callbacks)
  206. rv = callbacks->close (callbacks, fd);
  207. else if (fd > 2)
  208. rv = close (fd);
  209. else
  210. rv = 0;
  211. if (trace)
  212. printf ("close(%d) = %d\n", fd, rv);
  213. put_reg (r0, rv);
  214. }
  215. break;
  216. case SYS_read:
  217. {
  218. int fd = arg (2);
  219. int addr = arg (PTRSZ);
  220. int count = arg (2);
  221. if (count > sizeof (buf))
  222. count = sizeof (buf);
  223. if (callbacks)
  224. rv = callbacks->read (callbacks, fd, buf, count);
  225. else
  226. rv = read (fd, buf, count);
  227. if (trace)
  228. printf ("read(%d,%d) = %d\n", fd, count, rv);
  229. if (rv > 0)
  230. write_target (buf, addr, rv, 0);
  231. put_reg (r0, rv);
  232. }
  233. break;
  234. case SYS_write:
  235. {
  236. int fd = arg (2);
  237. int addr = arg (PTRSZ);
  238. int count = arg (2);
  239. if (count > sizeof (buf))
  240. count = sizeof (buf);
  241. if (trace)
  242. printf ("write(%d,0x%x,%d)\n", fd, addr, count);
  243. read_target (buf, addr, count, 0);
  244. if (trace)
  245. fflush (stdout);
  246. if (callbacks)
  247. rv = callbacks->write (callbacks, fd, buf, count);
  248. else
  249. rv = write (fd, buf, count);
  250. if (trace)
  251. printf ("write(%d,%d) = %d\n", fd, count, rv);
  252. put_reg (r0, rv);
  253. }
  254. break;
  255. case SYS_getpid:
  256. put_reg (r0, 42);
  257. break;
  258. case SYS_gettimeofday:
  259. {
  260. int tvaddr = arg (PTRSZ);
  261. struct timeval tv;
  262. rv = gettimeofday (&tv, 0);
  263. if (trace)
  264. printf ("gettimeofday: %ld sec %ld usec to 0x%x\n", tv.tv_sec,
  265. tv.tv_usec, tvaddr);
  266. mem_put_si (tvaddr, tv.tv_sec);
  267. mem_put_si (tvaddr + 4, tv.tv_usec);
  268. put_reg (r0, rv);
  269. }
  270. break;
  271. case SYS_kill:
  272. {
  273. int pid = arg (2);
  274. int sig = arg (2);
  275. if (pid == 42)
  276. {
  277. if (verbose)
  278. printf ("[signal %d]\n", sig);
  279. step_result = M32C_MAKE_STOPPED (sig);
  280. }
  281. }
  282. break;
  283. case 11:
  284. {
  285. int heaptop_arg = arg (PTRSZ);
  286. if (trace)
  287. printf ("sbrk: heap top set to %x\n", heaptop_arg);
  288. heaptop = heaptop_arg;
  289. if (heapbottom == 0)
  290. heapbottom = heaptop_arg;
  291. }
  292. break;
  293. }
  294. }