2
0

syscall.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <stdint.h>
  22. #include <stdarg.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include <unistd.h>
  26. #include <fcntl.h>
  27. #include <time.h>
  28. #include <limits.h>
  29. #include <sys/types.h>
  30. #include <sys/mman.h>
  31. #include <sys/syscall.h>
  32. #include <sys/param.h>
  33. #include <sys/sysctl.h>
  34. #include <utime.h>
  35. #include "qemu.h"
  36. #include "qemu-common.h"
  37. //#define DEBUG
  38. static abi_ulong target_brk;
  39. static abi_ulong target_original_brk;
  40. static inline abi_long get_errno(abi_long ret)
  41. {
  42. if (ret == -1)
  43. /* XXX need to translate host -> target errnos here */
  44. return -(errno);
  45. else
  46. return ret;
  47. }
  48. #define target_to_host_bitmask(x, tbl) (x)
  49. static inline int is_error(abi_long ret)
  50. {
  51. return (abi_ulong)ret >= (abi_ulong)(-4096);
  52. }
  53. void target_set_brk(abi_ulong new_brk)
  54. {
  55. target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk);
  56. }
  57. /* do_obreak() must return target errnos. */
  58. static abi_long do_obreak(abi_ulong new_brk)
  59. {
  60. abi_ulong brk_page;
  61. abi_long mapped_addr;
  62. int new_alloc_size;
  63. if (!new_brk)
  64. return 0;
  65. if (new_brk < target_original_brk)
  66. return -TARGET_EINVAL;
  67. brk_page = HOST_PAGE_ALIGN(target_brk);
  68. /* If the new brk is less than this, set it and we're done... */
  69. if (new_brk < brk_page) {
  70. target_brk = new_brk;
  71. return 0;
  72. }
  73. /* We need to allocate more memory after the brk... */
  74. new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page + 1);
  75. mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size,
  76. PROT_READ|PROT_WRITE,
  77. MAP_ANON|MAP_FIXED|MAP_PRIVATE, -1, 0));
  78. if (!is_error(mapped_addr))
  79. target_brk = new_brk;
  80. else
  81. return mapped_addr;
  82. return 0;
  83. }
  84. #if defined(TARGET_I386)
  85. static abi_long do_freebsd_sysarch(CPUX86State *env, int op, abi_ulong parms)
  86. {
  87. abi_long ret = 0;
  88. abi_ulong val;
  89. int idx;
  90. switch(op) {
  91. #ifdef TARGET_ABI32
  92. case TARGET_FREEBSD_I386_SET_GSBASE:
  93. case TARGET_FREEBSD_I386_SET_FSBASE:
  94. if (op == TARGET_FREEBSD_I386_SET_GSBASE)
  95. #else
  96. case TARGET_FREEBSD_AMD64_SET_GSBASE:
  97. case TARGET_FREEBSD_AMD64_SET_FSBASE:
  98. if (op == TARGET_FREEBSD_AMD64_SET_GSBASE)
  99. #endif
  100. idx = R_GS;
  101. else
  102. idx = R_FS;
  103. if (get_user(val, parms, abi_ulong))
  104. return -TARGET_EFAULT;
  105. cpu_x86_load_seg(env, idx, 0);
  106. env->segs[idx].base = val;
  107. break;
  108. #ifdef TARGET_ABI32
  109. case TARGET_FREEBSD_I386_GET_GSBASE:
  110. case TARGET_FREEBSD_I386_GET_FSBASE:
  111. if (op == TARGET_FREEBSD_I386_GET_GSBASE)
  112. #else
  113. case TARGET_FREEBSD_AMD64_GET_GSBASE:
  114. case TARGET_FREEBSD_AMD64_GET_FSBASE:
  115. if (op == TARGET_FREEBSD_AMD64_GET_GSBASE)
  116. #endif
  117. idx = R_GS;
  118. else
  119. idx = R_FS;
  120. val = env->segs[idx].base;
  121. if (put_user(val, parms, abi_ulong))
  122. return -TARGET_EFAULT;
  123. break;
  124. /* XXX handle the others... */
  125. default:
  126. ret = -TARGET_EINVAL;
  127. break;
  128. }
  129. return ret;
  130. }
  131. #endif
  132. #ifdef TARGET_SPARC
  133. static abi_long do_freebsd_sysarch(void *env, int op, abi_ulong parms)
  134. {
  135. /* XXX handle
  136. * TARGET_FREEBSD_SPARC_UTRAP_INSTALL,
  137. * TARGET_FREEBSD_SPARC_SIGTRAMP_INSTALL
  138. */
  139. return -TARGET_EINVAL;
  140. }
  141. #endif
  142. #ifdef __FreeBSD__
  143. /*
  144. * XXX this uses the undocumented oidfmt interface to find the kind of
  145. * a requested sysctl, see /sys/kern/kern_sysctl.c:sysctl_sysctl_oidfmt()
  146. * (this is mostly copied from src/sbin/sysctl/sysctl.c)
  147. */
  148. static int
  149. oidfmt(int *oid, int len, char *fmt, uint32_t *kind)
  150. {
  151. int qoid[CTL_MAXNAME+2];
  152. uint8_t buf[BUFSIZ];
  153. int i;
  154. size_t j;
  155. qoid[0] = 0;
  156. qoid[1] = 4;
  157. memcpy(qoid + 2, oid, len * sizeof(int));
  158. j = sizeof(buf);
  159. i = sysctl(qoid, len + 2, buf, &j, 0, 0);
  160. if (i)
  161. return i;
  162. if (kind)
  163. *kind = *(uint32_t *)buf;
  164. if (fmt)
  165. strcpy(fmt, (char *)(buf + sizeof(uint32_t)));
  166. return (0);
  167. }
  168. /*
  169. * try and convert sysctl return data for the target.
  170. * XXX doesn't handle CTLTYPE_OPAQUE and CTLTYPE_STRUCT.
  171. */
  172. static int sysctl_oldcvt(void *holdp, size_t holdlen, uint32_t kind)
  173. {
  174. switch (kind & CTLTYPE) {
  175. case CTLTYPE_INT:
  176. case CTLTYPE_UINT:
  177. *(uint32_t *)holdp = tswap32(*(uint32_t *)holdp);
  178. break;
  179. #ifdef TARGET_ABI32
  180. case CTLTYPE_LONG:
  181. case CTLTYPE_ULONG:
  182. *(uint32_t *)holdp = tswap32(*(long *)holdp);
  183. break;
  184. #else
  185. case CTLTYPE_LONG:
  186. *(uint64_t *)holdp = tswap64(*(long *)holdp);
  187. case CTLTYPE_ULONG:
  188. *(uint64_t *)holdp = tswap64(*(unsigned long *)holdp);
  189. break;
  190. #endif
  191. case CTLTYPE_QUAD:
  192. *(uint64_t *)holdp = tswap64(*(uint64_t *)holdp);
  193. break;
  194. case CTLTYPE_STRING:
  195. break;
  196. default:
  197. /* XXX unhandled */
  198. return -1;
  199. }
  200. return 0;
  201. }
  202. /* XXX this needs to be emulated on non-FreeBSD hosts... */
  203. static abi_long do_freebsd_sysctl(abi_ulong namep, int32_t namelen, abi_ulong oldp,
  204. abi_ulong oldlenp, abi_ulong newp, abi_ulong newlen)
  205. {
  206. abi_long ret;
  207. void *hnamep, *holdp, *hnewp = NULL;
  208. size_t holdlen;
  209. abi_ulong oldlen = 0;
  210. int32_t *snamep = g_malloc(sizeof(int32_t) * namelen), *p, *q, i;
  211. uint32_t kind = 0;
  212. if (oldlenp)
  213. get_user_ual(oldlen, oldlenp);
  214. if (!(hnamep = lock_user(VERIFY_READ, namep, namelen, 1)))
  215. return -TARGET_EFAULT;
  216. if (newp && !(hnewp = lock_user(VERIFY_READ, newp, newlen, 1)))
  217. return -TARGET_EFAULT;
  218. if (!(holdp = lock_user(VERIFY_WRITE, oldp, oldlen, 0)))
  219. return -TARGET_EFAULT;
  220. holdlen = oldlen;
  221. for (p = hnamep, q = snamep, i = 0; i < namelen; p++, i++)
  222. *q++ = tswap32(*p);
  223. oidfmt(snamep, namelen, NULL, &kind);
  224. /* XXX swap hnewp */
  225. ret = get_errno(sysctl(snamep, namelen, holdp, &holdlen, hnewp, newlen));
  226. if (!ret)
  227. sysctl_oldcvt(holdp, holdlen, kind);
  228. put_user_ual(holdlen, oldlenp);
  229. unlock_user(hnamep, namep, 0);
  230. unlock_user(holdp, oldp, holdlen);
  231. if (hnewp)
  232. unlock_user(hnewp, newp, 0);
  233. g_free(snamep);
  234. return ret;
  235. }
  236. #endif
  237. /* FIXME
  238. * lock_iovec()/unlock_iovec() have a return code of 0 for success where
  239. * other lock functions have a return code of 0 for failure.
  240. */
  241. static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
  242. int count, int copy)
  243. {
  244. struct target_iovec *target_vec;
  245. abi_ulong base;
  246. int i;
  247. target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
  248. if (!target_vec)
  249. return -TARGET_EFAULT;
  250. for(i = 0;i < count; i++) {
  251. base = tswapl(target_vec[i].iov_base);
  252. vec[i].iov_len = tswapl(target_vec[i].iov_len);
  253. if (vec[i].iov_len != 0) {
  254. vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
  255. /* Don't check lock_user return value. We must call writev even
  256. if a element has invalid base address. */
  257. } else {
  258. /* zero length pointer is ignored */
  259. vec[i].iov_base = NULL;
  260. }
  261. }
  262. unlock_user (target_vec, target_addr, 0);
  263. return 0;
  264. }
  265. static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
  266. int count, int copy)
  267. {
  268. struct target_iovec *target_vec;
  269. abi_ulong base;
  270. int i;
  271. target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
  272. if (!target_vec)
  273. return -TARGET_EFAULT;
  274. for(i = 0;i < count; i++) {
  275. if (target_vec[i].iov_base) {
  276. base = tswapl(target_vec[i].iov_base);
  277. unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
  278. }
  279. }
  280. unlock_user (target_vec, target_addr, 0);
  281. return 0;
  282. }
  283. /* do_syscall() should always have a single exit point at the end so
  284. that actions, such as logging of syscall results, can be performed.
  285. All errnos that do_syscall() returns must be -TARGET_<errcode>. */
  286. abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
  287. abi_long arg2, abi_long arg3, abi_long arg4,
  288. abi_long arg5, abi_long arg6, abi_long arg7,
  289. abi_long arg8)
  290. {
  291. abi_long ret;
  292. void *p;
  293. #ifdef DEBUG
  294. gemu_log("freebsd syscall %d\n", num);
  295. #endif
  296. if(do_strace)
  297. print_freebsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
  298. switch(num) {
  299. case TARGET_FREEBSD_NR_exit:
  300. #ifdef TARGET_GPROF
  301. _mcleanup();
  302. #endif
  303. gdb_exit(cpu_env, arg1);
  304. /* XXX: should free thread stack and CPU env */
  305. _exit(arg1);
  306. ret = 0; /* avoid warning */
  307. break;
  308. case TARGET_FREEBSD_NR_read:
  309. if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
  310. goto efault;
  311. ret = get_errno(read(arg1, p, arg3));
  312. unlock_user(p, arg2, ret);
  313. break;
  314. case TARGET_FREEBSD_NR_write:
  315. if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
  316. goto efault;
  317. ret = get_errno(write(arg1, p, arg3));
  318. unlock_user(p, arg2, 0);
  319. break;
  320. case TARGET_FREEBSD_NR_writev:
  321. {
  322. int count = arg3;
  323. struct iovec *vec;
  324. vec = alloca(count * sizeof(struct iovec));
  325. if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
  326. goto efault;
  327. ret = get_errno(writev(arg1, vec, count));
  328. unlock_iovec(vec, arg2, count, 0);
  329. }
  330. break;
  331. case TARGET_FREEBSD_NR_open:
  332. if (!(p = lock_user_string(arg1)))
  333. goto efault;
  334. ret = get_errno(open(path(p),
  335. target_to_host_bitmask(arg2, fcntl_flags_tbl),
  336. arg3));
  337. unlock_user(p, arg1, 0);
  338. break;
  339. case TARGET_FREEBSD_NR_mmap:
  340. ret = get_errno(target_mmap(arg1, arg2, arg3,
  341. target_to_host_bitmask(arg4, mmap_flags_tbl),
  342. arg5,
  343. arg6));
  344. break;
  345. case TARGET_FREEBSD_NR_mprotect:
  346. ret = get_errno(target_mprotect(arg1, arg2, arg3));
  347. break;
  348. case TARGET_FREEBSD_NR_break:
  349. ret = do_obreak(arg1);
  350. break;
  351. #ifdef __FreeBSD__
  352. case TARGET_FREEBSD_NR___sysctl:
  353. ret = do_freebsd_sysctl(arg1, arg2, arg3, arg4, arg5, arg6);
  354. break;
  355. #endif
  356. case TARGET_FREEBSD_NR_sysarch:
  357. ret = do_freebsd_sysarch(cpu_env, arg1, arg2);
  358. break;
  359. case TARGET_FREEBSD_NR_syscall:
  360. case TARGET_FREEBSD_NR___syscall:
  361. ret = do_freebsd_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,arg7,arg8,0);
  362. break;
  363. default:
  364. ret = get_errno(syscall(num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8));
  365. break;
  366. }
  367. fail:
  368. #ifdef DEBUG
  369. gemu_log(" = %ld\n", ret);
  370. #endif
  371. if (do_strace)
  372. print_freebsd_syscall_ret(num, ret);
  373. return ret;
  374. efault:
  375. ret = -TARGET_EFAULT;
  376. goto fail;
  377. }
  378. abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1,
  379. abi_long arg2, abi_long arg3, abi_long arg4,
  380. abi_long arg5, abi_long arg6)
  381. {
  382. abi_long ret;
  383. void *p;
  384. #ifdef DEBUG
  385. gemu_log("netbsd syscall %d\n", num);
  386. #endif
  387. if(do_strace)
  388. print_netbsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
  389. switch(num) {
  390. case TARGET_NETBSD_NR_exit:
  391. #ifdef TARGET_GPROF
  392. _mcleanup();
  393. #endif
  394. gdb_exit(cpu_env, arg1);
  395. /* XXX: should free thread stack and CPU env */
  396. _exit(arg1);
  397. ret = 0; /* avoid warning */
  398. break;
  399. case TARGET_NETBSD_NR_read:
  400. if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
  401. goto efault;
  402. ret = get_errno(read(arg1, p, arg3));
  403. unlock_user(p, arg2, ret);
  404. break;
  405. case TARGET_NETBSD_NR_write:
  406. if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
  407. goto efault;
  408. ret = get_errno(write(arg1, p, arg3));
  409. unlock_user(p, arg2, 0);
  410. break;
  411. case TARGET_NETBSD_NR_open:
  412. if (!(p = lock_user_string(arg1)))
  413. goto efault;
  414. ret = get_errno(open(path(p),
  415. target_to_host_bitmask(arg2, fcntl_flags_tbl),
  416. arg3));
  417. unlock_user(p, arg1, 0);
  418. break;
  419. case TARGET_NETBSD_NR_mmap:
  420. ret = get_errno(target_mmap(arg1, arg2, arg3,
  421. target_to_host_bitmask(arg4, mmap_flags_tbl),
  422. arg5,
  423. arg6));
  424. break;
  425. case TARGET_NETBSD_NR_mprotect:
  426. ret = get_errno(target_mprotect(arg1, arg2, arg3));
  427. break;
  428. case TARGET_NETBSD_NR_syscall:
  429. case TARGET_NETBSD_NR___syscall:
  430. ret = do_netbsd_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
  431. break;
  432. default:
  433. ret = syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
  434. break;
  435. }
  436. fail:
  437. #ifdef DEBUG
  438. gemu_log(" = %ld\n", ret);
  439. #endif
  440. if (do_strace)
  441. print_netbsd_syscall_ret(num, ret);
  442. return ret;
  443. efault:
  444. ret = -TARGET_EFAULT;
  445. goto fail;
  446. }
  447. abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1,
  448. abi_long arg2, abi_long arg3, abi_long arg4,
  449. abi_long arg5, abi_long arg6)
  450. {
  451. abi_long ret;
  452. void *p;
  453. #ifdef DEBUG
  454. gemu_log("openbsd syscall %d\n", num);
  455. #endif
  456. if(do_strace)
  457. print_openbsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
  458. switch(num) {
  459. case TARGET_OPENBSD_NR_exit:
  460. #ifdef TARGET_GPROF
  461. _mcleanup();
  462. #endif
  463. gdb_exit(cpu_env, arg1);
  464. /* XXX: should free thread stack and CPU env */
  465. _exit(arg1);
  466. ret = 0; /* avoid warning */
  467. break;
  468. case TARGET_OPENBSD_NR_read:
  469. if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
  470. goto efault;
  471. ret = get_errno(read(arg1, p, arg3));
  472. unlock_user(p, arg2, ret);
  473. break;
  474. case TARGET_OPENBSD_NR_write:
  475. if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
  476. goto efault;
  477. ret = get_errno(write(arg1, p, arg3));
  478. unlock_user(p, arg2, 0);
  479. break;
  480. case TARGET_OPENBSD_NR_open:
  481. if (!(p = lock_user_string(arg1)))
  482. goto efault;
  483. ret = get_errno(open(path(p),
  484. target_to_host_bitmask(arg2, fcntl_flags_tbl),
  485. arg3));
  486. unlock_user(p, arg1, 0);
  487. break;
  488. case TARGET_OPENBSD_NR_mmap:
  489. ret = get_errno(target_mmap(arg1, arg2, arg3,
  490. target_to_host_bitmask(arg4, mmap_flags_tbl),
  491. arg5,
  492. arg6));
  493. break;
  494. case TARGET_OPENBSD_NR_mprotect:
  495. ret = get_errno(target_mprotect(arg1, arg2, arg3));
  496. break;
  497. case TARGET_OPENBSD_NR_syscall:
  498. case TARGET_OPENBSD_NR___syscall:
  499. ret = do_openbsd_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
  500. break;
  501. default:
  502. ret = syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
  503. break;
  504. }
  505. fail:
  506. #ifdef DEBUG
  507. gemu_log(" = %ld\n", ret);
  508. #endif
  509. if (do_strace)
  510. print_openbsd_syscall_ret(num, ret);
  511. return ret;
  512. efault:
  513. ret = -TARGET_EFAULT;
  514. goto fail;
  515. }
  516. void syscall_init(void)
  517. {
  518. }