| 1 | |
| 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 |
|
| 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;
|
| 52 | Elf32_Sym *dynsym;
|
| 53 | unsigned long dynsymsize;
|
| 54 | char *dynstr;
|
| 55 | };
|
| 56 |
|
| 57 | |
| 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 | |
| 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 |
|
| 97 | sechdrs = (void *)ehdr + ehdr->e_shoff;
|
| 98 | secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
|
| 99 |
|
| 100 |
|
| 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 | |
| 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 |
|
| 182 | vdso_text_pagelist = kzalloc_objs(struct page *, text_pages);
|
| 183 | if (vdso_text_pagelist == NULL)
|
| 184 | return -ENOMEM;
|
| 185 |
|
| 186 |
|
| 187 | for (i = 0; i < text_pages; i++) {
|
| 188 | struct page *page;
|
| 189 |
|
| 190 | page = virt_to_page(vdso_start + i * PAGE_SIZE);
|
| 191 | vdso_text_pagelist[i] = page;
|
| 192 | }
|
| 193 |
|
| 194 | vdso_text_mapping.pages = vdso_text_pagelist;
|
| 195 |
|
| 196 | vdso_total_pages = VDSO_NR_PAGES;
|
| 197 | vdso_total_pages += text_pages;
|
| 198 |
|
| 199 | patch_vdso(vdso_start);
|
| 200 |
|
| 201 | return 0;
|
| 202 | }
|
| 203 | arch_initcall(vdso_init);
|
| 204 |
|
| 205 | static_assert(__VDSO_PAGES == VDSO_NR_PAGES);
|
| 206 |
|
| 207 |
|
| 208 | void arm_install_vdso(struct mm_struct *mm, unsigned long addr)
|
| 209 | {
|
| 210 | struct vm_area_struct *vma;
|
| 211 | unsigned long len;
|
| 212 |
|
| 213 | mm->context.vdso = 0;
|
| 214 |
|
| 215 | if (vdso_text_pagelist == NULL)
|
| 216 | return;
|
| 217 |
|
| 218 | if (IS_ERR(vdso_install_vvar_mapping(mm, addr)))
|
| 219 | return;
|
| 220 |
|
| 221 |
|
| 222 | addr += VDSO_NR_PAGES * PAGE_SIZE;
|
| 223 | len = (vdso_total_pages - VDSO_NR_PAGES) << PAGE_SHIFT;
|
| 224 |
|
| 225 | vma = _install_special_mapping(mm, addr, len,
|
| 226 | VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
|
| 227 | &vdso_text_mapping);
|
| 228 |
|
| 229 | if (!IS_ERR(vma))
|
| 230 | mm->context.vdso = addr;
|
| 231 | }
|
| 232 |
|