1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Adapted from arm64 version. 4 * 5 * Copyright (C) 2012 ARM Limited 6 * Copyright (C) 2015 Mentor Graphics Corporation. 7 */ 8 9 #include <linux/cache.h> 10 #include <linux/vdso_datastore.h> 11 #include <linux/elf.h> 12 #include <linux/err.h> 13 #include <linux/kernel.h> 14 #include <linux/mm.h> 15 #include <linux/of.h> 16 #include <linux/printk.h> 17 #include <linux/slab.h> 18 #include <linux/vmalloc.h> 19 #include <asm/arch_timer.h> 20 #include <asm/barrier.h> 21 #include <asm/cacheflush.h> 22 #include <asm/page.h> 23 #include <asm/vdso.h> 24 #include <clocksource/arm_arch_timer.h> 25 #include <vdso/helpers.h> 26 #include <vdso/vsyscall.h> 27 28 #define MAX_SYMNAME 64 29 30 static struct page **vdso_text_pagelist; 31 32 extern char vdso_start[], vdso_end[]; 33 34 /* Total number of pages needed for the data and text portions of the VDSO. */ 35 unsigned int vdso_total_pages __ro_after_init; 36 37 static int vdso_mremap(const struct vm_special_mapping *sm, 38 struct vm_area_struct *new_vma) 39 { 40 current->mm->context.vdso = new_vma->vm_start; 41 42 return 0; 43 } 44 45 static struct vm_special_mapping vdso_text_mapping __ro_after_init = { 46 .name = "[vdso]", 47 .mremap = vdso_mremap, 48 }; 49 50 struct elfinfo { 51 Elf32_Ehdr *hdr; /* ptr to ELF */ 52 Elf32_Sym *dynsym; /* ptr to .dynsym section */ 53 unsigned long dynsymsize; /* size of .dynsym section */ 54 char *dynstr; /* ptr to .dynstr section */ 55 }; 56 57 /* Boot-time check for whether the arch timer exists, and if so, 58 * whether the virtual counter is usable. 59 */ 60 static bool __init cntvct_functional(void) 61 { 62 struct device_node *np; 63 bool ret = false; 64 65 if (!IS_ENABLED(CONFIG_ARM_ARCH_TIMER)) 66 goto out; 67 68 /* The arm_arch_timer core should export 69 * arch_timer_use_virtual or similar so we don't have to do 70 * this. 71 */ 72 np = of_find_compatible_node(NULL, NULL, "arm,armv7-timer"); 73 if (!np) 74 np = of_find_compatible_node(NULL, NULL, "arm,armv8-timer"); 75 if (!np) 76 goto out_put; 77 78 if (of_property_read_bool(np, "arm,cpu-registers-not-fw-configured")) 79 goto out_put; 80 81 ret = true; 82 83 out_put: 84 of_node_put(np); 85 out: 86 return ret; 87 } 88 89 static void * __init find_section(Elf32_Ehdr *ehdr, const char *name, 90 unsigned long *size) 91 { 92 Elf32_Shdr *sechdrs; 93 unsigned int i; 94 char *secnames; 95 96 /* Grab section headers and strings so we can tell who is who */ 97 sechdrs = (void *)ehdr + ehdr->e_shoff; 98 secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset; 99 100 /* Find the section they want */ 101 for (i = 1; i < ehdr->e_shnum; i++) { 102 if (strcmp(secnames + sechdrs[i].sh_name, name) == 0) { 103 if (size) 104 *size = sechdrs[i].sh_size; 105 return (void *)ehdr + sechdrs[i].sh_offset; 106 } 107 } 108 109 if (size) 110 *size = 0; 111 return NULL; 112 } 113 114 static Elf32_Sym * __init find_symbol(struct elfinfo *lib, const char *symname) 115 { 116 unsigned int i; 117 118 for (i = 0; i < (lib->dynsymsize / sizeof(Elf32_Sym)); i++) { 119 char name[MAX_SYMNAME], *c; 120 121 if (lib->dynsym[i].st_name == 0) 122 continue; 123 strscpy(name, lib->dynstr + lib->dynsym[i].st_name, 124 MAX_SYMNAME); 125 c = strchr(name, '@'); 126 if (c) 127 *c = 0; 128 if (strcmp(symname, name) == 0) 129 return &lib->dynsym[i]; 130 } 131 return NULL; 132 } 133 134 static void __init vdso_nullpatch_one(struct elfinfo *lib, const char *symname) 135 { 136 Elf32_Sym *sym; 137 138 sym = find_symbol(lib, symname); 139 if (!sym) 140 return; 141 142 sym->st_name = 0; 143 } 144 145 static void __init patch_vdso(void *ehdr) 146 { 147 struct elfinfo einfo; 148 149 einfo = (struct elfinfo) { 150 .hdr = ehdr, 151 }; 152 153 einfo.dynsym = find_section(einfo.hdr, ".dynsym", &einfo.dynsymsize); 154 einfo.dynstr = find_section(einfo.hdr, ".dynstr", NULL); 155 156 /* If the virtual counter is absent or non-functional we don't 157 * want programs to incur the slight additional overhead of 158 * dispatching through the VDSO only to fall back to syscalls. 159 */ 160 if (!cntvct_functional()) { 161 vdso_nullpatch_one(&einfo, "__vdso_gettimeofday"); 162 vdso_nullpatch_one(&einfo, "__vdso_clock_gettime"); 163 vdso_nullpatch_one(&einfo, "__vdso_clock_gettime64"); 164 vdso_nullpatch_one(&einfo, "__vdso_clock_getres"); 165 vdso_nullpatch_one(&einfo, "__vdso_clock_getres_time64"); 166 } 167 } 168 169 static int __init vdso_init(void) 170 { 171 unsigned int text_pages; 172 int i; 173 174 if (memcmp(vdso_start, "\177ELF", 4)) { 175 pr_err("VDSO is not a valid ELF object!\n"); 176 return -ENOEXEC; 177 } 178 179 text_pages = (vdso_end - vdso_start) >> PAGE_SHIFT; 180 181 /* Allocate the VDSO text pagelist */ 182 vdso_text_pagelist = kcalloc(text_pages, sizeof(struct page *), 183 GFP_KERNEL); 184 if (vdso_text_pagelist == NULL) 185 return -ENOMEM; 186 187 /* Grab the VDSO text pages. */ 188 for (i = 0; i < text_pages; i++) { 189 struct page *page; 190 191 page = virt_to_page(vdso_start + i * PAGE_SIZE); 192 vdso_text_pagelist[i] = page; 193 } 194 195 vdso_text_mapping.pages = vdso_text_pagelist; 196 197 vdso_total_pages = VDSO_NR_PAGES; /* for the data/vvar pages */ 198 vdso_total_pages += text_pages; 199 200 patch_vdso(vdso_start); 201 202 return 0; 203 } 204 arch_initcall(vdso_init); 205 206 static_assert(__VDSO_PAGES == VDSO_NR_PAGES); 207 208 /* assumes mmap_lock is write-locked */ 209 void arm_install_vdso(struct mm_struct *mm, unsigned long addr) 210 { 211 struct vm_area_struct *vma; 212 unsigned long len; 213 214 mm->context.vdso = 0; 215 216 if (vdso_text_pagelist == NULL) 217 return; 218 219 if (IS_ERR(vdso_install_vvar_mapping(mm, addr))) 220 return; 221 222 /* Account for vvar pages. */ 223 addr += VDSO_NR_PAGES * PAGE_SIZE; 224 len = (vdso_total_pages - VDSO_NR_PAGES) << PAGE_SHIFT; 225 226 vma = _install_special_mapping(mm, addr, len, 227 VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC, 228 &vdso_text_mapping); 229 230 if (!IS_ERR(vma)) 231 mm->context.vdso = addr; 232 } 233 234