fedora core 6 1.2949 + vserver 2.2.0
[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 #ifdef USE_INT80
19 extern unsigned char syscall32_int80[], syscall32_int80_end[];
20 #endif
21 extern unsigned char syscall32_syscall[], syscall32_syscall_end[];
22 extern unsigned char syscall32_sysenter[], syscall32_sysenter_end[];
23 extern int sysctl_vsyscall32;
24
25 char *syscall32_page; 
26 #ifndef USE_INT80
27 static int use_sysenter = -1;
28 #endif
29
30 static struct page *
31 syscall32_nopage(struct vm_area_struct *vma, unsigned long adr, int *type)
32 {
33         struct page *p = virt_to_page(adr - vma->vm_start + syscall32_page);
34         get_page(p);
35         return p;
36 }
37
38 /* Prevent VMA merging */
39 static void syscall32_vma_close(struct vm_area_struct *vma)
40 {
41 }
42
43 static struct vm_operations_struct syscall32_vm_ops = {
44         .close = syscall32_vma_close,
45         .nopage = syscall32_nopage,
46 };
47
48 struct linux_binprm;
49
50 /* Setup a VMA at program startup for the vsyscall page */
51 int syscall32_setup_pages(struct linux_binprm *bprm, int exstack,
52                           unsigned long start_code,
53                           unsigned long interp_map_address)
54 {
55         int npages = (VSYSCALL32_END - VSYSCALL32_BASE) >> PAGE_SHIFT;
56         struct vm_area_struct *vma;
57         struct mm_struct *mm = current->mm;
58         int ret;
59
60         vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
61         if (!vma)
62                 return -ENOMEM;
63
64         memset(vma, 0, sizeof(struct vm_area_struct));
65         /* Could randomize here */
66         vma->vm_start = VSYSCALL32_BASE;
67         vma->vm_end = VSYSCALL32_END;
68         /* MAYWRITE to allow gdb to COW and set breakpoints */
69         vma->vm_flags = VM_READ|VM_EXEC|VM_MAYREAD|VM_MAYEXEC|VM_MAYWRITE;
70         /*
71          * Make sure the vDSO gets into every core dump.
72          * Dumping its contents makes post-mortem fully interpretable later
73          * without matching up the same kernel and hardware config to see
74          * what PC values meant.
75          */
76         vma->vm_flags |= VM_ALWAYSDUMP;
77         vma->vm_flags |= mm->def_flags;
78         vma->vm_page_prot = protection_map[vma->vm_flags & 7];
79         vma->vm_ops = &syscall32_vm_ops;
80         vma->vm_mm = mm;
81
82         down_write(&mm->mmap_sem);
83         if ((ret = insert_vm_struct(mm, vma))) {
84                 up_write(&mm->mmap_sem);
85                 kmem_cache_free(vm_area_cachep, vma);
86                 return ret;
87         }
88         vx_vmpages_add(mm, npages);
89         up_write(&mm->mmap_sem);
90         return 0;
91 }
92
93 const char *arch_vma_name(struct vm_area_struct *vma)
94 {
95         if (vma->vm_start == VSYSCALL32_BASE &&
96             vma->vm_mm && vma->vm_mm->task_size == IA32_PAGE_OFFSET)
97                 return "[vdso]";
98         return NULL;
99 }
100
101 static int __init init_syscall32(void)
102
103         syscall32_page = (void *)get_zeroed_page(GFP_KERNEL); 
104         if (!syscall32_page) 
105                 panic("Cannot allocate syscall32 page"); 
106
107 #ifdef USE_INT80
108         /*
109          * At this point we use int 0x80.
110          */
111         memcpy(syscall32_page, syscall32_int80,
112                syscall32_int80_end - syscall32_int80);
113 #else
114         if (use_sysenter > 0) {
115                 memcpy(syscall32_page, syscall32_sysenter,
116                        syscall32_sysenter_end - syscall32_sysenter);
117         } else {
118                 memcpy(syscall32_page, syscall32_syscall,
119                        syscall32_syscall_end - syscall32_syscall);
120         }       
121 #endif
122         return 0;
123
124
125 /*
126  * This must be done early in case we have an initrd containing 32-bit
127  * binaries (e.g., hotplug). This could be pushed upstream to arch/x86_64.
128  */     
129 core_initcall(init_syscall32); 
130
131 /* May not be __init: called during resume */
132 void syscall32_cpu_init(void)
133 {
134 #ifndef USE_INT80
135         if (use_sysenter < 0)
136                 use_sysenter = (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL);
137
138         /* Load these always in case some future AMD CPU supports
139            SYSENTER from compat mode too. */
140         checking_wrmsrl(MSR_IA32_SYSENTER_CS, (u64)__KERNEL_CS);
141         checking_wrmsrl(MSR_IA32_SYSENTER_ESP, 0ULL);
142         checking_wrmsrl(MSR_IA32_SYSENTER_EIP, (u64)ia32_sysenter_target);
143
144         wrmsrl(MSR_CSTAR, ia32_cstar_target);
145 #endif
146 }