^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Set up the VMAs to tell the VM about the vDSO.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright 2007 Andi Kleen, SUSE Labs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/linkage.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/elf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <asm/cacheflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <asm/spitfire.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <asm/vdso.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <asm/vvar.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <asm/page.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) unsigned int __read_mostly vdso_enabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static struct vm_special_mapping vvar_mapping = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) .name = "[vvar]"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #ifdef CONFIG_SPARC64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static struct vm_special_mapping vdso_mapping64 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) .name = "[vdso]"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static struct vm_special_mapping vdso_mapping32 = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) .name = "[vdso]"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct vvar_data *vvar_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct vdso_elfinfo32 {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) Elf32_Ehdr *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) Elf32_Sym *dynsym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) unsigned long dynsymsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) const char *dynstr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) unsigned long text;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct vdso_elfinfo64 {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) Elf64_Ehdr *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) Elf64_Sym *dynsym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) unsigned long dynsymsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) const char *dynstr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) unsigned long text;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct vdso_elfinfo {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct vdso_elfinfo32 elf32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct vdso_elfinfo64 elf64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) } u;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) static void *one_section64(struct vdso_elfinfo64 *e, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) unsigned long *size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) const char *snames;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) Elf64_Shdr *shdrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) shdrs = (void *)e->hdr + e->hdr->e_shoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) snames = (void *)e->hdr + shdrs[e->hdr->e_shstrndx].sh_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) for (i = 1; i < e->hdr->e_shnum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (!strcmp(snames+shdrs[i].sh_name, name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) *size = shdrs[i].sh_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return (void *)e->hdr + shdrs[i].sh_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static int find_sections64(const struct vdso_image *image, struct vdso_elfinfo *_e)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct vdso_elfinfo64 *e = &_e->u.elf64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) e->hdr = image->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) e->dynsym = one_section64(e, ".dynsym", &e->dynsymsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) e->dynstr = one_section64(e, ".dynstr", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (!e->dynsym || !e->dynstr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) pr_err("VDSO64: Missing symbol sections.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static Elf64_Sym *find_sym64(const struct vdso_elfinfo64 *e, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) for (i = 0; i < (e->dynsymsize / sizeof(Elf64_Sym)); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) Elf64_Sym *s = &e->dynsym[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (s->st_name == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (!strcmp(e->dynstr + s->st_name, name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static int patchsym64(struct vdso_elfinfo *_e, const char *orig,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) const char *new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct vdso_elfinfo64 *e = &_e->u.elf64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) Elf64_Sym *osym = find_sym64(e, orig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) Elf64_Sym *nsym = find_sym64(e, new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (!nsym || !osym) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) pr_err("VDSO64: Missing symbols.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) osym->st_value = nsym->st_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) osym->st_size = nsym->st_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) osym->st_info = nsym->st_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) osym->st_other = nsym->st_other;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) osym->st_shndx = nsym->st_shndx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static void *one_section32(struct vdso_elfinfo32 *e, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) unsigned long *size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) const char *snames;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) Elf32_Shdr *shdrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) shdrs = (void *)e->hdr + e->hdr->e_shoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) snames = (void *)e->hdr + shdrs[e->hdr->e_shstrndx].sh_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) for (i = 1; i < e->hdr->e_shnum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (!strcmp(snames+shdrs[i].sh_name, name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) *size = shdrs[i].sh_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return (void *)e->hdr + shdrs[i].sh_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static int find_sections32(const struct vdso_image *image, struct vdso_elfinfo *_e)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) struct vdso_elfinfo32 *e = &_e->u.elf32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) e->hdr = image->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) e->dynsym = one_section32(e, ".dynsym", &e->dynsymsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) e->dynstr = one_section32(e, ".dynstr", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (!e->dynsym || !e->dynstr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) pr_err("VDSO32: Missing symbol sections.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) static Elf32_Sym *find_sym32(const struct vdso_elfinfo32 *e, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) for (i = 0; i < (e->dynsymsize / sizeof(Elf32_Sym)); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) Elf32_Sym *s = &e->dynsym[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (s->st_name == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (!strcmp(e->dynstr + s->st_name, name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) return s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static int patchsym32(struct vdso_elfinfo *_e, const char *orig,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) const char *new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) struct vdso_elfinfo32 *e = &_e->u.elf32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) Elf32_Sym *osym = find_sym32(e, orig);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) Elf32_Sym *nsym = find_sym32(e, new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (!nsym || !osym) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) pr_err("VDSO32: Missing symbols.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) osym->st_value = nsym->st_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) osym->st_size = nsym->st_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) osym->st_info = nsym->st_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) osym->st_other = nsym->st_other;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) osym->st_shndx = nsym->st_shndx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static int find_sections(const struct vdso_image *image, struct vdso_elfinfo *e,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) bool elf64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (elf64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) return find_sections64(image, e);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return find_sections32(image, e);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) static int patch_one_symbol(struct vdso_elfinfo *e, const char *orig,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) const char *new_target, bool elf64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (elf64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return patchsym64(e, orig, new_target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return patchsym32(e, orig, new_target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static int stick_patch(const struct vdso_image *image, struct vdso_elfinfo *e, bool elf64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) err = find_sections(image, e, elf64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) err = patch_one_symbol(e,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) "__vdso_gettimeofday",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) "__vdso_gettimeofday_stick", elf64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) return patch_one_symbol(e,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) "__vdso_clock_gettime",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) "__vdso_clock_gettime_stick", elf64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * Allocate pages for the vdso and vvar, and copy in the vdso text from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * kernel image.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) int __init init_vdso_image(const struct vdso_image *image,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) struct vm_special_mapping *vdso_mapping, bool elf64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) int cnpages = (image->size) / PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) struct page *dp, **dpp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) struct page *cp, **cpp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) struct vdso_elfinfo ei;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) int i, dnpages = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) if (tlb_type != spitfire) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) int err = stick_patch(image, &ei, elf64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) * First, the vdso text. This is initialied data, an integral number of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) * pages long.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) if (WARN_ON(image->size % PAGE_SIZE != 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) goto oom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) cpp = kcalloc(cnpages, sizeof(struct page *), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) vdso_mapping->pages = cpp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (!cpp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) goto oom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) for (i = 0; i < cnpages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) cp = alloc_page(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) if (!cp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) goto oom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) cpp[i] = cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) copy_page(page_address(cp), image->data + i * PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * Now the vvar page. This is uninitialized data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) if (vvar_data == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) dnpages = (sizeof(struct vvar_data) / PAGE_SIZE) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (WARN_ON(dnpages != 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) goto oom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) dpp = kcalloc(dnpages, sizeof(struct page *), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) vvar_mapping.pages = dpp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (!dpp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) goto oom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) dp = alloc_page(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (!dp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) goto oom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) dpp[0] = dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) vvar_data = page_address(dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) memset(vvar_data, 0, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) vvar_data->seq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) oom:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) if (cpp != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) for (i = 0; i < cnpages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) if (cpp[i] != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) __free_page(cpp[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) kfree(cpp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) vdso_mapping->pages = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (dpp != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) for (i = 0; i < dnpages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (dpp[i] != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) __free_page(dpp[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) kfree(dpp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) vvar_mapping.pages = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) pr_warn("Cannot allocate vdso\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) vdso_enabled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) static int __init init_vdso(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) #ifdef CONFIG_SPARC64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) err = init_vdso_image(&vdso_image_64_builtin, &vdso_mapping64, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) err = init_vdso_image(&vdso_image_32_builtin, &vdso_mapping32, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) subsys_initcall(init_vdso);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) struct linux_binprm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) /* Shuffle the vdso up a bit, randomly. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) static unsigned long vdso_addr(unsigned long start, unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) unsigned int offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) /* This loses some more bits than a modulo, but is cheaper */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) offset = get_random_int() & (PTRS_PER_PTE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) return start + (offset << PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) static int map_vdso(const struct vdso_image *image,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) struct vm_special_mapping *vdso_mapping)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) struct mm_struct *mm = current->mm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) struct vm_area_struct *vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) unsigned long text_start, addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) mmap_write_lock(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) * First, get an unmapped region: then randomize it, and make sure that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) * region is free.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) if (current->flags & PF_RANDOMIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) addr = get_unmapped_area(NULL, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) image->size - image->sym_vvar_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (IS_ERR_VALUE(addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) ret = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) goto up_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) addr = vdso_addr(addr, image->size - image->sym_vvar_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) addr = get_unmapped_area(NULL, addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) image->size - image->sym_vvar_start, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) if (IS_ERR_VALUE(addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) ret = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) goto up_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) text_start = addr - image->sym_vvar_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) current->mm->context.vdso = (void __user *)text_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) * MAYWRITE to allow gdb to COW and set breakpoints
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) vma = _install_special_mapping(mm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) text_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) image->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) VM_READ|VM_EXEC|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) vdso_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) if (IS_ERR(vma)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) ret = PTR_ERR(vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) goto up_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) vma = _install_special_mapping(mm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) -image->sym_vvar_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) VM_READ|VM_MAYREAD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) &vvar_mapping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) if (IS_ERR(vma)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) ret = PTR_ERR(vma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) do_munmap(mm, text_start, image->size, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) up_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) current->mm->context.vdso = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) mmap_write_unlock(mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) if (!vdso_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) #if defined CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) if (!(is_32bit_task()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) return map_vdso(&vdso_image_64_builtin, &vdso_mapping64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) return map_vdso(&vdso_image_32_builtin, &vdso_mapping32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) return map_vdso(&vdso_image_64_builtin, &vdso_mapping64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) static __init int vdso_setup(char *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) unsigned long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) err = kstrtoul(s, 10, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) vdso_enabled = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) __setup("vdso=", vdso_setup);