1a17ae4c3SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+ 21da177e4SLinus Torvalds /* 3a53c8fabSHeiko Carstens * Kernel module help for s390. 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * S390 version 6a53c8fabSHeiko Carstens * Copyright IBM Corp. 2002, 2003 71da177e4SLinus Torvalds * Author(s): Arnd Bergmann (arndb@de.ibm.com) 81da177e4SLinus Torvalds * Martin Schwidefsky (schwidefsky@de.ibm.com) 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * based on i386 version 111da177e4SLinus Torvalds * Copyright (C) 2001 Rusty Russell. 121da177e4SLinus Torvalds */ 131da177e4SLinus Torvalds #include <linux/module.h> 141da177e4SLinus Torvalds #include <linux/elf.h> 151da177e4SLinus Torvalds #include <linux/vmalloc.h> 161da177e4SLinus Torvalds #include <linux/fs.h> 17de5012b4SIlya Leoshkevich #include <linux/ftrace.h> 181da177e4SLinus Torvalds #include <linux/string.h> 191da177e4SLinus Torvalds #include <linux/kernel.h> 20793213a8SVasily Gorbik #include <linux/kasan.h> 212b67fc46SHeiko Carstens #include <linux/moduleloader.h> 22c0007f1aSHeiko Carstens #include <linux/bug.h> 23be242261SPeter Zijlstra #include <linux/memory.h> 24686140a1SVasily Gorbik #include <asm/alternative.h> 25f19fbd5eSMartin Schwidefsky #include <asm/nospec-branch.h> 26f19fbd5eSMartin Schwidefsky #include <asm/facility.h> 27de5012b4SIlya Leoshkevich #include <asm/ftrace.lds.h> 28de5012b4SIlya Leoshkevich #include <asm/set_memory.h> 29*34644cc2SHeiko Carstens #include <asm/setup.h> 301da177e4SLinus Torvalds 311da177e4SLinus Torvalds #if 0 321da177e4SLinus Torvalds #define DEBUGP printk 331da177e4SLinus Torvalds #else 341da177e4SLinus Torvalds #define DEBUGP(fmt , ...) 351da177e4SLinus Torvalds #endif 361da177e4SLinus Torvalds 37f3b7e73bSIlya Leoshkevich #define PLT_ENTRY_SIZE 22 381da177e4SLinus Torvalds 39*34644cc2SHeiko Carstens static unsigned long get_module_load_offset(void) 40*34644cc2SHeiko Carstens { 41*34644cc2SHeiko Carstens static DEFINE_MUTEX(module_kaslr_mutex); 42*34644cc2SHeiko Carstens static unsigned long module_load_offset; 43*34644cc2SHeiko Carstens 44*34644cc2SHeiko Carstens if (!kaslr_enabled()) 45*34644cc2SHeiko Carstens return 0; 46*34644cc2SHeiko Carstens /* 47*34644cc2SHeiko Carstens * Calculate the module_load_offset the first time this code 48*34644cc2SHeiko Carstens * is called. Once calculated it stays the same until reboot. 49*34644cc2SHeiko Carstens */ 50*34644cc2SHeiko Carstens mutex_lock(&module_kaslr_mutex); 51*34644cc2SHeiko Carstens if (!module_load_offset) 52*34644cc2SHeiko Carstens module_load_offset = get_random_u32_inclusive(1, 1024) * PAGE_SIZE; 53*34644cc2SHeiko Carstens mutex_unlock(&module_kaslr_mutex); 54*34644cc2SHeiko Carstens return module_load_offset; 55*34644cc2SHeiko Carstens } 56*34644cc2SHeiko Carstens 57c972cc60SHeiko Carstens void *module_alloc(unsigned long size) 58c972cc60SHeiko Carstens { 5960115fa5SKefeng Wang gfp_t gfp_mask = GFP_KERNEL; 60793213a8SVasily Gorbik void *p; 61793213a8SVasily Gorbik 62c972cc60SHeiko Carstens if (PAGE_ALIGN(size) > MODULES_LEN) 63c972cc60SHeiko Carstens return NULL; 64*34644cc2SHeiko Carstens p = __vmalloc_node_range(size, MODULE_ALIGN, 65*34644cc2SHeiko Carstens MODULES_VADDR + get_module_load_offset(), MODULES_END, 6660115fa5SKefeng Wang gfp_mask, PAGE_KERNEL_EXEC, VM_DEFER_KMEMLEAK, NUMA_NO_NODE, 67c972cc60SHeiko Carstens __builtin_return_address(0)); 6863840de2SAndrey Konovalov if (p && (kasan_alloc_module_shadow(p, size, gfp_mask) < 0)) { 69793213a8SVasily Gorbik vfree(p); 70793213a8SVasily Gorbik return NULL; 71793213a8SVasily Gorbik } 72793213a8SVasily Gorbik return p; 73c972cc60SHeiko Carstens } 74c972cc60SHeiko Carstens 75de5012b4SIlya Leoshkevich #ifdef CONFIG_FUNCTION_TRACER 76de5012b4SIlya Leoshkevich void module_arch_cleanup(struct module *mod) 77de5012b4SIlya Leoshkevich { 78de5012b4SIlya Leoshkevich module_memfree(mod->arch.trampolines_start); 79de5012b4SIlya Leoshkevich } 80de5012b4SIlya Leoshkevich #endif 81de5012b4SIlya Leoshkevich 82d453cdedSRusty Russell void module_arch_freeing_init(struct module *mod) 831da177e4SLinus Torvalds { 84f31e0960SJessica Yu if (is_livepatch_module(mod) && 85f31e0960SJessica Yu mod->state == MODULE_STATE_LIVE) 86f31e0960SJessica Yu return; 87f31e0960SJessica Yu 886a03f5f0SChristian Borntraeger vfree(mod->arch.syminfo); 896a03f5f0SChristian Borntraeger mod->arch.syminfo = NULL; 903164a3cbSHendrik Brueckner } 911da177e4SLinus Torvalds 92083e14c0SMartin Schwidefsky static void check_rela(Elf_Rela *rela, struct module *me) 931da177e4SLinus Torvalds { 941da177e4SLinus Torvalds struct mod_arch_syminfo *info; 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds info = me->arch.syminfo + ELF_R_SYM (rela->r_info); 971da177e4SLinus Torvalds switch (ELF_R_TYPE (rela->r_info)) { 981da177e4SLinus Torvalds case R_390_GOT12: /* 12 bit GOT offset. */ 991da177e4SLinus Torvalds case R_390_GOT16: /* 16 bit GOT offset. */ 1001da177e4SLinus Torvalds case R_390_GOT20: /* 20 bit GOT offset. */ 1011da177e4SLinus Torvalds case R_390_GOT32: /* 32 bit GOT offset. */ 1021da177e4SLinus Torvalds case R_390_GOT64: /* 64 bit GOT offset. */ 1031da177e4SLinus Torvalds case R_390_GOTENT: /* 32 bit PC rel. to GOT entry shifted by 1. */ 1041da177e4SLinus Torvalds case R_390_GOTPLT12: /* 12 bit offset to jump slot. */ 1051da177e4SLinus Torvalds case R_390_GOTPLT16: /* 16 bit offset to jump slot. */ 1061da177e4SLinus Torvalds case R_390_GOTPLT20: /* 20 bit offset to jump slot. */ 1071da177e4SLinus Torvalds case R_390_GOTPLT32: /* 32 bit offset to jump slot. */ 1081da177e4SLinus Torvalds case R_390_GOTPLT64: /* 64 bit offset to jump slot. */ 1091da177e4SLinus Torvalds case R_390_GOTPLTENT: /* 32 bit rel. offset to jump slot >> 1. */ 1101da177e4SLinus Torvalds if (info->got_offset == -1UL) { 1111da177e4SLinus Torvalds info->got_offset = me->arch.got_size; 1121da177e4SLinus Torvalds me->arch.got_size += sizeof(void*); 1131da177e4SLinus Torvalds } 1141da177e4SLinus Torvalds break; 1151da177e4SLinus Torvalds case R_390_PLT16DBL: /* 16 bit PC rel. PLT shifted by 1. */ 1161da177e4SLinus Torvalds case R_390_PLT32DBL: /* 32 bit PC rel. PLT shifted by 1. */ 1171da177e4SLinus Torvalds case R_390_PLT32: /* 32 bit PC relative PLT address. */ 1181da177e4SLinus Torvalds case R_390_PLT64: /* 64 bit PC relative PLT address. */ 1191da177e4SLinus Torvalds case R_390_PLTOFF16: /* 16 bit offset from GOT to PLT. */ 1201da177e4SLinus Torvalds case R_390_PLTOFF32: /* 32 bit offset from GOT to PLT. */ 1211da177e4SLinus Torvalds case R_390_PLTOFF64: /* 16 bit offset from GOT to PLT. */ 1221da177e4SLinus Torvalds if (info->plt_offset == -1UL) { 1231da177e4SLinus Torvalds info->plt_offset = me->arch.plt_size; 1241da177e4SLinus Torvalds me->arch.plt_size += PLT_ENTRY_SIZE; 1251da177e4SLinus Torvalds } 1261da177e4SLinus Torvalds break; 1271da177e4SLinus Torvalds case R_390_COPY: 1281da177e4SLinus Torvalds case R_390_GLOB_DAT: 1291da177e4SLinus Torvalds case R_390_JMP_SLOT: 1301da177e4SLinus Torvalds case R_390_RELATIVE: 1311da177e4SLinus Torvalds /* Only needed if we want to support loading of 1321da177e4SLinus Torvalds modules linked with -shared. */ 1331da177e4SLinus Torvalds break; 1341da177e4SLinus Torvalds } 1351da177e4SLinus Torvalds } 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds /* 1381da177e4SLinus Torvalds * Account for GOT and PLT relocations. We can't add sections for 1391da177e4SLinus Torvalds * got and plt but we can increase the core module size. 1401da177e4SLinus Torvalds */ 141083e14c0SMartin Schwidefsky int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs, 1421da177e4SLinus Torvalds char *secstrings, struct module *me) 1431da177e4SLinus Torvalds { 1441da177e4SLinus Torvalds Elf_Shdr *symtab; 1451da177e4SLinus Torvalds Elf_Sym *symbols; 1461da177e4SLinus Torvalds Elf_Rela *rela; 1471da177e4SLinus Torvalds char *strings; 1481da177e4SLinus Torvalds int nrela, i, j; 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds /* Find symbol table and string table. */ 151d2c993d8SHeiko Carstens symtab = NULL; 1521da177e4SLinus Torvalds for (i = 0; i < hdr->e_shnum; i++) 1531da177e4SLinus Torvalds switch (sechdrs[i].sh_type) { 1541da177e4SLinus Torvalds case SHT_SYMTAB: 1551da177e4SLinus Torvalds symtab = sechdrs + i; 1561da177e4SLinus Torvalds break; 1571da177e4SLinus Torvalds } 1581da177e4SLinus Torvalds if (!symtab) { 1591da177e4SLinus Torvalds printk(KERN_ERR "module %s: no symbol table\n", me->name); 1601da177e4SLinus Torvalds return -ENOEXEC; 1611da177e4SLinus Torvalds } 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds /* Allocate one syminfo structure per symbol. */ 1641da177e4SLinus Torvalds me->arch.nsyms = symtab->sh_size / sizeof(Elf_Sym); 16542bc47b3SKees Cook me->arch.syminfo = vmalloc(array_size(sizeof(struct mod_arch_syminfo), 16642bc47b3SKees Cook me->arch.nsyms)); 1671da177e4SLinus Torvalds if (!me->arch.syminfo) 1681da177e4SLinus Torvalds return -ENOMEM; 1691da177e4SLinus Torvalds symbols = (void *) hdr + symtab->sh_offset; 1701da177e4SLinus Torvalds strings = (void *) hdr + sechdrs[symtab->sh_link].sh_offset; 1711da177e4SLinus Torvalds for (i = 0; i < me->arch.nsyms; i++) { 1721da177e4SLinus Torvalds if (symbols[i].st_shndx == SHN_UNDEF && 1731da177e4SLinus Torvalds strcmp(strings + symbols[i].st_name, 1741da177e4SLinus Torvalds "_GLOBAL_OFFSET_TABLE_") == 0) 1751da177e4SLinus Torvalds /* "Define" it as absolute. */ 1761da177e4SLinus Torvalds symbols[i].st_shndx = SHN_ABS; 1771da177e4SLinus Torvalds me->arch.syminfo[i].got_offset = -1UL; 1781da177e4SLinus Torvalds me->arch.syminfo[i].plt_offset = -1UL; 1791da177e4SLinus Torvalds me->arch.syminfo[i].got_initialized = 0; 1801da177e4SLinus Torvalds me->arch.syminfo[i].plt_initialized = 0; 1811da177e4SLinus Torvalds } 1821da177e4SLinus Torvalds 1831da177e4SLinus Torvalds /* Search for got/plt relocations. */ 1841da177e4SLinus Torvalds me->arch.got_size = me->arch.plt_size = 0; 1851da177e4SLinus Torvalds for (i = 0; i < hdr->e_shnum; i++) { 1861da177e4SLinus Torvalds if (sechdrs[i].sh_type != SHT_RELA) 1871da177e4SLinus Torvalds continue; 1881da177e4SLinus Torvalds nrela = sechdrs[i].sh_size / sizeof(Elf_Rela); 1891da177e4SLinus Torvalds rela = (void *) hdr + sechdrs[i].sh_offset; 1901da177e4SLinus Torvalds for (j = 0; j < nrela; j++) 1911da177e4SLinus Torvalds check_rela(rela + j, me); 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds /* Increase core size by size of got & plt and set start 1951da177e4SLinus Torvalds offsets for got and plt. */ 1967523e4dcSRusty Russell me->core_layout.size = ALIGN(me->core_layout.size, 4); 1977523e4dcSRusty Russell me->arch.got_offset = me->core_layout.size; 1987523e4dcSRusty Russell me->core_layout.size += me->arch.got_size; 1997523e4dcSRusty Russell me->arch.plt_offset = me->core_layout.size; 200f19fbd5eSMartin Schwidefsky if (me->arch.plt_size) { 2016e179d64SMartin Schwidefsky if (IS_ENABLED(CONFIG_EXPOLINE) && !nospec_disable) 202f19fbd5eSMartin Schwidefsky me->arch.plt_size += PLT_ENTRY_SIZE; 2037523e4dcSRusty Russell me->core_layout.size += me->arch.plt_size; 204f19fbd5eSMartin Schwidefsky } 2051da177e4SLinus Torvalds return 0; 2061da177e4SLinus Torvalds } 2071da177e4SLinus Torvalds 208083e14c0SMartin Schwidefsky static int apply_rela_bits(Elf_Addr loc, Elf_Addr val, 209be242261SPeter Zijlstra int sign, int bits, int shift, 210be242261SPeter Zijlstra void *(*write)(void *dest, const void *src, size_t len)) 211083e14c0SMartin Schwidefsky { 212083e14c0SMartin Schwidefsky unsigned long umax; 213083e14c0SMartin Schwidefsky long min, max; 214be242261SPeter Zijlstra void *dest = (void *)loc; 215083e14c0SMartin Schwidefsky 216083e14c0SMartin Schwidefsky if (val & ((1UL << shift) - 1)) 217083e14c0SMartin Schwidefsky return -ENOEXEC; 218083e14c0SMartin Schwidefsky if (sign) { 219083e14c0SMartin Schwidefsky val = (Elf_Addr)(((long) val) >> shift); 220083e14c0SMartin Schwidefsky min = -(1L << (bits - 1)); 221083e14c0SMartin Schwidefsky max = (1L << (bits - 1)) - 1; 222083e14c0SMartin Schwidefsky if ((long) val < min || (long) val > max) 223083e14c0SMartin Schwidefsky return -ENOEXEC; 224083e14c0SMartin Schwidefsky } else { 225083e14c0SMartin Schwidefsky val >>= shift; 226083e14c0SMartin Schwidefsky umax = ((1UL << (bits - 1)) << 1) - 1; 227083e14c0SMartin Schwidefsky if ((unsigned long) val > umax) 228083e14c0SMartin Schwidefsky return -ENOEXEC; 229083e14c0SMartin Schwidefsky } 230083e14c0SMartin Schwidefsky 231be242261SPeter Zijlstra if (bits == 8) { 232be242261SPeter Zijlstra unsigned char tmp = val; 233be242261SPeter Zijlstra write(dest, &tmp, 1); 234be242261SPeter Zijlstra } else if (bits == 12) { 235be242261SPeter Zijlstra unsigned short tmp = (val & 0xfff) | 236083e14c0SMartin Schwidefsky (*(unsigned short *) loc & 0xf000); 237be242261SPeter Zijlstra write(dest, &tmp, 2); 238be242261SPeter Zijlstra } else if (bits == 16) { 239be242261SPeter Zijlstra unsigned short tmp = val; 240be242261SPeter Zijlstra write(dest, &tmp, 2); 241be242261SPeter Zijlstra } else if (bits == 20) { 242be242261SPeter Zijlstra unsigned int tmp = (val & 0xfff) << 16 | 243be242261SPeter Zijlstra (val & 0xff000) >> 4 | (*(unsigned int *) loc & 0xf00000ff); 244be242261SPeter Zijlstra write(dest, &tmp, 4); 245be242261SPeter Zijlstra } else if (bits == 32) { 246be242261SPeter Zijlstra unsigned int tmp = val; 247be242261SPeter Zijlstra write(dest, &tmp, 4); 248be242261SPeter Zijlstra } else if (bits == 64) { 249be242261SPeter Zijlstra unsigned long tmp = val; 250be242261SPeter Zijlstra write(dest, &tmp, 8); 251be242261SPeter Zijlstra } 252083e14c0SMartin Schwidefsky return 0; 253083e14c0SMartin Schwidefsky } 254083e14c0SMartin Schwidefsky 255083e14c0SMartin Schwidefsky static int apply_rela(Elf_Rela *rela, Elf_Addr base, Elf_Sym *symtab, 256be242261SPeter Zijlstra const char *strtab, struct module *me, 257be242261SPeter Zijlstra void *(*write)(void *dest, const void *src, size_t len)) 2581da177e4SLinus Torvalds { 2591da177e4SLinus Torvalds struct mod_arch_syminfo *info; 2601da177e4SLinus Torvalds Elf_Addr loc, val; 2611da177e4SLinus Torvalds int r_type, r_sym; 26272a6b43eSHeiko Carstens int rc = -ENOEXEC; 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds /* This is where to make the change */ 2651da177e4SLinus Torvalds loc = base + rela->r_offset; 2661da177e4SLinus Torvalds /* This is the symbol it is referring to. Note that all 2671da177e4SLinus Torvalds undefined symbols have been resolved. */ 2681da177e4SLinus Torvalds r_sym = ELF_R_SYM(rela->r_info); 2691da177e4SLinus Torvalds r_type = ELF_R_TYPE(rela->r_info); 2701da177e4SLinus Torvalds info = me->arch.syminfo + r_sym; 2711da177e4SLinus Torvalds val = symtab[r_sym].st_value; 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds switch (r_type) { 274e80cfc31SHendrik Brueckner case R_390_NONE: /* No relocation. */ 275e80cfc31SHendrik Brueckner rc = 0; 276e80cfc31SHendrik Brueckner break; 2771da177e4SLinus Torvalds case R_390_8: /* Direct 8 bit. */ 2781da177e4SLinus Torvalds case R_390_12: /* Direct 12 bit. */ 2791da177e4SLinus Torvalds case R_390_16: /* Direct 16 bit. */ 2801da177e4SLinus Torvalds case R_390_20: /* Direct 20 bit. */ 2811da177e4SLinus Torvalds case R_390_32: /* Direct 32 bit. */ 2821da177e4SLinus Torvalds case R_390_64: /* Direct 64 bit. */ 2831da177e4SLinus Torvalds val += rela->r_addend; 2841da177e4SLinus Torvalds if (r_type == R_390_8) 285be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 0, 8, 0, write); 2861da177e4SLinus Torvalds else if (r_type == R_390_12) 287be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 0, 12, 0, write); 2881da177e4SLinus Torvalds else if (r_type == R_390_16) 289be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 0, 16, 0, write); 2901da177e4SLinus Torvalds else if (r_type == R_390_20) 291be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 1, 20, 0, write); 2921da177e4SLinus Torvalds else if (r_type == R_390_32) 293be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 0, 32, 0, write); 2941da177e4SLinus Torvalds else if (r_type == R_390_64) 295be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 0, 64, 0, write); 2961da177e4SLinus Torvalds break; 2971da177e4SLinus Torvalds case R_390_PC16: /* PC relative 16 bit. */ 2981da177e4SLinus Torvalds case R_390_PC16DBL: /* PC relative 16 bit shifted by 1. */ 2991da177e4SLinus Torvalds case R_390_PC32DBL: /* PC relative 32 bit shifted by 1. */ 3001da177e4SLinus Torvalds case R_390_PC32: /* PC relative 32 bit. */ 3011da177e4SLinus Torvalds case R_390_PC64: /* PC relative 64 bit. */ 3021da177e4SLinus Torvalds val += rela->r_addend - loc; 3031da177e4SLinus Torvalds if (r_type == R_390_PC16) 304be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 1, 16, 0, write); 3051da177e4SLinus Torvalds else if (r_type == R_390_PC16DBL) 306be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 1, 16, 1, write); 3071da177e4SLinus Torvalds else if (r_type == R_390_PC32DBL) 308be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 1, 32, 1, write); 3091da177e4SLinus Torvalds else if (r_type == R_390_PC32) 310be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 1, 32, 0, write); 3111da177e4SLinus Torvalds else if (r_type == R_390_PC64) 312be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 1, 64, 0, write); 3131da177e4SLinus Torvalds break; 3141da177e4SLinus Torvalds case R_390_GOT12: /* 12 bit GOT offset. */ 3151da177e4SLinus Torvalds case R_390_GOT16: /* 16 bit GOT offset. */ 3161da177e4SLinus Torvalds case R_390_GOT20: /* 20 bit GOT offset. */ 3171da177e4SLinus Torvalds case R_390_GOT32: /* 32 bit GOT offset. */ 3181da177e4SLinus Torvalds case R_390_GOT64: /* 64 bit GOT offset. */ 3191da177e4SLinus Torvalds case R_390_GOTENT: /* 32 bit PC rel. to GOT entry shifted by 1. */ 3201da177e4SLinus Torvalds case R_390_GOTPLT12: /* 12 bit offset to jump slot. */ 3211da177e4SLinus Torvalds case R_390_GOTPLT20: /* 20 bit offset to jump slot. */ 3221da177e4SLinus Torvalds case R_390_GOTPLT16: /* 16 bit offset to jump slot. */ 3231da177e4SLinus Torvalds case R_390_GOTPLT32: /* 32 bit offset to jump slot. */ 3241da177e4SLinus Torvalds case R_390_GOTPLT64: /* 64 bit offset to jump slot. */ 3251da177e4SLinus Torvalds case R_390_GOTPLTENT: /* 32 bit rel. offset to jump slot >> 1. */ 3261da177e4SLinus Torvalds if (info->got_initialized == 0) { 327be242261SPeter Zijlstra Elf_Addr *gotent = me->core_layout.base + 328be242261SPeter Zijlstra me->arch.got_offset + 3291da177e4SLinus Torvalds info->got_offset; 330be242261SPeter Zijlstra 331be242261SPeter Zijlstra write(gotent, &val, sizeof(*gotent)); 3321da177e4SLinus Torvalds info->got_initialized = 1; 3331da177e4SLinus Torvalds } 3341da177e4SLinus Torvalds val = info->got_offset + rela->r_addend; 3351da177e4SLinus Torvalds if (r_type == R_390_GOT12 || 3361da177e4SLinus Torvalds r_type == R_390_GOTPLT12) 337be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 0, 12, 0, write); 3381da177e4SLinus Torvalds else if (r_type == R_390_GOT16 || 3391da177e4SLinus Torvalds r_type == R_390_GOTPLT16) 340be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 0, 16, 0, write); 3411da177e4SLinus Torvalds else if (r_type == R_390_GOT20 || 3421da177e4SLinus Torvalds r_type == R_390_GOTPLT20) 343be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 1, 20, 0, write); 3441da177e4SLinus Torvalds else if (r_type == R_390_GOT32 || 3451da177e4SLinus Torvalds r_type == R_390_GOTPLT32) 346be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 0, 32, 0, write); 3471da177e4SLinus Torvalds else if (r_type == R_390_GOT64 || 3481da177e4SLinus Torvalds r_type == R_390_GOTPLT64) 349be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 0, 64, 0, write); 350083e14c0SMartin Schwidefsky else if (r_type == R_390_GOTENT || 351083e14c0SMartin Schwidefsky r_type == R_390_GOTPLTENT) { 3527523e4dcSRusty Russell val += (Elf_Addr) me->core_layout.base - loc; 353be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 1, 32, 1, write); 354083e14c0SMartin Schwidefsky } 3551da177e4SLinus Torvalds break; 3561da177e4SLinus Torvalds case R_390_PLT16DBL: /* 16 bit PC rel. PLT shifted by 1. */ 3571da177e4SLinus Torvalds case R_390_PLT32DBL: /* 32 bit PC rel. PLT shifted by 1. */ 3581da177e4SLinus Torvalds case R_390_PLT32: /* 32 bit PC relative PLT address. */ 3591da177e4SLinus Torvalds case R_390_PLT64: /* 64 bit PC relative PLT address. */ 3601da177e4SLinus Torvalds case R_390_PLTOFF16: /* 16 bit offset from GOT to PLT. */ 3611da177e4SLinus Torvalds case R_390_PLTOFF32: /* 32 bit offset from GOT to PLT. */ 3621da177e4SLinus Torvalds case R_390_PLTOFF64: /* 16 bit offset from GOT to PLT. */ 3631da177e4SLinus Torvalds if (info->plt_initialized == 0) { 364f3b7e73bSIlya Leoshkevich unsigned char insn[PLT_ENTRY_SIZE]; 365f3b7e73bSIlya Leoshkevich char *plt_base; 366f3b7e73bSIlya Leoshkevich char *ip; 367be242261SPeter Zijlstra 368f3b7e73bSIlya Leoshkevich plt_base = me->core_layout.base + me->arch.plt_offset; 369f3b7e73bSIlya Leoshkevich ip = plt_base + info->plt_offset; 370f3b7e73bSIlya Leoshkevich *(int *)insn = 0x0d10e310; /* basr 1,0 */ 371f3b7e73bSIlya Leoshkevich *(int *)&insn[4] = 0x100c0004; /* lg 1,12(1) */ 3726e179d64SMartin Schwidefsky if (IS_ENABLED(CONFIG_EXPOLINE) && !nospec_disable) { 373f3b7e73bSIlya Leoshkevich char *jump_r1; 374f3b7e73bSIlya Leoshkevich 375f3b7e73bSIlya Leoshkevich jump_r1 = plt_base + me->arch.plt_size - 376f3b7e73bSIlya Leoshkevich PLT_ENTRY_SIZE; 377f3b7e73bSIlya Leoshkevich /* brcl 0xf,__jump_r1 */ 378f3b7e73bSIlya Leoshkevich *(short *)&insn[8] = 0xc0f4; 379f3b7e73bSIlya Leoshkevich *(int *)&insn[10] = (jump_r1 - (ip + 8)) / 2; 380f19fbd5eSMartin Schwidefsky } else { 381f3b7e73bSIlya Leoshkevich *(int *)&insn[8] = 0x07f10000; /* br %r1 */ 382f19fbd5eSMartin Schwidefsky } 383f3b7e73bSIlya Leoshkevich *(long *)&insn[14] = val; 384be242261SPeter Zijlstra 385be242261SPeter Zijlstra write(ip, insn, sizeof(insn)); 3861da177e4SLinus Torvalds info->plt_initialized = 1; 3871da177e4SLinus Torvalds } 3881da177e4SLinus Torvalds if (r_type == R_390_PLTOFF16 || 389504665a9SMartin Schwidefsky r_type == R_390_PLTOFF32 || 390504665a9SMartin Schwidefsky r_type == R_390_PLTOFF64) 3911da177e4SLinus Torvalds val = me->arch.plt_offset - me->arch.got_offset + 3921da177e4SLinus Torvalds info->plt_offset + rela->r_addend; 393504665a9SMartin Schwidefsky else { 394504665a9SMartin Schwidefsky if (!((r_type == R_390_PLT16DBL && 395504665a9SMartin Schwidefsky val - loc + 0xffffUL < 0x1ffffeUL) || 396504665a9SMartin Schwidefsky (r_type == R_390_PLT32DBL && 397504665a9SMartin Schwidefsky val - loc + 0xffffffffULL < 0x1fffffffeULL))) 3987523e4dcSRusty Russell val = (Elf_Addr) me->core_layout.base + 399504665a9SMartin Schwidefsky me->arch.plt_offset + 400504665a9SMartin Schwidefsky info->plt_offset; 401504665a9SMartin Schwidefsky val += rela->r_addend - loc; 402504665a9SMartin Schwidefsky } 4031da177e4SLinus Torvalds if (r_type == R_390_PLT16DBL) 404be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 1, 16, 1, write); 4051da177e4SLinus Torvalds else if (r_type == R_390_PLTOFF16) 406be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 0, 16, 0, write); 4071da177e4SLinus Torvalds else if (r_type == R_390_PLT32DBL) 408be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 1, 32, 1, write); 4091da177e4SLinus Torvalds else if (r_type == R_390_PLT32 || 4101da177e4SLinus Torvalds r_type == R_390_PLTOFF32) 411be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 0, 32, 0, write); 4121da177e4SLinus Torvalds else if (r_type == R_390_PLT64 || 4131da177e4SLinus Torvalds r_type == R_390_PLTOFF64) 414be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 0, 64, 0, write); 4151da177e4SLinus Torvalds break; 4161da177e4SLinus Torvalds case R_390_GOTOFF16: /* 16 bit offset to GOT. */ 4171da177e4SLinus Torvalds case R_390_GOTOFF32: /* 32 bit offset to GOT. */ 4181da177e4SLinus Torvalds case R_390_GOTOFF64: /* 64 bit offset to GOT. */ 4191da177e4SLinus Torvalds val = val + rela->r_addend - 4207523e4dcSRusty Russell ((Elf_Addr) me->core_layout.base + me->arch.got_offset); 4211da177e4SLinus Torvalds if (r_type == R_390_GOTOFF16) 422be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 0, 16, 0, write); 4231da177e4SLinus Torvalds else if (r_type == R_390_GOTOFF32) 424be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 0, 32, 0, write); 4251da177e4SLinus Torvalds else if (r_type == R_390_GOTOFF64) 426be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 0, 64, 0, write); 4271da177e4SLinus Torvalds break; 4281da177e4SLinus Torvalds case R_390_GOTPC: /* 32 bit PC relative offset to GOT. */ 4291da177e4SLinus Torvalds case R_390_GOTPCDBL: /* 32 bit PC rel. off. to GOT shifted by 1. */ 4307523e4dcSRusty Russell val = (Elf_Addr) me->core_layout.base + me->arch.got_offset + 4311da177e4SLinus Torvalds rela->r_addend - loc; 4321da177e4SLinus Torvalds if (r_type == R_390_GOTPC) 433be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 1, 32, 0, write); 4341da177e4SLinus Torvalds else if (r_type == R_390_GOTPCDBL) 435be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 1, 32, 1, write); 4361da177e4SLinus Torvalds break; 4371da177e4SLinus Torvalds case R_390_COPY: 4381da177e4SLinus Torvalds case R_390_GLOB_DAT: /* Create GOT entry. */ 4391da177e4SLinus Torvalds case R_390_JMP_SLOT: /* Create PLT entry. */ 4401da177e4SLinus Torvalds case R_390_RELATIVE: /* Adjust by program base. */ 4411da177e4SLinus Torvalds /* Only needed if we want to support loading of 4421da177e4SLinus Torvalds modules linked with -shared. */ 443083e14c0SMartin Schwidefsky return -ENOEXEC; 4441da177e4SLinus Torvalds default: 445083e14c0SMartin Schwidefsky printk(KERN_ERR "module %s: unknown relocation: %u\n", 4461da177e4SLinus Torvalds me->name, r_type); 4471da177e4SLinus Torvalds return -ENOEXEC; 4481da177e4SLinus Torvalds } 449083e14c0SMartin Schwidefsky if (rc) { 450083e14c0SMartin Schwidefsky printk(KERN_ERR "module %s: relocation error for symbol %s " 451083e14c0SMartin Schwidefsky "(r_type %i, value 0x%lx)\n", 452083e14c0SMartin Schwidefsky me->name, strtab + symtab[r_sym].st_name, 453083e14c0SMartin Schwidefsky r_type, (unsigned long) val); 454083e14c0SMartin Schwidefsky return rc; 455083e14c0SMartin Schwidefsky } 4561da177e4SLinus Torvalds return 0; 4571da177e4SLinus Torvalds } 4581da177e4SLinus Torvalds 459be242261SPeter Zijlstra static int __apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab, 4601da177e4SLinus Torvalds unsigned int symindex, unsigned int relsec, 461be242261SPeter Zijlstra struct module *me, 462be242261SPeter Zijlstra void *(*write)(void *dest, const void *src, size_t len)) 4631da177e4SLinus Torvalds { 4641da177e4SLinus Torvalds Elf_Addr base; 4651da177e4SLinus Torvalds Elf_Sym *symtab; 4661da177e4SLinus Torvalds Elf_Rela *rela; 4671da177e4SLinus Torvalds unsigned long i, n; 4681da177e4SLinus Torvalds int rc; 4691da177e4SLinus Torvalds 4701da177e4SLinus Torvalds DEBUGP("Applying relocate section %u to %u\n", 4711da177e4SLinus Torvalds relsec, sechdrs[relsec].sh_info); 4721da177e4SLinus Torvalds base = sechdrs[sechdrs[relsec].sh_info].sh_addr; 4731da177e4SLinus Torvalds symtab = (Elf_Sym *) sechdrs[symindex].sh_addr; 4741da177e4SLinus Torvalds rela = (Elf_Rela *) sechdrs[relsec].sh_addr; 4751da177e4SLinus Torvalds n = sechdrs[relsec].sh_size / sizeof(Elf_Rela); 4761da177e4SLinus Torvalds 4771da177e4SLinus Torvalds for (i = 0; i < n; i++, rela++) { 478be242261SPeter Zijlstra rc = apply_rela(rela, base, symtab, strtab, me, write); 4791da177e4SLinus Torvalds if (rc) 4801da177e4SLinus Torvalds return rc; 4811da177e4SLinus Torvalds } 4821da177e4SLinus Torvalds return 0; 4831da177e4SLinus Torvalds } 4841da177e4SLinus Torvalds 485be242261SPeter Zijlstra int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab, 486be242261SPeter Zijlstra unsigned int symindex, unsigned int relsec, 487be242261SPeter Zijlstra struct module *me) 488be242261SPeter Zijlstra { 489be242261SPeter Zijlstra bool early = me->state == MODULE_STATE_UNFORMED; 490be242261SPeter Zijlstra void *(*write)(void *, const void *, size_t) = memcpy; 491be242261SPeter Zijlstra 492be242261SPeter Zijlstra if (!early) 493be242261SPeter Zijlstra write = s390_kernel_write; 494be242261SPeter Zijlstra 495be242261SPeter Zijlstra return __apply_relocate_add(sechdrs, strtab, symindex, relsec, me, 496be242261SPeter Zijlstra write); 497be242261SPeter Zijlstra } 498be242261SPeter Zijlstra 499de5012b4SIlya Leoshkevich #ifdef CONFIG_FUNCTION_TRACER 500de5012b4SIlya Leoshkevich static int module_alloc_ftrace_hotpatch_trampolines(struct module *me, 501de5012b4SIlya Leoshkevich const Elf_Shdr *s) 502de5012b4SIlya Leoshkevich { 503de5012b4SIlya Leoshkevich char *start, *end; 504de5012b4SIlya Leoshkevich int numpages; 505de5012b4SIlya Leoshkevich size_t size; 506de5012b4SIlya Leoshkevich 507de5012b4SIlya Leoshkevich size = FTRACE_HOTPATCH_TRAMPOLINES_SIZE(s->sh_size); 508de5012b4SIlya Leoshkevich numpages = DIV_ROUND_UP(size, PAGE_SIZE); 509de5012b4SIlya Leoshkevich start = module_alloc(numpages * PAGE_SIZE); 510de5012b4SIlya Leoshkevich if (!start) 511de5012b4SIlya Leoshkevich return -ENOMEM; 512de5012b4SIlya Leoshkevich set_memory_ro((unsigned long)start, numpages); 513de5012b4SIlya Leoshkevich end = start + size; 514de5012b4SIlya Leoshkevich 515de5012b4SIlya Leoshkevich me->arch.trampolines_start = (struct ftrace_hotpatch_trampoline *)start; 516de5012b4SIlya Leoshkevich me->arch.trampolines_end = (struct ftrace_hotpatch_trampoline *)end; 517de5012b4SIlya Leoshkevich me->arch.next_trampoline = me->arch.trampolines_start; 518de5012b4SIlya Leoshkevich 519de5012b4SIlya Leoshkevich return 0; 520de5012b4SIlya Leoshkevich } 521de5012b4SIlya Leoshkevich #endif /* CONFIG_FUNCTION_TRACER */ 522de5012b4SIlya Leoshkevich 5231da177e4SLinus Torvalds int module_finalize(const Elf_Ehdr *hdr, 5241da177e4SLinus Torvalds const Elf_Shdr *sechdrs, 5251da177e4SLinus Torvalds struct module *me) 5261da177e4SLinus Torvalds { 527686140a1SVasily Gorbik const Elf_Shdr *s; 528f19fbd5eSMartin Schwidefsky char *secstrings, *secname; 529f19fbd5eSMartin Schwidefsky void *aseg; 530de5012b4SIlya Leoshkevich #ifdef CONFIG_FUNCTION_TRACER 531de5012b4SIlya Leoshkevich int ret; 532de5012b4SIlya Leoshkevich #endif 533f19fbd5eSMartin Schwidefsky 534f19fbd5eSMartin Schwidefsky if (IS_ENABLED(CONFIG_EXPOLINE) && 5356e179d64SMartin Schwidefsky !nospec_disable && me->arch.plt_size) { 536f19fbd5eSMartin Schwidefsky unsigned int *ij; 537f19fbd5eSMartin Schwidefsky 538f19fbd5eSMartin Schwidefsky ij = me->core_layout.base + me->arch.plt_offset + 539f19fbd5eSMartin Schwidefsky me->arch.plt_size - PLT_ENTRY_SIZE; 540f19fbd5eSMartin Schwidefsky ij[0] = 0xc6000000; /* exrl %r0,.+10 */ 541f19fbd5eSMartin Schwidefsky ij[1] = 0x0005a7f4; /* j . */ 542f19fbd5eSMartin Schwidefsky ij[2] = 0x000007f1; /* br %r1 */ 543f19fbd5eSMartin Schwidefsky } 544686140a1SVasily Gorbik 545686140a1SVasily Gorbik secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; 546686140a1SVasily Gorbik for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) { 547f19fbd5eSMartin Schwidefsky aseg = (void *) s->sh_addr; 548f19fbd5eSMartin Schwidefsky secname = secstrings + s->sh_name; 549686140a1SVasily Gorbik 550f19fbd5eSMartin Schwidefsky if (!strcmp(".altinstructions", secname)) 551f19fbd5eSMartin Schwidefsky /* patch .altinstructions */ 552686140a1SVasily Gorbik apply_alternatives(aseg, aseg + s->sh_size); 553f19fbd5eSMartin Schwidefsky 554f19fbd5eSMartin Schwidefsky if (IS_ENABLED(CONFIG_EXPOLINE) && 555b29cd7c4SVasily Gorbik (str_has_prefix(secname, ".s390_indirect"))) 5566e179d64SMartin Schwidefsky nospec_revert(aseg, aseg + s->sh_size); 557f19fbd5eSMartin Schwidefsky 558f19fbd5eSMartin Schwidefsky if (IS_ENABLED(CONFIG_EXPOLINE) && 559b29cd7c4SVasily Gorbik (str_has_prefix(secname, ".s390_return"))) 5606e179d64SMartin Schwidefsky nospec_revert(aseg, aseg + s->sh_size); 561de5012b4SIlya Leoshkevich 562de5012b4SIlya Leoshkevich #ifdef CONFIG_FUNCTION_TRACER 563de5012b4SIlya Leoshkevich if (!strcmp(FTRACE_CALLSITE_SECTION, secname)) { 564de5012b4SIlya Leoshkevich ret = module_alloc_ftrace_hotpatch_trampolines(me, s); 565de5012b4SIlya Leoshkevich if (ret < 0) 566de5012b4SIlya Leoshkevich return ret; 567de5012b4SIlya Leoshkevich } 568de5012b4SIlya Leoshkevich #endif /* CONFIG_FUNCTION_TRACER */ 569686140a1SVasily Gorbik } 570686140a1SVasily Gorbik 5715336377dSLinus Torvalds return 0; 5721da177e4SLinus Torvalds } 573