blob: 713ba39d32c66db0e2ad4ca0580088ffccef785f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/i386/kernel/sysenter.c
3 *
4 * (C) Copyright 2002 Linus Torvalds
Ingo Molnare6e54942006-06-27 02:53:50 -07005 * Portions based on the vdso-randomization code from exec-shield:
6 * Copyright(C) 2005-2006, Red Hat, Inc., Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
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>
Ingo Molnare6e54942006-06-27 02:53:50 -070018#include <linux/mm.h>
19#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include <asm/cpufeature.h>
22#include <asm/msr.h>
23#include <asm/pgtable.h>
24#include <asm/unistd.h>
25
Ingo Molnare6e54942006-06-27 02:53:50 -070026/*
27 * Should the kernel map a VDSO page into processes and pass its
28 * address down to glibc upon exec()?
29 */
30unsigned int __read_mostly vdso_enabled = 1;
31
32EXPORT_SYMBOL_GPL(vdso_enabled);
33
34static int __init vdso_setup(char *s)
35{
36 vdso_enabled = simple_strtoul(s, NULL, 0);
37
38 return 1;
39}
40
41__setup("vdso=", vdso_setup);
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043extern asmlinkage void sysenter_entry(void);
44
Li Shaohua6fe940d2005-06-25 14:54:53 -070045void enable_sep_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 int cpu = get_cpu();
48 struct tss_struct *tss = &per_cpu(init_tss, cpu);
49
Li Shaohua6fe940d2005-06-25 14:54:53 -070050 if (!boot_cpu_has(X86_FEATURE_SEP)) {
51 put_cpu();
52 return;
53 }
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 tss->ss1 = __KERNEL_CS;
56 tss->esp1 = sizeof(struct tss_struct) + (unsigned long) tss;
57 wrmsr(MSR_IA32_SYSENTER_CS, __KERNEL_CS, 0);
58 wrmsr(MSR_IA32_SYSENTER_ESP, tss->esp1, 0);
59 wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long) sysenter_entry, 0);
60 put_cpu();
61}
62
63/*
64 * These symbols are defined by vsyscall.o to mark the bounds
65 * of the ELF DSO images included therein.
66 */
67extern const char vsyscall_int80_start, vsyscall_int80_end;
68extern const char vsyscall_sysenter_start, vsyscall_sysenter_end;
Ingo Molnare6e54942006-06-27 02:53:50 -070069static void *syscall_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Li Shaohua6fe940d2005-06-25 14:54:53 -070071int __init sysenter_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Ingo Molnare6e54942006-06-27 02:53:50 -070073 syscall_page = (void *)get_zeroed_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Ingo Molnare6e54942006-06-27 02:53:50 -070075#ifdef CONFIG_COMPAT_VDSO
76 __set_fixmap(FIX_VDSO, __pa(syscall_page), PAGE_READONLY);
77 printk("Compat vDSO mapped to %08lx.\n", __fix_to_virt(FIX_VDSO));
78#else
79 /*
80 * In the non-compat case the ELF coredumping code needs the fixmap:
81 */
82 __set_fixmap(FIX_VDSO, __pa(syscall_page), PAGE_KERNEL_RO);
83#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 if (!boot_cpu_has(X86_FEATURE_SEP)) {
Ingo Molnare6e54942006-06-27 02:53:50 -070086 memcpy(syscall_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 &vsyscall_int80_start,
88 &vsyscall_int80_end - &vsyscall_int80_start);
89 return 0;
90 }
91
Ingo Molnare6e54942006-06-27 02:53:50 -070092 memcpy(syscall_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 &vsyscall_sysenter_start,
94 &vsyscall_sysenter_end - &vsyscall_sysenter_start);
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return 0;
97}
Ingo Molnare6e54942006-06-27 02:53:50 -070098
99static struct page *syscall_nopage(struct vm_area_struct *vma,
100 unsigned long adr, int *type)
101{
102 struct page *p = virt_to_page(adr - vma->vm_start + syscall_page);
103 get_page(p);
104 return p;
105}
106
107/* Prevent VMA merging */
108static void syscall_vma_close(struct vm_area_struct *vma)
109{
110}
111
112static struct vm_operations_struct syscall_vm_ops = {
113 .close = syscall_vma_close,
114 .nopage = syscall_nopage,
115};
116
117/* Defined in vsyscall-sysenter.S */
118extern void SYSENTER_RETURN;
119
120/* Setup a VMA at program startup for the vsyscall page */
121int arch_setup_additional_pages(struct linux_binprm *bprm, int exstack)
122{
123 struct vm_area_struct *vma;
124 struct mm_struct *mm = current->mm;
125 unsigned long addr;
126 int ret;
127
128 down_write(&mm->mmap_sem);
129 addr = get_unmapped_area(NULL, 0, PAGE_SIZE, 0, 0);
130 if (IS_ERR_VALUE(addr)) {
131 ret = addr;
132 goto up_fail;
133 }
134
135 vma = kmem_cache_zalloc(vm_area_cachep, SLAB_KERNEL);
136 if (!vma) {
137 ret = -ENOMEM;
138 goto up_fail;
139 }
140
141 vma->vm_start = addr;
142 vma->vm_end = addr + PAGE_SIZE;
143 /* MAYWRITE to allow gdb to COW and set breakpoints */
144 vma->vm_flags = VM_READ|VM_EXEC|VM_MAYREAD|VM_MAYEXEC|VM_MAYWRITE;
145 vma->vm_flags |= mm->def_flags;
146 vma->vm_page_prot = protection_map[vma->vm_flags & 7];
147 vma->vm_ops = &syscall_vm_ops;
148 vma->vm_mm = mm;
149
150 ret = insert_vm_struct(mm, vma);
pageexec@freemail.hu79bc79b2006-06-28 20:44:16 +0200151 if (unlikely(ret)) {
152 kmem_cache_free(vm_area_cachep, vma);
153 goto up_fail;
154 }
Ingo Molnare6e54942006-06-27 02:53:50 -0700155
156 current->mm->context.vdso = (void *)addr;
157 current_thread_info()->sysenter_return =
158 (void *)VDSO_SYM(&SYSENTER_RETURN);
159 mm->total_vm++;
160up_fail:
161 up_write(&mm->mmap_sem);
162 return ret;
Ingo Molnare6e54942006-06-27 02:53:50 -0700163}
164
165const char *arch_vma_name(struct vm_area_struct *vma)
166{
167 if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
168 return "[vdso]";
169 return NULL;
170}
171
172struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
173{
174 return NULL;
175}
176
177int in_gate_area(struct task_struct *task, unsigned long addr)
178{
179 return 0;
180}
181
182int in_gate_area_no_task(unsigned long addr)
183{
184 return 0;
185}