1 /* User memory access */
2 #include "qemu/osdep.h"
3 #include "qemu/cutils.h"
7 void *lock_user(int type
, abi_ulong guest_addr
, size_t len
, bool copy
)
11 guest_addr
= cpu_untagged_addr(thread_cpu
, guest_addr
);
12 if (!access_ok_untagged(type
, guest_addr
, len
)) {
15 host_addr
= g2h_untagged(guest_addr
);
18 host_addr
= g_memdup(host_addr
, len
);
20 host_addr
= g_malloc0(len
);
27 void unlock_user(void *host_ptr
, abi_ulong guest_addr
, size_t len
);
34 host_ptr_conv
= g2h(thread_cpu
, guest_addr
);
35 if (host_ptr
== host_ptr_conv
) {
39 memcpy(host_ptr_conv
, host_ptr
, len
);
45 void *lock_user_string(abi_ulong guest_addr
)
47 ssize_t len
= target_strlen(guest_addr
);
51 return lock_user(VERIFY_READ
, guest_addr
, (size_t)len
+ 1, 1);
54 /* copy_from_user() and copy_to_user() are usually used to copy data
55 * buffers between the target and host. These internally perform
56 * locking/unlocking of the memory.
58 int copy_from_user(void *hptr
, abi_ulong gaddr
, size_t len
)
61 void *ghptr
= lock_user(VERIFY_READ
, gaddr
, len
, 1);
64 memcpy(hptr
, ghptr
, len
);
65 unlock_user(ghptr
, gaddr
, 0);
72 int copy_to_user(abi_ulong gaddr
, void *hptr
, size_t len
)
75 void *ghptr
= lock_user(VERIFY_WRITE
, gaddr
, len
, 0);
78 memcpy(ghptr
, hptr
, len
);
79 unlock_user(ghptr
, gaddr
, len
);
87 /* Return the length of a string in target memory or -TARGET_EFAULT if
89 ssize_t
target_strlen(abi_ulong guest_addr1
)
95 guest_addr
= guest_addr1
;
97 max_len
= TARGET_PAGE_SIZE
- (guest_addr
& ~TARGET_PAGE_MASK
);
98 ptr
= lock_user(VERIFY_READ
, guest_addr
, max_len
, 1);
100 return -TARGET_EFAULT
;
101 len
= qemu_strnlen((const char *)ptr
, max_len
);
102 unlock_user(ptr
, guest_addr
, 0);
104 /* we don't allow wrapping or integer overflow */
105 if (guest_addr
== 0 || (guest_addr
- guest_addr1
) > 0x7fffffff) {
106 return -TARGET_EFAULT
;
108 if (len
!= max_len
) {
112 return guest_addr
- guest_addr1
;