This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / arch / x86_64 / ia32 / syscall32-xen.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 <asm/proto.h>
14 #include <asm/tlbflush.h>
15 #include <asm/ia32_unistd.h>
16
17 #ifdef USE_INT80
18 extern unsigned char syscall32_int80[], syscall32_int80_end[];
19 #endif
20 extern unsigned char syscall32_syscall[], syscall32_syscall_end[];
21 extern unsigned char syscall32_sysenter[], syscall32_sysenter_end[];
22 extern int sysctl_vsyscall32;
23
24 char *syscall32_page; 
25 #ifndef USE_INT80
26 static int use_sysenter = -1;
27 #endif
28
29 static struct page *
30 syscall32_nopage(struct vm_area_struct *vma, unsigned long adr, int *type)
31 {
32         struct page *p = virt_to_page(adr - vma->vm_start + syscall32_page);
33         get_page(p);
34         return p;
35 }
36
37 /* Prevent VMA merging */
38 static void syscall32_vma_close(struct vm_area_struct *vma)
39 {
40 }
41
42 static struct vm_operations_struct syscall32_vm_ops = {
43         .close = syscall32_vma_close,
44         .nopage = syscall32_nopage,
45 };
46
47 struct linux_binprm;
48
49 /* Setup a VMA at program startup for the vsyscall page */
50 int syscall32_setup_pages(struct linux_binprm *bprm, int exstack)
51 {
52         int npages = (VSYSCALL32_END - VSYSCALL32_BASE) >> PAGE_SHIFT;
53         struct vm_area_struct *vma;
54         struct mm_struct *mm = current->mm;
55         int ret;
56
57         vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
58         if (!vma)
59                 return -ENOMEM;
60
61         memset(vma, 0, sizeof(struct vm_area_struct));
62         /* Could randomize here */
63         vma->vm_start = VSYSCALL32_BASE;
64         vma->vm_end = VSYSCALL32_END;
65         /* MAYWRITE to allow gdb to COW and set breakpoints */
66         vma->vm_flags = VM_READ|VM_EXEC|VM_MAYREAD|VM_MAYEXEC|VM_MAYWRITE;
67         vma->vm_flags |= mm->def_flags;
68         vma->vm_page_prot = protection_map[vma->vm_flags & 7];
69         vma->vm_ops = &syscall32_vm_ops;
70         vma->vm_mm = mm;
71
72         down_write(&mm->mmap_sem);
73         if ((ret = insert_vm_struct(mm, vma))) {
74                 up_write(&mm->mmap_sem);
75                 kmem_cache_free(vm_area_cachep, vma);
76                 return ret;
77         }
78         mm->total_vm += npages;
79         up_write(&mm->mmap_sem);
80         return 0;
81 }
82
83 static int __init init_syscall32(void)
84
85         syscall32_page = (void *)get_zeroed_page(GFP_KERNEL); 
86         if (!syscall32_page) 
87                 panic("Cannot allocate syscall32 page"); 
88
89 #ifdef USE_INT80
90         /*
91          * At this point we use int 0x80.
92          */
93         memcpy(syscall32_page, syscall32_int80,
94                syscall32_int80_end - syscall32_int80);
95 #else
96         if (use_sysenter > 0) {
97                 memcpy(syscall32_page, syscall32_sysenter,
98                        syscall32_sysenter_end - syscall32_sysenter);
99         } else {
100                 memcpy(syscall32_page, syscall32_syscall,
101                        syscall32_syscall_end - syscall32_syscall);
102         }       
103 #endif
104         return 0;
105
106
107 /*
108  * This must be done early in case we have an initrd containing 32-bit
109  * binaries (e.g., hotplug). This could be pushed upstream to arch/x86_64.
110  */     
111 core_initcall(init_syscall32); 
112
113 /* May not be __init: called during resume */
114 void syscall32_cpu_init(void)
115 {
116 #ifndef USE_INT80
117         if (use_sysenter < 0)
118                 use_sysenter = (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL);
119
120         /* Load these always in case some future AMD CPU supports
121            SYSENTER from compat mode too. */
122         checking_wrmsrl(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
123         checking_wrmsrl(MSR_IA32_SYSENTER_ESP, 0ULL);
124         checking_wrmsrl(MSR_IA32_SYSENTER_EIP, (u64)ia32_sysenter_target);
125
126         wrmsrl(MSR_CSTAR, ia32_cstar_target);
127 #endif
128 }