]> git.proxmox.com Git - qemu.git/blame - exec.h
direct chaining for PowerPC and i386
[qemu.git] / exec.h
CommitLineData
d4e8164f
FB
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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#define GEN_FLAG_CODE32_SHIFT 0
22#define GEN_FLAG_ADDSEG_SHIFT 1
23#define GEN_FLAG_SS32_SHIFT 2
24#define GEN_FLAG_VM_SHIFT 3
25#define GEN_FLAG_ST_SHIFT 4
26#define GEN_FLAG_CPL_SHIFT 7
27#define GEN_FLAG_IOPL_SHIFT 9
28#define GEN_FLAG_TF_SHIFT 11
29
30struct TranslationBlock;
31int cpu_x86_gen_code(uint8_t *gen_code_buf, int max_code_size,
32 int *gen_code_size_ptr,
33 uint8_t *pc_start, uint8_t *cs_base, int flags,
34 int *code_size_ptr, struct TranslationBlock *tb);
35void cpu_x86_tblocks_init(void);
36void page_init(void);
37int page_unprotect(unsigned long address);
38
39#define CODE_GEN_MAX_SIZE 65536
40#define CODE_GEN_ALIGN 16 /* must be >= of the size of a icache line */
41
42#define CODE_GEN_HASH_BITS 15
43#define CODE_GEN_HASH_SIZE (1 << CODE_GEN_HASH_BITS)
44
45/* maximum total translate dcode allocated */
46#define CODE_GEN_BUFFER_SIZE (2048 * 1024)
47//#define CODE_GEN_BUFFER_SIZE (128 * 1024)
48
49#if defined(__powerpc__)
50#define USE_DIRECT_JUMP
51#endif
52
53typedef struct TranslationBlock {
54 unsigned long pc; /* simulated PC corresponding to this block (EIP + CS base) */
55 unsigned long cs_base; /* CS base for this block */
56 unsigned int flags; /* flags defining in which context the code was generated */
57 uint16_t size; /* size of target code for this block (1 <=
58 size <= TARGET_PAGE_SIZE) */
59 uint8_t *tc_ptr; /* pointer to the translated code */
60 struct TranslationBlock *hash_next; /* next matching block */
61 struct TranslationBlock *page_next[2]; /* next blocks in even/odd page */
62 /* the following data are used to directly call another TB from
63 the code of this one. */
64 uint16_t tb_next_offset[2]; /* offset of original jump target */
65#ifdef USE_DIRECT_JUMP
66 uint16_t tb_jmp_offset[2]; /* offset of jump instruction */
67#else
68 uint8_t *tb_next[2]; /* address of jump generated code */
69#endif
70 /* list of TBs jumping to this one. This is a circular list using
71 the two least significant bits of the pointers to tell what is
72 the next pointer: 0 = jmp_next[0], 1 = jmp_next[1], 2 =
73 jmp_first */
74 struct TranslationBlock *jmp_next[2];
75 struct TranslationBlock *jmp_first;
76} TranslationBlock;
77
78static inline unsigned int tb_hash_func(unsigned long pc)
79{
80 return pc & (CODE_GEN_HASH_SIZE - 1);
81}
82
83TranslationBlock *tb_alloc(unsigned long pc);
84void tb_flush(void);
85void tb_link(TranslationBlock *tb);
86
87extern TranslationBlock *tb_hash[CODE_GEN_HASH_SIZE];
88
89extern uint8_t code_gen_buffer[CODE_GEN_BUFFER_SIZE];
90extern uint8_t *code_gen_ptr;
91
92/* find a translation block in the translation cache. If not found,
93 return NULL and the pointer to the last element of the list in pptb */
94static inline TranslationBlock *tb_find(TranslationBlock ***pptb,
95 unsigned long pc,
96 unsigned long cs_base,
97 unsigned int flags)
98{
99 TranslationBlock **ptb, *tb;
100 unsigned int h;
101
102 h = tb_hash_func(pc);
103 ptb = &tb_hash[h];
104 for(;;) {
105 tb = *ptb;
106 if (!tb)
107 break;
108 if (tb->pc == pc && tb->cs_base == cs_base && tb->flags == flags)
109 return tb;
110 ptb = &tb->hash_next;
111 }
112 *pptb = ptb;
113 return NULL;
114}
115
116#if defined(__powerpc__)
117
118static inline void tb_set_jmp_target(TranslationBlock *tb,
119 int n, unsigned long addr)
120{
121 uint32_t val, *ptr;
122 unsigned long offset;
123
124 offset = (unsigned long)(tb->tc_ptr + tb->tb_jmp_offset[n]);
125
126 /* patch the branch destination */
127 ptr = (uint32_t *)offset;
128 val = *ptr;
129 val = (val & ~0x03fffffc) | ((addr - offset) & 0x03fffffc);
130 *ptr = val;
131 /* flush icache */
132 asm volatile ("dcbst 0,%0" : : "r"(ptr) : "memory");
133 asm volatile ("sync" : : : "memory");
134 asm volatile ("icbi 0,%0" : : "r"(ptr) : "memory");
135 asm volatile ("sync" : : : "memory");
136 asm volatile ("isync" : : : "memory");
137}
138
139#else
140
141/* set the jump target */
142static inline void tb_set_jmp_target(TranslationBlock *tb,
143 int n, unsigned long addr)
144{
145 tb->tb_next[n] = (void *)addr;
146}
147
148#endif
149
150static inline void tb_add_jump(TranslationBlock *tb, int n,
151 TranslationBlock *tb_next)
152{
153 /* patch the native jump address */
154 tb_set_jmp_target(tb, n, (unsigned long)tb_next->tc_ptr);
155
156 /* add in TB jmp circular list */
157 tb->jmp_next[n] = tb_next->jmp_first;
158 tb_next->jmp_first = (TranslationBlock *)((long)(tb) | (n));
159}
160
161#ifndef offsetof
162#define offsetof(type, field) ((size_t) &((type *)0)->field)
163#endif
164
165#ifdef __powerpc__
166static inline int testandset (int *p)
167{
168 int ret;
169 __asm__ __volatile__ (
170 "0: lwarx %0,0,%1 ;"
171 " xor. %0,%3,%0;"
172 " bne 1f;"
173 " stwcx. %2,0,%1;"
174 " bne- 0b;"
175 "1: "
176 : "=&r" (ret)
177 : "r" (p), "r" (1), "r" (0)
178 : "cr0", "memory");
179 return ret;
180}
181#endif
182
183#ifdef __i386__
184static inline int testandset (int *p)
185{
186 char ret;
187 long int readval;
188
189 __asm__ __volatile__ ("lock; cmpxchgl %3, %1; sete %0"
190 : "=q" (ret), "=m" (*p), "=a" (readval)
191 : "r" (1), "m" (*p), "a" (0)
192 : "memory");
193 return ret;
194}
195#endif
196
197#ifdef __s390__
198static inline int testandset (int *p)
199{
200 int ret;
201
202 __asm__ __volatile__ ("0: cs %0,%1,0(%2)\n"
203 " jl 0b"
204 : "=&d" (ret)
205 : "r" (1), "a" (p), "0" (*p)
206 : "cc", "memory" );
207 return ret;
208}
209#endif
210
211#ifdef __alpha__
212int testandset (int *p)
213{
214 int ret;
215 unsigned long one;
216
217 __asm__ __volatile__ ("0: mov 1,%2\n"
218 " ldl_l %0,%1\n"
219 " stl_c %2,%1\n"
220 " beq %2,1f\n"
221 ".subsection 2\n"
222 "1: br 0b\n"
223 ".previous"
224 : "=r" (ret), "=m" (*p), "=r" (one)
225 : "m" (*p));
226 return ret;
227}
228#endif
229
230#ifdef __sparc__
231static inline int testandset (int *p)
232{
233 int ret;
234
235 __asm__ __volatile__("ldstub [%1], %0"
236 : "=r" (ret)
237 : "r" (p)
238 : "memory");
239
240 return (ret ? 1 : 0);
241}
242#endif
243
244typedef int spinlock_t;
245
246#define SPIN_LOCK_UNLOCKED 0
247
248static inline void spin_lock(spinlock_t *lock)
249{
250 while (testandset(lock));
251}
252
253static inline void spin_unlock(spinlock_t *lock)
254{
255 *lock = 0;
256}
257
258static inline int spin_trylock(spinlock_t *lock)
259{
260 return !testandset(lock);
261}
262
263extern spinlock_t tb_lock;
264