blob: adbc5f8089e9a209707087ca86048c8358caa93e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Copyright 2002,2003 Andi Kleen, SuSE Labs */
2
3/* vsyscall handling for 32bit processes. Map a stub page into it
4 on demand because 32bit cannot reach the kernel's fixmaps */
5
6#include <linux/mm.h>
7#include <linux/string.h>
8#include <linux/kernel.h>
9#include <linux/gfp.h>
10#include <linux/init.h>
11#include <linux/stringify.h>
Andi Kleen1e014412005-04-16 15:24:55 -070012#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <asm/proto.h>
14#include <asm/tlbflush.h>
15#include <asm/ia32_unistd.h>
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017extern unsigned char syscall32_syscall[], syscall32_syscall_end[];
18extern unsigned char syscall32_sysenter[], syscall32_sysenter_end[];
19extern int sysctl_vsyscall32;
20
21char *syscall32_page;
22static int use_sysenter = -1;
23
Andi Kleen1e014412005-04-16 15:24:55 -070024static struct page *
25syscall32_nopage(struct vm_area_struct *vma, unsigned long adr, int *type)
26{
27 struct page *p = virt_to_page(adr - vma->vm_start + syscall32_page);
28 get_page(p);
29 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030}
31
Andi Kleen1e014412005-04-16 15:24:55 -070032/* Prevent VMA merging */
33static void syscall32_vma_close(struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Andi Kleen1e014412005-04-16 15:24:55 -070035}
36
37static struct vm_operations_struct syscall32_vm_ops = {
38 .close = syscall32_vma_close,
39 .nopage = syscall32_nopage,
40};
41
42struct linux_binprm;
43
44/* Setup a VMA at program startup for the vsyscall page */
45int syscall32_setup_pages(struct linux_binprm *bprm, int exstack)
46{
47 int npages = (VSYSCALL32_END - VSYSCALL32_BASE) >> PAGE_SHIFT;
48 struct vm_area_struct *vma;
49 struct mm_struct *mm = current->mm;
Siddha, Suresh B9fb17592005-07-15 19:17:44 -070050 int ret;
Andi Kleen1e014412005-04-16 15:24:55 -070051
52 vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
53 if (!vma)
54 return -ENOMEM;
55 if (security_vm_enough_memory(npages)) {
56 kmem_cache_free(vm_area_cachep, vma);
57 return -ENOMEM;
58 }
59
60 memset(vma, 0, sizeof(struct vm_area_struct));
61 /* Could randomize here */
62 vma->vm_start = VSYSCALL32_BASE;
63 vma->vm_end = VSYSCALL32_END;
64 /* MAYWRITE to allow gdb to COW and set breakpoints */
65 vma->vm_flags = VM_READ|VM_EXEC|VM_MAYREAD|VM_MAYEXEC|VM_MAYEXEC|VM_MAYWRITE;
66 vma->vm_flags |= mm->def_flags;
67 vma->vm_page_prot = protection_map[vma->vm_flags & 7];
68 vma->vm_ops = &syscall32_vm_ops;
69 vma->vm_mm = mm;
70
71 down_write(&mm->mmap_sem);
Siddha, Suresh B9fb17592005-07-15 19:17:44 -070072 if ((ret = insert_vm_struct(mm, vma))) {
73 up_write(&mm->mmap_sem);
74 kmem_cache_free(vm_area_cachep, vma);
75 return ret;
76 }
Andi Kleen1e014412005-04-16 15:24:55 -070077 mm->total_vm += npages;
78 up_write(&mm->mmap_sem);
79 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
82static int __init init_syscall32(void)
83{
84 syscall32_page = (void *)get_zeroed_page(GFP_KERNEL);
85 if (!syscall32_page)
86 panic("Cannot allocate syscall32 page");
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (use_sysenter > 0) {
88 memcpy(syscall32_page, syscall32_sysenter,
89 syscall32_sysenter_end - syscall32_sysenter);
90 } else {
91 memcpy(syscall32_page, syscall32_syscall,
92 syscall32_syscall_end - syscall32_syscall);
93 }
94 return 0;
95}
96
97__initcall(init_syscall32);
98
99/* May not be __init: called during resume */
100void syscall32_cpu_init(void)
101{
102 if (use_sysenter < 0)
103 use_sysenter = (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL);
104
105 /* Load these always in case some future AMD CPU supports
106 SYSENTER from compat mode too. */
107 checking_wrmsrl(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
108 checking_wrmsrl(MSR_IA32_SYSENTER_ESP, 0ULL);
109 checking_wrmsrl(MSR_IA32_SYSENTER_EIP, (u64)ia32_sysenter_target);
110
111 wrmsrl(MSR_CSTAR, ia32_cstar_target);
112}