Home | History | Annotate | Line # | Download | only in aarch64
      1  1.9    andvar /*	$NetBSD: kobj_machdep.c,v 1.9 2024/02/16 17:18:19 andvar Exp $	*/
      2  1.1       ryo 
      3  1.1       ryo /*
      4  1.8   msaitoh  * Copyright (c) 2018 Ryo Shimizu
      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.9    andvar __KERNEL_RCSID(0, "$NetBSD: kobj_machdep.c,v 1.9 2024/02/16 17:18:19 andvar 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.7     skrll 	const Elf_Sym *sym = kobj_symbol(ko, symidx);
    172  1.7     skrll 
    173  1.7     skrll 	if (!local && ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
    174  1.7     skrll 		return 0;
    175  1.7     skrll 	}
    176  1.7     skrll 
    177  1.1       ryo 	error = kobj_sym_lookup(ko, symidx, &saddr);
    178  1.1       ryo 	if (error != 0) {
    179  1.1       ryo 		printf("kobj_reloc: symidx %d lookup failure."
    180  1.1       ryo 		    " relocation type %d at %p (base+0x%jx)\n",
    181  1.1       ryo 		    symidx, rtype, where, off);
    182  1.1       ryo 		goto done;
    183  1.1       ryo 	}
    184  1.1       ryo 
    185  1.1       ryo #ifdef KOBJ_MACHDEP_DEBUG
    186  1.1       ryo 	printf("%s:%d: symidx=%d, saddr=0x%jx, addend=0x%jx\n",
    187  1.1       ryo 	    __func__, __LINE__, symidx, (uintptr_t)saddr, (uintptr_t)addend);
    188  1.1       ryo 	printf("%s:%d: rtype=%s, where=%p (base+0x%jx)\n",
    189  1.1       ryo 	    __func__, __LINE__, strrtype(rtype), where, off);
    190  1.1       ryo 	old = *where;
    191  1.1       ryo #ifdef DDB
    192  1.1       ryo 	snprintf(disasmbuf, sizeof(disasmbuf), "%08x %s",
    193  1.9    andvar 	    le32toh(*insn), strdisasm((vaddr_t)insn, 0));
    194  1.1       ryo #endif
    195  1.1       ryo #endif /* KOBJ_MACHDEP_DEBUG */
    196  1.1       ryo 
    197  1.1       ryo 	switch (rtype) {
    198  1.1       ryo 	case R_AARCH64_ABS64:
    199  1.1       ryo 		/*
    200  1.1       ryo 		 * S + A
    201  1.1       ryo 		 *  e.g.) .quad <sym>+addend
    202  1.1       ryo 		 */
    203  1.1       ryo 		*where = saddr + addend;
    204  1.1       ryo 		break;
    205  1.1       ryo 	case R_AARCH64_ABS32:
    206  1.1       ryo 		/*
    207  1.1       ryo 		 * S + A
    208  1.1       ryo 		 *  e.g.) .word <sym>+addend
    209  1.1       ryo 		 */
    210  1.1       ryo 		*where32 = saddr + addend;
    211  1.1       ryo 		break;
    212  1.1       ryo 	case R_AARCH64_ABS16:
    213  1.1       ryo 		/*
    214  1.1       ryo 		 * S + A
    215  1.1       ryo 		 *  e.g.) .short <sym>+addend
    216  1.1       ryo 		 */
    217  1.1       ryo 		*where16 = saddr + addend;
    218  1.1       ryo 		break;
    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 	case R_AARCH_LDST16_ABS_LO12_NC:
    222  1.1       ryo 	case R_AARCH_LDST32_ABS_LO12_NC:
    223  1.1       ryo 	case R_AARCH_LDST64_ABS_LO12_NC:
    224  1.1       ryo 		switch (rtype) {
    225  1.1       ryo 		case R_AARCH64_ADD_ABS_LO12_NC:
    226  1.1       ryo 		case R_AARCH64_LDST8_ABS_LO12_NC:
    227  1.1       ryo 			shift = 0;
    228  1.1       ryo 			break;
    229  1.1       ryo 		case R_AARCH_LDST16_ABS_LO12_NC:
    230  1.1       ryo 			shift = 1;
    231  1.1       ryo 			break;
    232  1.1       ryo 		case R_AARCH_LDST32_ABS_LO12_NC:
    233  1.1       ryo 			shift = 2;
    234  1.1       ryo 			break;
    235  1.1       ryo 		case R_AARCH_LDST64_ABS_LO12_NC:
    236  1.1       ryo 			shift = 3;
    237  1.1       ryo 			break;
    238  1.1       ryo 		default:
    239  1.1       ryo 			panic("illegal rtype: %d\n", rtype);
    240  1.1       ryo 		}
    241  1.1       ryo 		/*
    242  1.1       ryo 		 * S + A
    243  1.1       ryo 		 *  e.g.) add  x0,x0,#:lo12:<sym>+<addend>
    244  1.1       ryo 		 *        ldrb w0,[x0,#:lo12:<sym>+<addend>]
    245  1.1       ryo 		 *        ldrh w0,[x0,#:lo12:<sym>+<addend>]
    246  1.1       ryo 		 *        ldr  w0,[x0,#:lo12:<sym>+<addend>]
    247  1.1       ryo 		 *        ldr  x0,[x0,#:lo12:<sym>+<addend>]
    248  1.1       ryo 		 */
    249  1.1       ryo 		val = saddr + addend;
    250  1.1       ryo 		if (checkalign(val, 1 << shift, where, off)) {
    251  1.1       ryo 			error = 1;
    252  1.1       ryo 			break;
    253  1.1       ryo 		}
    254  1.1       ryo 		val &= WIDTHMASK(12);
    255  1.1       ryo 		val >>= shift;
    256  1.5       ryo 		*insn = htole32(
    257  1.5       ryo 		    (le32toh(*insn) & ~__BITS(21,10)) | (val << 10));
    258  1.1       ryo 		break;
    259  1.1       ryo 
    260  1.1       ryo 	case R_AARCH64_ADR_PREL_PG_HI21_NC:
    261  1.1       ryo 		nc = true;
    262  1.1       ryo 		/* FALLTHRU */
    263  1.1       ryo 	case R_AARCH64_ADR_PREL_PG_HI21:
    264  1.1       ryo 		/*
    265  1.1       ryo 		 * Page(S + A) - Page(P)
    266  1.1       ryo 		 *  e.g.) adrp x0,<sym>+<addend>
    267  1.1       ryo 		 */
    268  1.1       ryo 		val = saddr + addend;
    269  1.1       ryo 		val = val >> 12;
    270  1.1       ryo 		raddr = val << 12;
    271  1.1       ryo 		val -= (uintptr_t)where >> 12;
    272  1.2       ryo 		if (!nc && checkoverflow(val, 21, raddr, " x 4k", where, off)) {
    273  1.1       ryo 			error = 1;
    274  1.1       ryo 			break;
    275  1.1       ryo 		}
    276  1.1       ryo 		immlo = val & WIDTHMASK(2);
    277  1.1       ryo 		immhi = (val >> 2) & WIDTHMASK(19);
    278  1.5       ryo 		*insn = htole32((le32toh(*insn) &
    279  1.5       ryo 		    ~(__BITS(30,29) | __BITS(23,5))) |
    280  1.5       ryo 		    (immlo << 29) | (immhi << 5));
    281  1.1       ryo 		break;
    282  1.1       ryo 
    283  1.1       ryo 	case R_AARCH_JUMP26:
    284  1.1       ryo 	case R_AARCH_CALL26:
    285  1.1       ryo 		/*
    286  1.1       ryo 		 * S + A - P
    287  1.1       ryo 		 *  e.g.) b <sym>+<addend>
    288  1.1       ryo 		 *        bl <sym>+<addend>
    289  1.1       ryo 		 */
    290  1.1       ryo 		raddr = saddr + addend;
    291  1.1       ryo 		val = raddr - (uintptr_t)where;
    292  1.1       ryo 		if (checkalign(val, 4, where, off)) {
    293  1.1       ryo 			error = 1;
    294  1.1       ryo 			break;
    295  1.1       ryo 		}
    296  1.1       ryo 		val = (intptr_t)val >> 2;
    297  1.2       ryo 		if (checkoverflow(val, 26, raddr, " word", where, off)) {
    298  1.1       ryo 			error = 1;
    299  1.1       ryo 			break;
    300  1.1       ryo 		}
    301  1.1       ryo 		val &= WIDTHMASK(26);
    302  1.5       ryo 		*insn = htole32((le32toh(*insn) & ~__BITS(25,0)) | val);
    303  1.1       ryo 		break;
    304  1.1       ryo 
    305  1.1       ryo 	case R_AARCH64_PREL64:
    306  1.1       ryo 		/*
    307  1.1       ryo 		 * S + A - P
    308  1.1       ryo 		 *  e.g.) 1: .quad <sym>+<addend>-1b
    309  1.1       ryo 		 */
    310  1.1       ryo 		raddr = saddr + addend;
    311  1.1       ryo 		val = raddr - (uintptr_t)where;
    312  1.2       ryo 		if (checkoverflow(val, 64, raddr, "", where, off)) {
    313  1.1       ryo 			error = 1;
    314  1.1       ryo 			break;
    315  1.1       ryo 		}
    316  1.1       ryo 		*where = val;
    317  1.1       ryo 		break;
    318  1.1       ryo 	case R_AARCH64_PREL32:
    319  1.1       ryo 		/*
    320  1.1       ryo 		 * S + A - P
    321  1.1       ryo 		 *  e.g.) 1: .word <sym>+<addend>-1b
    322  1.1       ryo 		 */
    323  1.1       ryo 		raddr = saddr + addend;
    324  1.1       ryo 		val = raddr - (uintptr_t)where;
    325  1.2       ryo 		if (checkoverflow(val, 32, raddr, "", where, off)) {
    326  1.1       ryo 			error = 1;
    327  1.1       ryo 			break;
    328  1.1       ryo 		}
    329  1.1       ryo 		*where32 = val;
    330  1.1       ryo 		break;
    331  1.1       ryo 	case R_AARCH64_PREL16:
    332  1.1       ryo 		/*
    333  1.1       ryo 		 * S + A - P
    334  1.1       ryo 		 *  e.g.) 1: .short <sym>+<addend>-1b
    335  1.1       ryo 		 */
    336  1.1       ryo 		raddr = saddr + addend;
    337  1.1       ryo 		val = raddr - (uintptr_t)where;
    338  1.2       ryo 		if (checkoverflow(val, 16, raddr, "", where, off)) {
    339  1.1       ryo 			error = 1;
    340  1.1       ryo 			break;
    341  1.1       ryo 		}
    342  1.1       ryo 		*where16 = val;
    343  1.1       ryo 		break;
    344  1.1       ryo 	default:
    345  1.1       ryo 		printf("kobj_reloc: unsupported relocation type %d"
    346  1.1       ryo 		    " at %p (base+0x%jx) symidx %u\n",
    347  1.1       ryo 		    rtype, where, off, symidx);
    348  1.1       ryo 		error = 1;
    349  1.1       ryo 		break;
    350  1.1       ryo 	}
    351  1.1       ryo 
    352  1.1       ryo #ifdef KOBJ_MACHDEP_DEBUG
    353  1.1       ryo 	printf("%s: reloc\n", __func__);
    354  1.1       ryo 	printf("%s:  *where %016jx\n", __func__, (uintptr_t)old);
    355  1.1       ryo 	printf("%s:      -> %016jx\n", __func__, (uintptr_t)*where);
    356  1.1       ryo #ifdef DDB
    357  1.1       ryo 	printf("%s:    insn %s\n", __func__, disasmbuf);
    358  1.1       ryo 	printf("%s:      -> %08x %s\n", __func__,
    359  1.5       ryo 	    le32toh(*insn), strdisasm((vaddr_t)insn, 0));
    360  1.1       ryo #endif
    361  1.1       ryo 	printf("\n");
    362  1.1       ryo #endif /* KOBJ_MACHDEP_DEBUG */
    363  1.1       ryo 
    364  1.1       ryo  done:
    365  1.1       ryo 	if (error != 0)
    366  1.1       ryo 		return -1;
    367  1.1       ryo 	return 0;
    368  1.1       ryo }
    369  1.1       ryo 
    370  1.3  jmcneill static void
    371  1.3  jmcneill kobj_idcache_wbinv_all(void)
    372  1.3  jmcneill {
    373  1.3  jmcneill 	cpu_idcache_wbinv_all();
    374  1.3  jmcneill }
    375  1.3  jmcneill 
    376  1.1       ryo int
    377  1.1       ryo kobj_machdep(kobj_t ko, void *base, size_t size, bool load)
    378  1.1       ryo {
    379  1.3  jmcneill 	uint64_t where;
    380  1.3  jmcneill 
    381  1.3  jmcneill 	if (load) {
    382  1.3  jmcneill 		if (cold) {
    383  1.3  jmcneill 			kobj_idcache_wbinv_all();
    384  1.3  jmcneill 		} else {
    385  1.3  jmcneill 			where = xc_broadcast(0,
    386  1.3  jmcneill 			    (xcfunc_t)kobj_idcache_wbinv_all, NULL, NULL);
    387  1.3  jmcneill 			xc_wait(where);
    388  1.3  jmcneill 		}
    389  1.3  jmcneill 	}
    390  1.3  jmcneill 
    391  1.1       ryo 	return 0;
    392  1.1       ryo }
    393