]> git.proxmox.com Git - qemu.git/blame - exec-i386.c
added getrusage
[qemu.git] / exec-i386.c
CommitLineData
7d13299d
FB
1/*
2 * i386 emulator main execution loop
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
3ef693a0
FB
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
7d13299d 10 *
3ef693a0
FB
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
7d13299d 15 *
3ef693a0
FB
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
7d13299d
FB
19 */
20#include "exec-i386.h"
956034d7 21#include "disas.h"
7d13299d 22
dc99065b 23//#define DEBUG_EXEC
9de5e440 24//#define DEBUG_SIGNAL
7d13299d
FB
25
26/* main execution loop */
27
1b6b029e
FB
28/* thread support */
29
25eb4484 30spinlock_t global_cpu_lock = SPIN_LOCK_UNLOCKED;
1b6b029e
FB
31
32void cpu_lock(void)
33{
25eb4484 34 spin_lock(&global_cpu_lock);
1b6b029e
FB
35}
36
37void cpu_unlock(void)
38{
25eb4484 39 spin_unlock(&global_cpu_lock);
1b6b029e
FB
40}
41
9de5e440
FB
42/* exception support */
43/* NOTE: not static to force relocation generation by GCC */
b56dad1c 44void raise_exception_err(int exception_index, int error_code)
9de5e440
FB
45{
46 /* NOTE: the register at this point must be saved by hand because
47 longjmp restore them */
ae228531
FB
48#ifdef __sparc__
49 /* We have to stay in the same register window as our caller,
50 * thus this trick.
51 */
52 __asm__ __volatile__("restore\n\t"
53 "mov\t%o0, %i0");
54#endif
9de5e440
FB
55#ifdef reg_EAX
56 env->regs[R_EAX] = EAX;
57#endif
58#ifdef reg_ECX
59 env->regs[R_ECX] = ECX;
60#endif
61#ifdef reg_EDX
62 env->regs[R_EDX] = EDX;
63#endif
64#ifdef reg_EBX
65 env->regs[R_EBX] = EBX;
66#endif
67#ifdef reg_ESP
68 env->regs[R_ESP] = ESP;
69#endif
70#ifdef reg_EBP
71 env->regs[R_EBP] = EBP;
72#endif
73#ifdef reg_ESI
74 env->regs[R_ESI] = ESI;
75#endif
76#ifdef reg_EDI
77 env->regs[R_EDI] = EDI;
78#endif
79 env->exception_index = exception_index;
b56dad1c 80 env->error_code = error_code;
9de5e440
FB
81 longjmp(env->jmp_env, 1);
82}
83
b56dad1c
FB
84/* short cut if error_code is 0 or not present */
85void raise_exception(int exception_index)
86{
87 raise_exception_err(exception_index, 0);
88}
89
7d13299d
FB
90int cpu_x86_exec(CPUX86State *env1)
91{
92 int saved_T0, saved_T1, saved_A0;
93 CPUX86State *saved_env;
04369ff2
FB
94#ifdef reg_EAX
95 int saved_EAX;
96#endif
97#ifdef reg_ECX
98 int saved_ECX;
99#endif
100#ifdef reg_EDX
101 int saved_EDX;
102#endif
103#ifdef reg_EBX
104 int saved_EBX;
105#endif
106#ifdef reg_ESP
107 int saved_ESP;
108#endif
109#ifdef reg_EBP
110 int saved_EBP;
111#endif
112#ifdef reg_ESI
113 int saved_ESI;
114#endif
115#ifdef reg_EDI
116 int saved_EDI;
117#endif
fd6ce8f6 118 int code_gen_size, ret, code_size;
7d13299d 119 void (*gen_func)(void);
9de5e440 120 TranslationBlock *tb, **ptb;
dab2ed99 121 uint8_t *tc_ptr, *cs_base, *pc;
6dbad63e
FB
122 unsigned int flags;
123
7d13299d
FB
124 /* first we save global registers */
125 saved_T0 = T0;
126 saved_T1 = T1;
127 saved_A0 = A0;
128 saved_env = env;
129 env = env1;
04369ff2
FB
130#ifdef reg_EAX
131 saved_EAX = EAX;
132 EAX = env->regs[R_EAX];
133#endif
134#ifdef reg_ECX
135 saved_ECX = ECX;
136 ECX = env->regs[R_ECX];
137#endif
138#ifdef reg_EDX
139 saved_EDX = EDX;
140 EDX = env->regs[R_EDX];
141#endif
142#ifdef reg_EBX
143 saved_EBX = EBX;
144 EBX = env->regs[R_EBX];
145#endif
146#ifdef reg_ESP
147 saved_ESP = ESP;
148 ESP = env->regs[R_ESP];
149#endif
150#ifdef reg_EBP
151 saved_EBP = EBP;
152 EBP = env->regs[R_EBP];
153#endif
154#ifdef reg_ESI
155 saved_ESI = ESI;
156 ESI = env->regs[R_ESI];
157#endif
158#ifdef reg_EDI
159 saved_EDI = EDI;
160 EDI = env->regs[R_EDI];
161#endif
7d13299d 162
9de5e440 163 /* put eflags in CPU temporary format */
fc2b4c48
FB
164 CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
165 DF = 1 - (2 * ((env->eflags >> 10) & 1));
9de5e440 166 CC_OP = CC_OP_EFLAGS;
fc2b4c48 167 env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
9de5e440 168 env->interrupt_request = 0;
9d27abd9 169
7d13299d
FB
170 /* prepare setjmp context for exception handling */
171 if (setjmp(env->jmp_env) == 0) {
172 for(;;) {
9de5e440
FB
173 if (env->interrupt_request) {
174 raise_exception(EXCP_INTERRUPT);
175 }
7d13299d
FB
176#ifdef DEBUG_EXEC
177 if (loglevel) {
9d27abd9
FB
178 /* XXX: save all volatile state in cpu state */
179 /* restore flags in standard format */
180 env->regs[R_EAX] = EAX;
181 env->regs[R_EBX] = EBX;
182 env->regs[R_ECX] = ECX;
183 env->regs[R_EDX] = EDX;
184 env->regs[R_ESI] = ESI;
185 env->regs[R_EDI] = EDI;
186 env->regs[R_EBP] = EBP;
187 env->regs[R_ESP] = ESP;
188 env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
189 cpu_x86_dump_state(env, logfile, 0);
190 env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
7d13299d
FB
191 }
192#endif
6dbad63e
FB
193 /* we compute the CPU state. We assume it will not
194 change during the whole generated block. */
195 flags = env->seg_cache[R_CS].seg_32bit << GEN_FLAG_CODE32_SHIFT;
dab2ed99 196 flags |= env->seg_cache[R_SS].seg_32bit << GEN_FLAG_SS32_SHIFT;
6dbad63e
FB
197 flags |= (((unsigned long)env->seg_cache[R_DS].base |
198 (unsigned long)env->seg_cache[R_ES].base |
199 (unsigned long)env->seg_cache[R_SS].base) != 0) <<
200 GEN_FLAG_ADDSEG_SHIFT;
9d27abd9
FB
201 if (!(env->eflags & VM_MASK)) {
202 flags |= (env->segs[R_CS] & 3) << GEN_FLAG_CPL_SHIFT;
203 } else {
204 /* NOTE: a dummy CPL is kept */
205 flags |= (1 << GEN_FLAG_VM_SHIFT);
206 flags |= (3 << GEN_FLAG_CPL_SHIFT);
207 }
b56dad1c 208 flags |= (env->eflags & IOPL_MASK) >> (12 - GEN_FLAG_IOPL_SHIFT);
cabb4d61 209 flags |= (env->eflags & TF_MASK) << (GEN_FLAG_TF_SHIFT - 8);
dab2ed99
FB
210 cs_base = env->seg_cache[R_CS].base;
211 pc = cs_base + env->eip;
9de5e440
FB
212 tb = tb_find(&ptb, (unsigned long)pc, (unsigned long)cs_base,
213 flags);
214 if (!tb) {
7d13299d 215 /* if no translated code available, then translate it now */
25eb4484
FB
216 /* very inefficient but safe: we lock all the cpus
217 when generating code */
218 spin_lock(&tb_lock);
7d13299d 219 tc_ptr = code_gen_ptr;
9de5e440 220 ret = cpu_x86_gen_code(code_gen_ptr, CODE_GEN_MAX_SIZE,
fd6ce8f6
FB
221 &code_gen_size, pc, cs_base, flags,
222 &code_size);
9de5e440
FB
223 /* if invalid instruction, signal it */
224 if (ret != 0) {
25eb4484 225 spin_unlock(&tb_lock);
9de5e440
FB
226 raise_exception(EXCP06_ILLOP);
227 }
fd6ce8f6 228 tb = tb_alloc((unsigned long)pc, code_size);
9de5e440 229 *ptb = tb;
9de5e440
FB
230 tb->cs_base = (unsigned long)cs_base;
231 tb->flags = flags;
7d13299d 232 tb->tc_ptr = tc_ptr;
9de5e440 233 tb->hash_next = NULL;
7d13299d 234 code_gen_ptr = (void *)(((unsigned long)code_gen_ptr + code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1));
25eb4484 235 spin_unlock(&tb_lock);
7d13299d 236 }
9d27abd9 237#ifdef DEBUG_EXEC
956034d7
FB
238 if (loglevel) {
239 fprintf(logfile, "Trace 0x%08lx [0x%08lx] %s\n",
240 (long)tb->tc_ptr, (long)tb->pc,
241 lookup_symbol((void *)tb->pc));
956034d7 242 }
9d27abd9 243#endif
7d13299d 244 /* execute the generated code */
9de5e440 245 tc_ptr = tb->tc_ptr;
7d13299d 246 gen_func = (void *)tc_ptr;
ae228531
FB
247#ifdef __sparc__
248 __asm__ __volatile__("call %0\n\t"
249 " mov %%o7,%%i0"
250 : /* no outputs */
251 : "r" (gen_func)
252 : "i0", "i1", "i2", "i3", "i4", "i5");
253#else
7d13299d 254 gen_func();
ae228531 255#endif
7d13299d
FB
256 }
257 }
258 ret = env->exception_index;
259
9de5e440 260 /* restore flags in standard format */
fc2b4c48 261 env->eflags = env->eflags | cc_table[CC_OP].compute_all() | (DF & DF_MASK);
9de5e440 262
7d13299d 263 /* restore global registers */
04369ff2
FB
264#ifdef reg_EAX
265 EAX = saved_EAX;
266#endif
267#ifdef reg_ECX
268 ECX = saved_ECX;
269#endif
270#ifdef reg_EDX
271 EDX = saved_EDX;
272#endif
273#ifdef reg_EBX
274 EBX = saved_EBX;
275#endif
276#ifdef reg_ESP
277 ESP = saved_ESP;
278#endif
279#ifdef reg_EBP
280 EBP = saved_EBP;
281#endif
282#ifdef reg_ESI
283 ESI = saved_ESI;
284#endif
285#ifdef reg_EDI
286 EDI = saved_EDI;
287#endif
7d13299d
FB
288 T0 = saved_T0;
289 T1 = saved_T1;
290 A0 = saved_A0;
291 env = saved_env;
292 return ret;
293}
6dbad63e 294
9de5e440
FB
295void cpu_x86_interrupt(CPUX86State *s)
296{
297 s->interrupt_request = 1;
298}
299
300
6dbad63e
FB
301void cpu_x86_load_seg(CPUX86State *s, int seg_reg, int selector)
302{
303 CPUX86State *saved_env;
304
305 saved_env = env;
306 env = s;
307 load_seg(seg_reg, selector);
308 env = saved_env;
309}
9de5e440
FB
310
311#undef EAX
312#undef ECX
313#undef EDX
314#undef EBX
315#undef ESP
316#undef EBP
317#undef ESI
318#undef EDI
319#undef EIP
320#include <signal.h>
321#include <sys/ucontext.h>
322
b56dad1c 323/* 'pc' is the host PC at which the exception was raised. 'address' is
fd6ce8f6
FB
324 the effective address of the memory exception. 'is_write' is 1 if a
325 write caused the exception and otherwise 0'. 'old_set' is the
326 signal set which should be restored */
2b413144
FB
327static inline int handle_cpu_signal(unsigned long pc, unsigned long address,
328 int is_write, sigset_t *old_set)
9de5e440 329{
fd6ce8f6
FB
330#if defined(DEBUG_SIGNAL)
331 printf("qemu: SIGSEGV pc=0x%08lx address=%08lx wr=%d oldset=0x%08lx\n",
332 pc, address, is_write, *(unsigned long *)old_set);
9de5e440 333#endif
25eb4484 334 /* XXX: locking issue */
fd6ce8f6 335 if (is_write && page_unprotect(address)) {
fd6ce8f6
FB
336 return 1;
337 }
9de5e440
FB
338 if (pc >= (unsigned long)code_gen_buffer &&
339 pc < (unsigned long)code_gen_buffer + CODE_GEN_BUFFER_SIZE) {
340 /* the PC is inside the translated code. It means that we have
341 a virtual CPU fault */
342 /* we restore the process signal mask as the sigreturn should
343 do it */
344 sigprocmask(SIG_SETMASK, old_set, NULL);
345 /* XXX: need to compute virtual pc position by retranslating
346 code. The rest of the CPU state should be correct. */
b56dad1c 347 env->cr2 = address;
fd6ce8f6 348 raise_exception_err(EXCP0E_PAGE, 4 | (is_write << 1));
9de5e440
FB
349 /* never comes here */
350 return 1;
351 } else {
352 return 0;
353 }
354}
355
2b413144
FB
356#if defined(__i386__)
357
9de5e440
FB
358int cpu_x86_signal_handler(int host_signum, struct siginfo *info,
359 void *puc)
360{
9de5e440
FB
361 struct ucontext *uc = puc;
362 unsigned long pc;
9de5e440 363
d691f669
FB
364#ifndef REG_EIP
365/* for glibc 2.1 */
fd6ce8f6
FB
366#define REG_EIP EIP
367#define REG_ERR ERR
368#define REG_TRAPNO TRAPNO
d691f669 369#endif
fc2b4c48 370 pc = uc->uc_mcontext.gregs[REG_EIP];
fd6ce8f6
FB
371 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
372 uc->uc_mcontext.gregs[REG_TRAPNO] == 0xe ?
373 (uc->uc_mcontext.gregs[REG_ERR] >> 1) & 1 : 0,
2b413144
FB
374 &uc->uc_sigmask);
375}
376
25eb4484 377#elif defined(__powerpc)
2b413144
FB
378
379int cpu_x86_signal_handler(int host_signum, struct siginfo *info,
380 void *puc)
381{
25eb4484
FB
382 struct ucontext *uc = puc;
383 struct pt_regs *regs = uc->uc_mcontext.regs;
384 unsigned long pc;
25eb4484
FB
385 int is_write;
386
387 pc = regs->nip;
25eb4484
FB
388 is_write = 0;
389#if 0
390 /* ppc 4xx case */
391 if (regs->dsisr & 0x00800000)
392 is_write = 1;
393#else
394 if (regs->trap != 0x400 && (regs->dsisr & 0x02000000))
395 is_write = 1;
396#endif
397 return handle_cpu_signal(pc, (unsigned long)info->si_addr,
2b413144
FB
398 is_write, &uc->uc_sigmask);
399}
400
9de5e440 401#else
2b413144 402
25eb4484 403#error CPU specific signal handler needed
2b413144 404
9de5e440 405#endif