automatic ioctl number conversion - minimum ARM fork() support
[qemu] / exec.h
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_TF_SHIFT     8 /* same position as eflags */
27 #define GEN_FLAG_CPL_SHIFT    9
28 #define GEN_FLAG_IOPL_SHIFT   12 /* same position as eflags */
29
30 struct TranslationBlock;
31 int cpu_x86_gen_code(struct TranslationBlock *tb,
32                      int max_code_size, int *gen_code_size_ptr);
33 int cpu_x86_search_pc(struct TranslationBlock *tb, 
34                       uint32_t *found_pc, unsigned long searched_pc);
35 void cpu_x86_tblocks_init(void);
36 void page_init(void);
37 int 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
53 typedef 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     uint32_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
78 static inline unsigned int tb_hash_func(unsigned long pc)
79 {
80     return pc & (CODE_GEN_HASH_SIZE - 1);
81 }
82
83 TranslationBlock *tb_alloc(unsigned long pc);
84 void tb_flush(void);
85 void tb_link(TranslationBlock *tb);
86
87 extern TranslationBlock *tb_hash[CODE_GEN_HASH_SIZE];
88
89 extern uint8_t code_gen_buffer[CODE_GEN_BUFFER_SIZE];
90 extern 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 */
94 static 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
118 static 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 */
142 static inline void tb_set_jmp_target(TranslationBlock *tb, 
143                                      int n, unsigned long addr)
144 {
145     tb->tb_next[n] = addr;
146 }
147
148 #endif
149
150 static inline void tb_add_jump(TranslationBlock *tb, int n, 
151                                TranslationBlock *tb_next)
152 {
153     /* NOTE: this test is only needed for thread safety */
154     if (!tb->jmp_next[n]) {
155         /* patch the native jump address */
156         tb_set_jmp_target(tb, n, (unsigned long)tb_next->tc_ptr);
157         
158         /* add in TB jmp circular list */
159         tb->jmp_next[n] = tb_next->jmp_first;
160         tb_next->jmp_first = (TranslationBlock *)((long)(tb) | (n));
161     }
162 }
163
164 TranslationBlock *tb_find_pc(unsigned long pc_ptr);
165
166 #ifndef offsetof
167 #define offsetof(type, field) ((size_t) &((type *)0)->field)
168 #endif
169
170 #ifdef __powerpc__
171 static inline int testandset (int *p)
172 {
173     int ret;
174     __asm__ __volatile__ (
175                           "0:    lwarx %0,0,%1 ;"
176                           "      xor. %0,%3,%0;"
177                           "      bne 1f;"
178                           "      stwcx. %2,0,%1;"
179                           "      bne- 0b;"
180                           "1:    "
181                           : "=&r" (ret)
182                           : "r" (p), "r" (1), "r" (0)
183                           : "cr0", "memory");
184     return ret;
185 }
186 #endif
187
188 #ifdef __i386__
189 static inline int testandset (int *p)
190 {
191     char ret;
192     long int readval;
193     
194     __asm__ __volatile__ ("lock; cmpxchgl %3, %1; sete %0"
195                           : "=q" (ret), "=m" (*p), "=a" (readval)
196                           : "r" (1), "m" (*p), "a" (0)
197                           : "memory");
198     return ret;
199 }
200 #endif
201
202 #ifdef __s390__
203 static inline int testandset (int *p)
204 {
205     int ret;
206
207     __asm__ __volatile__ ("0: cs    %0,%1,0(%2)\n"
208                           "   jl    0b"
209                           : "=&d" (ret)
210                           : "r" (1), "a" (p), "0" (*p) 
211                           : "cc", "memory" );
212     return ret;
213 }
214 #endif
215
216 #ifdef __alpha__
217 static inline int testandset (int *p)
218 {
219     int ret;
220     unsigned long one;
221
222     __asm__ __volatile__ ("0:   mov 1,%2\n"
223                           "     ldl_l %0,%1\n"
224                           "     stl_c %2,%1\n"
225                           "     beq %2,1f\n"
226                           ".subsection 2\n"
227                           "1:   br 0b\n"
228                           ".previous"
229                           : "=r" (ret), "=m" (*p), "=r" (one)
230                           : "m" (*p));
231     return ret;
232 }
233 #endif
234
235 #ifdef __sparc__
236 static inline int testandset (int *p)
237 {
238         int ret;
239
240         __asm__ __volatile__("ldstub    [%1], %0"
241                              : "=r" (ret)
242                              : "r" (p)
243                              : "memory");
244
245         return (ret ? 1 : 0);
246 }
247 #endif
248
249 #ifdef __arm__
250 static inline int testandset (int *spinlock)
251 {
252     register unsigned int ret;
253     __asm__ __volatile__("swp %0, %1, [%2]"
254                          : "=r"(ret)
255                          : "0"(1), "r"(spinlock));
256     
257     return ret;
258 }
259 #endif
260
261 typedef int spinlock_t;
262
263 #define SPIN_LOCK_UNLOCKED 0
264
265 static inline void spin_lock(spinlock_t *lock)
266 {
267     while (testandset(lock));
268 }
269
270 static inline void spin_unlock(spinlock_t *lock)
271 {
272     *lock = 0;
273 }
274
275 static inline int spin_trylock(spinlock_t *lock)
276 {
277     return !testandset(lock);
278 }
279
280 extern spinlock_t tb_lock;
281