]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - arch/x86/vdso/vdso32-setup.c
Merge remote-tracking branch 'spi/for-next'
[karo-tx-linux.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 #include <linux/slab.h>
20
21 #include <asm/cpufeature.h>
22 #include <asm/msr.h>
23 #include <asm/pgtable.h>
24 #include <asm/unistd.h>
25 #include <asm/elf.h>
26 #include <asm/tlbflush.h>
27 #include <asm/vdso.h>
28 #include <asm/proto.h>
29 #include <asm/fixmap.h>
30 #include <asm/hpet.h>
31 #include <asm/vvar.h>
32
33 #ifdef CONFIG_COMPAT_VDSO
34 #define VDSO_DEFAULT    0
35 #else
36 #define VDSO_DEFAULT    1
37 #endif
38
39 #ifdef CONFIG_X86_64
40 #define vdso_enabled                    sysctl_vsyscall32
41 #define arch_setup_additional_pages     syscall32_setup_pages
42 extern int sysctl_ldt16;
43 #endif
44
45 /*
46  * Should the kernel map a VDSO page into processes and pass its
47  * address down to glibc upon exec()?
48  */
49 unsigned int __read_mostly vdso_enabled = VDSO_DEFAULT;
50
51 static int __init vdso_setup(char *s)
52 {
53         vdso_enabled = simple_strtoul(s, NULL, 0);
54
55         if (vdso_enabled > 1)
56                 pr_warn("vdso32 values other than 0 and 1 are no longer allowed; vdso disabled\n");
57
58         return 1;
59 }
60
61 /*
62  * For consistency, the argument vdso32=[012] affects the 32-bit vDSO
63  * behavior on both 64-bit and 32-bit kernels.
64  * On 32-bit kernels, vdso=[012] means the same thing.
65  */
66 __setup("vdso32=", vdso_setup);
67
68 #ifdef CONFIG_X86_32
69 __setup_param("vdso=", vdso32_setup, vdso_setup, 0);
70
71 EXPORT_SYMBOL_GPL(vdso_enabled);
72 #endif
73
74 static struct page **vdso32_pages;
75 static unsigned vdso32_size;
76
77 #ifdef CONFIG_X86_64
78
79 #define vdso32_sysenter()       (boot_cpu_has(X86_FEATURE_SYSENTER32))
80 #define vdso32_syscall()        (boot_cpu_has(X86_FEATURE_SYSCALL32))
81
82 /* May not be __init: called during resume */
83 void syscall32_cpu_init(void)
84 {
85         /* Load these always in case some future AMD CPU supports
86            SYSENTER from compat mode too. */
87         wrmsrl_safe(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
88         wrmsrl_safe(MSR_IA32_SYSENTER_ESP, 0ULL);
89         wrmsrl_safe(MSR_IA32_SYSENTER_EIP, (u64)ia32_sysenter_target);
90
91         wrmsrl(MSR_CSTAR, ia32_cstar_target);
92 }
93
94 #else  /* CONFIG_X86_32 */
95
96 #define vdso32_sysenter()       (boot_cpu_has(X86_FEATURE_SEP))
97 #define vdso32_syscall()        (0)
98
99 void enable_sep_cpu(void)
100 {
101         int cpu = get_cpu();
102         struct tss_struct *tss = &per_cpu(init_tss, cpu);
103
104         if (!boot_cpu_has(X86_FEATURE_SEP)) {
105                 put_cpu();
106                 return;
107         }
108
109         tss->x86_tss.ss1 = __KERNEL_CS;
110         tss->x86_tss.sp1 = sizeof(struct tss_struct) + (unsigned long) tss;
111         wrmsr(MSR_IA32_SYSENTER_CS, __KERNEL_CS, 0);
112         wrmsr(MSR_IA32_SYSENTER_ESP, tss->x86_tss.sp1, 0);
113         wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long) ia32_sysenter_target, 0);
114         put_cpu();      
115 }
116
117 #endif  /* CONFIG_X86_64 */
118
119 int __init sysenter_setup(void)
120 {
121         char *vdso32_start, *vdso32_end;
122         int npages, i;
123
124 #ifdef CONFIG_COMPAT
125         if (vdso32_syscall()) {
126                 vdso32_start = vdso32_syscall_start;
127                 vdso32_end = vdso32_syscall_end;
128                 vdso32_pages = vdso32_syscall_pages;
129         } else
130 #endif
131         if (vdso32_sysenter()) {
132                 vdso32_start = vdso32_sysenter_start;
133                 vdso32_end = vdso32_sysenter_end;
134                 vdso32_pages = vdso32_sysenter_pages;
135         } else {
136                 vdso32_start = vdso32_int80_start;
137                 vdso32_end = vdso32_int80_end;
138                 vdso32_pages = vdso32_int80_pages;
139         }
140
141         npages = ((vdso32_end - vdso32_start) + PAGE_SIZE - 1) / PAGE_SIZE;
142         vdso32_size = npages << PAGE_SHIFT;
143         for (i = 0; i < npages; i++)
144                 vdso32_pages[i] = virt_to_page(vdso32_start + i*PAGE_SIZE);
145
146         patch_vdso32(vdso32_start, vdso32_size);
147
148         return 0;
149 }
150
151 /* Setup a VMA at program startup for the vsyscall page */
152 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
153 {
154         struct mm_struct *mm = current->mm;
155         unsigned long addr;
156         int ret = 0;
157         struct vm_area_struct *vma;
158
159 #ifdef CONFIG_X86_X32_ABI
160         if (test_thread_flag(TIF_X32))
161                 return x32_setup_additional_pages(bprm, uses_interp);
162 #endif
163
164         if (vdso_enabled != 1)  /* Other values all mean "disabled" */
165                 return 0;
166
167         down_write(&mm->mmap_sem);
168
169         addr = get_unmapped_area(NULL, 0, vdso32_size + VDSO_OFFSET(VDSO_PREV_PAGES), 0, 0);
170         if (IS_ERR_VALUE(addr)) {
171                 ret = addr;
172                 goto up_fail;
173         }
174
175         addr += VDSO_OFFSET(VDSO_PREV_PAGES);
176
177         current->mm->context.vdso = (void *)addr;
178
179         /*
180          * MAYWRITE to allow gdb to COW and set breakpoints
181          */
182         ret = install_special_mapping(mm,
183                         addr,
184                         vdso32_size,
185                         VM_READ|VM_EXEC|
186                         VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
187                         vdso32_pages);
188
189         if (ret)
190                 goto up_fail;
191
192         vma = _install_special_mapping(mm,
193                         addr -  VDSO_OFFSET(VDSO_PREV_PAGES),
194                         VDSO_OFFSET(VDSO_PREV_PAGES),
195                         VM_READ,
196                         NULL);
197
198         if (IS_ERR(vma)) {
199                 ret = PTR_ERR(vma);
200                 goto up_fail;
201         }
202
203         ret = remap_pfn_range(vma,
204                 addr - VDSO_OFFSET(VDSO_VVAR_PAGE),
205                 __pa_symbol(&__vvar_page) >> PAGE_SHIFT,
206                 PAGE_SIZE,
207                 PAGE_READONLY);
208
209         if (ret)
210                 goto up_fail;
211
212 #ifdef CONFIG_HPET_TIMER
213         if (hpet_address) {
214                 ret = io_remap_pfn_range(vma,
215                         addr - VDSO_OFFSET(VDSO_HPET_PAGE),
216                         hpet_address >> PAGE_SHIFT,
217                         PAGE_SIZE,
218                         pgprot_noncached(PAGE_READONLY));
219
220                 if (ret)
221                         goto up_fail;
222         }
223 #endif
224
225         current_thread_info()->sysenter_return =
226                 VDSO32_SYMBOL(addr, SYSENTER_RETURN);
227
228   up_fail:
229         if (ret)
230                 current->mm->context.vdso = NULL;
231
232         up_write(&mm->mmap_sem);
233
234         return ret;
235 }
236
237 #ifdef CONFIG_X86_64
238
239 subsys_initcall(sysenter_setup);
240
241 #ifdef CONFIG_SYSCTL
242 /* Register vsyscall32 into the ABI table */
243 #include <linux/sysctl.h>
244
245 static struct ctl_table abi_table2[] = {
246         {
247                 .procname       = "vsyscall32",
248                 .data           = &sysctl_vsyscall32,
249                 .maxlen         = sizeof(int),
250                 .mode           = 0644,
251                 .proc_handler   = proc_dointvec
252         },
253         {
254                 .procname       = "ldt16",
255                 .data           = &sysctl_ldt16,
256                 .maxlen         = sizeof(int),
257                 .mode           = 0644,
258                 .proc_handler   = proc_dointvec
259         },
260         {}
261 };
262
263 static struct ctl_table abi_root_table2[] = {
264         {
265                 .procname = "abi",
266                 .mode = 0555,
267                 .child = abi_table2
268         },
269         {}
270 };
271
272 static __init int ia32_binfmt_init(void)
273 {
274         register_sysctl_table(abi_root_table2);
275         return 0;
276 }
277 __initcall(ia32_binfmt_init);
278 #endif
279
280 #else  /* CONFIG_X86_32 */
281
282 const char *arch_vma_name(struct vm_area_struct *vma)
283 {
284         if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
285                 return "[vdso]";
286         return NULL;
287 }
288
289 struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
290 {
291         return NULL;
292 }
293
294 int in_gate_area(struct mm_struct *mm, unsigned long addr)
295 {
296         return 0;
297 }
298
299 int in_gate_area_no_mm(unsigned long addr)
300 {
301         return 0;
302 }
303
304 #endif  /* CONFIG_X86_64 */