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