1 // SPDX-License-Identifier: GPL-2.0-or-later 2 3 /* 4 * Copyright (C) 2004 Benjamin Herrenschmidt, IBM Corp. 5 * <benh@kernel.crashing.org> 6 */ 7 8 #include <linux/errno.h> 9 #include <linux/sched.h> 10 #include <linux/kernel.h> 11 #include <linux/mm.h> 12 #include <linux/smp.h> 13 #include <linux/stddef.h> 14 #include <linux/unistd.h> 15 #include <linux/slab.h> 16 #include <linux/user.h> 17 #include <linux/elf.h> 18 #include <linux/security.h> 19 #include <linux/syscalls.h> 20 #include <linux/time_namespace.h> 21 #include <vdso/datapage.h> 22 23 #include <asm/syscall.h> 24 #include <asm/processor.h> 25 #include <asm/mmu.h> 26 #include <asm/mmu_context.h> 27 #include <asm/machdep.h> 28 #include <asm/cputable.h> 29 #include <asm/sections.h> 30 #include <asm/firmware.h> 31 #include <asm/vdso.h> 32 #include <asm/vdso_datapage.h> 33 #include <asm/setup.h> 34 35 /* The alignment of the vDSO */ 36 #define VDSO_ALIGNMENT (1 << 16) 37 38 extern char vdso32_start, vdso32_end; 39 extern char vdso64_start, vdso64_end; 40 41 long sys_ni_syscall(void); 42 43 /* 44 * The vdso data page (aka. systemcfg for old ppc64 fans) is here. 45 * Once the early boot kernel code no longer needs to muck around 46 * with it, it will become dynamically allocated 47 */ 48 static union { 49 struct vdso_arch_data data; 50 u8 page[PAGE_SIZE]; 51 } vdso_data_store __page_aligned_data; 52 struct vdso_arch_data *vdso_data = &vdso_data_store.data; 53 54 enum vvar_pages { 55 VVAR_DATA_PAGE_OFFSET, 56 VVAR_TIMENS_PAGE_OFFSET, 57 VVAR_NR_PAGES, 58 }; 59 60 static int vdso_mremap(const struct vm_special_mapping *sm, struct vm_area_struct *new_vma, 61 unsigned long text_size) 62 { 63 unsigned long new_size = new_vma->vm_end - new_vma->vm_start; 64 65 if (new_size != text_size) 66 return -EINVAL; 67 68 current->mm->context.vdso = (void __user *)new_vma->vm_start; 69 70 return 0; 71 } 72 73 static int vdso32_mremap(const struct vm_special_mapping *sm, struct vm_area_struct *new_vma) 74 { 75 return vdso_mremap(sm, new_vma, &vdso32_end - &vdso32_start); 76 } 77 78 static int vdso64_mremap(const struct vm_special_mapping *sm, struct vm_area_struct *new_vma) 79 { 80 return vdso_mremap(sm, new_vma, &vdso64_end - &vdso64_start); 81 } 82 83 static void vdso_close(const struct vm_special_mapping *sm, struct vm_area_struct *vma) 84 { 85 struct mm_struct *mm = vma->vm_mm; 86 87 /* 88 * close() is called for munmap() but also for mremap(). In the mremap() 89 * case the vdso pointer has already been updated by the mremap() hook 90 * above, so it must not be set to NULL here. 91 */ 92 if (vma->vm_start != (unsigned long)mm->context.vdso) 93 return; 94 95 mm->context.vdso = NULL; 96 } 97 98 static vm_fault_t vvar_fault(const struct vm_special_mapping *sm, 99 struct vm_area_struct *vma, struct vm_fault *vmf); 100 101 static struct vm_special_mapping vvar_spec __ro_after_init = { 102 .name = "[vvar]", 103 .fault = vvar_fault, 104 }; 105 106 static struct vm_special_mapping vdso32_spec __ro_after_init = { 107 .name = "[vdso]", 108 .mremap = vdso32_mremap, 109 .close = vdso_close, 110 }; 111 112 static struct vm_special_mapping vdso64_spec __ro_after_init = { 113 .name = "[vdso]", 114 .mremap = vdso64_mremap, 115 .close = vdso_close, 116 }; 117 118 #ifdef CONFIG_TIME_NS 119 struct vdso_data *arch_get_vdso_data(void *vvar_page) 120 { 121 return ((struct vdso_arch_data *)vvar_page)->data; 122 } 123 124 /* 125 * The vvar mapping contains data for a specific time namespace, so when a task 126 * changes namespace we must unmap its vvar data for the old namespace. 127 * Subsequent faults will map in data for the new namespace. 128 * 129 * For more details see timens_setup_vdso_data(). 130 */ 131 int vdso_join_timens(struct task_struct *task, struct time_namespace *ns) 132 { 133 struct mm_struct *mm = task->mm; 134 VMA_ITERATOR(vmi, mm, 0); 135 struct vm_area_struct *vma; 136 137 mmap_read_lock(mm); 138 for_each_vma(vmi, vma) { 139 if (vma_is_special_mapping(vma, &vvar_spec)) 140 zap_vma_pages(vma); 141 } 142 mmap_read_unlock(mm); 143 144 return 0; 145 } 146 #endif 147 148 static vm_fault_t vvar_fault(const struct vm_special_mapping *sm, 149 struct vm_area_struct *vma, struct vm_fault *vmf) 150 { 151 struct page *timens_page = find_timens_vvar_page(vma); 152 unsigned long pfn; 153 154 switch (vmf->pgoff) { 155 case VVAR_DATA_PAGE_OFFSET: 156 if (timens_page) 157 pfn = page_to_pfn(timens_page); 158 else 159 pfn = virt_to_pfn(vdso_data); 160 break; 161 #ifdef CONFIG_TIME_NS 162 case VVAR_TIMENS_PAGE_OFFSET: 163 /* 164 * If a task belongs to a time namespace then a namespace 165 * specific VVAR is mapped with the VVAR_DATA_PAGE_OFFSET and 166 * the real VVAR page is mapped with the VVAR_TIMENS_PAGE_OFFSET 167 * offset. 168 * See also the comment near timens_setup_vdso_data(). 169 */ 170 if (!timens_page) 171 return VM_FAULT_SIGBUS; 172 pfn = virt_to_pfn(vdso_data); 173 break; 174 #endif /* CONFIG_TIME_NS */ 175 default: 176 return VM_FAULT_SIGBUS; 177 } 178 179 return vmf_insert_pfn(vma, vmf->address, pfn); 180 } 181 182 /* 183 * This is called from binfmt_elf, we create the special vma for the 184 * vDSO and insert it into the mm struct tree 185 */ 186 static int __arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) 187 { 188 unsigned long vdso_size, vdso_base, mappings_size; 189 struct vm_special_mapping *vdso_spec; 190 unsigned long vvar_size = VVAR_NR_PAGES * PAGE_SIZE; 191 struct mm_struct *mm = current->mm; 192 struct vm_area_struct *vma; 193 194 if (is_32bit_task()) { 195 vdso_spec = &vdso32_spec; 196 vdso_size = &vdso32_end - &vdso32_start; 197 } else { 198 vdso_spec = &vdso64_spec; 199 vdso_size = &vdso64_end - &vdso64_start; 200 } 201 202 mappings_size = vdso_size + vvar_size; 203 mappings_size += (VDSO_ALIGNMENT - 1) & PAGE_MASK; 204 205 /* 206 * Pick a base address for the vDSO in process space. 207 * Add enough to the size so that the result can be aligned. 208 */ 209 vdso_base = get_unmapped_area(NULL, 0, mappings_size, 0, 0); 210 if (IS_ERR_VALUE(vdso_base)) 211 return vdso_base; 212 213 /* Add required alignment. */ 214 vdso_base = ALIGN(vdso_base, VDSO_ALIGNMENT); 215 216 vma = _install_special_mapping(mm, vdso_base, vvar_size, 217 VM_READ | VM_MAYREAD | VM_IO | 218 VM_DONTDUMP | VM_PFNMAP, &vvar_spec); 219 if (IS_ERR(vma)) 220 return PTR_ERR(vma); 221 222 /* 223 * our vma flags don't have VM_WRITE so by default, the process isn't 224 * allowed to write those pages. 225 * gdb can break that with ptrace interface, and thus trigger COW on 226 * those pages but it's then your responsibility to never do that on 227 * the "data" page of the vDSO or you'll stop getting kernel updates 228 * and your nice userland gettimeofday will be totally dead. 229 * It's fine to use that for setting breakpoints in the vDSO code 230 * pages though. 231 */ 232 vma = _install_special_mapping(mm, vdso_base + vvar_size, vdso_size, 233 VM_READ | VM_EXEC | VM_MAYREAD | 234 VM_MAYWRITE | VM_MAYEXEC, vdso_spec); 235 if (IS_ERR(vma)) { 236 do_munmap(mm, vdso_base, vvar_size, NULL); 237 return PTR_ERR(vma); 238 } 239 240 // Now that the mappings are in place, set the mm VDSO pointer 241 mm->context.vdso = (void __user *)vdso_base + vvar_size; 242 243 return 0; 244 } 245 246 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) 247 { 248 struct mm_struct *mm = current->mm; 249 int rc; 250 251 mm->context.vdso = NULL; 252 253 if (mmap_write_lock_killable(mm)) 254 return -EINTR; 255 256 rc = __arch_setup_additional_pages(bprm, uses_interp); 257 258 mmap_write_unlock(mm); 259 return rc; 260 } 261 262 #define VDSO_DO_FIXUPS(type, value, bits, sec) do { \ 263 void *__start = (void *)VDSO##bits##_SYMBOL(&vdso##bits##_start, sec##_start); \ 264 void *__end = (void *)VDSO##bits##_SYMBOL(&vdso##bits##_start, sec##_end); \ 265 \ 266 do_##type##_fixups((value), __start, __end); \ 267 } while (0) 268 269 static void __init vdso_fixup_features(void) 270 { 271 #ifdef CONFIG_PPC64 272 VDSO_DO_FIXUPS(feature, cur_cpu_spec->cpu_features, 64, ftr_fixup); 273 VDSO_DO_FIXUPS(feature, cur_cpu_spec->mmu_features, 64, mmu_ftr_fixup); 274 VDSO_DO_FIXUPS(feature, powerpc_firmware_features, 64, fw_ftr_fixup); 275 VDSO_DO_FIXUPS(lwsync, cur_cpu_spec->cpu_features, 64, lwsync_fixup); 276 #endif /* CONFIG_PPC64 */ 277 278 #ifdef CONFIG_VDSO32 279 VDSO_DO_FIXUPS(feature, cur_cpu_spec->cpu_features, 32, ftr_fixup); 280 VDSO_DO_FIXUPS(feature, cur_cpu_spec->mmu_features, 32, mmu_ftr_fixup); 281 #ifdef CONFIG_PPC64 282 VDSO_DO_FIXUPS(feature, powerpc_firmware_features, 32, fw_ftr_fixup); 283 #endif /* CONFIG_PPC64 */ 284 VDSO_DO_FIXUPS(lwsync, cur_cpu_spec->cpu_features, 32, lwsync_fixup); 285 #endif 286 } 287 288 /* 289 * Called from setup_arch to initialize the bitmap of available 290 * syscalls in the systemcfg page 291 */ 292 static void __init vdso_setup_syscall_map(void) 293 { 294 unsigned int i; 295 296 for (i = 0; i < NR_syscalls; i++) { 297 if (sys_call_table[i] != (void *)&sys_ni_syscall) 298 vdso_data->syscall_map[i >> 5] |= 0x80000000UL >> (i & 0x1f); 299 if (IS_ENABLED(CONFIG_COMPAT) && 300 compat_sys_call_table[i] != (void *)&sys_ni_syscall) 301 vdso_data->compat_syscall_map[i >> 5] |= 0x80000000UL >> (i & 0x1f); 302 } 303 } 304 305 #ifdef CONFIG_PPC64 306 int vdso_getcpu_init(void) 307 { 308 unsigned long cpu, node, val; 309 310 /* 311 * SPRG_VDSO contains the CPU in the bottom 16 bits and the NUMA node 312 * in the next 16 bits. The VDSO uses this to implement getcpu(). 313 */ 314 cpu = get_cpu(); 315 WARN_ON_ONCE(cpu > 0xffff); 316 317 node = cpu_to_node(cpu); 318 WARN_ON_ONCE(node > 0xffff); 319 320 val = (cpu & 0xffff) | ((node & 0xffff) << 16); 321 mtspr(SPRN_SPRG_VDSO_WRITE, val); 322 get_paca()->sprg_vdso = val; 323 324 put_cpu(); 325 326 return 0; 327 } 328 /* We need to call this before SMP init */ 329 early_initcall(vdso_getcpu_init); 330 #endif 331 332 static struct page ** __init vdso_setup_pages(void *start, void *end) 333 { 334 int i; 335 struct page **pagelist; 336 int pages = (end - start) >> PAGE_SHIFT; 337 338 pagelist = kcalloc(pages + 1, sizeof(struct page *), GFP_KERNEL); 339 if (!pagelist) 340 panic("%s: Cannot allocate page list for VDSO", __func__); 341 342 for (i = 0; i < pages; i++) 343 pagelist[i] = virt_to_page(start + i * PAGE_SIZE); 344 345 return pagelist; 346 } 347 348 static int __init vdso_init(void) 349 { 350 #ifdef CONFIG_PPC64 351 vdso_data->dcache_block_size = ppc64_caches.l1d.block_size; 352 vdso_data->icache_block_size = ppc64_caches.l1i.block_size; 353 vdso_data->dcache_log_block_size = ppc64_caches.l1d.log_block_size; 354 vdso_data->icache_log_block_size = ppc64_caches.l1i.log_block_size; 355 #endif /* CONFIG_PPC64 */ 356 357 vdso_setup_syscall_map(); 358 359 vdso_fixup_features(); 360 361 if (IS_ENABLED(CONFIG_VDSO32)) 362 vdso32_spec.pages = vdso_setup_pages(&vdso32_start, &vdso32_end); 363 364 if (IS_ENABLED(CONFIG_PPC64)) 365 vdso64_spec.pages = vdso_setup_pages(&vdso64_start, &vdso64_end); 366 367 smp_wmb(); 368 369 return 0; 370 } 371 arch_initcall(vdso_init); 372