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