ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1 | /* |
pbrook | 6b3a45c | 2007-05-26 16:46:21 +0000 | [diff] [blame] | 2 | * Helper routines to provide target memory access for semihosting |
| 3 | * syscalls in system emulation mode. |
| 4 | * |
| 5 | * Copyright (c) 2007 CodeSourcery. |
| 6 | * |
| 7 | * This code is licenced under the GPL |
| 8 | */ |
| 9 | |
| 10 | static inline uint32_t softmmu_tget32(CPUState *env, uint32_t addr) |
| 11 | { |
| 12 | uint32_t val; |
| 13 | |
| 14 | cpu_memory_rw_debug(env, addr, (uint8_t *)&val, 4, 0); |
| 15 | return tswap32(val); |
| 16 | } |
| 17 | static inline uint32_t softmmu_tget8(CPUState *env, uint32_t addr) |
| 18 | { |
| 19 | uint8_t val; |
| 20 | |
| 21 | cpu_memory_rw_debug(env, addr, &val, 1, 0); |
| 22 | return val; |
| 23 | } |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 24 | |
| 25 | #define get_user_u32(arg, p) ({ arg = softmmu_tget32(env, p) ; 0; }) |
| 26 | #define get_user_u8(arg, p) ({ arg = softmmu_tget8(env, p) ; 0; }) |
| 27 | #define get_user_ual(arg, p) get_user_u32(arg, p) |
pbrook | 6b3a45c | 2007-05-26 16:46:21 +0000 | [diff] [blame] | 28 | |
| 29 | static inline void softmmu_tput32(CPUState *env, uint32_t addr, uint32_t val) |
| 30 | { |
| 31 | val = tswap32(val); |
| 32 | cpu_memory_rw_debug(env, addr, (uint8_t *)&val, 4, 1); |
| 33 | } |
bellard | 2f61969 | 2007-11-16 10:46:05 +0000 | [diff] [blame] | 34 | #define put_user_u32(arg, p) ({ softmmu_tput32(env, p, arg) ; 0; }) |
| 35 | #define put_user_ual(arg, p) put_user_u32(arg, p) |
pbrook | 6b3a45c | 2007-05-26 16:46:21 +0000 | [diff] [blame] | 36 | |
| 37 | static void *softmmu_lock_user(CPUState *env, uint32_t addr, uint32_t len, |
| 38 | int copy) |
| 39 | { |
blueswir1 | b55266b | 2008-09-20 08:07:15 +0000 | [diff] [blame] | 40 | uint8_t *p; |
pbrook | 6b3a45c | 2007-05-26 16:46:21 +0000 | [diff] [blame] | 41 | /* TODO: Make this something that isn't fixed size. */ |
| 42 | p = malloc(len); |
| 43 | if (copy) |
| 44 | cpu_memory_rw_debug(env, addr, p, len, 0); |
| 45 | return p; |
| 46 | } |
bellard | 579a97f | 2007-11-11 14:26:47 +0000 | [diff] [blame] | 47 | #define lock_user(type, p, len, copy) softmmu_lock_user(env, p, len, copy) |
pbrook | 6b3a45c | 2007-05-26 16:46:21 +0000 | [diff] [blame] | 48 | static char *softmmu_lock_user_string(CPUState *env, uint32_t addr) |
| 49 | { |
| 50 | char *p; |
| 51 | char *s; |
| 52 | uint8_t c; |
| 53 | /* TODO: Make this something that isn't fixed size. */ |
| 54 | s = p = malloc(1024); |
| 55 | do { |
| 56 | cpu_memory_rw_debug(env, addr, &c, 1, 0); |
| 57 | addr++; |
| 58 | *(p++) = c; |
| 59 | } while (c); |
| 60 | return s; |
| 61 | } |
| 62 | #define lock_user_string(p) softmmu_lock_user_string(env, p) |
| 63 | static void softmmu_unlock_user(CPUState *env, void *p, target_ulong addr, |
| 64 | target_ulong len) |
| 65 | { |
| 66 | if (len) |
| 67 | cpu_memory_rw_debug(env, addr, p, len, 1); |
| 68 | free(p); |
| 69 | } |
| 70 | #define unlock_user(s, args, len) softmmu_unlock_user(env, s, args, len) |