Merge branch 'master' of /home/trondmy/kernel/linux-2.6/ into merge_linus
[linux-2.6] / 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/module.h>
20
21 #include <asm/cpufeature.h>
22 #include <asm/msr.h>
23 #include <asm/pgtable.h>
24 #include <asm/unistd.h>
25
26 /*
27  * Should the kernel map a VDSO page into processes and pass its
28  * address down to glibc upon exec()?
29  */
30 #ifdef CONFIG_PARAVIRT
31 unsigned int __read_mostly vdso_enabled = 0;
32 #else
33 unsigned int __read_mostly vdso_enabled = 1;
34 #endif
35
36 EXPORT_SYMBOL_GPL(vdso_enabled);
37
38 static int __init vdso_setup(char *s)
39 {
40         vdso_enabled = simple_strtoul(s, NULL, 0);
41
42         return 1;
43 }
44
45 __setup("vdso=", vdso_setup);
46
47 extern asmlinkage void sysenter_entry(void);
48
49 void enable_sep_cpu(void)
50 {
51         int cpu = get_cpu();
52         struct tss_struct *tss = &per_cpu(init_tss, cpu);
53
54         if (!boot_cpu_has(X86_FEATURE_SEP)) {
55                 put_cpu();
56                 return;
57         }
58
59         tss->ss1 = __KERNEL_CS;
60         tss->esp1 = sizeof(struct tss_struct) + (unsigned long) tss;
61         wrmsr(MSR_IA32_SYSENTER_CS, __KERNEL_CS, 0);
62         wrmsr(MSR_IA32_SYSENTER_ESP, tss->esp1, 0);
63         wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long) sysenter_entry, 0);
64         put_cpu();      
65 }
66
67 /*
68  * These symbols are defined by vsyscall.o to mark the bounds
69  * of the ELF DSO images included therein.
70  */
71 extern const char vsyscall_int80_start, vsyscall_int80_end;
72 extern const char vsyscall_sysenter_start, vsyscall_sysenter_end;
73 static void *syscall_page;
74
75 int __init sysenter_setup(void)
76 {
77         syscall_page = (void *)get_zeroed_page(GFP_ATOMIC);
78
79 #ifdef CONFIG_COMPAT_VDSO
80         __set_fixmap(FIX_VDSO, __pa(syscall_page), PAGE_READONLY);
81         printk("Compat vDSO mapped to %08lx.\n", __fix_to_virt(FIX_VDSO));
82 #else
83         /*
84          * In the non-compat case the ELF coredumping code needs the fixmap:
85          */
86         __set_fixmap(FIX_VDSO, __pa(syscall_page), PAGE_KERNEL_RO);
87 #endif
88
89         if (!boot_cpu_has(X86_FEATURE_SEP)) {
90                 memcpy(syscall_page,
91                        &vsyscall_int80_start,
92                        &vsyscall_int80_end - &vsyscall_int80_start);
93                 return 0;
94         }
95
96         memcpy(syscall_page,
97                &vsyscall_sysenter_start,
98                &vsyscall_sysenter_end - &vsyscall_sysenter_start);
99
100         return 0;
101 }
102
103 static struct page *syscall_nopage(struct vm_area_struct *vma,
104                                 unsigned long adr, int *type)
105 {
106         struct page *p = virt_to_page(adr - vma->vm_start + syscall_page);
107         get_page(p);
108         return p;
109 }
110
111 /* Prevent VMA merging */
112 static void syscall_vma_close(struct vm_area_struct *vma)
113 {
114 }
115
116 static struct vm_operations_struct syscall_vm_ops = {
117         .close = syscall_vma_close,
118         .nopage = syscall_nopage,
119 };
120
121 /* Defined in vsyscall-sysenter.S */
122 extern void SYSENTER_RETURN;
123
124 /* Setup a VMA at program startup for the vsyscall page */
125 int arch_setup_additional_pages(struct linux_binprm *bprm, int exstack)
126 {
127         struct vm_area_struct *vma;
128         struct mm_struct *mm = current->mm;
129         unsigned long addr;
130         int ret;
131
132         down_write(&mm->mmap_sem);
133         addr = get_unmapped_area(NULL, 0, PAGE_SIZE, 0, 0);
134         if (IS_ERR_VALUE(addr)) {
135                 ret = addr;
136                 goto up_fail;
137         }
138
139         vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
140         if (!vma) {
141                 ret = -ENOMEM;
142                 goto up_fail;
143         }
144
145         vma->vm_start = addr;
146         vma->vm_end = addr + PAGE_SIZE;
147         /* MAYWRITE to allow gdb to COW and set breakpoints */
148         vma->vm_flags = VM_READ|VM_EXEC|VM_MAYREAD|VM_MAYEXEC|VM_MAYWRITE;
149         vma->vm_flags |= mm->def_flags;
150         vma->vm_page_prot = protection_map[vma->vm_flags & 7];
151         vma->vm_ops = &syscall_vm_ops;
152         vma->vm_mm = mm;
153
154         ret = insert_vm_struct(mm, vma);
155         if (unlikely(ret)) {
156                 kmem_cache_free(vm_area_cachep, vma);
157                 goto up_fail;
158         }
159
160         current->mm->context.vdso = (void *)addr;
161         current_thread_info()->sysenter_return =
162                                     (void *)VDSO_SYM(&SYSENTER_RETURN);
163         mm->total_vm++;
164 up_fail:
165         up_write(&mm->mmap_sem);
166         return ret;
167 }
168
169 const char *arch_vma_name(struct vm_area_struct *vma)
170 {
171         if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
172                 return "[vdso]";
173         return NULL;
174 }
175
176 struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
177 {
178         return NULL;
179 }
180
181 int in_gate_area(struct task_struct *task, unsigned long addr)
182 {
183         return 0;
184 }
185
186 int in_gate_area_no_task(unsigned long addr)
187 {
188         return 0;
189 }