2
0

m68k-semi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * m68k/ColdFire Semihosting syscall interface
  3. *
  4. * Copyright (c) 2005-2007 CodeSourcery.
  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 <sys/time.h>
  27. #include <time.h>
  28. #include "cpu.h"
  29. #if defined(CONFIG_USER_ONLY)
  30. #include "qemu.h"
  31. #define SEMIHOSTING_HEAP_SIZE (128 * 1024 * 1024)
  32. #else
  33. #include "qemu-common.h"
  34. #include "gdbstub.h"
  35. #include "softmmu-semi.h"
  36. #endif
  37. #include "sysemu.h"
  38. #define HOSTED_EXIT 0
  39. #define HOSTED_INIT_SIM 1
  40. #define HOSTED_OPEN 2
  41. #define HOSTED_CLOSE 3
  42. #define HOSTED_READ 4
  43. #define HOSTED_WRITE 5
  44. #define HOSTED_LSEEK 6
  45. #define HOSTED_RENAME 7
  46. #define HOSTED_UNLINK 8
  47. #define HOSTED_STAT 9
  48. #define HOSTED_FSTAT 10
  49. #define HOSTED_GETTIMEOFDAY 11
  50. #define HOSTED_ISATTY 12
  51. #define HOSTED_SYSTEM 13
  52. typedef uint32_t gdb_mode_t;
  53. typedef uint32_t gdb_time_t;
  54. struct m68k_gdb_stat {
  55. uint32_t gdb_st_dev; /* device */
  56. uint32_t gdb_st_ino; /* inode */
  57. gdb_mode_t gdb_st_mode; /* protection */
  58. uint32_t gdb_st_nlink; /* number of hard links */
  59. uint32_t gdb_st_uid; /* user ID of owner */
  60. uint32_t gdb_st_gid; /* group ID of owner */
  61. uint32_t gdb_st_rdev; /* device type (if inode device) */
  62. uint64_t gdb_st_size; /* total size, in bytes */
  63. uint64_t gdb_st_blksize; /* blocksize for filesystem I/O */
  64. uint64_t gdb_st_blocks; /* number of blocks allocated */
  65. gdb_time_t gdb_st_atime; /* time of last access */
  66. gdb_time_t gdb_st_mtime; /* time of last modification */
  67. gdb_time_t gdb_st_ctime; /* time of last change */
  68. } QEMU_PACKED;
  69. struct gdb_timeval {
  70. gdb_time_t tv_sec; /* second */
  71. uint64_t tv_usec; /* microsecond */
  72. } QEMU_PACKED;
  73. #define GDB_O_RDONLY 0x0
  74. #define GDB_O_WRONLY 0x1
  75. #define GDB_O_RDWR 0x2
  76. #define GDB_O_APPEND 0x8
  77. #define GDB_O_CREAT 0x200
  78. #define GDB_O_TRUNC 0x400
  79. #define GDB_O_EXCL 0x800
  80. static int translate_openflags(int flags)
  81. {
  82. int hf;
  83. if (flags & GDB_O_WRONLY)
  84. hf = O_WRONLY;
  85. else if (flags & GDB_O_RDWR)
  86. hf = O_RDWR;
  87. else
  88. hf = O_RDONLY;
  89. if (flags & GDB_O_APPEND) hf |= O_APPEND;
  90. if (flags & GDB_O_CREAT) hf |= O_CREAT;
  91. if (flags & GDB_O_TRUNC) hf |= O_TRUNC;
  92. if (flags & GDB_O_EXCL) hf |= O_EXCL;
  93. return hf;
  94. }
  95. static void translate_stat(CPUM68KState *env, target_ulong addr, struct stat *s)
  96. {
  97. struct m68k_gdb_stat *p;
  98. if (!(p = lock_user(VERIFY_WRITE, addr, sizeof(struct m68k_gdb_stat), 0)))
  99. /* FIXME - should this return an error code? */
  100. return;
  101. p->gdb_st_dev = cpu_to_be32(s->st_dev);
  102. p->gdb_st_ino = cpu_to_be32(s->st_ino);
  103. p->gdb_st_mode = cpu_to_be32(s->st_mode);
  104. p->gdb_st_nlink = cpu_to_be32(s->st_nlink);
  105. p->gdb_st_uid = cpu_to_be32(s->st_uid);
  106. p->gdb_st_gid = cpu_to_be32(s->st_gid);
  107. p->gdb_st_rdev = cpu_to_be32(s->st_rdev);
  108. p->gdb_st_size = cpu_to_be64(s->st_size);
  109. #ifdef _WIN32
  110. /* Windows stat is missing some fields. */
  111. p->gdb_st_blksize = 0;
  112. p->gdb_st_blocks = 0;
  113. #else
  114. p->gdb_st_blksize = cpu_to_be64(s->st_blksize);
  115. p->gdb_st_blocks = cpu_to_be64(s->st_blocks);
  116. #endif
  117. p->gdb_st_atime = cpu_to_be32(s->st_atime);
  118. p->gdb_st_mtime = cpu_to_be32(s->st_mtime);
  119. p->gdb_st_ctime = cpu_to_be32(s->st_ctime);
  120. unlock_user(p, addr, sizeof(struct m68k_gdb_stat));
  121. }
  122. static int m68k_semi_is_fseek;
  123. static void m68k_semi_cb(CPUM68KState *env, target_ulong ret, target_ulong err)
  124. {
  125. target_ulong args;
  126. args = env->dregs[1];
  127. if (m68k_semi_is_fseek) {
  128. /* FIXME: We've already lost the high bits of the fseek
  129. return value. */
  130. /* FIXME - handle put_user() failure */
  131. put_user_u32(0, args);
  132. args += 4;
  133. m68k_semi_is_fseek = 0;
  134. }
  135. /* FIXME - handle put_user() failure */
  136. put_user_u32(ret, args);
  137. put_user_u32(errno, args + 4);
  138. }
  139. #define ARG(n) \
  140. ({ \
  141. target_ulong __arg; \
  142. /* FIXME - handle get_user() failure */ \
  143. get_user_ual(__arg, args + (n) * 4); \
  144. __arg; \
  145. })
  146. #define PARG(x) ((unsigned long)ARG(x))
  147. void do_m68k_semihosting(CPUM68KState *env, int nr)
  148. {
  149. uint32_t args;
  150. void *p;
  151. void *q;
  152. uint32_t len;
  153. uint32_t result;
  154. args = env->dregs[1];
  155. switch (nr) {
  156. case HOSTED_EXIT:
  157. gdb_exit(env, env->dregs[0]);
  158. exit(env->dregs[0]);
  159. case HOSTED_OPEN:
  160. if (use_gdb_syscalls()) {
  161. gdb_do_syscall(m68k_semi_cb, "open,%s,%x,%x", ARG(0), (int)ARG(1),
  162. ARG(2), ARG(3));
  163. return;
  164. } else {
  165. if (!(p = lock_user_string(ARG(0)))) {
  166. /* FIXME - check error code? */
  167. result = -1;
  168. } else {
  169. result = open(p, translate_openflags(ARG(2)), ARG(3));
  170. unlock_user(p, ARG(0), 0);
  171. }
  172. }
  173. break;
  174. case HOSTED_CLOSE:
  175. {
  176. /* Ignore attempts to close stdin/out/err. */
  177. int fd = ARG(0);
  178. if (fd > 2) {
  179. if (use_gdb_syscalls()) {
  180. gdb_do_syscall(m68k_semi_cb, "close,%x", ARG(0));
  181. return;
  182. } else {
  183. result = close(fd);
  184. }
  185. } else {
  186. result = 0;
  187. }
  188. break;
  189. }
  190. case HOSTED_READ:
  191. len = ARG(2);
  192. if (use_gdb_syscalls()) {
  193. gdb_do_syscall(m68k_semi_cb, "read,%x,%x,%x",
  194. ARG(0), ARG(1), len);
  195. return;
  196. } else {
  197. if (!(p = lock_user(VERIFY_WRITE, ARG(1), len, 0))) {
  198. /* FIXME - check error code? */
  199. result = -1;
  200. } else {
  201. result = read(ARG(0), p, len);
  202. unlock_user(p, ARG(1), len);
  203. }
  204. }
  205. break;
  206. case HOSTED_WRITE:
  207. len = ARG(2);
  208. if (use_gdb_syscalls()) {
  209. gdb_do_syscall(m68k_semi_cb, "write,%x,%x,%x",
  210. ARG(0), ARG(1), len);
  211. return;
  212. } else {
  213. if (!(p = lock_user(VERIFY_READ, ARG(1), len, 1))) {
  214. /* FIXME - check error code? */
  215. result = -1;
  216. } else {
  217. result = write(ARG(0), p, len);
  218. unlock_user(p, ARG(0), 0);
  219. }
  220. }
  221. break;
  222. case HOSTED_LSEEK:
  223. {
  224. uint64_t off;
  225. off = (uint32_t)ARG(2) | ((uint64_t)ARG(1) << 32);
  226. if (use_gdb_syscalls()) {
  227. m68k_semi_is_fseek = 1;
  228. gdb_do_syscall(m68k_semi_cb, "fseek,%x,%lx,%x",
  229. ARG(0), off, ARG(3));
  230. } else {
  231. off = lseek(ARG(0), off, ARG(3));
  232. /* FIXME - handle put_user() failure */
  233. put_user_u32(off >> 32, args);
  234. put_user_u32(off, args + 4);
  235. put_user_u32(errno, args + 8);
  236. }
  237. return;
  238. }
  239. case HOSTED_RENAME:
  240. if (use_gdb_syscalls()) {
  241. gdb_do_syscall(m68k_semi_cb, "rename,%s,%s",
  242. ARG(0), (int)ARG(1), ARG(2), (int)ARG(3));
  243. return;
  244. } else {
  245. p = lock_user_string(ARG(0));
  246. q = lock_user_string(ARG(2));
  247. if (!p || !q) {
  248. /* FIXME - check error code? */
  249. result = -1;
  250. } else {
  251. result = rename(p, q);
  252. }
  253. unlock_user(p, ARG(0), 0);
  254. unlock_user(q, ARG(2), 0);
  255. }
  256. break;
  257. case HOSTED_UNLINK:
  258. if (use_gdb_syscalls()) {
  259. gdb_do_syscall(m68k_semi_cb, "unlink,%s",
  260. ARG(0), (int)ARG(1));
  261. return;
  262. } else {
  263. if (!(p = lock_user_string(ARG(0)))) {
  264. /* FIXME - check error code? */
  265. result = -1;
  266. } else {
  267. result = unlink(p);
  268. unlock_user(p, ARG(0), 0);
  269. }
  270. }
  271. break;
  272. case HOSTED_STAT:
  273. if (use_gdb_syscalls()) {
  274. gdb_do_syscall(m68k_semi_cb, "stat,%s,%x",
  275. ARG(0), (int)ARG(1), ARG(2));
  276. return;
  277. } else {
  278. struct stat s;
  279. if (!(p = lock_user_string(ARG(0)))) {
  280. /* FIXME - check error code? */
  281. result = -1;
  282. } else {
  283. result = stat(p, &s);
  284. unlock_user(p, ARG(0), 0);
  285. }
  286. if (result == 0) {
  287. translate_stat(env, ARG(2), &s);
  288. }
  289. }
  290. break;
  291. case HOSTED_FSTAT:
  292. if (use_gdb_syscalls()) {
  293. gdb_do_syscall(m68k_semi_cb, "fstat,%x,%x",
  294. ARG(0), ARG(1));
  295. return;
  296. } else {
  297. struct stat s;
  298. result = fstat(ARG(0), &s);
  299. if (result == 0) {
  300. translate_stat(env, ARG(1), &s);
  301. }
  302. }
  303. break;
  304. case HOSTED_GETTIMEOFDAY:
  305. if (use_gdb_syscalls()) {
  306. gdb_do_syscall(m68k_semi_cb, "gettimeofday,%x,%x",
  307. ARG(0), ARG(1));
  308. return;
  309. } else {
  310. qemu_timeval tv;
  311. struct gdb_timeval *p;
  312. result = qemu_gettimeofday(&tv);
  313. if (result != 0) {
  314. if (!(p = lock_user(VERIFY_WRITE,
  315. ARG(0), sizeof(struct gdb_timeval), 0))) {
  316. /* FIXME - check error code? */
  317. result = -1;
  318. } else {
  319. p->tv_sec = cpu_to_be32(tv.tv_sec);
  320. p->tv_usec = cpu_to_be64(tv.tv_usec);
  321. unlock_user(p, ARG(0), sizeof(struct gdb_timeval));
  322. }
  323. }
  324. }
  325. break;
  326. case HOSTED_ISATTY:
  327. if (use_gdb_syscalls()) {
  328. gdb_do_syscall(m68k_semi_cb, "isatty,%x", ARG(0));
  329. return;
  330. } else {
  331. result = isatty(ARG(0));
  332. }
  333. break;
  334. case HOSTED_SYSTEM:
  335. if (use_gdb_syscalls()) {
  336. gdb_do_syscall(m68k_semi_cb, "system,%s",
  337. ARG(0), (int)ARG(1));
  338. return;
  339. } else {
  340. if (!(p = lock_user_string(ARG(0)))) {
  341. /* FIXME - check error code? */
  342. result = -1;
  343. } else {
  344. result = system(p);
  345. unlock_user(p, ARG(0), 0);
  346. }
  347. }
  348. break;
  349. case HOSTED_INIT_SIM:
  350. #if defined(CONFIG_USER_ONLY)
  351. {
  352. TaskState *ts = env->opaque;
  353. /* Allocate the heap using sbrk. */
  354. if (!ts->heap_limit) {
  355. abi_ulong ret;
  356. uint32_t size;
  357. uint32_t base;
  358. base = do_brk(0);
  359. size = SEMIHOSTING_HEAP_SIZE;
  360. /* Try a big heap, and reduce the size if that fails. */
  361. for (;;) {
  362. ret = do_brk(base + size);
  363. if (ret >= (base + size)) {
  364. break;
  365. }
  366. size >>= 1;
  367. }
  368. ts->heap_limit = base + size;
  369. }
  370. /* This call may happen before we have writable memory, so return
  371. values directly in registers. */
  372. env->dregs[1] = ts->heap_limit;
  373. env->aregs[7] = ts->stack_base;
  374. }
  375. #else
  376. /* FIXME: This is wrong for boards where RAM does not start at
  377. address zero. */
  378. env->dregs[1] = ram_size;
  379. env->aregs[7] = ram_size;
  380. #endif
  381. return;
  382. default:
  383. cpu_abort(env, "Unsupported semihosting syscall %d\n", nr);
  384. result = 0;
  385. }
  386. /* FIXME - handle put_user() failure */
  387. put_user_u32(result, args);
  388. put_user_u32(errno, args + 4);
  389. }