syscall.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. #ifdef CTLTYPE_U64
  192. case CTLTYPE_S64:
  193. case CTLTYPE_U64:
  194. #else
  195. case CTLTYPE_QUAD:
  196. #endif
  197. *(uint64_t *)holdp = tswap64(*(uint64_t *)holdp);
  198. break;
  199. case CTLTYPE_STRING:
  200. break;
  201. default:
  202. /* XXX unhandled */
  203. return -1;
  204. }
  205. return 0;
  206. }
  207. /* XXX this needs to be emulated on non-FreeBSD hosts... */
  208. static abi_long do_freebsd_sysctl(abi_ulong namep, int32_t namelen, abi_ulong oldp,
  209. abi_ulong oldlenp, abi_ulong newp, abi_ulong newlen)
  210. {
  211. abi_long ret;
  212. void *hnamep, *holdp, *hnewp = NULL;
  213. size_t holdlen;
  214. abi_ulong oldlen = 0;
  215. int32_t *snamep = g_malloc(sizeof(int32_t) * namelen), *p, *q, i;
  216. uint32_t kind = 0;
  217. if (oldlenp)
  218. get_user_ual(oldlen, oldlenp);
  219. if (!(hnamep = lock_user(VERIFY_READ, namep, namelen, 1)))
  220. return -TARGET_EFAULT;
  221. if (newp && !(hnewp = lock_user(VERIFY_READ, newp, newlen, 1)))
  222. return -TARGET_EFAULT;
  223. if (!(holdp = lock_user(VERIFY_WRITE, oldp, oldlen, 0)))
  224. return -TARGET_EFAULT;
  225. holdlen = oldlen;
  226. for (p = hnamep, q = snamep, i = 0; i < namelen; p++, i++)
  227. *q++ = tswap32(*p);
  228. oidfmt(snamep, namelen, NULL, &kind);
  229. /* XXX swap hnewp */
  230. ret = get_errno(sysctl(snamep, namelen, holdp, &holdlen, hnewp, newlen));
  231. if (!ret)
  232. sysctl_oldcvt(holdp, holdlen, kind);
  233. put_user_ual(holdlen, oldlenp);
  234. unlock_user(hnamep, namep, 0);
  235. unlock_user(holdp, oldp, holdlen);
  236. if (hnewp)
  237. unlock_user(hnewp, newp, 0);
  238. g_free(snamep);
  239. return ret;
  240. }
  241. #endif
  242. /* FIXME
  243. * lock_iovec()/unlock_iovec() have a return code of 0 for success where
  244. * other lock functions have a return code of 0 for failure.
  245. */
  246. static abi_long lock_iovec(int type, struct iovec *vec, abi_ulong target_addr,
  247. int count, int copy)
  248. {
  249. struct target_iovec *target_vec;
  250. abi_ulong base;
  251. int i;
  252. target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
  253. if (!target_vec)
  254. return -TARGET_EFAULT;
  255. for(i = 0;i < count; i++) {
  256. base = tswapl(target_vec[i].iov_base);
  257. vec[i].iov_len = tswapl(target_vec[i].iov_len);
  258. if (vec[i].iov_len != 0) {
  259. vec[i].iov_base = lock_user(type, base, vec[i].iov_len, copy);
  260. /* Don't check lock_user return value. We must call writev even
  261. if a element has invalid base address. */
  262. } else {
  263. /* zero length pointer is ignored */
  264. vec[i].iov_base = NULL;
  265. }
  266. }
  267. unlock_user (target_vec, target_addr, 0);
  268. return 0;
  269. }
  270. static abi_long unlock_iovec(struct iovec *vec, abi_ulong target_addr,
  271. int count, int copy)
  272. {
  273. struct target_iovec *target_vec;
  274. abi_ulong base;
  275. int i;
  276. target_vec = lock_user(VERIFY_READ, target_addr, count * sizeof(struct target_iovec), 1);
  277. if (!target_vec)
  278. return -TARGET_EFAULT;
  279. for(i = 0;i < count; i++) {
  280. if (target_vec[i].iov_base) {
  281. base = tswapl(target_vec[i].iov_base);
  282. unlock_user(vec[i].iov_base, base, copy ? vec[i].iov_len : 0);
  283. }
  284. }
  285. unlock_user (target_vec, target_addr, 0);
  286. return 0;
  287. }
  288. /* do_syscall() should always have a single exit point at the end so
  289. that actions, such as logging of syscall results, can be performed.
  290. All errnos that do_syscall() returns must be -TARGET_<errcode>. */
  291. abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
  292. abi_long arg2, abi_long arg3, abi_long arg4,
  293. abi_long arg5, abi_long arg6, abi_long arg7,
  294. abi_long arg8)
  295. {
  296. abi_long ret;
  297. void *p;
  298. #ifdef DEBUG
  299. gemu_log("freebsd syscall %d\n", num);
  300. #endif
  301. if(do_strace)
  302. print_freebsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
  303. switch(num) {
  304. case TARGET_FREEBSD_NR_exit:
  305. #ifdef TARGET_GPROF
  306. _mcleanup();
  307. #endif
  308. gdb_exit(cpu_env, arg1);
  309. /* XXX: should free thread stack and CPU env */
  310. _exit(arg1);
  311. ret = 0; /* avoid warning */
  312. break;
  313. case TARGET_FREEBSD_NR_read:
  314. if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
  315. goto efault;
  316. ret = get_errno(read(arg1, p, arg3));
  317. unlock_user(p, arg2, ret);
  318. break;
  319. case TARGET_FREEBSD_NR_write:
  320. if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
  321. goto efault;
  322. ret = get_errno(write(arg1, p, arg3));
  323. unlock_user(p, arg2, 0);
  324. break;
  325. case TARGET_FREEBSD_NR_writev:
  326. {
  327. int count = arg3;
  328. struct iovec *vec;
  329. vec = alloca(count * sizeof(struct iovec));
  330. if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
  331. goto efault;
  332. ret = get_errno(writev(arg1, vec, count));
  333. unlock_iovec(vec, arg2, count, 0);
  334. }
  335. break;
  336. case TARGET_FREEBSD_NR_open:
  337. if (!(p = lock_user_string(arg1)))
  338. goto efault;
  339. ret = get_errno(open(path(p),
  340. target_to_host_bitmask(arg2, fcntl_flags_tbl),
  341. arg3));
  342. unlock_user(p, arg1, 0);
  343. break;
  344. case TARGET_FREEBSD_NR_mmap:
  345. ret = get_errno(target_mmap(arg1, arg2, arg3,
  346. target_to_host_bitmask(arg4, mmap_flags_tbl),
  347. arg5,
  348. arg6));
  349. break;
  350. case TARGET_FREEBSD_NR_mprotect:
  351. ret = get_errno(target_mprotect(arg1, arg2, arg3));
  352. break;
  353. case TARGET_FREEBSD_NR_break:
  354. ret = do_obreak(arg1);
  355. break;
  356. #ifdef __FreeBSD__
  357. case TARGET_FREEBSD_NR___sysctl:
  358. ret = do_freebsd_sysctl(arg1, arg2, arg3, arg4, arg5, arg6);
  359. break;
  360. #endif
  361. case TARGET_FREEBSD_NR_sysarch:
  362. ret = do_freebsd_sysarch(cpu_env, arg1, arg2);
  363. break;
  364. case TARGET_FREEBSD_NR_syscall:
  365. case TARGET_FREEBSD_NR___syscall:
  366. ret = do_freebsd_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,arg7,arg8,0);
  367. break;
  368. default:
  369. ret = get_errno(syscall(num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8));
  370. break;
  371. }
  372. fail:
  373. #ifdef DEBUG
  374. gemu_log(" = %ld\n", ret);
  375. #endif
  376. if (do_strace)
  377. print_freebsd_syscall_ret(num, ret);
  378. return ret;
  379. efault:
  380. ret = -TARGET_EFAULT;
  381. goto fail;
  382. }
  383. abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1,
  384. abi_long arg2, abi_long arg3, abi_long arg4,
  385. abi_long arg5, abi_long arg6)
  386. {
  387. abi_long ret;
  388. void *p;
  389. #ifdef DEBUG
  390. gemu_log("netbsd syscall %d\n", num);
  391. #endif
  392. if(do_strace)
  393. print_netbsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
  394. switch(num) {
  395. case TARGET_NETBSD_NR_exit:
  396. #ifdef TARGET_GPROF
  397. _mcleanup();
  398. #endif
  399. gdb_exit(cpu_env, arg1);
  400. /* XXX: should free thread stack and CPU env */
  401. _exit(arg1);
  402. ret = 0; /* avoid warning */
  403. break;
  404. case TARGET_NETBSD_NR_read:
  405. if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
  406. goto efault;
  407. ret = get_errno(read(arg1, p, arg3));
  408. unlock_user(p, arg2, ret);
  409. break;
  410. case TARGET_NETBSD_NR_write:
  411. if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
  412. goto efault;
  413. ret = get_errno(write(arg1, p, arg3));
  414. unlock_user(p, arg2, 0);
  415. break;
  416. case TARGET_NETBSD_NR_open:
  417. if (!(p = lock_user_string(arg1)))
  418. goto efault;
  419. ret = get_errno(open(path(p),
  420. target_to_host_bitmask(arg2, fcntl_flags_tbl),
  421. arg3));
  422. unlock_user(p, arg1, 0);
  423. break;
  424. case TARGET_NETBSD_NR_mmap:
  425. ret = get_errno(target_mmap(arg1, arg2, arg3,
  426. target_to_host_bitmask(arg4, mmap_flags_tbl),
  427. arg5,
  428. arg6));
  429. break;
  430. case TARGET_NETBSD_NR_mprotect:
  431. ret = get_errno(target_mprotect(arg1, arg2, arg3));
  432. break;
  433. case TARGET_NETBSD_NR_syscall:
  434. case TARGET_NETBSD_NR___syscall:
  435. ret = do_netbsd_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
  436. break;
  437. default:
  438. ret = syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
  439. break;
  440. }
  441. fail:
  442. #ifdef DEBUG
  443. gemu_log(" = %ld\n", ret);
  444. #endif
  445. if (do_strace)
  446. print_netbsd_syscall_ret(num, ret);
  447. return ret;
  448. efault:
  449. ret = -TARGET_EFAULT;
  450. goto fail;
  451. }
  452. abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1,
  453. abi_long arg2, abi_long arg3, abi_long arg4,
  454. abi_long arg5, abi_long arg6)
  455. {
  456. abi_long ret;
  457. void *p;
  458. #ifdef DEBUG
  459. gemu_log("openbsd syscall %d\n", num);
  460. #endif
  461. if(do_strace)
  462. print_openbsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
  463. switch(num) {
  464. case TARGET_OPENBSD_NR_exit:
  465. #ifdef TARGET_GPROF
  466. _mcleanup();
  467. #endif
  468. gdb_exit(cpu_env, arg1);
  469. /* XXX: should free thread stack and CPU env */
  470. _exit(arg1);
  471. ret = 0; /* avoid warning */
  472. break;
  473. case TARGET_OPENBSD_NR_read:
  474. if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
  475. goto efault;
  476. ret = get_errno(read(arg1, p, arg3));
  477. unlock_user(p, arg2, ret);
  478. break;
  479. case TARGET_OPENBSD_NR_write:
  480. if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
  481. goto efault;
  482. ret = get_errno(write(arg1, p, arg3));
  483. unlock_user(p, arg2, 0);
  484. break;
  485. case TARGET_OPENBSD_NR_open:
  486. if (!(p = lock_user_string(arg1)))
  487. goto efault;
  488. ret = get_errno(open(path(p),
  489. target_to_host_bitmask(arg2, fcntl_flags_tbl),
  490. arg3));
  491. unlock_user(p, arg1, 0);
  492. break;
  493. case TARGET_OPENBSD_NR_mmap:
  494. ret = get_errno(target_mmap(arg1, arg2, arg3,
  495. target_to_host_bitmask(arg4, mmap_flags_tbl),
  496. arg5,
  497. arg6));
  498. break;
  499. case TARGET_OPENBSD_NR_mprotect:
  500. ret = get_errno(target_mprotect(arg1, arg2, arg3));
  501. break;
  502. case TARGET_OPENBSD_NR_syscall:
  503. case TARGET_OPENBSD_NR___syscall:
  504. ret = do_openbsd_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
  505. break;
  506. default:
  507. ret = syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
  508. break;
  509. }
  510. fail:
  511. #ifdef DEBUG
  512. gemu_log(" = %ld\n", ret);
  513. #endif
  514. if (do_strace)
  515. print_openbsd_syscall_ret(num, ret);
  516. return ret;
  517. efault:
  518. ret = -TARGET_EFAULT;
  519. goto fail;
  520. }
  521. void syscall_init(void)
  522. {
  523. }