• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6/arch/tile/kernel/
1/*
2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
3 *
4 *   This program is free software; you can redistribute it and/or
5 *   modify it under the terms of the GNU General Public License
6 *   as published by the Free Software Foundation, version 2.
7 *
8 *   This program is distributed in the hope that it will be useful, but
9 *   WITHOUT ANY WARRANTY; without even the implied warranty of
10 *   MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 *   NON INFRINGEMENT.  See the GNU General Public License for
12 *   more details.
13 *
14 * Based on i386 version, copyright (C) 2001 Rusty Russell.
15 */
16
17#include <linux/moduleloader.h>
18#include <linux/elf.h>
19#include <linux/vmalloc.h>
20#include <linux/fs.h>
21#include <linux/string.h>
22#include <linux/kernel.h>
23#include <asm/opcode-tile.h>
24#include <asm/pgtable.h>
25
26#ifdef __tilegx__
27# define Elf_Rela Elf64_Rela
28# define ELF_R_SYM ELF64_R_SYM
29# define ELF_R_TYPE ELF64_R_TYPE
30#else
31# define Elf_Rela Elf32_Rela
32# define ELF_R_SYM ELF32_R_SYM
33# define ELF_R_TYPE ELF32_R_TYPE
34#endif
35
36#ifdef MODULE_DEBUG
37#define DEBUGP printk
38#else
39#define DEBUGP(fmt...)
40#endif
41
42/*
43 * Allocate some address space in the range MEM_MODULE_START to
44 * MEM_MODULE_END and populate it with memory.
45 */
46void *module_alloc(unsigned long size)
47{
48	struct page **pages;
49	pgprot_t prot_rwx = __pgprot(_PAGE_KERNEL | _PAGE_KERNEL_EXEC);
50	struct vm_struct *area;
51	int i = 0;
52	int npages;
53
54	if (size == 0)
55		return NULL;
56	npages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
57	pages = kmalloc(npages * sizeof(struct page *), GFP_KERNEL);
58	if (pages == NULL)
59		return NULL;
60	for (; i < npages; ++i) {
61		pages[i] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
62		if (!pages[i])
63			goto error;
64	}
65
66	area = __get_vm_area(size, VM_ALLOC, MEM_MODULE_START, MEM_MODULE_END);
67	if (!area)
68		goto error;
69
70	if (map_vm_area(area, prot_rwx, &pages)) {
71		vunmap(area->addr);
72		goto error;
73	}
74
75	return area->addr;
76
77error:
78	while (--i >= 0)
79		__free_page(pages[i]);
80	kfree(pages);
81	return NULL;
82}
83
84
85/* Free memory returned from module_alloc */
86void module_free(struct module *mod, void *module_region)
87{
88	vfree(module_region);
89}
90
91/* We don't need anything special. */
92int module_frob_arch_sections(Elf_Ehdr *hdr,
93			      Elf_Shdr *sechdrs,
94			      char *secstrings,
95			      struct module *mod)
96{
97	return 0;
98}
99
100int apply_relocate(Elf_Shdr *sechdrs,
101		   const char *strtab,
102		   unsigned int symindex,
103		   unsigned int relsec,
104		   struct module *me)
105{
106	pr_err("module %s: .rel relocation unsupported\n", me->name);
107	return -ENOEXEC;
108}
109
110#ifdef __tilegx__
111/*
112 * Validate that the high 16 bits of "value" is just the sign-extension of
113 * the low 48 bits.
114 */
115static int validate_hw2_last(long value, struct module *me)
116{
117	if (((value << 16) >> 16) != value) {
118		pr_warning("module %s: Out of range HW2_LAST value %#lx\n",
119			   me->name, value);
120		return 0;
121	}
122	return 1;
123}
124
125/*
126 * Validate that "value" isn't too big to hold in a JumpOff relocation.
127 */
128static int validate_jumpoff(long value)
129{
130	/* Determine size of jump offset. */
131	int shift = __builtin_clzl(get_JumpOff_X1(create_JumpOff_X1(-1)));
132
133	/* Check to see if it fits into the relocation slot. */
134	long f = get_JumpOff_X1(create_JumpOff_X1(value));
135	f = (f << shift) >> shift;
136
137	return f == value;
138}
139#endif
140
141int apply_relocate_add(Elf_Shdr *sechdrs,
142		       const char *strtab,
143		       unsigned int symindex,
144		       unsigned int relsec,
145		       struct module *me)
146{
147	unsigned int i;
148	Elf_Rela *rel = (void *)sechdrs[relsec].sh_addr;
149	Elf_Sym *sym;
150	u64 *location;
151	unsigned long value;
152
153	DEBUGP("Applying relocate section %u to %u\n", relsec,
154	       sechdrs[relsec].sh_info);
155	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
156		/* This is where to make the change */
157		location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
158			+ rel[i].r_offset;
159		/*
160		 * This is the symbol it is referring to.
161		 * Note that all undefined symbols have been resolved.
162		 */
163		sym = (Elf_Sym *)sechdrs[symindex].sh_addr
164			+ ELF_R_SYM(rel[i].r_info);
165		value = sym->st_value + rel[i].r_addend;
166
167		switch (ELF_R_TYPE(rel[i].r_info)) {
168
169#define MUNGE(func) (*location = ((*location & ~func(-1)) | func(value)))
170
171#ifndef __tilegx__
172		case R_TILE_32:
173			*(uint32_t *)location = value;
174			break;
175		case R_TILE_IMM16_X0_HA:
176			value = (value + 0x8000) >> 16;
177			/*FALLTHROUGH*/
178		case R_TILE_IMM16_X0_LO:
179			MUNGE(create_Imm16_X0);
180			break;
181		case R_TILE_IMM16_X1_HA:
182			value = (value + 0x8000) >> 16;
183			/*FALLTHROUGH*/
184		case R_TILE_IMM16_X1_LO:
185			MUNGE(create_Imm16_X1);
186			break;
187		case R_TILE_JOFFLONG_X1:
188			value -= (unsigned long) location;  /* pc-relative */
189			value = (long) value >> 3;     /* count by instrs */
190			MUNGE(create_JOffLong_X1);
191			break;
192#else
193		case R_TILEGX_64:
194			*location = value;
195			break;
196		case R_TILEGX_IMM16_X0_HW2_LAST:
197			if (!validate_hw2_last(value, me))
198				return -ENOEXEC;
199			value >>= 16;
200			/*FALLTHROUGH*/
201		case R_TILEGX_IMM16_X0_HW1:
202			value >>= 16;
203			/*FALLTHROUGH*/
204		case R_TILEGX_IMM16_X0_HW0:
205			MUNGE(create_Imm16_X0);
206			break;
207		case R_TILEGX_IMM16_X1_HW2_LAST:
208			if (!validate_hw2_last(value, me))
209				return -ENOEXEC;
210			value >>= 16;
211			/*FALLTHROUGH*/
212		case R_TILEGX_IMM16_X1_HW1:
213			value >>= 16;
214			/*FALLTHROUGH*/
215		case R_TILEGX_IMM16_X1_HW0:
216			MUNGE(create_Imm16_X1);
217			break;
218		case R_TILEGX_JUMPOFF_X1:
219			value -= (unsigned long) location;  /* pc-relative */
220			value = (long) value >> 3;     /* count by instrs */
221			if (!validate_jumpoff(value)) {
222				pr_warning("module %s: Out of range jump to"
223					   " %#llx at %#llx (%p)\n", me->name,
224					   sym->st_value + rel[i].r_addend,
225					   rel[i].r_offset, location);
226				return -ENOEXEC;
227			}
228			MUNGE(create_JumpOff_X1);
229			break;
230#endif
231
232#undef MUNGE
233
234		default:
235			pr_err("module %s: Unknown relocation: %d\n",
236			       me->name, (int) ELF_R_TYPE(rel[i].r_info));
237			return -ENOEXEC;
238		}
239	}
240	return 0;
241}
242
243int module_finalize(const Elf_Ehdr *hdr,
244		    const Elf_Shdr *sechdrs,
245		    struct module *me)
246{
247	return 0;
248}
249
250void module_arch_cleanup(struct module *mod)
251{
252}
253