2 * linux/arch/i386/kernel/sysenter.c
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
8 * This file contains the needed initializations to support sysenter.
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>
19 #include <linux/module.h>
21 #include <asm/cpufeature.h>
23 #include <asm/pgtable.h>
24 #include <asm/unistd.h>
26 #include <asm/tlbflush.h>
34 #ifdef CONFIG_COMPAT_VDSO
35 #define VDSO_DEFAULT VDSO_COMPAT
37 #define VDSO_DEFAULT VDSO_ENABLED
41 * Should the kernel map a VDSO page into processes and pass its
42 * address down to glibc upon exec()?
44 unsigned int __read_mostly vdso_enabled = VDSO_DEFAULT;
46 EXPORT_SYMBOL_GPL(vdso_enabled);
48 static int __init vdso_setup(char *s)
50 vdso_enabled = simple_strtoul(s, NULL, 0);
55 __setup("vdso=", vdso_setup);
57 extern asmlinkage void sysenter_entry(void);
59 static __init void reloc_symtab(Elf32_Ehdr *ehdr,
60 unsigned offset, unsigned size)
62 Elf32_Sym *sym = (void *)ehdr + offset;
63 unsigned nsym = size / sizeof(*sym);
66 for(i = 0; i < nsym; i++, sym++) {
67 if (sym->st_shndx == SHN_UNDEF ||
68 sym->st_shndx == SHN_ABS)
71 if (sym->st_shndx > SHN_LORESERVE) {
72 printk(KERN_INFO "VDSO: unexpected st_shndx %x\n",
77 switch(ELF_ST_TYPE(sym->st_info)) {
82 sym->st_value += VDSO_HIGH_BASE;
87 static __init void reloc_dyn(Elf32_Ehdr *ehdr, unsigned offset)
89 Elf32_Dyn *dyn = (void *)ehdr + offset;
91 for(; dyn->d_tag != DT_NULL; dyn++)
106 case DT_ADDRRNGLO ... DT_ADDRRNGHI:
107 /* definitely pointers needing relocation */
108 dyn->d_un.d_ptr += VDSO_HIGH_BASE;
111 case DT_ENCODING ... OLD_DT_LOOS-1:
112 case DT_LOOS ... DT_HIOS-1:
113 /* Tags above DT_ENCODING are pointers if
115 if (dyn->d_tag >= DT_ENCODING &&
116 (dyn->d_tag & 1) == 0)
117 dyn->d_un.d_ptr += VDSO_HIGH_BASE;
125 case DT_VALRNGLO ... DT_VALRNGHI:
126 /* definitely not pointers */
129 case OLD_DT_LOOS ... DT_LOOS-1:
130 case DT_HIOS ... DT_VALRNGLO-1:
132 if (dyn->d_tag > DT_ENCODING)
133 printk(KERN_INFO "VDSO: unexpected DT_tag %x\n",
139 static __init void relocate_vdso(Elf32_Ehdr *ehdr)
145 BUG_ON(memcmp(ehdr->e_ident, ELFMAG, 4) != 0 ||
146 !elf_check_arch(ehdr) ||
147 ehdr->e_type != ET_DYN);
149 ehdr->e_entry += VDSO_HIGH_BASE;
152 phdr = (void *)ehdr + ehdr->e_phoff;
153 for (i = 0; i < ehdr->e_phnum; i++) {
154 phdr[i].p_vaddr += VDSO_HIGH_BASE;
156 /* relocate dynamic stuff */
157 if (phdr[i].p_type == PT_DYNAMIC)
158 reloc_dyn(ehdr, phdr[i].p_offset);
161 /* rebase sections */
162 shdr = (void *)ehdr + ehdr->e_shoff;
163 for(i = 0; i < ehdr->e_shnum; i++) {
164 if (!(shdr[i].sh_flags & SHF_ALLOC))
167 shdr[i].sh_addr += VDSO_HIGH_BASE;
169 if (shdr[i].sh_type == SHT_SYMTAB ||
170 shdr[i].sh_type == SHT_DYNSYM)
171 reloc_symtab(ehdr, shdr[i].sh_offset,
176 void enable_sep_cpu(void)
179 struct tss_struct *tss = &per_cpu(init_tss, cpu);
181 if (!boot_cpu_has(X86_FEATURE_SEP)) {
186 tss->x86_tss.ss1 = __KERNEL_CS;
187 tss->x86_tss.esp1 = sizeof(struct tss_struct) + (unsigned long) tss;
188 wrmsr(MSR_IA32_SYSENTER_CS, __KERNEL_CS, 0);
189 wrmsr(MSR_IA32_SYSENTER_ESP, tss->x86_tss.esp1, 0);
190 wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long) sysenter_entry, 0);
194 static struct vm_area_struct gate_vma;
196 static int __init gate_vma_init(void)
198 gate_vma.vm_mm = NULL;
199 gate_vma.vm_start = FIXADDR_USER_START;
200 gate_vma.vm_end = FIXADDR_USER_END;
201 gate_vma.vm_flags = VM_READ | VM_MAYREAD | VM_EXEC | VM_MAYEXEC;
202 gate_vma.vm_page_prot = __P101;
204 * Make sure the vDSO gets into every core dump.
205 * Dumping its contents makes post-mortem fully interpretable later
206 * without matching up the same kernel and hardware config to see
207 * what PC values meant.
209 gate_vma.vm_flags |= VM_ALWAYSDUMP;
214 * These symbols are defined by vsyscall.o to mark the bounds
215 * of the ELF DSO images included therein.
217 extern const char vsyscall_int80_start, vsyscall_int80_end;
218 extern const char vsyscall_sysenter_start, vsyscall_sysenter_end;
219 static struct page *syscall_pages[1];
221 static void map_compat_vdso(int map)
223 static int vdso_mapped;
225 if (map == vdso_mapped)
230 __set_fixmap(FIX_VDSO, page_to_pfn(syscall_pages[0]) << PAGE_SHIFT,
231 map ? PAGE_READONLY_EXEC : PAGE_NONE);
233 /* flush stray tlbs */
237 int __init sysenter_setup(void)
239 void *syscall_page = (void *)get_zeroed_page(GFP_ATOMIC);
240 const void *vsyscall;
243 syscall_pages[0] = virt_to_page(syscall_page);
247 printk("Compat vDSO mapped to %08lx.\n", __fix_to_virt(FIX_VDSO));
249 if (!boot_cpu_has(X86_FEATURE_SEP)) {
250 vsyscall = &vsyscall_int80_start;
251 vsyscall_len = &vsyscall_int80_end - &vsyscall_int80_start;
253 vsyscall = &vsyscall_sysenter_start;
254 vsyscall_len = &vsyscall_sysenter_end - &vsyscall_sysenter_start;
257 memcpy(syscall_page, vsyscall, vsyscall_len);
258 relocate_vdso(syscall_page);
263 /* Defined in vsyscall-sysenter.S */
264 extern void SYSENTER_RETURN;
266 /* Setup a VMA at program startup for the vsyscall page */
267 int arch_setup_additional_pages(struct linux_binprm *bprm, int exstack)
269 struct mm_struct *mm = current->mm;
274 down_write(&mm->mmap_sem);
276 /* Test compat mode once here, in case someone
277 changes it via sysctl */
278 compat = (vdso_enabled == VDSO_COMPAT);
280 map_compat_vdso(compat);
283 addr = VDSO_HIGH_BASE;
285 addr = get_unmapped_area(NULL, 0, PAGE_SIZE, 0, 0);
286 if (IS_ERR_VALUE(addr)) {
292 * MAYWRITE to allow gdb to COW and set breakpoints
294 * Make sure the vDSO gets into every core dump.
295 * Dumping its contents makes post-mortem fully
296 * interpretable later without matching up the same
297 * kernel and hardware config to see what PC values
300 ret = install_special_mapping(mm, addr, PAGE_SIZE,
302 VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC|
310 current->mm->context.vdso = (void *)addr;
311 current_thread_info()->sysenter_return =
312 (void *)VDSO_SYM(&SYSENTER_RETURN);
315 up_write(&mm->mmap_sem);
320 const char *arch_vma_name(struct vm_area_struct *vma)
322 if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
327 struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
329 struct mm_struct *mm = tsk->mm;
331 /* Check to see if this task was created in compat vdso mode */
332 if (mm && mm->context.vdso == (void *)VDSO_HIGH_BASE)
337 int in_gate_area(struct task_struct *task, unsigned long addr)
342 int in_gate_area_no_task(unsigned long addr)