syscall.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * BSD syscalls
  3. *
  4. * Copyright (c) 2003 - 2008 Fabrice Bellard
  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, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  19. * MA 02110-1301, USA.
  20. */
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <stdint.h>
  24. #include <stdarg.h>
  25. #include <string.h>
  26. #include <errno.h>
  27. #include <unistd.h>
  28. #include <fcntl.h>
  29. #include <time.h>
  30. #include <limits.h>
  31. #include <sys/types.h>
  32. #include <sys/mman.h>
  33. #include <sys/syscall.h>
  34. #include <signal.h>
  35. #include <utime.h>
  36. #include "qemu.h"
  37. #include "qemu-common.h"
  38. //#define DEBUG
  39. static abi_ulong target_brk;
  40. static abi_ulong target_original_brk;
  41. #define get_errno(x) (x)
  42. #define target_to_host_bitmask(x, tbl) (x)
  43. void target_set_brk(abi_ulong new_brk)
  44. {
  45. target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
  46. }
  47. /* do_syscall() should always have a single exit point at the end so
  48. that actions, such as logging of syscall results, can be performed.
  49. All errnos that do_syscall() returns must be -TARGET_<errcode>. */
  50. abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
  51. abi_long arg2, abi_long arg3, abi_long arg4,
  52. abi_long arg5, abi_long arg6)
  53. {
  54. abi_long ret;
  55. void *p;
  56. #ifdef DEBUG
  57. gemu_log("freebsd syscall %d\n", num);
  58. #endif
  59. if(do_strace)
  60. print_freebsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
  61. switch(num) {
  62. case TARGET_FREEBSD_NR_exit:
  63. #ifdef HAVE_GPROF
  64. _mcleanup();
  65. #endif
  66. gdb_exit(cpu_env, arg1);
  67. /* XXX: should free thread stack and CPU env */
  68. _exit(arg1);
  69. ret = 0; /* avoid warning */
  70. break;
  71. case TARGET_FREEBSD_NR_read:
  72. if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
  73. goto efault;
  74. ret = get_errno(read(arg1, p, arg3));
  75. unlock_user(p, arg2, ret);
  76. break;
  77. case TARGET_FREEBSD_NR_write:
  78. if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
  79. goto efault;
  80. ret = get_errno(write(arg1, p, arg3));
  81. unlock_user(p, arg2, 0);
  82. break;
  83. case TARGET_FREEBSD_NR_open:
  84. if (!(p = lock_user_string(arg1)))
  85. goto efault;
  86. ret = get_errno(open(path(p),
  87. target_to_host_bitmask(arg2, fcntl_flags_tbl),
  88. arg3));
  89. unlock_user(p, arg1, 0);
  90. break;
  91. case TARGET_FREEBSD_NR_mmap:
  92. ret = get_errno(target_mmap(arg1, arg2, arg3,
  93. target_to_host_bitmask(arg4, mmap_flags_tbl),
  94. arg5,
  95. arg6));
  96. break;
  97. case TARGET_FREEBSD_NR_mprotect:
  98. ret = get_errno(target_mprotect(arg1, arg2, arg3));
  99. break;
  100. case TARGET_FREEBSD_NR_syscall:
  101. case TARGET_FREEBSD_NR___syscall:
  102. ret = do_freebsd_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
  103. break;
  104. default:
  105. ret = syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
  106. break;
  107. }
  108. fail:
  109. #ifdef DEBUG
  110. gemu_log(" = %ld\n", ret);
  111. #endif
  112. if (do_strace)
  113. print_freebsd_syscall_ret(num, ret);
  114. return ret;
  115. efault:
  116. ret = -TARGET_EFAULT;
  117. goto fail;
  118. }
  119. abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1,
  120. abi_long arg2, abi_long arg3, abi_long arg4,
  121. abi_long arg5, abi_long arg6)
  122. {
  123. abi_long ret;
  124. void *p;
  125. #ifdef DEBUG
  126. gemu_log("netbsd syscall %d\n", num);
  127. #endif
  128. if(do_strace)
  129. print_netbsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
  130. switch(num) {
  131. case TARGET_NETBSD_NR_exit:
  132. #ifdef HAVE_GPROF
  133. _mcleanup();
  134. #endif
  135. gdb_exit(cpu_env, arg1);
  136. /* XXX: should free thread stack and CPU env */
  137. _exit(arg1);
  138. ret = 0; /* avoid warning */
  139. break;
  140. case TARGET_NETBSD_NR_read:
  141. if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
  142. goto efault;
  143. ret = get_errno(read(arg1, p, arg3));
  144. unlock_user(p, arg2, ret);
  145. break;
  146. case TARGET_NETBSD_NR_write:
  147. if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
  148. goto efault;
  149. ret = get_errno(write(arg1, p, arg3));
  150. unlock_user(p, arg2, 0);
  151. break;
  152. case TARGET_NETBSD_NR_open:
  153. if (!(p = lock_user_string(arg1)))
  154. goto efault;
  155. ret = get_errno(open(path(p),
  156. target_to_host_bitmask(arg2, fcntl_flags_tbl),
  157. arg3));
  158. unlock_user(p, arg1, 0);
  159. break;
  160. case TARGET_NETBSD_NR_mmap:
  161. ret = get_errno(target_mmap(arg1, arg2, arg3,
  162. target_to_host_bitmask(arg4, mmap_flags_tbl),
  163. arg5,
  164. arg6));
  165. break;
  166. case TARGET_NETBSD_NR_mprotect:
  167. ret = get_errno(target_mprotect(arg1, arg2, arg3));
  168. break;
  169. case TARGET_NETBSD_NR_syscall:
  170. case TARGET_NETBSD_NR___syscall:
  171. ret = do_netbsd_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
  172. break;
  173. default:
  174. ret = syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
  175. break;
  176. }
  177. fail:
  178. #ifdef DEBUG
  179. gemu_log(" = %ld\n", ret);
  180. #endif
  181. if (do_strace)
  182. print_netbsd_syscall_ret(num, ret);
  183. return ret;
  184. efault:
  185. ret = -TARGET_EFAULT;
  186. goto fail;
  187. }
  188. abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1,
  189. abi_long arg2, abi_long arg3, abi_long arg4,
  190. abi_long arg5, abi_long arg6)
  191. {
  192. abi_long ret;
  193. void *p;
  194. #ifdef DEBUG
  195. gemu_log("openbsd syscall %d\n", num);
  196. #endif
  197. if(do_strace)
  198. print_openbsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
  199. switch(num) {
  200. case TARGET_OPENBSD_NR_exit:
  201. #ifdef HAVE_GPROF
  202. _mcleanup();
  203. #endif
  204. gdb_exit(cpu_env, arg1);
  205. /* XXX: should free thread stack and CPU env */
  206. _exit(arg1);
  207. ret = 0; /* avoid warning */
  208. break;
  209. case TARGET_OPENBSD_NR_read:
  210. if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
  211. goto efault;
  212. ret = get_errno(read(arg1, p, arg3));
  213. unlock_user(p, arg2, ret);
  214. break;
  215. case TARGET_OPENBSD_NR_write:
  216. if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
  217. goto efault;
  218. ret = get_errno(write(arg1, p, arg3));
  219. unlock_user(p, arg2, 0);
  220. break;
  221. case TARGET_OPENBSD_NR_open:
  222. if (!(p = lock_user_string(arg1)))
  223. goto efault;
  224. ret = get_errno(open(path(p),
  225. target_to_host_bitmask(arg2, fcntl_flags_tbl),
  226. arg3));
  227. unlock_user(p, arg1, 0);
  228. break;
  229. case TARGET_OPENBSD_NR_mmap:
  230. ret = get_errno(target_mmap(arg1, arg2, arg3,
  231. target_to_host_bitmask(arg4, mmap_flags_tbl),
  232. arg5,
  233. arg6));
  234. break;
  235. case TARGET_OPENBSD_NR_mprotect:
  236. ret = get_errno(target_mprotect(arg1, arg2, arg3));
  237. break;
  238. case TARGET_OPENBSD_NR_syscall:
  239. case TARGET_OPENBSD_NR___syscall:
  240. ret = do_openbsd_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
  241. break;
  242. default:
  243. ret = syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
  244. break;
  245. }
  246. fail:
  247. #ifdef DEBUG
  248. gemu_log(" = %ld\n", ret);
  249. #endif
  250. if (do_strace)
  251. print_openbsd_syscall_ret(num, ret);
  252. return ret;
  253. efault:
  254. ret = -TARGET_EFAULT;
  255. goto fail;
  256. }
  257. void syscall_init(void)
  258. {
  259. }