Home | History | Annotate | Line # | Download | only in arm32
      1 /*	$NetBSD: kobj_machdep.c,v 1.16 2023/04/28 07:33:56 skrll Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /*-
     30  * Copyright 1996-1998 John D. Polstra.
     31  * All rights reserved.
     32  *
     33  * Redistribution and use in source and binary forms, with or without
     34  * modification, are permitted provided that the following conditions
     35  * are met:
     36  * 1. Redistributions of source code must retain the above copyright
     37  *    notice, this list of conditions and the following disclaimer.
     38  * 2. Redistributions in binary form must reproduce the above copyright
     39  *    notice, this list of conditions and the following disclaimer in the
     40  *    documentation and/or other materials provided with the distribution.
     41  *
     42  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     43  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     44  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     45  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     46  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     47  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     48  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     49  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     50  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     51  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     52  */
     53 
     54 #include <sys/cdefs.h>
     55 __KERNEL_RCSID(0, "$NetBSD: kobj_machdep.c,v 1.16 2023/04/28 07:33:56 skrll Exp $");
     56 
     57 #define	ELFSIZE		ARCH_ELFSIZE
     58 
     59 #include <sys/param.h>
     60 
     61 #include <sys/exec.h>
     62 #include <sys/exec_elf.h>
     63 #include <sys/kmem.h>
     64 #include <sys/kobj.h>
     65 #include <sys/kobj_impl.h>
     66 #include <sys/ksyms.h>
     67 #include <sys/systm.h>
     68 
     69 #include <arm/cpufunc.h>
     70 #include <arm/locore.h>
     71 
     72 int
     73 kobj_reloc(kobj_t ko, uintptr_t relocbase, const void *data,
     74 	   bool isrela, bool local)
     75 {
     76 	Elf_Addr *where;
     77 	Elf_Addr addr;
     78 	Elf_Addr addend;
     79 	Elf_Word rtype, symidx;
     80 	const Elf_Rel *rel;
     81 	const Elf_Rela *rela;
     82 	int error;
     83 
     84 	if (isrela) {
     85 		rela = (const Elf_Rela *)data;
     86 		where = (Elf_Addr *) (relocbase + rela->r_offset);
     87 		addend = rela->r_addend;
     88 		rtype = ELF_R_TYPE(rela->r_info);
     89 		symidx = ELF_R_SYM(rela->r_info);
     90 	} else {
     91 		rel = (const Elf_Rel *)data;
     92 		where = (Elf_Addr *) (relocbase + rel->r_offset);
     93 		addend = *where;
     94 		rtype = ELF_R_TYPE(rel->r_info);
     95 		symidx = ELF_R_SYM(rel->r_info);
     96 	}
     97 
     98 	const Elf_Sym *sym = kobj_symbol(ko, symidx);
     99 
    100 	if (!local && ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
    101 		return 0;
    102 	}
    103 
    104 	switch (rtype) {
    105 	case R_ARM_NONE:	/* none */
    106 	case R_ARM_V4BX:	/* none */
    107 		return 0;
    108 
    109 	case R_ARM_ABS32:
    110 		error = kobj_sym_lookup(ko, symidx, &addr);
    111 		if (error)
    112 			break;
    113 		*where = addr + addend;
    114 		return 0;
    115 
    116 	case R_ARM_COPY:	/* none */
    117 		/* There shouldn't be copy relocations in kernel objects. */
    118 		break;
    119 
    120 	case R_ARM_JUMP_SLOT:
    121 		error = kobj_sym_lookup(ko, symidx, &addr);
    122 		if (error)
    123 			break;
    124 		*where = addr;
    125 		return 0;
    126 
    127 	case R_ARM_RELATIVE:	/* A + B */
    128 		addr = relocbase + addend;
    129 		if (*where != addr)
    130 			*where = addr;
    131 		return 0;
    132 
    133 	case R_ARM_MOVW_ABS_NC:	/* (S + A) | T */
    134 	case R_ARM_MOVT_ABS:
    135 		if ((*where & 0x0fb00000) != 0x03000000)
    136 			break;
    137 		error = kobj_sym_lookup(ko, symidx, &addr);
    138 		if (error)
    139 			break;
    140 		if (rtype == R_ARM_MOVT_ABS)
    141 			addr >>= 16;
    142 		*where = (*where & 0xfff0f000)
    143 		    | ((addr << 4) & 0x000f0000) | (addr & 0x00000fff);
    144 		return 0;
    145 
    146 	case R_ARM_CALL:	/* ((S + A) | T) -  P */
    147 	case R_ARM_JUMP24:
    148 	case R_ARM_PC24:	/* Deprecated */
    149 		if (local && (*where & 0x00ffffff) != 0x00fffffe)
    150 			return 0;
    151 
    152 		/* Remove the instruction from the 24 bit offset */
    153 		addend &= 0x00ffffff;
    154 
    155 		/* Sign extend if necessary */
    156 		if (addend & 0x00800000)
    157 			addend |= 0xff000000;
    158 
    159 		addend <<= 2;
    160 
    161 		error = kobj_sym_lookup(ko, symidx, &addr);
    162 		if (error)
    163 			break;
    164 
    165 		addend += (uintptr_t)addr - (uintptr_t)where;
    166 
    167 		if (addend & 3) {
    168 			printf ("Relocation %x unaligned @ %p\n", addend, where);
    169 			return -1;
    170 		}
    171 
    172 		if ((addend & 0xfe000000) != 0x00000000 &&
    173 		    (addend & 0xfe000000) != 0xfe000000) {
    174 			printf ("Relocation %x too far @ %p\n", addend, where);
    175 			return -1;
    176 		}
    177 		*where = (*where & 0xff000000) | ((addend >> 2) & 0x00ffffff);
    178 		return 0;
    179 
    180 	case R_ARM_REL32:	/* ((S + A) | T) -  P */
    181 		/* T = 0 for now */
    182 		error = kobj_sym_lookup(ko, symidx, &addr);
    183 		if (error)
    184 			break;
    185 
    186 		addend += (uintptr_t)addr - (uintptr_t)where;
    187 		*where = addend;
    188 		return 0;
    189 
    190 	case R_ARM_PREL31:	/* ((S + A) | T) -  P */
    191 		/* Sign extend if necessary */
    192 		if (addend & 0x40000000)
    193 			addend |= 0xc0000000;
    194 		/* T = 0 for now */
    195 		error = kobj_sym_lookup(ko, symidx, &addr);
    196 		if (error)
    197 			break;
    198 
    199 		addend += (uintptr_t)addr - (uintptr_t)where;
    200 
    201 		if ((addend & 0x80000000) != 0x00000000 &&
    202 		    (addend & 0x80000000) != 0x80000000) {
    203 			printf ("Relocation %x too far @ %p\n", addend, where);
    204 			return -1;
    205 		}
    206 
    207 		*where = (*where & 0x80000000) | (addend & 0x7fffffff);
    208 
    209 	default:
    210 		break;
    211 	}
    212 
    213 	printf("kobj_reloc: unexpected/invalid relocation type %d @ %p symidx %u\n",
    214 	    rtype, where, symidx);
    215 	return -1;
    216 }
    217 
    218 #ifdef _ARM_ARCH_BE8
    219 
    220 enum be8_magic_sym_type {
    221 	Other, ArmStart, ThumbStart, DataStart
    222 };
    223 
    224 struct be8_marker {
    225 	enum be8_magic_sym_type type;
    226 	void *addr;
    227 };
    228 
    229 struct be8_marker_list {
    230 	size_t cnt;
    231 	struct be8_marker *markers;
    232 };
    233 
    234 /*
    235  * See ELF for the ARM Architecture, Section 4.5.5: Mapping Symbols
    236  * ARM reserves $a/$d/$t (and variants like $a.2) to mark start of
    237  * arm/thumb code sections to allow conversion from ARM32-EB to -BE8
    238  * format.
    239  */
    240 static enum be8_magic_sym_type
    241 be8_sym_type(const char *name, int info)
    242 {
    243 	if (ELF_ST_BIND(info) != STB_LOCAL)
    244 		return Other;
    245 	if (ELF_ST_TYPE(info) != STT_NOTYPE)
    246 		return Other;
    247 	if (name[0] != '$' || name[1] == '\0' ||
    248 	    (name[2] != '\0' && name[2] != '.'))
    249 		return Other;
    250 
    251 	switch (name[1]) {
    252 	case 'a':
    253 		return ArmStart;
    254 	case 'd':
    255 		return DataStart;
    256 	case 't':
    257 		return ThumbStart;
    258 	default:
    259 		return Other;
    260 	}
    261 }
    262 
    263 static int
    264 be8_ksym_count(const char *name, int symindex, void *value, uint32_t size,
    265 	int info, void *cookie)
    266 {
    267 	size_t *res = cookie;
    268 	enum be8_magic_sym_type t = be8_sym_type(name, info);
    269 
    270 	if (t != Other)
    271 		(*res)++;
    272 	return 0;
    273 }
    274 
    275 static int
    276 be8_ksym_add(const char *name, int symindex, void *value, uint32_t size,
    277 	int info, void *cookie)
    278 {
    279 	size_t ndx;
    280 	struct be8_marker_list *list = cookie;
    281 	enum be8_magic_sym_type t = be8_sym_type(name, info);
    282 
    283 	if (t == Other)
    284 		return 0;
    285 
    286 	ndx = list->cnt++;
    287 	list->markers[ndx].type = t;
    288 	list->markers[ndx].addr = value;
    289 
    290 	return 0;
    291 }
    292 
    293 static int
    294 be8_ksym_comp(const void *a, const void *b)
    295 {
    296 	const struct be8_marker *ma = a, *mb = b;
    297 	uintptr_t va = (uintptr_t)ma->addr, vb = (uintptr_t)mb->addr;
    298 
    299 	if (va == vb)
    300 		return 0;
    301 	if (va < vb)
    302 		return -1;
    303 	return 1;
    304 }
    305 
    306 static void
    307 be8_ksym_swap(void *start, size_t size, const struct be8_marker_list *list)
    308 {
    309 	uintptr_t va_end = (uintptr_t)start + size;
    310 	size_t i;
    311 	uint32_t *p32, *p32_end, v32;
    312 	uint16_t *p16, *p16_end, v16;
    313 
    314 	/* find first relevant list entry */
    315 	for (i = 0; i < list->cnt; i++)
    316 		if (start <= list->markers[i].addr)
    317 			break;
    318 
    319 	/* swap all arm and thumb code parts of this section */
    320 	for ( ; i < list->cnt; i++) {
    321 		switch (list->markers[i].type) {
    322 		case ArmStart:
    323 			p32 = (uint32_t*)list->markers[i].addr;
    324 			p32_end = (uint32_t*)va_end;
    325 			if (i+1 < list->cnt) {
    326 				if ((uintptr_t)list->markers[i+1].addr
    327 				    < va_end)
    328 					p32_end = (uint32_t*)
    329 						list->markers[i+1].addr;
    330 			}
    331 			while (p32 < p32_end) {
    332 				v32 = bswap32(*p32);
    333 				*p32++ = v32;
    334 			}
    335 			break;
    336 		case ThumbStart:
    337 			p16 = (uint16_t*)list->markers[i].addr;
    338 			p16_end = (uint16_t*)va_end;
    339 			if (i+1 < list->cnt) {
    340 				if ((uintptr_t)list->markers[i+1].addr
    341 				    < va_end)
    342 					p16_end = (uint16_t*)
    343 						list->markers[i+1].addr;
    344 			}
    345 			while (p16 < p16_end) {
    346 				v16 = bswap16(*p16);
    347 				*p16++ = v16;
    348 			}
    349 			break;
    350 		default:
    351 			break;
    352 		}
    353 	}
    354 }
    355 
    356 static void
    357 kobj_be8_fixup(kobj_t ko)
    358 {
    359 	size_t relsym_cnt = 0, i, msize;
    360 	struct be8_marker_list list;
    361 	struct be8_marker tmp;
    362 
    363 	/*
    364 	 * Count all special relocations symbols
    365 	 */
    366 	ksyms_mod_foreach(ko->ko_name, be8_ksym_count, &relsym_cnt);
    367 
    368 	/*
    369 	 * Provide storage for the address list and add the symbols
    370 	 */
    371 	list.cnt = 0;
    372 	msize = relsym_cnt*sizeof(*list.markers);
    373 	list.markers = kmem_alloc(msize, KM_SLEEP);
    374 	ksyms_mod_foreach(ko->ko_name, be8_ksym_add, &list);
    375 	KASSERT(list.cnt == relsym_cnt);
    376 
    377 	/*
    378 	 * Sort symbols by ascending address
    379 	 */
    380 	if (kheapsort(list.markers, relsym_cnt, sizeof(*list.markers),
    381 	    be8_ksym_comp, &tmp) != 0)
    382 		panic("could not sort be8 marker symbols");
    383 
    384 	/*
    385 	 * Apply swaps to the .text section (XXX we do not have the
    386 	 * section header available any more, it has been jetisoned
    387 	 * already, so we can not check for all PROGBIT sections).
    388 	 */
    389 	for (i = 0; i < ko->ko_nprogtab; i++) {
    390 		if (strcmp(ko->ko_progtab[i].name, ".text") != 0)
    391 			continue;
    392 		be8_ksym_swap(ko->ko_progtab[i].addr,
    393 		    (size_t)ko->ko_progtab[i].size,
    394 		    &list);
    395 	}
    396 
    397 	/*
    398 	 * Done, free list
    399 	 */
    400 	kmem_free(list.markers, msize);
    401 }
    402 #endif
    403 
    404 int
    405 kobj_machdep(kobj_t ko, void *base, size_t size, bool load)
    406 {
    407 
    408 	if (load) {
    409 #ifdef _ARM_ARCH_BE8
    410 		if (base == (void*)ko->ko_text_address)
    411 			kobj_be8_fixup(ko);
    412 #endif
    413 #ifndef _RUMPKERNEL
    414 		cpu_idcache_wbinv_range((vaddr_t)base, size);
    415 		cpu_tlb_flushID();
    416 #endif
    417 	}
    418 
    419 	return 0;
    420 }
    421