syscall.c 16 KB

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