Avoid accessing guest memory directly in usermode emulation.
[qemu/mini2440.git] / linux-user / qemu.h
bloba797c512bb7f0c38360d2c76bc85f7fe082263e7
1 #ifndef QEMU_H
2 #define QEMU_H
4 #include "thunk.h"
6 #include <signal.h>
7 #include <string.h>
8 #include "syscall_defs.h"
10 #include "cpu.h"
11 #include "syscall.h"
12 #include "gdbstub.h"
14 /* This struct is used to hold certain information about the image.
15 * Basically, it replicates in user space what would be certain
16 * task_struct fields in the kernel
18 struct image_info {
19 unsigned long start_code;
20 unsigned long end_code;
21 unsigned long end_data;
22 unsigned long start_brk;
23 unsigned long brk;
24 unsigned long start_mmap;
25 unsigned long mmap;
26 unsigned long rss;
27 unsigned long start_stack;
28 unsigned long arg_start;
29 unsigned long arg_end;
30 unsigned long env_start;
31 unsigned long env_end;
32 unsigned long entry;
33 int personality;
36 #ifdef TARGET_I386
37 /* Information about the current linux thread */
38 struct vm86_saved_state {
39 uint32_t eax; /* return code */
40 uint32_t ebx;
41 uint32_t ecx;
42 uint32_t edx;
43 uint32_t esi;
44 uint32_t edi;
45 uint32_t ebp;
46 uint32_t esp;
47 uint32_t eflags;
48 uint32_t eip;
49 uint16_t cs, ss, ds, es, fs, gs;
51 #endif
53 #ifdef TARGET_ARM
54 /* FPU emulator */
55 #include "nwfpe/fpa11.h"
56 #endif
58 /* NOTE: we force a big alignment so that the stack stored after is
59 aligned too */
60 typedef struct TaskState {
61 struct TaskState *next;
62 #ifdef TARGET_ARM
63 /* FPA state */
64 FPA11 fpa;
65 /* Extra fields for semihosted binaries. */
66 uint32_t stack_base;
67 uint32_t heap_base;
68 uint32_t heap_limit;
69 int swi_errno;
70 #endif
71 #ifdef TARGET_I386
72 target_ulong target_v86;
73 struct vm86_saved_state vm86_saved_regs;
74 struct target_vm86plus_struct vm86plus;
75 uint32_t v86flags;
76 uint32_t v86mask;
77 #endif
78 int used; /* non zero if used */
79 uint8_t stack[0];
80 } __attribute__((aligned(16))) TaskState;
82 extern TaskState *first_task_state;
84 int elf_exec(const char * filename, char ** argv, char ** envp,
85 struct target_pt_regs * regs, struct image_info *infop);
87 void target_set_brk(target_ulong new_brk);
88 long do_brk(target_ulong new_brk);
89 void syscall_init(void);
90 long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3,
91 long arg4, long arg5, long arg6);
92 void gemu_log(const char *fmt, ...) __attribute__((format(printf,1,2)));
93 extern CPUState *global_env;
94 void cpu_loop(CPUState *env);
95 void init_paths(const char *prefix);
96 const char *path(const char *pathname);
98 extern int loglevel;
99 extern FILE *logfile;
101 /* signal.c */
102 void process_pending_signals(void *cpu_env);
103 void signal_init(void);
104 int queue_signal(int sig, target_siginfo_t *info);
105 void host_to_target_siginfo(target_siginfo_t *tinfo, const siginfo_t *info);
106 void target_to_host_siginfo(siginfo_t *info, const target_siginfo_t *tinfo);
107 long do_sigreturn(CPUState *env);
108 long do_rt_sigreturn(CPUState *env);
110 #ifdef TARGET_I386
111 /* vm86.c */
112 void save_v86_state(CPUX86State *env);
113 void handle_vm86_trap(CPUX86State *env, int trapno);
114 void handle_vm86_fault(CPUX86State *env);
115 int do_vm86(CPUX86State *env, long subfunction, target_ulong v86_addr);
116 #endif
118 /* mmap.c */
119 int target_mprotect(target_ulong start, target_ulong len, int prot);
120 long target_mmap(target_ulong start, target_ulong len, int prot,
121 int flags, int fd, target_ulong offset);
122 int target_munmap(target_ulong start, target_ulong len);
123 long target_mremap(target_ulong old_addr, target_ulong old_size,
124 target_ulong new_size, unsigned long flags,
125 target_ulong new_addr);
126 int target_msync(target_ulong start, target_ulong len, int flags);
128 /* user access */
130 #define VERIFY_READ 0
131 #define VERIFY_WRITE 1
133 #define access_ok(type,addr,size) (1)
135 /* NOTE get_user and put_user use host addresses. */
136 #define __put_user(x,ptr)\
138 int size = sizeof(*ptr);\
139 switch(size) {\
140 case 1:\
141 *(uint8_t *)(ptr) = (typeof(*ptr))(x);\
142 break;\
143 case 2:\
144 *(uint16_t *)(ptr) = tswap16((typeof(*ptr))(x));\
145 break;\
146 case 4:\
147 *(uint32_t *)(ptr) = tswap32((typeof(*ptr))(x));\
148 break;\
149 case 8:\
150 *(uint64_t *)(ptr) = tswap64((typeof(*ptr))(x));\
151 break;\
152 default:\
153 abort();\
158 #define __get_user(x, ptr) \
160 int size = sizeof(*ptr);\
161 switch(size) {\
162 case 1:\
163 x = (typeof(*ptr))*(uint8_t *)(ptr);\
164 break;\
165 case 2:\
166 x = (typeof(*ptr))tswap16(*(uint16_t *)(ptr));\
167 break;\
168 case 4:\
169 x = (typeof(*ptr))tswap32(*(uint32_t *)(ptr));\
170 break;\
171 case 8:\
172 x = (typeof(*ptr))tswap64(*(uint64_t *)(ptr));\
173 break;\
174 default:\
175 abort();\
180 #define put_user(x,ptr)\
182 int __ret;\
183 if (access_ok(VERIFY_WRITE, ptr, sizeof(*ptr)))\
184 __ret = __put_user(x, ptr);\
185 else\
186 __ret = -EFAULT;\
187 __ret;\
190 #define get_user(x,ptr)\
192 int __ret;\
193 if (access_ok(VERIFY_READ, ptr, sizeof(*ptr)))\
194 __ret = __get_user(x, ptr);\
195 else\
196 __ret = -EFAULT;\
197 __ret;\
200 /* Functions for accessing guest memory. The tget and tput functions
201 read/write single values, byteswapping as neccessary. The lock_user
202 gets a pointer to a contiguous area of guest memory, but does not perform
203 and byteswapping. lock_user may return either a pointer to the guest
204 memory, or a temporary buffer. */
206 /* Lock an area of guest memory into the host. If copy is true then the
207 host area will have the same contents as the guest. */
208 static inline void *lock_user(target_ulong guest_addr, long len, int copy)
210 #ifdef DEBUG_REMAP
211 void *addr;
212 addr = malloc(len);
213 if (copy)
214 memcpy(addr, g2h(guest_addr), len);
215 else
216 memset(addr, 0, len);
217 return addr;
218 #else
219 return g2h(guest_addr);
220 #endif
223 /* Unlock an area of guest memory. The first LEN bytes must be flushed back
224 to guest memory. */
225 static inline void unlock_user(void *host_addr, target_ulong guest_addr,
226 long len)
228 #ifdef DEBUG_REMAP
229 if (host_addr == g2h(guest_addr))
230 return;
231 if (len > 0)
232 memcpy(g2h(guest_addr), host_addr, len);
233 free(host_addr);
234 #endif
237 /* Return the length of a string in target memory. */
238 static inline int target_strlen(target_ulong ptr)
240 return strlen(g2h(ptr));
243 /* Like lock_user but for null terminated strings. */
244 static inline void *lock_user_string(target_ulong guest_addr)
246 long len;
247 len = target_strlen(guest_addr) + 1;
248 return lock_user(guest_addr, len, 1);
251 /* Helper macros for locking/ulocking a target struct. */
252 #define lock_user_struct(host_ptr, guest_addr, copy) \
253 host_ptr = lock_user(guest_addr, sizeof(*host_ptr), copy)
254 #define unlock_user_struct(host_ptr, guest_addr, copy) \
255 unlock_user(host_ptr, guest_addr, (copy) ? sizeof(*host_ptr) : 0)
257 #define tget8(addr) ldub(addr)
258 #define tput8(addr, val) stb(addr, val)
259 #define tget16(addr) lduw(addr)
260 #define tput16(addr, val) stw(addr, val)
261 #define tget32(addr) ldl(addr)
262 #define tput32(addr, val) stl(addr, val)
263 #define tget64(addr) ldq(addr)
264 #define tput64(addr, val) stq(addr, val)
265 #if TARGET_LONG_BITS == 64
266 #define tgetl(addr) ldq(addr)
267 #define tputl(addr, val) stq(addr, val)
268 #else
269 #define tgetl(addr) ldl(addr)
270 #define tputl(addr, val) stl(addr, val)
271 #endif
273 #endif /* QEMU_H */