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