1 /* SPDX-License-Identifier: LGPL-2.1-or-later */ 2 /* 3 * guest <-> host helpers. 4 * 5 * Copyright (c) 2003 Fabrice Bellard 6 */ 7 8 #ifndef USER_GUEST_HOST_H 9 #define USER_GUEST_HOST_H 10 11 #include "exec/vaddr.h" 12 #include "user/guest-base.h" 13 #include "cpu.h" 14 15 /* 16 * If non-zero, the guest virtual address space is a contiguous subset 17 * of the host virtual address space, i.e. '-R reserved_va' is in effect 18 * either from the command-line or by default. The value is the last 19 * byte of the guest address space e.g. UINT32_MAX. 20 * 21 * If zero, the host and guest virtual address spaces are intermingled. 22 */ 23 extern unsigned long reserved_va; 24 25 /* 26 * The last byte of the guest address space. 27 * If reserved_va is non-zero, guest_addr_max matches. 28 * If reserved_va is zero, guest_addr_max equals the full guest space. 29 */ 30 extern unsigned long guest_addr_max; 31 32 #ifndef TARGET_TAGGED_ADDRESSES 33 static inline vaddr cpu_untagged_addr(CPUState *cs, vaddr x) 34 { 35 return x; 36 } 37 #endif 38 39 /* All direct uses of g2h and h2g need to go away for usermode softmmu. */ 40 static inline void *g2h_untagged(vaddr x) 41 { 42 return (void *)((uintptr_t)(x) + guest_base); 43 } 44 45 static inline void *g2h(CPUState *cs, vaddr x) 46 { 47 return g2h_untagged(cpu_untagged_addr(cs, x)); 48 } 49 50 static inline bool guest_addr_valid_untagged(vaddr x) 51 { 52 return x <= guest_addr_max; 53 } 54 55 static inline bool guest_range_valid_untagged(vaddr start, vaddr len) 56 { 57 return len - 1 <= guest_addr_max && start <= guest_addr_max - len + 1; 58 } 59 60 #define h2g_valid(x) \ 61 ((uintptr_t)(x) - guest_base <= guest_addr_max) 62 63 #define h2g_nocheck(x) ({ \ 64 uintptr_t __ret = (uintptr_t)(x) - guest_base; \ 65 (vaddr)__ret; \ 66 }) 67 68 #define h2g(x) ({ \ 69 /* Check if given address fits target address space */ \ 70 assert(h2g_valid(x)); \ 71 h2g_nocheck(x); \ 72 }) 73 74 #endif 75