hw/sd/sdcard: Add spi_cmd_SEND_CSD/CID handlers (CMD9 & CMD10)
[qemu/armbru.git] / bsd-user / qemu.h
blob9d2fc7148ebfe3ddb46d20863b4008594dd7112a
1 /*
2 * qemu bsd user mode definition
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.
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.
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/>.
17 #ifndef QEMU_H
18 #define QEMU_H
20 #include "cpu.h"
21 #include "qemu/units.h"
22 #include "exec/cpu_ldst.h"
23 #include "exec/exec-all.h"
25 #include "user/abitypes.h"
27 extern char **environ;
29 #include "user/thunk.h"
30 #include "target_arch.h"
31 #include "syscall_defs.h"
32 #include "target_syscall.h"
33 #include "target_os_vmparam.h"
34 #include "target_os_signal.h"
35 #include "target.h"
36 #include "exec/gdbstub.h"
37 #include "exec/page-protection.h"
38 #include "qemu/clang-tsa.h"
39 #include "accel/tcg/vcpu-state.h"
41 #include "qemu-os.h"
43 * TODO: Remove these and rely only on qemu_real_host_page_size().
45 extern uintptr_t qemu_host_page_size;
46 extern intptr_t qemu_host_page_mask;
47 #define HOST_PAGE_ALIGN(addr) ROUND_UP((addr), qemu_host_page_size)
50 * This struct is used to hold certain information about the image. Basically,
51 * it replicates in user space what would be certain task_struct fields in the
52 * kernel
54 struct image_info {
55 abi_ulong load_bias;
56 abi_ulong load_addr;
57 abi_ulong start_code;
58 abi_ulong end_code;
59 abi_ulong start_data;
60 abi_ulong end_data;
61 abi_ulong brk;
62 abi_ulong rss;
63 abi_ulong start_stack;
64 abi_ulong entry;
65 abi_ulong code_offset;
66 abi_ulong data_offset;
67 abi_ulong arg_start;
68 abi_ulong arg_end;
69 uint32_t elf_flags;
72 struct emulated_sigtable {
73 int pending; /* true if signal is pending */
74 target_siginfo_t info;
78 * NOTE: we force a big alignment so that the stack stored after is aligned too
80 struct TaskState {
81 pid_t ts_tid; /* tid (or pid) of this task */
83 struct TaskState *next;
84 struct bsd_binprm *bprm;
85 struct image_info *info;
87 struct emulated_sigtable sync_signal;
89 * TODO: Since we block all signals while returning to the main CPU
90 * loop, this needn't be an array
92 struct emulated_sigtable sigtab[TARGET_NSIG];
94 * Nonzero if process_pending_signals() needs to do something (either
95 * handle a pending signal or unblock signals).
96 * This flag is written from a signal handler so should be accessed via
97 * the qatomic_read() and qatomic_set() functions. (It is not accessed
98 * from multiple threads.)
100 int signal_pending;
101 /* True if we're leaving a sigsuspend and sigsuspend_mask is valid. */
102 bool in_sigsuspend;
104 * This thread's signal mask, as requested by the guest program.
105 * The actual signal mask of this thread may differ:
106 * + we don't let SIGSEGV and SIGBUS be blocked while running guest code
107 * + sometimes we block all signals to avoid races
109 sigset_t signal_mask;
111 * The signal mask imposed by a guest sigsuspend syscall, if we are
112 * currently in the middle of such a syscall
114 sigset_t sigsuspend_mask;
116 /* This thread's sigaltstack, if it has one */
117 struct target_sigaltstack sigaltstack_used;
118 } __attribute__((aligned(16)));
120 void stop_all_tasks(void);
121 extern const char *interp_prefix;
122 extern const char *qemu_uname_release;
125 * TARGET_ARG_MAX defines the number of bytes allocated for arguments
126 * and envelope for the new program. 256k should suffice for a reasonable
127 * maximum env+arg in 32-bit environments, bump it up to 512k for !ILP32
128 * platforms.
130 #if TARGET_ABI_BITS > 32
131 #define TARGET_ARG_MAX (512 * KiB)
132 #else
133 #define TARGET_ARG_MAX (256 * KiB)
134 #endif
135 #define MAX_ARG_PAGES (TARGET_ARG_MAX / TARGET_PAGE_SIZE)
138 * This structure is used to hold the arguments that are
139 * used when loading binaries.
141 struct bsd_binprm {
142 char buf[128];
143 void *page[MAX_ARG_PAGES];
144 abi_ulong p;
145 abi_ulong stringp;
146 int fd;
147 int e_uid, e_gid;
148 int argc, envc;
149 char **argv;
150 char **envp;
151 char *filename; /* (Given) Name of binary */
152 char *fullpath; /* Full path of binary */
153 int (*core_dump)(int, CPUArchState *);
156 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop);
157 abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
158 abi_ulong stringp);
159 int loader_exec(const char *filename, char **argv, char **envp,
160 struct target_pt_regs *regs, struct image_info *infop,
161 struct bsd_binprm *bprm);
163 int load_elf_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
164 struct image_info *info);
165 int load_flt_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
166 struct image_info *info);
167 int is_target_elf_binary(int fd);
169 abi_long memcpy_to_target(abi_ulong dest, const void *src,
170 unsigned long len);
171 void target_set_brk(abi_ulong new_brk);
172 abi_long do_brk(abi_ulong new_brk);
173 void syscall_init(void);
174 abi_long do_freebsd_syscall(void *cpu_env, int num, abi_long arg1,
175 abi_long arg2, abi_long arg3, abi_long arg4,
176 abi_long arg5, abi_long arg6, abi_long arg7,
177 abi_long arg8);
178 abi_long do_netbsd_syscall(void *cpu_env, int num, abi_long arg1,
179 abi_long arg2, abi_long arg3, abi_long arg4,
180 abi_long arg5, abi_long arg6);
181 abi_long do_openbsd_syscall(void *cpu_env, int num, abi_long arg1,
182 abi_long arg2, abi_long arg3, abi_long arg4,
183 abi_long arg5, abi_long arg6);
184 void gemu_log(const char *fmt, ...) G_GNUC_PRINTF(1, 2);
185 extern __thread CPUState *thread_cpu;
186 void cpu_loop(CPUArchState *env);
187 char *target_strerror(int err);
188 int get_osversion(void);
189 void fork_start(void);
190 void fork_end(pid_t pid);
192 #include "qemu/log.h"
194 /* strace.c */
195 struct syscallname {
196 int nr;
197 const char *name;
198 const char *format;
199 void (*call)(const struct syscallname *,
200 abi_long, abi_long, abi_long,
201 abi_long, abi_long, abi_long);
202 void (*result)(const struct syscallname *, abi_long);
205 void
206 print_freebsd_syscall(int num,
207 abi_long arg1, abi_long arg2, abi_long arg3,
208 abi_long arg4, abi_long arg5, abi_long arg6);
209 void print_freebsd_syscall_ret(int num, abi_long ret);
210 void
211 print_netbsd_syscall(int num,
212 abi_long arg1, abi_long arg2, abi_long arg3,
213 abi_long arg4, abi_long arg5, abi_long arg6);
214 void print_netbsd_syscall_ret(int num, abi_long ret);
215 void
216 print_openbsd_syscall(int num,
217 abi_long arg1, abi_long arg2, abi_long arg3,
218 abi_long arg4, abi_long arg5, abi_long arg6);
219 void print_openbsd_syscall_ret(int num, abi_long ret);
221 * print_taken_signal:
222 * @target_signum: target signal being taken
223 * @tinfo: target_siginfo_t which will be passed to the guest for the signal
225 * Print strace output indicating that this signal is being taken by the guest,
226 * in a format similar to:
227 * --- SIGSEGV {si_signo=SIGSEGV, si_code=SI_KERNEL, si_addr=0} ---
229 void print_taken_signal(int target_signum, const target_siginfo_t *tinfo);
230 extern int do_strace;
232 /* mmap.c */
233 int target_mprotect(abi_ulong start, abi_ulong len, int prot);
234 abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
235 int flags, int fd, off_t offset);
236 int target_munmap(abi_ulong start, abi_ulong len);
237 abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
238 abi_ulong new_size, unsigned long flags,
239 abi_ulong new_addr);
240 int target_msync(abi_ulong start, abi_ulong len, int flags);
241 extern abi_ulong mmap_next_start;
242 abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size);
243 void mmap_reserve(abi_ulong start, abi_ulong size);
244 void TSA_NO_TSA mmap_fork_start(void);
245 void TSA_NO_TSA mmap_fork_end(int child);
247 /* main.c */
248 extern char qemu_proc_pathname[];
249 extern unsigned long target_maxtsiz;
250 extern unsigned long target_dfldsiz;
251 extern unsigned long target_maxdsiz;
252 extern unsigned long target_dflssiz;
253 extern unsigned long target_maxssiz;
254 extern unsigned long target_sgrowsiz;
256 /* os-syscall.c */
257 abi_long get_errno(abi_long ret);
258 bool is_error(abi_long ret);
259 int host_to_target_errno(int err);
261 /* os-proc.c */
262 abi_long freebsd_exec_common(abi_ulong path_or_fd, abi_ulong guest_argp,
263 abi_ulong guest_envp, int do_fexec);
264 abi_long do_freebsd_procctl(void *cpu_env, int idtype, abi_ulong arg2,
265 abi_ulong arg3, abi_ulong arg4, abi_ulong arg5, abi_ulong arg6);
267 /* os-sys.c */
268 abi_long do_freebsd_sysctl(CPUArchState *env, abi_ulong namep, int32_t namelen,
269 abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp, abi_ulong newlen);
270 abi_long do_freebsd_sysctlbyname(CPUArchState *env, abi_ulong namep,
271 int32_t namelen, abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp,
272 abi_ulong newlen);
273 abi_long do_freebsd_sysarch(void *cpu_env, abi_long arg1, abi_long arg2);
275 /* user access */
277 #define VERIFY_READ PAGE_READ
278 #define VERIFY_WRITE (PAGE_READ | PAGE_WRITE)
280 static inline bool access_ok(int type, abi_ulong addr, abi_ulong size)
282 return page_check_range((target_ulong)addr, size, type);
286 * NOTE __get_user and __put_user use host pointers and don't check access.
288 * These are usually used to access struct data members once the struct has been
289 * locked - usually with lock_user_struct().
293 * Tricky points:
294 * - Use __builtin_choose_expr to avoid type promotion from ?:,
295 * - Invalid sizes result in a compile time error stemming from
296 * the fact that abort has no parameters.
297 * - It's easier to use the endian-specific unaligned load/store
298 * functions than host-endian unaligned load/store plus tswapN.
299 * - The pragmas are necessary only to silence a clang false-positive
300 * warning: see https://bugs.llvm.org/show_bug.cgi?id=39113 .
301 * - gcc has bugs in its _Pragma() support in some versions, eg
302 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83256 -- so we only
303 * include the warning-suppression pragmas for clang
305 #if defined(__clang__) && __has_warning("-Waddress-of-packed-member")
306 #define PRAGMA_DISABLE_PACKED_WARNING \
307 _Pragma("GCC diagnostic push"); \
308 _Pragma("GCC diagnostic ignored \"-Waddress-of-packed-member\"")
310 #define PRAGMA_REENABLE_PACKED_WARNING \
311 _Pragma("GCC diagnostic pop")
313 #else
314 #define PRAGMA_DISABLE_PACKED_WARNING
315 #define PRAGMA_REENABLE_PACKED_WARNING
316 #endif
318 #define __put_user_e(x, hptr, e) \
319 do { \
320 PRAGMA_DISABLE_PACKED_WARNING; \
321 (__builtin_choose_expr(sizeof(*(hptr)) == 1, stb_p, \
322 __builtin_choose_expr(sizeof(*(hptr)) == 2, stw_##e##_p, \
323 __builtin_choose_expr(sizeof(*(hptr)) == 4, stl_##e##_p, \
324 __builtin_choose_expr(sizeof(*(hptr)) == 8, stq_##e##_p, abort)))) \
325 ((hptr), (x)), (void)0); \
326 PRAGMA_REENABLE_PACKED_WARNING; \
327 } while (0)
329 #define __get_user_e(x, hptr, e) \
330 do { \
331 PRAGMA_DISABLE_PACKED_WARNING; \
332 ((x) = (typeof(*hptr))( \
333 __builtin_choose_expr(sizeof(*(hptr)) == 1, ldub_p, \
334 __builtin_choose_expr(sizeof(*(hptr)) == 2, lduw_##e##_p, \
335 __builtin_choose_expr(sizeof(*(hptr)) == 4, ldl_##e##_p, \
336 __builtin_choose_expr(sizeof(*(hptr)) == 8, ldq_##e##_p, abort)))) \
337 (hptr)), (void)0); \
338 PRAGMA_REENABLE_PACKED_WARNING; \
339 } while (0)
342 #if TARGET_BIG_ENDIAN
343 # define __put_user(x, hptr) __put_user_e(x, hptr, be)
344 # define __get_user(x, hptr) __get_user_e(x, hptr, be)
345 #else
346 # define __put_user(x, hptr) __put_user_e(x, hptr, le)
347 # define __get_user(x, hptr) __get_user_e(x, hptr, le)
348 #endif
351 * put_user()/get_user() take a guest address and check access
353 * These are usually used to access an atomic data type, such as an int, that
354 * has been passed by address. These internally perform locking and unlocking
355 * on the data type.
357 #define put_user(x, gaddr, target_type) \
358 ({ \
359 abi_ulong __gaddr = (gaddr); \
360 target_type *__hptr; \
361 abi_long __ret = 0; \
362 __hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0); \
363 if (__hptr) { \
364 __put_user((x), __hptr); \
365 unlock_user(__hptr, __gaddr, sizeof(target_type)); \
366 } else \
367 __ret = -TARGET_EFAULT; \
368 __ret; \
371 #define get_user(x, gaddr, target_type) \
372 ({ \
373 abi_ulong __gaddr = (gaddr); \
374 target_type *__hptr; \
375 abi_long __ret = 0; \
376 __hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1); \
377 if (__hptr) { \
378 __get_user((x), __hptr); \
379 unlock_user(__hptr, __gaddr, 0); \
380 } else { \
381 (x) = 0; \
382 __ret = -TARGET_EFAULT; \
384 __ret; \
387 #define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong)
388 #define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long)
389 #define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t)
390 #define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t)
391 #define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t)
392 #define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t)
393 #define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t)
394 #define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t)
395 #define put_user_u8(x, gaddr) put_user((x), (gaddr), uint8_t)
396 #define put_user_s8(x, gaddr) put_user((x), (gaddr), int8_t)
398 #define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong)
399 #define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long)
400 #define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t)
401 #define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t)
402 #define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t)
403 #define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t)
404 #define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t)
405 #define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t)
406 #define get_user_u8(x, gaddr) get_user((x), (gaddr), uint8_t)
407 #define get_user_s8(x, gaddr) get_user((x), (gaddr), int8_t)
410 * copy_from_user() and copy_to_user() are usually used to copy data
411 * buffers between the target and host. These internally perform
412 * locking/unlocking of the memory.
414 abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len);
415 abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len);
418 * Functions for accessing guest memory. The tget and tput functions
419 * read/write single values, byteswapping as necessary. The lock_user function
420 * gets a pointer to a contiguous area of guest memory, but does not perform
421 * any byteswapping. lock_user may return either a pointer to the guest
422 * memory, or a temporary buffer.
426 * Lock an area of guest memory into the host. If copy is true then the
427 * host area will have the same contents as the guest.
429 static inline void *lock_user(int type, abi_ulong guest_addr, long len,
430 int copy)
432 if (!access_ok(type, guest_addr, len)) {
433 return NULL;
435 #ifdef CONFIG_DEBUG_REMAP
437 void *addr;
438 addr = g_malloc(len);
439 if (copy) {
440 memcpy(addr, g2h_untagged(guest_addr), len);
441 } else {
442 memset(addr, 0, len);
444 return addr;
446 #else
447 return g2h_untagged(guest_addr);
448 #endif
452 * Unlock an area of guest memory. The first LEN bytes must be flushed back to
453 * guest memory. host_ptr = NULL is explicitly allowed and does nothing.
455 static inline void unlock_user(void *host_ptr, abi_ulong guest_addr,
456 long len)
459 #ifdef CONFIG_DEBUG_REMAP
460 if (!host_ptr) {
461 return;
463 if (host_ptr == g2h_untagged(guest_addr)) {
464 return;
466 if (len > 0) {
467 memcpy(g2h_untagged(guest_addr), host_ptr, len);
469 g_free(host_ptr);
470 #endif
474 * Return the length of a string in target memory or -TARGET_EFAULT if access
475 * error.
477 abi_long target_strlen(abi_ulong gaddr);
479 /* Like lock_user but for null terminated strings. */
480 static inline void *lock_user_string(abi_ulong guest_addr)
482 abi_long len;
483 len = target_strlen(guest_addr);
484 if (len < 0) {
485 return NULL;
487 return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1);
490 /* Helper macros for locking/unlocking a target struct. */
491 #define lock_user_struct(type, host_ptr, guest_addr, copy) \
492 (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy))
493 #define unlock_user_struct(host_ptr, guest_addr, copy) \
494 unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
496 static inline uint64_t target_arg64(uint32_t word0, uint32_t word1)
498 #if TARGET_ABI_BITS == 32
499 #if TARGET_BIG_ENDIAN
500 return ((uint64_t)word0 << 32) | word1;
501 #else
502 return ((uint64_t)word1 << 32) | word0;
503 #endif
504 #else /* TARGET_ABI_BITS != 32 */
505 return word0;
506 #endif /* TARGET_ABI_BITS != 32 */
509 #include <pthread.h>
511 #include "user/safe-syscall.h"
513 #endif /* QEMU_H */