Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Set up the VMAs to tell the VM about the vDSO. |
| 3 | * Copyright 2007 Andi Kleen, SUSE Labs. |
| 4 | * Subject to the GPL, v.2 |
| 5 | */ |
| 6 | #include <linux/mm.h> |
Alexey Dobriyan | 4e950f6 | 2007-07-30 02:36:13 +0400 | [diff] [blame] | 7 | #include <linux/err.h> |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 8 | #include <linux/sched.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 9 | #include <linux/slab.h> |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 10 | #include <linux/init.h> |
| 11 | #include <linux/random.h> |
Jaswinder Singh Rajput | 3fa89ca | 2009-04-12 20:37:25 +0530 | [diff] [blame] | 12 | #include <linux/elf.h> |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 13 | #include <asm/vsyscall.h> |
| 14 | #include <asm/vgtod.h> |
| 15 | #include <asm/proto.h> |
Roland McGrath | 7f3646a | 2008-01-30 13:30:41 +0100 | [diff] [blame] | 16 | #include <asm/vdso.h> |
Andy Lutomirski | aafade2 | 2011-07-21 15:47:10 -0400 | [diff] [blame] | 17 | #include <asm/page.h> |
Andy Lutomirski | 18d0a6f | 2014-05-05 12:19:35 -0700 | [diff] [blame] | 18 | #include <asm/hpet.h> |
Roland McGrath | 7f3646a | 2008-01-30 13:30:41 +0100 | [diff] [blame] | 19 | |
Andy Lutomirski | b4b541a | 2014-03-17 23:22:08 +0100 | [diff] [blame] | 20 | #if defined(CONFIG_X86_64) |
Andy Lutomirski | 3d7ee96 | 2014-05-05 12:19:32 -0700 | [diff] [blame] | 21 | unsigned int __read_mostly vdso64_enabled = 1; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 22 | |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 23 | extern unsigned short vdso_sync_cpuid; |
Andy Lutomirski | b4b541a | 2014-03-17 23:22:08 +0100 | [diff] [blame] | 24 | #endif |
H. J. Lu | 1a21d4e | 2012-02-19 11:38:06 -0800 | [diff] [blame] | 25 | |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 26 | void __init init_vdso_image(const struct vdso_image *image) |
H. J. Lu | 1a21d4e | 2012-02-19 11:38:06 -0800 | [diff] [blame] | 27 | { |
H. J. Lu | 1a21d4e | 2012-02-19 11:38:06 -0800 | [diff] [blame] | 28 | int i; |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 29 | int npages = (image->size) / PAGE_SIZE; |
H. J. Lu | 1a21d4e | 2012-02-19 11:38:06 -0800 | [diff] [blame] | 30 | |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 31 | BUG_ON(image->size % PAGE_SIZE != 0); |
| 32 | for (i = 0; i < npages; i++) |
| 33 | image->pages[i] = virt_to_page(image->data + i*PAGE_SIZE); |
H. J. Lu | 1a21d4e | 2012-02-19 11:38:06 -0800 | [diff] [blame] | 34 | |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 35 | apply_alternatives((struct alt_instr *)(image->data + image->alt), |
| 36 | (struct alt_instr *)(image->data + image->alt + |
| 37 | image->alt_len)); |
H. J. Lu | 1a21d4e | 2012-02-19 11:38:06 -0800 | [diff] [blame] | 38 | } |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 39 | |
Andy Lutomirski | b4b541a | 2014-03-17 23:22:08 +0100 | [diff] [blame] | 40 | #if defined(CONFIG_X86_64) |
Andy Lutomirski | aafade2 | 2011-07-21 15:47:10 -0400 | [diff] [blame] | 41 | static int __init init_vdso(void) |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 42 | { |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 43 | init_vdso_image(&vdso_image_64); |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 44 | |
H. J. Lu | 1a21d4e | 2012-02-19 11:38:06 -0800 | [diff] [blame] | 45 | #ifdef CONFIG_X86_X32_ABI |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 46 | init_vdso_image(&vdso_image_x32); |
H. J. Lu | 1a21d4e | 2012-02-19 11:38:06 -0800 | [diff] [blame] | 47 | #endif |
| 48 | |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 49 | return 0; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 50 | } |
Andy Lutomirski | aafade2 | 2011-07-21 15:47:10 -0400 | [diff] [blame] | 51 | subsys_initcall(init_vdso); |
Andy Lutomirski | 18d0a6f | 2014-05-05 12:19:35 -0700 | [diff] [blame] | 52 | #endif |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 53 | |
| 54 | struct linux_binprm; |
| 55 | |
| 56 | /* Put the vdso above the (randomized) stack with another randomized offset. |
| 57 | This way there is no hole in the middle of address space. |
| 58 | To save memory make sure it is still in the same PTE as the stack top. |
Andy Lutomirski | 18d0a6f | 2014-05-05 12:19:35 -0700 | [diff] [blame] | 59 | This doesn't give that many random bits. |
| 60 | |
| 61 | Only used for the 64-bit and x32 vdsos. */ |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 62 | static unsigned long vdso_addr(unsigned long start, unsigned len) |
| 63 | { |
| 64 | unsigned long addr, end; |
| 65 | unsigned offset; |
| 66 | end = (start + PMD_SIZE - 1) & PMD_MASK; |
Ingo Molnar | d951734 | 2009-02-20 23:32:28 +0100 | [diff] [blame] | 67 | if (end >= TASK_SIZE_MAX) |
| 68 | end = TASK_SIZE_MAX; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 69 | end -= len; |
| 70 | /* This loses some more bits than a modulo, but is cheaper */ |
| 71 | offset = get_random_int() & (PTRS_PER_PTE - 1); |
| 72 | addr = start + (offset << PAGE_SHIFT); |
| 73 | if (addr >= end) |
| 74 | addr = end; |
Borislav Petkov | dfb09f9 | 2011-08-05 15:15:08 +0200 | [diff] [blame] | 75 | |
| 76 | /* |
| 77 | * page-align it here so that get_unmapped_area doesn't |
| 78 | * align it wrongfully again to the next page. addr can come in 4K |
| 79 | * unaligned here as a result of stack start randomization. |
| 80 | */ |
| 81 | addr = PAGE_ALIGN(addr); |
Michel Lespinasse | f9902472 | 2012-12-11 16:01:52 -0800 | [diff] [blame] | 82 | addr = align_vdso_addr(addr); |
Borislav Petkov | dfb09f9 | 2011-08-05 15:15:08 +0200 | [diff] [blame] | 83 | |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 84 | return addr; |
| 85 | } |
| 86 | |
Andy Lutomirski | 18d0a6f | 2014-05-05 12:19:35 -0700 | [diff] [blame] | 87 | static int map_vdso(const struct vdso_image *image, bool calculate_addr) |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 88 | { |
| 89 | struct mm_struct *mm = current->mm; |
Andy Lutomirski | 18d0a6f | 2014-05-05 12:19:35 -0700 | [diff] [blame] | 90 | struct vm_area_struct *vma; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 91 | unsigned long addr; |
Andy Lutomirski | 18d0a6f | 2014-05-05 12:19:35 -0700 | [diff] [blame] | 92 | int ret = 0; |
Andy Lutomirski | 1e844fb | 2014-05-19 15:58:31 -0700 | [diff] [blame^] | 93 | static struct page *no_pages[] = {NULL}; |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 94 | |
Andy Lutomirski | 18d0a6f | 2014-05-05 12:19:35 -0700 | [diff] [blame] | 95 | if (calculate_addr) { |
| 96 | addr = vdso_addr(current->mm->start_stack, |
| 97 | image->sym_end_mapping); |
| 98 | } else { |
| 99 | addr = 0; |
| 100 | } |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 101 | |
| 102 | down_write(&mm->mmap_sem); |
Andy Lutomirski | 18d0a6f | 2014-05-05 12:19:35 -0700 | [diff] [blame] | 103 | |
| 104 | addr = get_unmapped_area(NULL, addr, image->sym_end_mapping, 0, 0); |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 105 | if (IS_ERR_VALUE(addr)) { |
| 106 | ret = addr; |
| 107 | goto up_fail; |
| 108 | } |
| 109 | |
Andy Lutomirski | 6f121e5 | 2014-05-05 12:19:34 -0700 | [diff] [blame] | 110 | current->mm->context.vdso = (void __user *)addr; |
Peter Zijlstra | f7b6eb3 | 2009-06-05 14:04:51 +0200 | [diff] [blame] | 111 | |
Andy Lutomirski | 18d0a6f | 2014-05-05 12:19:35 -0700 | [diff] [blame] | 112 | /* |
| 113 | * MAYWRITE to allow gdb to COW and set breakpoints |
| 114 | */ |
| 115 | ret = install_special_mapping(mm, |
| 116 | addr, |
| 117 | image->size, |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 118 | VM_READ|VM_EXEC| |
Jason Baron | 909af76 | 2012-03-23 15:02:51 -0700 | [diff] [blame] | 119 | VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC, |
Andy Lutomirski | 18d0a6f | 2014-05-05 12:19:35 -0700 | [diff] [blame] | 120 | image->pages); |
| 121 | |
| 122 | if (ret) |
| 123 | goto up_fail; |
| 124 | |
| 125 | vma = _install_special_mapping(mm, |
| 126 | addr + image->size, |
| 127 | image->sym_end_mapping - image->size, |
| 128 | VM_READ, |
Andy Lutomirski | 1e844fb | 2014-05-19 15:58:31 -0700 | [diff] [blame^] | 129 | no_pages); |
Andy Lutomirski | 18d0a6f | 2014-05-05 12:19:35 -0700 | [diff] [blame] | 130 | |
| 131 | if (IS_ERR(vma)) { |
| 132 | ret = PTR_ERR(vma); |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 133 | goto up_fail; |
Peter Zijlstra | f7b6eb3 | 2009-06-05 14:04:51 +0200 | [diff] [blame] | 134 | } |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 135 | |
Andy Lutomirski | 18d0a6f | 2014-05-05 12:19:35 -0700 | [diff] [blame] | 136 | if (image->sym_vvar_page) |
| 137 | ret = remap_pfn_range(vma, |
| 138 | addr + image->sym_vvar_page, |
| 139 | __pa_symbol(&__vvar_page) >> PAGE_SHIFT, |
| 140 | PAGE_SIZE, |
| 141 | PAGE_READONLY); |
| 142 | |
| 143 | if (ret) |
| 144 | goto up_fail; |
| 145 | |
| 146 | #ifdef CONFIG_HPET_TIMER |
| 147 | if (hpet_address && image->sym_hpet_page) { |
| 148 | ret = io_remap_pfn_range(vma, |
| 149 | addr + image->sym_hpet_page, |
| 150 | hpet_address >> PAGE_SHIFT, |
| 151 | PAGE_SIZE, |
| 152 | pgprot_noncached(PAGE_READONLY)); |
| 153 | |
| 154 | if (ret) |
| 155 | goto up_fail; |
| 156 | } |
| 157 | #endif |
| 158 | |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 159 | up_fail: |
Andy Lutomirski | 18d0a6f | 2014-05-05 12:19:35 -0700 | [diff] [blame] | 160 | if (ret) |
| 161 | current->mm->context.vdso = NULL; |
| 162 | |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 163 | up_write(&mm->mmap_sem); |
| 164 | return ret; |
| 165 | } |
| 166 | |
Andy Lutomirski | 18d0a6f | 2014-05-05 12:19:35 -0700 | [diff] [blame] | 167 | #if defined(CONFIG_X86_32) || defined(CONFIG_COMPAT) |
| 168 | static int load_vdso32(void) |
H. J. Lu | 1a21d4e | 2012-02-19 11:38:06 -0800 | [diff] [blame] | 169 | { |
Andy Lutomirski | 18d0a6f | 2014-05-05 12:19:35 -0700 | [diff] [blame] | 170 | int ret; |
H. J. Lu | 1a21d4e | 2012-02-19 11:38:06 -0800 | [diff] [blame] | 171 | |
Andy Lutomirski | 18d0a6f | 2014-05-05 12:19:35 -0700 | [diff] [blame] | 172 | if (vdso32_enabled != 1) /* Other values all mean "disabled" */ |
| 173 | return 0; |
| 174 | |
| 175 | ret = map_vdso(selected_vdso32, false); |
| 176 | if (ret) |
| 177 | return ret; |
| 178 | |
| 179 | if (selected_vdso32->sym_VDSO32_SYSENTER_RETURN) |
| 180 | current_thread_info()->sysenter_return = |
| 181 | current->mm->context.vdso + |
| 182 | selected_vdso32->sym_VDSO32_SYSENTER_RETURN; |
| 183 | |
| 184 | return 0; |
H. J. Lu | 1a21d4e | 2012-02-19 11:38:06 -0800 | [diff] [blame] | 185 | } |
| 186 | #endif |
| 187 | |
Andy Lutomirski | 18d0a6f | 2014-05-05 12:19:35 -0700 | [diff] [blame] | 188 | #ifdef CONFIG_X86_64 |
| 189 | int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) |
| 190 | { |
| 191 | if (!vdso64_enabled) |
| 192 | return 0; |
| 193 | |
| 194 | return map_vdso(&vdso_image_64, true); |
| 195 | } |
| 196 | |
| 197 | #ifdef CONFIG_COMPAT |
| 198 | int compat_arch_setup_additional_pages(struct linux_binprm *bprm, |
| 199 | int uses_interp) |
| 200 | { |
| 201 | #ifdef CONFIG_X86_X32_ABI |
| 202 | if (test_thread_flag(TIF_X32)) { |
| 203 | if (!vdso64_enabled) |
| 204 | return 0; |
| 205 | |
| 206 | return map_vdso(&vdso_image_x32, true); |
| 207 | } |
| 208 | #endif |
| 209 | |
| 210 | return load_vdso32(); |
| 211 | } |
| 212 | #endif |
| 213 | #else |
| 214 | int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) |
| 215 | { |
| 216 | return load_vdso32(); |
| 217 | } |
| 218 | #endif |
| 219 | |
| 220 | #ifdef CONFIG_X86_64 |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 221 | static __init int vdso_setup(char *s) |
| 222 | { |
Andy Lutomirski | 3d7ee96 | 2014-05-05 12:19:32 -0700 | [diff] [blame] | 223 | vdso64_enabled = simple_strtoul(s, NULL, 0); |
Andi Kleen | 2aae950 | 2007-07-21 17:10:01 +0200 | [diff] [blame] | 224 | return 0; |
| 225 | } |
| 226 | __setup("vdso=", vdso_setup); |
Andy Lutomirski | b4b541a | 2014-03-17 23:22:08 +0100 | [diff] [blame] | 227 | #endif |