root / dyngen.h @ 3e11db9a
History | View | Annotate | Download (5.5 kB)
1 | ff1f20a3 | bellard | /*
|
---|---|---|---|
2 | ff1f20a3 | bellard | * dyngen helpers
|
3 | ff1f20a3 | bellard | *
|
4 | ff1f20a3 | bellard | * Copyright (c) 2003 Fabrice Bellard
|
5 | ff1f20a3 | bellard | *
|
6 | ff1f20a3 | bellard | * This library is free software; you can redistribute it and/or
|
7 | ff1f20a3 | bellard | * modify it under the terms of the GNU Lesser General Public
|
8 | ff1f20a3 | bellard | * License as published by the Free Software Foundation; either
|
9 | ff1f20a3 | bellard | * version 2 of the License, or (at your option) any later version.
|
10 | ff1f20a3 | bellard | *
|
11 | ff1f20a3 | bellard | * This library is distributed in the hope that it will be useful,
|
12 | ff1f20a3 | bellard | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | ff1f20a3 | bellard | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | ff1f20a3 | bellard | * Lesser General Public License for more details.
|
15 | ff1f20a3 | bellard | *
|
16 | ff1f20a3 | bellard | * You should have received a copy of the GNU Lesser General Public
|
17 | ff1f20a3 | bellard | * License along with this library; if not, write to the Free Software
|
18 | ff1f20a3 | bellard | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
19 | ff1f20a3 | bellard | */
|
20 | ff1f20a3 | bellard | |
21 | 03daf0e3 | bellard | int __op_param1, __op_param2, __op_param3;
|
22 | c106152d | bellard | int __op_jmp0, __op_jmp1, __op_jmp2, __op_jmp3;
|
23 | 03daf0e3 | bellard | |
24 | 03daf0e3 | bellard | #ifdef __i386__
|
25 | 03daf0e3 | bellard | static inline void flush_icache_range(unsigned long start, unsigned long stop) |
26 | 03daf0e3 | bellard | { |
27 | 03daf0e3 | bellard | } |
28 | 03daf0e3 | bellard | #endif
|
29 | 03daf0e3 | bellard | |
30 | bc51c5c9 | bellard | #ifdef __x86_64__
|
31 | bc51c5c9 | bellard | static inline void flush_icache_range(unsigned long start, unsigned long stop) |
32 | bc51c5c9 | bellard | { |
33 | bc51c5c9 | bellard | } |
34 | bc51c5c9 | bellard | #endif
|
35 | bc51c5c9 | bellard | |
36 | 03daf0e3 | bellard | #ifdef __s390__
|
37 | 03daf0e3 | bellard | static inline void flush_icache_range(unsigned long start, unsigned long stop) |
38 | 03daf0e3 | bellard | { |
39 | 03daf0e3 | bellard | } |
40 | 03daf0e3 | bellard | #endif
|
41 | 03daf0e3 | bellard | |
42 | 03daf0e3 | bellard | #ifdef __ia64__
|
43 | 03daf0e3 | bellard | static inline void flush_icache_range(unsigned long start, unsigned long stop) |
44 | 03daf0e3 | bellard | { |
45 | 03daf0e3 | bellard | } |
46 | 03daf0e3 | bellard | #endif
|
47 | 03daf0e3 | bellard | |
48 | 03daf0e3 | bellard | #ifdef __powerpc__
|
49 | 03daf0e3 | bellard | |
50 | 03daf0e3 | bellard | #define MIN_CACHE_LINE_SIZE 8 /* conservative value */ |
51 | 03daf0e3 | bellard | |
52 | 03daf0e3 | bellard | static void inline flush_icache_range(unsigned long start, unsigned long stop) |
53 | 03daf0e3 | bellard | { |
54 | 03daf0e3 | bellard | unsigned long p; |
55 | 03daf0e3 | bellard | |
56 | 03daf0e3 | bellard | p = start & ~(MIN_CACHE_LINE_SIZE - 1);
|
57 | 03daf0e3 | bellard | stop = (stop + MIN_CACHE_LINE_SIZE - 1) & ~(MIN_CACHE_LINE_SIZE - 1); |
58 | 03daf0e3 | bellard | |
59 | 03daf0e3 | bellard | for (p = start; p < stop; p += MIN_CACHE_LINE_SIZE) {
|
60 | 03daf0e3 | bellard | asm volatile ("dcbst 0,%0" : : "r"(p) : "memory"); |
61 | 03daf0e3 | bellard | } |
62 | 03daf0e3 | bellard | asm volatile ("sync" : : : "memory"); |
63 | 03daf0e3 | bellard | for (p = start; p < stop; p += MIN_CACHE_LINE_SIZE) {
|
64 | 03daf0e3 | bellard | asm volatile ("icbi 0,%0" : : "r"(p) : "memory"); |
65 | 03daf0e3 | bellard | } |
66 | 03daf0e3 | bellard | asm volatile ("sync" : : : "memory"); |
67 | 03daf0e3 | bellard | asm volatile ("isync" : : : "memory"); |
68 | 03daf0e3 | bellard | } |
69 | 03daf0e3 | bellard | #endif
|
70 | 03daf0e3 | bellard | |
71 | 03daf0e3 | bellard | #ifdef __alpha__
|
72 | 03daf0e3 | bellard | static inline void flush_icache_range(unsigned long start, unsigned long stop) |
73 | 03daf0e3 | bellard | { |
74 | 03daf0e3 | bellard | asm ("imb"); |
75 | 03daf0e3 | bellard | } |
76 | 03daf0e3 | bellard | #endif
|
77 | 03daf0e3 | bellard | |
78 | 03daf0e3 | bellard | #ifdef __sparc__
|
79 | 03daf0e3 | bellard | |
80 | 03daf0e3 | bellard | static void inline flush_icache_range(unsigned long start, unsigned long stop) |
81 | 03daf0e3 | bellard | { |
82 | 03daf0e3 | bellard | unsigned long p; |
83 | 03daf0e3 | bellard | |
84 | 03daf0e3 | bellard | p = start & ~(8UL - 1UL); |
85 | 03daf0e3 | bellard | stop = (stop + (8UL - 1UL)) & ~(8UL - 1UL); |
86 | 03daf0e3 | bellard | |
87 | 03daf0e3 | bellard | for (; p < stop; p += 8) |
88 | 03daf0e3 | bellard | __asm__ __volatile__("flush\t%0" : : "r" (p)); |
89 | 03daf0e3 | bellard | } |
90 | 03daf0e3 | bellard | |
91 | 03daf0e3 | bellard | #endif
|
92 | 03daf0e3 | bellard | |
93 | 03daf0e3 | bellard | #ifdef __arm__
|
94 | 03daf0e3 | bellard | static inline void flush_icache_range(unsigned long start, unsigned long stop) |
95 | 03daf0e3 | bellard | { |
96 | 03daf0e3 | bellard | register unsigned long _beg __asm ("a1") = start; |
97 | 03daf0e3 | bellard | register unsigned long _end __asm ("a2") = stop; |
98 | 03daf0e3 | bellard | register unsigned long _flg __asm ("a3") = 0; |
99 | 03daf0e3 | bellard | __asm __volatile__ ("swi 0x9f0002" : : "r" (_beg), "r" (_end), "r" (_flg)); |
100 | 03daf0e3 | bellard | } |
101 | 03daf0e3 | bellard | #endif
|
102 | 03daf0e3 | bellard | |
103 | 38e584a0 | bellard | #ifdef __mc68000
|
104 | 38e584a0 | bellard | #include <asm/cachectl.h> |
105 | 38e584a0 | bellard | static inline void flush_icache_range(unsigned long start, unsigned long stop) |
106 | 38e584a0 | bellard | { |
107 | 38e584a0 | bellard | cacheflush(start,FLUSH_SCOPE_LINE,FLUSH_CACHE_BOTH,stop-start+16);
|
108 | 38e584a0 | bellard | } |
109 | 38e584a0 | bellard | #endif
|
110 | 38e584a0 | bellard | |
111 | ff1f20a3 | bellard | #ifdef __alpha__
|
112 | ff1f20a3 | bellard | |
113 | ff1f20a3 | bellard | register int gp asm("$29"); |
114 | ff1f20a3 | bellard | |
115 | ff1f20a3 | bellard | static inline void immediate_ldah(void *p, int val) { |
116 | ff1f20a3 | bellard | uint32_t *dest = p; |
117 | ff1f20a3 | bellard | long high = ((val >> 16) + ((val >> 15) & 1)) & 0xffff; |
118 | ff1f20a3 | bellard | |
119 | ff1f20a3 | bellard | *dest &= ~0xffff;
|
120 | ff1f20a3 | bellard | *dest |= high; |
121 | ff1f20a3 | bellard | *dest |= 31 << 16; |
122 | ff1f20a3 | bellard | } |
123 | ff1f20a3 | bellard | static inline void immediate_lda(void *dest, int val) { |
124 | ff1f20a3 | bellard | *(uint16_t *) dest = val; |
125 | ff1f20a3 | bellard | } |
126 | ff1f20a3 | bellard | void fix_bsr(void *p, int offset) { |
127 | ff1f20a3 | bellard | uint32_t *dest = p; |
128 | ff1f20a3 | bellard | *dest &= ~((1 << 21) - 1); |
129 | ff1f20a3 | bellard | *dest |= (offset >> 2) & ((1 << 21) - 1); |
130 | ff1f20a3 | bellard | } |
131 | ff1f20a3 | bellard | |
132 | ff1f20a3 | bellard | #endif /* __alpha__ */ |
133 | ff1f20a3 | bellard | |
134 | ff1f20a3 | bellard | #ifdef __arm__
|
135 | ff1f20a3 | bellard | |
136 | ff1f20a3 | bellard | #define MAX_OP_SIZE (128 * 4) /* in bytes */ |
137 | ff1f20a3 | bellard | /* max size of the code that can be generated without calling arm_flush_ldr */
|
138 | ff1f20a3 | bellard | #define MAX_FRAG_SIZE (1024 * 4) |
139 | ff1f20a3 | bellard | //#define MAX_FRAG_SIZE (135 * 4) /* for testing */
|
140 | ff1f20a3 | bellard | |
141 | ff1f20a3 | bellard | typedef struct LDREntry { |
142 | ff1f20a3 | bellard | uint8_t *ptr; |
143 | ff1f20a3 | bellard | uint32_t *data_ptr; |
144 | ff1f20a3 | bellard | } LDREntry; |
145 | ff1f20a3 | bellard | |
146 | ff1f20a3 | bellard | static LDREntry arm_ldr_table[1024]; |
147 | ff1f20a3 | bellard | static uint32_t arm_data_table[1024]; |
148 | ff1f20a3 | bellard | |
149 | ff1f20a3 | bellard | extern char exec_loop; |
150 | ff1f20a3 | bellard | |
151 | ff1f20a3 | bellard | static inline void arm_reloc_pc24(uint32_t *ptr, uint32_t insn, int val) |
152 | ff1f20a3 | bellard | { |
153 | ff1f20a3 | bellard | *ptr = (insn & ~0xffffff) | ((insn + ((val - (int)ptr) >> 2)) & 0xffffff); |
154 | ff1f20a3 | bellard | } |
155 | ff1f20a3 | bellard | |
156 | ff1f20a3 | bellard | static uint8_t *arm_flush_ldr(uint8_t *gen_code_ptr,
|
157 | ff1f20a3 | bellard | LDREntry *ldr_start, LDREntry *ldr_end, |
158 | ff1f20a3 | bellard | uint32_t *data_start, uint32_t *data_end, |
159 | ff1f20a3 | bellard | int gen_jmp)
|
160 | ff1f20a3 | bellard | { |
161 | ff1f20a3 | bellard | LDREntry *le; |
162 | ff1f20a3 | bellard | uint32_t *ptr; |
163 | ff1f20a3 | bellard | int offset, data_size, target;
|
164 | ff1f20a3 | bellard | uint8_t *data_ptr; |
165 | ff1f20a3 | bellard | uint32_t insn; |
166 | ff1f20a3 | bellard | |
167 | ff1f20a3 | bellard | data_size = (uint8_t *)data_end - (uint8_t *)data_start; |
168 | ff1f20a3 | bellard | |
169 | 9621339d | bellard | if (gen_jmp) {
|
170 | ff1f20a3 | bellard | /* generate branch to skip the data */
|
171 | ff1f20a3 | bellard | if (data_size == 0) |
172 | ff1f20a3 | bellard | return gen_code_ptr;
|
173 | ff1f20a3 | bellard | target = (long)gen_code_ptr + data_size + 4; |
174 | ff1f20a3 | bellard | arm_reloc_pc24((uint32_t *)gen_code_ptr, 0xeafffffe, target);
|
175 | ff1f20a3 | bellard | gen_code_ptr += 4;
|
176 | ff1f20a3 | bellard | } |
177 | ff1f20a3 | bellard | |
178 | ff1f20a3 | bellard | /* copy the data */
|
179 | ff1f20a3 | bellard | data_ptr = gen_code_ptr; |
180 | ff1f20a3 | bellard | memcpy(gen_code_ptr, data_start, data_size); |
181 | ff1f20a3 | bellard | gen_code_ptr += data_size; |
182 | ff1f20a3 | bellard | |
183 | ff1f20a3 | bellard | /* patch the ldr to point to the data */
|
184 | ff1f20a3 | bellard | for(le = ldr_start; le < ldr_end; le++) {
|
185 | ff1f20a3 | bellard | ptr = (uint32_t *)le->ptr; |
186 | ff1f20a3 | bellard | offset = ((unsigned long)(le->data_ptr) - (unsigned long)data_start) + |
187 | ff1f20a3 | bellard | (unsigned long)data_ptr - |
188 | ff1f20a3 | bellard | (unsigned long)ptr - 8; |
189 | ff1f20a3 | bellard | insn = *ptr & ~(0xfff | 0x00800000); |
190 | ff1f20a3 | bellard | if (offset < 0) { |
191 | ff1f20a3 | bellard | offset = - offset; |
192 | ff1f20a3 | bellard | } else {
|
193 | ff1f20a3 | bellard | insn |= 0x00800000;
|
194 | ff1f20a3 | bellard | } |
195 | ff1f20a3 | bellard | if (offset > 0xfff) { |
196 | ff1f20a3 | bellard | fprintf(stderr, "Error ldr offset\n");
|
197 | ff1f20a3 | bellard | abort(); |
198 | ff1f20a3 | bellard | } |
199 | ff1f20a3 | bellard | insn |= offset; |
200 | ff1f20a3 | bellard | *ptr = insn; |
201 | ff1f20a3 | bellard | } |
202 | ff1f20a3 | bellard | return gen_code_ptr;
|
203 | ff1f20a3 | bellard | } |
204 | ff1f20a3 | bellard | |
205 | ff1f20a3 | bellard | #endif /* __arm__ */ |
206 | ff1f20a3 | bellard | |
207 | ff1f20a3 | bellard | |
208 | ff1f20a3 | bellard |