1 #ifndef QEMU_H 2 #define QEMU_H 3 4 #include "cpu.h" 5 #include "exec/exec-all.h" 6 #include "exec/cpu_ldst.h" 7 8 #undef DEBUG_REMAP 9 10 #include "exec/user/abitypes.h" 11 12 #include "syscall_defs.h" 13 #include "target_syscall.h" 14 #include "exec/gdbstub.h" 15 16 /* 17 * This is the size of the host kernel's sigset_t, needed where we make 18 * direct system calls that take a sigset_t pointer and a size. 19 */ 20 #define SIGSET_T_SIZE (_NSIG / 8) 21 22 /* 23 * This struct is used to hold certain information about the image. 24 * Basically, it replicates in user space what would be certain 25 * task_struct fields in the kernel 26 */ 27 struct image_info { 28 abi_ulong load_bias; 29 abi_ulong load_addr; 30 abi_ulong start_code; 31 abi_ulong end_code; 32 abi_ulong start_data; 33 abi_ulong end_data; 34 abi_ulong start_brk; 35 abi_ulong brk; 36 abi_ulong reserve_brk; 37 abi_ulong start_mmap; 38 abi_ulong start_stack; 39 abi_ulong stack_limit; 40 abi_ulong entry; 41 abi_ulong code_offset; 42 abi_ulong data_offset; 43 abi_ulong saved_auxv; 44 abi_ulong auxv_len; 45 abi_ulong arg_start; 46 abi_ulong arg_end; 47 abi_ulong arg_strings; 48 abi_ulong env_strings; 49 abi_ulong file_string; 50 uint32_t elf_flags; 51 int personality; 52 abi_ulong alignment; 53 54 /* The fields below are used in FDPIC mode. */ 55 abi_ulong loadmap_addr; 56 uint16_t nsegs; 57 void *loadsegs; 58 abi_ulong pt_dynamic_addr; 59 abi_ulong interpreter_loadmap_addr; 60 abi_ulong interpreter_pt_dynamic_addr; 61 struct image_info *other_info; 62 63 /* For target-specific processing of NT_GNU_PROPERTY_TYPE_0. */ 64 uint32_t note_flags; 65 66 #ifdef TARGET_MIPS 67 int fp_abi; 68 int interp_fp_abi; 69 #endif 70 }; 71 72 #ifdef TARGET_I386 73 /* Information about the current linux thread */ 74 struct vm86_saved_state { 75 uint32_t eax; /* return code */ 76 uint32_t ebx; 77 uint32_t ecx; 78 uint32_t edx; 79 uint32_t esi; 80 uint32_t edi; 81 uint32_t ebp; 82 uint32_t esp; 83 uint32_t eflags; 84 uint32_t eip; 85 uint16_t cs, ss, ds, es, fs, gs; 86 }; 87 #endif 88 89 #if defined(TARGET_ARM) && defined(TARGET_ABI32) 90 /* FPU emulator */ 91 #include "nwfpe/fpa11.h" 92 #endif 93 94 #define MAX_SIGQUEUE_SIZE 1024 95 96 struct emulated_sigtable { 97 int pending; /* true if signal is pending */ 98 target_siginfo_t info; 99 }; 100 101 /* 102 * NOTE: we force a big alignment so that the stack stored after is 103 * aligned too 104 */ 105 typedef struct TaskState { 106 pid_t ts_tid; /* tid (or pid) of this task */ 107 #ifdef TARGET_ARM 108 # ifdef TARGET_ABI32 109 /* FPA state */ 110 FPA11 fpa; 111 # endif 112 #endif 113 #if defined(TARGET_ARM) || defined(TARGET_RISCV) 114 int swi_errno; 115 #endif 116 #if defined(TARGET_I386) && !defined(TARGET_X86_64) 117 abi_ulong target_v86; 118 struct vm86_saved_state vm86_saved_regs; 119 struct target_vm86plus_struct vm86plus; 120 uint32_t v86flags; 121 uint32_t v86mask; 122 #endif 123 abi_ulong child_tidptr; 124 #ifdef TARGET_M68K 125 abi_ulong tp_value; 126 #endif 127 #if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_RISCV) 128 /* Extra fields for semihosted binaries. */ 129 abi_ulong heap_base; 130 abi_ulong heap_limit; 131 #endif 132 abi_ulong stack_base; 133 int used; /* non zero if used */ 134 struct image_info *info; 135 struct linux_binprm *bprm; 136 137 struct emulated_sigtable sync_signal; 138 struct emulated_sigtable sigtab[TARGET_NSIG]; 139 /* 140 * This thread's signal mask, as requested by the guest program. 141 * The actual signal mask of this thread may differ: 142 * + we don't let SIGSEGV and SIGBUS be blocked while running guest code 143 * + sometimes we block all signals to avoid races 144 */ 145 sigset_t signal_mask; 146 /* 147 * The signal mask imposed by a guest sigsuspend syscall, if we are 148 * currently in the middle of such a syscall 149 */ 150 sigset_t sigsuspend_mask; 151 /* Nonzero if we're leaving a sigsuspend and sigsuspend_mask is valid. */ 152 int in_sigsuspend; 153 154 /* 155 * Nonzero if process_pending_signals() needs to do something (either 156 * handle a pending signal or unblock signals). 157 * This flag is written from a signal handler so should be accessed via 158 * the qatomic_read() and qatomic_set() functions. (It is not accessed 159 * from multiple threads.) 160 */ 161 int signal_pending; 162 163 /* This thread's sigaltstack, if it has one */ 164 struct target_sigaltstack sigaltstack_used; 165 } __attribute__((aligned(16))) TaskState; 166 167 #include "qemu/log.h" 168 169 abi_long do_brk(abi_ulong new_brk); 170 171 /* user access */ 172 173 #define VERIFY_READ PAGE_READ 174 #define VERIFY_WRITE (PAGE_READ | PAGE_WRITE) 175 176 static inline bool access_ok_untagged(int type, abi_ulong addr, abi_ulong size) 177 { 178 if (size == 0 179 ? !guest_addr_valid_untagged(addr) 180 : !guest_range_valid_untagged(addr, size)) { 181 return false; 182 } 183 return page_check_range((target_ulong)addr, size, type) == 0; 184 } 185 186 static inline bool access_ok(CPUState *cpu, int type, 187 abi_ulong addr, abi_ulong size) 188 { 189 return access_ok_untagged(type, cpu_untagged_addr(cpu, addr), size); 190 } 191 192 /* NOTE __get_user and __put_user use host pointers and don't check access. 193 These are usually used to access struct data members once the struct has 194 been locked - usually with lock_user_struct. */ 195 196 /* 197 * Tricky points: 198 * - Use __builtin_choose_expr to avoid type promotion from ?:, 199 * - Invalid sizes result in a compile time error stemming from 200 * the fact that abort has no parameters. 201 * - It's easier to use the endian-specific unaligned load/store 202 * functions than host-endian unaligned load/store plus tswapN. 203 * - The pragmas are necessary only to silence a clang false-positive 204 * warning: see https://bugs.llvm.org/show_bug.cgi?id=39113 . 205 * - gcc has bugs in its _Pragma() support in some versions, eg 206 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83256 -- so we only 207 * include the warning-suppression pragmas for clang 208 */ 209 #if defined(__clang__) && __has_warning("-Waddress-of-packed-member") 210 #define PRAGMA_DISABLE_PACKED_WARNING \ 211 _Pragma("GCC diagnostic push"); \ 212 _Pragma("GCC diagnostic ignored \"-Waddress-of-packed-member\"") 213 214 #define PRAGMA_REENABLE_PACKED_WARNING \ 215 _Pragma("GCC diagnostic pop") 216 217 #else 218 #define PRAGMA_DISABLE_PACKED_WARNING 219 #define PRAGMA_REENABLE_PACKED_WARNING 220 #endif 221 222 #define __put_user_e(x, hptr, e) \ 223 do { \ 224 PRAGMA_DISABLE_PACKED_WARNING; \ 225 (__builtin_choose_expr(sizeof(*(hptr)) == 1, stb_p, \ 226 __builtin_choose_expr(sizeof(*(hptr)) == 2, stw_##e##_p, \ 227 __builtin_choose_expr(sizeof(*(hptr)) == 4, stl_##e##_p, \ 228 __builtin_choose_expr(sizeof(*(hptr)) == 8, stq_##e##_p, abort)))) \ 229 ((hptr), (x)), (void)0); \ 230 PRAGMA_REENABLE_PACKED_WARNING; \ 231 } while (0) 232 233 #define __get_user_e(x, hptr, e) \ 234 do { \ 235 PRAGMA_DISABLE_PACKED_WARNING; \ 236 ((x) = (typeof(*hptr))( \ 237 __builtin_choose_expr(sizeof(*(hptr)) == 1, ldub_p, \ 238 __builtin_choose_expr(sizeof(*(hptr)) == 2, lduw_##e##_p, \ 239 __builtin_choose_expr(sizeof(*(hptr)) == 4, ldl_##e##_p, \ 240 __builtin_choose_expr(sizeof(*(hptr)) == 8, ldq_##e##_p, abort)))) \ 241 (hptr)), (void)0); \ 242 PRAGMA_REENABLE_PACKED_WARNING; \ 243 } while (0) 244 245 246 #ifdef TARGET_WORDS_BIGENDIAN 247 # define __put_user(x, hptr) __put_user_e(x, hptr, be) 248 # define __get_user(x, hptr) __get_user_e(x, hptr, be) 249 #else 250 # define __put_user(x, hptr) __put_user_e(x, hptr, le) 251 # define __get_user(x, hptr) __get_user_e(x, hptr, le) 252 #endif 253 254 /* put_user()/get_user() take a guest address and check access */ 255 /* These are usually used to access an atomic data type, such as an int, 256 * that has been passed by address. These internally perform locking 257 * and unlocking on the data type. 258 */ 259 #define put_user(x, gaddr, target_type) \ 260 ({ \ 261 abi_ulong __gaddr = (gaddr); \ 262 target_type *__hptr; \ 263 abi_long __ret = 0; \ 264 if ((__hptr = lock_user(VERIFY_WRITE, __gaddr, sizeof(target_type), 0))) { \ 265 __put_user((x), __hptr); \ 266 unlock_user(__hptr, __gaddr, sizeof(target_type)); \ 267 } else \ 268 __ret = -TARGET_EFAULT; \ 269 __ret; \ 270 }) 271 272 #define get_user(x, gaddr, target_type) \ 273 ({ \ 274 abi_ulong __gaddr = (gaddr); \ 275 target_type *__hptr; \ 276 abi_long __ret = 0; \ 277 if ((__hptr = lock_user(VERIFY_READ, __gaddr, sizeof(target_type), 1))) { \ 278 __get_user((x), __hptr); \ 279 unlock_user(__hptr, __gaddr, 0); \ 280 } else { \ 281 /* avoid warning */ \ 282 (x) = 0; \ 283 __ret = -TARGET_EFAULT; \ 284 } \ 285 __ret; \ 286 }) 287 288 #define put_user_ual(x, gaddr) put_user((x), (gaddr), abi_ulong) 289 #define put_user_sal(x, gaddr) put_user((x), (gaddr), abi_long) 290 #define put_user_u64(x, gaddr) put_user((x), (gaddr), uint64_t) 291 #define put_user_s64(x, gaddr) put_user((x), (gaddr), int64_t) 292 #define put_user_u32(x, gaddr) put_user((x), (gaddr), uint32_t) 293 #define put_user_s32(x, gaddr) put_user((x), (gaddr), int32_t) 294 #define put_user_u16(x, gaddr) put_user((x), (gaddr), uint16_t) 295 #define put_user_s16(x, gaddr) put_user((x), (gaddr), int16_t) 296 #define put_user_u8(x, gaddr) put_user((x), (gaddr), uint8_t) 297 #define put_user_s8(x, gaddr) put_user((x), (gaddr), int8_t) 298 299 #define get_user_ual(x, gaddr) get_user((x), (gaddr), abi_ulong) 300 #define get_user_sal(x, gaddr) get_user((x), (gaddr), abi_long) 301 #define get_user_u64(x, gaddr) get_user((x), (gaddr), uint64_t) 302 #define get_user_s64(x, gaddr) get_user((x), (gaddr), int64_t) 303 #define get_user_u32(x, gaddr) get_user((x), (gaddr), uint32_t) 304 #define get_user_s32(x, gaddr) get_user((x), (gaddr), int32_t) 305 #define get_user_u16(x, gaddr) get_user((x), (gaddr), uint16_t) 306 #define get_user_s16(x, gaddr) get_user((x), (gaddr), int16_t) 307 #define get_user_u8(x, gaddr) get_user((x), (gaddr), uint8_t) 308 #define get_user_s8(x, gaddr) get_user((x), (gaddr), int8_t) 309 310 /* copy_from_user() and copy_to_user() are usually used to copy data 311 * buffers between the target and host. These internally perform 312 * locking/unlocking of the memory. 313 */ 314 int copy_from_user(void *hptr, abi_ulong gaddr, ssize_t len); 315 int copy_to_user(abi_ulong gaddr, void *hptr, ssize_t len); 316 317 /* Functions for accessing guest memory. The tget and tput functions 318 read/write single values, byteswapping as necessary. The lock_user function 319 gets a pointer to a contiguous area of guest memory, but does not perform 320 any byteswapping. lock_user may return either a pointer to the guest 321 memory, or a temporary buffer. */ 322 323 /* Lock an area of guest memory into the host. If copy is true then the 324 host area will have the same contents as the guest. */ 325 void *lock_user(int type, abi_ulong guest_addr, ssize_t len, bool copy); 326 327 /* Unlock an area of guest memory. The first LEN bytes must be 328 flushed back to guest memory. host_ptr = NULL is explicitly 329 allowed and does nothing. */ 330 #ifndef DEBUG_REMAP 331 static inline void unlock_user(void *host_ptr, abi_ulong guest_addr, 332 ssize_t len) 333 { 334 /* no-op */ 335 } 336 #else 337 void unlock_user(void *host_ptr, abi_ulong guest_addr, ssize_t len); 338 #endif 339 340 /* Return the length of a string in target memory or -TARGET_EFAULT if 341 access error. */ 342 ssize_t target_strlen(abi_ulong gaddr); 343 344 /* Like lock_user but for null terminated strings. */ 345 void *lock_user_string(abi_ulong guest_addr); 346 347 /* Helper macros for locking/unlocking a target struct. */ 348 #define lock_user_struct(type, host_ptr, guest_addr, copy) \ 349 (host_ptr = lock_user(type, guest_addr, sizeof(*host_ptr), copy)) 350 #define unlock_user_struct(host_ptr, guest_addr, copy) \ 351 unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0) 352 353 #include <pthread.h> 354 #endif /* QEMU_H */ 355