xref: /linux/arch/sparc/vdso/vma.c (revision caf539cd1087f7c36b9c4df271575e9aee49fde5)
19a08862aSNagarathnam Muthusamy /*
29a08862aSNagarathnam Muthusamy  * Set up the VMAs to tell the VM about the vDSO.
39a08862aSNagarathnam Muthusamy  * Copyright 2007 Andi Kleen, SUSE Labs.
49a08862aSNagarathnam Muthusamy  * Subject to the GPL, v.2
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 
45*caf539cdSDavid S. Miller struct vdso_elfinfo32 {
46*caf539cdSDavid S. Miller 	Elf32_Ehdr	*hdr;
47*caf539cdSDavid S. Miller 	Elf32_Sym	*dynsym;
48*caf539cdSDavid S. Miller 	unsigned long	dynsymsize;
49*caf539cdSDavid S. Miller 	const char	*dynstr;
50*caf539cdSDavid S. Miller 	unsigned long	text;
512f6c9bf3SDavid S. Miller };
522f6c9bf3SDavid S. Miller 
53*caf539cdSDavid S. Miller struct vdso_elfinfo64 {
54*caf539cdSDavid S. Miller 	Elf64_Ehdr	*hdr;
55*caf539cdSDavid S. Miller 	Elf64_Sym	*dynsym;
56*caf539cdSDavid S. Miller 	unsigned long	dynsymsize;
57*caf539cdSDavid S. Miller 	const char	*dynstr;
58*caf539cdSDavid S. Miller 	unsigned long	text;
59*caf539cdSDavid S. Miller };
60*caf539cdSDavid S. Miller 
61*caf539cdSDavid S. Miller struct vdso_elfinfo {
62*caf539cdSDavid S. Miller 	union {
63*caf539cdSDavid S. Miller 		struct vdso_elfinfo32 elf32;
64*caf539cdSDavid S. Miller 		struct vdso_elfinfo64 elf64;
65*caf539cdSDavid S. Miller 	} u;
66*caf539cdSDavid S. Miller };
67*caf539cdSDavid S. Miller 
68*caf539cdSDavid S. Miller static void *one_section64(struct vdso_elfinfo64 *e, const char *name,
69*caf539cdSDavid S. Miller 			   unsigned long *size)
702f6c9bf3SDavid S. Miller {
71*caf539cdSDavid S. Miller 	const char *snames;
72*caf539cdSDavid S. Miller 	Elf64_Shdr *shdrs;
73*caf539cdSDavid S. Miller 	unsigned int i;
742f6c9bf3SDavid S. Miller 
75*caf539cdSDavid S. Miller 	shdrs = (void *)e->hdr + e->hdr->e_shoff;
76*caf539cdSDavid S. Miller 	snames = (void *)e->hdr + shdrs[e->hdr->e_shstrndx].sh_offset;
77*caf539cdSDavid S. Miller 	for (i = 1; i < e->hdr->e_shnum; i++) {
78*caf539cdSDavid S. Miller 		if (!strcmp(snames+shdrs[i].sh_name, name)) {
79*caf539cdSDavid S. Miller 			if (size)
80*caf539cdSDavid S. Miller 				*size = shdrs[i].sh_size;
81*caf539cdSDavid S. Miller 			return (void *)e->hdr + shdrs[i].sh_offset;
822f6c9bf3SDavid S. Miller 		}
832f6c9bf3SDavid S. Miller 	}
84*caf539cdSDavid S. Miller 	return NULL;
85*caf539cdSDavid S. Miller }
86*caf539cdSDavid S. Miller 
87*caf539cdSDavid S. Miller static int find_sections64(const struct vdso_image *image, struct vdso_elfinfo *_e)
88*caf539cdSDavid S. Miller {
89*caf539cdSDavid S. Miller 	struct vdso_elfinfo64 *e = &_e->u.elf64;
90*caf539cdSDavid S. Miller 
91*caf539cdSDavid S. Miller 	e->hdr = image->data;
92*caf539cdSDavid S. Miller 	e->dynsym = one_section64(e, ".dynsym", &e->dynsymsize);
93*caf539cdSDavid S. Miller 	e->dynstr = one_section64(e, ".dynstr", NULL);
94*caf539cdSDavid S. Miller 
95*caf539cdSDavid S. Miller 	if (!e->dynsym || !e->dynstr) {
96*caf539cdSDavid S. Miller 		pr_err("VDSO64: Missing symbol sections.\n");
97*caf539cdSDavid S. Miller 		return -ENODEV;
98*caf539cdSDavid S. Miller 	}
99*caf539cdSDavid S. Miller 	return 0;
100*caf539cdSDavid S. Miller }
101*caf539cdSDavid S. Miller 
102*caf539cdSDavid S. Miller static Elf64_Sym *find_sym64(const struct vdso_elfinfo64 *e, const char *name)
103*caf539cdSDavid S. Miller {
104*caf539cdSDavid S. Miller 	unsigned int i;
105*caf539cdSDavid S. Miller 
106*caf539cdSDavid S. Miller 	for (i = 0; i < (e->dynsymsize / sizeof(Elf64_Sym)); i++) {
107*caf539cdSDavid S. Miller 		Elf64_Sym *s = &e->dynsym[i];
108*caf539cdSDavid S. Miller 		if (s->st_name == 0)
109*caf539cdSDavid S. Miller 			continue;
110*caf539cdSDavid S. Miller 		if (!strcmp(e->dynstr + s->st_name, name))
111*caf539cdSDavid S. Miller 			return s;
112*caf539cdSDavid S. Miller 	}
113*caf539cdSDavid S. Miller 	return NULL;
114*caf539cdSDavid S. Miller }
115*caf539cdSDavid S. Miller 
116*caf539cdSDavid S. Miller static int patchsym64(struct vdso_elfinfo *_e, const char *orig,
117*caf539cdSDavid S. Miller 		      const char *new)
118*caf539cdSDavid S. Miller {
119*caf539cdSDavid S. Miller 	struct vdso_elfinfo64 *e = &_e->u.elf64;
120*caf539cdSDavid S. Miller 	Elf64_Sym *osym = find_sym64(e, orig);
121*caf539cdSDavid S. Miller 	Elf64_Sym *nsym = find_sym64(e, new);
122*caf539cdSDavid S. Miller 
123*caf539cdSDavid S. Miller 	if (!nsym || !osym) {
124*caf539cdSDavid S. Miller 		pr_err("VDSO64: Missing symbols.\n");
125*caf539cdSDavid S. Miller 		return -ENODEV;
126*caf539cdSDavid S. Miller 	}
127*caf539cdSDavid S. Miller 	osym->st_value = nsym->st_value;
128*caf539cdSDavid S. Miller 	osym->st_size = nsym->st_size;
129*caf539cdSDavid S. Miller 	osym->st_info = nsym->st_info;
130*caf539cdSDavid S. Miller 	osym->st_other = nsym->st_other;
131*caf539cdSDavid S. Miller 	osym->st_shndx = nsym->st_shndx;
132*caf539cdSDavid S. Miller 
133*caf539cdSDavid S. Miller 	return 0;
134*caf539cdSDavid S. Miller }
135*caf539cdSDavid S. Miller 
136*caf539cdSDavid S. Miller static void *one_section32(struct vdso_elfinfo32 *e, const char *name,
137*caf539cdSDavid S. Miller 			   unsigned long *size)
138*caf539cdSDavid S. Miller {
139*caf539cdSDavid S. Miller 	const char *snames;
140*caf539cdSDavid S. Miller 	Elf32_Shdr *shdrs;
141*caf539cdSDavid S. Miller 	unsigned int i;
142*caf539cdSDavid S. Miller 
143*caf539cdSDavid S. Miller 	shdrs = (void *)e->hdr + e->hdr->e_shoff;
144*caf539cdSDavid S. Miller 	snames = (void *)e->hdr + shdrs[e->hdr->e_shstrndx].sh_offset;
145*caf539cdSDavid S. Miller 	for (i = 1; i < e->hdr->e_shnum; i++) {
146*caf539cdSDavid S. Miller 		if (!strcmp(snames+shdrs[i].sh_name, name)) {
147*caf539cdSDavid S. Miller 			if (size)
148*caf539cdSDavid S. Miller 				*size = shdrs[i].sh_size;
149*caf539cdSDavid S. Miller 			return (void *)e->hdr + shdrs[i].sh_offset;
150*caf539cdSDavid S. Miller 		}
151*caf539cdSDavid S. Miller 	}
152*caf539cdSDavid S. Miller 	return NULL;
153*caf539cdSDavid S. Miller }
154*caf539cdSDavid S. Miller 
155*caf539cdSDavid S. Miller static int find_sections32(const struct vdso_image *image, struct vdso_elfinfo *_e)
156*caf539cdSDavid S. Miller {
157*caf539cdSDavid S. Miller 	struct vdso_elfinfo32 *e = &_e->u.elf32;
158*caf539cdSDavid S. Miller 
159*caf539cdSDavid S. Miller 	e->hdr = image->data;
160*caf539cdSDavid S. Miller 	e->dynsym = one_section32(e, ".dynsym", &e->dynsymsize);
161*caf539cdSDavid S. Miller 	e->dynstr = one_section32(e, ".dynstr", NULL);
162*caf539cdSDavid S. Miller 
163*caf539cdSDavid S. Miller 	if (!e->dynsym || !e->dynstr) {
164*caf539cdSDavid S. Miller 		pr_err("VDSO32: Missing symbol sections.\n");
165*caf539cdSDavid S. Miller 		return -ENODEV;
166*caf539cdSDavid S. Miller 	}
167*caf539cdSDavid S. Miller 	return 0;
168*caf539cdSDavid S. Miller }
169*caf539cdSDavid S. Miller 
170*caf539cdSDavid S. Miller static Elf32_Sym *find_sym32(const struct vdso_elfinfo32 *e, const char *name)
171*caf539cdSDavid S. Miller {
172*caf539cdSDavid S. Miller 	unsigned int i;
173*caf539cdSDavid S. Miller 
174*caf539cdSDavid S. Miller 	for (i = 0; i < (e->dynsymsize / sizeof(Elf32_Sym)); i++) {
175*caf539cdSDavid S. Miller 		Elf32_Sym *s = &e->dynsym[i];
176*caf539cdSDavid S. Miller 		if (s->st_name == 0)
177*caf539cdSDavid S. Miller 			continue;
178*caf539cdSDavid S. Miller 		if (!strcmp(e->dynstr + s->st_name, name))
179*caf539cdSDavid S. Miller 			return s;
180*caf539cdSDavid S. Miller 	}
181*caf539cdSDavid S. Miller 	return NULL;
182*caf539cdSDavid S. Miller }
183*caf539cdSDavid S. Miller 
184*caf539cdSDavid S. Miller static int patchsym32(struct vdso_elfinfo *_e, const char *orig,
185*caf539cdSDavid S. Miller 		      const char *new)
186*caf539cdSDavid S. Miller {
187*caf539cdSDavid S. Miller 	struct vdso_elfinfo32 *e = &_e->u.elf32;
188*caf539cdSDavid S. Miller 	Elf32_Sym *osym = find_sym32(e, orig);
189*caf539cdSDavid S. Miller 	Elf32_Sym *nsym = find_sym32(e, new);
190*caf539cdSDavid S. Miller 
191*caf539cdSDavid S. Miller 	if (!nsym || !osym) {
192*caf539cdSDavid S. Miller 		pr_err("VDSO32: Missing symbols.\n");
193*caf539cdSDavid S. Miller 		return -ENODEV;
194*caf539cdSDavid S. Miller 	}
195*caf539cdSDavid S. Miller 	osym->st_value = nsym->st_value;
196*caf539cdSDavid S. Miller 	osym->st_size = nsym->st_size;
197*caf539cdSDavid S. Miller 	osym->st_info = nsym->st_info;
198*caf539cdSDavid S. Miller 	osym->st_other = nsym->st_other;
199*caf539cdSDavid S. Miller 	osym->st_shndx = nsym->st_shndx;
200*caf539cdSDavid S. Miller 
201*caf539cdSDavid S. Miller 	return 0;
202*caf539cdSDavid S. Miller }
203*caf539cdSDavid S. Miller 
204*caf539cdSDavid S. Miller static int find_sections(const struct vdso_image *image, struct vdso_elfinfo *e,
205*caf539cdSDavid S. Miller 			 bool elf64)
206*caf539cdSDavid S. Miller {
207*caf539cdSDavid S. Miller 	if (elf64)
208*caf539cdSDavid S. Miller 		return find_sections64(image, e);
209*caf539cdSDavid S. Miller 	else
210*caf539cdSDavid S. Miller 		return find_sections32(image, e);
211*caf539cdSDavid S. Miller }
212*caf539cdSDavid S. Miller 
213*caf539cdSDavid S. Miller static int patch_one_symbol(struct vdso_elfinfo *e, const char *orig,
214*caf539cdSDavid S. Miller 			    const char *new_target, bool elf64)
215*caf539cdSDavid S. Miller {
216*caf539cdSDavid S. Miller 	if (elf64)
217*caf539cdSDavid S. Miller 		return patchsym64(e, orig, new_target);
218*caf539cdSDavid S. Miller 	else
219*caf539cdSDavid S. Miller 		return patchsym32(e, orig, new_target);
220*caf539cdSDavid S. Miller }
221*caf539cdSDavid S. Miller 
222*caf539cdSDavid S. Miller static int stick_patch(const struct vdso_image *image, struct vdso_elfinfo *e, bool elf64)
223*caf539cdSDavid S. Miller {
224*caf539cdSDavid S. Miller 	int err;
225*caf539cdSDavid S. Miller 
226*caf539cdSDavid S. Miller 	err = find_sections(image, e, elf64);
227*caf539cdSDavid S. Miller 	if (err)
228*caf539cdSDavid S. Miller 		return err;
229*caf539cdSDavid S. Miller 
230*caf539cdSDavid S. Miller 	err = patch_one_symbol(e,
231*caf539cdSDavid S. Miller 			       "__vdso_gettimeofday",
232*caf539cdSDavid S. Miller 			       "__vdso_gettimeofday_stick", elf64);
233*caf539cdSDavid S. Miller 	if (err)
234*caf539cdSDavid S. Miller 		return err;
235*caf539cdSDavid S. Miller 
236*caf539cdSDavid S. Miller 	return patch_one_symbol(e,
237*caf539cdSDavid S. Miller 				"__vdso_clock_gettime",
238*caf539cdSDavid S. Miller 				"__vdso_clock_gettime_stick", elf64);
239*caf539cdSDavid S. Miller 	return 0;
240*caf539cdSDavid 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  */
2469a08862aSNagarathnam Muthusamy int __init init_vdso_image(const struct vdso_image *image,
247*caf539cdSDavid S. Miller 			   struct vm_special_mapping *vdso_mapping, bool elf64)
2489a08862aSNagarathnam Muthusamy {
2499a08862aSNagarathnam Muthusamy 	int cnpages = (image->size) / PAGE_SIZE;
250*caf539cdSDavid S. Miller 	struct page *dp, **dpp = NULL;
251*caf539cdSDavid S. Miller 	struct page *cp, **cpp = NULL;
252*caf539cdSDavid S. Miller 	struct vdso_elfinfo ei;
253*caf539cdSDavid S. Miller 	int i, dnpages = 0;
254*caf539cdSDavid S. Miller 
255*caf539cdSDavid S. Miller 	if (tlb_type != spitfire) {
256*caf539cdSDavid S. Miller 		int err = stick_patch(image, &ei, elf64);
257*caf539cdSDavid S. Miller 		if (err)
258*caf539cdSDavid S. Miller 			return err;
259*caf539cdSDavid 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 
3329a08862aSNagarathnam Muthusamy static int __init init_vdso(void)
3339a08862aSNagarathnam Muthusamy {
3349a08862aSNagarathnam Muthusamy 	int err = 0;
3359a08862aSNagarathnam Muthusamy #ifdef CONFIG_SPARC64
336*caf539cdSDavid 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
342*caf539cdSDavid 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. */
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 */
3579a08862aSNagarathnam Muthusamy 	offset = get_random_int() & (PTRS_PER_PTE - 1);
3589a08862aSNagarathnam Muthusamy 	return start + (offset << PAGE_SHIFT);
3599a08862aSNagarathnam Muthusamy }
3609a08862aSNagarathnam Muthusamy 
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 
3699a08862aSNagarathnam Muthusamy 	down_write(&mm->mmap_sem);
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 
4259a08862aSNagarathnam Muthusamy 	up_write(&mm->mmap_sem);
4269a08862aSNagarathnam Muthusamy 	return ret;
4279a08862aSNagarathnam Muthusamy }
4289a08862aSNagarathnam Muthusamy 
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 
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);
45262d6f3b7SDan Carpenter 	if (err)
4539a08862aSNagarathnam Muthusamy 		return err;
45462d6f3b7SDan Carpenter 	vdso_enabled = val;
45562d6f3b7SDan Carpenter 	return 0;
4569a08862aSNagarathnam Muthusamy }
4579a08862aSNagarathnam Muthusamy __setup("vdso=", vdso_setup);
458