Loading...
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#include <asm/homecache.h>
26
27#ifdef __tilegx__
28# define Elf_Rela Elf64_Rela
29# define ELF_R_SYM ELF64_R_SYM
30# define ELF_R_TYPE ELF64_R_TYPE
31#else
32# define Elf_Rela Elf32_Rela
33# define ELF_R_SYM ELF32_R_SYM
34# define ELF_R_TYPE ELF32_R_TYPE
35#endif
36
37#ifdef MODULE_DEBUG
38#define DEBUGP printk
39#else
40#define DEBUGP(fmt...)
41#endif
42
43/*
44 * Allocate some address space in the range MEM_MODULE_START to
45 * MEM_MODULE_END and populate it with memory.
46 */
47void *module_alloc(unsigned long size)
48{
49 struct page **pages;
50 pgprot_t prot_rwx = __pgprot(_PAGE_KERNEL | _PAGE_KERNEL_EXEC);
51 struct vm_struct *area;
52 int i = 0;
53 int npages;
54
55 if (size == 0)
56 return NULL;
57 npages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
58 pages = kmalloc(npages * sizeof(struct page *), GFP_KERNEL);
59 if (pages == NULL)
60 return NULL;
61 for (; i < npages; ++i) {
62 pages[i] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
63 if (!pages[i])
64 goto error;
65 }
66
67 area = __get_vm_area(size, VM_ALLOC, MEM_MODULE_START, MEM_MODULE_END);
68 if (!area)
69 goto error;
70
71 if (map_vm_area(area, prot_rwx, &pages)) {
72 vunmap(area->addr);
73 goto error;
74 }
75
76 return area->addr;
77
78error:
79 while (--i >= 0)
80 __free_page(pages[i]);
81 kfree(pages);
82 return NULL;
83}
84
85
86/* Free memory returned from module_alloc */
87void module_free(struct module *mod, void *module_region)
88{
89 vfree(module_region);
90
91 /* Globally flush the L1 icache. */
92 flush_remote(0, HV_FLUSH_EVICT_L1I, cpu_online_mask,
93 0, 0, 0, NULL, NULL, 0);
94
95 /*
96 * FIXME: If module_region == mod->module_init, trim exception
97 * table entries.
98 */
99}
100
101#ifdef __tilegx__
102/*
103 * Validate that the high 16 bits of "value" is just the sign-extension of
104 * the low 48 bits.
105 */
106static int validate_hw2_last(long value, struct module *me)
107{
108 if (((value << 16) >> 16) != value) {
109 pr_warning("module %s: Out of range HW2_LAST value %#lx\n",
110 me->name, value);
111 return 0;
112 }
113 return 1;
114}
115
116/*
117 * Validate that "value" isn't too big to hold in a JumpOff relocation.
118 */
119static int validate_jumpoff(long value)
120{
121 /* Determine size of jump offset. */
122 int shift = __builtin_clzl(get_JumpOff_X1(create_JumpOff_X1(-1)));
123
124 /* Check to see if it fits into the relocation slot. */
125 long f = get_JumpOff_X1(create_JumpOff_X1(value));
126 f = (f << shift) >> shift;
127
128 return f == value;
129}
130#endif
131
132int apply_relocate_add(Elf_Shdr *sechdrs,
133 const char *strtab,
134 unsigned int symindex,
135 unsigned int relsec,
136 struct module *me)
137{
138 unsigned int i;
139 Elf_Rela *rel = (void *)sechdrs[relsec].sh_addr;
140 Elf_Sym *sym;
141 u64 *location;
142 unsigned long value;
143
144 DEBUGP("Applying relocate section %u to %u\n", relsec,
145 sechdrs[relsec].sh_info);
146 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
147 /* This is where to make the change */
148 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
149 + rel[i].r_offset;
150 /*
151 * This is the symbol it is referring to.
152 * Note that all undefined symbols have been resolved.
153 */
154 sym = (Elf_Sym *)sechdrs[symindex].sh_addr
155 + ELF_R_SYM(rel[i].r_info);
156 value = sym->st_value + rel[i].r_addend;
157
158 switch (ELF_R_TYPE(rel[i].r_info)) {
159
160#define MUNGE(func) (*location = ((*location & ~func(-1)) | func(value)))
161
162#ifndef __tilegx__
163 case R_TILE_32:
164 *(uint32_t *)location = value;
165 break;
166 case R_TILE_IMM16_X0_HA:
167 value = (value + 0x8000) >> 16;
168 /*FALLTHROUGH*/
169 case R_TILE_IMM16_X0_LO:
170 MUNGE(create_Imm16_X0);
171 break;
172 case R_TILE_IMM16_X1_HA:
173 value = (value + 0x8000) >> 16;
174 /*FALLTHROUGH*/
175 case R_TILE_IMM16_X1_LO:
176 MUNGE(create_Imm16_X1);
177 break;
178 case R_TILE_JOFFLONG_X1:
179 value -= (unsigned long) location; /* pc-relative */
180 value = (long) value >> 3; /* count by instrs */
181 MUNGE(create_JOffLong_X1);
182 break;
183#else
184 case R_TILEGX_64:
185 *location = value;
186 break;
187 case R_TILEGX_IMM16_X0_HW2_LAST:
188 if (!validate_hw2_last(value, me))
189 return -ENOEXEC;
190 value >>= 16;
191 /*FALLTHROUGH*/
192 case R_TILEGX_IMM16_X0_HW1:
193 value >>= 16;
194 /*FALLTHROUGH*/
195 case R_TILEGX_IMM16_X0_HW0:
196 MUNGE(create_Imm16_X0);
197 break;
198 case R_TILEGX_IMM16_X1_HW2_LAST:
199 if (!validate_hw2_last(value, me))
200 return -ENOEXEC;
201 value >>= 16;
202 /*FALLTHROUGH*/
203 case R_TILEGX_IMM16_X1_HW1:
204 value >>= 16;
205 /*FALLTHROUGH*/
206 case R_TILEGX_IMM16_X1_HW0:
207 MUNGE(create_Imm16_X1);
208 break;
209 case R_TILEGX_JUMPOFF_X1:
210 value -= (unsigned long) location; /* pc-relative */
211 value = (long) value >> 3; /* count by instrs */
212 if (!validate_jumpoff(value)) {
213 pr_warning("module %s: Out of range jump to"
214 " %#llx at %#llx (%p)\n", me->name,
215 sym->st_value + rel[i].r_addend,
216 rel[i].r_offset, location);
217 return -ENOEXEC;
218 }
219 MUNGE(create_JumpOff_X1);
220 break;
221#endif
222
223#undef MUNGE
224
225 default:
226 pr_err("module %s: Unknown relocation: %d\n",
227 me->name, (int) ELF_R_TYPE(rel[i].r_info));
228 return -ENOEXEC;
229 }
230 }
231 return 0;
232}
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/pgtable.h>
24#include <asm/homecache.h>
25#include <arch/opcode.h>
26
27#ifdef MODULE_DEBUG
28#define DEBUGP printk
29#else
30#define DEBUGP(fmt...)
31#endif
32
33/*
34 * Allocate some address space in the range MEM_MODULE_START to
35 * MEM_MODULE_END and populate it with memory.
36 */
37void *module_alloc(unsigned long size)
38{
39 struct page **pages;
40 pgprot_t prot_rwx = __pgprot(_PAGE_KERNEL | _PAGE_KERNEL_EXEC);
41 struct vm_struct *area;
42 int i = 0;
43 int npages;
44
45 npages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
46 pages = kmalloc_array(npages, sizeof(*pages), GFP_KERNEL);
47 if (pages == NULL)
48 return NULL;
49 for (; i < npages; ++i) {
50 pages[i] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
51 if (!pages[i])
52 goto free_pages;
53 }
54
55 area = __get_vm_area(size, VM_ALLOC, MEM_MODULE_START, MEM_MODULE_END);
56 if (!area)
57 goto free_pages;
58 area->nr_pages = npages;
59 area->pages = pages;
60
61 if (map_vm_area(area, prot_rwx, pages)) {
62 vunmap(area->addr);
63 goto free_pages;
64 }
65
66 return area->addr;
67 free_pages:
68 while (--i >= 0)
69 __free_page(pages[i]);
70 kfree(pages);
71 return NULL;
72}
73
74
75/* Free memory returned from module_alloc */
76void module_memfree(void *module_region)
77{
78 vfree(module_region);
79
80 /* Globally flush the L1 icache. */
81 flush_remote(0, HV_FLUSH_EVICT_L1I, cpu_online_mask,
82 0, 0, 0, NULL, NULL, 0);
83
84 /*
85 * FIXME: Add module_arch_freeing_init to trim exception
86 * table entries.
87 */
88}
89
90#ifdef __tilegx__
91/*
92 * Validate that the high 16 bits of "value" is just the sign-extension of
93 * the low 48 bits.
94 */
95static int validate_hw2_last(long value, struct module *me)
96{
97 if (((value << 16) >> 16) != value) {
98 pr_warn("module %s: Out of range HW2_LAST value %#lx\n",
99 me->name, value);
100 return 0;
101 }
102 return 1;
103}
104
105/*
106 * Validate that "value" isn't too big to hold in a JumpOff relocation.
107 */
108static int validate_jumpoff(long value)
109{
110 /* Determine size of jump offset. */
111 int shift = __builtin_clzl(get_JumpOff_X1(create_JumpOff_X1(-1)));
112
113 /* Check to see if it fits into the relocation slot. */
114 long f = get_JumpOff_X1(create_JumpOff_X1(value));
115 f = (f << shift) >> shift;
116
117 return f == value;
118}
119#endif
120
121int apply_relocate_add(Elf_Shdr *sechdrs,
122 const char *strtab,
123 unsigned int symindex,
124 unsigned int relsec,
125 struct module *me)
126{
127 unsigned int i;
128 Elf_Rela *rel = (void *)sechdrs[relsec].sh_addr;
129 Elf_Sym *sym;
130 u64 *location;
131 unsigned long value;
132
133 DEBUGP("Applying relocate section %u to %u\n", relsec,
134 sechdrs[relsec].sh_info);
135 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
136 /* This is where to make the change */
137 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
138 + rel[i].r_offset;
139 /*
140 * This is the symbol it is referring to.
141 * Note that all undefined symbols have been resolved.
142 */
143 sym = (Elf_Sym *)sechdrs[symindex].sh_addr
144 + ELF_R_SYM(rel[i].r_info);
145 value = sym->st_value + rel[i].r_addend;
146
147 switch (ELF_R_TYPE(rel[i].r_info)) {
148
149#ifdef __LITTLE_ENDIAN
150# define MUNGE(func) \
151 (*location = ((*location & ~func(-1)) | func(value)))
152#else
153/*
154 * Instructions are always little-endian, so when we read them as data,
155 * we have to swap them around before and after modifying them.
156 */
157# define MUNGE(func) \
158 (*location = swab64((swab64(*location) & ~func(-1)) | func(value)))
159#endif
160
161#ifndef __tilegx__
162 case R_TILE_32:
163 *(uint32_t *)location = value;
164 break;
165 case R_TILE_IMM16_X0_HA:
166 value = (value + 0x8000) >> 16;
167 /*FALLTHROUGH*/
168 case R_TILE_IMM16_X0_LO:
169 MUNGE(create_Imm16_X0);
170 break;
171 case R_TILE_IMM16_X1_HA:
172 value = (value + 0x8000) >> 16;
173 /*FALLTHROUGH*/
174 case R_TILE_IMM16_X1_LO:
175 MUNGE(create_Imm16_X1);
176 break;
177 case R_TILE_JOFFLONG_X1:
178 value -= (unsigned long) location; /* pc-relative */
179 value = (long) value >> 3; /* count by instrs */
180 MUNGE(create_JOffLong_X1);
181 break;
182#else
183 case R_TILEGX_64:
184 *location = value;
185 break;
186 case R_TILEGX_IMM16_X0_HW2_LAST:
187 if (!validate_hw2_last(value, me))
188 return -ENOEXEC;
189 value >>= 16;
190 /*FALLTHROUGH*/
191 case R_TILEGX_IMM16_X0_HW1:
192 value >>= 16;
193 /*FALLTHROUGH*/
194 case R_TILEGX_IMM16_X0_HW0:
195 MUNGE(create_Imm16_X0);
196 break;
197 case R_TILEGX_IMM16_X1_HW2_LAST:
198 if (!validate_hw2_last(value, me))
199 return -ENOEXEC;
200 value >>= 16;
201 /*FALLTHROUGH*/
202 case R_TILEGX_IMM16_X1_HW1:
203 value >>= 16;
204 /*FALLTHROUGH*/
205 case R_TILEGX_IMM16_X1_HW0:
206 MUNGE(create_Imm16_X1);
207 break;
208 case R_TILEGX_JUMPOFF_X1:
209 value -= (unsigned long) location; /* pc-relative */
210 value = (long) value >> 3; /* count by instrs */
211 if (!validate_jumpoff(value)) {
212 pr_warn("module %s: Out of range jump to %#llx at %#llx (%p)\n",
213 me->name,
214 sym->st_value + rel[i].r_addend,
215 rel[i].r_offset, location);
216 return -ENOEXEC;
217 }
218 MUNGE(create_JumpOff_X1);
219 break;
220#endif
221
222#undef MUNGE
223
224 default:
225 pr_err("module %s: Unknown relocation: %d\n",
226 me->name, (int) ELF_R_TYPE(rel[i].r_info));
227 return -ENOEXEC;
228 }
229 }
230 return 0;
231}