1ea42d08349417bc718c8dc289fdc1746edbf5d3
[firefly-linux-kernel-4.4.55.git] / arch / x86 / kernel / module.c
1 /*  Kernel module help for x86.
2     Copyright (C) 2001 Rusty Russell.
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18 #include <linux/moduleloader.h>
19 #include <linux/elf.h>
20 #include <linux/vmalloc.h>
21 #include <linux/fs.h>
22 #include <linux/string.h>
23 #include <linux/kernel.h>
24 #include <linux/bug.h>
25 #include <linux/mm.h>
26
27 #include <asm/system.h>
28 #include <asm/page.h>
29 #include <asm/pgtable.h>
30
31 #if 0
32 #define DEBUGP printk
33 #else
34 #define DEBUGP(fmt...)
35 #endif
36
37 /* Free memory returned from module_alloc */
38 void module_free(struct module *mod, void *module_region)
39 {
40         vfree(module_region);
41         /* FIXME: If module_region == mod->init_region, trim exception
42            table entries. */
43 }
44
45 /* We don't need anything special. */
46 int module_frob_arch_sections(Elf_Ehdr *hdr,
47                               Elf_Shdr *sechdrs,
48                               char *secstrings,
49                               struct module *mod)
50 {
51         return 0;
52 }
53
54 int module_finalize(const Elf_Ehdr *hdr,
55                     const Elf_Shdr *sechdrs,
56                     struct module *me)
57 {
58         const Elf_Shdr *s, *text = NULL, *alt = NULL, *locks = NULL,
59                 *para = NULL;
60         char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
61
62         for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
63                 if (!strcmp(".text", secstrings + s->sh_name))
64                         text = s;
65                 if (!strcmp(".altinstructions", secstrings + s->sh_name))
66                         alt = s;
67                 if (!strcmp(".smp_locks", secstrings + s->sh_name))
68                         locks = s;
69                 if (!strcmp(".parainstructions", secstrings + s->sh_name))
70                         para = s;
71         }
72
73         if (alt) {
74                 /* patch .altinstructions */
75                 void *aseg = (void *)alt->sh_addr;
76                 apply_alternatives(aseg, aseg + alt->sh_size);
77         }
78         if (locks && text) {
79                 void *lseg = (void *)locks->sh_addr;
80                 void *tseg = (void *)text->sh_addr;
81                 alternatives_smp_module_add(me, me->name,
82                                             lseg, lseg + locks->sh_size,
83                                             tseg, tseg + text->sh_size);
84         }
85
86         if (para) {
87                 void *pseg = (void *)para->sh_addr;
88                 apply_paravirt(pseg, pseg + para->sh_size);
89         }
90
91         return module_bug_finalize(hdr, sechdrs, me);
92 }
93
94 void module_arch_cleanup(struct module *mod)
95 {
96         alternatives_smp_module_del(mod);
97         module_bug_cleanup(mod);
98 }