Sean Bruno | 88dae46 | 2014-06-08 09:57:23 -0700 | [diff] [blame] | 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 | */ |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 17 | #ifndef QEMU_H |
| 18 | #define QEMU_H |
| 19 | |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 20 | #include "cpu.h" |
Warner Losh | e5e4426 | 2021-08-04 00:19:23 -0600 | [diff] [blame] | 21 | #include "qemu/units.h" |
Paolo Bonzini | f08b617 | 2014-03-28 19:42:10 +0100 | [diff] [blame] | 22 | #include "exec/cpu_ldst.h" |
Warner Losh | ab77bd8 | 2021-08-03 23:27:25 -0600 | [diff] [blame] | 23 | #include "exec/exec-all.h" |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 24 | |
Philippe Mathieu-Daudé | 22879b6 | 2024-03-22 11:10:04 +0100 | [diff] [blame] | 25 | #include "user/abitypes.h" |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 26 | |
Warner Losh | 4b59984 | 2021-04-23 10:41:11 -0600 | [diff] [blame] | 27 | extern char **environ; |
| 28 | |
Philippe Mathieu-Daudé | 4e11165 | 2024-03-22 11:08:12 +0100 | [diff] [blame] | 29 | #include "user/thunk.h" |
Warner Losh | ab77bd8 | 2021-08-03 23:27:25 -0600 | [diff] [blame] | 30 | #include "target_arch.h" |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 31 | #include "syscall_defs.h" |
Lluís Vilanova | 0c6940d | 2016-02-01 19:38:47 +0100 | [diff] [blame] | 32 | #include "target_syscall.h" |
Warner Losh | 8279224 | 2021-08-04 00:39:39 -0600 | [diff] [blame] | 33 | #include "target_os_vmparam.h" |
Warner Losh | 790baac | 2021-08-05 13:41:52 -0600 | [diff] [blame] | 34 | #include "target_os_signal.h" |
Warner Losh | 647afdf | 2022-01-31 16:01:05 -0700 | [diff] [blame] | 35 | #include "target.h" |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 36 | #include "exec/gdbstub.h" |
Philippe Mathieu-Daudé | 74781c0 | 2023-12-06 20:27:32 +0100 | [diff] [blame] | 37 | #include "exec/page-protection.h" |
Emanuele Giuseppe Esposito | e022d9c | 2023-01-17 08:52:02 -0500 | [diff] [blame] | 38 | #include "qemu/clang-tsa.h" |
Philippe Mathieu-Daudé | 8019601 | 2024-04-25 11:11:49 +0200 | [diff] [blame] | 39 | #include "accel/tcg/vcpu-state.h" |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 40 | |
Stacey Son | 9b4a902 | 2023-08-13 10:41:25 +0200 | [diff] [blame] | 41 | #include "qemu-os.h" |
Warner Losh | c2bdd9a | 2021-04-23 19:50:53 -0600 | [diff] [blame] | 42 | /* |
Richard Henderson | 8c45039 | 2024-01-02 12:58:02 +1100 | [diff] [blame] | 43 | * TODO: Remove these and rely only on qemu_real_host_page_size(). |
| 44 | */ |
| 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) |
| 48 | |
| 49 | /* |
Warner Losh | c2bdd9a | 2021-04-23 19:50:53 -0600 | [diff] [blame] | 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 |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 53 | */ |
| 54 | struct image_info { |
Warner Losh | 0475f8f | 2021-08-06 14:20:16 -0600 | [diff] [blame] | 55 | abi_ulong load_bias; |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 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; |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 61 | abi_ulong brk; |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 62 | abi_ulong rss; |
| 63 | abi_ulong start_stack; |
| 64 | abi_ulong entry; |
| 65 | abi_ulong code_offset; |
| 66 | abi_ulong data_offset; |
Warner Losh | 0475f8f | 2021-08-06 14:20:16 -0600 | [diff] [blame] | 67 | abi_ulong arg_start; |
| 68 | abi_ulong arg_end; |
| 69 | uint32_t elf_flags; |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 72 | struct emulated_sigtable { |
| 73 | int pending; /* true if signal is pending */ |
Warner Losh | b46d4ad | 2022-01-16 16:14:18 -0700 | [diff] [blame] | 74 | target_siginfo_t info; |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
Warner Losh | c2bdd9a | 2021-04-23 19:50:53 -0600 | [diff] [blame] | 77 | /* |
| 78 | * NOTE: we force a big alignment so that the stack stored after is aligned too |
| 79 | */ |
Philippe Mathieu-Daudé | ef932e2 | 2024-01-10 12:41:55 +0100 | [diff] [blame] | 80 | struct TaskState { |
Alex Bennée | bd88c78 | 2017-07-12 11:52:15 +0100 | [diff] [blame] | 81 | pid_t ts_tid; /* tid (or pid) of this task */ |
| 82 | |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 83 | struct TaskState *next; |
Warner Losh | 031fe7a | 2021-08-03 19:05:40 -0600 | [diff] [blame] | 84 | struct bsd_binprm *bprm; |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 85 | struct image_info *info; |
| 86 | |
Warner Losh | 38be620 | 2022-01-08 21:15:11 -0700 | [diff] [blame] | 87 | struct emulated_sigtable sync_signal; |
| 88 | /* |
| 89 | * TODO: Since we block all signals while returning to the main CPU |
| 90 | * loop, this needn't be an array |
| 91 | */ |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 92 | struct emulated_sigtable sigtab[TARGET_NSIG]; |
Warner Losh | 4804722 | 2022-01-16 16:33:21 -0700 | [diff] [blame] | 93 | /* |
| 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.) |
| 99 | */ |
| 100 | int signal_pending; |
Warner Losh | 6c6d4b5 | 2022-01-08 21:46:07 -0700 | [diff] [blame] | 101 | /* True if we're leaving a sigsuspend and sigsuspend_mask is valid. */ |
| 102 | bool in_sigsuspend; |
Warner Losh | 149076a | 2022-01-08 16:57:31 -0700 | [diff] [blame] | 103 | /* |
| 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 |
| 108 | */ |
| 109 | sigset_t signal_mask; |
Warner Losh | 6c6d4b5 | 2022-01-08 21:46:07 -0700 | [diff] [blame] | 110 | /* |
| 111 | * The signal mask imposed by a guest sigsuspend syscall, if we are |
| 112 | * currently in the middle of such a syscall |
| 113 | */ |
| 114 | sigset_t sigsuspend_mask; |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 115 | |
Warner Losh | 46f4f76 | 2022-01-08 21:40:28 -0700 | [diff] [blame] | 116 | /* This thread's sigaltstack, if it has one */ |
| 117 | struct target_sigaltstack sigaltstack_used; |
Philippe Mathieu-Daudé | ef932e2 | 2024-01-10 12:41:55 +0100 | [diff] [blame] | 118 | } __attribute__((aligned(16))); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 119 | |
Warner Losh | 653ccec | 2021-09-19 01:11:43 -0600 | [diff] [blame] | 120 | void stop_all_tasks(void); |
Stacey Son | 8632729 | 2023-09-25 21:24:17 +0300 | [diff] [blame] | 121 | extern const char *interp_prefix; |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 122 | extern const char *qemu_uname_release; |
| 123 | |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 124 | /* |
Warner Losh | e5e4426 | 2021-08-04 00:19:23 -0600 | [diff] [blame] | 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 |
Michael Tokarev | 944399f | 2023-07-14 14:06:43 +0300 | [diff] [blame] | 127 | * maximum env+arg in 32-bit environments, bump it up to 512k for !ILP32 |
Warner Losh | e5e4426 | 2021-08-04 00:19:23 -0600 | [diff] [blame] | 128 | * platforms. |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 129 | */ |
Warner Losh | e5e4426 | 2021-08-04 00:19:23 -0600 | [diff] [blame] | 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) |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 136 | |
| 137 | /* |
| 138 | * This structure is used to hold the arguments that are |
| 139 | * used when loading binaries. |
| 140 | */ |
Warner Losh | afcbcff | 2021-04-29 10:04:28 -0600 | [diff] [blame] | 141 | struct bsd_binprm { |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 142 | char buf[128]; |
| 143 | void *page[MAX_ARG_PAGES]; |
| 144 | abi_ulong p; |
Warner Losh | 98b34d3 | 2021-08-04 17:13:24 -0600 | [diff] [blame] | 145 | abi_ulong stringp; |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 146 | int fd; |
| 147 | int e_uid, e_gid; |
| 148 | int argc, envc; |
| 149 | char **argv; |
| 150 | char **envp; |
Warner Losh | 1b50ff6 | 2021-04-29 19:34:34 -0600 | [diff] [blame] | 151 | char *filename; /* (Given) Name of binary */ |
| 152 | char *fullpath; /* Full path of binary */ |
Warner Losh | 0475f8f | 2021-08-06 14:20:16 -0600 | [diff] [blame] | 153 | int (*core_dump)(int, CPUArchState *); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
| 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, |
Warner Losh | ffa0366 | 2021-04-30 08:17:23 -0600 | [diff] [blame] | 158 | abi_ulong stringp); |
Warner Losh | 036a013 | 2021-04-23 19:45:20 -0600 | [diff] [blame] | 159 | int loader_exec(const char *filename, char **argv, char **envp, |
Warner Losh | d37853f | 2021-04-29 18:45:13 -0600 | [diff] [blame] | 160 | struct target_pt_regs *regs, struct image_info *infop, |
| 161 | struct bsd_binprm *bprm); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 162 | |
Warner Losh | afcbcff | 2021-04-29 10:04:28 -0600 | [diff] [blame] | 163 | int load_elf_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs, |
Warner Losh | 036a013 | 2021-04-23 19:45:20 -0600 | [diff] [blame] | 164 | struct image_info *info); |
Warner Losh | afcbcff | 2021-04-29 10:04:28 -0600 | [diff] [blame] | 165 | int load_flt_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs, |
Warner Losh | 036a013 | 2021-04-23 19:45:20 -0600 | [diff] [blame] | 166 | struct image_info *info); |
Warner Losh | 0475f8f | 2021-08-06 14:20:16 -0600 | [diff] [blame] | 167 | int is_target_elf_binary(int fd); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 168 | |
| 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, |
Juergen Lock | 78cfb07 | 2009-10-17 00:34:26 +0200 | [diff] [blame] | 176 | abi_long arg5, abi_long arg6, abi_long arg7, |
| 177 | abi_long arg8); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 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); |
Marc-André Lureau | 9edc631 | 2022-02-20 20:39:25 +0400 | [diff] [blame] | 184 | void gemu_log(const char *fmt, ...) G_GNUC_PRINTF(1, 2); |
Warner Losh | d42df50 | 2021-08-03 12:34:52 -0600 | [diff] [blame] | 185 | extern __thread CPUState *thread_cpu; |
Andreas Färber | 9349b4f | 2012-03-14 01:38:32 +0100 | [diff] [blame] | 186 | void cpu_loop(CPUArchState *env); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 187 | char *target_strerror(int err); |
| 188 | int get_osversion(void); |
| 189 | void fork_start(void); |
Ilya Leoshkevich | 4edc98f | 2024-03-05 12:09:42 +0000 | [diff] [blame] | 190 | void fork_end(pid_t pid); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 191 | |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 192 | #include "qemu/log.h" |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 193 | |
| 194 | /* strace.c */ |
Sean Bruno | 88dae46 | 2014-06-08 09:57:23 -0700 | [diff] [blame] | 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); |
| 203 | }; |
| 204 | |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 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); |
Warner Losh | fd5bec9 | 2022-01-08 20:04:18 -0700 | [diff] [blame] | 220 | /** |
| 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 |
| 224 | * |
| 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} --- |
| 228 | */ |
| 229 | void print_taken_signal(int target_signum, const target_siginfo_t *tinfo); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 230 | extern int do_strace; |
| 231 | |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 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, |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 235 | int flags, int fd, off_t offset); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 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); |
Warner Losh | be04f21 | 2021-08-05 18:15:47 -0600 | [diff] [blame] | 241 | extern abi_ulong mmap_next_start; |
| 242 | abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size); |
Stacey Son | 4e00b7d | 2023-09-25 21:27:08 +0300 | [diff] [blame] | 243 | void mmap_reserve(abi_ulong start, abi_ulong size); |
Emanuele Giuseppe Esposito | e022d9c | 2023-01-17 08:52:02 -0500 | [diff] [blame] | 244 | void TSA_NO_TSA mmap_fork_start(void); |
| 245 | void TSA_NO_TSA mmap_fork_end(int child); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 246 | |
Blue Swirl | 28e738d | 2009-08-01 10:29:42 +0000 | [diff] [blame] | 247 | /* main.c */ |
Warner Losh | 01a298a | 2021-08-03 13:39:31 -0600 | [diff] [blame] | 248 | extern char qemu_proc_pathname[]; |
Warner Losh | 312a0b1 | 2021-08-06 18:48:37 -0600 | [diff] [blame] | 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; |
Blue Swirl | 28e738d | 2009-08-01 10:29:42 +0000 | [diff] [blame] | 255 | |
Warner Losh | deeff83 | 2022-01-31 13:19:44 -0700 | [diff] [blame] | 256 | /* os-syscall.c */ |
Warner Losh | e5f674f | 2021-09-18 00:26:49 -0600 | [diff] [blame] | 257 | abi_long get_errno(abi_long ret); |
| 258 | bool is_error(abi_long ret); |
Warner Losh | deeff83 | 2022-01-31 13:19:44 -0700 | [diff] [blame] | 259 | int host_to_target_errno(int err); |
Warner Losh | e5f674f | 2021-09-18 00:26:49 -0600 | [diff] [blame] | 260 | |
Stacey Son | cc47390 | 2023-09-25 21:24:01 +0300 | [diff] [blame] | 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); |
| 266 | |
Warner Losh | da07e69 | 2021-09-20 13:41:38 -0600 | [diff] [blame] | 267 | /* os-sys.c */ |
Kyle Evans | 7adda6d | 2023-02-10 10:02:45 -0700 | [diff] [blame] | 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); |
Kyle Evans | 6da777e | 2023-02-10 11:16:25 -0700 | [diff] [blame] | 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); |
Warner Losh | da07e69 | 2021-09-20 13:41:38 -0600 | [diff] [blame] | 273 | abi_long do_freebsd_sysarch(void *cpu_env, abi_long arg1, abi_long arg2); |
| 274 | |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 275 | /* user access */ |
| 276 | |
Richard Henderson | 1720751 | 2021-02-12 10:48:39 -0800 | [diff] [blame] | 277 | #define VERIFY_READ PAGE_READ |
| 278 | #define VERIFY_WRITE (PAGE_READ | PAGE_WRITE) |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 279 | |
Richard Henderson | 1720751 | 2021-02-12 10:48:39 -0800 | [diff] [blame] | 280 | static inline bool access_ok(int type, abi_ulong addr, abi_ulong size) |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 281 | { |
Richard Henderson | bef6f00 | 2023-07-07 21:40:52 +0100 | [diff] [blame] | 282 | return page_check_range((target_ulong)addr, size, type); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Warner Losh | c2bdd9a | 2021-04-23 19:50:53 -0600 | [diff] [blame] | 285 | /* |
| 286 | * NOTE __get_user and __put_user use host pointers and don't check access. |
| 287 | * |
| 288 | * These are usually used to access struct data members once the struct has been |
| 289 | * locked - usually with lock_user_struct(). |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 290 | */ |
Warner Losh | 8a45962 | 2023-08-30 08:44:01 -0600 | [diff] [blame] | 291 | |
| 292 | /* |
| 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 |
| 304 | */ |
| 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\"") |
| 309 | |
| 310 | #define PRAGMA_REENABLE_PACKED_WARNING \ |
| 311 | _Pragma("GCC diagnostic pop") |
| 312 | |
| 313 | #else |
| 314 | #define PRAGMA_DISABLE_PACKED_WARNING |
| 315 | #define PRAGMA_REENABLE_PACKED_WARNING |
| 316 | #endif |
| 317 | |
Warner Losh | 6538c68 | 2023-08-13 10:41:24 +0200 | [diff] [blame] | 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) |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 328 | |
Warner Losh | 6538c68 | 2023-08-13 10:41:24 +0200 | [diff] [blame] | 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) |
| 340 | |
| 341 | |
| 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 |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 349 | |
Warner Losh | c2bdd9a | 2021-04-23 19:50:53 -0600 | [diff] [blame] | 350 | /* |
| 351 | * put_user()/get_user() take a guest address and check access |
| 352 | * |
| 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. |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 356 | */ |
| 357 | #define put_user(x, gaddr, target_type) \ |
| 358 | ({ \ |
| 359 | abi_ulong __gaddr = (gaddr); \ |
| 360 | target_type *__hptr; \ |
Warner Losh | 6538c68 | 2023-08-13 10:41:24 +0200 | [diff] [blame] | 361 | abi_long __ret = 0; \ |
Warner Losh | 3306693 | 2021-04-23 19:53:36 -0600 | [diff] [blame] | 362 | __hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0); \ |
| 363 | if (__hptr) { \ |
Warner Losh | 6538c68 | 2023-08-13 10:41:24 +0200 | [diff] [blame] | 364 | __put_user((x), __hptr); \ |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 365 | unlock_user(__hptr, __gaddr, sizeof(target_type)); \ |
| 366 | } else \ |
| 367 | __ret = -TARGET_EFAULT; \ |
| 368 | __ret; \ |
| 369 | }) |
| 370 | |
| 371 | #define get_user(x, gaddr, target_type) \ |
| 372 | ({ \ |
| 373 | abi_ulong __gaddr = (gaddr); \ |
| 374 | target_type *__hptr; \ |
Warner Losh | 6538c68 | 2023-08-13 10:41:24 +0200 | [diff] [blame] | 375 | abi_long __ret = 0; \ |
Warner Losh | 3306693 | 2021-04-23 19:53:36 -0600 | [diff] [blame] | 376 | __hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1); \ |
| 377 | if (__hptr) { \ |
Warner Losh | 6538c68 | 2023-08-13 10:41:24 +0200 | [diff] [blame] | 378 | __get_user((x), __hptr); \ |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 379 | unlock_user(__hptr, __gaddr, 0); \ |
| 380 | } else { \ |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 381 | (x) = 0; \ |
| 382 | __ret = -TARGET_EFAULT; \ |
| 383 | } \ |
| 384 | __ret; \ |
| 385 | }) |
| 386 | |
| 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) |
| 397 | |
| 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) |
| 408 | |
Warner Losh | c2bdd9a | 2021-04-23 19:50:53 -0600 | [diff] [blame] | 409 | /* |
| 410 | * copy_from_user() and copy_to_user() are usually used to copy data |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 411 | * buffers between the target and host. These internally perform |
| 412 | * locking/unlocking of the memory. |
| 413 | */ |
| 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); |
| 416 | |
Warner Losh | c2bdd9a | 2021-04-23 19:50:53 -0600 | [diff] [blame] | 417 | /* |
| 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. |
| 423 | */ |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 424 | |
Warner Losh | c2bdd9a | 2021-04-23 19:50:53 -0600 | [diff] [blame] | 425 | /* |
| 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. |
| 428 | */ |
| 429 | static inline void *lock_user(int type, abi_ulong guest_addr, long len, |
| 430 | int copy) |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 431 | { |
Warner Losh | cb0ea01 | 2021-04-23 19:55:03 -0600 | [diff] [blame] | 432 | if (!access_ok(type, guest_addr, len)) { |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 433 | return NULL; |
Warner Losh | cb0ea01 | 2021-04-23 19:55:03 -0600 | [diff] [blame] | 434 | } |
Ilya Leoshkevich | 1f2355f | 2024-03-12 01:23:30 +0100 | [diff] [blame] | 435 | #ifdef CONFIG_DEBUG_REMAP |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 436 | { |
| 437 | void *addr; |
Md Haris Iqbal | fd9a304 | 2016-04-05 18:39:03 +0530 | [diff] [blame] | 438 | addr = g_malloc(len); |
Warner Losh | cb0ea01 | 2021-04-23 19:55:03 -0600 | [diff] [blame] | 439 | if (copy) { |
Richard Henderson | 3e8f162 | 2021-02-12 10:48:43 -0800 | [diff] [blame] | 440 | memcpy(addr, g2h_untagged(guest_addr), len); |
Warner Losh | cb0ea01 | 2021-04-23 19:55:03 -0600 | [diff] [blame] | 441 | } else { |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 442 | memset(addr, 0, len); |
Warner Losh | cb0ea01 | 2021-04-23 19:55:03 -0600 | [diff] [blame] | 443 | } |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 444 | return addr; |
| 445 | } |
| 446 | #else |
Richard Henderson | 3e8f162 | 2021-02-12 10:48:43 -0800 | [diff] [blame] | 447 | return g2h_untagged(guest_addr); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 448 | #endif |
| 449 | } |
| 450 | |
Warner Losh | c2bdd9a | 2021-04-23 19:50:53 -0600 | [diff] [blame] | 451 | /* |
| 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. |
| 454 | */ |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 455 | static inline void unlock_user(void *host_ptr, abi_ulong guest_addr, |
| 456 | long len) |
| 457 | { |
| 458 | |
Ilya Leoshkevich | 1f2355f | 2024-03-12 01:23:30 +0100 | [diff] [blame] | 459 | #ifdef CONFIG_DEBUG_REMAP |
Warner Losh | cb0ea01 | 2021-04-23 19:55:03 -0600 | [diff] [blame] | 460 | if (!host_ptr) { |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 461 | return; |
Warner Losh | cb0ea01 | 2021-04-23 19:55:03 -0600 | [diff] [blame] | 462 | } |
| 463 | if (host_ptr == g2h_untagged(guest_addr)) { |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 464 | return; |
Warner Losh | cb0ea01 | 2021-04-23 19:55:03 -0600 | [diff] [blame] | 465 | } |
| 466 | if (len > 0) { |
Richard Henderson | 3e8f162 | 2021-02-12 10:48:43 -0800 | [diff] [blame] | 467 | memcpy(g2h_untagged(guest_addr), host_ptr, len); |
Warner Losh | cb0ea01 | 2021-04-23 19:55:03 -0600 | [diff] [blame] | 468 | } |
Md Haris Iqbal | fd9a304 | 2016-04-05 18:39:03 +0530 | [diff] [blame] | 469 | g_free(host_ptr); |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 470 | #endif |
| 471 | } |
| 472 | |
Warner Losh | c2bdd9a | 2021-04-23 19:50:53 -0600 | [diff] [blame] | 473 | /* |
| 474 | * Return the length of a string in target memory or -TARGET_EFAULT if access |
| 475 | * error. |
| 476 | */ |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 477 | abi_long target_strlen(abi_ulong gaddr); |
| 478 | |
| 479 | /* Like lock_user but for null terminated strings. */ |
| 480 | static inline void *lock_user_string(abi_ulong guest_addr) |
| 481 | { |
| 482 | abi_long len; |
| 483 | len = target_strlen(guest_addr); |
Warner Losh | cb0ea01 | 2021-04-23 19:55:03 -0600 | [diff] [blame] | 484 | if (len < 0) { |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 485 | return NULL; |
Warner Losh | cb0ea01 | 2021-04-23 19:55:03 -0600 | [diff] [blame] | 486 | } |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 487 | return lock_user(VERIFY_READ, guest_addr, (long)(len + 1), 1); |
| 488 | } |
| 489 | |
Stefan Weil | 41d1af4 | 2013-09-12 19:57:41 +0200 | [diff] [blame] | 490 | /* Helper macros for locking/unlocking a target struct. */ |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 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) |
| 495 | |
Warner Losh | 0ff0508 | 2022-01-31 16:06:46 -0700 | [diff] [blame] | 496 | static inline uint64_t target_arg64(uint32_t word0, uint32_t word1) |
| 497 | { |
| 498 | #if TARGET_ABI_BITS == 32 |
Marc-André Lureau | ee3eb3a | 2022-03-23 19:57:18 +0400 | [diff] [blame] | 499 | #if TARGET_BIG_ENDIAN |
Warner Losh | 0ff0508 | 2022-01-31 16:06:46 -0700 | [diff] [blame] | 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 */ |
| 507 | } |
| 508 | |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 509 | #include <pthread.h> |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 510 | |
Warner Losh | aae57ac | 2022-01-08 17:55:56 -0700 | [diff] [blame] | 511 | #include "user/safe-syscall.h" |
| 512 | |
blueswir1 | 8477850 | 2008-10-26 20:33:16 +0000 | [diff] [blame] | 513 | #endif /* QEMU_H */ |