]>
Commit | Line | Data |
---|---|---|
88dae46d SB |
1 | /* |
2 | * qemu bsd user mode definition | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | |
16 | */ | |
84778508 BS |
17 | #ifndef QEMU_H |
18 | #define QEMU_H | |
19 | ||
84778508 | 20 | |
ab77bd84 | 21 | #include "qemu/osdep.h" |
84778508 | 22 | #include "cpu.h" |
e5e44263 | 23 | #include "qemu/units.h" |
f08b6170 | 24 | #include "exec/cpu_ldst.h" |
ab77bd84 | 25 | #include "exec/exec-all.h" |
84778508 BS |
26 | |
27 | #undef DEBUG_REMAP | |
84778508 | 28 | |
022c62cb | 29 | #include "exec/user/abitypes.h" |
84778508 | 30 | |
4b599848 WL |
31 | extern char **environ; |
32 | ||
84778508 BS |
33 | enum BSDType { |
34 | target_freebsd, | |
35 | target_netbsd, | |
36 | target_openbsd, | |
37 | }; | |
78cfb07f | 38 | extern enum BSDType bsd_type; |
84778508 | 39 | |
ab77bd84 WL |
40 | #include "exec/user/thunk.h" |
41 | #include "target_arch.h" | |
84778508 | 42 | #include "syscall_defs.h" |
0c6940d0 | 43 | #include "target_syscall.h" |
82792244 | 44 | #include "target_os_vmparam.h" |
022c62cb | 45 | #include "exec/gdbstub.h" |
84778508 | 46 | |
c2bdd9a1 WL |
47 | /* |
48 | * This struct is used to hold certain information about the image. Basically, | |
49 | * it replicates in user space what would be certain task_struct fields in the | |
50 | * kernel | |
84778508 BS |
51 | */ |
52 | struct image_info { | |
53 | abi_ulong load_addr; | |
54 | abi_ulong start_code; | |
55 | abi_ulong end_code; | |
56 | abi_ulong start_data; | |
57 | abi_ulong end_data; | |
58 | abi_ulong start_brk; | |
59 | abi_ulong brk; | |
60 | abi_ulong start_mmap; | |
61 | abi_ulong mmap; | |
62 | abi_ulong rss; | |
63 | abi_ulong start_stack; | |
64 | abi_ulong entry; | |
65 | abi_ulong code_offset; | |
66 | abi_ulong data_offset; | |
84778508 BS |
67 | }; |
68 | ||
69 | #define MAX_SIGQUEUE_SIZE 1024 | |
70 | ||
71 | struct sigqueue { | |
72 | struct sigqueue *next; | |
84778508 BS |
73 | }; |
74 | ||
75 | struct emulated_sigtable { | |
76 | int pending; /* true if signal is pending */ | |
77 | struct sigqueue *first; | |
c2bdd9a1 WL |
78 | /* in order to always have memory for the first signal, we put it here */ |
79 | struct sigqueue info; | |
84778508 BS |
80 | }; |
81 | ||
c2bdd9a1 WL |
82 | /* |
83 | * NOTE: we force a big alignment so that the stack stored after is aligned too | |
84 | */ | |
84778508 | 85 | typedef struct TaskState { |
bd88c780 AB |
86 | pid_t ts_tid; /* tid (or pid) of this task */ |
87 | ||
84778508 | 88 | struct TaskState *next; |
031fe7af | 89 | struct bsd_binprm *bprm; |
84778508 BS |
90 | int used; /* non zero if used */ |
91 | struct image_info *info; | |
92 | ||
93 | struct emulated_sigtable sigtab[TARGET_NSIG]; | |
94 | struct sigqueue sigqueue_table[MAX_SIGQUEUE_SIZE]; /* siginfo queue */ | |
95 | struct sigqueue *first_free; /* first free siginfo queue entry */ | |
96 | int signal_pending; /* non zero if a signal may be pending */ | |
97 | ||
f7795e40 | 98 | uint8_t stack[]; |
84778508 BS |
99 | } __attribute__((aligned(16))) TaskState; |
100 | ||
101 | void init_task_state(TaskState *ts); | |
102 | extern const char *qemu_uname_release; | |
2fa5d9ba | 103 | extern unsigned long mmap_min_addr; |
84778508 | 104 | |
84778508 | 105 | /* |
e5e44263 WL |
106 | * TARGET_ARG_MAX defines the number of bytes allocated for arguments |
107 | * and envelope for the new program. 256k should suffice for a reasonable | |
108 | * maxiumum env+arg in 32-bit environments, bump it up to 512k for !ILP32 | |
109 | * platforms. | |
84778508 | 110 | */ |
e5e44263 WL |
111 | #if TARGET_ABI_BITS > 32 |
112 | #define TARGET_ARG_MAX (512 * KiB) | |
113 | #else | |
114 | #define TARGET_ARG_MAX (256 * KiB) | |
115 | #endif | |
116 | #define MAX_ARG_PAGES (TARGET_ARG_MAX / TARGET_PAGE_SIZE) | |
84778508 BS |
117 | |
118 | /* | |
119 | * This structure is used to hold the arguments that are | |
120 | * used when loading binaries. | |
121 | */ | |
afcbcff8 | 122 | struct bsd_binprm { |
84778508 BS |
123 | char buf[128]; |
124 | void *page[MAX_ARG_PAGES]; | |
125 | abi_ulong p; | |
126 | int fd; | |
127 | int e_uid, e_gid; | |
128 | int argc, envc; | |
129 | char **argv; | |
130 | char **envp; | |
1b50ff64 WL |
131 | char *filename; /* (Given) Name of binary */ |
132 | char *fullpath; /* Full path of binary */ | |
84778508 BS |
133 | }; |
134 | ||
135 | void do_init_thread(struct target_pt_regs *regs, struct image_info *infop); | |
136 | abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp, | |
ffa03665 | 137 | abi_ulong stringp); |
036a013f | 138 | int loader_exec(const char *filename, char **argv, char **envp, |
d37853f9 WL |
139 | struct target_pt_regs *regs, struct image_info *infop, |
140 | struct bsd_binprm *bprm); | |
84778508 | 141 | |
afcbcff8 | 142 | int load_elf_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs, |
036a013f | 143 | struct image_info *info); |
afcbcff8 | 144 | int load_flt_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs, |
036a013f | 145 | struct image_info *info); |
84778508 BS |
146 | |
147 | abi_long memcpy_to_target(abi_ulong dest, const void *src, | |
148 | unsigned long len); | |
149 | void target_set_brk(abi_ulong new_brk); | |
150 | abi_long do_brk(abi_ulong new_brk); | |
151 | void syscall_init(void); | |
152 | abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1, | |
153 | abi_long arg2, abi_long arg3, abi_long arg4, | |
78cfb07f JL |
154 | abi_long arg5, abi_long arg6, abi_long arg7, |
155 | abi_long arg8); | |
84778508 BS |
156 | abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1, |
157 | abi_long arg2, abi_long arg3, abi_long arg4, | |
158 | abi_long arg5, abi_long arg6); | |
159 | abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1, | |
160 | abi_long arg2, abi_long arg3, abi_long arg4, | |
161 | abi_long arg5, abi_long arg6); | |
e5924d89 | 162 | void gemu_log(const char *fmt, ...) GCC_FMT_ATTR(1, 2); |
d42df502 | 163 | extern __thread CPUState *thread_cpu; |
9349b4f9 | 164 | void cpu_loop(CPUArchState *env); |
84778508 BS |
165 | char *target_strerror(int err); |
166 | int get_osversion(void); | |
167 | void fork_start(void); | |
168 | void fork_end(int child); | |
169 | ||
1de7afc9 | 170 | #include "qemu/log.h" |
84778508 BS |
171 | |
172 | /* strace.c */ | |
88dae46d SB |
173 | struct syscallname { |
174 | int nr; | |
175 | const char *name; | |
176 | const char *format; | |
177 | void (*call)(const struct syscallname *, | |
178 | abi_long, abi_long, abi_long, | |
179 | abi_long, abi_long, abi_long); | |
180 | void (*result)(const struct syscallname *, abi_long); | |
181 | }; | |
182 | ||
84778508 BS |
183 | void |
184 | print_freebsd_syscall(int num, | |
185 | abi_long arg1, abi_long arg2, abi_long arg3, | |
186 | abi_long arg4, abi_long arg5, abi_long arg6); | |
187 | void print_freebsd_syscall_ret(int num, abi_long ret); | |
188 | void | |
189 | print_netbsd_syscall(int num, | |
190 | abi_long arg1, abi_long arg2, abi_long arg3, | |
191 | abi_long arg4, abi_long arg5, abi_long arg6); | |
192 | void print_netbsd_syscall_ret(int num, abi_long ret); | |
193 | void | |
194 | print_openbsd_syscall(int num, | |
195 | abi_long arg1, abi_long arg2, abi_long arg3, | |
196 | abi_long arg4, abi_long arg5, abi_long arg6); | |
197 | void print_openbsd_syscall_ret(int num, abi_long ret); | |
198 | extern int do_strace; | |
199 | ||
200 | /* signal.c */ | |
9349b4f9 | 201 | void process_pending_signals(CPUArchState *cpu_env); |
84778508 | 202 | void signal_init(void); |
9349b4f9 AF |
203 | long do_sigreturn(CPUArchState *env); |
204 | long do_rt_sigreturn(CPUArchState *env); | |
84778508 BS |
205 | abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp); |
206 | ||
207 | /* mmap.c */ | |
208 | int target_mprotect(abi_ulong start, abi_ulong len, int prot); | |
209 | abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, | |
210 | int flags, int fd, abi_ulong offset); | |
211 | int target_munmap(abi_ulong start, abi_ulong len); | |
212 | abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, | |
213 | abi_ulong new_size, unsigned long flags, | |
214 | abi_ulong new_addr); | |
215 | int target_msync(abi_ulong start, abi_ulong len, int flags); | |
216 | extern unsigned long last_brk; | |
84778508 BS |
217 | void mmap_fork_start(void); |
218 | void mmap_fork_end(int child); | |
84778508 | 219 | |
28e738dc | 220 | /* main.c */ |
01a298a5 | 221 | extern char qemu_proc_pathname[]; |
28e738dc BS |
222 | extern unsigned long x86_stack_size; |
223 | ||
84778508 BS |
224 | /* user access */ |
225 | ||
1720751f RH |
226 | #define VERIFY_READ PAGE_READ |
227 | #define VERIFY_WRITE (PAGE_READ | PAGE_WRITE) | |
84778508 | 228 | |
1720751f | 229 | static inline bool access_ok(int type, abi_ulong addr, abi_ulong size) |
84778508 | 230 | { |
1720751f | 231 | return page_check_range((target_ulong)addr, size, type) == 0; |
84778508 BS |
232 | } |
233 | ||
c2bdd9a1 WL |
234 | /* |
235 | * NOTE __get_user and __put_user use host pointers and don't check access. | |
236 | * | |
237 | * These are usually used to access struct data members once the struct has been | |
238 | * locked - usually with lock_user_struct(). | |
84778508 BS |
239 | */ |
240 | #define __put_user(x, hptr)\ | |
241 | ({\ | |
242 | int size = sizeof(*hptr);\ | |
cefbade1 | 243 | switch (size) {\ |
84778508 BS |
244 | case 1:\ |
245 | *(uint8_t *)(hptr) = (uint8_t)(typeof(*hptr))(x);\ | |
246 | break;\ | |
247 | case 2:\ | |
248 | *(uint16_t *)(hptr) = tswap16((typeof(*hptr))(x));\ | |
249 | break;\ | |
250 | case 4:\ | |
251 | *(uint32_t *)(hptr) = tswap32((typeof(*hptr))(x));\ | |
252 | break;\ | |
253 | case 8:\ | |
254 | *(uint64_t *)(hptr) = tswap64((typeof(*hptr))(x));\ | |
255 | break;\ | |
256 | default:\ | |
257 | abort();\ | |
036a013f | 258 | } \ |
84778508 BS |
259 | 0;\ |
260 | }) | |
261 | ||
262 | #define __get_user(x, hptr) \ | |
263 | ({\ | |
264 | int size = sizeof(*hptr);\ | |
cefbade1 | 265 | switch (size) {\ |
84778508 BS |
266 | case 1:\ |
267 | x = (typeof(*hptr))*(uint8_t *)(hptr);\ | |
268 | break;\ | |
269 | case 2:\ | |
270 | x = (typeof(*hptr))tswap16(*(uint16_t *)(hptr));\ | |
271 | break;\ | |
272 | case 4:\ | |
273 | x = (typeof(*hptr))tswap32(*(uint32_t *)(hptr));\ | |
274 | break;\ | |
275 | case 8:\ | |
276 | x = (typeof(*hptr))tswap64(*(uint64_t *)(hptr));\ | |
277 | break;\ | |
278 | default:\ | |
84778508 BS |
279 | x = 0;\ |
280 | abort();\ | |
036a013f | 281 | } \ |
84778508 BS |
282 | 0;\ |
283 | }) | |
284 | ||
c2bdd9a1 WL |
285 | /* |
286 | * put_user()/get_user() take a guest address and check access | |
287 | * | |
288 | * These are usually used to access an atomic data type, such as an int, that | |
289 | * has been passed by address. These internally perform locking and unlocking | |
290 | * on the data type. | |
84778508 BS |
291 | */ |
292 | #define put_user(x, gaddr, target_type) \ | |
293 | ({ \ | |
294 | abi_ulong __gaddr = (gaddr); \ | |
295 | target_type *__hptr; \ | |
296 | abi_long __ret; \ | |
33066934 WL |
297 | __hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0); \ |
298 | if (__hptr) { \ | |
84778508 BS |
299 | __ret = __put_user((x), __hptr); \ |
300 | unlock_user(__hptr, __gaddr, sizeof(target_type)); \ | |
301 | } else \ | |
302 | __ret = -TARGET_EFAULT; \ | |
303 | __ret; \ | |
304 | }) | |
305 | ||
306 | #define get_user(x, gaddr, target_type) \ | |
307 | ({ \ | |
308 | abi_ulong __gaddr = (gaddr); \ | |
309 | target_type *__hptr; \ | |
310 | abi_long __ret; \ | |
33066934 WL |
311 | __hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1); \ |
312 | if (__hptr) { \ | |
84778508 BS |
313 | __ret = __get_user((x), __hptr); \ |
314 | unlock_user(__hptr, __gaddr, 0); \ | |
315 | } else { \ | |
84778508 BS |
316 | (x) = 0; \ |
317 | __ret = -TARGET_EFAULT; \ | |
318 | } \ | |
319 | __ret; \ | |
320 | }) | |
321 | ||
322 | #define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong) | |
323 | #define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long) | |
324 | #define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t) | |
325 | #define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t) | |
326 | #define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t) | |
327 | #define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t) | |
328 | #define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t) | |
329 | #define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t) | |
330 | #define put_user_u8(x, gaddr) put_user((x), (gaddr), uint8_t) | |
331 | #define put_user_s8(x, gaddr) put_user((x), (gaddr), int8_t) | |
332 | ||
333 | #define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong) | |
334 | #define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long) | |
335 | #define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t) | |
336 | #define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t) | |
337 | #define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t) | |
338 | #define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t) | |
339 | #define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t) | |
340 | #define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t) | |
341 | #define get_user_u8(x, gaddr) get_user((x), (gaddr), uint8_t) | |
342 | #define get_user_s8(x, gaddr) get_user((x), (gaddr), int8_t) | |
343 | ||
c2bdd9a1 WL |
344 | /* |
345 | * copy_from_user() and copy_to_user() are usually used to copy data | |
84778508 BS |
346 | * buffers between the target and host. These internally perform |
347 | * locking/unlocking of the memory. | |
348 | */ | |
349 | abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len); | |
350 | abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len); | |
351 | ||
c2bdd9a1 WL |
352 | /* |
353 | * Functions for accessing guest memory. The tget and tput functions | |
354 | * read/write single values, byteswapping as necessary. The lock_user function | |
355 | * gets a pointer to a contiguous area of guest memory, but does not perform | |
356 | * any byteswapping. lock_user may return either a pointer to the guest | |
357 | * memory, or a temporary buffer. | |
358 | */ | |
84778508 | 359 | |
c2bdd9a1 WL |
360 | /* |
361 | * Lock an area of guest memory into the host. If copy is true then the | |
362 | * host area will have the same contents as the guest. | |
363 | */ | |
364 | static inline void *lock_user(int type, abi_ulong guest_addr, long len, | |
365 | int copy) | |
84778508 | 366 | { |
cb0ea019 | 367 | if (!access_ok(type, guest_addr, len)) { |
84778508 | 368 | return NULL; |
cb0ea019 | 369 | } |
84778508 BS |
370 | #ifdef DEBUG_REMAP |
371 | { | |
372 | void *addr; | |
fd9a3048 | 373 | addr = g_malloc(len); |
cb0ea019 | 374 | if (copy) { |
3e8f1628 | 375 | memcpy(addr, g2h_untagged(guest_addr), len); |
cb0ea019 | 376 | } else { |
84778508 | 377 | memset(addr, 0, len); |
cb0ea019 | 378 | } |
84778508 BS |
379 | return addr; |
380 | } | |
381 | #else | |
3e8f1628 | 382 | return g2h_untagged(guest_addr); |
84778508 BS |
383 | #endif |
384 | } | |
385 | ||
c2bdd9a1 WL |
386 | /* |
387 | * Unlock an area of guest memory. The first LEN bytes must be flushed back to | |
388 | * guest memory. host_ptr = NULL is explicitly allowed and does nothing. | |
389 | */ | |
84778508 BS |
390 | static inline void unlock_user(void *host_ptr, abi_ulong guest_addr, |
391 | long len) | |
392 | { | |
393 | ||
394 | #ifdef DEBUG_REMAP | |
cb0ea019 | 395 | if (!host_ptr) { |
84778508 | 396 | return; |
cb0ea019 WL |
397 | } |
398 | if (host_ptr == g2h_untagged(guest_addr)) { | |
84778508 | 399 | return; |
cb0ea019 WL |
400 | } |
401 | if (len > 0) { | |
3e8f1628 | 402 | memcpy(g2h_untagged(guest_addr), host_ptr, len); |
cb0ea019 | 403 | } |
fd9a3048 | 404 | g_free(host_ptr); |
84778508 BS |
405 | #endif |
406 | } | |
407 | ||
c2bdd9a1 WL |
408 | /* |
409 | * Return the length of a string in target memory or -TARGET_EFAULT if access | |
410 | * error. | |
411 | */ | |
84778508 BS |
412 | abi_long target_strlen(abi_ulong gaddr); |
413 | ||
414 | /* Like lock_user but for null terminated strings. */ | |
415 | static inline void *lock_user_string(abi_ulong guest_addr) | |
416 | { | |
417 | abi_long len; | |
418 | len = target_strlen(guest_addr); | |
cb0ea019 | 419 | if (len < 0) { |
84778508 | 420 | return NULL; |
cb0ea019 | 421 | } |
84778508 BS |
422 | return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1); |
423 | } | |
424 | ||
41d1af4d | 425 | /* Helper macros for locking/unlocking a target struct. */ |
84778508 BS |
426 | #define lock_user_struct(type, host_ptr, guest_addr, copy) \ |
427 | (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy)) | |
428 | #define unlock_user_struct(host_ptr, guest_addr, copy) \ | |
429 | unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0) | |
430 | ||
84778508 | 431 | #include <pthread.h> |
84778508 BS |
432 | |
433 | #endif /* QEMU_H */ |