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 "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. CPUState *cpu = ENV_GET_CPU(cpu_env);
  288. abi_long ret;
  289. void *p;
  290. #ifdef DEBUG
  291. gemu_log("freebsd syscall %d\n", num);
  292. #endif
  293. trace_guest_user_syscall(cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
  294. if(do_strace)
  295. print_freebsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
  296. switch(num) {
  297. case TARGET_FREEBSD_NR_exit:
  298. #ifdef TARGET_GPROF
  299. _mcleanup();
  300. #endif
  301. gdb_exit(cpu_env, arg1);
  302. /* XXX: should free thread stack and CPU env */
  303. _exit(arg1);
  304. ret = 0; /* avoid warning */
  305. break;
  306. case TARGET_FREEBSD_NR_read:
  307. if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
  308. goto efault;
  309. ret = get_errno(read(arg1, p, arg3));
  310. unlock_user(p, arg2, ret);
  311. break;
  312. case TARGET_FREEBSD_NR_write:
  313. if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
  314. goto efault;
  315. ret = get_errno(write(arg1, p, arg3));
  316. unlock_user(p, arg2, 0);
  317. break;
  318. case TARGET_FREEBSD_NR_writev:
  319. {
  320. int count = arg3;
  321. struct iovec *vec;
  322. vec = alloca(count * sizeof(struct iovec));
  323. if (lock_iovec(VERIFY_READ, vec, arg2, count, 1) < 0)
  324. goto efault;
  325. ret = get_errno(writev(arg1, vec, count));
  326. unlock_iovec(vec, arg2, count, 0);
  327. }
  328. break;
  329. case TARGET_FREEBSD_NR_open:
  330. if (!(p = lock_user_string(arg1)))
  331. goto efault;
  332. ret = get_errno(open(path(p),
  333. target_to_host_bitmask(arg2, fcntl_flags_tbl),
  334. arg3));
  335. unlock_user(p, arg1, 0);
  336. break;
  337. case TARGET_FREEBSD_NR_mmap:
  338. ret = get_errno(target_mmap(arg1, arg2, arg3,
  339. target_to_host_bitmask(arg4, mmap_flags_tbl),
  340. arg5,
  341. arg6));
  342. break;
  343. case TARGET_FREEBSD_NR_mprotect:
  344. ret = get_errno(target_mprotect(arg1, arg2, arg3));
  345. break;
  346. case TARGET_FREEBSD_NR_break:
  347. ret = do_obreak(arg1);
  348. break;
  349. #ifdef __FreeBSD__
  350. case TARGET_FREEBSD_NR___sysctl:
  351. ret = do_freebsd_sysctl(arg1, arg2, arg3, arg4, arg5, arg6);
  352. break;
  353. #endif
  354. case TARGET_FREEBSD_NR_sysarch:
  355. ret = do_freebsd_sysarch(cpu_env, arg1, arg2);
  356. break;
  357. case TARGET_FREEBSD_NR_syscall:
  358. case TARGET_FREEBSD_NR___syscall:
  359. ret = do_freebsd_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,arg7,arg8,0);
  360. break;
  361. default:
  362. ret = get_errno(syscall(num, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8));
  363. break;
  364. }
  365. fail:
  366. #ifdef DEBUG
  367. gemu_log(" = %ld\n", ret);
  368. #endif
  369. if (do_strace)
  370. print_freebsd_syscall_ret(num, ret);
  371. trace_guest_user_syscall_ret(cpu, num, ret);
  372. return ret;
  373. efault:
  374. ret = -TARGET_EFAULT;
  375. goto fail;
  376. }
  377. abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1,
  378. abi_long arg2, abi_long arg3, abi_long arg4,
  379. abi_long arg5, abi_long arg6)
  380. {
  381. CPUState *cpu = ENV_GET_CPU(cpu_env);
  382. abi_long ret;
  383. void *p;
  384. #ifdef DEBUG
  385. gemu_log("netbsd syscall %d\n", num);
  386. #endif
  387. trace_guest_user_syscall(cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, 0, 0);
  388. if(do_strace)
  389. print_netbsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
  390. switch(num) {
  391. case TARGET_NETBSD_NR_exit:
  392. #ifdef TARGET_GPROF
  393. _mcleanup();
  394. #endif
  395. gdb_exit(cpu_env, arg1);
  396. /* XXX: should free thread stack and CPU env */
  397. _exit(arg1);
  398. ret = 0; /* avoid warning */
  399. break;
  400. case TARGET_NETBSD_NR_read:
  401. if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
  402. goto efault;
  403. ret = get_errno(read(arg1, p, arg3));
  404. unlock_user(p, arg2, ret);
  405. break;
  406. case TARGET_NETBSD_NR_write:
  407. if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
  408. goto efault;
  409. ret = get_errno(write(arg1, p, arg3));
  410. unlock_user(p, arg2, 0);
  411. break;
  412. case TARGET_NETBSD_NR_open:
  413. if (!(p = lock_user_string(arg1)))
  414. goto efault;
  415. ret = get_errno(open(path(p),
  416. target_to_host_bitmask(arg2, fcntl_flags_tbl),
  417. arg3));
  418. unlock_user(p, arg1, 0);
  419. break;
  420. case TARGET_NETBSD_NR_mmap:
  421. ret = get_errno(target_mmap(arg1, arg2, arg3,
  422. target_to_host_bitmask(arg4, mmap_flags_tbl),
  423. arg5,
  424. arg6));
  425. break;
  426. case TARGET_NETBSD_NR_mprotect:
  427. ret = get_errno(target_mprotect(arg1, arg2, arg3));
  428. break;
  429. case TARGET_NETBSD_NR_syscall:
  430. case TARGET_NETBSD_NR___syscall:
  431. ret = do_netbsd_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
  432. break;
  433. default:
  434. ret = syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
  435. break;
  436. }
  437. fail:
  438. #ifdef DEBUG
  439. gemu_log(" = %ld\n", ret);
  440. #endif
  441. if (do_strace)
  442. print_netbsd_syscall_ret(num, ret);
  443. trace_guest_user_syscall_ret(cpu, num, ret);
  444. return ret;
  445. efault:
  446. ret = -TARGET_EFAULT;
  447. goto fail;
  448. }
  449. abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1,
  450. abi_long arg2, abi_long arg3, abi_long arg4,
  451. abi_long arg5, abi_long arg6)
  452. {
  453. CPUState *cpu = ENV_GET_CPU(cpu_env);
  454. abi_long ret;
  455. void *p;
  456. #ifdef DEBUG
  457. gemu_log("openbsd syscall %d\n", num);
  458. #endif
  459. trace_guest_user_syscall(cpu, num, arg1, arg2, arg3, arg4, arg5, arg6, 0, 0);
  460. if(do_strace)
  461. print_openbsd_syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
  462. switch(num) {
  463. case TARGET_OPENBSD_NR_exit:
  464. #ifdef TARGET_GPROF
  465. _mcleanup();
  466. #endif
  467. gdb_exit(cpu_env, arg1);
  468. /* XXX: should free thread stack and CPU env */
  469. _exit(arg1);
  470. ret = 0; /* avoid warning */
  471. break;
  472. case TARGET_OPENBSD_NR_read:
  473. if (!(p = lock_user(VERIFY_WRITE, arg2, arg3, 0)))
  474. goto efault;
  475. ret = get_errno(read(arg1, p, arg3));
  476. unlock_user(p, arg2, ret);
  477. break;
  478. case TARGET_OPENBSD_NR_write:
  479. if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
  480. goto efault;
  481. ret = get_errno(write(arg1, p, arg3));
  482. unlock_user(p, arg2, 0);
  483. break;
  484. case TARGET_OPENBSD_NR_open:
  485. if (!(p = lock_user_string(arg1)))
  486. goto efault;
  487. ret = get_errno(open(path(p),
  488. target_to_host_bitmask(arg2, fcntl_flags_tbl),
  489. arg3));
  490. unlock_user(p, arg1, 0);
  491. break;
  492. case TARGET_OPENBSD_NR_mmap:
  493. ret = get_errno(target_mmap(arg1, arg2, arg3,
  494. target_to_host_bitmask(arg4, mmap_flags_tbl),
  495. arg5,
  496. arg6));
  497. break;
  498. case TARGET_OPENBSD_NR_mprotect:
  499. ret = get_errno(target_mprotect(arg1, arg2, arg3));
  500. break;
  501. case TARGET_OPENBSD_NR_syscall:
  502. case TARGET_OPENBSD_NR___syscall:
  503. ret = do_openbsd_syscall(cpu_env,arg1 & 0xffff,arg2,arg3,arg4,arg5,arg6,0);
  504. break;
  505. default:
  506. ret = syscall(num, arg1, arg2, arg3, arg4, arg5, arg6);
  507. break;
  508. }
  509. fail:
  510. #ifdef DEBUG
  511. gemu_log(" = %ld\n", ret);
  512. #endif
  513. if (do_strace)
  514. print_openbsd_syscall_ret(num, ret);
  515. trace_guest_user_syscall_ret(cpu, num, ret);
  516. return ret;
  517. efault:
  518. ret = -TARGET_EFAULT;
  519. goto fail;
  520. }
  521. void syscall_init(void)
  522. {
  523. }