kobj_machdep.c revision 1.6 1 1.6 skrll /* $NetBSD: kobj_machdep.c,v 1.6 2020/12/11 18:03:33 skrll Exp $ */
2 1.1 ryo
3 1.1 ryo /*
4 1.1 ryo * Copyright (c) 2018 Ryo Shimizu <ryo (at) nerv.org>
5 1.1 ryo * All rights reserved.
6 1.1 ryo *
7 1.1 ryo * Redistribution and use in source and binary forms, with or without
8 1.1 ryo * modification, are permitted provided that the following conditions
9 1.1 ryo * are met:
10 1.1 ryo * 1. Redistributions of source code must retain the above copyright
11 1.1 ryo * notice, this list of conditions and the following disclaimer.
12 1.1 ryo * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 ryo * notice, this list of conditions and the following disclaimer in the
14 1.1 ryo * documentation and/or other materials provided with the distribution.
15 1.1 ryo *
16 1.1 ryo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 ryo * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 1.1 ryo * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 1.1 ryo * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 1.1 ryo * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 1.1 ryo * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 1.1 ryo * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 ryo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 1.1 ryo * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25 1.1 ryo * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 ryo * POSSIBILITY OF SUCH DAMAGE.
27 1.1 ryo */
28 1.1 ryo
29 1.1 ryo #include <sys/cdefs.h>
30 1.6 skrll __KERNEL_RCSID(0, "$NetBSD: kobj_machdep.c,v 1.6 2020/12/11 18:03:33 skrll Exp $");
31 1.1 ryo
32 1.1 ryo #define ELFSIZE ARCH_ELFSIZE
33 1.1 ryo
34 1.1 ryo #include "opt_ddb.h"
35 1.1 ryo
36 1.1 ryo #include <sys/param.h>
37 1.3 jmcneill #include <sys/kernel.h>
38 1.1 ryo #include <sys/kobj.h>
39 1.1 ryo #include <sys/exec.h>
40 1.1 ryo #include <sys/exec_elf.h>
41 1.1 ryo #include <sys/errno.h>
42 1.1 ryo #include <sys/queue.h>
43 1.1 ryo #include <sys/tree.h>
44 1.3 jmcneill #include <sys/xcall.h>
45 1.1 ryo
46 1.6 skrll #include <arm/cpufunc.h>
47 1.1 ryo
48 1.1 ryo /* #define KOBJ_MACHDEP_DEBUG */
49 1.1 ryo
50 1.1 ryo #ifdef KOBJ_MACHDEP_DEBUG
51 1.1 ryo #ifdef DDB
52 1.1 ryo #include <aarch64/db_machdep.h> /* for strdisasm() */
53 1.1 ryo #endif
54 1.1 ryo
55 1.1 ryo struct rtypeinfo {
56 1.1 ryo Elf_Word rtype;
57 1.1 ryo const char *name;
58 1.1 ryo };
59 1.1 ryo
60 1.1 ryo static const struct rtypeinfo rtypetbl[] = {
61 1.1 ryo { R_AARCH64_ABS64, "R_AARCH64_ABS64" },
62 1.1 ryo { R_AARCH64_ADD_ABS_LO12_NC, "R_AARCH64_ADD_ABS_LO12_NC" },
63 1.1 ryo { R_AARCH_LDST64_ABS_LO12_NC, "R_AARCH64_LDST64_ABS_LO12_NC" },
64 1.1 ryo { R_AARCH_LDST32_ABS_LO12_NC, "R_AARCH64_LDST32_ABS_LO12_NC" },
65 1.1 ryo { R_AARCH_LDST16_ABS_LO12_NC, "R_AARCH64_LDST16_ABS_LO12_NC" },
66 1.1 ryo { R_AARCH64_LDST8_ABS_LO12_NC, "R_AARCH64_LDST8_ABS_LO12_NC" },
67 1.1 ryo { R_AARCH64_ADR_PREL_PG_HI21_NC, "R_AARCH64_ADR_PREL_PG_HI21_NC"},
68 1.1 ryo { R_AARCH64_ADR_PREL_PG_HI21, "R_AARCH64_ADR_PREL_PG_HI21" },
69 1.1 ryo { R_AARCH_JUMP26, "R_AARCH64_JUMP26" },
70 1.1 ryo { R_AARCH_CALL26, "R_AARCH64_CALL26" },
71 1.1 ryo { R_AARCH64_PREL32, "R_AARCH64_PREL32" },
72 1.1 ryo { R_AARCH64_PREL16, "R_AARCH64_PREL16" }
73 1.1 ryo };
74 1.1 ryo
75 1.1 ryo static const char *
76 1.1 ryo strrtype(Elf_Word rtype)
77 1.1 ryo {
78 1.1 ryo int i;
79 1.1 ryo static char buf[64];
80 1.1 ryo
81 1.1 ryo for (i = 0; i < __arraycount(rtypetbl); i++) {
82 1.1 ryo if (rtypetbl[i].rtype == rtype)
83 1.1 ryo return rtypetbl[i].name;
84 1.1 ryo }
85 1.1 ryo snprintf(buf, sizeof(buf), "RELOCATION-TYPE-%d", rtype);
86 1.1 ryo return buf;
87 1.1 ryo }
88 1.1 ryo #endif /* KOBJ_MACHDEP_DEBUG */
89 1.1 ryo
90 1.1 ryo static inline bool
91 1.1 ryo checkalign(Elf_Addr addr, int alignbyte, void *where, Elf64_Addr off)
92 1.1 ryo {
93 1.1 ryo if ((addr & (alignbyte - 1)) != 0) {
94 1.1 ryo printf("kobj_reloc: Relocation 0x%jx unaligned at %p"
95 1.1 ryo " (base+0x%jx). must be aligned %d\n",
96 1.1 ryo (uintptr_t)addr, where, off, alignbyte);
97 1.1 ryo return true;
98 1.1 ryo }
99 1.1 ryo return false;
100 1.1 ryo }
101 1.1 ryo
102 1.1 ryo static inline bool
103 1.1 ryo checkoverflow(Elf_Addr addr, int bitwidth, Elf_Addr targetaddr,
104 1.1 ryo const char *bitscale, void *where, Elf64_Addr off)
105 1.1 ryo {
106 1.1 ryo const Elf_Addr mask = ~__BITS(bitwidth - 1, 0);
107 1.1 ryo
108 1.1 ryo if (((addr & mask) != 0) && ((addr & mask) != mask)) {
109 1.1 ryo printf("kobj_reloc: Relocation 0x%jx too far from %p"
110 1.1 ryo " (base+0x%jx) for %dbit%s\n",
111 1.1 ryo (uintptr_t)targetaddr, where, off, bitwidth, bitscale);
112 1.1 ryo return true;
113 1.1 ryo }
114 1.1 ryo return false;
115 1.1 ryo }
116 1.1 ryo
117 1.1 ryo #define WIDTHMASK(w) (0xffffffffffffffffUL >> (64 - (w)))
118 1.1 ryo
119 1.1 ryo int
120 1.1 ryo kobj_reloc(kobj_t ko, uintptr_t relocbase, const void *data,
121 1.1 ryo bool isrela, bool local)
122 1.1 ryo {
123 1.1 ryo Elf_Addr saddr, addend, raddr, val;
124 1.1 ryo Elf64_Addr off, *where;
125 1.1 ryo Elf32_Addr *where32;
126 1.1 ryo uint16_t *where16;
127 1.1 ryo Elf_Word rtype, symidx;
128 1.1 ryo const Elf_Rela *rela;
129 1.1 ryo int error;
130 1.1 ryo uint32_t *insn, immhi, immlo, shift;
131 1.1 ryo bool nc = false;
132 1.1 ryo #ifdef KOBJ_MACHDEP_DEBUG
133 1.1 ryo #ifdef DDB
134 1.1 ryo char disasmbuf[256];
135 1.1 ryo #endif
136 1.1 ryo Elf_Addr old;
137 1.1 ryo #endif /* KOBJ_MACHDEP_DEBUG */
138 1.1 ryo
139 1.1 ryo
140 1.1 ryo #ifdef KOBJ_MACHDEP_DEBUG
141 1.1 ryo printf("%s:%d: ko=%p, relocbase=0x%jx, data=%p"
142 1.1 ryo ", isrela=%d, local=%d\n", __func__, __LINE__,
143 1.1 ryo ko, relocbase, data, isrela, local);
144 1.1 ryo #endif /* KOBJ_MACHDEP_DEBUG */
145 1.1 ryo
146 1.1 ryo if (!isrela) {
147 1.1 ryo printf("kobj_reloc: REL relocations not supported");
148 1.1 ryo error = 1;
149 1.1 ryo goto done;
150 1.1 ryo }
151 1.1 ryo
152 1.1 ryo rela = (const Elf_Rela *)data;
153 1.1 ryo addend = rela->r_addend;
154 1.1 ryo rtype = ELF_R_TYPE(rela->r_info);
155 1.1 ryo symidx = ELF_R_SYM(rela->r_info);
156 1.1 ryo off = rela->r_offset;
157 1.1 ryo where = (Elf_Addr *)(relocbase + off);
158 1.1 ryo
159 1.1 ryo /* pointer to 32bit, 16bit, and instruction */
160 1.1 ryo where32 = (void *)where;
161 1.1 ryo where16 = (void *)where;
162 1.1 ryo insn = (uint32_t *)where;
163 1.1 ryo
164 1.1 ryo /* no need to lookup any symbols */
165 1.1 ryo switch (rtype) {
166 1.1 ryo case R_AARCH64_NONE:
167 1.1 ryo case R_AARCH64_NONE2:
168 1.1 ryo return 0;
169 1.1 ryo }
170 1.1 ryo
171 1.1 ryo error = kobj_sym_lookup(ko, symidx, &saddr);
172 1.1 ryo if (error != 0) {
173 1.1 ryo printf("kobj_reloc: symidx %d lookup failure."
174 1.1 ryo " relocation type %d at %p (base+0x%jx)\n",
175 1.1 ryo symidx, rtype, where, off);
176 1.1 ryo goto done;
177 1.1 ryo }
178 1.1 ryo
179 1.1 ryo #ifdef KOBJ_MACHDEP_DEBUG
180 1.1 ryo printf("%s:%d: symidx=%d, saddr=0x%jx, addend=0x%jx\n",
181 1.1 ryo __func__, __LINE__, symidx, (uintptr_t)saddr, (uintptr_t)addend);
182 1.1 ryo printf("%s:%d: rtype=%s, where=%p (base+0x%jx)\n",
183 1.1 ryo __func__, __LINE__, strrtype(rtype), where, off);
184 1.1 ryo old = *where;
185 1.1 ryo #ifdef DDB
186 1.1 ryo snprintf(disasmbuf, sizeof(disasmbuf), "%08x %s",
187 1.5 ryo le32toh(*insn), strdisasm((vaddr_t)insn), 0);
188 1.1 ryo #endif
189 1.1 ryo #endif /* KOBJ_MACHDEP_DEBUG */
190 1.1 ryo
191 1.1 ryo switch (rtype) {
192 1.1 ryo case R_AARCH64_ABS64:
193 1.1 ryo /*
194 1.1 ryo * S + A
195 1.1 ryo * e.g.) .quad <sym>+addend
196 1.1 ryo */
197 1.1 ryo *where = saddr + addend;
198 1.1 ryo break;
199 1.1 ryo case R_AARCH64_ABS32:
200 1.1 ryo /*
201 1.1 ryo * S + A
202 1.1 ryo * e.g.) .word <sym>+addend
203 1.1 ryo */
204 1.1 ryo *where32 = saddr + addend;
205 1.1 ryo break;
206 1.1 ryo case R_AARCH64_ABS16:
207 1.1 ryo /*
208 1.1 ryo * S + A
209 1.1 ryo * e.g.) .short <sym>+addend
210 1.1 ryo */
211 1.1 ryo *where16 = saddr + addend;
212 1.1 ryo break;
213 1.1 ryo case R_AARCH64_ADD_ABS_LO12_NC:
214 1.1 ryo case R_AARCH64_LDST8_ABS_LO12_NC:
215 1.1 ryo case R_AARCH_LDST16_ABS_LO12_NC:
216 1.1 ryo case R_AARCH_LDST32_ABS_LO12_NC:
217 1.1 ryo case R_AARCH_LDST64_ABS_LO12_NC:
218 1.1 ryo switch (rtype) {
219 1.1 ryo case R_AARCH64_ADD_ABS_LO12_NC:
220 1.1 ryo case R_AARCH64_LDST8_ABS_LO12_NC:
221 1.1 ryo shift = 0;
222 1.1 ryo break;
223 1.1 ryo case R_AARCH_LDST16_ABS_LO12_NC:
224 1.1 ryo shift = 1;
225 1.1 ryo break;
226 1.1 ryo case R_AARCH_LDST32_ABS_LO12_NC:
227 1.1 ryo shift = 2;
228 1.1 ryo break;
229 1.1 ryo case R_AARCH_LDST64_ABS_LO12_NC:
230 1.1 ryo shift = 3;
231 1.1 ryo break;
232 1.1 ryo default:
233 1.1 ryo panic("illegal rtype: %d\n", rtype);
234 1.1 ryo }
235 1.1 ryo /*
236 1.1 ryo * S + A
237 1.1 ryo * e.g.) add x0,x0,#:lo12:<sym>+<addend>
238 1.1 ryo * ldrb w0,[x0,#:lo12:<sym>+<addend>]
239 1.1 ryo * ldrh w0,[x0,#:lo12:<sym>+<addend>]
240 1.1 ryo * ldr w0,[x0,#:lo12:<sym>+<addend>]
241 1.1 ryo * ldr x0,[x0,#:lo12:<sym>+<addend>]
242 1.1 ryo */
243 1.1 ryo val = saddr + addend;
244 1.1 ryo if (checkalign(val, 1 << shift, where, off)) {
245 1.1 ryo error = 1;
246 1.1 ryo break;
247 1.1 ryo }
248 1.1 ryo val &= WIDTHMASK(12);
249 1.1 ryo val >>= shift;
250 1.5 ryo *insn = htole32(
251 1.5 ryo (le32toh(*insn) & ~__BITS(21,10)) | (val << 10));
252 1.1 ryo break;
253 1.1 ryo
254 1.1 ryo case R_AARCH64_ADR_PREL_PG_HI21_NC:
255 1.1 ryo nc = true;
256 1.1 ryo /* FALLTHRU */
257 1.1 ryo case R_AARCH64_ADR_PREL_PG_HI21:
258 1.1 ryo /*
259 1.1 ryo * Page(S + A) - Page(P)
260 1.1 ryo * e.g.) adrp x0,<sym>+<addend>
261 1.1 ryo */
262 1.1 ryo val = saddr + addend;
263 1.1 ryo val = val >> 12;
264 1.1 ryo raddr = val << 12;
265 1.1 ryo val -= (uintptr_t)where >> 12;
266 1.2 ryo if (!nc && checkoverflow(val, 21, raddr, " x 4k", where, off)) {
267 1.1 ryo error = 1;
268 1.1 ryo break;
269 1.1 ryo }
270 1.1 ryo immlo = val & WIDTHMASK(2);
271 1.1 ryo immhi = (val >> 2) & WIDTHMASK(19);
272 1.5 ryo *insn = htole32((le32toh(*insn) &
273 1.5 ryo ~(__BITS(30,29) | __BITS(23,5))) |
274 1.5 ryo (immlo << 29) | (immhi << 5));
275 1.1 ryo break;
276 1.1 ryo
277 1.1 ryo case R_AARCH_JUMP26:
278 1.1 ryo case R_AARCH_CALL26:
279 1.1 ryo /*
280 1.1 ryo * S + A - P
281 1.1 ryo * e.g.) b <sym>+<addend>
282 1.1 ryo * bl <sym>+<addend>
283 1.1 ryo */
284 1.1 ryo raddr = saddr + addend;
285 1.1 ryo val = raddr - (uintptr_t)where;
286 1.1 ryo if (checkalign(val, 4, where, off)) {
287 1.1 ryo error = 1;
288 1.1 ryo break;
289 1.1 ryo }
290 1.1 ryo val = (intptr_t)val >> 2;
291 1.2 ryo if (checkoverflow(val, 26, raddr, " word", where, off)) {
292 1.1 ryo error = 1;
293 1.1 ryo break;
294 1.1 ryo }
295 1.1 ryo val &= WIDTHMASK(26);
296 1.5 ryo *insn = htole32((le32toh(*insn) & ~__BITS(25,0)) | val);
297 1.1 ryo break;
298 1.1 ryo
299 1.1 ryo case R_AARCH64_PREL64:
300 1.1 ryo /*
301 1.1 ryo * S + A - P
302 1.1 ryo * e.g.) 1: .quad <sym>+<addend>-1b
303 1.1 ryo */
304 1.1 ryo raddr = saddr + addend;
305 1.1 ryo val = raddr - (uintptr_t)where;
306 1.2 ryo if (checkoverflow(val, 64, raddr, "", where, off)) {
307 1.1 ryo error = 1;
308 1.1 ryo break;
309 1.1 ryo }
310 1.1 ryo *where = val;
311 1.1 ryo break;
312 1.1 ryo case R_AARCH64_PREL32:
313 1.1 ryo /*
314 1.1 ryo * S + A - P
315 1.1 ryo * e.g.) 1: .word <sym>+<addend>-1b
316 1.1 ryo */
317 1.1 ryo raddr = saddr + addend;
318 1.1 ryo val = raddr - (uintptr_t)where;
319 1.2 ryo if (checkoverflow(val, 32, raddr, "", where, off)) {
320 1.1 ryo error = 1;
321 1.1 ryo break;
322 1.1 ryo }
323 1.1 ryo *where32 = val;
324 1.1 ryo break;
325 1.1 ryo case R_AARCH64_PREL16:
326 1.1 ryo /*
327 1.1 ryo * S + A - P
328 1.1 ryo * e.g.) 1: .short <sym>+<addend>-1b
329 1.1 ryo */
330 1.1 ryo raddr = saddr + addend;
331 1.1 ryo val = raddr - (uintptr_t)where;
332 1.2 ryo if (checkoverflow(val, 16, raddr, "", where, off)) {
333 1.1 ryo error = 1;
334 1.1 ryo break;
335 1.1 ryo }
336 1.1 ryo *where16 = val;
337 1.1 ryo break;
338 1.1 ryo default:
339 1.1 ryo printf("kobj_reloc: unsupported relocation type %d"
340 1.1 ryo " at %p (base+0x%jx) symidx %u\n",
341 1.1 ryo rtype, where, off, symidx);
342 1.1 ryo error = 1;
343 1.1 ryo break;
344 1.1 ryo }
345 1.1 ryo
346 1.1 ryo #ifdef KOBJ_MACHDEP_DEBUG
347 1.1 ryo printf("%s: reloc\n", __func__);
348 1.1 ryo printf("%s: *where %016jx\n", __func__, (uintptr_t)old);
349 1.1 ryo printf("%s: -> %016jx\n", __func__, (uintptr_t)*where);
350 1.1 ryo #ifdef DDB
351 1.1 ryo printf("%s: insn %s\n", __func__, disasmbuf);
352 1.1 ryo printf("%s: -> %08x %s\n", __func__,
353 1.5 ryo le32toh(*insn), strdisasm((vaddr_t)insn, 0));
354 1.1 ryo #endif
355 1.1 ryo printf("\n");
356 1.1 ryo #endif /* KOBJ_MACHDEP_DEBUG */
357 1.1 ryo
358 1.1 ryo done:
359 1.1 ryo if (error != 0)
360 1.1 ryo return -1;
361 1.1 ryo return 0;
362 1.1 ryo }
363 1.1 ryo
364 1.3 jmcneill static void
365 1.3 jmcneill kobj_idcache_wbinv_all(void)
366 1.3 jmcneill {
367 1.3 jmcneill cpu_idcache_wbinv_all();
368 1.3 jmcneill }
369 1.3 jmcneill
370 1.1 ryo int
371 1.1 ryo kobj_machdep(kobj_t ko, void *base, size_t size, bool load)
372 1.1 ryo {
373 1.3 jmcneill uint64_t where;
374 1.3 jmcneill
375 1.3 jmcneill if (load) {
376 1.3 jmcneill if (cold) {
377 1.3 jmcneill kobj_idcache_wbinv_all();
378 1.3 jmcneill } else {
379 1.3 jmcneill where = xc_broadcast(0,
380 1.3 jmcneill (xcfunc_t)kobj_idcache_wbinv_all, NULL, NULL);
381 1.3 jmcneill xc_wait(where);
382 1.3 jmcneill }
383 1.3 jmcneill }
384 1.3 jmcneill
385 1.1 ryo return 0;
386 1.1 ryo }
387