1/*
2 *  This program is free software; you can redistribute it and/or modify
3 *  it under the terms of the GNU General Public License as published by
4 *  the Free Software Foundation; either version 2 of the License, or
5 *  (at your option) any later version.
6 *
7 *  This program is distributed in the hope that it will be useful,
8 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 *  GNU General Public License for more details.
11 *
12 *  You should have received a copy of the GNU General Public License
13 *  along with this program; if not, write to the Free Software
14 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15 *
16 *  Copyright (C) 2001 Rusty Russell.
17 *  Copyright (C) 2003, 2004 Ralf Baechle (ralf@linux-mips.org)
18 *  Copyright (C) 2005 Thiemo Seufer
19 */
20
21#undef DEBUG
22
23#include <linux/moduleloader.h>
24#include <linux/elf.h>
25#include <linux/vmalloc.h>
26#include <linux/slab.h>
27#include <linux/fs.h>
28#include <linux/string.h>
29#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/spinlock.h>
32#include <asm/pgtable.h>	/* MODULE_START */
33
34struct mips_hi16 {
35	struct mips_hi16 *next;
36	Elf_Addr *addr;
37	Elf_Addr value;
38};
39
40static struct mips_hi16 *mips_hi16_list;
41
42static LIST_HEAD(dbe_list);
43static DEFINE_SPINLOCK(dbe_lock);
44
45void *module_alloc(unsigned long size)
46{
47#ifdef MODULE_START
48	struct vm_struct *area;
49
50	size = PAGE_ALIGN(size);
51	if (!size)
52		return NULL;
53
54	area = __get_vm_area(size, VM_ALLOC, MODULE_START, MODULE_END);
55	if (!area)
56		return NULL;
57
58	return __vmalloc_area(area, GFP_KERNEL, PAGE_KERNEL);
59#else
60	if (size == 0)
61		return NULL;
62	return vmalloc(size);
63#endif
64}
65
66/* Free memory returned from module_alloc */
67void module_free(struct module *mod, void *module_region)
68{
69	vfree(module_region);
70}
71
72int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
73			      char *secstrings, struct module *mod)
74{
75	return 0;
76}
77
78static int apply_r_mips_none(struct module *me, u32 *location, Elf_Addr v)
79{
80	return 0;
81}
82
83static int apply_r_mips_32_rel(struct module *me, u32 *location, Elf_Addr v)
84{
85	*location += v;
86
87	return 0;
88}
89
90static int apply_r_mips_32_rela(struct module *me, u32 *location, Elf_Addr v)
91{
92	*location = v;
93
94	return 0;
95}
96
97static int apply_r_mips_26_rel(struct module *me, u32 *location, Elf_Addr v)
98{
99	if (v % 4) {
100		printk(KERN_ERR "module %s: dangerous relocation\n", me->name);
101		return -ENOEXEC;
102	}
103
104	if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
105		printk(KERN_ERR
106		       "module %s: relocation overflow\n",
107		       me->name);
108		return -ENOEXEC;
109	}
110
111	*location = (*location & ~0x03ffffff) |
112	            ((*location + (v >> 2)) & 0x03ffffff);
113
114	return 0;
115}
116
117static int apply_r_mips_26_rela(struct module *me, u32 *location, Elf_Addr v)
118{
119	if (v % 4) {
120		printk(KERN_ERR "module %s: dangerous relocation\n", me->name);
121		return -ENOEXEC;
122	}
123
124	if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
125		printk(KERN_ERR
126		       "module %s: relocation overflow\n",
127		       me->name);
128		return -ENOEXEC;
129	}
130
131	*location = (*location & ~0x03ffffff) | ((v >> 2) & 0x03ffffff);
132
133	return 0;
134}
135
136static int apply_r_mips_hi16_rel(struct module *me, u32 *location, Elf_Addr v)
137{
138	struct mips_hi16 *n;
139
140	/*
141	 * We cannot relocate this one now because we don't know the value of
142	 * the carry we need to add.  Save the information, and let LO16 do the
143	 * actual relocation.
144	 */
145	n = kmalloc(sizeof *n, GFP_KERNEL);
146	if (!n)
147		return -ENOMEM;
148
149	n->addr = (Elf_Addr *)location;
150	n->value = v;
151	n->next = mips_hi16_list;
152	mips_hi16_list = n;
153
154	return 0;
155}
156
157static int apply_r_mips_hi16_rela(struct module *me, u32 *location, Elf_Addr v)
158{
159	*location = (*location & 0xffff0000) |
160	            ((((long long) v + 0x8000LL) >> 16) & 0xffff);
161
162	return 0;
163}
164
165static int apply_r_mips_lo16_rel(struct module *me, u32 *location, Elf_Addr v)
166{
167	unsigned long insnlo = *location;
168	Elf_Addr val, vallo;
169
170	/* Sign extend the addend we extract from the lo insn.  */
171	vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
172
173	if (mips_hi16_list != NULL) {
174		struct mips_hi16 *l;
175
176		l = mips_hi16_list;
177		while (l != NULL) {
178			struct mips_hi16 *next;
179			unsigned long insn;
180
181			/*
182			 * The value for the HI16 had best be the same.
183			 */
184			if (v != l->value)
185				goto out_danger;
186
187			/*
188			 * Do the HI16 relocation.  Note that we actually don't
189			 * need to know anything about the LO16 itself, except
190			 * where to find the low 16 bits of the addend needed
191			 * by the LO16.
192			 */
193			insn = *l->addr;
194			val = ((insn & 0xffff) << 16) + vallo;
195			val += v;
196
197			/*
198			 * Account for the sign extension that will happen in
199			 * the low bits.
200			 */
201			val = ((val >> 16) + ((val & 0x8000) != 0)) & 0xffff;
202
203			insn = (insn & ~0xffff) | val;
204			*l->addr = insn;
205
206			next = l->next;
207			kfree(l);
208			l = next;
209		}
210
211		mips_hi16_list = NULL;
212	}
213
214	/*
215	 * Ok, we're done with the HI16 relocs.  Now deal with the LO16.
216	 */
217	val = v + vallo;
218	insnlo = (insnlo & ~0xffff) | (val & 0xffff);
219	*location = insnlo;
220
221	return 0;
222
223out_danger:
224	printk(KERN_ERR "module %s: dangerous " "relocation\n", me->name);
225
226	return -ENOEXEC;
227}
228
229static int apply_r_mips_lo16_rela(struct module *me, u32 *location, Elf_Addr v)
230{
231	*location = (*location & 0xffff0000) | (v & 0xffff);
232
233	return 0;
234}
235
236static int apply_r_mips_64_rela(struct module *me, u32 *location, Elf_Addr v)
237{
238	*(Elf_Addr *)location = v;
239
240	return 0;
241}
242
243static int apply_r_mips_higher_rela(struct module *me, u32 *location,
244				    Elf_Addr v)
245{
246	*location = (*location & 0xffff0000) |
247	            ((((long long) v + 0x80008000LL) >> 32) & 0xffff);
248
249	return 0;
250}
251
252static int apply_r_mips_highest_rela(struct module *me, u32 *location,
253				     Elf_Addr v)
254{
255	*location = (*location & 0xffff0000) |
256	            ((((long long) v + 0x800080008000LL) >> 48) & 0xffff);
257
258	return 0;
259}
260
261static int (*reloc_handlers_rel[]) (struct module *me, u32 *location,
262				Elf_Addr v) = {
263	[R_MIPS_NONE]		= apply_r_mips_none,
264	[R_MIPS_32]		= apply_r_mips_32_rel,
265	[R_MIPS_26]		= apply_r_mips_26_rel,
266	[R_MIPS_HI16]		= apply_r_mips_hi16_rel,
267	[R_MIPS_LO16]		= apply_r_mips_lo16_rel
268};
269
270static int (*reloc_handlers_rela[]) (struct module *me, u32 *location,
271				Elf_Addr v) = {
272	[R_MIPS_NONE]		= apply_r_mips_none,
273	[R_MIPS_32]		= apply_r_mips_32_rela,
274	[R_MIPS_26]		= apply_r_mips_26_rela,
275	[R_MIPS_HI16]		= apply_r_mips_hi16_rela,
276	[R_MIPS_LO16]		= apply_r_mips_lo16_rela,
277	[R_MIPS_64]		= apply_r_mips_64_rela,
278	[R_MIPS_HIGHER]		= apply_r_mips_higher_rela,
279	[R_MIPS_HIGHEST]	= apply_r_mips_highest_rela
280};
281
282int apply_relocate(Elf_Shdr *sechdrs, const char *strtab,
283		   unsigned int symindex, unsigned int relsec,
284		   struct module *me)
285{
286	Elf_Mips_Rel *rel = (void *) sechdrs[relsec].sh_addr;
287	Elf_Sym *sym;
288	u32 *location;
289	unsigned int i;
290	Elf_Addr v;
291	int res;
292
293	pr_debug("Applying relocate section %u to %u\n", relsec,
294	       sechdrs[relsec].sh_info);
295
296	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
297		/* This is where to make the change */
298		location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
299			+ rel[i].r_offset;
300		/* This is the symbol it is referring to */
301		sym = (Elf_Sym *)sechdrs[symindex].sh_addr
302			+ ELF_MIPS_R_SYM(rel[i]);
303		if (!sym->st_value) {
304			/* Ignore unresolved weak symbol */
305			if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
306				continue;
307			printk(KERN_WARNING "%s: Unknown symbol %s\n",
308			       me->name, strtab + sym->st_name);
309			return -ENOENT;
310		}
311
312		v = sym->st_value;
313
314		res = reloc_handlers_rel[ELF_MIPS_R_TYPE(rel[i])](me, location, v);
315		if (res)
316			return res;
317	}
318
319	return 0;
320}
321
322int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
323		       unsigned int symindex, unsigned int relsec,
324		       struct module *me)
325{
326	Elf_Mips_Rela *rel = (void *) sechdrs[relsec].sh_addr;
327	Elf_Sym *sym;
328	u32 *location;
329	unsigned int i;
330	Elf_Addr v;
331	int res;
332
333	pr_debug("Applying relocate section %u to %u\n", relsec,
334	       sechdrs[relsec].sh_info);
335
336	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
337		/* This is where to make the change */
338		location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
339			+ rel[i].r_offset;
340		/* This is the symbol it is referring to */
341		sym = (Elf_Sym *)sechdrs[symindex].sh_addr
342			+ ELF_MIPS_R_SYM(rel[i]);
343		if (!sym->st_value) {
344			/* Ignore unresolved weak symbol */
345			if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
346				continue;
347			printk(KERN_WARNING "%s: Unknown symbol %s\n",
348			       me->name, strtab + sym->st_name);
349			return -ENOENT;
350		}
351
352		v = sym->st_value + rel[i].r_addend;
353
354		res = reloc_handlers_rela[ELF_MIPS_R_TYPE(rel[i])](me, location, v);
355		if (res)
356			return res;
357	}
358
359	return 0;
360}
361
362/* Given an address, look for it in the module exception tables. */
363const struct exception_table_entry *search_module_dbetables(unsigned long addr)
364{
365	unsigned long flags;
366	const struct exception_table_entry *e = NULL;
367	struct mod_arch_specific *dbe;
368
369	spin_lock_irqsave(&dbe_lock, flags);
370	list_for_each_entry(dbe, &dbe_list, dbe_list) {
371		e = search_extable(dbe->dbe_start, dbe->dbe_end - 1, addr);
372		if (e)
373			break;
374	}
375	spin_unlock_irqrestore(&dbe_lock, flags);
376
377	/* Now, if we found one, we are running inside it now, hence
378           we cannot unload the module, hence no refcnt needed. */
379	return e;
380}
381
382/* Put in dbe list if neccessary. */
383int module_finalize(const Elf_Ehdr *hdr,
384		    const Elf_Shdr *sechdrs,
385		    struct module *me)
386{
387	const Elf_Shdr *s;
388	char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
389
390	INIT_LIST_HEAD(&me->arch.dbe_list);
391	for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
392		if (strcmp("__dbe_table", secstrings + s->sh_name) != 0)
393			continue;
394		me->arch.dbe_start = (void *)s->sh_addr;
395		me->arch.dbe_end = (void *)s->sh_addr + s->sh_size;
396		spin_lock_irq(&dbe_lock);
397		list_add(&me->arch.dbe_list, &dbe_list);
398		spin_unlock_irq(&dbe_lock);
399	}
400	return 0;
401}
402
403void module_arch_cleanup(struct module *mod)
404{
405	spin_lock_irq(&dbe_lock);
406	list_del(&mod->arch.dbe_list);
407	spin_unlock_irq(&dbe_lock);
408}
409