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