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
;
22 #if defined(__sparc__) || defined(__arm__)
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 ARM_LDR_TABLE_SIZE 1024
150 typedef struct LDREntry
{
156 static LDREntry arm_ldr_table
[1024];
157 static uint32_t arm_data_table
[ARM_LDR_TABLE_SIZE
];
159 extern char exec_loop
;
161 static inline void arm_reloc_pc24(uint32_t *ptr
, uint32_t insn
, int val
)
163 *ptr
= (insn
& ~0xffffff) | ((insn
+ ((val
- (int)ptr
) >> 2)) & 0xffffff);
166 static uint8_t *arm_flush_ldr(uint8_t *gen_code_ptr
,
167 LDREntry
*ldr_start
, LDREntry
*ldr_end
,
168 uint32_t *data_start
, uint32_t *data_end
,
173 int offset
, data_size
, target
;
178 data_size
= (data_end
- data_start
) << 2;
181 /* generate branch to skip the data */
184 target
= (long)gen_code_ptr
+ data_size
+ 4;
185 arm_reloc_pc24((uint32_t *)gen_code_ptr
, 0xeafffffe, target
);
190 data_ptr
= gen_code_ptr
;
191 memcpy(gen_code_ptr
, data_start
, data_size
);
192 gen_code_ptr
+= data_size
;
194 /* patch the ldr to point to the data */
195 for(le
= ldr_start
; le
< ldr_end
; le
++) {
196 ptr
= (uint32_t *)le
->ptr
;
197 offset
= ((unsigned long)(le
->data_ptr
) - (unsigned long)data_start
) +
198 (unsigned long)data_ptr
-
199 (unsigned long)ptr
- 8;
201 fprintf(stderr
, "Negative constant pool offset\n");
207 if (offset
>= 4096) {
208 fprintf(stderr
, "Bad ldr offset\n");
214 if (offset
>= 1024 ) {
215 fprintf(stderr
, "Bad ldc offset\n");
221 if (offset
>= 1024 ) {
222 fprintf(stderr
, "Bad add offset\n");
227 fprintf(stderr
, "Bad pc relative fixup\n");
233 insn
|= offset
| 0x00800000;
236 insn
|= (offset
>> 2) | 0x00800000;
239 insn
|= (offset
>> 2) | 0xf00;
252 /* Patch instruction with "val" where "mask" has 1 bits. */
253 static inline void ia64_patch (uint64_t insn_addr
, uint64_t mask
, uint64_t val
)
255 uint64_t m0
, m1
, v0
, v1
, b0
, b1
, *b
= (uint64_t *) (insn_addr
& -16);
256 # define insn_mask ((1UL << 41) - 1)
259 b0
= b
[0]; b1
= b
[1];
260 shift
= 5 + 41 * (insn_addr
% 16); /* 5 template, 3 x 41-bit insns */
262 m1
= mask
<< (shift
- 64);
263 v1
= val
<< (shift
- 64);
265 m0
= mask
<< shift
; m1
= mask
>> (64 - shift
);
266 v0
= val
<< shift
; v1
= val
>> (64 - shift
);
267 b
[0] = (b0
& ~m0
) | (v0
& m0
);
269 b
[1] = (b1
& ~m1
) | (v1
& m1
);
272 static inline void ia64_patch_imm60 (uint64_t insn_addr
, uint64_t val
)
274 ia64_patch(insn_addr
,
276 ( ((val
& 0x0800000000000000UL
) >> 23) /* bit 59 -> 36 */
277 | ((val
& 0x00000000000fffffUL
) << 13) /* bit 0 -> 13 */));
278 ia64_patch(insn_addr
- 1, 0x1fffffffffcUL
, val
>> 18);
281 static inline void ia64_imm64 (void *insn
, uint64_t val
)
283 /* Ignore the slot number of the relocation; GCC and Intel
284 toolchains differed for some time on whether IMM64 relocs are
285 against slot 1 (Intel) or slot 2 (GCC). */
286 uint64_t insn_addr
= (uint64_t) insn
& ~3UL;
288 ia64_patch(insn_addr
+ 2,
290 ( ((val
& 0x8000000000000000UL
) >> 27) /* bit 63 -> 36 */
291 | ((val
& 0x0000000000200000UL
) << 0) /* bit 21 -> 21 */
292 | ((val
& 0x00000000001f0000UL
) << 6) /* bit 16 -> 22 */
293 | ((val
& 0x000000000000ff80UL
) << 20) /* bit 7 -> 27 */
294 | ((val
& 0x000000000000007fUL
) << 13) /* bit 0 -> 13 */)
296 ia64_patch(insn_addr
+ 1, 0x1ffffffffffUL
, val
>> 22);
299 static inline void ia64_imm60b (void *insn
, uint64_t val
)
301 /* Ignore the slot number of the relocation; GCC and Intel
302 toolchains differed for some time on whether IMM64 relocs are
303 against slot 1 (Intel) or slot 2 (GCC). */
304 uint64_t insn_addr
= (uint64_t) insn
& ~3UL;
306 if (val
+ ((uint64_t) 1 << 59) >= (1UL << 60))
307 fprintf(stderr
, "%s: value %ld out of IMM60 range\n",
308 __FUNCTION__
, (int64_t) val
);
309 ia64_patch_imm60(insn_addr
+ 2, val
);
312 static inline void ia64_imm22 (void *insn
, uint64_t val
)
314 if (val
+ (1 << 21) >= (1 << 22))
315 fprintf(stderr
, "%s: value %li out of IMM22 range\n",
316 __FUNCTION__
, (int64_t)val
);
317 ia64_patch((uint64_t) insn
, 0x01fffcfe000UL
,
318 ( ((val
& 0x200000UL
) << 15) /* bit 21 -> 36 */
319 | ((val
& 0x1f0000UL
) << 6) /* bit 16 -> 22 */
320 | ((val
& 0x00ff80UL
) << 20) /* bit 7 -> 27 */
321 | ((val
& 0x00007fUL
) << 13) /* bit 0 -> 13 */));
324 /* Like ia64_imm22(), but also clear bits 20-21. For addl, this has
325 the effect of turning "addl rX=imm22,rY" into "addl
327 static inline void ia64_imm22_r0 (void *insn
, uint64_t val
)
329 if (val
+ (1 << 21) >= (1 << 22))
330 fprintf(stderr
, "%s: value %li out of IMM22 range\n",
331 __FUNCTION__
, (int64_t)val
);
332 ia64_patch((uint64_t) insn
, 0x01fffcfe000UL
| (0x3UL
<< 20),
333 ( ((val
& 0x200000UL
) << 15) /* bit 21 -> 36 */
334 | ((val
& 0x1f0000UL
) << 6) /* bit 16 -> 22 */
335 | ((val
& 0x00ff80UL
) << 20) /* bit 7 -> 27 */
336 | ((val
& 0x00007fUL
) << 13) /* bit 0 -> 13 */));
339 static inline void ia64_imm21b (void *insn
, uint64_t val
)
341 if (val
+ (1 << 20) >= (1 << 21))
342 fprintf(stderr
, "%s: value %li out of IMM21b range\n",
343 __FUNCTION__
, (int64_t)val
);
344 ia64_patch((uint64_t) insn
, 0x11ffffe000UL
,
345 ( ((val
& 0x100000UL
) << 16) /* bit 20 -> 36 */
346 | ((val
& 0x0fffffUL
) << 13) /* bit 0 -> 13 */));
349 static inline void ia64_nop_b (void *insn
)
351 ia64_patch((uint64_t) insn
, (1UL << 41) - 1, 2UL << 37);
354 static inline void ia64_ldxmov(void *insn
, uint64_t val
)
356 if (val
+ (1 << 21) < (1 << 22))
357 ia64_patch((uint64_t) insn
, 0x1fff80fe000UL
, 8UL << 37);
360 static inline int ia64_patch_ltoff(void *insn
, uint64_t val
,
363 if (relaxable
&& (val
+ (1 << 21) < (1 << 22))) {
364 ia64_imm22_r0(insn
, val
);
371 struct ia64_fixup
*next
;
372 void *addr
; /* address that needs to be patched */
376 #define IA64_PLT(insn, plt_index) \
378 struct ia64_fixup *fixup = alloca(sizeof(*fixup)); \
379 fixup->next = plt_fixes; \
381 fixup->addr = (insn); \
382 fixup->value = (plt_index); \
383 plt_offset[(plt_index)] = 1; \
386 #define IA64_LTOFF(insn, val, relaxable) \
388 if (ia64_patch_ltoff(insn, val, relaxable)) { \
389 struct ia64_fixup *fixup = alloca(sizeof(*fixup)); \
390 fixup->next = ltoff_fixes; \
391 ltoff_fixes = fixup; \
392 fixup->addr = (insn); \
393 fixup->value = (val); \
397 static inline void ia64_apply_fixes (uint8_t **gen_code_pp
,
398 struct ia64_fixup
*ltoff_fixes
,
400 struct ia64_fixup
*plt_fixes
,
402 unsigned long *plt_target
,
403 unsigned int *plt_offset
)
405 static const uint8_t plt_bundle
[] = {
406 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, /* nop 0; movl r1=GP */
407 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x60,
409 0x05, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, /* nop 0; brl IP */
410 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0
412 uint8_t *gen_code_ptr
= *gen_code_pp
, *plt_start
, *got_start
, *vp
;
413 struct ia64_fixup
*fixup
;
414 unsigned int offset
= 0;
422 plt_start
= gen_code_ptr
;
424 for (i
= 0; i
< num_plts
; ++i
) {
426 plt_offset
[i
] = offset
;
427 offset
+= sizeof(plt_bundle
);
429 fdesc
= (struct fdesc
*) plt_target
[i
];
430 memcpy(gen_code_ptr
, plt_bundle
, sizeof(plt_bundle
));
431 ia64_imm64 (gen_code_ptr
+ 0x02, fdesc
->gp
);
432 ia64_imm60b(gen_code_ptr
+ 0x12,
433 (fdesc
->ip
- (long) (gen_code_ptr
+ 0x10)) >> 4);
434 gen_code_ptr
+= sizeof(plt_bundle
);
438 for (fixup
= plt_fixes
; fixup
; fixup
= fixup
->next
)
439 ia64_imm21b(fixup
->addr
,
440 ((long) plt_start
+ plt_offset
[fixup
->value
]
441 - ((long) fixup
->addr
& ~0xf)) >> 4);
444 got_start
= gen_code_ptr
;
446 /* First, create the GOT: */
447 for (fixup
= ltoff_fixes
; fixup
; fixup
= fixup
->next
) {
448 /* first check if we already have this value in the GOT: */
449 for (vp
= got_start
; vp
< gen_code_ptr
; ++vp
)
450 if (*(uint64_t *) vp
== fixup
->value
)
452 if (vp
== gen_code_ptr
) {
453 /* Nope, we need to put the value in the GOT: */
454 *(uint64_t *) vp
= fixup
->value
;
457 ia64_imm22(fixup
->addr
, (long) vp
- gp
);
459 /* Keep code ptr aligned. */
460 if ((long) gen_code_ptr
& 15)
462 *gen_code_pp
= gen_code_ptr
;