ec4959f74736891cb8298fef35047f0640254778
[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  *
6  * This file contains the needed initializations to support sysenter.
7  */
8
9 #include <linux/init.h>
10 #include <linux/smp.h>
11 #include <linux/thread_info.h>
12 #include <linux/sched.h>
13 #include <linux/gfp.h>
14 #include <linux/string.h>
15 #include <linux/elf.h>
16 #include <linux/mman.h>
17
18 #include <asm/a.out.h>
19 #include <asm/cpufeature.h>
20 #include <asm/msr.h>
21 #include <asm/pgtable.h>
22 #include <asm/unistd.h>
23
24 extern asmlinkage void sysenter_entry(void);
25
26 void enable_sep_cpu(void)
27 {
28         int cpu = get_cpu();
29         struct tss_struct *tss = &per_cpu(init_tss, cpu);
30
31         if (!boot_cpu_has(X86_FEATURE_SEP)) {
32                 put_cpu();
33                 return;
34         }
35
36         tss->ss1 = __KERNEL_CS;
37         tss->esp1 = sizeof(struct tss_struct) + (unsigned long) tss;
38         wrmsr(MSR_IA32_SYSENTER_CS, __KERNEL_CS, 0);
39         wrmsr(MSR_IA32_SYSENTER_ESP, tss->esp1, 0);
40         wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long) sysenter_entry, 0);
41         put_cpu();      
42 }
43
44 /*
45  * These symbols are defined by vsyscall.o to mark the bounds
46  * of the ELF DSO images included therein.
47  */
48 extern const char vsyscall_int80_start, vsyscall_int80_end;
49 extern const char vsyscall_sysenter_start, vsyscall_sysenter_end;
50
51 static struct page *sysenter_pages[2];
52
53 int __init sysenter_setup(void)
54 {
55         void *page = (void *)get_zeroed_page(GFP_ATOMIC);
56
57         sysenter_pages[0] = virt_to_page(page);
58
59         if (!boot_cpu_has(X86_FEATURE_SEP)) {
60                 memcpy(page,
61                        &vsyscall_int80_start,
62                        &vsyscall_int80_end - &vsyscall_int80_start);
63                 return 0;
64         }
65
66         memcpy(page,
67                &vsyscall_sysenter_start,
68                &vsyscall_sysenter_end - &vsyscall_sysenter_start);
69
70         return 0;
71 }
72
73 extern void SYSENTER_RETURN_OFFSET;
74
75 unsigned int vdso_enabled = 1;
76
77 /*
78  * This is called from binfmt_elf, we create the special vma for the
79  * vDSO and insert it into the mm struct tree.
80  */
81 int arch_setup_additional_pages(struct linux_binprm *bprm,
82         int executable_stack, unsigned long start_code,
83         unsigned long interp_map_address)
84 {
85         struct thread_info *ti = current_thread_info();
86         unsigned long addr = 0, len;
87         unsigned flags = MAP_PRIVATE;
88         int err;
89
90         current->mm->context.vdso = NULL;
91         if (unlikely(!vdso_enabled) || unlikely(!sysenter_pages[0]))
92                 return 0;
93
94         /*
95          * Map the vDSO (it will be randomized):
96          */
97         down_write(&current->mm->mmap_sem);
98         len = PAGE_SIZE > ELF_EXEC_PAGESIZE ? PAGE_SIZE : ELF_EXEC_PAGESIZE;
99         if (0==exec_shield) { /* off; %cs limit off */
100                 addr = STACK_TOP;  /* minimal interference with anybody */
101                 flags = MAP_PRIVATE | MAP_FIXED;
102         }
103         else if ((3<<2) & exec_shield) { /* vdso just below .text */
104                 addr = (((2<<2) & exec_shield) && interp_map_address) ?
105                         interp_map_address : start_code;
106                 /* 1MB for vm86; 64K for vm86 himem */
107                 if ((0x110000 + len) <= addr) {
108                         addr = (PAGE_MASK & addr) - len;
109                 }
110                 else { /* start_code is too low */
111                         addr = 0;
112                 }
113         }
114         addr = get_unmapped_area_prot(NULL, addr, len, 0,
115                                       flags, PROT_READ | PROT_EXEC);
116         if (unlikely(addr & ~PAGE_MASK)) {
117                 up_write(&current->mm->mmap_sem);
118                 return addr;
119         }
120         err = install_special_mapping(current->mm, addr, len,
121                                       VM_DONTEXPAND | VM_READ | VM_EXEC |
122                                       VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
123                                       PAGE_READONLY_EXEC,
124                                       sysenter_pages);
125         if (likely(err == 0)) {
126                 current->mm->context.vdso = (void *)addr;
127                 ti->sysenter_return = &SYSENTER_RETURN_OFFSET + addr;
128         }
129         up_write(&current->mm->mmap_sem);
130         return err;
131 }
132
133 int in_gate_area_no_task(unsigned long addr)
134 {
135         return 0;
136 }
137
138 int in_gate_area(struct task_struct *task, unsigned long addr)
139 {
140         return 0;
141 }
142
143 struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
144 {
145         return NULL;
146 }