VirtualBox

source: vbox/trunk/src/recompiler/exec-all.h@ 37675

Last change on this file since 37675 was 37675, checked in by vboxsync, 14 years ago

rem: Synced with v0.12.5.

  • Property svn:eol-style set to native
File size: 13.7 KB
Line 
1/*
2 * internal execution defines for qemu
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
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.
10 *
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.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20/*
21 * Oracle LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
22 * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
23 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
24 * a choice of LGPL license versions is made available with the language indicating
25 * that LGPLv2 or any later version may be used, or where a choice of which version
26 * of the LGPL is applied is otherwise unspecified.
27 */
28
29#ifndef _EXEC_ALL_H_
30#define _EXEC_ALL_H_
31
32#include "qemu-common.h"
33
34/* allow to see translation results - the slowdown should be negligible, so we leave it */
35#ifndef VBOX
36#define DEBUG_DISAS
37#endif
38
39#ifdef VBOX
40# include <VBox/vmm/tm.h>
41# include <VBox/vmm/pgm.h> /* PGM_DYNAMIC_RAM_ALLOC */
42# ifndef LOG_GROUP
43# define LOG_GROUP LOG_GROUP_REM
44# endif
45# include <VBox/log.h>
46# include "REMInternal.h"
47# include <VBox/vmm/vm.h>
48#endif /* VBOX */
49
50/* is_jmp field values */
51#define DISAS_NEXT 0 /* next instruction can be analyzed */
52#define DISAS_JUMP 1 /* only pc was modified dynamically */
53#define DISAS_UPDATE 2 /* cpu state was modified dynamically */
54#define DISAS_TB_JUMP 3 /* only pc was modified statically */
55
56typedef struct TranslationBlock TranslationBlock;
57
58/* XXX: make safe guess about sizes */
59#define MAX_OP_PER_INSTR 96
60/* A Call op needs up to 6 + 2N parameters (N = number of arguments). */
61#define MAX_OPC_PARAM 10
62#define OPC_BUF_SIZE 640
63#define OPC_MAX_SIZE (OPC_BUF_SIZE - MAX_OP_PER_INSTR)
64
65/* Maximum size a TCG op can expand to. This is complicated because a
66 single op may require several host instructions and register reloads.
67 For now take a wild guess at 192 bytes, which should allow at least
68 a couple of fixup instructions per argument. */
69#define TCG_MAX_OP_SIZE 192
70
71#define OPPARAM_BUF_SIZE (OPC_BUF_SIZE * MAX_OPC_PARAM)
72
73extern target_ulong gen_opc_pc[OPC_BUF_SIZE];
74extern target_ulong gen_opc_npc[OPC_BUF_SIZE];
75extern uint8_t gen_opc_cc_op[OPC_BUF_SIZE];
76extern uint8_t gen_opc_instr_start[OPC_BUF_SIZE];
77extern uint16_t gen_opc_icount[OPC_BUF_SIZE];
78extern target_ulong gen_opc_jump_pc[2];
79extern uint32_t gen_opc_hflags[OPC_BUF_SIZE];
80
81#include "qemu-log.h"
82
83void gen_intermediate_code(CPUState *env, struct TranslationBlock *tb);
84void gen_intermediate_code_pc(CPUState *env, struct TranslationBlock *tb);
85void gen_pc_load(CPUState *env, struct TranslationBlock *tb,
86 unsigned long searched_pc, int pc_pos, void *puc);
87
88unsigned long code_gen_max_block_size(void);
89void cpu_gen_init(void);
90int cpu_gen_code(CPUState *env, struct TranslationBlock *tb,
91 int *gen_code_size_ptr);
92int cpu_restore_state(struct TranslationBlock *tb,
93 CPUState *env, unsigned long searched_pc,
94 void *puc);
95int cpu_restore_state_copy(struct TranslationBlock *tb,
96 CPUState *env, unsigned long searched_pc,
97 void *puc);
98void cpu_resume_from_signal(CPUState *env1, void *puc);
99void cpu_io_recompile(CPUState *env, void *retaddr);
100TranslationBlock *tb_gen_code(CPUState *env,
101 target_ulong pc, target_ulong cs_base, int flags,
102 int cflags);
103void cpu_exec_init(CPUState *env);
104void QEMU_NORETURN cpu_loop_exit(void);
105int page_unprotect(target_ulong address, unsigned long pc, void *puc);
106void tb_invalidate_phys_page_range(target_phys_addr_t start, target_phys_addr_t end,
107 int is_cpu_write_access);
108void tb_invalidate_page_range(target_ulong start, target_ulong end);
109void tlb_flush_page(CPUState *env, target_ulong addr);
110void tlb_flush(CPUState *env, int flush_global);
111int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
112 target_phys_addr_t paddr, int prot,
113 int mmu_idx, int is_softmmu);
114static inline int tlb_set_page(CPUState *env1, target_ulong vaddr,
115 target_phys_addr_t paddr, int prot,
116 int mmu_idx, int is_softmmu)
117{
118 if (prot & PAGE_READ)
119 prot |= PAGE_EXEC;
120 return tlb_set_page_exec(env1, vaddr, paddr, prot, mmu_idx, is_softmmu);
121}
122
123#define CODE_GEN_ALIGN 16 /* must be >= of the size of a icache line */
124
125#define CODE_GEN_PHYS_HASH_BITS 15
126#define CODE_GEN_PHYS_HASH_SIZE (1 << CODE_GEN_PHYS_HASH_BITS)
127
128#define MIN_CODE_GEN_BUFFER_SIZE (1024 * 1024)
129
130/* estimated block size for TB allocation */
131/* XXX: use a per code average code fragment size and modulate it
132 according to the host CPU */
133#if defined(CONFIG_SOFTMMU)
134#define CODE_GEN_AVG_BLOCK_SIZE 128
135#else
136#define CODE_GEN_AVG_BLOCK_SIZE 64
137#endif
138
139#if defined(_ARCH_PPC) || defined(__x86_64__) || defined(__arm__) || defined(__i386__)
140#define USE_DIRECT_JUMP
141#endif
142
143#ifdef VBOX /* bird: not safe in next step because of threading & cpu_interrupt. */
144#undef USE_DIRECT_JUMP
145#endif /* VBOX */
146
147struct TranslationBlock {
148 target_ulong pc; /* simulated PC corresponding to this block (EIP + CS base) */
149 target_ulong cs_base; /* CS base for this block */
150 uint64_t flags; /* flags defining in which context the code was generated */
151 uint16_t size; /* size of target code for this block (1 <=
152 size <= TARGET_PAGE_SIZE) */
153 uint16_t cflags; /* compile flags */
154#define CF_COUNT_MASK 0x7fff
155#define CF_LAST_IO 0x8000 /* Last insn may be an IO access. */
156
157#ifdef VBOX
158#define CF_RAW_MODE 0x0010 /* block was generated in raw mode */
159#endif
160
161 uint8_t *tc_ptr; /* pointer to the translated code */
162 /* next matching tb for physical address. */
163 struct TranslationBlock *phys_hash_next;
164 /* first and second physical page containing code. The lower bit
165 of the pointer tells the index in page_next[] */
166 struct TranslationBlock *page_next[2];
167 target_ulong page_addr[2];
168
169 /* the following data are used to directly call another TB from
170 the code of this one. */
171 uint16_t tb_next_offset[2]; /* offset of original jump target */
172#ifdef USE_DIRECT_JUMP
173 uint16_t tb_jmp_offset[4]; /* offset of jump instruction */
174#else
175 unsigned long tb_next[2]; /* address of jump generated code */
176#endif
177 /* list of TBs jumping to this one. This is a circular list using
178 the two least significant bits of the pointers to tell what is
179 the next pointer: 0 = jmp_next[0], 1 = jmp_next[1], 2 =
180 jmp_first */
181 struct TranslationBlock *jmp_next[2];
182 struct TranslationBlock *jmp_first;
183 uint32_t icount;
184};
185
186static inline unsigned int tb_jmp_cache_hash_page(target_ulong pc)
187{
188 target_ulong tmp;
189 tmp = pc ^ (pc >> (TARGET_PAGE_BITS - TB_JMP_PAGE_BITS));
190 return (tmp >> (TARGET_PAGE_BITS - TB_JMP_PAGE_BITS)) & TB_JMP_PAGE_MASK;
191}
192
193static inline unsigned int tb_jmp_cache_hash_func(target_ulong pc)
194{
195 target_ulong tmp;
196 tmp = pc ^ (pc >> (TARGET_PAGE_BITS - TB_JMP_PAGE_BITS));
197 return (((tmp >> (TARGET_PAGE_BITS - TB_JMP_PAGE_BITS)) & TB_JMP_PAGE_MASK)
198 | (tmp & TB_JMP_ADDR_MASK));
199}
200
201static inline unsigned int tb_phys_hash_func(unsigned long pc)
202{
203 return pc & (CODE_GEN_PHYS_HASH_SIZE - 1);
204}
205
206TranslationBlock *tb_alloc(target_ulong pc);
207void tb_free(TranslationBlock *tb);
208void tb_flush(CPUState *env);
209void tb_link_phys(TranslationBlock *tb,
210 target_ulong phys_pc, target_ulong phys_page2);
211void tb_phys_invalidate(TranslationBlock *tb, target_ulong page_addr);
212
213extern TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE];
214extern uint8_t *code_gen_ptr;
215extern int code_gen_max_blocks;
216
217#if defined(USE_DIRECT_JUMP)
218
219#if defined(_ARCH_PPC)
220extern void ppc_tb_set_jmp_target(unsigned long jmp_addr, unsigned long addr);
221#define tb_set_jmp_target1 ppc_tb_set_jmp_target
222#elif defined(__i386__) || defined(__x86_64__)
223static inline void tb_set_jmp_target1(unsigned long jmp_addr, unsigned long addr)
224{
225 /* patch the branch destination */
226 *(uint32_t *)jmp_addr = addr - (jmp_addr + 4);
227 /* no need to flush icache explicitly */
228}
229#elif defined(__arm__)
230static inline void tb_set_jmp_target1(unsigned long jmp_addr, unsigned long addr)
231{
232#if QEMU_GNUC_PREREQ(4, 1)
233 void __clear_cache(char *beg, char *end);
234#else
235 register unsigned long _beg __asm ("a1");
236 register unsigned long _end __asm ("a2");
237 register unsigned long _flg __asm ("a3");
238#endif
239
240 /* we could use a ldr pc, [pc, #-4] kind of branch and avoid the flush */
241 *(uint32_t *)jmp_addr =
242 (*(uint32_t *)jmp_addr & ~0xffffff)
243 | (((addr - (jmp_addr + 8)) >> 2) & 0xffffff);
244
245#if QEMU_GNUC_PREREQ(4, 1)
246 __clear_cache((char *) jmp_addr, (char *) jmp_addr + 4);
247#else
248 /* flush icache */
249 _beg = jmp_addr;
250 _end = jmp_addr + 4;
251 _flg = 0;
252 __asm __volatile__ ("swi 0x9f0002" : : "r" (_beg), "r" (_end), "r" (_flg));
253#endif
254}
255#endif
256
257static inline void tb_set_jmp_target(TranslationBlock *tb,
258 int n, unsigned long addr)
259{
260 unsigned long offset;
261
262 offset = tb->tb_jmp_offset[n];
263 tb_set_jmp_target1((unsigned long)(tb->tc_ptr + offset), addr);
264 offset = tb->tb_jmp_offset[n + 2];
265 if (offset != 0xffff)
266 tb_set_jmp_target1((unsigned long)(tb->tc_ptr + offset), addr);
267}
268
269#else
270
271/* set the jump target */
272static inline void tb_set_jmp_target(TranslationBlock *tb,
273 int n, unsigned long addr)
274{
275 tb->tb_next[n] = addr;
276}
277
278#endif
279
280static inline void tb_add_jump(TranslationBlock *tb, int n,
281 TranslationBlock *tb_next)
282{
283 /* NOTE: this test is only needed for thread safety */
284 if (!tb->jmp_next[n]) {
285 /* patch the native jump address */
286 tb_set_jmp_target(tb, n, (unsigned long)tb_next->tc_ptr);
287
288 /* add in TB jmp circular list */
289 tb->jmp_next[n] = tb_next->jmp_first;
290 tb_next->jmp_first = (TranslationBlock *)((long)(tb) | (n));
291 }
292}
293
294TranslationBlock *tb_find_pc(unsigned long pc_ptr);
295
296extern CPUWriteMemoryFunc *io_mem_write[IO_MEM_NB_ENTRIES][4];
297extern CPUReadMemoryFunc *io_mem_read[IO_MEM_NB_ENTRIES][4];
298extern void *io_mem_opaque[IO_MEM_NB_ENTRIES];
299
300#include "qemu-lock.h"
301
302extern spinlock_t tb_lock;
303
304extern int tb_invalidated_flag;
305
306#if !defined(CONFIG_USER_ONLY)
307
308void tlb_fill(target_ulong addr, int is_write, int mmu_idx,
309 void *retaddr);
310
311#include "softmmu_defs.h"
312
313#define ACCESS_TYPE (NB_MMU_MODES + 1)
314#define MEMSUFFIX _code
315#define env cpu_single_env
316
317#define DATA_SIZE 1
318#include "softmmu_header.h"
319
320#define DATA_SIZE 2
321#include "softmmu_header.h"
322
323#define DATA_SIZE 4
324#include "softmmu_header.h"
325
326#define DATA_SIZE 8
327#include "softmmu_header.h"
328
329#undef ACCESS_TYPE
330#undef MEMSUFFIX
331#undef env
332
333#endif
334
335#if defined(CONFIG_USER_ONLY)
336static inline target_ulong get_phys_addr_code(CPUState *env1, target_ulong addr)
337{
338 return addr;
339}
340#else
341# ifdef VBOX
342target_ulong remR3PhysGetPhysicalAddressCode(CPUState *env, target_ulong addr, CPUTLBEntry *pTLBEntry, target_phys_addr_t ioTLBEntry);
343# endif
344/* NOTE: this function can trigger an exception */
345/* NOTE2: the returned address is not exactly the physical address: it
346 is the offset relative to phys_ram_base */
347static inline target_ulong get_phys_addr_code(CPUState *env1, target_ulong addr)
348{
349 int mmu_idx, page_index, pd;
350 void *p;
351
352 page_index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
353 mmu_idx = cpu_mmu_index(env1);
354 if (unlikely(env1->tlb_table[mmu_idx][page_index].addr_code !=
355 (addr & TARGET_PAGE_MASK))) {
356 ldub_code(addr);
357 }
358 pd = env1->tlb_table[mmu_idx][page_index].addr_code & ~TARGET_PAGE_MASK;
359 if (pd > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) {
360# ifdef VBOX
361 /* deal with non-MMIO access handlers. */
362 return remR3PhysGetPhysicalAddressCode(env1, addr,
363 &env1->tlb_table[mmu_idx][page_index],
364 env1->iotlb[mmu_idx][page_index]);
365# elif defined(TARGET_SPARC) || defined(TARGET_MIPS)
366 do_unassigned_access(addr, 0, 1, 0, 4);
367#else
368 cpu_abort(env1, "Trying to execute code outside RAM or ROM at 0x" TARGET_FMT_lx "\n", addr);
369#endif
370 }
371
372# if defined(VBOX) && defined(REM_PHYS_ADDR_IN_TLB)
373 return addr + env1->tlb_table[mmu_idx][page_index].addend;
374# elif defined(VBOX)
375 Assert(env1->phys_addends[mmu_idx][page_index] != -1);
376 return addr + env1->phys_addends[mmu_idx][page_index];
377# else
378 p = (void *)(unsigned long)addr
379 + env1->tlb_table[mmu_idx][page_index].addend;
380 return qemu_ram_addr_from_host(p);
381# endif
382}
383
384/* Deterministic execution requires that IO only be performed on the last
385 instruction of a TB so that interrupts take effect immediately. */
386static inline int can_do_io(CPUState *env)
387{
388 if (!use_icount)
389 return 1;
390
391 /* If not executing code then assume we are ok. */
392 if (!env->current_tb)
393 return 1;
394
395 return env->can_do_io != 0;
396}
397#endif
398
399typedef void (CPUDebugExcpHandler)(CPUState *env);
400
401CPUDebugExcpHandler *cpu_set_debug_excp_handler(CPUDebugExcpHandler *handler);
402
403/* vl.c */
404#ifndef VBOX
405extern int singlestep;
406#endif /*!VBOX*/
407
408#endif
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette