Merge with Linux 2.5.48.
[linux-2.6/linux-mips.git] / arch / sparc64 / kernel / module.c
blobabff5ca9c68be915c2578c35c6a53b40d8566817
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/vmalloc.h>
15 #include <linux/mm.h>
17 static struct vm_struct * modvmlist = NULL;
19 static void module_unmap(void * addr)
21 struct vm_struct **p, *tmp;
22 int i;
24 if (!addr)
25 return;
26 if ((PAGE_SIZE-1) & (unsigned long) addr) {
27 printk("Trying to unmap module with bad address (%p)\n", addr);
28 return;
31 for (p = &modvmlist ; (tmp = *p) ; p = &tmp->next) {
32 if (tmp->addr == addr) {
33 *p = tmp->next;
34 goto found;
37 printk("Trying to unmap nonexistent module vm area (%p)\n", addr);
38 return;
40 found:
41 unmap_vm_area(tmp);
43 for (i = 0; i < tmp->nr_pages; i++) {
44 if (unlikely(!tmp->pages[i]))
45 BUG();
46 __free_page(tmp->pages[i]);
49 kfree(tmp->pages);
50 kfree(tmp);
54 static void *module_map(unsigned long size)
56 struct vm_struct **p, *tmp, *area;
57 struct page **pages;
58 void * addr;
59 unsigned int nr_pages, array_size, i;
61 size = PAGE_ALIGN(size);
62 if (!size || size > MODULES_LEN)
63 return NULL;
65 addr = (void *) MODULES_VADDR;
66 for (p = &modvmlist; (tmp = *p) ; p = &tmp->next) {
67 if (size + (unsigned long) addr < (unsigned long) tmp->addr)
68 break;
69 addr = (void *) (tmp->size + (unsigned long) tmp->addr);
71 if ((unsigned long) addr + size >= MODULES_END)
72 return NULL;
74 area = (struct vm_struct *) kmalloc(sizeof(*area), GFP_KERNEL);
75 if (!area)
76 return NULL;
77 area->size = size + PAGE_SIZE;
78 area->addr = addr;
79 area->next = *p;
80 area->pages = NULL;
81 area->nr_pages = 0;
82 area->phys_addr = 0;
83 *p = area;
85 nr_pages = size >> PAGE_SHIFT;
86 array_size = (nr_pages * sizeof(struct page *));
88 area->nr_pages = nr_pages;
89 area->pages = pages = kmalloc(array_size, GFP_KERNEL);
90 if (!area->pages)
91 goto fail;
93 memset(area->pages, 0, array_size);
95 for (i = 0; i < area->nr_pages; i++) {
96 area->pages[i] = alloc_page(GFP_KERNEL);
97 if (unlikely(!area->pages[i]))
98 goto fail;
101 if (map_vm_area(area, PAGE_KERNEL, &pages)) {
102 unmap_vm_area(area);
103 goto fail;
106 return area->addr;
108 fail:
109 if (area->pages) {
110 for (i = 0; i < area->nr_pages; i++) {
111 if (area->pages[i])
112 __free_page(area->pages[i]);
114 kfree(area->pages);
116 kfree(area);
118 return NULL;
121 static void *alloc_and_zero(unsigned long size)
123 void *ret;
125 /* We handle the zero case fine, unlike vmalloc */
126 if (size == 0)
127 return NULL;
129 ret = module_map(size);
130 if (!ret)
131 ret = ERR_PTR(-ENOMEM);
132 else
133 memset(ret, 0, size);
135 return ret;
138 /* Free memory returned from module_core_alloc/module_init_alloc */
139 void module_free(struct module *mod, void *module_region)
141 module_unmap(module_region);
142 /* FIXME: If module_region == mod->init_region, trim exception
143 table entries. */
146 void *module_core_alloc(const Elf64_Ehdr *hdr,
147 const Elf64_Shdr *sechdrs,
148 const char *secstrings,
149 struct module *module)
151 return alloc_and_zero(module->core_size);
154 void *module_init_alloc(const Elf64_Ehdr *hdr,
155 const Elf64_Shdr *sechdrs,
156 const char *secstrings,
157 struct module *module)
159 return alloc_and_zero(module->init_size);
162 int apply_relocate(Elf64_Shdr *sechdrs,
163 const char *strtab,
164 unsigned int symindex,
165 unsigned int relsec,
166 struct module *me)
168 printk(KERN_ERR "module %s: non-ADD RELOCATION unsupported\n",
169 me->name);
170 return -ENOEXEC;
173 int apply_relocate_add(Elf64_Shdr *sechdrs,
174 const char *strtab,
175 unsigned int symindex,
176 unsigned int relsec,
177 struct module *me)
179 unsigned int i;
180 Elf64_Rela *rel = (void *)sechdrs[relsec].sh_offset;
181 Elf64_Sym *sym;
182 u8 *location;
183 u32 *loc32;
185 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
186 Elf64_Addr v;
188 /* This is where to make the change */
189 location = (u8 *)sechdrs[sechdrs[relsec].sh_info].sh_offset
190 + rel[i].r_offset;
191 loc32 = (u32 *) location;
193 BUG_ON(((u64)location >> (u64)32) != (u64)0);
195 /* This is the symbol it is referring to */
196 sym = (Elf64_Sym *)sechdrs[symindex].sh_offset
197 + ELF64_R_SYM(rel[i].r_info);
198 if (!(v = sym->st_value)) {
199 printk(KERN_WARNING "%s: Unknown symbol %s\n",
200 me->name, strtab + sym->st_name);
201 return -ENOENT;
203 v += rel[i].r_addend;
205 switch (ELF64_R_TYPE(rel[i].r_info) & 0xff) {
206 case R_SPARC_64:
207 location[0] = v >> 56;
208 location[1] = v >> 48;
209 location[2] = v >> 40;
210 location[3] = v >> 32;
211 location[4] = v >> 24;
212 location[5] = v >> 16;
213 location[6] = v >> 8;
214 location[7] = v >> 0;
215 break;
217 case R_SPARC_32:
218 location[0] = v >> 24;
219 location[1] = v >> 16;
220 location[2] = v >> 8;
221 location[3] = v >> 0;
222 break;
224 case R_SPARC_WDISP30:
225 v -= (Elf64_Addr) location;
226 *loc32 = (*loc32 & ~0x3fffffff) |
227 ((v >> 2) & 0x3fffffff);
228 break;
230 case R_SPARC_WDISP22:
231 v -= (Elf64_Addr) location;
232 *loc32 = (*loc32 & ~0x3fffff) |
233 ((v >> 2) & 0x3fffff);
234 break;
236 case R_SPARC_LO10:
237 *loc32 = (*loc32 & ~0x3ff) | (v & 0x3ff);
238 break;
240 case R_SPARC_HI22:
241 *loc32 = (*loc32 & ~0x3fffff) |
242 ((v >> 10) & 0x3fffff);
243 break;
245 default:
246 printk(KERN_ERR "module %s: Unknown relocation: %x\n",
247 me->name,
248 (int) (ELF64_R_TYPE(rel[i].r_info) & 0xff));
249 return -ENOEXEC;
252 return 0;
255 int module_finalize(const Elf_Ehdr *hdr,
256 const Elf_Shdr *sechdrs,
257 struct module *me)
259 return 0;