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