OSDN Git Service

s390: introduce CPU alternatives
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / arch / s390 / kernel / module.c
1 /*
2  *  Kernel module help for s390.
3  *
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)
8  *
9  *  based on i386 version
10  *    Copyright (C) 2001 Rusty Russell.
11  *
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.
16  *
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.
21  *
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
25  */
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
36 #if 0
37 #define DEBUGP printk
38 #else
39 #define DEBUGP(fmt , ...)
40 #endif
41
42 #define PLT_ENTRY_SIZE 20
43
44 void *module_alloc(unsigned long size)
45 {
46         if (PAGE_ALIGN(size) > MODULES_LEN)
47                 return NULL;
48         return __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END,
49                                     GFP_KERNEL, PAGE_KERNEL, 0, NUMA_NO_NODE,
50                                     __builtin_return_address(0));
51 }
52
53 void module_arch_freeing_init(struct module *mod)
54 {
55         vfree(mod->arch.syminfo);
56         mod->arch.syminfo = NULL;
57 }
58
59 static void check_rela(Elf_Rela *rela, struct module *me)
60 {
61         struct mod_arch_syminfo *info;
62
63         info = me->arch.syminfo + ELF_R_SYM (rela->r_info);
64         switch (ELF_R_TYPE (rela->r_info)) {
65         case R_390_GOT12:       /* 12 bit GOT offset.  */
66         case R_390_GOT16:       /* 16 bit GOT offset.  */
67         case R_390_GOT20:       /* 20 bit GOT offset.  */
68         case R_390_GOT32:       /* 32 bit GOT offset.  */
69         case R_390_GOT64:       /* 64 bit GOT offset.  */
70         case R_390_GOTENT:      /* 32 bit PC rel. to GOT entry shifted by 1. */
71         case R_390_GOTPLT12:    /* 12 bit offset to jump slot.  */
72         case R_390_GOTPLT16:    /* 16 bit offset to jump slot.  */
73         case R_390_GOTPLT20:    /* 20 bit offset to jump slot.  */
74         case R_390_GOTPLT32:    /* 32 bit offset to jump slot.  */
75         case R_390_GOTPLT64:    /* 64 bit offset to jump slot.  */
76         case R_390_GOTPLTENT:   /* 32 bit rel. offset to jump slot >> 1. */
77                 if (info->got_offset == -1UL) {
78                         info->got_offset = me->arch.got_size;
79                         me->arch.got_size += sizeof(void*);
80                 }
81                 break;
82         case R_390_PLT16DBL:    /* 16 bit PC rel. PLT shifted by 1.  */
83         case R_390_PLT32DBL:    /* 32 bit PC rel. PLT shifted by 1.  */
84         case R_390_PLT32:       /* 32 bit PC relative PLT address.  */
85         case R_390_PLT64:       /* 64 bit PC relative PLT address.  */
86         case R_390_PLTOFF16:    /* 16 bit offset from GOT to PLT. */
87         case R_390_PLTOFF32:    /* 32 bit offset from GOT to PLT. */
88         case R_390_PLTOFF64:    /* 16 bit offset from GOT to PLT. */
89                 if (info->plt_offset == -1UL) {
90                         info->plt_offset = me->arch.plt_size;
91                         me->arch.plt_size += PLT_ENTRY_SIZE;
92                 }
93                 break;
94         case R_390_COPY:
95         case R_390_GLOB_DAT:
96         case R_390_JMP_SLOT:
97         case R_390_RELATIVE:
98                 /* Only needed if we want to support loading of 
99                    modules linked with -shared. */
100                 break;
101         }
102 }
103
104 /*
105  * Account for GOT and PLT relocations. We can't add sections for
106  * got and plt but we can increase the core module size.
107  */
108 int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
109                               char *secstrings, struct module *me)
110 {
111         Elf_Shdr *symtab;
112         Elf_Sym *symbols;
113         Elf_Rela *rela;
114         char *strings;
115         int nrela, i, j;
116
117         /* Find symbol table and string table. */
118         symtab = NULL;
119         for (i = 0; i < hdr->e_shnum; i++)
120                 switch (sechdrs[i].sh_type) {
121                 case SHT_SYMTAB:
122                         symtab = sechdrs + i;
123                         break;
124                 }
125         if (!symtab) {
126                 printk(KERN_ERR "module %s: no symbol table\n", me->name);
127                 return -ENOEXEC;
128         }
129
130         /* Allocate one syminfo structure per symbol. */
131         me->arch.nsyms = symtab->sh_size / sizeof(Elf_Sym);
132         me->arch.syminfo = vmalloc(me->arch.nsyms *
133                                    sizeof(struct mod_arch_syminfo));
134         if (!me->arch.syminfo)
135                 return -ENOMEM;
136         symbols = (void *) hdr + symtab->sh_offset;
137         strings = (void *) hdr + sechdrs[symtab->sh_link].sh_offset;
138         for (i = 0; i < me->arch.nsyms; i++) {
139                 if (symbols[i].st_shndx == SHN_UNDEF &&
140                     strcmp(strings + symbols[i].st_name,
141                            "_GLOBAL_OFFSET_TABLE_") == 0)
142                         /* "Define" it as absolute. */
143                         symbols[i].st_shndx = SHN_ABS;
144                 me->arch.syminfo[i].got_offset = -1UL;
145                 me->arch.syminfo[i].plt_offset = -1UL;
146                 me->arch.syminfo[i].got_initialized = 0;
147                 me->arch.syminfo[i].plt_initialized = 0;
148         }
149
150         /* Search for got/plt relocations. */
151         me->arch.got_size = me->arch.plt_size = 0;
152         for (i = 0; i < hdr->e_shnum; i++) {
153                 if (sechdrs[i].sh_type != SHT_RELA)
154                         continue;
155                 nrela = sechdrs[i].sh_size / sizeof(Elf_Rela);
156                 rela = (void *) hdr + sechdrs[i].sh_offset;
157                 for (j = 0; j < nrela; j++)
158                         check_rela(rela + j, me);
159         }
160
161         /* Increase core size by size of got & plt and set start
162            offsets for got and plt. */
163         me->core_size = ALIGN(me->core_size, 4);
164         me->arch.got_offset = me->core_size;
165         me->core_size += me->arch.got_size;
166         me->arch.plt_offset = me->core_size;
167         me->core_size += me->arch.plt_size;
168         return 0;
169 }
170
171 static int apply_rela_bits(Elf_Addr loc, Elf_Addr val,
172                            int sign, int bits, int shift)
173 {
174         unsigned long umax;
175         long min, max;
176
177         if (val & ((1UL << shift) - 1))
178                 return -ENOEXEC;
179         if (sign) {
180                 val = (Elf_Addr)(((long) val) >> shift);
181                 min = -(1L << (bits - 1));
182                 max = (1L << (bits - 1)) - 1;
183                 if ((long) val < min || (long) val > max)
184                         return -ENOEXEC;
185         } else {
186                 val >>= shift;
187                 umax = ((1UL << (bits - 1)) << 1) - 1;
188                 if ((unsigned long) val > umax)
189                         return -ENOEXEC;
190         }
191
192         if (bits == 8)
193                 *(unsigned char *) loc = val;
194         else if (bits == 12)
195                 *(unsigned short *) loc = (val & 0xfff) |
196                         (*(unsigned short *) loc & 0xf000);
197         else if (bits == 16)
198                 *(unsigned short *) loc = val;
199         else if (bits == 20)
200                 *(unsigned int *) loc = (val & 0xfff) << 16 |
201                         (val & 0xff000) >> 4 |
202                         (*(unsigned int *) loc & 0xf00000ff);
203         else if (bits == 32)
204                 *(unsigned int *) loc = val;
205         else if (bits == 64)
206                 *(unsigned long *) loc = val;
207         return 0;
208 }
209
210 static int apply_rela(Elf_Rela *rela, Elf_Addr base, Elf_Sym *symtab,
211                       const char *strtab, struct module *me)
212 {
213         struct mod_arch_syminfo *info;
214         Elf_Addr loc, val;
215         int r_type, r_sym;
216         int rc = -ENOEXEC;
217
218         /* This is where to make the change */
219         loc = base + rela->r_offset;
220         /* This is the symbol it is referring to.  Note that all
221            undefined symbols have been resolved.  */
222         r_sym = ELF_R_SYM(rela->r_info);
223         r_type = ELF_R_TYPE(rela->r_info);
224         info = me->arch.syminfo + r_sym;
225         val = symtab[r_sym].st_value;
226
227         switch (r_type) {
228         case R_390_NONE:        /* No relocation.  */
229                 rc = 0;
230                 break;
231         case R_390_8:           /* Direct 8 bit.   */
232         case R_390_12:          /* Direct 12 bit.  */
233         case R_390_16:          /* Direct 16 bit.  */
234         case R_390_20:          /* Direct 20 bit.  */
235         case R_390_32:          /* Direct 32 bit.  */
236         case R_390_64:          /* Direct 64 bit.  */
237                 val += rela->r_addend;
238                 if (r_type == R_390_8)
239                         rc = apply_rela_bits(loc, val, 0, 8, 0);
240                 else if (r_type == R_390_12)
241                         rc = apply_rela_bits(loc, val, 0, 12, 0);
242                 else if (r_type == R_390_16)
243                         rc = apply_rela_bits(loc, val, 0, 16, 0);
244                 else if (r_type == R_390_20)
245                         rc = apply_rela_bits(loc, val, 1, 20, 0);
246                 else if (r_type == R_390_32)
247                         rc = apply_rela_bits(loc, val, 0, 32, 0);
248                 else if (r_type == R_390_64)
249                         rc = apply_rela_bits(loc, val, 0, 64, 0);
250                 break;
251         case R_390_PC16:        /* PC relative 16 bit.  */
252         case R_390_PC16DBL:     /* PC relative 16 bit shifted by 1.  */
253         case R_390_PC32DBL:     /* PC relative 32 bit shifted by 1.  */
254         case R_390_PC32:        /* PC relative 32 bit.  */
255         case R_390_PC64:        /* PC relative 64 bit.  */
256                 val += rela->r_addend - loc;
257                 if (r_type == R_390_PC16)
258                         rc = apply_rela_bits(loc, val, 1, 16, 0);
259                 else if (r_type == R_390_PC16DBL)
260                         rc = apply_rela_bits(loc, val, 1, 16, 1);
261                 else if (r_type == R_390_PC32DBL)
262                         rc = apply_rela_bits(loc, val, 1, 32, 1);
263                 else if (r_type == R_390_PC32)
264                         rc = apply_rela_bits(loc, val, 1, 32, 0);
265                 else if (r_type == R_390_PC64)
266                         rc = apply_rela_bits(loc, val, 1, 64, 0);
267                 break;
268         case R_390_GOT12:       /* 12 bit GOT offset.  */
269         case R_390_GOT16:       /* 16 bit GOT offset.  */
270         case R_390_GOT20:       /* 20 bit GOT offset.  */
271         case R_390_GOT32:       /* 32 bit GOT offset.  */
272         case R_390_GOT64:       /* 64 bit GOT offset.  */
273         case R_390_GOTENT:      /* 32 bit PC rel. to GOT entry shifted by 1. */
274         case R_390_GOTPLT12:    /* 12 bit offset to jump slot.  */
275         case R_390_GOTPLT20:    /* 20 bit offset to jump slot.  */
276         case R_390_GOTPLT16:    /* 16 bit offset to jump slot.  */
277         case R_390_GOTPLT32:    /* 32 bit offset to jump slot.  */
278         case R_390_GOTPLT64:    /* 64 bit offset to jump slot.  */
279         case R_390_GOTPLTENT:   /* 32 bit rel. offset to jump slot >> 1. */
280                 if (info->got_initialized == 0) {
281                         Elf_Addr *gotent;
282
283                         gotent = me->module_core + me->arch.got_offset +
284                                 info->got_offset;
285                         *gotent = val;
286                         info->got_initialized = 1;
287                 }
288                 val = info->got_offset + rela->r_addend;
289                 if (r_type == R_390_GOT12 ||
290                     r_type == R_390_GOTPLT12)
291                         rc = apply_rela_bits(loc, val, 0, 12, 0);
292                 else if (r_type == R_390_GOT16 ||
293                          r_type == R_390_GOTPLT16)
294                         rc = apply_rela_bits(loc, val, 0, 16, 0);
295                 else if (r_type == R_390_GOT20 ||
296                          r_type == R_390_GOTPLT20)
297                         rc = apply_rela_bits(loc, val, 1, 20, 0);
298                 else if (r_type == R_390_GOT32 ||
299                          r_type == R_390_GOTPLT32)
300                         rc = apply_rela_bits(loc, val, 0, 32, 0);
301                 else if (r_type == R_390_GOT64 ||
302                          r_type == R_390_GOTPLT64)
303                         rc = apply_rela_bits(loc, val, 0, 64, 0);
304                 else if (r_type == R_390_GOTENT ||
305                          r_type == R_390_GOTPLTENT) {
306                         val += (Elf_Addr) me->module_core - loc;
307                         rc = apply_rela_bits(loc, val, 1, 32, 1);
308                 }
309                 break;
310         case R_390_PLT16DBL:    /* 16 bit PC rel. PLT shifted by 1.  */
311         case R_390_PLT32DBL:    /* 32 bit PC rel. PLT shifted by 1.  */
312         case R_390_PLT32:       /* 32 bit PC relative PLT address.  */
313         case R_390_PLT64:       /* 64 bit PC relative PLT address.  */
314         case R_390_PLTOFF16:    /* 16 bit offset from GOT to PLT. */
315         case R_390_PLTOFF32:    /* 32 bit offset from GOT to PLT. */
316         case R_390_PLTOFF64:    /* 16 bit offset from GOT to PLT. */
317                 if (info->plt_initialized == 0) {
318                         unsigned int *ip;
319                         ip = me->module_core + me->arch.plt_offset +
320                                 info->plt_offset;
321                         ip[0] = 0x0d10e310; /* basr 1,0; lg 1,10(1); br 1 */
322                         ip[1] = 0x100a0004;
323                         ip[2] = 0x07f10000;
324                         ip[3] = (unsigned int) (val >> 32);
325                         ip[4] = (unsigned int) val;
326                         info->plt_initialized = 1;
327                 }
328                 if (r_type == R_390_PLTOFF16 ||
329                     r_type == R_390_PLTOFF32 ||
330                     r_type == R_390_PLTOFF64)
331                         val = me->arch.plt_offset - me->arch.got_offset +
332                                 info->plt_offset + rela->r_addend;
333                 else {
334                         if (!((r_type == R_390_PLT16DBL &&
335                                val - loc + 0xffffUL < 0x1ffffeUL) ||
336                               (r_type == R_390_PLT32DBL &&
337                                val - loc + 0xffffffffULL < 0x1fffffffeULL)))
338                                 val = (Elf_Addr) me->module_core +
339                                         me->arch.plt_offset +
340                                         info->plt_offset;
341                         val += rela->r_addend - loc;
342                 }
343                 if (r_type == R_390_PLT16DBL)
344                         rc = apply_rela_bits(loc, val, 1, 16, 1);
345                 else if (r_type == R_390_PLTOFF16)
346                         rc = apply_rela_bits(loc, val, 0, 16, 0);
347                 else if (r_type == R_390_PLT32DBL)
348                         rc = apply_rela_bits(loc, val, 1, 32, 1);
349                 else if (r_type == R_390_PLT32 ||
350                          r_type == R_390_PLTOFF32)
351                         rc = apply_rela_bits(loc, val, 0, 32, 0);
352                 else if (r_type == R_390_PLT64 ||
353                          r_type == R_390_PLTOFF64)
354                         rc = apply_rela_bits(loc, val, 0, 64, 0);
355                 break;
356         case R_390_GOTOFF16:    /* 16 bit offset to GOT.  */
357         case R_390_GOTOFF32:    /* 32 bit offset to GOT.  */
358         case R_390_GOTOFF64:    /* 64 bit offset to GOT. */
359                 val = val + rela->r_addend -
360                         ((Elf_Addr) me->module_core + me->arch.got_offset);
361                 if (r_type == R_390_GOTOFF16)
362                         rc = apply_rela_bits(loc, val, 0, 16, 0);
363                 else if (r_type == R_390_GOTOFF32)
364                         rc = apply_rela_bits(loc, val, 0, 32, 0);
365                 else if (r_type == R_390_GOTOFF64)
366                         rc = apply_rela_bits(loc, val, 0, 64, 0);
367                 break;
368         case R_390_GOTPC:       /* 32 bit PC relative offset to GOT. */
369         case R_390_GOTPCDBL:    /* 32 bit PC rel. off. to GOT shifted by 1. */
370                 val = (Elf_Addr) me->module_core + me->arch.got_offset +
371                         rela->r_addend - loc;
372                 if (r_type == R_390_GOTPC)
373                         rc = apply_rela_bits(loc, val, 1, 32, 0);
374                 else if (r_type == R_390_GOTPCDBL)
375                         rc = apply_rela_bits(loc, val, 1, 32, 1);
376                 break;
377         case R_390_COPY:
378         case R_390_GLOB_DAT:    /* Create GOT entry.  */
379         case R_390_JMP_SLOT:    /* Create PLT entry.  */
380         case R_390_RELATIVE:    /* Adjust by program base.  */
381                 /* Only needed if we want to support loading of 
382                    modules linked with -shared. */
383                 return -ENOEXEC;
384         default:
385                 printk(KERN_ERR "module %s: unknown relocation: %u\n",
386                        me->name, r_type);
387                 return -ENOEXEC;
388         }
389         if (rc) {
390                 printk(KERN_ERR "module %s: relocation error for symbol %s "
391                        "(r_type %i, value 0x%lx)\n",
392                        me->name, strtab + symtab[r_sym].st_name,
393                        r_type, (unsigned long) val);
394                 return rc;
395         }
396         return 0;
397 }
398
399 int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
400                        unsigned int symindex, unsigned int relsec,
401                        struct module *me)
402 {
403         Elf_Addr base;
404         Elf_Sym *symtab;
405         Elf_Rela *rela;
406         unsigned long i, n;
407         int rc;
408
409         DEBUGP("Applying relocate section %u to %u\n",
410                relsec, sechdrs[relsec].sh_info);
411         base = sechdrs[sechdrs[relsec].sh_info].sh_addr;
412         symtab = (Elf_Sym *) sechdrs[symindex].sh_addr;
413         rela = (Elf_Rela *) sechdrs[relsec].sh_addr;
414         n = sechdrs[relsec].sh_size / sizeof(Elf_Rela);
415
416         for (i = 0; i < n; i++, rela++) {
417                 rc = apply_rela(rela, base, symtab, strtab, me);
418                 if (rc)
419                         return rc;
420         }
421         return 0;
422 }
423
424 int module_finalize(const Elf_Ehdr *hdr,
425                     const Elf_Shdr *sechdrs,
426                     struct module *me)
427 {
428         const Elf_Shdr *s;
429         char *secstrings;
430
431         if (IS_ENABLED(CONFIG_ALTERNATIVES)) {
432                 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
433                 for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
434                         if (!strcmp(".altinstructions",
435                                     secstrings + s->sh_name)) {
436                                 /* patch .altinstructions */
437                                 void *aseg = (void *)s->sh_addr;
438
439                                 apply_alternatives(aseg, aseg + s->sh_size);
440                         }
441                 }
442         }
443
444         jump_label_apply_nops(me);
445         vfree(me->arch.syminfo);
446         me->arch.syminfo = NULL;
447         return 0;
448 }