1003ba957SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
29a08862aSNagarathnam Muthusamy /*
39a08862aSNagarathnam Muthusamy * Set up the VMAs to tell the VM about the vDSO.
49a08862aSNagarathnam Muthusamy * Copyright 2007 Andi Kleen, SUSE Labs.
59a08862aSNagarathnam Muthusamy */
69a08862aSNagarathnam Muthusamy
79a08862aSNagarathnam Muthusamy /*
89a08862aSNagarathnam Muthusamy * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
99a08862aSNagarathnam Muthusamy */
109a08862aSNagarathnam Muthusamy
119a08862aSNagarathnam Muthusamy #include <linux/mm.h>
129a08862aSNagarathnam Muthusamy #include <linux/err.h>
139a08862aSNagarathnam Muthusamy #include <linux/sched.h>
149a08862aSNagarathnam Muthusamy #include <linux/slab.h>
159a08862aSNagarathnam Muthusamy #include <linux/init.h>
169a08862aSNagarathnam Muthusamy #include <linux/linkage.h>
179a08862aSNagarathnam Muthusamy #include <linux/random.h>
189a08862aSNagarathnam Muthusamy #include <linux/elf.h>
192f6c9bf3SDavid S. Miller #include <asm/cacheflush.h>
202f6c9bf3SDavid S. Miller #include <asm/spitfire.h>
219a08862aSNagarathnam Muthusamy #include <asm/vdso.h>
229a08862aSNagarathnam Muthusamy #include <asm/vvar.h>
239a08862aSNagarathnam Muthusamy #include <asm/page.h>
249a08862aSNagarathnam Muthusamy
259a08862aSNagarathnam Muthusamy unsigned int __read_mostly vdso_enabled = 1;
269a08862aSNagarathnam Muthusamy
279a08862aSNagarathnam Muthusamy static struct vm_special_mapping vvar_mapping = {
289a08862aSNagarathnam Muthusamy .name = "[vvar]"
299a08862aSNagarathnam Muthusamy };
309a08862aSNagarathnam Muthusamy
319a08862aSNagarathnam Muthusamy #ifdef CONFIG_SPARC64
329a08862aSNagarathnam Muthusamy static struct vm_special_mapping vdso_mapping64 = {
339a08862aSNagarathnam Muthusamy .name = "[vdso]"
349a08862aSNagarathnam Muthusamy };
359a08862aSNagarathnam Muthusamy #endif
369a08862aSNagarathnam Muthusamy
379a08862aSNagarathnam Muthusamy #ifdef CONFIG_COMPAT
389a08862aSNagarathnam Muthusamy static struct vm_special_mapping vdso_mapping32 = {
399a08862aSNagarathnam Muthusamy .name = "[vdso]"
409a08862aSNagarathnam Muthusamy };
419a08862aSNagarathnam Muthusamy #endif
429a08862aSNagarathnam Muthusamy
439a08862aSNagarathnam Muthusamy struct vvar_data *vvar_data;
449a08862aSNagarathnam Muthusamy
45caf539cdSDavid S. Miller struct vdso_elfinfo32 {
46caf539cdSDavid S. Miller Elf32_Ehdr *hdr;
47caf539cdSDavid S. Miller Elf32_Sym *dynsym;
48caf539cdSDavid S. Miller unsigned long dynsymsize;
49caf539cdSDavid S. Miller const char *dynstr;
50caf539cdSDavid S. Miller unsigned long text;
512f6c9bf3SDavid S. Miller };
522f6c9bf3SDavid S. Miller
53caf539cdSDavid S. Miller struct vdso_elfinfo64 {
54caf539cdSDavid S. Miller Elf64_Ehdr *hdr;
55caf539cdSDavid S. Miller Elf64_Sym *dynsym;
56caf539cdSDavid S. Miller unsigned long dynsymsize;
57caf539cdSDavid S. Miller const char *dynstr;
58caf539cdSDavid S. Miller unsigned long text;
59caf539cdSDavid S. Miller };
60caf539cdSDavid S. Miller
61caf539cdSDavid S. Miller struct vdso_elfinfo {
62caf539cdSDavid S. Miller union {
63caf539cdSDavid S. Miller struct vdso_elfinfo32 elf32;
64caf539cdSDavid S. Miller struct vdso_elfinfo64 elf64;
65caf539cdSDavid S. Miller } u;
66caf539cdSDavid S. Miller };
67caf539cdSDavid S. Miller
one_section64(struct vdso_elfinfo64 * e,const char * name,unsigned long * size)68caf539cdSDavid S. Miller static void *one_section64(struct vdso_elfinfo64 *e, const char *name,
69caf539cdSDavid S. Miller unsigned long *size)
702f6c9bf3SDavid S. Miller {
71caf539cdSDavid S. Miller const char *snames;
72caf539cdSDavid S. Miller Elf64_Shdr *shdrs;
73caf539cdSDavid S. Miller unsigned int i;
742f6c9bf3SDavid S. Miller
75caf539cdSDavid S. Miller shdrs = (void *)e->hdr + e->hdr->e_shoff;
76caf539cdSDavid S. Miller snames = (void *)e->hdr + shdrs[e->hdr->e_shstrndx].sh_offset;
77caf539cdSDavid S. Miller for (i = 1; i < e->hdr->e_shnum; i++) {
78caf539cdSDavid S. Miller if (!strcmp(snames+shdrs[i].sh_name, name)) {
79caf539cdSDavid S. Miller if (size)
80caf539cdSDavid S. Miller *size = shdrs[i].sh_size;
81caf539cdSDavid S. Miller return (void *)e->hdr + shdrs[i].sh_offset;
822f6c9bf3SDavid S. Miller }
832f6c9bf3SDavid S. Miller }
84caf539cdSDavid S. Miller return NULL;
85caf539cdSDavid S. Miller }
86caf539cdSDavid S. Miller
find_sections64(const struct vdso_image * image,struct vdso_elfinfo * _e)87caf539cdSDavid S. Miller static int find_sections64(const struct vdso_image *image, struct vdso_elfinfo *_e)
88caf539cdSDavid S. Miller {
89caf539cdSDavid S. Miller struct vdso_elfinfo64 *e = &_e->u.elf64;
90caf539cdSDavid S. Miller
91caf539cdSDavid S. Miller e->hdr = image->data;
92caf539cdSDavid S. Miller e->dynsym = one_section64(e, ".dynsym", &e->dynsymsize);
93caf539cdSDavid S. Miller e->dynstr = one_section64(e, ".dynstr", NULL);
94caf539cdSDavid S. Miller
95caf539cdSDavid S. Miller if (!e->dynsym || !e->dynstr) {
96caf539cdSDavid S. Miller pr_err("VDSO64: Missing symbol sections.\n");
97caf539cdSDavid S. Miller return -ENODEV;
98caf539cdSDavid S. Miller }
99caf539cdSDavid S. Miller return 0;
100caf539cdSDavid S. Miller }
101caf539cdSDavid S. Miller
find_sym64(const struct vdso_elfinfo64 * e,const char * name)102caf539cdSDavid S. Miller static Elf64_Sym *find_sym64(const struct vdso_elfinfo64 *e, const char *name)
103caf539cdSDavid S. Miller {
104caf539cdSDavid S. Miller unsigned int i;
105caf539cdSDavid S. Miller
106caf539cdSDavid S. Miller for (i = 0; i < (e->dynsymsize / sizeof(Elf64_Sym)); i++) {
107caf539cdSDavid S. Miller Elf64_Sym *s = &e->dynsym[i];
108caf539cdSDavid S. Miller if (s->st_name == 0)
109caf539cdSDavid S. Miller continue;
110caf539cdSDavid S. Miller if (!strcmp(e->dynstr + s->st_name, name))
111caf539cdSDavid S. Miller return s;
112caf539cdSDavid S. Miller }
113caf539cdSDavid S. Miller return NULL;
114caf539cdSDavid S. Miller }
115caf539cdSDavid S. Miller
patchsym64(struct vdso_elfinfo * _e,const char * orig,const char * new)116caf539cdSDavid S. Miller static int patchsym64(struct vdso_elfinfo *_e, const char *orig,
117caf539cdSDavid S. Miller const char *new)
118caf539cdSDavid S. Miller {
119caf539cdSDavid S. Miller struct vdso_elfinfo64 *e = &_e->u.elf64;
120caf539cdSDavid S. Miller Elf64_Sym *osym = find_sym64(e, orig);
121caf539cdSDavid S. Miller Elf64_Sym *nsym = find_sym64(e, new);
122caf539cdSDavid S. Miller
123caf539cdSDavid S. Miller if (!nsym || !osym) {
124caf539cdSDavid S. Miller pr_err("VDSO64: Missing symbols.\n");
125caf539cdSDavid S. Miller return -ENODEV;
126caf539cdSDavid S. Miller }
127caf539cdSDavid S. Miller osym->st_value = nsym->st_value;
128caf539cdSDavid S. Miller osym->st_size = nsym->st_size;
129caf539cdSDavid S. Miller osym->st_info = nsym->st_info;
130caf539cdSDavid S. Miller osym->st_other = nsym->st_other;
131caf539cdSDavid S. Miller osym->st_shndx = nsym->st_shndx;
132caf539cdSDavid S. Miller
133caf539cdSDavid S. Miller return 0;
134caf539cdSDavid S. Miller }
135caf539cdSDavid S. Miller
one_section32(struct vdso_elfinfo32 * e,const char * name,unsigned long * size)136caf539cdSDavid S. Miller static void *one_section32(struct vdso_elfinfo32 *e, const char *name,
137caf539cdSDavid S. Miller unsigned long *size)
138caf539cdSDavid S. Miller {
139caf539cdSDavid S. Miller const char *snames;
140caf539cdSDavid S. Miller Elf32_Shdr *shdrs;
141caf539cdSDavid S. Miller unsigned int i;
142caf539cdSDavid S. Miller
143caf539cdSDavid S. Miller shdrs = (void *)e->hdr + e->hdr->e_shoff;
144caf539cdSDavid S. Miller snames = (void *)e->hdr + shdrs[e->hdr->e_shstrndx].sh_offset;
145caf539cdSDavid S. Miller for (i = 1; i < e->hdr->e_shnum; i++) {
146caf539cdSDavid S. Miller if (!strcmp(snames+shdrs[i].sh_name, name)) {
147caf539cdSDavid S. Miller if (size)
148caf539cdSDavid S. Miller *size = shdrs[i].sh_size;
149caf539cdSDavid S. Miller return (void *)e->hdr + shdrs[i].sh_offset;
150caf539cdSDavid S. Miller }
151caf539cdSDavid S. Miller }
152caf539cdSDavid S. Miller return NULL;
153caf539cdSDavid S. Miller }
154caf539cdSDavid S. Miller
find_sections32(const struct vdso_image * image,struct vdso_elfinfo * _e)155caf539cdSDavid S. Miller static int find_sections32(const struct vdso_image *image, struct vdso_elfinfo *_e)
156caf539cdSDavid S. Miller {
157caf539cdSDavid S. Miller struct vdso_elfinfo32 *e = &_e->u.elf32;
158caf539cdSDavid S. Miller
159caf539cdSDavid S. Miller e->hdr = image->data;
160caf539cdSDavid S. Miller e->dynsym = one_section32(e, ".dynsym", &e->dynsymsize);
161caf539cdSDavid S. Miller e->dynstr = one_section32(e, ".dynstr", NULL);
162caf539cdSDavid S. Miller
163caf539cdSDavid S. Miller if (!e->dynsym || !e->dynstr) {
164caf539cdSDavid S. Miller pr_err("VDSO32: Missing symbol sections.\n");
165caf539cdSDavid S. Miller return -ENODEV;
166caf539cdSDavid S. Miller }
167caf539cdSDavid S. Miller return 0;
168caf539cdSDavid S. Miller }
169caf539cdSDavid S. Miller
find_sym32(const struct vdso_elfinfo32 * e,const char * name)170caf539cdSDavid S. Miller static Elf32_Sym *find_sym32(const struct vdso_elfinfo32 *e, const char *name)
171caf539cdSDavid S. Miller {
172caf539cdSDavid S. Miller unsigned int i;
173caf539cdSDavid S. Miller
174caf539cdSDavid S. Miller for (i = 0; i < (e->dynsymsize / sizeof(Elf32_Sym)); i++) {
175caf539cdSDavid S. Miller Elf32_Sym *s = &e->dynsym[i];
176caf539cdSDavid S. Miller if (s->st_name == 0)
177caf539cdSDavid S. Miller continue;
178caf539cdSDavid S. Miller if (!strcmp(e->dynstr + s->st_name, name))
179caf539cdSDavid S. Miller return s;
180caf539cdSDavid S. Miller }
181caf539cdSDavid S. Miller return NULL;
182caf539cdSDavid S. Miller }
183caf539cdSDavid S. Miller
patchsym32(struct vdso_elfinfo * _e,const char * orig,const char * new)184caf539cdSDavid S. Miller static int patchsym32(struct vdso_elfinfo *_e, const char *orig,
185caf539cdSDavid S. Miller const char *new)
186caf539cdSDavid S. Miller {
187caf539cdSDavid S. Miller struct vdso_elfinfo32 *e = &_e->u.elf32;
188caf539cdSDavid S. Miller Elf32_Sym *osym = find_sym32(e, orig);
189caf539cdSDavid S. Miller Elf32_Sym *nsym = find_sym32(e, new);
190caf539cdSDavid S. Miller
191caf539cdSDavid S. Miller if (!nsym || !osym) {
192caf539cdSDavid S. Miller pr_err("VDSO32: Missing symbols.\n");
193caf539cdSDavid S. Miller return -ENODEV;
194caf539cdSDavid S. Miller }
195caf539cdSDavid S. Miller osym->st_value = nsym->st_value;
196caf539cdSDavid S. Miller osym->st_size = nsym->st_size;
197caf539cdSDavid S. Miller osym->st_info = nsym->st_info;
198caf539cdSDavid S. Miller osym->st_other = nsym->st_other;
199caf539cdSDavid S. Miller osym->st_shndx = nsym->st_shndx;
200caf539cdSDavid S. Miller
201caf539cdSDavid S. Miller return 0;
202caf539cdSDavid S. Miller }
203caf539cdSDavid S. Miller
find_sections(const struct vdso_image * image,struct vdso_elfinfo * e,bool elf64)204caf539cdSDavid S. Miller static int find_sections(const struct vdso_image *image, struct vdso_elfinfo *e,
205caf539cdSDavid S. Miller bool elf64)
206caf539cdSDavid S. Miller {
207caf539cdSDavid S. Miller if (elf64)
208caf539cdSDavid S. Miller return find_sections64(image, e);
209caf539cdSDavid S. Miller else
210caf539cdSDavid S. Miller return find_sections32(image, e);
211caf539cdSDavid S. Miller }
212caf539cdSDavid S. Miller
patch_one_symbol(struct vdso_elfinfo * e,const char * orig,const char * new_target,bool elf64)213caf539cdSDavid S. Miller static int patch_one_symbol(struct vdso_elfinfo *e, const char *orig,
214caf539cdSDavid S. Miller const char *new_target, bool elf64)
215caf539cdSDavid S. Miller {
216caf539cdSDavid S. Miller if (elf64)
217caf539cdSDavid S. Miller return patchsym64(e, orig, new_target);
218caf539cdSDavid S. Miller else
219caf539cdSDavid S. Miller return patchsym32(e, orig, new_target);
220caf539cdSDavid S. Miller }
221caf539cdSDavid S. Miller
stick_patch(const struct vdso_image * image,struct vdso_elfinfo * e,bool elf64)222caf539cdSDavid S. Miller static int stick_patch(const struct vdso_image *image, struct vdso_elfinfo *e, bool elf64)
223caf539cdSDavid S. Miller {
224caf539cdSDavid S. Miller int err;
225caf539cdSDavid S. Miller
226caf539cdSDavid S. Miller err = find_sections(image, e, elf64);
227caf539cdSDavid S. Miller if (err)
228caf539cdSDavid S. Miller return err;
229caf539cdSDavid S. Miller
230caf539cdSDavid S. Miller err = patch_one_symbol(e,
231caf539cdSDavid S. Miller "__vdso_gettimeofday",
232caf539cdSDavid S. Miller "__vdso_gettimeofday_stick", elf64);
233caf539cdSDavid S. Miller if (err)
234caf539cdSDavid S. Miller return err;
235caf539cdSDavid S. Miller
236caf539cdSDavid S. Miller return patch_one_symbol(e,
237caf539cdSDavid S. Miller "__vdso_clock_gettime",
238caf539cdSDavid S. Miller "__vdso_clock_gettime_stick", elf64);
239caf539cdSDavid S. Miller return 0;
240caf539cdSDavid S. Miller }
2419a08862aSNagarathnam Muthusamy
2429a08862aSNagarathnam Muthusamy /*
2439a08862aSNagarathnam Muthusamy * Allocate pages for the vdso and vvar, and copy in the vdso text from the
2449a08862aSNagarathnam Muthusamy * kernel image.
2459a08862aSNagarathnam Muthusamy */
init_vdso_image(const struct vdso_image * image,struct vm_special_mapping * vdso_mapping,bool elf64)2469a08862aSNagarathnam Muthusamy int __init init_vdso_image(const struct vdso_image *image,
247caf539cdSDavid S. Miller struct vm_special_mapping *vdso_mapping, bool elf64)
2489a08862aSNagarathnam Muthusamy {
2499a08862aSNagarathnam Muthusamy int cnpages = (image->size) / PAGE_SIZE;
250caf539cdSDavid S. Miller struct page *dp, **dpp = NULL;
251caf539cdSDavid S. Miller struct page *cp, **cpp = NULL;
252caf539cdSDavid S. Miller struct vdso_elfinfo ei;
253caf539cdSDavid S. Miller int i, dnpages = 0;
254caf539cdSDavid S. Miller
255caf539cdSDavid S. Miller if (tlb_type != spitfire) {
256caf539cdSDavid S. Miller int err = stick_patch(image, &ei, elf64);
257caf539cdSDavid S. Miller if (err)
258caf539cdSDavid S. Miller return err;
259caf539cdSDavid S. Miller }
2609a08862aSNagarathnam Muthusamy
2619a08862aSNagarathnam Muthusamy /*
2629a08862aSNagarathnam Muthusamy * First, the vdso text. This is initialied data, an integral number of
2639a08862aSNagarathnam Muthusamy * pages long.
2649a08862aSNagarathnam Muthusamy */
2659a08862aSNagarathnam Muthusamy if (WARN_ON(image->size % PAGE_SIZE != 0))
2669a08862aSNagarathnam Muthusamy goto oom;
2679a08862aSNagarathnam Muthusamy
2689a08862aSNagarathnam Muthusamy cpp = kcalloc(cnpages, sizeof(struct page *), GFP_KERNEL);
2699a08862aSNagarathnam Muthusamy vdso_mapping->pages = cpp;
2709a08862aSNagarathnam Muthusamy
2719a08862aSNagarathnam Muthusamy if (!cpp)
2729a08862aSNagarathnam Muthusamy goto oom;
2739a08862aSNagarathnam Muthusamy
2749a08862aSNagarathnam Muthusamy for (i = 0; i < cnpages; i++) {
2759a08862aSNagarathnam Muthusamy cp = alloc_page(GFP_KERNEL);
2769a08862aSNagarathnam Muthusamy if (!cp)
2779a08862aSNagarathnam Muthusamy goto oom;
2789a08862aSNagarathnam Muthusamy cpp[i] = cp;
2799a08862aSNagarathnam Muthusamy copy_page(page_address(cp), image->data + i * PAGE_SIZE);
2809a08862aSNagarathnam Muthusamy }
2819a08862aSNagarathnam Muthusamy
2829a08862aSNagarathnam Muthusamy /*
2839a08862aSNagarathnam Muthusamy * Now the vvar page. This is uninitialized data.
2849a08862aSNagarathnam Muthusamy */
2859a08862aSNagarathnam Muthusamy
2869a08862aSNagarathnam Muthusamy if (vvar_data == NULL) {
2879a08862aSNagarathnam Muthusamy dnpages = (sizeof(struct vvar_data) / PAGE_SIZE) + 1;
2889a08862aSNagarathnam Muthusamy if (WARN_ON(dnpages != 1))
2899a08862aSNagarathnam Muthusamy goto oom;
2909a08862aSNagarathnam Muthusamy dpp = kcalloc(dnpages, sizeof(struct page *), GFP_KERNEL);
2919a08862aSNagarathnam Muthusamy vvar_mapping.pages = dpp;
2929a08862aSNagarathnam Muthusamy
2939a08862aSNagarathnam Muthusamy if (!dpp)
2949a08862aSNagarathnam Muthusamy goto oom;
2959a08862aSNagarathnam Muthusamy
2969a08862aSNagarathnam Muthusamy dp = alloc_page(GFP_KERNEL);
2979a08862aSNagarathnam Muthusamy if (!dp)
2989a08862aSNagarathnam Muthusamy goto oom;
2999a08862aSNagarathnam Muthusamy
3009a08862aSNagarathnam Muthusamy dpp[0] = dp;
3019a08862aSNagarathnam Muthusamy vvar_data = page_address(dp);
3029a08862aSNagarathnam Muthusamy memset(vvar_data, 0, PAGE_SIZE);
3039a08862aSNagarathnam Muthusamy
3049a08862aSNagarathnam Muthusamy vvar_data->seq = 0;
3059a08862aSNagarathnam Muthusamy }
3069a08862aSNagarathnam Muthusamy
3079a08862aSNagarathnam Muthusamy return 0;
3089a08862aSNagarathnam Muthusamy oom:
3099a08862aSNagarathnam Muthusamy if (cpp != NULL) {
3109a08862aSNagarathnam Muthusamy for (i = 0; i < cnpages; i++) {
3119a08862aSNagarathnam Muthusamy if (cpp[i] != NULL)
3129a08862aSNagarathnam Muthusamy __free_page(cpp[i]);
3139a08862aSNagarathnam Muthusamy }
3149a08862aSNagarathnam Muthusamy kfree(cpp);
3159a08862aSNagarathnam Muthusamy vdso_mapping->pages = NULL;
3169a08862aSNagarathnam Muthusamy }
3179a08862aSNagarathnam Muthusamy
3189a08862aSNagarathnam Muthusamy if (dpp != NULL) {
3199a08862aSNagarathnam Muthusamy for (i = 0; i < dnpages; i++) {
3209a08862aSNagarathnam Muthusamy if (dpp[i] != NULL)
3219a08862aSNagarathnam Muthusamy __free_page(dpp[i]);
3229a08862aSNagarathnam Muthusamy }
3239a08862aSNagarathnam Muthusamy kfree(dpp);
3249a08862aSNagarathnam Muthusamy vvar_mapping.pages = NULL;
3259a08862aSNagarathnam Muthusamy }
3269a08862aSNagarathnam Muthusamy
3279a08862aSNagarathnam Muthusamy pr_warn("Cannot allocate vdso\n");
3289a08862aSNagarathnam Muthusamy vdso_enabled = 0;
3299a08862aSNagarathnam Muthusamy return -ENOMEM;
3309a08862aSNagarathnam Muthusamy }
3319a08862aSNagarathnam Muthusamy
init_vdso(void)3329a08862aSNagarathnam Muthusamy static int __init init_vdso(void)
3339a08862aSNagarathnam Muthusamy {
3349a08862aSNagarathnam Muthusamy int err = 0;
3359a08862aSNagarathnam Muthusamy #ifdef CONFIG_SPARC64
336caf539cdSDavid S. Miller err = init_vdso_image(&vdso_image_64_builtin, &vdso_mapping64, true);
3379a08862aSNagarathnam Muthusamy if (err)
3389a08862aSNagarathnam Muthusamy return err;
3399a08862aSNagarathnam Muthusamy #endif
3409a08862aSNagarathnam Muthusamy
3419a08862aSNagarathnam Muthusamy #ifdef CONFIG_COMPAT
342caf539cdSDavid S. Miller err = init_vdso_image(&vdso_image_32_builtin, &vdso_mapping32, false);
3439a08862aSNagarathnam Muthusamy #endif
3449a08862aSNagarathnam Muthusamy return err;
3459a08862aSNagarathnam Muthusamy
3469a08862aSNagarathnam Muthusamy }
3479a08862aSNagarathnam Muthusamy subsys_initcall(init_vdso);
3489a08862aSNagarathnam Muthusamy
3499a08862aSNagarathnam Muthusamy struct linux_binprm;
3509a08862aSNagarathnam Muthusamy
3519a08862aSNagarathnam Muthusamy /* Shuffle the vdso up a bit, randomly. */
vdso_addr(unsigned long start,unsigned int len)3529a08862aSNagarathnam Muthusamy static unsigned long vdso_addr(unsigned long start, unsigned int len)
3539a08862aSNagarathnam Muthusamy {
3549a08862aSNagarathnam Muthusamy unsigned int offset;
3559a08862aSNagarathnam Muthusamy
3569a08862aSNagarathnam Muthusamy /* This loses some more bits than a modulo, but is cheaper */
3578032bf12SJason A. Donenfeld offset = get_random_u32_below(PTRS_PER_PTE);
3589a08862aSNagarathnam Muthusamy return start + (offset << PAGE_SHIFT);
3599a08862aSNagarathnam Muthusamy }
3609a08862aSNagarathnam Muthusamy
map_vdso(const struct vdso_image * image,struct vm_special_mapping * vdso_mapping)3619a08862aSNagarathnam Muthusamy static int map_vdso(const struct vdso_image *image,
3629a08862aSNagarathnam Muthusamy struct vm_special_mapping *vdso_mapping)
3639a08862aSNagarathnam Muthusamy {
3649a08862aSNagarathnam Muthusamy struct mm_struct *mm = current->mm;
3659a08862aSNagarathnam Muthusamy struct vm_area_struct *vma;
3669a08862aSNagarathnam Muthusamy unsigned long text_start, addr = 0;
3679a08862aSNagarathnam Muthusamy int ret = 0;
3689a08862aSNagarathnam Muthusamy
369d8ed45c5SMichel Lespinasse mmap_write_lock(mm);
3709a08862aSNagarathnam Muthusamy
3719a08862aSNagarathnam Muthusamy /*
3729a08862aSNagarathnam Muthusamy * First, get an unmapped region: then randomize it, and make sure that
3739a08862aSNagarathnam Muthusamy * region is free.
3749a08862aSNagarathnam Muthusamy */
3759a08862aSNagarathnam Muthusamy if (current->flags & PF_RANDOMIZE) {
3769a08862aSNagarathnam Muthusamy addr = get_unmapped_area(NULL, 0,
3779a08862aSNagarathnam Muthusamy image->size - image->sym_vvar_start,
3789a08862aSNagarathnam Muthusamy 0, 0);
3799a08862aSNagarathnam Muthusamy if (IS_ERR_VALUE(addr)) {
3809a08862aSNagarathnam Muthusamy ret = addr;
3819a08862aSNagarathnam Muthusamy goto up_fail;
3829a08862aSNagarathnam Muthusamy }
3839a08862aSNagarathnam Muthusamy addr = vdso_addr(addr, image->size - image->sym_vvar_start);
3849a08862aSNagarathnam Muthusamy }
3859a08862aSNagarathnam Muthusamy addr = get_unmapped_area(NULL, addr,
3869a08862aSNagarathnam Muthusamy image->size - image->sym_vvar_start, 0, 0);
3879a08862aSNagarathnam Muthusamy if (IS_ERR_VALUE(addr)) {
3889a08862aSNagarathnam Muthusamy ret = addr;
3899a08862aSNagarathnam Muthusamy goto up_fail;
3909a08862aSNagarathnam Muthusamy }
3919a08862aSNagarathnam Muthusamy
3929a08862aSNagarathnam Muthusamy text_start = addr - image->sym_vvar_start;
3939a08862aSNagarathnam Muthusamy current->mm->context.vdso = (void __user *)text_start;
3949a08862aSNagarathnam Muthusamy
3959a08862aSNagarathnam Muthusamy /*
3969a08862aSNagarathnam Muthusamy * MAYWRITE to allow gdb to COW and set breakpoints
3979a08862aSNagarathnam Muthusamy */
3989a08862aSNagarathnam Muthusamy vma = _install_special_mapping(mm,
3999a08862aSNagarathnam Muthusamy text_start,
4009a08862aSNagarathnam Muthusamy image->size,
4019a08862aSNagarathnam Muthusamy VM_READ|VM_EXEC|
4029a08862aSNagarathnam Muthusamy VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
4039a08862aSNagarathnam Muthusamy vdso_mapping);
4049a08862aSNagarathnam Muthusamy
4059a08862aSNagarathnam Muthusamy if (IS_ERR(vma)) {
4069a08862aSNagarathnam Muthusamy ret = PTR_ERR(vma);
4079a08862aSNagarathnam Muthusamy goto up_fail;
4089a08862aSNagarathnam Muthusamy }
4099a08862aSNagarathnam Muthusamy
4109a08862aSNagarathnam Muthusamy vma = _install_special_mapping(mm,
4119a08862aSNagarathnam Muthusamy addr,
4129a08862aSNagarathnam Muthusamy -image->sym_vvar_start,
4139a08862aSNagarathnam Muthusamy VM_READ|VM_MAYREAD,
4149a08862aSNagarathnam Muthusamy &vvar_mapping);
4159a08862aSNagarathnam Muthusamy
4169a08862aSNagarathnam Muthusamy if (IS_ERR(vma)) {
4179a08862aSNagarathnam Muthusamy ret = PTR_ERR(vma);
4189a08862aSNagarathnam Muthusamy do_munmap(mm, text_start, image->size, NULL);
4199a08862aSNagarathnam Muthusamy }
4209a08862aSNagarathnam Muthusamy
4219a08862aSNagarathnam Muthusamy up_fail:
4229a08862aSNagarathnam Muthusamy if (ret)
4239a08862aSNagarathnam Muthusamy current->mm->context.vdso = NULL;
4249a08862aSNagarathnam Muthusamy
425d8ed45c5SMichel Lespinasse mmap_write_unlock(mm);
4269a08862aSNagarathnam Muthusamy return ret;
4279a08862aSNagarathnam Muthusamy }
4289a08862aSNagarathnam Muthusamy
arch_setup_additional_pages(struct linux_binprm * bprm,int uses_interp)4299a08862aSNagarathnam Muthusamy int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
4309a08862aSNagarathnam Muthusamy {
4319a08862aSNagarathnam Muthusamy
4329a08862aSNagarathnam Muthusamy if (!vdso_enabled)
4339a08862aSNagarathnam Muthusamy return 0;
4349a08862aSNagarathnam Muthusamy
4359a08862aSNagarathnam Muthusamy #if defined CONFIG_COMPAT
4369a08862aSNagarathnam Muthusamy if (!(is_32bit_task()))
4379a08862aSNagarathnam Muthusamy return map_vdso(&vdso_image_64_builtin, &vdso_mapping64);
4389a08862aSNagarathnam Muthusamy else
4399a08862aSNagarathnam Muthusamy return map_vdso(&vdso_image_32_builtin, &vdso_mapping32);
4409a08862aSNagarathnam Muthusamy #else
4419a08862aSNagarathnam Muthusamy return map_vdso(&vdso_image_64_builtin, &vdso_mapping64);
4429a08862aSNagarathnam Muthusamy #endif
4439a08862aSNagarathnam Muthusamy
4449a08862aSNagarathnam Muthusamy }
4459a08862aSNagarathnam Muthusamy
vdso_setup(char * s)4469a08862aSNagarathnam Muthusamy static __init int vdso_setup(char *s)
4479a08862aSNagarathnam Muthusamy {
4489a08862aSNagarathnam Muthusamy int err;
4499a08862aSNagarathnam Muthusamy unsigned long val;
4509a08862aSNagarathnam Muthusamy
4519a08862aSNagarathnam Muthusamy err = kstrtoul(s, 10, &val);
452*e8ac8003SRandy Dunlap if (!err)
45362d6f3b7SDan Carpenter vdso_enabled = val;
454*e8ac8003SRandy Dunlap return 1;
4559a08862aSNagarathnam Muthusamy }
4569a08862aSNagarathnam Muthusamy __setup("vdso=", vdso_setup);
457