blob: 97655e0fd2438cdee8188bb4748a3c1c87f825fa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Kernel module help for sparc64.
2 *
3 * Copyright (C) 2001 Rusty Russell.
4 * Copyright (C) 2002 David S. Miller.
5 */
6
7#include <linux/moduleloader.h>
8#include <linux/kernel.h>
9#include <linux/elf.h>
10#include <linux/vmalloc.h>
11#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/string.h>
Sam Ravnborge6b04fe2008-12-26 15:38:17 -080014#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/mm.h>
16
17#include <asm/processor.h>
18#include <asm/spitfire.h>
David Howellsd550bbd2012-03-28 18:30:03 +010019#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
David S. Miller0b641202011-11-17 22:44:58 -080021#include "entry.h"
22
Sam Ravnborg627bf2f2008-12-26 15:37:24 -080023#ifdef CONFIG_SPARC64
David S. Millerdff9d3c2010-09-17 11:09:25 -040024
25#include <linux/jump_label.h>
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static void *module_map(unsigned long size)
28{
David Rientjesd0a21262011-01-13 15:46:02 -080029 if (PAGE_ALIGN(size) > MODULES_LEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 return NULL;
David Rientjesd0a21262011-01-13 15:46:02 -080031 return __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END,
Jianguo Wu40c3baa2013-11-12 15:07:27 -080032 GFP_KERNEL, PAGE_KERNEL, NUMA_NO_NODE,
David Rientjesd0a21262011-01-13 15:46:02 -080033 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -070034}
Sam Ravnborge6b04fe2008-12-26 15:38:17 -080035#else
36static void *module_map(unsigned long size)
37{
38 return vmalloc(size);
39}
Sam Ravnborg627bf2f2008-12-26 15:37:24 -080040#endif /* CONFIG_SPARC64 */
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042void *module_alloc(unsigned long size)
43{
44 void *ret;
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 ret = module_map(size);
Wei Yongjuna9e8d1a2012-09-20 18:57:51 +000047 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 memset(ret, 0, size);
49
50 return ret;
51}
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/* Make generic code ignore STT_REGISTER dummy undefined symbols. */
54int module_frob_arch_sections(Elf_Ehdr *hdr,
55 Elf_Shdr *sechdrs,
56 char *secstrings,
57 struct module *mod)
58{
59 unsigned int symidx;
Sam Ravnborgc45d1c22008-12-26 15:36:29 -080060 Elf_Sym *sym;
Sam Ravnborg627bf2f2008-12-26 15:37:24 -080061 char *strtab;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 int i;
63
64 for (symidx = 0; sechdrs[symidx].sh_type != SHT_SYMTAB; symidx++) {
65 if (symidx == hdr->e_shnum-1) {
66 printk("%s: no symtab found.\n", mod->name);
67 return -ENOEXEC;
68 }
69 }
Sam Ravnborgc45d1c22008-12-26 15:36:29 -080070 sym = (Elf_Sym *)sechdrs[symidx].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 strtab = (char *)sechdrs[sechdrs[symidx].sh_link].sh_addr;
72
73 for (i = 1; i < sechdrs[symidx].sh_size / sizeof(Elf_Sym); i++) {
Sam Ravnborg627bf2f2008-12-26 15:37:24 -080074 if (sym[i].st_shndx == SHN_UNDEF) {
David S. Miller1b35a572012-05-15 11:23:01 -070075 if (ELF_ST_TYPE(sym[i].st_info) == STT_REGISTER)
Sam Ravnborg627bf2f2008-12-26 15:37:24 -080076 sym[i].st_shndx = SHN_ABS;
Sam Ravnborg627bf2f2008-12-26 15:37:24 -080077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 }
79 return 0;
80}
81
Sam Ravnborgc45d1c22008-12-26 15:36:29 -080082int apply_relocate_add(Elf_Shdr *sechdrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 const char *strtab,
84 unsigned int symindex,
85 unsigned int relsec,
86 struct module *me)
87{
88 unsigned int i;
Sam Ravnborgc45d1c22008-12-26 15:36:29 -080089 Elf_Rela *rel = (void *)sechdrs[relsec].sh_addr;
90 Elf_Sym *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 u8 *location;
92 u32 *loc32;
93
94 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
Sam Ravnborgc45d1c22008-12-26 15:36:29 -080095 Elf_Addr v;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 /* This is where to make the change */
98 location = (u8 *)sechdrs[sechdrs[relsec].sh_info].sh_addr
99 + rel[i].r_offset;
100 loc32 = (u32 *) location;
101
Sam Ravnborg627bf2f2008-12-26 15:37:24 -0800102#ifdef CONFIG_SPARC64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 BUG_ON(((u64)location >> (u64)32) != (u64)0);
Sam Ravnborg627bf2f2008-12-26 15:37:24 -0800104#endif /* CONFIG_SPARC64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 /* This is the symbol it is referring to. Note that all
107 undefined symbols have been resolved. */
Sam Ravnborgc45d1c22008-12-26 15:36:29 -0800108 sym = (Elf_Sym *)sechdrs[symindex].sh_addr
109 + ELF_R_SYM(rel[i].r_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 v = sym->st_value + rel[i].r_addend;
111
Sam Ravnborgc45d1c22008-12-26 15:36:29 -0800112 switch (ELF_R_TYPE(rel[i].r_info) & 0xff) {
Andreas Larssondfa520a2012-09-17 19:59:52 +0000113 case R_SPARC_DISP32:
114 v -= (Elf_Addr) location;
115 *loc32 = v;
116 break;
Sam Ravnborg627bf2f2008-12-26 15:37:24 -0800117#ifdef CONFIG_SPARC64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 case R_SPARC_64:
119 location[0] = v >> 56;
120 location[1] = v >> 48;
121 location[2] = v >> 40;
122 location[3] = v >> 32;
123 location[4] = v >> 24;
124 location[5] = v >> 16;
125 location[6] = v >> 8;
126 location[7] = v >> 0;
127 break;
128
Sam Ravnborg627bf2f2008-12-26 15:37:24 -0800129 case R_SPARC_WDISP19:
130 v -= (Elf_Addr) location;
131 *loc32 = (*loc32 & ~0x7ffff) |
132 ((v >> 2) & 0x7ffff);
133 break;
134
135 case R_SPARC_OLO10:
136 *loc32 = (*loc32 & ~0x1fff) |
137 (((v & 0x3ff) +
138 (ELF_R_TYPE(rel[i].r_info) >> 8))
139 & 0x1fff);
140 break;
141#endif /* CONFIG_SPARC64 */
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 case R_SPARC_32:
Sam Ravnborge6b04fe2008-12-26 15:38:17 -0800144 case R_SPARC_UA32:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 location[0] = v >> 24;
146 location[1] = v >> 16;
147 location[2] = v >> 8;
148 location[3] = v >> 0;
149 break;
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 case R_SPARC_WDISP30:
Sam Ravnborgc45d1c22008-12-26 15:36:29 -0800152 v -= (Elf_Addr) location;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 *loc32 = (*loc32 & ~0x3fffffff) |
154 ((v >> 2) & 0x3fffffff);
155 break;
156
157 case R_SPARC_WDISP22:
Sam Ravnborgc45d1c22008-12-26 15:36:29 -0800158 v -= (Elf_Addr) location;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 *loc32 = (*loc32 & ~0x3fffff) |
160 ((v >> 2) & 0x3fffff);
161 break;
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 case R_SPARC_LO10:
164 *loc32 = (*loc32 & ~0x3ff) | (v & 0x3ff);
165 break;
166
167 case R_SPARC_HI22:
168 *loc32 = (*loc32 & ~0x3fffff) |
169 ((v >> 10) & 0x3fffff);
170 break;
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 default:
173 printk(KERN_ERR "module %s: Unknown relocation: %x\n",
174 me->name,
Sam Ravnborgc45d1c22008-12-26 15:36:29 -0800175 (int) (ELF_R_TYPE(rel[i].r_info) & 0xff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return -ENOEXEC;
Joe Perches6cb79b32011-06-03 14:45:23 +0000177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179 return 0;
180}
181
Sam Ravnborg627bf2f2008-12-26 15:37:24 -0800182#ifdef CONFIG_SPARC64
David S. Miller0b641202011-11-17 22:44:58 -0800183static void do_patch_sections(const Elf_Ehdr *hdr,
184 const Elf_Shdr *sechdrs)
185{
186 const Elf_Shdr *s, *sun4v_1insn = NULL, *sun4v_2insn = NULL;
187 char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
188
189 for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
190 if (!strcmp(".sun4v_1insn_patch", secstrings + s->sh_name))
191 sun4v_1insn = s;
192 if (!strcmp(".sun4v_2insn_patch", secstrings + s->sh_name))
193 sun4v_2insn = s;
194 }
195
196 if (sun4v_1insn && tlb_type == hypervisor) {
197 void *p = (void *) sun4v_1insn->sh_addr;
198 sun4v_patch_1insn_range(p, p + sun4v_1insn->sh_size);
199 }
200 if (sun4v_2insn && tlb_type == hypervisor) {
201 void *p = (void *) sun4v_2insn->sh_addr;
202 sun4v_patch_2insn_range(p, p + sun4v_2insn->sh_size);
203 }
204}
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206int module_finalize(const Elf_Ehdr *hdr,
207 const Elf_Shdr *sechdrs,
208 struct module *me)
209{
David S. Millerdff9d3c2010-09-17 11:09:25 -0400210 /* make jump label nops */
211 jump_label_apply_nops(me);
212
David S. Miller0b641202011-11-17 22:44:58 -0800213 do_patch_sections(hdr, sechdrs);
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /* Cheetah's I-cache is fully coherent. */
216 if (tlb_type == spitfire) {
217 unsigned long va;
218
219 flushw_all();
220 for (va = 0; va < (PAGE_SIZE << 1); va += 32)
221 spitfire_put_icache_tag(va, 0x0);
222 __asm__ __volatile__("flush %g6");
223 }
224
225 return 0;
226}
Sam Ravnborg627bf2f2008-12-26 15:37:24 -0800227#endif /* CONFIG_SPARC64 */