1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2004 Benjamin Herrenschmidt, IBM Corp. 4 * <benh@kernel.crashing.org> 5 * Copyright (C) 2012 ARM Limited 6 * Copyright (C) 2015 Regents of the University of California 7 */ 8 9 #include <linux/elf.h> 10 #include <linux/mm.h> 11 #include <linux/slab.h> 12 #include <linux/binfmts.h> 13 #include <linux/err.h> 14 #include <asm/page.h> 15 #include <asm/vdso.h> 16 #include <linux/vdso_datastore.h> 17 #include <vdso/datapage.h> 18 19 #define VVAR_SIZE (VDSO_NR_PAGES << PAGE_SHIFT) 20 21 struct __vdso_info { 22 const char *name; 23 const char *vdso_code_start; 24 const char *vdso_code_end; 25 unsigned long vdso_pages; 26 /* Code Mapping */ 27 struct vm_special_mapping *cm; 28 }; 29 30 static struct __vdso_info vdso_info; 31 #ifdef CONFIG_COMPAT 32 static struct __vdso_info compat_vdso_info; 33 #endif 34 35 static int vdso_mremap(const struct vm_special_mapping *sm, 36 struct vm_area_struct *new_vma) 37 { 38 current->mm->context.vdso = (void *)new_vma->vm_start; 39 40 return 0; 41 } 42 43 static void __init __vdso_init(struct __vdso_info *vdso_info) 44 { 45 unsigned int i; 46 struct page **vdso_pagelist; 47 unsigned long pfn; 48 49 if (memcmp(vdso_info->vdso_code_start, "\177ELF", 4)) 50 panic("vDSO is not a valid ELF object!\n"); 51 52 vdso_info->vdso_pages = ( 53 vdso_info->vdso_code_end - 54 vdso_info->vdso_code_start) >> 55 PAGE_SHIFT; 56 57 vdso_pagelist = kzalloc_objs(struct page *, vdso_info->vdso_pages); 58 if (vdso_pagelist == NULL) 59 panic("vDSO kcalloc failed!\n"); 60 61 /* Grab the vDSO code pages. */ 62 pfn = sym_to_pfn(vdso_info->vdso_code_start); 63 64 for (i = 0; i < vdso_info->vdso_pages; i++) 65 vdso_pagelist[i] = pfn_to_page(pfn + i); 66 67 vdso_info->cm->pages = vdso_pagelist; 68 } 69 70 static struct vm_special_mapping rv_vdso_map __ro_after_init = { 71 .name = "[vdso]", 72 .mremap = vdso_mremap, 73 }; 74 75 static struct __vdso_info vdso_info __ro_after_init = { 76 .name = "vdso", 77 .vdso_code_start = vdso_start, 78 .vdso_code_end = vdso_end, 79 .cm = &rv_vdso_map, 80 }; 81 82 #ifdef CONFIG_COMPAT 83 static struct vm_special_mapping rv_compat_vdso_map __ro_after_init = { 84 .name = "[vdso]", 85 .mremap = vdso_mremap, 86 }; 87 88 static struct __vdso_info compat_vdso_info __ro_after_init = { 89 .name = "compat_vdso", 90 .vdso_code_start = compat_vdso_start, 91 .vdso_code_end = compat_vdso_end, 92 .cm = &rv_compat_vdso_map, 93 }; 94 #endif 95 96 static int __init vdso_init(void) 97 { 98 /* Hart implements zimop, expose cfi compiled vdso */ 99 if (IS_ENABLED(CONFIG_RISCV_USER_CFI) && 100 riscv_has_extension_unlikely(RISCV_ISA_EXT_ZIMOP)) { 101 vdso_info.vdso_code_start = vdso_cfi_start; 102 vdso_info.vdso_code_end = vdso_cfi_end; 103 } 104 105 __vdso_init(&vdso_info); 106 #ifdef CONFIG_COMPAT 107 __vdso_init(&compat_vdso_info); 108 #endif 109 110 return 0; 111 } 112 arch_initcall(vdso_init); 113 114 static int __setup_additional_pages(struct mm_struct *mm, 115 struct linux_binprm *bprm, 116 int uses_interp, 117 struct __vdso_info *vdso_info) 118 { 119 unsigned long vdso_base, vdso_text_len, vdso_mapping_len; 120 void *ret; 121 122 BUILD_BUG_ON(VDSO_NR_PAGES != __VDSO_PAGES); 123 124 vdso_text_len = vdso_info->vdso_pages << PAGE_SHIFT; 125 /* Be sure to map the data page */ 126 vdso_mapping_len = vdso_text_len + VVAR_SIZE; 127 128 vdso_base = get_unmapped_area(NULL, 0, vdso_mapping_len, 0, 0); 129 if (IS_ERR_VALUE(vdso_base)) { 130 ret = ERR_PTR(vdso_base); 131 goto up_fail; 132 } 133 134 ret = vdso_install_vvar_mapping(mm, vdso_base); 135 if (IS_ERR(ret)) 136 goto up_fail; 137 138 vdso_base += VVAR_SIZE; 139 mm->context.vdso = (void *)vdso_base; 140 141 ret = 142 _install_special_mapping(mm, vdso_base, vdso_text_len, 143 (VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC | VM_SEALED_SYSMAP), 144 vdso_info->cm); 145 146 if (IS_ERR(ret)) 147 goto up_fail; 148 149 return 0; 150 151 up_fail: 152 mm->context.vdso = NULL; 153 return PTR_ERR(ret); 154 } 155 156 #ifdef CONFIG_COMPAT 157 int compat_arch_setup_additional_pages(struct linux_binprm *bprm, 158 int uses_interp) 159 { 160 struct mm_struct *mm = current->mm; 161 int ret; 162 163 if (mmap_write_lock_killable(mm)) 164 return -EINTR; 165 166 ret = __setup_additional_pages(mm, bprm, uses_interp, 167 &compat_vdso_info); 168 mmap_write_unlock(mm); 169 170 return ret; 171 } 172 #endif 173 174 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) 175 { 176 struct mm_struct *mm = current->mm; 177 int ret; 178 179 if (mmap_write_lock_killable(mm)) 180 return -EINTR; 181 182 ret = __setup_additional_pages(mm, bprm, uses_interp, &vdso_info); 183 mmap_write_unlock(mm); 184 185 return ret; 186 } 187