]> git.proxmox.com Git - mirror_qemu.git/blame - bsd-user/qemu.h
bsd-user: Add system independent stack, data and text limiting
[mirror_qemu.git] / bsd-user / qemu.h
CommitLineData
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
31extern char **environ;
32
84778508
BS
33enum BSDType {
34 target_freebsd,
35 target_netbsd,
36 target_openbsd,
37};
78cfb07f 38extern 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 */
52struct 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
71struct sigqueue {
72 struct sigqueue *next;
84778508
BS
73};
74
75struct 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 85typedef 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
101void init_task_state(TaskState *ts);
102extern const char *qemu_uname_release;
2fa5d9ba 103extern 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 122struct 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
135void do_init_thread(struct target_pt_regs *regs, struct image_info *infop);
136abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
ffa03665 137 abi_ulong stringp);
036a013f 138int 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 142int load_elf_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
036a013f 143 struct image_info *info);
afcbcff8 144int load_flt_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
036a013f 145 struct image_info *info);
84778508
BS
146
147abi_long memcpy_to_target(abi_ulong dest, const void *src,
148 unsigned long len);
149void target_set_brk(abi_ulong new_brk);
150abi_long do_brk(abi_ulong new_brk);
151void syscall_init(void);
152abi_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
156abi_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);
159abi_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 162void gemu_log(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
d42df502 163extern __thread CPUState *thread_cpu;
9349b4f9 164void cpu_loop(CPUArchState *env);
84778508
BS
165char *target_strerror(int err);
166int get_osversion(void);
167void fork_start(void);
168void fork_end(int child);
169
1de7afc9 170#include "qemu/log.h"
84778508
BS
171
172/* strace.c */
88dae46d
SB
173struct 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
183void
184print_freebsd_syscall(int num,
185 abi_long arg1, abi_long arg2, abi_long arg3,
186 abi_long arg4, abi_long arg5, abi_long arg6);
187void print_freebsd_syscall_ret(int num, abi_long ret);
188void
189print_netbsd_syscall(int num,
190 abi_long arg1, abi_long arg2, abi_long arg3,
191 abi_long arg4, abi_long arg5, abi_long arg6);
192void print_netbsd_syscall_ret(int num, abi_long ret);
193void
194print_openbsd_syscall(int num,
195 abi_long arg1, abi_long arg2, abi_long arg3,
196 abi_long arg4, abi_long arg5, abi_long arg6);
197void print_openbsd_syscall_ret(int num, abi_long ret);
198extern int do_strace;
199
200/* signal.c */
9349b4f9 201void process_pending_signals(CPUArchState *cpu_env);
84778508 202void signal_init(void);
9349b4f9
AF
203long do_sigreturn(CPUArchState *env);
204long do_rt_sigreturn(CPUArchState *env);
84778508
BS
205abi_long do_sigaltstack(abi_ulong uss_addr, abi_ulong uoss_addr, abi_ulong sp);
206
207/* mmap.c */
208int target_mprotect(abi_ulong start, abi_ulong len, int prot);
209abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
210 int flags, int fd, abi_ulong offset);
211int target_munmap(abi_ulong start, abi_ulong len);
212abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
213 abi_ulong new_size, unsigned long flags,
214 abi_ulong new_addr);
215int target_msync(abi_ulong start, abi_ulong len, int flags);
216extern unsigned long last_brk;
84778508
BS
217void mmap_fork_start(void);
218void mmap_fork_end(int child);
84778508 219
28e738dc 220/* main.c */
01a298a5 221extern char qemu_proc_pathname[];
312a0b1c
WL
222extern unsigned long target_maxtsiz;
223extern unsigned long target_dfldsiz;
224extern unsigned long target_maxdsiz;
225extern unsigned long target_dflssiz;
226extern unsigned long target_maxssiz;
227extern unsigned long target_sgrowsiz;
28e738dc 228
84778508
BS
229/* user access */
230
1720751f
RH
231#define VERIFY_READ PAGE_READ
232#define VERIFY_WRITE (PAGE_READ | PAGE_WRITE)
84778508 233
1720751f 234static inline bool access_ok(int type, abi_ulong addr, abi_ulong size)
84778508 235{
1720751f 236 return page_check_range((target_ulong)addr, size, type) == 0;
84778508
BS
237}
238
c2bdd9a1
WL
239/*
240 * NOTE __get_user and __put_user use host pointers and don't check access.
241 *
242 * These are usually used to access struct data members once the struct has been
243 * locked - usually with lock_user_struct().
84778508
BS
244 */
245#define __put_user(x, hptr)\
246({\
247 int size = sizeof(*hptr);\
cefbade1 248 switch (size) {\
84778508
BS
249 case 1:\
250 *(uint8_t *)(hptr) = (uint8_t)(typeof(*hptr))(x);\
251 break;\
252 case 2:\
253 *(uint16_t *)(hptr) = tswap16((typeof(*hptr))(x));\
254 break;\
255 case 4:\
256 *(uint32_t *)(hptr) = tswap32((typeof(*hptr))(x));\
257 break;\
258 case 8:\
259 *(uint64_t *)(hptr) = tswap64((typeof(*hptr))(x));\
260 break;\
261 default:\
262 abort();\
036a013f 263 } \
84778508
BS
264 0;\
265})
266
267#define __get_user(x, hptr) \
268({\
269 int size = sizeof(*hptr);\
cefbade1 270 switch (size) {\
84778508
BS
271 case 1:\
272 x = (typeof(*hptr))*(uint8_t *)(hptr);\
273 break;\
274 case 2:\
275 x = (typeof(*hptr))tswap16(*(uint16_t *)(hptr));\
276 break;\
277 case 4:\
278 x = (typeof(*hptr))tswap32(*(uint32_t *)(hptr));\
279 break;\
280 case 8:\
281 x = (typeof(*hptr))tswap64(*(uint64_t *)(hptr));\
282 break;\
283 default:\
84778508
BS
284 x = 0;\
285 abort();\
036a013f 286 } \
84778508
BS
287 0;\
288})
289
c2bdd9a1
WL
290/*
291 * put_user()/get_user() take a guest address and check access
292 *
293 * These are usually used to access an atomic data type, such as an int, that
294 * has been passed by address. These internally perform locking and unlocking
295 * on the data type.
84778508
BS
296 */
297#define put_user(x, gaddr, target_type) \
298({ \
299 abi_ulong __gaddr = (gaddr); \
300 target_type *__hptr; \
301 abi_long __ret; \
33066934
WL
302 __hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0); \
303 if (__hptr) { \
84778508
BS
304 __ret = __put_user((x), __hptr); \
305 unlock_user(__hptr, __gaddr, sizeof(target_type)); \
306 } else \
307 __ret = -TARGET_EFAULT; \
308 __ret; \
309})
310
311#define get_user(x, gaddr, target_type) \
312({ \
313 abi_ulong __gaddr = (gaddr); \
314 target_type *__hptr; \
315 abi_long __ret; \
33066934
WL
316 __hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1); \
317 if (__hptr) { \
84778508
BS
318 __ret = __get_user((x), __hptr); \
319 unlock_user(__hptr, __gaddr, 0); \
320 } else { \
84778508
BS
321 (x) = 0; \
322 __ret = -TARGET_EFAULT; \
323 } \
324 __ret; \
325})
326
327#define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong)
328#define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long)
329#define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t)
330#define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t)
331#define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t)
332#define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t)
333#define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t)
334#define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t)
335#define put_user_u8(x, gaddr) put_user((x), (gaddr), uint8_t)
336#define put_user_s8(x, gaddr) put_user((x), (gaddr), int8_t)
337
338#define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong)
339#define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long)
340#define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t)
341#define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t)
342#define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t)
343#define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t)
344#define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t)
345#define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t)
346#define get_user_u8(x, gaddr) get_user((x), (gaddr), uint8_t)
347#define get_user_s8(x, gaddr) get_user((x), (gaddr), int8_t)
348
c2bdd9a1
WL
349/*
350 * copy_from_user() and copy_to_user() are usually used to copy data
84778508
BS
351 * buffers between the target and host. These internally perform
352 * locking/unlocking of the memory.
353 */
354abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len);
355abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len);
356
c2bdd9a1
WL
357/*
358 * Functions for accessing guest memory. The tget and tput functions
359 * read/write single values, byteswapping as necessary. The lock_user function
360 * gets a pointer to a contiguous area of guest memory, but does not perform
361 * any byteswapping. lock_user may return either a pointer to the guest
362 * memory, or a temporary buffer.
363 */
84778508 364
c2bdd9a1
WL
365/*
366 * Lock an area of guest memory into the host. If copy is true then the
367 * host area will have the same contents as the guest.
368 */
369static inline void *lock_user(int type, abi_ulong guest_addr, long len,
370 int copy)
84778508 371{
cb0ea019 372 if (!access_ok(type, guest_addr, len)) {
84778508 373 return NULL;
cb0ea019 374 }
84778508
BS
375#ifdef DEBUG_REMAP
376 {
377 void *addr;
fd9a3048 378 addr = g_malloc(len);
cb0ea019 379 if (copy) {
3e8f1628 380 memcpy(addr, g2h_untagged(guest_addr), len);
cb0ea019 381 } else {
84778508 382 memset(addr, 0, len);
cb0ea019 383 }
84778508
BS
384 return addr;
385 }
386#else
3e8f1628 387 return g2h_untagged(guest_addr);
84778508
BS
388#endif
389}
390
c2bdd9a1
WL
391/*
392 * Unlock an area of guest memory. The first LEN bytes must be flushed back to
393 * guest memory. host_ptr = NULL is explicitly allowed and does nothing.
394 */
84778508
BS
395static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
396 long len)
397{
398
399#ifdef DEBUG_REMAP
cb0ea019 400 if (!host_ptr) {
84778508 401 return;
cb0ea019
WL
402 }
403 if (host_ptr == g2h_untagged(guest_addr)) {
84778508 404 return;
cb0ea019
WL
405 }
406 if (len > 0) {
3e8f1628 407 memcpy(g2h_untagged(guest_addr), host_ptr, len);
cb0ea019 408 }
fd9a3048 409 g_free(host_ptr);
84778508
BS
410#endif
411}
412
c2bdd9a1
WL
413/*
414 * Return the length of a string in target memory or -TARGET_EFAULT if access
415 * error.
416 */
84778508
BS
417abi_long target_strlen(abi_ulong gaddr);
418
419/* Like lock_user but for null terminated strings. */
420static inline void *lock_user_string(abi_ulong guest_addr)
421{
422 abi_long len;
423 len = target_strlen(guest_addr);
cb0ea019 424 if (len < 0) {
84778508 425 return NULL;
cb0ea019 426 }
84778508
BS
427 return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1);
428}
429
41d1af4d 430/* Helper macros for locking/unlocking a target struct. */
84778508
BS
431#define lock_user_struct(type, host_ptr, guest_addr, copy) \
432 (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
433#define unlock_user_struct(host_ptr, guest_addr, copy) \
434 unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
435
84778508 436#include <pthread.h>
84778508
BS
437
438#endif /* QEMU_H */