4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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
21 int __op_param1
, __op_param2
, __op_param3
;
23 void __op_gen_label1(){}
24 void __op_gen_label2(){}
25 void __op_gen_label3(){}
27 int __op_gen_label1
, __op_gen_label2
, __op_gen_label3
;
29 int __op_jmp0
, __op_jmp1
, __op_jmp2
, __op_jmp3
;
32 static inline void flush_icache_range(unsigned long start
, unsigned long stop
)
38 static inline void flush_icache_range(unsigned long start
, unsigned long stop
)
44 static inline void flush_icache_range(unsigned long start
, unsigned long stop
)
50 static inline void flush_icache_range(unsigned long start
, unsigned long stop
)
52 while (start
< stop
) {
53 asm volatile ("fc %0" :: "r"(start
));
56 asm volatile (";;sync.i;;srlz.i;;");
62 #define MIN_CACHE_LINE_SIZE 8 /* conservative value */
64 static void inline flush_icache_range(unsigned long start
, unsigned long stop
)
68 start
&= ~(MIN_CACHE_LINE_SIZE
- 1);
69 stop
= (stop
+ MIN_CACHE_LINE_SIZE
- 1) & ~(MIN_CACHE_LINE_SIZE
- 1);
71 for (p
= start
; p
< stop
; p
+= MIN_CACHE_LINE_SIZE
) {
72 asm volatile ("dcbst 0,%0" : : "r"(p
) : "memory");
74 asm volatile ("sync" : : : "memory");
75 for (p
= start
; p
< stop
; p
+= MIN_CACHE_LINE_SIZE
) {
76 asm volatile ("icbi 0,%0" : : "r"(p
) : "memory");
78 asm volatile ("sync" : : : "memory");
79 asm volatile ("isync" : : : "memory");
84 static inline void flush_icache_range(unsigned long start
, unsigned long stop
)
92 static void inline flush_icache_range(unsigned long start
, unsigned long stop
)
96 p
= start
& ~(8UL - 1UL);
97 stop
= (stop
+ (8UL - 1UL)) & ~(8UL - 1UL);
99 for (; p
< stop
; p
+= 8)
100 __asm__
__volatile__("flush\t%0" : : "r" (p
));
106 static inline void flush_icache_range(unsigned long start
, unsigned long stop
)
108 register unsigned long _beg
__asm ("a1") = start
;
109 register unsigned long _end
__asm ("a2") = stop
;
110 register unsigned long _flg
__asm ("a3") = 0;
111 __asm
__volatile__ ("swi 0x9f0002" : : "r" (_beg
), "r" (_end
), "r" (_flg
));
116 #include <asm/cachectl.h>
117 static inline void flush_icache_range(unsigned long start
, unsigned long stop
)
119 cacheflush(start
,FLUSH_SCOPE_LINE
,FLUSH_CACHE_BOTH
,stop
-start
+16);
125 register int gp
asm("$29");
127 static inline void immediate_ldah(void *p
, int val
) {
129 long high
= ((val
>> 16) + ((val
>> 15) & 1)) & 0xffff;
135 static inline void immediate_lda(void *dest
, int val
) {
136 *(uint16_t *) dest
= val
;
138 void fix_bsr(void *p
, int offset
) {
140 *dest
&= ~((1 << 21) - 1);
141 *dest
|= (offset
>> 2) & ((1 << 21) - 1);
144 #endif /* __alpha__ */
148 #define MAX_OP_SIZE (128 * 4) /* in bytes */
149 /* max size of the code that can be generated without calling arm_flush_ldr */
150 #define MAX_FRAG_SIZE (1024 * 4)
151 //#define MAX_FRAG_SIZE (135 * 4) /* for testing */
153 typedef struct LDREntry
{
158 static LDREntry arm_ldr_table
[1024];
159 static uint32_t arm_data_table
[1024];
161 extern char exec_loop
;
163 static inline void arm_reloc_pc24(uint32_t *ptr
, uint32_t insn
, int val
)
165 *ptr
= (insn
& ~0xffffff) | ((insn
+ ((val
- (int)ptr
) >> 2)) & 0xffffff);
168 static uint8_t *arm_flush_ldr(uint8_t *gen_code_ptr
,
169 LDREntry
*ldr_start
, LDREntry
*ldr_end
,
170 uint32_t *data_start
, uint32_t *data_end
,
175 int offset
, data_size
, target
;
179 data_size
= (uint8_t *)data_end
- (uint8_t *)data_start
;
182 /* generate branch to skip the data */
185 target
= (long)gen_code_ptr
+ data_size
+ 4;
186 arm_reloc_pc24((uint32_t *)gen_code_ptr
, 0xeafffffe, target
);
191 data_ptr
= gen_code_ptr
;
192 memcpy(gen_code_ptr
, data_start
, data_size
);
193 gen_code_ptr
+= data_size
;
195 /* patch the ldr to point to the data */
196 for(le
= ldr_start
; le
< ldr_end
; le
++) {
197 ptr
= (uint32_t *)le
->ptr
;
198 offset
= ((unsigned long)(le
->data_ptr
) - (unsigned long)data_start
) +
199 (unsigned long)data_ptr
-
200 (unsigned long)ptr
- 8;
201 insn
= *ptr
& ~(0xfff | 0x00800000);
207 if (offset
> 0xfff) {
208 fprintf(stderr
, "Error ldr offset\n");
222 /* Patch instruction with "val" where "mask" has 1 bits. */
223 static inline void ia64_patch (uint64_t insn_addr
, uint64_t mask
, uint64_t val
)
225 uint64_t m0
, m1
, v0
, v1
, b0
, b1
, *b
= (uint64_t *) (insn_addr
& -16);
226 # define insn_mask ((1UL << 41) - 1)
229 b0
= b
[0]; b1
= b
[1];
230 shift
= 5 + 41 * (insn_addr
% 16); /* 5 template, 3 x 41-bit insns */
232 m1
= mask
<< (shift
- 64);
233 v1
= val
<< (shift
- 64);
235 m0
= mask
<< shift
; m1
= mask
>> (64 - shift
);
236 v0
= val
<< shift
; v1
= val
>> (64 - shift
);
237 b
[0] = (b0
& ~m0
) | (v0
& m0
);
239 b
[1] = (b1
& ~m1
) | (v1
& m1
);
242 static inline void ia64_patch_imm60 (uint64_t insn_addr
, uint64_t val
)
244 ia64_patch(insn_addr
,
246 ( ((val
& 0x0800000000000000UL
) >> 23) /* bit 59 -> 36 */
247 | ((val
& 0x00000000000fffffUL
) << 13) /* bit 0 -> 13 */));
248 ia64_patch(insn_addr
- 1, 0x1fffffffffcUL
, val
>> 18);
251 static inline void ia64_imm64 (void *insn
, uint64_t val
)
253 /* Ignore the slot number of the relocation; GCC and Intel
254 toolchains differed for some time on whether IMM64 relocs are
255 against slot 1 (Intel) or slot 2 (GCC). */
256 uint64_t insn_addr
= (uint64_t) insn
& ~3UL;
258 ia64_patch(insn_addr
+ 2,
260 ( ((val
& 0x8000000000000000UL
) >> 27) /* bit 63 -> 36 */
261 | ((val
& 0x0000000000200000UL
) << 0) /* bit 21 -> 21 */
262 | ((val
& 0x00000000001f0000UL
) << 6) /* bit 16 -> 22 */
263 | ((val
& 0x000000000000ff80UL
) << 20) /* bit 7 -> 27 */
264 | ((val
& 0x000000000000007fUL
) << 13) /* bit 0 -> 13 */)
266 ia64_patch(insn_addr
+ 1, 0x1ffffffffffUL
, val
>> 22);
269 static inline void ia64_imm60b (void *insn
, uint64_t val
)
271 /* Ignore the slot number of the relocation; GCC and Intel
272 toolchains differed for some time on whether IMM64 relocs are
273 against slot 1 (Intel) or slot 2 (GCC). */
274 uint64_t insn_addr
= (uint64_t) insn
& ~3UL;
276 if (val
+ ((uint64_t) 1 << 59) >= (1UL << 60))
277 fprintf(stderr
, "%s: value %ld out of IMM60 range\n",
278 __FUNCTION__
, (int64_t) val
);
279 ia64_patch_imm60(insn_addr
+ 2, val
);
282 static inline void ia64_imm22 (void *insn
, uint64_t val
)
284 if (val
+ (1 << 21) >= (1 << 22))
285 fprintf(stderr
, "%s: value %li out of IMM22 range\n",
286 __FUNCTION__
, (int64_t)val
);
287 ia64_patch((uint64_t) insn
, 0x01fffcfe000UL
,
288 ( ((val
& 0x200000UL
) << 15) /* bit 21 -> 36 */
289 | ((val
& 0x1f0000UL
) << 6) /* bit 16 -> 22 */
290 | ((val
& 0x00ff80UL
) << 20) /* bit 7 -> 27 */
291 | ((val
& 0x00007fUL
) << 13) /* bit 0 -> 13 */));
294 /* Like ia64_imm22(), but also clear bits 20-21. For addl, this has
295 the effect of turning "addl rX=imm22,rY" into "addl
297 static inline void ia64_imm22_r0 (void *insn
, uint64_t val
)
299 if (val
+ (1 << 21) >= (1 << 22))
300 fprintf(stderr
, "%s: value %li out of IMM22 range\n",
301 __FUNCTION__
, (int64_t)val
);
302 ia64_patch((uint64_t) insn
, 0x01fffcfe000UL
| (0x3UL
<< 20),
303 ( ((val
& 0x200000UL
) << 15) /* bit 21 -> 36 */
304 | ((val
& 0x1f0000UL
) << 6) /* bit 16 -> 22 */
305 | ((val
& 0x00ff80UL
) << 20) /* bit 7 -> 27 */
306 | ((val
& 0x00007fUL
) << 13) /* bit 0 -> 13 */));
309 static inline void ia64_imm21b (void *insn
, uint64_t val
)
311 if (val
+ (1 << 20) >= (1 << 21))
312 fprintf(stderr
, "%s: value %li out of IMM21b range\n",
313 __FUNCTION__
, (int64_t)val
);
314 ia64_patch((uint64_t) insn
, 0x11ffffe000UL
,
315 ( ((val
& 0x100000UL
) << 16) /* bit 20 -> 36 */
316 | ((val
& 0x0fffffUL
) << 13) /* bit 0 -> 13 */));
319 static inline void ia64_nop_b (void *insn
)
321 ia64_patch((uint64_t) insn
, (1UL << 41) - 1, 2UL << 37);
324 static inline void ia64_ldxmov(void *insn
, uint64_t val
)
326 if (val
+ (1 << 21) < (1 << 22))
327 ia64_patch((uint64_t) insn
, 0x1fff80fe000UL
, 8UL << 37);
330 static inline int ia64_patch_ltoff(void *insn
, uint64_t val
,
333 if (relaxable
&& (val
+ (1 << 21) < (1 << 22))) {
334 ia64_imm22_r0(insn
, val
);
341 struct ia64_fixup
*next
;
342 void *addr
; /* address that needs to be patched */
346 #define IA64_PLT(insn, plt_index) \
348 struct ia64_fixup *fixup = alloca(sizeof(*fixup)); \
349 fixup->next = plt_fixes; \
351 fixup->addr = (insn); \
352 fixup->value = (plt_index); \
353 plt_offset[(plt_index)] = 1; \
356 #define IA64_LTOFF(insn, val, relaxable) \
358 if (ia64_patch_ltoff(insn, val, relaxable)) { \
359 struct ia64_fixup *fixup = alloca(sizeof(*fixup)); \
360 fixup->next = ltoff_fixes; \
361 ltoff_fixes = fixup; \
362 fixup->addr = (insn); \
363 fixup->value = (val); \
367 static inline void ia64_apply_fixes (uint8_t **gen_code_pp
,
368 struct ia64_fixup
*ltoff_fixes
,
370 struct ia64_fixup
*plt_fixes
,
372 unsigned long *plt_target
,
373 unsigned int *plt_offset
)
375 static const uint8_t plt_bundle
[] = {
376 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, /* nop 0; movl r1=GP */
377 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x60,
379 0x05, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, /* nop 0; brl IP */
380 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0
382 uint8_t *gen_code_ptr
= *gen_code_pp
, *plt_start
, *got_start
, *vp
;
383 struct ia64_fixup
*fixup
;
384 unsigned int offset
= 0;
392 plt_start
= gen_code_ptr
;
394 for (i
= 0; i
< num_plts
; ++i
) {
396 plt_offset
[i
] = offset
;
397 offset
+= sizeof(plt_bundle
);
399 fdesc
= (struct fdesc
*) plt_target
[i
];
400 memcpy(gen_code_ptr
, plt_bundle
, sizeof(plt_bundle
));
401 ia64_imm64 (gen_code_ptr
+ 0x02, fdesc
->gp
);
402 ia64_imm60b(gen_code_ptr
+ 0x12,
403 (fdesc
->ip
- (long) (gen_code_ptr
+ 0x10)) >> 4);
404 gen_code_ptr
+= sizeof(plt_bundle
);
408 for (fixup
= plt_fixes
; fixup
; fixup
= fixup
->next
)
409 ia64_imm21b(fixup
->addr
,
410 ((long) plt_start
+ plt_offset
[fixup
->value
]
411 - ((long) fixup
->addr
& ~0xf)) >> 4);
414 got_start
= gen_code_ptr
;
416 /* First, create the GOT: */
417 for (fixup
= ltoff_fixes
; fixup
; fixup
= fixup
->next
) {
418 /* first check if we already have this value in the GOT: */
419 for (vp
= got_start
; vp
< gen_code_ptr
; ++vp
)
420 if (*(uint64_t *) vp
== fixup
->value
)
422 if (vp
== gen_code_ptr
) {
423 /* Nope, we need to put the value in the GOT: */
424 *(uint64_t *) vp
= fixup
->value
;
427 ia64_imm22(fixup
->addr
, (long) vp
- gp
);
429 /* Keep code ptr aligned. */
430 if ((long) gen_code_ptr
& 15)
432 *gen_code_pp
= gen_code_ptr
;