Initial commit
[wrt350n-kernel.git] / arch / sparc64 / kernel / module.c
blob158484bf59993dd915b6190d0ac31eeac7938404
1 /* Kernel module help for sparc64.
3 * Copyright (C) 2001 Rusty Russell.
4 * Copyright (C) 2002 David S. Miller.
5 */
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>
12 #include <linux/string.h>
13 #include <linux/slab.h>
14 #include <linux/mm.h>
16 #include <asm/processor.h>
17 #include <asm/spitfire.h>
19 static void *module_map(unsigned long size)
21 struct vm_struct *area;
23 size = PAGE_ALIGN(size);
24 if (!size || size > MODULES_LEN)
25 return NULL;
27 area = __get_vm_area(size, VM_ALLOC, MODULES_VADDR, MODULES_END);
28 if (!area)
29 return NULL;
31 return __vmalloc_area(area, GFP_KERNEL, PAGE_KERNEL);
34 void *module_alloc(unsigned long size)
36 void *ret;
38 /* We handle the zero case fine, unlike vmalloc */
39 if (size == 0)
40 return NULL;
42 ret = module_map(size);
43 if (!ret)
44 ret = ERR_PTR(-ENOMEM);
45 else
46 memset(ret, 0, size);
48 return ret;
51 /* Free memory returned from module_core_alloc/module_init_alloc */
52 void module_free(struct module *mod, void *module_region)
54 vfree(module_region);
55 /* FIXME: If module_region == mod->init_region, trim exception
56 table entries. */
59 /* Make generic code ignore STT_REGISTER dummy undefined symbols. */
60 int module_frob_arch_sections(Elf_Ehdr *hdr,
61 Elf_Shdr *sechdrs,
62 char *secstrings,
63 struct module *mod)
65 unsigned int symidx;
66 Elf64_Sym *sym;
67 const char *strtab;
68 int i;
70 for (symidx = 0; sechdrs[symidx].sh_type != SHT_SYMTAB; symidx++) {
71 if (symidx == hdr->e_shnum-1) {
72 printk("%s: no symtab found.\n", mod->name);
73 return -ENOEXEC;
76 sym = (Elf64_Sym *)sechdrs[symidx].sh_addr;
77 strtab = (char *)sechdrs[sechdrs[symidx].sh_link].sh_addr;
79 for (i = 1; i < sechdrs[symidx].sh_size / sizeof(Elf_Sym); i++) {
80 if (sym[i].st_shndx == SHN_UNDEF &&
81 ELF64_ST_TYPE(sym[i].st_info) == STT_REGISTER)
82 sym[i].st_shndx = SHN_ABS;
84 return 0;
87 int apply_relocate(Elf64_Shdr *sechdrs,
88 const char *strtab,
89 unsigned int symindex,
90 unsigned int relsec,
91 struct module *me)
93 printk(KERN_ERR "module %s: non-ADD RELOCATION unsupported\n",
94 me->name);
95 return -ENOEXEC;
98 int apply_relocate_add(Elf64_Shdr *sechdrs,
99 const char *strtab,
100 unsigned int symindex,
101 unsigned int relsec,
102 struct module *me)
104 unsigned int i;
105 Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
106 Elf64_Sym *sym;
107 u8 *location;
108 u32 *loc32;
110 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
111 Elf64_Addr v;
113 /* This is where to make the change */
114 location = (u8 *)sechdrs[sechdrs[relsec].sh_info].sh_addr
115 + rel[i].r_offset;
116 loc32 = (u32 *) location;
118 BUG_ON(((u64)location >> (u64)32) != (u64)0);
120 /* This is the symbol it is referring to. Note that all
121 undefined symbols have been resolved. */
122 sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
123 + ELF64_R_SYM(rel[i].r_info);
124 v = sym->st_value + rel[i].r_addend;
126 switch (ELF64_R_TYPE(rel[i].r_info) & 0xff) {
127 case R_SPARC_64:
128 location[0] = v >> 56;
129 location[1] = v >> 48;
130 location[2] = v >> 40;
131 location[3] = v >> 32;
132 location[4] = v >> 24;
133 location[5] = v >> 16;
134 location[6] = v >> 8;
135 location[7] = v >> 0;
136 break;
138 case R_SPARC_32:
139 location[0] = v >> 24;
140 location[1] = v >> 16;
141 location[2] = v >> 8;
142 location[3] = v >> 0;
143 break;
145 case R_SPARC_DISP32:
146 v -= (Elf64_Addr) location;
147 *loc32 = v;
148 break;
150 case R_SPARC_WDISP30:
151 v -= (Elf64_Addr) location;
152 *loc32 = (*loc32 & ~0x3fffffff) |
153 ((v >> 2) & 0x3fffffff);
154 break;
156 case R_SPARC_WDISP22:
157 v -= (Elf64_Addr) location;
158 *loc32 = (*loc32 & ~0x3fffff) |
159 ((v >> 2) & 0x3fffff);
160 break;
162 case R_SPARC_WDISP19:
163 v -= (Elf64_Addr) location;
164 *loc32 = (*loc32 & ~0x7ffff) |
165 ((v >> 2) & 0x7ffff);
166 break;
168 case R_SPARC_LO10:
169 *loc32 = (*loc32 & ~0x3ff) | (v & 0x3ff);
170 break;
172 case R_SPARC_HI22:
173 *loc32 = (*loc32 & ~0x3fffff) |
174 ((v >> 10) & 0x3fffff);
175 break;
177 case R_SPARC_OLO10:
178 *loc32 = (*loc32 & ~0x1fff) |
179 (((v & 0x3ff) +
180 (ELF64_R_TYPE(rel[i].r_info) >> 8))
181 & 0x1fff);
182 break;
184 default:
185 printk(KERN_ERR "module %s: Unknown relocation: %x\n",
186 me->name,
187 (int) (ELF64_R_TYPE(rel[i].r_info) & 0xff));
188 return -ENOEXEC;
191 return 0;
194 int module_finalize(const Elf_Ehdr *hdr,
195 const Elf_Shdr *sechdrs,
196 struct module *me)
198 /* Cheetah's I-cache is fully coherent. */
199 if (tlb_type == spitfire) {
200 unsigned long va;
202 flushw_all();
203 for (va = 0; va < (PAGE_SIZE << 1); va += 32)
204 spitfire_put_icache_tag(va, 0x0);
205 __asm__ __volatile__("flush %g6");
208 return 0;
211 void module_arch_cleanup(struct module *mod)