x86/speculation/mds: Fix documentation typo
[linux/fpc-iii.git] / arch / s390 / kernel / module.c
blobb441e069e674f53fc23948179a7bdc0f170902da
1 /*
2 * Kernel module help for s390.
4 * S390 version
5 * Copyright IBM Corp. 2002, 2003
6 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
7 * Martin Schwidefsky (schwidefsky@de.ibm.com)
9 * based on i386 version
10 * Copyright (C) 2001 Rusty Russell.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <linux/module.h>
27 #include <linux/elf.h>
28 #include <linux/vmalloc.h>
29 #include <linux/fs.h>
30 #include <linux/string.h>
31 #include <linux/kernel.h>
32 #include <linux/moduleloader.h>
33 #include <linux/bug.h>
34 #include <asm/alternative.h>
35 #include <asm/nospec-branch.h>
36 #include <asm/facility.h>
38 #if 0
39 #define DEBUGP printk
40 #else
41 #define DEBUGP(fmt , ...)
42 #endif
44 #define PLT_ENTRY_SIZE 20
46 void *module_alloc(unsigned long size)
48 if (PAGE_ALIGN(size) > MODULES_LEN)
49 return NULL;
50 return __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END,
51 GFP_KERNEL, PAGE_KERNEL_EXEC,
52 0, NUMA_NO_NODE,
53 __builtin_return_address(0));
56 void module_arch_freeing_init(struct module *mod)
58 if (is_livepatch_module(mod) &&
59 mod->state == MODULE_STATE_LIVE)
60 return;
62 vfree(mod->arch.syminfo);
63 mod->arch.syminfo = NULL;
66 static void check_rela(Elf_Rela *rela, struct module *me)
68 struct mod_arch_syminfo *info;
70 info = me->arch.syminfo + ELF_R_SYM (rela->r_info);
71 switch (ELF_R_TYPE (rela->r_info)) {
72 case R_390_GOT12: /* 12 bit GOT offset. */
73 case R_390_GOT16: /* 16 bit GOT offset. */
74 case R_390_GOT20: /* 20 bit GOT offset. */
75 case R_390_GOT32: /* 32 bit GOT offset. */
76 case R_390_GOT64: /* 64 bit GOT offset. */
77 case R_390_GOTENT: /* 32 bit PC rel. to GOT entry shifted by 1. */
78 case R_390_GOTPLT12: /* 12 bit offset to jump slot. */
79 case R_390_GOTPLT16: /* 16 bit offset to jump slot. */
80 case R_390_GOTPLT20: /* 20 bit offset to jump slot. */
81 case R_390_GOTPLT32: /* 32 bit offset to jump slot. */
82 case R_390_GOTPLT64: /* 64 bit offset to jump slot. */
83 case R_390_GOTPLTENT: /* 32 bit rel. offset to jump slot >> 1. */
84 if (info->got_offset == -1UL) {
85 info->got_offset = me->arch.got_size;
86 me->arch.got_size += sizeof(void*);
88 break;
89 case R_390_PLT16DBL: /* 16 bit PC rel. PLT shifted by 1. */
90 case R_390_PLT32DBL: /* 32 bit PC rel. PLT shifted by 1. */
91 case R_390_PLT32: /* 32 bit PC relative PLT address. */
92 case R_390_PLT64: /* 64 bit PC relative PLT address. */
93 case R_390_PLTOFF16: /* 16 bit offset from GOT to PLT. */
94 case R_390_PLTOFF32: /* 32 bit offset from GOT to PLT. */
95 case R_390_PLTOFF64: /* 16 bit offset from GOT to PLT. */
96 if (info->plt_offset == -1UL) {
97 info->plt_offset = me->arch.plt_size;
98 me->arch.plt_size += PLT_ENTRY_SIZE;
100 break;
101 case R_390_COPY:
102 case R_390_GLOB_DAT:
103 case R_390_JMP_SLOT:
104 case R_390_RELATIVE:
105 /* Only needed if we want to support loading of
106 modules linked with -shared. */
107 break;
112 * Account for GOT and PLT relocations. We can't add sections for
113 * got and plt but we can increase the core module size.
115 int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
116 char *secstrings, struct module *me)
118 Elf_Shdr *symtab;
119 Elf_Sym *symbols;
120 Elf_Rela *rela;
121 char *strings;
122 int nrela, i, j;
124 /* Find symbol table and string table. */
125 symtab = NULL;
126 for (i = 0; i < hdr->e_shnum; i++)
127 switch (sechdrs[i].sh_type) {
128 case SHT_SYMTAB:
129 symtab = sechdrs + i;
130 break;
132 if (!symtab) {
133 printk(KERN_ERR "module %s: no symbol table\n", me->name);
134 return -ENOEXEC;
137 /* Allocate one syminfo structure per symbol. */
138 me->arch.nsyms = symtab->sh_size / sizeof(Elf_Sym);
139 me->arch.syminfo = vmalloc(me->arch.nsyms *
140 sizeof(struct mod_arch_syminfo));
141 if (!me->arch.syminfo)
142 return -ENOMEM;
143 symbols = (void *) hdr + symtab->sh_offset;
144 strings = (void *) hdr + sechdrs[symtab->sh_link].sh_offset;
145 for (i = 0; i < me->arch.nsyms; i++) {
146 if (symbols[i].st_shndx == SHN_UNDEF &&
147 strcmp(strings + symbols[i].st_name,
148 "_GLOBAL_OFFSET_TABLE_") == 0)
149 /* "Define" it as absolute. */
150 symbols[i].st_shndx = SHN_ABS;
151 me->arch.syminfo[i].got_offset = -1UL;
152 me->arch.syminfo[i].plt_offset = -1UL;
153 me->arch.syminfo[i].got_initialized = 0;
154 me->arch.syminfo[i].plt_initialized = 0;
157 /* Search for got/plt relocations. */
158 me->arch.got_size = me->arch.plt_size = 0;
159 for (i = 0; i < hdr->e_shnum; i++) {
160 if (sechdrs[i].sh_type != SHT_RELA)
161 continue;
162 nrela = sechdrs[i].sh_size / sizeof(Elf_Rela);
163 rela = (void *) hdr + sechdrs[i].sh_offset;
164 for (j = 0; j < nrela; j++)
165 check_rela(rela + j, me);
168 /* Increase core size by size of got & plt and set start
169 offsets for got and plt. */
170 me->core_layout.size = ALIGN(me->core_layout.size, 4);
171 me->arch.got_offset = me->core_layout.size;
172 me->core_layout.size += me->arch.got_size;
173 me->arch.plt_offset = me->core_layout.size;
174 if (me->arch.plt_size) {
175 if (IS_ENABLED(CONFIG_EXPOLINE) && !nospec_disable)
176 me->arch.plt_size += PLT_ENTRY_SIZE;
177 me->core_layout.size += me->arch.plt_size;
179 return 0;
182 static int apply_rela_bits(Elf_Addr loc, Elf_Addr val,
183 int sign, int bits, int shift)
185 unsigned long umax;
186 long min, max;
188 if (val & ((1UL << shift) - 1))
189 return -ENOEXEC;
190 if (sign) {
191 val = (Elf_Addr)(((long) val) >> shift);
192 min = -(1L << (bits - 1));
193 max = (1L << (bits - 1)) - 1;
194 if ((long) val < min || (long) val > max)
195 return -ENOEXEC;
196 } else {
197 val >>= shift;
198 umax = ((1UL << (bits - 1)) << 1) - 1;
199 if ((unsigned long) val > umax)
200 return -ENOEXEC;
203 if (bits == 8)
204 *(unsigned char *) loc = val;
205 else if (bits == 12)
206 *(unsigned short *) loc = (val & 0xfff) |
207 (*(unsigned short *) loc & 0xf000);
208 else if (bits == 16)
209 *(unsigned short *) loc = val;
210 else if (bits == 20)
211 *(unsigned int *) loc = (val & 0xfff) << 16 |
212 (val & 0xff000) >> 4 |
213 (*(unsigned int *) loc & 0xf00000ff);
214 else if (bits == 32)
215 *(unsigned int *) loc = val;
216 else if (bits == 64)
217 *(unsigned long *) loc = val;
218 return 0;
221 static int apply_rela(Elf_Rela *rela, Elf_Addr base, Elf_Sym *symtab,
222 const char *strtab, struct module *me)
224 struct mod_arch_syminfo *info;
225 Elf_Addr loc, val;
226 int r_type, r_sym;
227 int rc = -ENOEXEC;
229 /* This is where to make the change */
230 loc = base + rela->r_offset;
231 /* This is the symbol it is referring to. Note that all
232 undefined symbols have been resolved. */
233 r_sym = ELF_R_SYM(rela->r_info);
234 r_type = ELF_R_TYPE(rela->r_info);
235 info = me->arch.syminfo + r_sym;
236 val = symtab[r_sym].st_value;
238 switch (r_type) {
239 case R_390_NONE: /* No relocation. */
240 rc = 0;
241 break;
242 case R_390_8: /* Direct 8 bit. */
243 case R_390_12: /* Direct 12 bit. */
244 case R_390_16: /* Direct 16 bit. */
245 case R_390_20: /* Direct 20 bit. */
246 case R_390_32: /* Direct 32 bit. */
247 case R_390_64: /* Direct 64 bit. */
248 val += rela->r_addend;
249 if (r_type == R_390_8)
250 rc = apply_rela_bits(loc, val, 0, 8, 0);
251 else if (r_type == R_390_12)
252 rc = apply_rela_bits(loc, val, 0, 12, 0);
253 else if (r_type == R_390_16)
254 rc = apply_rela_bits(loc, val, 0, 16, 0);
255 else if (r_type == R_390_20)
256 rc = apply_rela_bits(loc, val, 1, 20, 0);
257 else if (r_type == R_390_32)
258 rc = apply_rela_bits(loc, val, 0, 32, 0);
259 else if (r_type == R_390_64)
260 rc = apply_rela_bits(loc, val, 0, 64, 0);
261 break;
262 case R_390_PC16: /* PC relative 16 bit. */
263 case R_390_PC16DBL: /* PC relative 16 bit shifted by 1. */
264 case R_390_PC32DBL: /* PC relative 32 bit shifted by 1. */
265 case R_390_PC32: /* PC relative 32 bit. */
266 case R_390_PC64: /* PC relative 64 bit. */
267 val += rela->r_addend - loc;
268 if (r_type == R_390_PC16)
269 rc = apply_rela_bits(loc, val, 1, 16, 0);
270 else if (r_type == R_390_PC16DBL)
271 rc = apply_rela_bits(loc, val, 1, 16, 1);
272 else if (r_type == R_390_PC32DBL)
273 rc = apply_rela_bits(loc, val, 1, 32, 1);
274 else if (r_type == R_390_PC32)
275 rc = apply_rela_bits(loc, val, 1, 32, 0);
276 else if (r_type == R_390_PC64)
277 rc = apply_rela_bits(loc, val, 1, 64, 0);
278 break;
279 case R_390_GOT12: /* 12 bit GOT offset. */
280 case R_390_GOT16: /* 16 bit GOT offset. */
281 case R_390_GOT20: /* 20 bit GOT offset. */
282 case R_390_GOT32: /* 32 bit GOT offset. */
283 case R_390_GOT64: /* 64 bit GOT offset. */
284 case R_390_GOTENT: /* 32 bit PC rel. to GOT entry shifted by 1. */
285 case R_390_GOTPLT12: /* 12 bit offset to jump slot. */
286 case R_390_GOTPLT20: /* 20 bit offset to jump slot. */
287 case R_390_GOTPLT16: /* 16 bit offset to jump slot. */
288 case R_390_GOTPLT32: /* 32 bit offset to jump slot. */
289 case R_390_GOTPLT64: /* 64 bit offset to jump slot. */
290 case R_390_GOTPLTENT: /* 32 bit rel. offset to jump slot >> 1. */
291 if (info->got_initialized == 0) {
292 Elf_Addr *gotent;
294 gotent = me->core_layout.base + me->arch.got_offset +
295 info->got_offset;
296 *gotent = val;
297 info->got_initialized = 1;
299 val = info->got_offset + rela->r_addend;
300 if (r_type == R_390_GOT12 ||
301 r_type == R_390_GOTPLT12)
302 rc = apply_rela_bits(loc, val, 0, 12, 0);
303 else if (r_type == R_390_GOT16 ||
304 r_type == R_390_GOTPLT16)
305 rc = apply_rela_bits(loc, val, 0, 16, 0);
306 else if (r_type == R_390_GOT20 ||
307 r_type == R_390_GOTPLT20)
308 rc = apply_rela_bits(loc, val, 1, 20, 0);
309 else if (r_type == R_390_GOT32 ||
310 r_type == R_390_GOTPLT32)
311 rc = apply_rela_bits(loc, val, 0, 32, 0);
312 else if (r_type == R_390_GOT64 ||
313 r_type == R_390_GOTPLT64)
314 rc = apply_rela_bits(loc, val, 0, 64, 0);
315 else if (r_type == R_390_GOTENT ||
316 r_type == R_390_GOTPLTENT) {
317 val += (Elf_Addr) me->core_layout.base - loc;
318 rc = apply_rela_bits(loc, val, 1, 32, 1);
320 break;
321 case R_390_PLT16DBL: /* 16 bit PC rel. PLT shifted by 1. */
322 case R_390_PLT32DBL: /* 32 bit PC rel. PLT shifted by 1. */
323 case R_390_PLT32: /* 32 bit PC relative PLT address. */
324 case R_390_PLT64: /* 64 bit PC relative PLT address. */
325 case R_390_PLTOFF16: /* 16 bit offset from GOT to PLT. */
326 case R_390_PLTOFF32: /* 32 bit offset from GOT to PLT. */
327 case R_390_PLTOFF64: /* 16 bit offset from GOT to PLT. */
328 if (info->plt_initialized == 0) {
329 unsigned int *ip;
330 ip = me->core_layout.base + me->arch.plt_offset +
331 info->plt_offset;
332 ip[0] = 0x0d10e310; /* basr 1,0 */
333 ip[1] = 0x100a0004; /* lg 1,10(1) */
334 if (IS_ENABLED(CONFIG_EXPOLINE) && !nospec_disable) {
335 unsigned int *ij;
336 ij = me->core_layout.base +
337 me->arch.plt_offset +
338 me->arch.plt_size - PLT_ENTRY_SIZE;
339 ip[2] = 0xa7f40000 + /* j __jump_r1 */
340 (unsigned int)(u16)
341 (((unsigned long) ij - 8 -
342 (unsigned long) ip) / 2);
343 } else {
344 ip[2] = 0x07f10000; /* br %r1 */
346 ip[3] = (unsigned int) (val >> 32);
347 ip[4] = (unsigned int) val;
348 info->plt_initialized = 1;
350 if (r_type == R_390_PLTOFF16 ||
351 r_type == R_390_PLTOFF32 ||
352 r_type == R_390_PLTOFF64)
353 val = me->arch.plt_offset - me->arch.got_offset +
354 info->plt_offset + rela->r_addend;
355 else {
356 if (!((r_type == R_390_PLT16DBL &&
357 val - loc + 0xffffUL < 0x1ffffeUL) ||
358 (r_type == R_390_PLT32DBL &&
359 val - loc + 0xffffffffULL < 0x1fffffffeULL)))
360 val = (Elf_Addr) me->core_layout.base +
361 me->arch.plt_offset +
362 info->plt_offset;
363 val += rela->r_addend - loc;
365 if (r_type == R_390_PLT16DBL)
366 rc = apply_rela_bits(loc, val, 1, 16, 1);
367 else if (r_type == R_390_PLTOFF16)
368 rc = apply_rela_bits(loc, val, 0, 16, 0);
369 else if (r_type == R_390_PLT32DBL)
370 rc = apply_rela_bits(loc, val, 1, 32, 1);
371 else if (r_type == R_390_PLT32 ||
372 r_type == R_390_PLTOFF32)
373 rc = apply_rela_bits(loc, val, 0, 32, 0);
374 else if (r_type == R_390_PLT64 ||
375 r_type == R_390_PLTOFF64)
376 rc = apply_rela_bits(loc, val, 0, 64, 0);
377 break;
378 case R_390_GOTOFF16: /* 16 bit offset to GOT. */
379 case R_390_GOTOFF32: /* 32 bit offset to GOT. */
380 case R_390_GOTOFF64: /* 64 bit offset to GOT. */
381 val = val + rela->r_addend -
382 ((Elf_Addr) me->core_layout.base + me->arch.got_offset);
383 if (r_type == R_390_GOTOFF16)
384 rc = apply_rela_bits(loc, val, 0, 16, 0);
385 else if (r_type == R_390_GOTOFF32)
386 rc = apply_rela_bits(loc, val, 0, 32, 0);
387 else if (r_type == R_390_GOTOFF64)
388 rc = apply_rela_bits(loc, val, 0, 64, 0);
389 break;
390 case R_390_GOTPC: /* 32 bit PC relative offset to GOT. */
391 case R_390_GOTPCDBL: /* 32 bit PC rel. off. to GOT shifted by 1. */
392 val = (Elf_Addr) me->core_layout.base + me->arch.got_offset +
393 rela->r_addend - loc;
394 if (r_type == R_390_GOTPC)
395 rc = apply_rela_bits(loc, val, 1, 32, 0);
396 else if (r_type == R_390_GOTPCDBL)
397 rc = apply_rela_bits(loc, val, 1, 32, 1);
398 break;
399 case R_390_COPY:
400 case R_390_GLOB_DAT: /* Create GOT entry. */
401 case R_390_JMP_SLOT: /* Create PLT entry. */
402 case R_390_RELATIVE: /* Adjust by program base. */
403 /* Only needed if we want to support loading of
404 modules linked with -shared. */
405 return -ENOEXEC;
406 default:
407 printk(KERN_ERR "module %s: unknown relocation: %u\n",
408 me->name, r_type);
409 return -ENOEXEC;
411 if (rc) {
412 printk(KERN_ERR "module %s: relocation error for symbol %s "
413 "(r_type %i, value 0x%lx)\n",
414 me->name, strtab + symtab[r_sym].st_name,
415 r_type, (unsigned long) val);
416 return rc;
418 return 0;
421 int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
422 unsigned int symindex, unsigned int relsec,
423 struct module *me)
425 Elf_Addr base;
426 Elf_Sym *symtab;
427 Elf_Rela *rela;
428 unsigned long i, n;
429 int rc;
431 DEBUGP("Applying relocate section %u to %u\n",
432 relsec, sechdrs[relsec].sh_info);
433 base = sechdrs[sechdrs[relsec].sh_info].sh_addr;
434 symtab = (Elf_Sym *) sechdrs[symindex].sh_addr;
435 rela = (Elf_Rela *) sechdrs[relsec].sh_addr;
436 n = sechdrs[relsec].sh_size / sizeof(Elf_Rela);
438 for (i = 0; i < n; i++, rela++) {
439 rc = apply_rela(rela, base, symtab, strtab, me);
440 if (rc)
441 return rc;
443 return 0;
446 int module_finalize(const Elf_Ehdr *hdr,
447 const Elf_Shdr *sechdrs,
448 struct module *me)
450 const Elf_Shdr *s;
451 char *secstrings, *secname;
452 void *aseg;
454 if (IS_ENABLED(CONFIG_EXPOLINE) &&
455 !nospec_disable && me->arch.plt_size) {
456 unsigned int *ij;
458 ij = me->core_layout.base + me->arch.plt_offset +
459 me->arch.plt_size - PLT_ENTRY_SIZE;
460 if (test_facility(35)) {
461 ij[0] = 0xc6000000; /* exrl %r0,.+10 */
462 ij[1] = 0x0005a7f4; /* j . */
463 ij[2] = 0x000007f1; /* br %r1 */
464 } else {
465 ij[0] = 0x44000000 | (unsigned int)
466 offsetof(struct lowcore, br_r1_trampoline);
467 ij[1] = 0xa7f40000; /* j . */
471 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
472 for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
473 aseg = (void *) s->sh_addr;
474 secname = secstrings + s->sh_name;
476 if (!strcmp(".altinstructions", secname))
477 /* patch .altinstructions */
478 apply_alternatives(aseg, aseg + s->sh_size);
480 if (IS_ENABLED(CONFIG_EXPOLINE) &&
481 (!strncmp(".s390_indirect", secname, 14)))
482 nospec_revert(aseg, aseg + s->sh_size);
484 if (IS_ENABLED(CONFIG_EXPOLINE) &&
485 (!strncmp(".s390_return", secname, 12)))
486 nospec_revert(aseg, aseg + s->sh_size);
489 jump_label_apply_nops(me);
490 return 0;