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