arm-semi.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. * Arm "Angel" semihosting syscalls
  3. *
  4. * Copyright (c) 2005, 2007 CodeSourcery.
  5. * Written by Paul Brook.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <fcntl.h>
  23. #include <unistd.h>
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <time.h>
  27. #include "cpu.h"
  28. #ifdef CONFIG_USER_ONLY
  29. #include "qemu.h"
  30. #define ARM_ANGEL_HEAP_SIZE (128 * 1024 * 1024)
  31. #else
  32. #include "qemu-common.h"
  33. #include "gdbstub.h"
  34. #include "hw/arm-misc.h"
  35. #endif
  36. #define SYS_OPEN 0x01
  37. #define SYS_CLOSE 0x02
  38. #define SYS_WRITEC 0x03
  39. #define SYS_WRITE0 0x04
  40. #define SYS_WRITE 0x05
  41. #define SYS_READ 0x06
  42. #define SYS_READC 0x07
  43. #define SYS_ISTTY 0x09
  44. #define SYS_SEEK 0x0a
  45. #define SYS_FLEN 0x0c
  46. #define SYS_TMPNAM 0x0d
  47. #define SYS_REMOVE 0x0e
  48. #define SYS_RENAME 0x0f
  49. #define SYS_CLOCK 0x10
  50. #define SYS_TIME 0x11
  51. #define SYS_SYSTEM 0x12
  52. #define SYS_ERRNO 0x13
  53. #define SYS_GET_CMDLINE 0x15
  54. #define SYS_HEAPINFO 0x16
  55. #define SYS_EXIT 0x18
  56. #ifndef O_BINARY
  57. #define O_BINARY 0
  58. #endif
  59. #define GDB_O_RDONLY 0x000
  60. #define GDB_O_WRONLY 0x001
  61. #define GDB_O_RDWR 0x002
  62. #define GDB_O_APPEND 0x008
  63. #define GDB_O_CREAT 0x200
  64. #define GDB_O_TRUNC 0x400
  65. #define GDB_O_BINARY 0
  66. static int gdb_open_modeflags[12] = {
  67. GDB_O_RDONLY,
  68. GDB_O_RDONLY | GDB_O_BINARY,
  69. GDB_O_RDWR,
  70. GDB_O_RDWR | GDB_O_BINARY,
  71. GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC,
  72. GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC | GDB_O_BINARY,
  73. GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC,
  74. GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC | GDB_O_BINARY,
  75. GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND,
  76. GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND | GDB_O_BINARY,
  77. GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND,
  78. GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND | GDB_O_BINARY
  79. };
  80. static int open_modeflags[12] = {
  81. O_RDONLY,
  82. O_RDONLY | O_BINARY,
  83. O_RDWR,
  84. O_RDWR | O_BINARY,
  85. O_WRONLY | O_CREAT | O_TRUNC,
  86. O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
  87. O_RDWR | O_CREAT | O_TRUNC,
  88. O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
  89. O_WRONLY | O_CREAT | O_APPEND,
  90. O_WRONLY | O_CREAT | O_APPEND | O_BINARY,
  91. O_RDWR | O_CREAT | O_APPEND,
  92. O_RDWR | O_CREAT | O_APPEND | O_BINARY
  93. };
  94. #ifdef CONFIG_USER_ONLY
  95. static inline uint32_t set_swi_errno(TaskState *ts, uint32_t code)
  96. {
  97. if (code == (uint32_t)-1)
  98. ts->swi_errno = errno;
  99. return code;
  100. }
  101. #else
  102. static inline uint32_t set_swi_errno(CPUState *env, uint32_t code)
  103. {
  104. return code;
  105. }
  106. #include "softmmu-semi.h"
  107. #endif
  108. static target_ulong arm_semi_syscall_len;
  109. #if !defined(CONFIG_USER_ONLY)
  110. static target_ulong syscall_err;
  111. #endif
  112. static void arm_semi_cb(CPUState *env, target_ulong ret, target_ulong err)
  113. {
  114. #ifdef CONFIG_USER_ONLY
  115. TaskState *ts = env->opaque;
  116. #endif
  117. if (ret == (target_ulong)-1) {
  118. #ifdef CONFIG_USER_ONLY
  119. ts->swi_errno = err;
  120. #else
  121. syscall_err = err;
  122. #endif
  123. env->regs[0] = ret;
  124. } else {
  125. /* Fixup syscalls that use nonstardard return conventions. */
  126. switch (env->regs[0]) {
  127. case SYS_WRITE:
  128. case SYS_READ:
  129. env->regs[0] = arm_semi_syscall_len - ret;
  130. break;
  131. case SYS_SEEK:
  132. env->regs[0] = 0;
  133. break;
  134. default:
  135. env->regs[0] = ret;
  136. break;
  137. }
  138. }
  139. }
  140. static void arm_semi_flen_cb(CPUState *env, target_ulong ret, target_ulong err)
  141. {
  142. /* The size is always stored in big-endian order, extract
  143. the value. We assume the size always fit in 32 bits. */
  144. uint32_t size;
  145. cpu_memory_rw_debug(env, env->regs[13]-64+32, (uint8_t *)&size, 4, 0);
  146. env->regs[0] = be32_to_cpu(size);
  147. #ifdef CONFIG_USER_ONLY
  148. ((TaskState *)env->opaque)->swi_errno = err;
  149. #else
  150. syscall_err = err;
  151. #endif
  152. }
  153. #define ARG(n) \
  154. ({ \
  155. target_ulong __arg; \
  156. /* FIXME - handle get_user() failure */ \
  157. get_user_ual(__arg, args + (n) * 4); \
  158. __arg; \
  159. })
  160. #define SET_ARG(n, val) put_user_ual(val, args + (n) * 4)
  161. uint32_t do_arm_semihosting(CPUState *env)
  162. {
  163. target_ulong args;
  164. char * s;
  165. int nr;
  166. uint32_t ret;
  167. uint32_t len;
  168. #ifdef CONFIG_USER_ONLY
  169. TaskState *ts = env->opaque;
  170. #else
  171. CPUState *ts = env;
  172. #endif
  173. nr = env->regs[0];
  174. args = env->regs[1];
  175. switch (nr) {
  176. case SYS_OPEN:
  177. if (!(s = lock_user_string(ARG(0))))
  178. /* FIXME - should this error code be -TARGET_EFAULT ? */
  179. return (uint32_t)-1;
  180. if (ARG(1) >= 12)
  181. return (uint32_t)-1;
  182. if (strcmp(s, ":tt") == 0) {
  183. if (ARG(1) < 4)
  184. return STDIN_FILENO;
  185. else
  186. return STDOUT_FILENO;
  187. }
  188. if (use_gdb_syscalls()) {
  189. gdb_do_syscall(arm_semi_cb, "open,%s,%x,1a4", ARG(0),
  190. (int)ARG(2)+1, gdb_open_modeflags[ARG(1)]);
  191. return env->regs[0];
  192. } else {
  193. ret = set_swi_errno(ts, open(s, open_modeflags[ARG(1)], 0644));
  194. }
  195. unlock_user(s, ARG(0), 0);
  196. return ret;
  197. case SYS_CLOSE:
  198. if (use_gdb_syscalls()) {
  199. gdb_do_syscall(arm_semi_cb, "close,%x", ARG(0));
  200. return env->regs[0];
  201. } else {
  202. return set_swi_errno(ts, close(ARG(0)));
  203. }
  204. case SYS_WRITEC:
  205. {
  206. char c;
  207. if (get_user_u8(c, args))
  208. /* FIXME - should this error code be -TARGET_EFAULT ? */
  209. return (uint32_t)-1;
  210. /* Write to debug console. stderr is near enough. */
  211. if (use_gdb_syscalls()) {
  212. gdb_do_syscall(arm_semi_cb, "write,2,%x,1", args);
  213. return env->regs[0];
  214. } else {
  215. return write(STDERR_FILENO, &c, 1);
  216. }
  217. }
  218. case SYS_WRITE0:
  219. if (!(s = lock_user_string(args)))
  220. /* FIXME - should this error code be -TARGET_EFAULT ? */
  221. return (uint32_t)-1;
  222. len = strlen(s);
  223. if (use_gdb_syscalls()) {
  224. gdb_do_syscall(arm_semi_cb, "write,2,%x,%x\n", args, len);
  225. ret = env->regs[0];
  226. } else {
  227. ret = write(STDERR_FILENO, s, len);
  228. }
  229. unlock_user(s, args, 0);
  230. return ret;
  231. case SYS_WRITE:
  232. len = ARG(2);
  233. if (use_gdb_syscalls()) {
  234. arm_semi_syscall_len = len;
  235. gdb_do_syscall(arm_semi_cb, "write,%x,%x,%x", ARG(0), ARG(1), len);
  236. return env->regs[0];
  237. } else {
  238. if (!(s = lock_user(VERIFY_READ, ARG(1), len, 1)))
  239. /* FIXME - should this error code be -TARGET_EFAULT ? */
  240. return (uint32_t)-1;
  241. ret = set_swi_errno(ts, write(ARG(0), s, len));
  242. unlock_user(s, ARG(1), 0);
  243. if (ret == (uint32_t)-1)
  244. return -1;
  245. return len - ret;
  246. }
  247. case SYS_READ:
  248. len = ARG(2);
  249. if (use_gdb_syscalls()) {
  250. arm_semi_syscall_len = len;
  251. gdb_do_syscall(arm_semi_cb, "read,%x,%x,%x", ARG(0), ARG(1), len);
  252. return env->regs[0];
  253. } else {
  254. if (!(s = lock_user(VERIFY_WRITE, ARG(1), len, 0)))
  255. /* FIXME - should this error code be -TARGET_EFAULT ? */
  256. return (uint32_t)-1;
  257. do
  258. ret = set_swi_errno(ts, read(ARG(0), s, len));
  259. while (ret == -1 && errno == EINTR);
  260. unlock_user(s, ARG(1), len);
  261. if (ret == (uint32_t)-1)
  262. return -1;
  263. return len - ret;
  264. }
  265. case SYS_READC:
  266. /* XXX: Read from debug cosole. Not implemented. */
  267. return 0;
  268. case SYS_ISTTY:
  269. if (use_gdb_syscalls()) {
  270. gdb_do_syscall(arm_semi_cb, "isatty,%x", ARG(0));
  271. return env->regs[0];
  272. } else {
  273. return isatty(ARG(0));
  274. }
  275. case SYS_SEEK:
  276. if (use_gdb_syscalls()) {
  277. gdb_do_syscall(arm_semi_cb, "lseek,%x,%x,0", ARG(0), ARG(1));
  278. return env->regs[0];
  279. } else {
  280. ret = set_swi_errno(ts, lseek(ARG(0), ARG(1), SEEK_SET));
  281. if (ret == (uint32_t)-1)
  282. return -1;
  283. return 0;
  284. }
  285. case SYS_FLEN:
  286. if (use_gdb_syscalls()) {
  287. gdb_do_syscall(arm_semi_flen_cb, "fstat,%x,%x",
  288. ARG(0), env->regs[13]-64);
  289. return env->regs[0];
  290. } else {
  291. struct stat buf;
  292. ret = set_swi_errno(ts, fstat(ARG(0), &buf));
  293. if (ret == (uint32_t)-1)
  294. return -1;
  295. return buf.st_size;
  296. }
  297. case SYS_TMPNAM:
  298. /* XXX: Not implemented. */
  299. return -1;
  300. case SYS_REMOVE:
  301. if (use_gdb_syscalls()) {
  302. gdb_do_syscall(arm_semi_cb, "unlink,%s", ARG(0), (int)ARG(1)+1);
  303. ret = env->regs[0];
  304. } else {
  305. if (!(s = lock_user_string(ARG(0))))
  306. /* FIXME - should this error code be -TARGET_EFAULT ? */
  307. return (uint32_t)-1;
  308. ret = set_swi_errno(ts, remove(s));
  309. unlock_user(s, ARG(0), 0);
  310. }
  311. return ret;
  312. case SYS_RENAME:
  313. if (use_gdb_syscalls()) {
  314. gdb_do_syscall(arm_semi_cb, "rename,%s,%s",
  315. ARG(0), (int)ARG(1)+1, ARG(2), (int)ARG(3)+1);
  316. return env->regs[0];
  317. } else {
  318. char *s2;
  319. s = lock_user_string(ARG(0));
  320. s2 = lock_user_string(ARG(2));
  321. if (!s || !s2)
  322. /* FIXME - should this error code be -TARGET_EFAULT ? */
  323. ret = (uint32_t)-1;
  324. else
  325. ret = set_swi_errno(ts, rename(s, s2));
  326. if (s2)
  327. unlock_user(s2, ARG(2), 0);
  328. if (s)
  329. unlock_user(s, ARG(0), 0);
  330. return ret;
  331. }
  332. case SYS_CLOCK:
  333. return clock() / (CLOCKS_PER_SEC / 100);
  334. case SYS_TIME:
  335. return set_swi_errno(ts, time(NULL));
  336. case SYS_SYSTEM:
  337. if (use_gdb_syscalls()) {
  338. gdb_do_syscall(arm_semi_cb, "system,%s", ARG(0), (int)ARG(1)+1);
  339. return env->regs[0];
  340. } else {
  341. if (!(s = lock_user_string(ARG(0))))
  342. /* FIXME - should this error code be -TARGET_EFAULT ? */
  343. return (uint32_t)-1;
  344. ret = set_swi_errno(ts, system(s));
  345. unlock_user(s, ARG(0), 0);
  346. return ret;
  347. }
  348. case SYS_ERRNO:
  349. #ifdef CONFIG_USER_ONLY
  350. return ts->swi_errno;
  351. #else
  352. return syscall_err;
  353. #endif
  354. case SYS_GET_CMDLINE:
  355. {
  356. /* Build a command-line from the original argv.
  357. *
  358. * The inputs are:
  359. * * ARG(0), pointer to a buffer of at least the size
  360. * specified in ARG(1).
  361. * * ARG(1), size of the buffer pointed to by ARG(0) in
  362. * bytes.
  363. *
  364. * The outputs are:
  365. * * ARG(0), pointer to null-terminated string of the
  366. * command line.
  367. * * ARG(1), length of the string pointed to by ARG(0).
  368. */
  369. char *output_buffer;
  370. size_t input_size = ARG(1);
  371. size_t output_size;
  372. int status = 0;
  373. /* Compute the size of the output string. */
  374. #if !defined(CONFIG_USER_ONLY)
  375. output_size = strlen(ts->boot_info->kernel_filename)
  376. + 1 /* Separating space. */
  377. + strlen(ts->boot_info->kernel_cmdline)
  378. + 1; /* Terminating null byte. */
  379. #else
  380. unsigned int i;
  381. output_size = ts->info->arg_end - ts->info->arg_start;
  382. if (!output_size) {
  383. /* We special-case the "empty command line" case (argc==0).
  384. Just provide the terminating 0. */
  385. output_size = 1;
  386. }
  387. #endif
  388. if (output_size > input_size) {
  389. /* Not enough space to store command-line arguments. */
  390. return -1;
  391. }
  392. /* Adjust the command-line length. */
  393. SET_ARG(1, output_size - 1);
  394. /* Lock the buffer on the ARM side. */
  395. output_buffer = lock_user(VERIFY_WRITE, ARG(0), output_size, 0);
  396. if (!output_buffer) {
  397. return -1;
  398. }
  399. /* Copy the command-line arguments. */
  400. #if !defined(CONFIG_USER_ONLY)
  401. pstrcpy(output_buffer, output_size, ts->boot_info->kernel_filename);
  402. pstrcat(output_buffer, output_size, " ");
  403. pstrcat(output_buffer, output_size, ts->boot_info->kernel_cmdline);
  404. #else
  405. if (output_size == 1) {
  406. /* Empty command-line. */
  407. output_buffer[0] = '\0';
  408. goto out;
  409. }
  410. if (copy_from_user(output_buffer, ts->info->arg_start,
  411. output_size)) {
  412. status = -1;
  413. goto out;
  414. }
  415. /* Separate arguments by white spaces. */
  416. for (i = 0; i < output_size - 1; i++) {
  417. if (output_buffer[i] == 0) {
  418. output_buffer[i] = ' ';
  419. }
  420. }
  421. out:
  422. #endif
  423. /* Unlock the buffer on the ARM side. */
  424. unlock_user(output_buffer, ARG(0), output_size);
  425. return status;
  426. }
  427. case SYS_HEAPINFO:
  428. {
  429. uint32_t *ptr;
  430. uint32_t limit;
  431. #ifdef CONFIG_USER_ONLY
  432. /* Some C libraries assume the heap immediately follows .bss, so
  433. allocate it using sbrk. */
  434. if (!ts->heap_limit) {
  435. abi_ulong ret;
  436. ts->heap_base = do_brk(0);
  437. limit = ts->heap_base + ARM_ANGEL_HEAP_SIZE;
  438. /* Try a big heap, and reduce the size if that fails. */
  439. for (;;) {
  440. ret = do_brk(limit);
  441. if (ret >= limit) {
  442. break;
  443. }
  444. limit = (ts->heap_base >> 1) + (limit >> 1);
  445. }
  446. ts->heap_limit = limit;
  447. }
  448. if (!(ptr = lock_user(VERIFY_WRITE, ARG(0), 16, 0)))
  449. /* FIXME - should this error code be -TARGET_EFAULT ? */
  450. return (uint32_t)-1;
  451. ptr[0] = tswap32(ts->heap_base);
  452. ptr[1] = tswap32(ts->heap_limit);
  453. ptr[2] = tswap32(ts->stack_base);
  454. ptr[3] = tswap32(0); /* Stack limit. */
  455. unlock_user(ptr, ARG(0), 16);
  456. #else
  457. limit = ram_size;
  458. if (!(ptr = lock_user(VERIFY_WRITE, ARG(0), 16, 0)))
  459. /* FIXME - should this error code be -TARGET_EFAULT ? */
  460. return (uint32_t)-1;
  461. /* TODO: Make this use the limit of the loaded application. */
  462. ptr[0] = tswap32(limit / 2);
  463. ptr[1] = tswap32(limit);
  464. ptr[2] = tswap32(limit); /* Stack base */
  465. ptr[3] = tswap32(0); /* Stack limit. */
  466. unlock_user(ptr, ARG(0), 16);
  467. #endif
  468. return 0;
  469. }
  470. case SYS_EXIT:
  471. gdb_exit(env, 0);
  472. exit(0);
  473. default:
  474. fprintf(stderr, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr);
  475. cpu_dump_state(env, stderr, fprintf, 0);
  476. abort();
  477. }
  478. }