meson: move accelerator dependency checks together
[qemu/ar7.git] / bsd-user / bsd-proc.c
blobca3c1bf94f4a9a790c8af3358877d44f295e9946
1 /*
2 * BSD process related system call helpers
4 * Copyright (c) 2013-14 Stacey D. Son
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"
21 #include <sys/param.h>
22 #include <sys/types.h>
23 #include <sys/cpuset.h>
24 #include <sys/resource.h>
25 #include <sys/wait.h>
27 #include "qemu.h"
28 #include "qemu-bsd.h"
29 #include "signal-common.h"
31 #include "bsd-proc.h"
34 * resource/rusage conversion
36 int target_to_host_resource(int code)
38 return code;
41 rlim_t target_to_host_rlim(abi_llong target_rlim)
43 return tswap64(target_rlim);
46 abi_llong host_to_target_rlim(rlim_t rlim)
48 return tswap64(rlim);
51 void h2g_rusage(const struct rusage *rusage,
52 struct target_freebsd_rusage *target_rusage)
54 __put_user(rusage->ru_utime.tv_sec, &target_rusage->ru_utime.tv_sec);
55 __put_user(rusage->ru_utime.tv_usec, &target_rusage->ru_utime.tv_usec);
57 __put_user(rusage->ru_stime.tv_sec, &target_rusage->ru_stime.tv_sec);
58 __put_user(rusage->ru_stime.tv_usec, &target_rusage->ru_stime.tv_usec);
60 __put_user(rusage->ru_maxrss, &target_rusage->ru_maxrss);
61 __put_user(rusage->ru_idrss, &target_rusage->ru_idrss);
62 __put_user(rusage->ru_idrss, &target_rusage->ru_idrss);
63 __put_user(rusage->ru_isrss, &target_rusage->ru_isrss);
64 __put_user(rusage->ru_minflt, &target_rusage->ru_minflt);
65 __put_user(rusage->ru_majflt, &target_rusage->ru_majflt);
66 __put_user(rusage->ru_nswap, &target_rusage->ru_nswap);
67 __put_user(rusage->ru_inblock, &target_rusage->ru_inblock);
68 __put_user(rusage->ru_oublock, &target_rusage->ru_oublock);
69 __put_user(rusage->ru_msgsnd, &target_rusage->ru_msgsnd);
70 __put_user(rusage->ru_msgrcv, &target_rusage->ru_msgrcv);
71 __put_user(rusage->ru_nsignals, &target_rusage->ru_nsignals);
72 __put_user(rusage->ru_nvcsw, &target_rusage->ru_nvcsw);
73 __put_user(rusage->ru_nivcsw, &target_rusage->ru_nivcsw);
76 abi_long host_to_target_rusage(abi_ulong target_addr,
77 const struct rusage *rusage)
79 struct target_freebsd_rusage *target_rusage;
81 if (!lock_user_struct(VERIFY_WRITE, target_rusage, target_addr, 0)) {
82 return -TARGET_EFAULT;
84 h2g_rusage(rusage, target_rusage);
85 unlock_user_struct(target_rusage, target_addr, 1);
87 return 0;
90 abi_long host_to_target_wrusage(abi_ulong target_addr,
91 const struct __wrusage *wrusage)
93 struct target_freebsd__wrusage *target_wrusage;
95 if (!lock_user_struct(VERIFY_WRITE, target_wrusage, target_addr, 0)) {
96 return -TARGET_EFAULT;
98 h2g_rusage(&wrusage->wru_self, &target_wrusage->wru_self);
99 h2g_rusage(&wrusage->wru_children, &target_wrusage->wru_children);
100 unlock_user_struct(target_wrusage, target_addr, 1);
102 return 0;
106 * wait status conversion.
108 * Map host to target signal numbers for the wait family of syscalls.
109 * Assume all other status bits are the same.
111 int host_to_target_waitstatus(int status)
113 if (WIFSIGNALED(status)) {
114 return host_to_target_signal(WTERMSIG(status)) | (status & ~0x7f);
116 if (WIFSTOPPED(status)) {
117 return (host_to_target_signal(WSTOPSIG(status)) << 8) | (status & 0xff);
119 return status;
122 int bsd_get_ncpu(void)
124 int ncpu = -1;
125 cpuset_t mask;
127 CPU_ZERO(&mask);
129 if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(mask),
130 &mask) == 0) {
131 ncpu = CPU_COUNT(&mask);
134 if (ncpu == -1) {
135 ncpu = sysconf(_SC_NPROCESSORS_ONLN);
138 if (ncpu == -1) {
139 gemu_log("XXX Missing bsd_get_ncpu() implementation\n");
140 ncpu = 1;
143 return ncpu;