linux 2.6.16.38 w/ vs2.0.3-rc1
[linux-2.6.git] / arch / x86_64 / ia32 / syscall32.c
1 /* 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>
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>
17
18 extern unsigned char syscall32_syscall[], syscall32_syscall_end[];
19 extern unsigned char syscall32_sysenter[], syscall32_sysenter_end[];
20 extern int sysctl_vsyscall32;
21
22 char *syscall32_page; 
23 static int use_sysenter = -1;
24
25 static struct page *
26 syscall32_nopage(struct vm_area_struct *vma, unsigned long adr, int *type)
27 {
28         struct page *p = virt_to_page(adr - vma->vm_start + syscall32_page);
29         get_page(p);
30         return p;
31 }
32
33 /* Prevent VMA merging */
34 static void syscall32_vma_close(struct vm_area_struct *vma)
35 {
36 }
37
38 static struct vm_operations_struct syscall32_vm_ops = {
39         .close = syscall32_vma_close,
40         .nopage = syscall32_nopage,
41 };
42
43 struct linux_binprm;
44
45 /* Setup a VMA at program startup for the vsyscall page */
46 int syscall32_setup_pages(struct linux_binprm *bprm, int exstack)
47 {
48         int npages = (VSYSCALL32_END - VSYSCALL32_BASE) >> PAGE_SHIFT;
49         struct vm_area_struct *vma;
50         struct mm_struct *mm = current->mm;
51         int ret;
52
53         vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
54         if (!vma)
55                 return -ENOMEM;
56
57         memset(vma, 0, sizeof(struct vm_area_struct));
58         /* Could randomize here */
59         vma->vm_start = VSYSCALL32_BASE;
60         vma->vm_end = VSYSCALL32_END;
61         /* MAYWRITE to allow gdb to COW and set breakpoints */
62         vma->vm_flags = VM_READ|VM_EXEC|VM_MAYREAD|VM_MAYEXEC|VM_MAYWRITE;
63         vma->vm_flags |= mm->def_flags;
64         vma->vm_page_prot = protection_map[vma->vm_flags & 7];
65         vma->vm_ops = &syscall32_vm_ops;
66         vma->vm_mm = mm;
67
68         down_write(&mm->mmap_sem);
69         if ((ret = insert_vm_struct(mm, vma))) {
70                 up_write(&mm->mmap_sem);
71                 kmem_cache_free(vm_area_cachep, vma);
72                 return ret;
73         }
74         vx_vmpages_add(mm, npages);
75         up_write(&mm->mmap_sem);
76         return 0;
77 }
78
79 static int __init init_syscall32(void)
80
81         syscall32_page = (void *)get_zeroed_page(GFP_KERNEL); 
82         if (!syscall32_page) 
83                 panic("Cannot allocate syscall32 page"); 
84         if (use_sysenter > 0) {
85                 memcpy(syscall32_page, syscall32_sysenter,
86                        syscall32_sysenter_end - syscall32_sysenter);
87         } else {
88                 memcpy(syscall32_page, syscall32_syscall,
89                        syscall32_syscall_end - syscall32_syscall);
90         }       
91         return 0;
92
93         
94 __initcall(init_syscall32); 
95
96 /* May not be __init: called during resume */
97 void syscall32_cpu_init(void)
98 {
99         if (use_sysenter < 0)
100                 use_sysenter = (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL);
101
102         /* Load these always in case some future AMD CPU supports
103            SYSENTER from compat mode too. */
104         checking_wrmsrl(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
105         checking_wrmsrl(MSR_IA32_SYSENTER_ESP, 0ULL);
106         checking_wrmsrl(MSR_IA32_SYSENTER_EIP, (u64)ia32_sysenter_target);
107
108         wrmsrl(MSR_CSTAR, ia32_cstar_target);
109 }