2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
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.
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
14 * Based on i386 version, copyright (C) 2001 Rusty Russell.
17 #include <linux/moduleloader.h>
18 #include <linux/elf.h>
19 #include <linux/vmalloc.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>
28 # define Elf_Rela Elf64_Rela
29 # define ELF_R_SYM ELF64_R_SYM
30 # define ELF_R_TYPE ELF64_R_TYPE
32 # define Elf_Rela Elf32_Rela
33 # define ELF_R_SYM ELF32_R_SYM
34 # define ELF_R_TYPE ELF32_R_TYPE
40 #define DEBUGP(fmt...)
44 * Allocate some address space in the range MEM_MODULE_START to
45 * MEM_MODULE_END and populate it with memory.
47 void *module_alloc(unsigned long size
)
50 pgprot_t prot_rwx
= __pgprot(_PAGE_KERNEL
| _PAGE_KERNEL_EXEC
);
51 struct vm_struct
*area
;
57 npages
= (size
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
58 pages
= kmalloc(npages
* sizeof(struct page
*), GFP_KERNEL
);
61 for (; i
< npages
; ++i
) {
62 pages
[i
] = alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
67 area
= __get_vm_area(size
, VM_ALLOC
, MEM_MODULE_START
, MEM_MODULE_END
);
70 area
->nr_pages
= npages
;
73 if (map_vm_area(area
, prot_rwx
, &pages
)) {
82 __free_page(pages
[i
]);
88 /* Free memory returned from module_alloc */
89 void module_free(struct module
*mod
, void *module_region
)
93 /* Globally flush the L1 icache. */
94 flush_remote(0, HV_FLUSH_EVICT_L1I
, cpu_online_mask
,
95 0, 0, 0, NULL
, NULL
, 0);
98 * FIXME: If module_region == mod->module_init, trim exception
105 * Validate that the high 16 bits of "value" is just the sign-extension of
108 static int validate_hw2_last(long value
, struct module
*me
)
110 if (((value
<< 16) >> 16) != value
) {
111 pr_warning("module %s: Out of range HW2_LAST value %#lx\n",
119 * Validate that "value" isn't too big to hold in a JumpOff relocation.
121 static int validate_jumpoff(long value
)
123 /* Determine size of jump offset. */
124 int shift
= __builtin_clzl(get_JumpOff_X1(create_JumpOff_X1(-1)));
126 /* Check to see if it fits into the relocation slot. */
127 long f
= get_JumpOff_X1(create_JumpOff_X1(value
));
128 f
= (f
<< shift
) >> shift
;
134 int apply_relocate_add(Elf_Shdr
*sechdrs
,
136 unsigned int symindex
,
141 Elf_Rela
*rel
= (void *)sechdrs
[relsec
].sh_addr
;
146 DEBUGP("Applying relocate section %u to %u\n", relsec
,
147 sechdrs
[relsec
].sh_info
);
148 for (i
= 0; i
< sechdrs
[relsec
].sh_size
/ sizeof(*rel
); i
++) {
149 /* This is where to make the change */
150 location
= (void *)sechdrs
[sechdrs
[relsec
].sh_info
].sh_addr
153 * This is the symbol it is referring to.
154 * Note that all undefined symbols have been resolved.
156 sym
= (Elf_Sym
*)sechdrs
[symindex
].sh_addr
157 + ELF_R_SYM(rel
[i
].r_info
);
158 value
= sym
->st_value
+ rel
[i
].r_addend
;
160 switch (ELF_R_TYPE(rel
[i
].r_info
)) {
162 #ifdef __LITTLE_ENDIAN
163 # define MUNGE(func) \
164 (*location = ((*location & ~func(-1)) | func(value)))
167 * Instructions are always little-endian, so when we read them as data,
168 * we have to swap them around before and after modifying them.
170 # define MUNGE(func) \
171 (*location = swab64((swab64(*location) & ~func(-1)) | func(value)))
176 *(uint32_t *)location
= value
;
178 case R_TILE_IMM16_X0_HA
:
179 value
= (value
+ 0x8000) >> 16;
181 case R_TILE_IMM16_X0_LO
:
182 MUNGE(create_Imm16_X0
);
184 case R_TILE_IMM16_X1_HA
:
185 value
= (value
+ 0x8000) >> 16;
187 case R_TILE_IMM16_X1_LO
:
188 MUNGE(create_Imm16_X1
);
190 case R_TILE_JOFFLONG_X1
:
191 value
-= (unsigned long) location
; /* pc-relative */
192 value
= (long) value
>> 3; /* count by instrs */
193 MUNGE(create_JOffLong_X1
);
199 case R_TILEGX_IMM16_X0_HW2_LAST
:
200 if (!validate_hw2_last(value
, me
))
204 case R_TILEGX_IMM16_X0_HW1
:
207 case R_TILEGX_IMM16_X0_HW0
:
208 MUNGE(create_Imm16_X0
);
210 case R_TILEGX_IMM16_X1_HW2_LAST
:
211 if (!validate_hw2_last(value
, me
))
215 case R_TILEGX_IMM16_X1_HW1
:
218 case R_TILEGX_IMM16_X1_HW0
:
219 MUNGE(create_Imm16_X1
);
221 case R_TILEGX_JUMPOFF_X1
:
222 value
-= (unsigned long) location
; /* pc-relative */
223 value
= (long) value
>> 3; /* count by instrs */
224 if (!validate_jumpoff(value
)) {
225 pr_warning("module %s: Out of range jump to"
226 " %#llx at %#llx (%p)\n", me
->name
,
227 sym
->st_value
+ rel
[i
].r_addend
,
228 rel
[i
].r_offset
, location
);
231 MUNGE(create_JumpOff_X1
);
238 pr_err("module %s: Unknown relocation: %d\n",
239 me
->name
, (int) ELF_R_TYPE(rel
[i
].r_info
));