Merge tag v3.10.42 into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.git] / arch / x86 / vdso / vdso32-setup.c
1 /*
2  * (C) Copyright 2002 Linus Torvalds
3  * Portions based on the vdso-randomization code from exec-shield:
4  * Copyright(C) 2005-2006, Red Hat, Inc., Ingo Molnar
5  *
6  * This file contains the needed initializations to support sysenter.
7  */
8
9 #include <linux/init.h>
10 #include <linux/smp.h>
11 #include <linux/thread_info.h>
12 #include <linux/sched.h>
13 #include <linux/gfp.h>
14 #include <linux/string.h>
15 #include <linux/elf.h>
16 #include <linux/mm.h>
17 #include <linux/err.h>
18 #include <linux/module.h>
19
20 #include <asm/cpufeature.h>
21 #include <asm/msr.h>
22 #include <asm/pgtable.h>
23 #include <asm/unistd.h>
24 #include <asm/elf.h>
25 #include <asm/tlbflush.h>
26 #include <asm/vdso.h>
27 #include <asm/proto.h>
28
29 enum {
30         VDSO_DISABLED = 0,
31         VDSO_ENABLED = 1,
32         VDSO_COMPAT = 2,
33 };
34
35 #ifdef CONFIG_COMPAT_VDSO
36 #define VDSO_DEFAULT    VDSO_COMPAT
37 #else
38 #define VDSO_DEFAULT    VDSO_ENABLED
39 #endif
40
41 #ifdef CONFIG_X86_64
42 #define vdso_enabled                    sysctl_vsyscall32
43 #define arch_setup_additional_pages     syscall32_setup_pages
44 extern int sysctl_ldt16;
45 #endif
46
47 /*
48  * This is the difference between the prelinked addresses in the vDSO images
49  * and the VDSO_HIGH_BASE address where CONFIG_COMPAT_VDSO places the vDSO
50  * in the user address space.
51  */
52 #define VDSO_ADDR_ADJUST        (VDSO_HIGH_BASE - (unsigned long)VDSO32_PRELINK)
53
54 /*
55  * Should the kernel map a VDSO page into processes and pass its
56  * address down to glibc upon exec()?
57  */
58 unsigned int __read_mostly vdso_enabled = VDSO_DEFAULT;
59
60 static int __init vdso_setup(char *s)
61 {
62         vdso_enabled = simple_strtoul(s, NULL, 0);
63
64         return 1;
65 }
66
67 /*
68  * For consistency, the argument vdso32=[012] affects the 32-bit vDSO
69  * behavior on both 64-bit and 32-bit kernels.
70  * On 32-bit kernels, vdso=[012] means the same thing.
71  */
72 __setup("vdso32=", vdso_setup);
73
74 #ifdef CONFIG_X86_32
75 __setup_param("vdso=", vdso32_setup, vdso_setup, 0);
76
77 EXPORT_SYMBOL_GPL(vdso_enabled);
78 #endif
79
80 static __init void reloc_symtab(Elf32_Ehdr *ehdr,
81                                 unsigned offset, unsigned size)
82 {
83         Elf32_Sym *sym = (void *)ehdr + offset;
84         unsigned nsym = size / sizeof(*sym);
85         unsigned i;
86
87         for(i = 0; i < nsym; i++, sym++) {
88                 if (sym->st_shndx == SHN_UNDEF ||
89                     sym->st_shndx == SHN_ABS)
90                         continue;  /* skip */
91
92                 if (sym->st_shndx > SHN_LORESERVE) {
93                         printk(KERN_INFO "VDSO: unexpected st_shndx %x\n",
94                                sym->st_shndx);
95                         continue;
96                 }
97
98                 switch(ELF_ST_TYPE(sym->st_info)) {
99                 case STT_OBJECT:
100                 case STT_FUNC:
101                 case STT_SECTION:
102                 case STT_FILE:
103                         sym->st_value += VDSO_ADDR_ADJUST;
104                 }
105         }
106 }
107
108 static __init void reloc_dyn(Elf32_Ehdr *ehdr, unsigned offset)
109 {
110         Elf32_Dyn *dyn = (void *)ehdr + offset;
111
112         for(; dyn->d_tag != DT_NULL; dyn++)
113                 switch(dyn->d_tag) {
114                 case DT_PLTGOT:
115                 case DT_HASH:
116                 case DT_STRTAB:
117                 case DT_SYMTAB:
118                 case DT_RELA:
119                 case DT_INIT:
120                 case DT_FINI:
121                 case DT_REL:
122                 case DT_DEBUG:
123                 case DT_JMPREL:
124                 case DT_VERSYM:
125                 case DT_VERDEF:
126                 case DT_VERNEED:
127                 case DT_ADDRRNGLO ... DT_ADDRRNGHI:
128                         /* definitely pointers needing relocation */
129                         dyn->d_un.d_ptr += VDSO_ADDR_ADJUST;
130                         break;
131
132                 case DT_ENCODING ... OLD_DT_LOOS-1:
133                 case DT_LOOS ... DT_HIOS-1:
134                         /* Tags above DT_ENCODING are pointers if
135                            they're even */
136                         if (dyn->d_tag >= DT_ENCODING &&
137                             (dyn->d_tag & 1) == 0)
138                                 dyn->d_un.d_ptr += VDSO_ADDR_ADJUST;
139                         break;
140
141                 case DT_VERDEFNUM:
142                 case DT_VERNEEDNUM:
143                 case DT_FLAGS_1:
144                 case DT_RELACOUNT:
145                 case DT_RELCOUNT:
146                 case DT_VALRNGLO ... DT_VALRNGHI:
147                         /* definitely not pointers */
148                         break;
149
150                 case OLD_DT_LOOS ... DT_LOOS-1:
151                 case DT_HIOS ... DT_VALRNGLO-1:
152                 default:
153                         if (dyn->d_tag > DT_ENCODING)
154                                 printk(KERN_INFO "VDSO: unexpected DT_tag %x\n",
155                                        dyn->d_tag);
156                         break;
157                 }
158 }
159
160 static __init void relocate_vdso(Elf32_Ehdr *ehdr)
161 {
162         Elf32_Phdr *phdr;
163         Elf32_Shdr *shdr;
164         int i;
165
166         BUG_ON(memcmp(ehdr->e_ident, ELFMAG, SELFMAG) != 0 ||
167                !elf_check_arch_ia32(ehdr) ||
168                ehdr->e_type != ET_DYN);
169
170         ehdr->e_entry += VDSO_ADDR_ADJUST;
171
172         /* rebase phdrs */
173         phdr = (void *)ehdr + ehdr->e_phoff;
174         for (i = 0; i < ehdr->e_phnum; i++) {
175                 phdr[i].p_vaddr += VDSO_ADDR_ADJUST;
176
177                 /* relocate dynamic stuff */
178                 if (phdr[i].p_type == PT_DYNAMIC)
179                         reloc_dyn(ehdr, phdr[i].p_offset);
180         }
181
182         /* rebase sections */
183         shdr = (void *)ehdr + ehdr->e_shoff;
184         for(i = 0; i < ehdr->e_shnum; i++) {
185                 if (!(shdr[i].sh_flags & SHF_ALLOC))
186                         continue;
187
188                 shdr[i].sh_addr += VDSO_ADDR_ADJUST;
189
190                 if (shdr[i].sh_type == SHT_SYMTAB ||
191                     shdr[i].sh_type == SHT_DYNSYM)
192                         reloc_symtab(ehdr, shdr[i].sh_offset,
193                                      shdr[i].sh_size);
194         }
195 }
196
197 static struct page *vdso32_pages[1];
198
199 #ifdef CONFIG_X86_64
200
201 #define vdso32_sysenter()       (boot_cpu_has(X86_FEATURE_SYSENTER32))
202 #define vdso32_syscall()        (boot_cpu_has(X86_FEATURE_SYSCALL32))
203
204 /* May not be __init: called during resume */
205 void syscall32_cpu_init(void)
206 {
207         /* Load these always in case some future AMD CPU supports
208            SYSENTER from compat mode too. */
209         wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
210         wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
211         wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)ia32_sysenter_target);
212
213         wrmsrl(MSR_CSTAR, ia32_cstar_target);
214 }
215
216 #define compat_uses_vma         1
217
218 static inline void map_compat_vdso(int map)
219 {
220 }
221
222 #else  /* CONFIG_X86_32 */
223
224 #define vdso32_sysenter()       (boot_cpu_has(X86_FEATURE_SEP))
225 #define vdso32_syscall()        (0)
226
227 void enable_sep_cpu(void)
228 {
229         int cpu = get_cpu();
230         struct tss_struct *tss = &per_cpu(init_tss, cpu);
231
232         if (!boot_cpu_has(X86_FEATURE_SEP)) {
233                 put_cpu();
234                 return;
235         }
236
237         tss->x86_tss.ss1 = __KERNEL_CS;
238         tss->x86_tss.sp1 = sizeof(struct tss_struct) + (unsigned long) tss;
239         wrmsr(MSR_IA32_SYSENTER_CS, __KERNEL_CS, 0);
240         wrmsr(MSR_IA32_SYSENTER_ESP, tss->x86_tss.sp1, 0);
241         wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long) ia32_sysenter_target, 0);
242         put_cpu();      
243 }
244
245 static struct vm_area_struct gate_vma;
246
247 static int __init gate_vma_init(void)
248 {
249         gate_vma.vm_mm = NULL;
250         gate_vma.vm_start = FIXADDR_USER_START;
251         gate_vma.vm_end = FIXADDR_USER_END;
252         gate_vma.vm_flags = VM_READ | VM_MAYREAD | VM_EXEC | VM_MAYEXEC;
253         gate_vma.vm_page_prot = __P101;
254
255         return 0;
256 }
257
258 #define compat_uses_vma         0
259
260 static void map_compat_vdso(int map)
261 {
262         static int vdso_mapped;
263
264         if (map == vdso_mapped)
265                 return;
266
267         vdso_mapped = map;
268
269         __set_fixmap(FIX_VDSO, page_to_pfn(vdso32_pages[0]) << PAGE_SHIFT,
270                      map ? PAGE_READONLY_EXEC : PAGE_NONE);
271
272         /* flush stray tlbs */
273         flush_tlb_all();
274 }
275
276 #endif  /* CONFIG_X86_64 */
277
278 int __init sysenter_setup(void)
279 {
280         void *syscall_page = (void *)get_zeroed_page(GFP_ATOMIC);
281         const void *vsyscall;
282         size_t vsyscall_len;
283
284         vdso32_pages[0] = virt_to_page(syscall_page);
285
286 #ifdef CONFIG_X86_32
287         gate_vma_init();
288 #endif
289
290         if (vdso32_syscall()) {
291                 vsyscall = &vdso32_syscall_start;
292                 vsyscall_len = &vdso32_syscall_end - &vdso32_syscall_start;
293         } else if (vdso32_sysenter()){
294                 vsyscall = &vdso32_sysenter_start;
295                 vsyscall_len = &vdso32_sysenter_end - &vdso32_sysenter_start;
296         } else {
297                 vsyscall = &vdso32_int80_start;
298                 vsyscall_len = &vdso32_int80_end - &vdso32_int80_start;
299         }
300
301         memcpy(syscall_page, vsyscall, vsyscall_len);
302         relocate_vdso(syscall_page);
303
304         return 0;
305 }
306
307 /* Setup a VMA at program startup for the vsyscall page */
308 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
309 {
310         struct mm_struct *mm = current->mm;
311         unsigned long addr;
312         int ret = 0;
313         bool compat;
314
315 #ifdef CONFIG_X86_X32_ABI
316         if (test_thread_flag(TIF_X32))
317                 return x32_setup_additional_pages(bprm, uses_interp);
318 #endif
319
320         if (vdso_enabled == VDSO_DISABLED)
321                 return 0;
322
323         down_write(&mm->mmap_sem);
324
325         /* Test compat mode once here, in case someone
326            changes it via sysctl */
327         compat = (vdso_enabled == VDSO_COMPAT);
328
329         map_compat_vdso(compat);
330
331         if (compat)
332                 addr = VDSO_HIGH_BASE;
333         else {
334                 addr = get_unmapped_area(NULL, 0, PAGE_SIZE, 0, 0);
335                 if (IS_ERR_VALUE(addr)) {
336                         ret = addr;
337                         goto up_fail;
338                 }
339         }
340
341         current->mm->context.vdso = (void *)addr;
342
343         if (compat_uses_vma || !compat) {
344                 /*
345                  * MAYWRITE to allow gdb to COW and set breakpoints
346                  */
347                 ret = install_special_mapping(mm, addr, PAGE_SIZE,
348                                               VM_READ|VM_EXEC|
349                                               VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
350                                               vdso32_pages);
351
352                 if (ret)
353                         goto up_fail;
354         }
355
356         current_thread_info()->sysenter_return =
357                 VDSO32_SYMBOL(addr, SYSENTER_RETURN);
358
359   up_fail:
360         if (ret)
361                 current->mm->context.vdso = NULL;
362
363         up_write(&mm->mmap_sem);
364
365         return ret;
366 }
367
368 #ifdef CONFIG_X86_64
369
370 subsys_initcall(sysenter_setup);
371
372 #ifdef CONFIG_SYSCTL
373 /* Register vsyscall32 into the ABI table */
374 #include <linux/sysctl.h>
375
376 static ctl_table abi_table2[] = {
377         {
378                 .procname       = "vsyscall32",
379                 .data           = &sysctl_vsyscall32,
380                 .maxlen         = sizeof(int),
381                 .mode           = 0644,
382                 .proc_handler   = proc_dointvec
383         },
384         {
385                 .procname       = "ldt16",
386                 .data           = &sysctl_ldt16,
387                 .maxlen         = sizeof(int),
388                 .mode           = 0644,
389                 .proc_handler   = proc_dointvec
390         },
391         {}
392 };
393
394 static ctl_table abi_root_table2[] = {
395         {
396                 .procname = "abi",
397                 .mode = 0555,
398                 .child = abi_table2
399         },
400         {}
401 };
402
403 static __init int ia32_binfmt_init(void)
404 {
405         register_sysctl_table(abi_root_table2);
406         return 0;
407 }
408 __initcall(ia32_binfmt_init);
409 #endif
410
411 #else  /* CONFIG_X86_32 */
412
413 const char *arch_vma_name(struct vm_area_struct *vma)
414 {
415         if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
416                 return "[vdso]";
417         return NULL;
418 }
419
420 struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
421 {
422         /*
423          * Check to see if the corresponding task was created in compat vdso
424          * mode.
425          */
426         if (mm && mm->context.vdso == (void *)VDSO_HIGH_BASE)
427                 return &gate_vma;
428         return NULL;
429 }
430
431 int in_gate_area(struct mm_struct *mm, unsigned long addr)
432 {
433         const struct vm_area_struct *vma = get_gate_vma(mm);
434
435         return vma && addr >= vma->vm_start && addr < vma->vm_end;
436 }
437
438 int in_gate_area_no_mm(unsigned long addr)
439 {
440         return 0;
441 }
442
443 #endif  /* CONFIG_X86_64 */