fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / arch / i386 / kernel / sysenter.c
1 /*
2  * linux/arch/i386/kernel/sysenter.c
3  *
4  * (C) Copyright 2002 Linus Torvalds
5  * Portions based on the vdso-randomization code from exec-shield:
6  * Copyright(C) 2005-2006, Red Hat, Inc., Ingo Molnar
7  *
8  * This file contains the needed initializations to support sysenter.
9  */
10
11 #include <linux/init.h>
12 #include <linux/smp.h>
13 #include <linux/thread_info.h>
14 #include <linux/sched.h>
15 #include <linux/gfp.h>
16 #include <linux/string.h>
17 #include <linux/elf.h>
18 #include <linux/mm.h>
19 #include <linux/mman.h>
20 #include <linux/module.h>
21 #include <linux/vs_memory.h>
22
23 #include <asm/cpufeature.h>
24 #include <asm/msr.h>
25 #include <asm/pgtable.h>
26 #include <asm/unistd.h>
27
28 #ifdef CONFIG_XEN
29 #include <xen/interface/callback.h>
30 #endif
31
32 /*
33  * Should the kernel map a VDSO page into processes and pass its
34  * address down to glibc upon exec()?
35  */
36 #ifdef CONFIG_PARAVIRT
37 unsigned int __read_mostly vdso_enabled = 0;
38 #else
39 unsigned int __read_mostly vdso_enabled = 1;
40 #endif
41
42 EXPORT_SYMBOL_GPL(vdso_enabled);
43
44 static int __init vdso_setup(char *s)
45 {
46         vdso_enabled = simple_strtoul(s, NULL, 0);
47
48         return 1;
49 }
50
51 __setup("vdso=", vdso_setup);
52
53 extern asmlinkage void sysenter_entry(void);
54
55 void enable_sep_cpu(void)
56 {
57 #ifndef CONFIG_X86_NO_TSS
58         int cpu = get_cpu();
59         struct tss_struct *tss = &per_cpu(init_tss, cpu);
60
61         if (!boot_cpu_has(X86_FEATURE_SEP)) {
62                 put_cpu();
63                 return;
64         }
65
66         tss->ss1 = __KERNEL_CS;
67         tss->esp1 = sizeof(struct tss_struct) + (unsigned long) tss;
68         wrmsr(MSR_IA32_SYSENTER_CS, __KERNEL_CS, 0);
69         wrmsr(MSR_IA32_SYSENTER_ESP, tss->esp1, 0);
70         wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long) sysenter_entry, 0);
71         put_cpu();      
72 #endif
73 }
74
75 /*
76  * These symbols are defined by vsyscall.o to mark the bounds
77  * of the ELF DSO images included therein.
78  */
79 extern const char vsyscall_int80_start, vsyscall_int80_end;
80 extern const char vsyscall_sysenter_start, vsyscall_sysenter_end;
81 static void *syscall_page;
82
83 int __init sysenter_setup(void)
84 {
85         syscall_page = (void *)get_zeroed_page(GFP_ATOMIC);
86
87 #ifdef CONFIG_XEN
88         if (boot_cpu_has(X86_FEATURE_SEP)) {
89                 struct callback_register sysenter = {
90                         .type = CALLBACKTYPE_sysenter,
91                         .address = { __KERNEL_CS, (unsigned long)sysenter_entry },
92                 };
93
94                 if (HYPERVISOR_callback_op(CALLBACKOP_register, &sysenter) < 0)
95                         clear_bit(X86_FEATURE_SEP, boot_cpu_data.x86_capability);
96         }
97 #endif
98
99 #ifdef CONFIG_COMPAT_VDSO
100         __set_fixmap(FIX_VDSO, __pa(syscall_page), PAGE_READONLY_EXEC);
101         printk("Compat vDSO mapped to %08lx.\n", __fix_to_virt(FIX_VDSO));
102 #endif
103
104         if (!boot_cpu_has(X86_FEATURE_SEP)) {
105                 memcpy(syscall_page,
106                        &vsyscall_int80_start,
107                        &vsyscall_int80_end - &vsyscall_int80_start);
108                 return 0;
109         }
110
111         memcpy(syscall_page,
112                &vsyscall_sysenter_start,
113                &vsyscall_sysenter_end - &vsyscall_sysenter_start);
114
115         return 0;
116 }
117
118 #ifndef CONFIG_COMPAT_VDSO
119 static struct page *syscall_nopage(struct vm_area_struct *vma,
120                                 unsigned long adr, int *type)
121 {
122         struct page *p = virt_to_page(adr - vma->vm_start + syscall_page);
123         get_page(p);
124         return p;
125 }
126
127 /* Prevent VMA merging */
128 static void syscall_vma_close(struct vm_area_struct *vma)
129 {
130 }
131
132 static struct vm_operations_struct syscall_vm_ops = {
133         .close = syscall_vma_close,
134         .nopage = syscall_nopage,
135 };
136
137 /* Defined in vsyscall-sysenter.S */
138 extern void SYSENTER_RETURN;
139
140 /* Setup a VMA at program startup for the vsyscall page */
141 int arch_setup_additional_pages(struct linux_binprm *bprm, int exstack,
142                                 unsigned long start_code, unsigned long interp_map_address)
143 {
144         struct vm_area_struct *vma;
145         struct mm_struct *mm = current->mm;
146         unsigned long addr;
147         int ret;
148
149         down_write(&mm->mmap_sem);
150         addr = get_unmapped_area_prot(NULL, 0, PAGE_SIZE, 0, 0, 1);
151         if (IS_ERR_VALUE(addr)) {
152                 ret = addr;
153                 goto up_fail;
154         }
155
156         vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
157         if (!vma) {
158                 ret = -ENOMEM;
159                 goto up_fail;
160         }
161
162         vma->vm_start = addr;
163         vma->vm_end = addr + PAGE_SIZE;
164         /* MAYWRITE to allow gdb to COW and set breakpoints */
165         vma->vm_flags = VM_READ|VM_EXEC|VM_MAYREAD|VM_MAYEXEC|VM_MAYWRITE;
166         /*
167          * Make sure the vDSO gets into every core dump.
168          * Dumping its contents makes post-mortem fully interpretable later
169          * without matching up the same kernel and hardware config to see
170          * what PC values meant.
171          */
172         vma->vm_flags |= VM_ALWAYSDUMP;
173         vma->vm_flags |= mm->def_flags;
174         vma->vm_page_prot = protection_map[vma->vm_flags & 7];
175         vma->vm_ops = &syscall_vm_ops;
176         vma->vm_mm = mm;
177
178         ret = insert_vm_struct(mm, vma);
179         if (unlikely(ret)) {
180                 kmem_cache_free(vm_area_cachep, vma);
181                 goto up_fail;
182         }
183
184         current->mm->context.vdso = (void *)addr;
185         current_thread_info()->sysenter_return =
186                                     (void *)VDSO_SYM(&SYSENTER_RETURN);
187         vx_vmpages_inc(mm);
188 up_fail:
189         up_write(&mm->mmap_sem);
190         return ret;
191 }
192
193 const char *arch_vma_name(struct vm_area_struct *vma)
194 {
195         if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
196                 return "[vdso]";
197         return NULL;
198 }
199
200 struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
201 {
202         return NULL;
203 }
204
205 int in_gate_area(struct task_struct *task, unsigned long addr)
206 {
207         return 0;
208 }
209
210 int in_gate_area_no_task(unsigned long addr)
211 {
212         return 0;
213 }
214 #endif