root / linux-user / uaccess.c @ 579a97f7
History | View | Annotate | Download (1.3 kB)
1 |
/* User memory access */
|
---|---|
2 |
#include <stdio.h> |
3 |
#include <string.h> |
4 |
|
5 |
#include "qemu.h" |
6 |
|
7 |
/* copy_from_user() and copy_to_user() are usually used to copy data
|
8 |
* buffers between the target and host. These internally perform
|
9 |
* locking/unlocking of the memory.
|
10 |
*/
|
11 |
abi_long copy_from_user(void *hptr, abi_ulong gaddr, size_t len)
|
12 |
{ |
13 |
abi_long ret = 0;
|
14 |
void *ghptr;
|
15 |
|
16 |
if ((ghptr = lock_user(VERIFY_READ, gaddr, len, 1))) { |
17 |
memcpy(hptr, ghptr, len); |
18 |
unlock_user(ghptr, gaddr, 0);
|
19 |
} else
|
20 |
ret = -TARGET_EFAULT; |
21 |
|
22 |
return ret;
|
23 |
} |
24 |
|
25 |
|
26 |
abi_long copy_to_user(abi_ulong gaddr, void *hptr, size_t len)
|
27 |
{ |
28 |
abi_long ret = 0;
|
29 |
void *ghptr;
|
30 |
|
31 |
if ((ghptr = lock_user(VERIFY_WRITE, gaddr, len, 0))) { |
32 |
memcpy(ghptr, hptr, len); |
33 |
unlock_user(ghptr, gaddr, len); |
34 |
} else
|
35 |
ret = -TARGET_EFAULT; |
36 |
|
37 |
return ret;
|
38 |
} |
39 |
|
40 |
|
41 |
/* Return the length of a string in target memory. */
|
42 |
/* FIXME - this doesn't check access_ok() - it's rather complicated to
|
43 |
* do it correctly because we need to check the bytes in a page and then
|
44 |
* skip to the next page and check the bytes there until we find the
|
45 |
* terminator. There should be a general function to do this that
|
46 |
* can look for any byte terminator in a buffer - not strlen().
|
47 |
*/
|
48 |
abi_long target_strlen(abi_ulong gaddr) |
49 |
{ |
50 |
return strlen(g2h(gaddr));
|
51 |
} |