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>
28 * Should the kernel map a VDSO page into processes and pass its
29 * address down to glibc upon exec()?
31 #ifdef CONFIG_PARAVIRT
32 unsigned int __read_mostly vdso_enabled = 0;
34 unsigned int __read_mostly vdso_enabled = 1;
37 EXPORT_SYMBOL_GPL(vdso_enabled);
39 static int __init vdso_setup(char *s)
41 vdso_enabled = simple_strtoul(s, NULL, 0);
46 __setup("vdso=", vdso_setup);
48 extern asmlinkage void sysenter_entry(void);
50 #ifdef CONFIG_COMPAT_VDSO
51 static __init void reloc_symtab(Elf32_Ehdr *ehdr,
52 unsigned offset, unsigned size)
54 Elf32_Sym *sym = (void *)ehdr + offset;
55 unsigned nsym = size / sizeof(*sym);
58 for(i = 0; i < nsym; i++, sym++) {
59 if (sym->st_shndx == SHN_UNDEF ||
60 sym->st_shndx == SHN_ABS)
63 if (sym->st_shndx > SHN_LORESERVE) {
64 printk(KERN_INFO "VDSO: unexpected st_shndx %x\n",
69 switch(ELF_ST_TYPE(sym->st_info)) {
74 sym->st_value += VDSO_HIGH_BASE;
79 static __init void reloc_dyn(Elf32_Ehdr *ehdr, unsigned offset)
81 Elf32_Dyn *dyn = (void *)ehdr + offset;
83 for(; dyn->d_tag != DT_NULL; dyn++)
98 case DT_ADDRRNGLO ... DT_ADDRRNGHI:
99 /* definitely pointers needing relocation */
100 dyn->d_un.d_ptr += VDSO_HIGH_BASE;
103 case DT_ENCODING ... OLD_DT_LOOS-1:
104 case DT_LOOS ... DT_HIOS-1:
105 /* Tags above DT_ENCODING are pointers if
107 if (dyn->d_tag >= DT_ENCODING &&
108 (dyn->d_tag & 1) == 0)
109 dyn->d_un.d_ptr += VDSO_HIGH_BASE;
117 case DT_VALRNGLO ... DT_VALRNGHI:
118 /* definitely not pointers */
121 case OLD_DT_LOOS ... DT_LOOS-1:
122 case DT_HIOS ... DT_VALRNGLO-1:
124 if (dyn->d_tag > DT_ENCODING)
125 printk(KERN_INFO "VDSO: unexpected DT_tag %x\n",
131 static __init void relocate_vdso(Elf32_Ehdr *ehdr)
137 BUG_ON(memcmp(ehdr->e_ident, ELFMAG, 4) != 0 ||
138 !elf_check_arch(ehdr) ||
139 ehdr->e_type != ET_DYN);
141 ehdr->e_entry += VDSO_HIGH_BASE;
144 phdr = (void *)ehdr + ehdr->e_phoff;
145 for (i = 0; i < ehdr->e_phnum; i++) {
146 phdr[i].p_vaddr += VDSO_HIGH_BASE;
148 /* relocate dynamic stuff */
149 if (phdr[i].p_type == PT_DYNAMIC)
150 reloc_dyn(ehdr, phdr[i].p_offset);
153 /* rebase sections */
154 shdr = (void *)ehdr + ehdr->e_shoff;
155 for(i = 0; i < ehdr->e_shnum; i++) {
156 if (!(shdr[i].sh_flags & SHF_ALLOC))
159 shdr[i].sh_addr += VDSO_HIGH_BASE;
161 if (shdr[i].sh_type == SHT_SYMTAB ||
162 shdr[i].sh_type == SHT_DYNSYM)
163 reloc_symtab(ehdr, shdr[i].sh_offset,
168 static inline void relocate_vdso(Elf32_Ehdr *ehdr)
171 #endif /* COMPAT_VDSO */
173 void enable_sep_cpu(void)
176 struct tss_struct *tss = &per_cpu(init_tss, cpu);
178 if (!boot_cpu_has(X86_FEATURE_SEP)) {
183 tss->ss1 = __KERNEL_CS;
184 tss->esp1 = sizeof(struct tss_struct) + (unsigned long) tss;
185 wrmsr(MSR_IA32_SYSENTER_CS, __KERNEL_CS, 0);
186 wrmsr(MSR_IA32_SYSENTER_ESP, tss->esp1, 0);
187 wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long) sysenter_entry, 0);
192 * These symbols are defined by vsyscall.o to mark the bounds
193 * of the ELF DSO images included therein.
195 extern const char vsyscall_int80_start, vsyscall_int80_end;
196 extern const char vsyscall_sysenter_start, vsyscall_sysenter_end;
197 static struct page *syscall_pages[1];
199 int __init sysenter_setup(void)
201 void *syscall_page = (void *)get_zeroed_page(GFP_ATOMIC);
202 const void *vsyscall;
205 syscall_pages[0] = virt_to_page(syscall_page);
207 #ifdef CONFIG_COMPAT_VDSO
208 __set_fixmap(FIX_VDSO, __pa(syscall_page), PAGE_READONLY_EXEC);
209 printk("Compat vDSO mapped to %08lx.\n", __fix_to_virt(FIX_VDSO));
212 if (!boot_cpu_has(X86_FEATURE_SEP)) {
213 vsyscall = &vsyscall_int80_start;
214 vsyscall_len = &vsyscall_int80_end - &vsyscall_int80_start;
216 vsyscall = &vsyscall_sysenter_start;
217 vsyscall_len = &vsyscall_sysenter_end - &vsyscall_sysenter_start;
220 memcpy(syscall_page, vsyscall, vsyscall_len);
221 relocate_vdso(syscall_page);
226 /* Defined in vsyscall-sysenter.S */
227 extern void SYSENTER_RETURN;
229 #ifdef __HAVE_ARCH_GATE_AREA
230 /* Setup a VMA at program startup for the vsyscall page */
231 int arch_setup_additional_pages(struct linux_binprm *bprm, int exstack)
233 struct mm_struct *mm = current->mm;
237 down_write(&mm->mmap_sem);
238 addr = get_unmapped_area(NULL, 0, PAGE_SIZE, 0, 0);
239 if (IS_ERR_VALUE(addr)) {
245 * MAYWRITE to allow gdb to COW and set breakpoints
247 * Make sure the vDSO gets into every core dump.
248 * Dumping its contents makes post-mortem fully interpretable later
249 * without matching up the same kernel and hardware config to see
250 * what PC values meant.
252 ret = install_special_mapping(mm, addr, PAGE_SIZE,
254 VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC|
260 current->mm->context.vdso = (void *)addr;
261 current_thread_info()->sysenter_return =
262 (void *)VDSO_SYM(&SYSENTER_RETURN);
264 up_write(&mm->mmap_sem);
268 const char *arch_vma_name(struct vm_area_struct *vma)
270 if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
275 struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
280 int in_gate_area(struct task_struct *task, unsigned long addr)
285 int in_gate_area_no_task(unsigned long addr)
289 #else /* !__HAVE_ARCH_GATE_AREA */
290 int arch_setup_additional_pages(struct linux_binprm *bprm, int exstack)
293 * If not creating userspace VMA, simply set vdso to point to
296 current->mm->context.vdso = (void *)VDSO_HIGH_BASE;
297 current_thread_info()->sysenter_return =
298 (void *)VDSO_SYM(&SYSENTER_RETURN);
302 #endif /* __HAVE_ARCH_GATE_AREA */