os-proc.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * FreeBSD process related emulation code
  3. *
  4. * Copyright (c) 2013-15 Stacey D. Son
  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 <sys/param.h>
  21. #include <sys/queue.h>
  22. #include <sys/sysctl.h>
  23. struct kinfo_proc;
  24. #include <libprocstat.h>
  25. #include "qemu.h"
  26. /*
  27. * execve/fexecve
  28. */
  29. abi_long freebsd_exec_common(abi_ulong path_or_fd, abi_ulong guest_argp,
  30. abi_ulong guest_envp, int do_fexec)
  31. {
  32. char **argp, **envp, **qarg0;
  33. int argc, envc;
  34. abi_ulong gp;
  35. abi_ulong addr;
  36. char **q;
  37. int total_size = 0;
  38. void *p;
  39. abi_long ret;
  40. argc = 0;
  41. for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
  42. if (get_user_ual(addr, gp)) {
  43. return -TARGET_EFAULT;
  44. }
  45. if (!addr) {
  46. break;
  47. }
  48. argc++;
  49. }
  50. envc = 0;
  51. for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
  52. if (get_user_ual(addr, gp)) {
  53. return -TARGET_EFAULT;
  54. }
  55. if (!addr) {
  56. break;
  57. }
  58. envc++;
  59. }
  60. qarg0 = argp = g_new0(char *, argc + 9);
  61. /* save the first argument for the emulator */
  62. *argp++ = (char *)getprogname();
  63. *argp++ = (char *)getprogname();
  64. envp = g_new0(char *, envc + 1);
  65. for (gp = guest_argp, q = argp; gp; gp += sizeof(abi_ulong), q++) {
  66. if (get_user_ual(addr, gp)) {
  67. ret = -TARGET_EFAULT;
  68. goto execve_end;
  69. }
  70. if (!addr) {
  71. break;
  72. }
  73. *q = lock_user_string(addr);
  74. if (*q == NULL) {
  75. ret = -TARGET_EFAULT;
  76. goto execve_end;
  77. }
  78. total_size += strlen(*q) + 1;
  79. }
  80. *q++ = NULL;
  81. for (gp = guest_envp, q = envp; gp; gp += sizeof(abi_ulong), q++) {
  82. if (get_user_ual(addr, gp)) {
  83. ret = -TARGET_EFAULT;
  84. goto execve_end;
  85. }
  86. if (!addr) {
  87. break;
  88. }
  89. *q = lock_user_string(addr);
  90. if (*q == NULL) {
  91. ret = -TARGET_EFAULT;
  92. goto execve_end;
  93. }
  94. total_size += strlen(*q) + 1;
  95. }
  96. *q = NULL;
  97. /*
  98. * This case will not be caught by the host's execve() if its
  99. * page size is bigger than the target's.
  100. */
  101. if (total_size > MAX_ARG_PAGES * TARGET_PAGE_SIZE) {
  102. ret = -TARGET_E2BIG;
  103. goto execve_end;
  104. }
  105. if (do_fexec) {
  106. ret = get_errno(fexecve((int)path_or_fd, argp, envp));
  107. } else {
  108. p = lock_user_string(path_or_fd);
  109. if (p == NULL) {
  110. ret = -TARGET_EFAULT;
  111. goto execve_end;
  112. }
  113. ret = get_errno(execve(p, argp, envp));
  114. unlock_user(p, path_or_fd, 0);
  115. }
  116. execve_end:
  117. for (gp = guest_argp, q = argp; *q; gp += sizeof(abi_ulong), q++) {
  118. if (get_user_ual(addr, gp) || !addr) {
  119. break;
  120. }
  121. unlock_user(*q, addr, 0);
  122. }
  123. for (gp = guest_envp, q = envp; *q; gp += sizeof(abi_ulong), q++) {
  124. if (get_user_ual(addr, gp) || !addr) {
  125. break;
  126. }
  127. unlock_user(*q, addr, 0);
  128. }
  129. g_free(qarg0);
  130. g_free(envp);
  131. return ret;
  132. }
  133. #include <sys/procctl.h>
  134. static abi_long
  135. t2h_procctl_cmd(int target_cmd, int *host_cmd)
  136. {
  137. switch (target_cmd) {
  138. case TARGET_PROC_SPROTECT:
  139. *host_cmd = PROC_SPROTECT;
  140. break;
  141. case TARGET_PROC_REAP_ACQUIRE:
  142. *host_cmd = PROC_REAP_ACQUIRE;
  143. break;
  144. case TARGET_PROC_REAP_RELEASE:
  145. *host_cmd = PROC_REAP_RELEASE;
  146. break;
  147. case TARGET_PROC_REAP_STATUS:
  148. *host_cmd = PROC_REAP_STATUS;
  149. break;
  150. case TARGET_PROC_REAP_KILL:
  151. *host_cmd = PROC_REAP_KILL;
  152. break;
  153. default:
  154. return -TARGET_EINVAL;
  155. }
  156. return 0;
  157. }
  158. static abi_long
  159. h2t_reaper_status(struct procctl_reaper_status *host_rs,
  160. abi_ulong target_rs_addr)
  161. {
  162. struct target_procctl_reaper_status *target_rs;
  163. if (!lock_user_struct(VERIFY_WRITE, target_rs, target_rs_addr, 0)) {
  164. return -TARGET_EFAULT;
  165. }
  166. __put_user(host_rs->rs_flags, &target_rs->rs_flags);
  167. __put_user(host_rs->rs_children, &target_rs->rs_children);
  168. __put_user(host_rs->rs_descendants, &target_rs->rs_descendants);
  169. __put_user(host_rs->rs_reaper, &target_rs->rs_reaper);
  170. __put_user(host_rs->rs_pid, &target_rs->rs_pid);
  171. unlock_user_struct(target_rs, target_rs_addr, 1);
  172. return 0;
  173. }
  174. static abi_long
  175. t2h_reaper_kill(abi_ulong target_rk_addr, struct procctl_reaper_kill *host_rk)
  176. {
  177. struct target_procctl_reaper_kill *target_rk;
  178. if (!lock_user_struct(VERIFY_READ, target_rk, target_rk_addr, 1)) {
  179. return -TARGET_EFAULT;
  180. }
  181. __get_user(host_rk->rk_sig, &target_rk->rk_sig);
  182. __get_user(host_rk->rk_flags, &target_rk->rk_flags);
  183. __get_user(host_rk->rk_subtree, &target_rk->rk_subtree);
  184. __get_user(host_rk->rk_killed, &target_rk->rk_killed);
  185. __get_user(host_rk->rk_fpid, &target_rk->rk_fpid);
  186. unlock_user_struct(target_rk, target_rk_addr, 0);
  187. return 0;
  188. }
  189. static abi_long
  190. h2t_reaper_kill(struct procctl_reaper_kill *host_rk, abi_ulong target_rk_addr)
  191. {
  192. struct target_procctl_reaper_kill *target_rk;
  193. if (!lock_user_struct(VERIFY_WRITE, target_rk, target_rk_addr, 0)) {
  194. return -TARGET_EFAULT;
  195. }
  196. __put_user(host_rk->rk_sig, &target_rk->rk_sig);
  197. __put_user(host_rk->rk_flags, &target_rk->rk_flags);
  198. __put_user(host_rk->rk_subtree, &target_rk->rk_subtree);
  199. __put_user(host_rk->rk_killed, &target_rk->rk_killed);
  200. __put_user(host_rk->rk_fpid, &target_rk->rk_fpid);
  201. unlock_user_struct(target_rk, target_rk_addr, 1);
  202. return 0;
  203. }
  204. static abi_long
  205. h2t_procctl_reaper_pidinfo(struct procctl_reaper_pidinfo *host_pi,
  206. abi_ulong target_pi_addr)
  207. {
  208. struct target_procctl_reaper_pidinfo *target_pi;
  209. if (!lock_user_struct(VERIFY_WRITE, target_pi, target_pi_addr, 0)) {
  210. return -TARGET_EFAULT;
  211. }
  212. __put_user(host_pi->pi_pid, &target_pi->pi_pid);
  213. __put_user(host_pi->pi_subtree, &target_pi->pi_subtree);
  214. __put_user(host_pi->pi_flags, &target_pi->pi_flags);
  215. unlock_user_struct(target_pi, target_pi_addr, 1);
  216. return 0;
  217. }
  218. abi_long
  219. do_freebsd_procctl(void *cpu_env, int idtype, abi_ulong arg2, abi_ulong arg3,
  220. abi_ulong arg4, abi_ulong arg5, abi_ulong arg6)
  221. {
  222. abi_long error = 0, target_rp_pids;
  223. void *data;
  224. int host_cmd, flags;
  225. uint32_t u, target_rp_count;
  226. g_autofree union {
  227. struct procctl_reaper_status rs;
  228. struct procctl_reaper_pids rp;
  229. struct procctl_reaper_kill rk;
  230. } host;
  231. struct target_procctl_reaper_pids *target_rp;
  232. id_t id; /* 64-bit */
  233. int target_cmd;
  234. abi_ulong target_arg;
  235. #if TARGET_ABI_BITS == 32
  236. /* See if we need to align the register pairs. */
  237. if (regpairs_aligned(cpu_env)) {
  238. id = (id_t)target_arg64(arg3, arg4);
  239. target_cmd = (int)arg5;
  240. target_arg = arg6;
  241. } else {
  242. id = (id_t)target_arg64(arg2, arg3);
  243. target_cmd = (int)arg4;
  244. target_arg = arg5;
  245. }
  246. #else
  247. id = (id_t)arg2;
  248. target_cmd = (int)arg3;
  249. target_arg = arg4;
  250. #endif
  251. error = t2h_procctl_cmd(target_cmd, &host_cmd);
  252. if (error) {
  253. return error;
  254. }
  255. switch (host_cmd) {
  256. case PROC_SPROTECT:
  257. data = &flags;
  258. break;
  259. case PROC_REAP_ACQUIRE:
  260. case PROC_REAP_RELEASE:
  261. if (target_arg == 0) {
  262. data = NULL;
  263. } else {
  264. error = -TARGET_EINVAL;
  265. }
  266. break;
  267. case PROC_REAP_STATUS:
  268. data = &host.rs;
  269. break;
  270. case PROC_REAP_GETPIDS:
  271. if (!lock_user_struct(VERIFY_READ, target_rp, target_arg, 1)) {
  272. return -TARGET_EFAULT;
  273. }
  274. __get_user(target_rp_count, &target_rp->rp_count);
  275. __get_user(target_rp_pids, &target_rp->rp_pids);
  276. unlock_user_struct(target_rp, target_arg, 0);
  277. host.rp.rp_count = target_rp_count;
  278. host.rp.rp_pids = g_try_new(struct procctl_reaper_pidinfo,
  279. target_rp_count);
  280. if (host.rp.rp_pids == NULL) {
  281. error = -TARGET_ENOMEM;
  282. } else {
  283. data = &host.rp;
  284. }
  285. break;
  286. case PROC_REAP_KILL:
  287. error = t2h_reaper_kill(target_arg, &host.rk);
  288. break;
  289. }
  290. if (error) {
  291. return error;
  292. }
  293. error = get_errno(procctl(idtype, id, host_cmd, data));
  294. if (error) {
  295. return error;
  296. }
  297. switch (host_cmd) {
  298. case PROC_SPROTECT:
  299. if (put_user_s32(flags, target_arg)) {
  300. return -TARGET_EFAULT;
  301. }
  302. break;
  303. case PROC_REAP_STATUS:
  304. error = h2t_reaper_status(&host.rs, target_arg);
  305. break;
  306. case PROC_REAP_GETPIDS:
  307. /* copyout reaper pidinfo */
  308. for (u = 0; u < target_rp_count; u++) {
  309. error = h2t_procctl_reaper_pidinfo(&host.rp.rp_pids[u],
  310. target_rp_pids +
  311. (u * sizeof(struct target_procctl_reaper_pidinfo)));
  312. if (error) {
  313. break;
  314. }
  315. }
  316. break;
  317. case PROC_REAP_KILL:
  318. error = h2t_reaper_kill(&host.rk, target_arg);
  319. break;
  320. }
  321. return error;
  322. }