1 /*
2 * Copyright (C) 2013-2020 Free Software Foundation, Inc.
3 *
4 * This file is part of GNU lightning.
5 *
6 * GNU lightning is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation; either version 3, or (at your option)
9 * any later version.
10 *
11 * GNU lightning is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 * License for more details.
15 *
16 * Authors:
17 * Paulo Cesar Pereira de Andrade
18 */
19
20 /* libgcc */
21 extern void __clear_cache(void *, void *);
22
23
24 static inline int32_t
read_signed_bitfield(uint32_t word,uint8_t width,uint8_t shift)25 read_signed_bitfield(uint32_t word, uint8_t width, uint8_t shift)
26 {
27 return ((int32_t)word) << (32 - width - shift) >> (32 - width);
28 }
29
30 static inline uint32_t
read_unsigned_bitfield(uint32_t word,uint8_t width,uint8_t shift)31 read_unsigned_bitfield(uint32_t word, uint8_t width, uint8_t shift)
32 {
33 return word << (32 - width - shift) >> (32 - width);
34 }
35
36 static inline int
in_signed_range(ptrdiff_t diff,uint8_t bits)37 in_signed_range(ptrdiff_t diff, uint8_t bits)
38 {
39 return (-1 << (bits - 1)) <= diff && diff < (1 << (bits - 1));
40 }
41
42 static inline int
in_unsigned_range(uint32_t val,uint8_t bits)43 in_unsigned_range(uint32_t val, uint8_t bits)
44 {
45 ASSERT(bits < __WORDSIZE);
46 return val < (1 << bits);
47 }
48
49 static inline uint32_t
write_unsigned_bitfield(uint32_t word,uint32_t val,uint8_t width,uint8_t shift)50 write_unsigned_bitfield(uint32_t word, uint32_t val, uint8_t width, uint8_t shift)
51 {
52 ASSERT(read_unsigned_bitfield(word, width, shift) == 0);
53 ASSERT(in_unsigned_range(val, width));
54 return word | (val << shift);
55 }
56
57 static inline int32_t
write_signed_bitfield(uint32_t word,ptrdiff_t val,uint8_t width,uint8_t shift)58 write_signed_bitfield(uint32_t word, ptrdiff_t val, uint8_t width, uint8_t shift)
59 {
60 ASSERT(read_signed_bitfield(word, width, shift) == 0);
61 ASSERT(in_signed_range(val, width));
62 return word | ((val & ((1 << width) - 1)) << shift);
63 }
64
65 #define DEFINE_ENCODER(name, width, shift, kind, val_t) \
66 static const uint8_t name##_width = width; \
67 static const uint8_t name##_shift = shift; \
68 static uint32_t \
69 write_##name##_bitfield(uint32_t word, val_t val) \
70 { \
71 return write_##kind##_bitfield(word, val, name##_width, name##_shift); \
72 }
73
74 DEFINE_ENCODER(Rd, 5, 0, unsigned, uint32_t)
75 DEFINE_ENCODER(Rm, 5, 16, unsigned, uint32_t)
76 DEFINE_ENCODER(Rn, 5, 5, unsigned, uint32_t)
77 DEFINE_ENCODER(cond2, 4, 0, unsigned, uint32_t)
78 DEFINE_ENCODER(simm9, 9, 12, signed, ptrdiff_t)
79 DEFINE_ENCODER(imm12, 12, 10, unsigned, uint32_t)
80 DEFINE_ENCODER(imm16, 16, 5, unsigned, uint32_t)
81 DEFINE_ENCODER(simm19, 19, 5, signed, ptrdiff_t)
82 DEFINE_ENCODER(simm26, 26, 0, signed, ptrdiff_t)
83 DEFINE_ENCODER(immr, 6, 16, unsigned, uint32_t)
84 DEFINE_ENCODER(imms, 6, 10, unsigned, uint32_t)
85 DEFINE_ENCODER(size, 2, 22, unsigned, uint32_t)
86
87 #define DEFINE_PATCHABLE_INSTRUCTION(name, kind, RELOC, rsh) \
88 static int32_t \
89 read_##name##_offset(uint32_t *loc) \
90 { \
91 return read_signed_bitfield(*loc, kind##_width, kind##_shift); \
92 } \
93 static int offset_in_##name##_range(ptrdiff_t diff, int flags) maybe_unused; \
94 static int \
95 offset_in_##name##_range(ptrdiff_t diff, int flags) \
96 { \
97 return in_signed_range(diff, kind##_width); \
98 } \
99 static void \
100 patch_##name##_offset(uint32_t *loc, ptrdiff_t diff) \
101 { \
102 *loc = write_##kind##_bitfield(*loc, diff); \
103 } \
104 static jit_reloc_t \
105 emit_##name(jit_state_t *_jit, uint32_t inst) \
106 { \
107 while (1) { \
108 jit_reloc_t ret = jit_reloc (_jit, JIT_RELOC_##RELOC, 0, \
109 _jit->pc.uc, _jit->pc.uc, rsh); \
110 if (add_pending_literal(_jit, ret, kind##_width - 1)) { \
111 emit_u32(_jit, inst); \
112 return ret; \
113 } \
114 } \
115 }
116
117 #define DEFINE_PATCHABLE_INSTRUCTIONS(name, kind, RELOC, rsh) \
118 DEFINE_PATCHABLE_INSTRUCTION(name, kind, RELOC, rsh); \
119 DEFINE_PATCHABLE_INSTRUCTION(veneer_##name, kind, RELOC, rsh);
120
121 DEFINE_PATCHABLE_INSTRUCTIONS(jmp, simm26, JMP_WITH_VENEER, 2);
122 DEFINE_PATCHABLE_INSTRUCTIONS(jcc, simm19, JCC_WITH_VENEER, 2);
123 DEFINE_PATCHABLE_INSTRUCTION(load_from_pool, simm19, LOAD_FROM_POOL, 2);
124
125 struct veneer
126 {
127 uint32_t ldr;
128 uint32_t br;
129 uint64_t addr;
130 };
131
132 static void
patch_veneer(uint32_t * loc,jit_pointer_t addr)133 patch_veneer(uint32_t *loc, jit_pointer_t addr)
134 {
135 struct veneer *v = (struct veneer*) loc;
136 v->addr = (uint64_t) addr;
137 }
138
139 #include "aarch64-cpu.c"
140 #include "aarch64-fpu.c"
141
142 static const jit_gpr_t abi_gpr_args[] = {
143 _X0, _X1, _X2, _X3, _X4, _X5, _X6, _X7
144 };
145
146 static const jit_fpr_t abi_fpr_args[] = {
147 _D0, _D1, _D2, _D3, _D4, _D5, _D6, _D7
148 };
149
150 static const int abi_gpr_arg_count = sizeof(abi_gpr_args) / sizeof(abi_gpr_args[0]);
151 static const int abi_fpr_arg_count = sizeof(abi_fpr_args) / sizeof(abi_fpr_args[0]);
152
153 struct abi_arg_iterator
154 {
155 const jit_operand_t *args;
156 size_t argc;
157
158 size_t arg_idx;
159 size_t gpr_idx;
160 size_t fpr_idx;
161 size_t stack_size;
162 size_t stack_padding;
163 };
164
165 static size_t page_size;
166
167 jit_bool_t
jit_get_cpu(void)168 jit_get_cpu(void)
169 {
170 page_size = sysconf(_SC_PAGE_SIZE);
171 return 1;
172 }
173
174 jit_bool_t
jit_init(jit_state_t * _jit)175 jit_init(jit_state_t *_jit)
176 {
177 return 1;
178 }
179
180 static size_t
jit_initial_frame_size(void)181 jit_initial_frame_size (void)
182 {
183 return 0;
184 }
185
186 static void
reset_abi_arg_iterator(struct abi_arg_iterator * iter,size_t argc,const jit_operand_t * args)187 reset_abi_arg_iterator(struct abi_arg_iterator *iter, size_t argc,
188 const jit_operand_t *args)
189 {
190 memset(iter, 0, sizeof *iter);
191 iter->argc = argc;
192 iter->args = args;
193 }
194
195 static void
next_abi_arg(struct abi_arg_iterator * iter,jit_operand_t * arg)196 next_abi_arg(struct abi_arg_iterator *iter, jit_operand_t *arg)
197 {
198 ASSERT(iter->arg_idx < iter->argc);
199 enum jit_operand_abi abi = iter->args[iter->arg_idx].abi;
200 if (is_gpr_arg(abi) && iter->gpr_idx < abi_gpr_arg_count) {
201 *arg = jit_operand_gpr (abi, abi_gpr_args[iter->gpr_idx++]);
202 } else if (is_fpr_arg(abi) && iter->fpr_idx < abi_fpr_arg_count) {
203 *arg = jit_operand_fpr (abi, abi_fpr_args[iter->fpr_idx++]);
204 } else {
205 *arg = jit_operand_mem (abi, JIT_SP, iter->stack_size);
206 iter->stack_size += 8;
207 }
208 iter->arg_idx++;
209 }
210
211 static void
jit_flush(void * fptr,void * tptr)212 jit_flush(void *fptr, void *tptr)
213 {
214 jit_word_t f = (jit_word_t)fptr & -page_size;
215 jit_word_t t = (((jit_word_t)tptr) + page_size - 1) & -page_size;
216 __clear_cache((void *)f, (void *)t);
217 }
218
219 static inline size_t
jit_stack_alignment(void)220 jit_stack_alignment(void)
221 {
222 return 16;
223 }
224
225 static void
jit_try_shorten(jit_state_t * _jit,jit_reloc_t reloc,jit_pointer_t addr)226 jit_try_shorten(jit_state_t *_jit, jit_reloc_t reloc, jit_pointer_t addr)
227 {
228 }
229
230 static void*
bless_function_pointer(void * ptr)231 bless_function_pointer(void *ptr)
232 {
233 return ptr;
234 }
235