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
6 * This file contains the needed initializations to support sysenter.
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>
17 #include <linux/err.h>
18 #include <linux/module.h>
20 #include <asm/cpufeature.h>
22 #include <asm/pgtable.h>
23 #include <asm/unistd.h>
25 #include <asm/tlbflush.h>
33 #ifdef CONFIG_COMPAT_VDSO
34 #define VDSO_DEFAULT VDSO_COMPAT
36 #define VDSO_DEFAULT VDSO_ENABLED
40 #include <xen/interface/callback.h>
44 * Should the kernel map a VDSO page into processes and pass its
45 * address down to glibc upon exec()?
47 unsigned int __read_mostly vdso_enabled = VDSO_DEFAULT;
49 EXPORT_SYMBOL_GPL(vdso_enabled);
51 static int __init vdso_setup(char *s)
53 vdso_enabled = simple_strtoul(s, NULL, 0);
58 __setup("vdso=", vdso_setup);
60 extern asmlinkage void sysenter_entry(void);
62 static __init void reloc_symtab(Elf32_Ehdr *ehdr,
63 unsigned offset, unsigned size)
65 Elf32_Sym *sym = (void *)ehdr + offset;
66 unsigned nsym = size / sizeof(*sym);
69 for(i = 0; i < nsym; i++, sym++) {
70 if (sym->st_shndx == SHN_UNDEF ||
71 sym->st_shndx == SHN_ABS)
74 if (sym->st_shndx > SHN_LORESERVE) {
75 printk(KERN_INFO "VDSO: unexpected st_shndx %x\n",
80 switch(ELF_ST_TYPE(sym->st_info)) {
85 sym->st_value += VDSO_HIGH_BASE;
90 static __init void reloc_dyn(Elf32_Ehdr *ehdr, unsigned offset)
92 Elf32_Dyn *dyn = (void *)ehdr + offset;
94 for(; dyn->d_tag != DT_NULL; dyn++)
109 case DT_ADDRRNGLO ... DT_ADDRRNGHI:
110 /* definitely pointers needing relocation */
111 dyn->d_un.d_ptr += VDSO_HIGH_BASE;
114 case DT_ENCODING ... OLD_DT_LOOS-1:
115 case DT_LOOS ... DT_HIOS-1:
116 /* Tags above DT_ENCODING are pointers if
118 if (dyn->d_tag >= DT_ENCODING &&
119 (dyn->d_tag & 1) == 0)
120 dyn->d_un.d_ptr += VDSO_HIGH_BASE;
128 case DT_VALRNGLO ... DT_VALRNGHI:
129 /* definitely not pointers */
132 case OLD_DT_LOOS ... DT_LOOS-1:
133 case DT_HIOS ... DT_VALRNGLO-1:
135 if (dyn->d_tag > DT_ENCODING)
136 printk(KERN_INFO "VDSO: unexpected DT_tag %x\n",
142 static __init void relocate_vdso(Elf32_Ehdr *ehdr)
148 BUG_ON(memcmp(ehdr->e_ident, ELFMAG, 4) != 0 ||
149 !elf_check_arch(ehdr) ||
150 ehdr->e_type != ET_DYN);
152 ehdr->e_entry += VDSO_HIGH_BASE;
155 phdr = (void *)ehdr + ehdr->e_phoff;
156 for (i = 0; i < ehdr->e_phnum; i++) {
157 phdr[i].p_vaddr += VDSO_HIGH_BASE;
159 /* relocate dynamic stuff */
160 if (phdr[i].p_type == PT_DYNAMIC)
161 reloc_dyn(ehdr, phdr[i].p_offset);
164 /* rebase sections */
165 shdr = (void *)ehdr + ehdr->e_shoff;
166 for(i = 0; i < ehdr->e_shnum; i++) {
167 if (!(shdr[i].sh_flags & SHF_ALLOC))
170 shdr[i].sh_addr += VDSO_HIGH_BASE;
172 if (shdr[i].sh_type == SHT_SYMTAB ||
173 shdr[i].sh_type == SHT_DYNSYM)
174 reloc_symtab(ehdr, shdr[i].sh_offset,
179 void enable_sep_cpu(void)
183 struct tss_struct *tss = &per_cpu(init_tss, cpu);
185 if (!boot_cpu_has(X86_FEATURE_SEP)) {
190 tss->x86_tss.ss1 = __KERNEL_CS;
191 tss->x86_tss.esp1 = sizeof(struct tss_struct) + (unsigned long) tss;
192 wrmsr(MSR_IA32_SYSENTER_CS, __KERNEL_CS, 0);
193 wrmsr(MSR_IA32_SYSENTER_ESP, tss->x86_tss.esp1, 0);
194 wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long) sysenter_entry, 0);
196 extern asmlinkage void sysenter_entry_pv(void);
197 static struct callback_register sysenter = {
198 .type = CALLBACKTYPE_sysenter,
199 .address = { __KERNEL_CS, (unsigned long)sysenter_entry_pv },
202 if (!boot_cpu_has(X86_FEATURE_SEP))
207 if (xen_feature(XENFEAT_supervisor_mode_kernel))
208 sysenter.address.eip = (unsigned long)sysenter_entry;
210 switch (HYPERVISOR_callback_op(CALLBACKOP_register, &sysenter)) {
213 #if CONFIG_XEN_COMPAT < 0x030200
215 sysenter.type = CALLBACKTYPE_sysenter_deprecated;
216 if (HYPERVISOR_callback_op(CALLBACKOP_register, &sysenter) == 0)
220 clear_bit(X86_FEATURE_SEP, boot_cpu_data.x86_capability);
227 static struct vm_area_struct gate_vma;
229 static int __init gate_vma_init(void)
231 gate_vma.vm_mm = NULL;
232 gate_vma.vm_start = FIXADDR_USER_START;
233 gate_vma.vm_end = FIXADDR_USER_END;
234 gate_vma.vm_flags = VM_READ | VM_MAYREAD | VM_EXEC | VM_MAYEXEC;
235 gate_vma.vm_page_prot = __P101;
237 * Make sure the vDSO gets into every core dump.
238 * Dumping its contents makes post-mortem fully interpretable later
239 * without matching up the same kernel and hardware config to see
240 * what PC values meant.
242 gate_vma.vm_flags |= VM_ALWAYSDUMP;
247 * These symbols are defined by vsyscall.o to mark the bounds
248 * of the ELF DSO images included therein.
250 extern const char vsyscall_int80_start, vsyscall_int80_end;
251 extern const char vsyscall_sysenter_start, vsyscall_sysenter_end;
252 static struct page *syscall_pages[1];
254 static void map_compat_vdso(int map)
256 static int vdso_mapped;
258 if (map == vdso_mapped)
263 __set_fixmap(FIX_VDSO, page_to_pfn(syscall_pages[0]) << PAGE_SHIFT,
264 map ? PAGE_READONLY_EXEC : PAGE_NONE);
266 /* flush stray tlbs */
270 int __init sysenter_setup(void)
272 void *syscall_page = (void *)get_zeroed_page(GFP_ATOMIC);
273 const void *vsyscall;
276 syscall_pages[0] = virt_to_page(syscall_page);
280 printk("Compat vDSO mapped to %08lx.\n", __fix_to_virt(FIX_VDSO));
282 if (!boot_cpu_has(X86_FEATURE_SEP)) {
283 vsyscall = &vsyscall_int80_start;
284 vsyscall_len = &vsyscall_int80_end - &vsyscall_int80_start;
286 vsyscall = &vsyscall_sysenter_start;
287 vsyscall_len = &vsyscall_sysenter_end - &vsyscall_sysenter_start;
290 memcpy(syscall_page, vsyscall, vsyscall_len);
291 relocate_vdso(syscall_page);
296 /* Defined in vsyscall-sysenter.S */
297 extern void SYSENTER_RETURN;
299 /* Setup a VMA at program startup for the vsyscall page */
300 int arch_setup_additional_pages(struct linux_binprm *bprm, int exstack)
302 struct mm_struct *mm = current->mm;
307 down_write(&mm->mmap_sem);
309 /* Test compat mode once here, in case someone
310 changes it via sysctl */
311 compat = (vdso_enabled == VDSO_COMPAT);
313 map_compat_vdso(compat);
316 addr = VDSO_HIGH_BASE;
318 addr = get_unmapped_area(NULL, 0, PAGE_SIZE, 0, 0);
319 if (IS_ERR_VALUE(addr)) {
325 * MAYWRITE to allow gdb to COW and set breakpoints
327 * Make sure the vDSO gets into every core dump.
328 * Dumping its contents makes post-mortem fully
329 * interpretable later without matching up the same
330 * kernel and hardware config to see what PC values
333 ret = install_special_mapping(mm, addr, PAGE_SIZE,
335 VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC|
343 current->mm->context.vdso = (void *)addr;
344 current_thread_info()->sysenter_return =
345 (void *)VDSO_SYM(&SYSENTER_RETURN);
348 up_write(&mm->mmap_sem);
353 const char *arch_vma_name(struct vm_area_struct *vma)
355 if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
360 struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
362 struct mm_struct *mm = tsk->mm;
364 /* Check to see if this task was created in compat vdso mode */
365 if (mm && mm->context.vdso == (void *)VDSO_HIGH_BASE)
370 int in_gate_area(struct task_struct *task, unsigned long addr)
372 const struct vm_area_struct *vma = get_gate_vma(task);
374 return vma && addr >= vma->vm_start && addr < vma->vm_end;
377 int in_gate_area_no_task(unsigned long addr)