1 /* Copyright 2002,2003 Andi Kleen, SuSE Labs */
3 /* vsyscall handling for 32bit processes. Map a stub page into it
4 on demand because 32bit cannot reach the kernel's fixmaps */
7 #include <linux/string.h>
8 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/stringify.h>
12 #include <linux/security.h>
13 #include <linux/vs_memory.h>
14 #include <asm/proto.h>
15 #include <asm/tlbflush.h>
16 #include <asm/ia32_unistd.h>
18 extern unsigned char syscall32_syscall[], syscall32_syscall_end[];
19 extern unsigned char syscall32_sysenter[], syscall32_sysenter_end[];
20 extern int sysctl_vsyscall32;
23 static int use_sysenter = -1;
26 syscall32_nopage(struct vm_area_struct *vma, unsigned long adr, int *type)
28 struct page *p = virt_to_page(adr - vma->vm_start + syscall32_page);
33 /* Prevent VMA merging */
34 static void syscall32_vma_close(struct vm_area_struct *vma)
38 static struct vm_operations_struct syscall32_vm_ops = {
39 .close = syscall32_vma_close,
40 .nopage = syscall32_nopage,
45 /* Setup a VMA at program startup for the vsyscall page */
46 int syscall32_setup_pages(struct linux_binprm *bprm, int exstack,
47 unsigned long start_code,
48 unsigned long interp_map_address)
50 int npages = (VSYSCALL32_END - VSYSCALL32_BASE) >> PAGE_SHIFT;
51 struct vm_area_struct *vma;
52 struct mm_struct *mm = current->mm;
55 vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
59 memset(vma, 0, sizeof(struct vm_area_struct));
60 /* Could randomize here */
61 vma->vm_start = VSYSCALL32_BASE;
62 vma->vm_end = VSYSCALL32_END;
63 /* MAYWRITE to allow gdb to COW and set breakpoints */
64 vma->vm_flags = VM_READ|VM_EXEC|VM_MAYREAD|VM_MAYEXEC|VM_MAYWRITE;
65 vma->vm_flags |= mm->def_flags;
66 vma->vm_page_prot = protection_map[vma->vm_flags & 7];
67 vma->vm_ops = &syscall32_vm_ops;
70 down_write(&mm->mmap_sem);
71 if ((ret = insert_vm_struct(mm, vma))) {
72 up_write(&mm->mmap_sem);
73 kmem_cache_free(vm_area_cachep, vma);
76 vx_vmpages_add(mm, npages);
77 up_write(&mm->mmap_sem);
81 static int __init init_syscall32(void)
83 syscall32_page = (void *)get_zeroed_page(GFP_KERNEL);
85 panic("Cannot allocate syscall32 page");
86 if (use_sysenter > 0) {
87 memcpy(syscall32_page, syscall32_sysenter,
88 syscall32_sysenter_end - syscall32_sysenter);
90 memcpy(syscall32_page, syscall32_syscall,
91 syscall32_syscall_end - syscall32_syscall);
96 __initcall(init_syscall32);
98 /* May not be __init: called during resume */
99 void syscall32_cpu_init(void)
101 if (use_sysenter < 0)
102 use_sysenter = (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL);
104 /* Load these always in case some future AMD CPU supports
105 SYSENTER from compat mode too. */
106 checking_wrmsrl(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
107 checking_wrmsrl(MSR_IA32_SYSENTER_ESP, 0ULL);
108 checking_wrmsrl(MSR_IA32_SYSENTER_EIP, (u64)ia32_sysenter_target);
110 wrmsrl(MSR_CSTAR, ia32_cstar_target);