blob: 454d12df59ea758193c38cfd8cd35fa72283d13c [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 */
Andi Kleen3bbf5472006-12-07 02:14:08 +010030#ifdef CONFIG_PARAVIRT
31unsigned int __read_mostly vdso_enabled = 0;
32#else
Ingo Molnare6e54942006-06-27 02:53:50 -070033unsigned int __read_mostly vdso_enabled = 1;
Andi Kleen3bbf5472006-12-07 02:14:08 +010034#endif
Ingo Molnare6e54942006-06-27 02:53:50 -070035
36EXPORT_SYMBOL_GPL(vdso_enabled);
37
38static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070047extern asmlinkage void sysenter_entry(void);
48
Li Shaohua6fe940d2005-06-25 14:54:53 -070049void enable_sep_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 int cpu = get_cpu();
52 struct tss_struct *tss = &per_cpu(init_tss, cpu);
53
Li Shaohua6fe940d2005-06-25 14:54:53 -070054 if (!boot_cpu_has(X86_FEATURE_SEP)) {
55 put_cpu();
56 return;
57 }
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 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 */
71extern const char vsyscall_int80_start, vsyscall_int80_end;
72extern const char vsyscall_sysenter_start, vsyscall_sysenter_end;
Ingo Molnare6e54942006-06-27 02:53:50 -070073static void *syscall_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Li Shaohua6fe940d2005-06-25 14:54:53 -070075int __init sysenter_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Ingo Molnare6e54942006-06-27 02:53:50 -070077 syscall_page = (void *)get_zeroed_page(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Ingo Molnare6e54942006-06-27 02:53:50 -070079#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 if (!boot_cpu_has(X86_FEATURE_SEP)) {
Ingo Molnare6e54942006-06-27 02:53:50 -070090 memcpy(syscall_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 &vsyscall_int80_start,
92 &vsyscall_int80_end - &vsyscall_int80_start);
93 return 0;
94 }
95
Ingo Molnare6e54942006-06-27 02:53:50 -070096 memcpy(syscall_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 &vsyscall_sysenter_start,
98 &vsyscall_sysenter_end - &vsyscall_sysenter_start);
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return 0;
101}
Ingo Molnare6e54942006-06-27 02:53:50 -0700102
Roland McGratha1f3bb92007-01-26 00:56:46 -0800103#ifndef CONFIG_COMPAT_VDSO
Ingo Molnare6e54942006-06-27 02:53:50 -0700104static struct page *syscall_nopage(struct vm_area_struct *vma,
105 unsigned long adr, int *type)
106{
107 struct page *p = virt_to_page(adr - vma->vm_start + syscall_page);
108 get_page(p);
109 return p;
110}
111
112/* Prevent VMA merging */
113static void syscall_vma_close(struct vm_area_struct *vma)
114{
115}
116
117static struct vm_operations_struct syscall_vm_ops = {
118 .close = syscall_vma_close,
119 .nopage = syscall_nopage,
120};
121
122/* Defined in vsyscall-sysenter.S */
123extern void SYSENTER_RETURN;
124
125/* Setup a VMA at program startup for the vsyscall page */
126int arch_setup_additional_pages(struct linux_binprm *bprm, int exstack)
127{
128 struct vm_area_struct *vma;
129 struct mm_struct *mm = current->mm;
130 unsigned long addr;
131 int ret;
132
133 down_write(&mm->mmap_sem);
134 addr = get_unmapped_area(NULL, 0, PAGE_SIZE, 0, 0);
135 if (IS_ERR_VALUE(addr)) {
136 ret = addr;
137 goto up_fail;
138 }
139
Christoph Lametere94b1762006-12-06 20:33:17 -0800140 vma = kmem_cache_zalloc(vm_area_cachep, GFP_KERNEL);
Ingo Molnare6e54942006-06-27 02:53:50 -0700141 if (!vma) {
142 ret = -ENOMEM;
143 goto up_fail;
144 }
145
146 vma->vm_start = addr;
147 vma->vm_end = addr + PAGE_SIZE;
148 /* MAYWRITE to allow gdb to COW and set breakpoints */
149 vma->vm_flags = VM_READ|VM_EXEC|VM_MAYREAD|VM_MAYEXEC|VM_MAYWRITE;
150 vma->vm_flags |= mm->def_flags;
151 vma->vm_page_prot = protection_map[vma->vm_flags & 7];
152 vma->vm_ops = &syscall_vm_ops;
153 vma->vm_mm = mm;
154
155 ret = insert_vm_struct(mm, vma);
pageexec@freemail.hu79bc79b2006-06-28 20:44:16 +0200156 if (unlikely(ret)) {
157 kmem_cache_free(vm_area_cachep, vma);
158 goto up_fail;
159 }
Ingo Molnare6e54942006-06-27 02:53:50 -0700160
161 current->mm->context.vdso = (void *)addr;
162 current_thread_info()->sysenter_return =
163 (void *)VDSO_SYM(&SYSENTER_RETURN);
164 mm->total_vm++;
165up_fail:
166 up_write(&mm->mmap_sem);
167 return ret;
Ingo Molnare6e54942006-06-27 02:53:50 -0700168}
169
170const char *arch_vma_name(struct vm_area_struct *vma)
171{
172 if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
173 return "[vdso]";
174 return NULL;
175}
176
177struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
178{
179 return NULL;
180}
181
182int in_gate_area(struct task_struct *task, unsigned long addr)
183{
184 return 0;
185}
186
187int in_gate_area_no_task(unsigned long addr)
188{
189 return 0;
190}
Roland McGratha1f3bb92007-01-26 00:56:46 -0800191#endif