]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/compat/linux/linux_vdso.c
MFC r283407:
[FreeBSD/stable/10.git] / sys / compat / linux / linux_vdso.c
1 /*-
2  * Copyright (c) 2013 Dmitry Chagin
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include "opt_compat.h"
31
32 #define __ELF_WORD_SIZE 32
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/elf.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/rwlock.h>
40 #include <sys/queue.h>
41 #include <sys/sysent.h>
42
43 #include <vm/vm.h>
44 #include <vm/vm_param.h>
45 #include <vm/pmap.h>
46 #include <vm/vm_extern.h>
47 #include <vm/vm_kern.h>
48 #include <vm/vm_map.h>
49 #include <vm/vm_object.h>
50 #include <vm/vm_page.h>
51 #include <vm/vm_pager.h>
52
53 #include <compat/linux/linux_vdso.h>
54
55 SLIST_HEAD(, linux_vdso_sym) __elfN(linux_vdso_syms) =
56     SLIST_HEAD_INITIALIZER(__elfN(linux_vdso_syms));
57
58 static int __elfN(symtabindex);
59 static int __elfN(symstrindex);
60
61 static void
62 __elfN(linux_vdso_lookup)(Elf_Ehdr *, struct linux_vdso_sym *);
63
64
65 void
66 __elfN(linux_vdso_sym_init)(struct linux_vdso_sym *s)
67 {
68
69         SLIST_INSERT_HEAD(&__elfN(linux_vdso_syms), s, sym);
70 }
71
72 vm_object_t
73 __elfN(linux_shared_page_init)(char **mapping)
74 {
75         vm_page_t m;
76         vm_object_t obj;
77         vm_offset_t addr;
78
79         obj = vm_pager_allocate(OBJT_PHYS, 0, PAGE_SIZE,
80             VM_PROT_DEFAULT, 0, NULL);
81         VM_OBJECT_WLOCK(obj);
82         m = vm_page_grab(obj, 0, VM_ALLOC_NOBUSY | VM_ALLOC_ZERO);
83         m->valid = VM_PAGE_BITS_ALL;
84         VM_OBJECT_WUNLOCK(obj);
85         addr = kva_alloc(PAGE_SIZE);
86         pmap_qenter(addr, &m, 1);
87         *mapping = (char *)addr;
88         return (obj);
89 }
90
91 void
92 __elfN(linux_shared_page_fini)(vm_object_t obj)
93 {
94
95         vm_object_deallocate(obj);
96 }
97
98 void
99 __elfN(linux_vdso_fixup)(struct sysentvec *sv)
100 {
101         Elf_Ehdr *ehdr;
102         Elf_Shdr *shdr;
103         int i;
104
105         ehdr = (Elf_Ehdr *) sv->sv_sigcode;
106
107         if (!IS_ELF(*ehdr) ||
108             ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
109             ehdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
110             ehdr->e_ident[EI_VERSION] != EV_CURRENT ||
111             ehdr->e_shoff == 0 ||
112             ehdr->e_shentsize != sizeof(Elf_Shdr))
113                 panic("Linux invalid vdso header.\n");
114
115         if (ehdr->e_type != ET_DYN)
116                 panic("Linux invalid vdso header.\n");
117
118         shdr = (Elf_Shdr *) ((caddr_t)ehdr + ehdr->e_shoff);
119
120         __elfN(symtabindex) = -1;
121         __elfN(symstrindex) = -1;
122         for (i = 0; i < ehdr->e_shnum; i++) {
123                 if (shdr[i].sh_size == 0)
124                         continue;
125                 if (shdr[i].sh_type == SHT_DYNSYM) {
126                         __elfN(symtabindex) = i;
127                         __elfN(symstrindex) = shdr[i].sh_link;
128                 }
129         }
130
131         if (__elfN(symtabindex) == -1 || __elfN(symstrindex) == -1)
132                 panic("Linux invalid vdso header.\n");
133
134         ehdr->e_ident[EI_OSABI] = ELFOSABI_LINUX;
135 }
136
137 void
138 __elfN(linux_vdso_reloc)(struct sysentvec *sv, int vdso_adjust)
139 {
140         struct linux_vdso_sym *lsym;
141         Elf_Ehdr *ehdr;
142         Elf_Phdr *phdr;
143         Elf_Shdr *shdr;
144         Elf_Dyn *dyn;
145         Elf_Sym *sym;
146         int i, symcnt;
147
148         ehdr = (Elf_Ehdr *) sv->sv_sigcode;
149
150         /* Adjust our so relative to the sigcode_base */
151         if (vdso_adjust != 0) {
152                 ehdr->e_entry += vdso_adjust;
153                 phdr = (Elf_Phdr *)((caddr_t)ehdr + ehdr->e_phoff);
154
155                 /* phdrs */
156                 for (i = 0; i < ehdr->e_phnum; i++) {
157                         phdr[i].p_vaddr += vdso_adjust;
158                         if (phdr[i].p_type != PT_DYNAMIC)
159                                 continue;
160                         dyn = (Elf_Dyn *)((caddr_t)ehdr + phdr[i].p_offset);
161                         for(; dyn->d_tag != DT_NULL; dyn++) {
162                                 switch (dyn->d_tag) {
163                                 case DT_PLTGOT:
164                                 case DT_HASH:
165                                 case DT_STRTAB:
166                                 case DT_SYMTAB:
167                                 case DT_RELA:
168                                 case DT_INIT:
169                                 case DT_FINI:
170                                 case DT_REL:
171                                 case DT_DEBUG:
172                                 case DT_JMPREL:
173                                 case DT_VERSYM:
174                                 case DT_VERDEF:
175                                 case DT_VERNEED:
176                                 case DT_ADDRRNGLO ... DT_ADDRRNGHI:
177                                         dyn->d_un.d_ptr += vdso_adjust;
178                                         break;
179                                 case DT_ENCODING ... DT_LOOS-1:
180                                 case DT_LOOS ... DT_HIOS:
181                                         if (dyn->d_tag >= DT_ENCODING &&
182                                             (dyn->d_tag & 1) == 0)
183                                                 dyn->d_un.d_ptr += vdso_adjust;
184                                         break;
185                                 default:
186                                         break;
187                                 }
188                         }
189                 }
190
191                 /* sections */
192                 shdr = (Elf_Shdr *)((caddr_t)ehdr + ehdr->e_shoff);
193                 for(i = 0; i < ehdr->e_shnum; i++) {
194                         if (!(shdr[i].sh_flags & SHF_ALLOC))
195                                 continue;
196                         shdr[i].sh_addr += vdso_adjust;
197                         if (shdr[i].sh_type != SHT_SYMTAB &&
198                             shdr[i].sh_type != SHT_DYNSYM)
199                                 continue;
200
201                         sym = (Elf_Sym *)((caddr_t)ehdr + shdr[i].sh_offset);
202                         symcnt = shdr[i].sh_size / sizeof(*sym);
203
204                         for(i = 0; i < symcnt; i++, sym++) {
205                                 if (sym->st_shndx == SHN_UNDEF ||
206                                     sym->st_shndx == SHN_ABS)
207                                         continue;
208                                 sym->st_value += vdso_adjust;
209                         }
210                 }
211         }
212
213         SLIST_FOREACH(lsym, &__elfN(linux_vdso_syms), sym)
214                 __elfN(linux_vdso_lookup)(ehdr, lsym);
215 }
216
217 static void
218 __elfN(linux_vdso_lookup)(Elf_Ehdr *ehdr, struct linux_vdso_sym *vsym)
219 {
220         vm_offset_t strtab, symname;
221         uint32_t symcnt;
222         Elf_Shdr *shdr;
223         int i;
224
225         shdr = (Elf_Shdr *) ((caddr_t)ehdr + ehdr->e_shoff);
226
227         strtab = (vm_offset_t)((caddr_t)ehdr +
228             shdr[__elfN(symstrindex)].sh_offset);
229         Elf_Sym *sym = (Elf_Sym *)((caddr_t)ehdr +
230             shdr[__elfN(symtabindex)].sh_offset);
231         symcnt = shdr[__elfN(symtabindex)].sh_size / sizeof(*sym);
232
233         for (i = 0; i < symcnt; ++i, ++sym) {
234                 symname = strtab + sym->st_name;
235                 if (strncmp(vsym->symname, (char *)symname, vsym->size) == 0) {
236                         *vsym->ptr = (uintptr_t)sym->st_value;
237                         break;
238                 }
239         }
240 }