xref: /linux/arch/powerpc/kernel/vdso.c (revision 550e6074c106e1a6fb57dfef62f0daede12d832c)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /*
4  *    Copyright (C) 2004 Benjamin Herrenschmidt, IBM Corp.
5  *			 <benh@kernel.crashing.org>
6  */
7 
8 #include <linux/errno.h>
9 #include <linux/sched.h>
10 #include <linux/kernel.h>
11 #include <linux/mm.h>
12 #include <linux/smp.h>
13 #include <linux/stddef.h>
14 #include <linux/unistd.h>
15 #include <linux/slab.h>
16 #include <linux/user.h>
17 #include <linux/elf.h>
18 #include <linux/security.h>
19 #include <linux/memblock.h>
20 #include <linux/syscalls.h>
21 #include <vdso/datapage.h>
22 
23 #include <asm/syscall.h>
24 #include <asm/processor.h>
25 #include <asm/mmu.h>
26 #include <asm/mmu_context.h>
27 #include <asm/prom.h>
28 #include <asm/machdep.h>
29 #include <asm/cputable.h>
30 #include <asm/sections.h>
31 #include <asm/firmware.h>
32 #include <asm/vdso.h>
33 #include <asm/vdso_datapage.h>
34 #include <asm/setup.h>
35 
36 #undef DEBUG
37 
38 #ifdef DEBUG
39 #define DBG(fmt...) printk(fmt)
40 #else
41 #define DBG(fmt...)
42 #endif
43 
44 /* Max supported size for symbol names */
45 #define MAX_SYMNAME	64
46 
47 /* The alignment of the vDSO */
48 #define VDSO_ALIGNMENT	(1 << 16)
49 
50 static unsigned int vdso32_pages;
51 static void *vdso32_kbase;
52 unsigned long vdso32_sigtramp;
53 unsigned long vdso32_rt_sigtramp;
54 
55 extern char vdso32_start, vdso32_end;
56 extern char vdso64_start, vdso64_end;
57 static void *vdso64_kbase = &vdso64_start;
58 static unsigned int vdso64_pages;
59 #ifdef CONFIG_PPC64
60 unsigned long vdso64_rt_sigtramp;
61 #endif /* CONFIG_PPC64 */
62 
63 static int vdso_ready;
64 
65 /*
66  * The vdso data page (aka. systemcfg for old ppc64 fans) is here.
67  * Once the early boot kernel code no longer needs to muck around
68  * with it, it will become dynamically allocated
69  */
70 static union {
71 	struct vdso_arch_data	data;
72 	u8			page[PAGE_SIZE];
73 } vdso_data_store __page_aligned_data;
74 struct vdso_arch_data *vdso_data = &vdso_data_store.data;
75 
76 /* Format of the patch table */
77 struct vdso_patch_def
78 {
79 	unsigned long	ftr_mask, ftr_value;
80 	const char	*gen_name;
81 	const char	*fix_name;
82 };
83 
84 /* Table of functions to patch based on the CPU type/revision
85  *
86  * Currently, we only change sync_dicache to do nothing on processors
87  * with a coherent icache
88  */
89 static struct vdso_patch_def vdso_patches[] = {
90 	{
91 		CPU_FTR_COHERENT_ICACHE, CPU_FTR_COHERENT_ICACHE,
92 		"__kernel_sync_dicache", "__kernel_sync_dicache_p5"
93 	},
94 };
95 
96 /*
97  * Some infos carried around for each of them during parsing at
98  * boot time.
99  */
100 struct lib32_elfinfo
101 {
102 	Elf32_Ehdr	*hdr;		/* ptr to ELF */
103 	Elf32_Sym	*dynsym;	/* ptr to .dynsym section */
104 	unsigned long	dynsymsize;	/* size of .dynsym section */
105 	char		*dynstr;	/* ptr to .dynstr section */
106 	unsigned long	text;		/* offset of .text section in .so */
107 };
108 
109 struct lib64_elfinfo
110 {
111 	Elf64_Ehdr	*hdr;
112 	Elf64_Sym	*dynsym;
113 	unsigned long	dynsymsize;
114 	char		*dynstr;
115 	unsigned long	text;
116 };
117 
118 static int vdso_mremap(const struct vm_special_mapping *sm, struct vm_area_struct *new_vma,
119 		       unsigned long text_size)
120 {
121 	unsigned long new_size = new_vma->vm_end - new_vma->vm_start;
122 
123 	if (new_size != text_size + PAGE_SIZE)
124 		return -EINVAL;
125 
126 	current->mm->context.vdso = (void __user *)new_vma->vm_start + PAGE_SIZE;
127 
128 	return 0;
129 }
130 
131 static int vdso32_mremap(const struct vm_special_mapping *sm, struct vm_area_struct *new_vma)
132 {
133 	return vdso_mremap(sm, new_vma, &vdso32_end - &vdso32_start);
134 }
135 
136 static int vdso64_mremap(const struct vm_special_mapping *sm, struct vm_area_struct *new_vma)
137 {
138 	return vdso_mremap(sm, new_vma, &vdso64_end - &vdso64_start);
139 }
140 
141 static struct vm_special_mapping vdso32_spec __ro_after_init = {
142 	.name = "[vdso]",
143 	.mremap = vdso32_mremap,
144 };
145 
146 static struct vm_special_mapping vdso64_spec __ro_after_init = {
147 	.name = "[vdso]",
148 	.mremap = vdso64_mremap,
149 };
150 
151 /*
152  * This is called from binfmt_elf, we create the special vma for the
153  * vDSO and insert it into the mm struct tree
154  */
155 static int __arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
156 {
157 	struct mm_struct *mm = current->mm;
158 	struct vm_special_mapping *vdso_spec;
159 	struct vm_area_struct *vma;
160 	unsigned long vdso_size;
161 	unsigned long vdso_base;
162 
163 	if (is_32bit_task()) {
164 		vdso_spec = &vdso32_spec;
165 		vdso_size = &vdso32_end - &vdso32_start;
166 		vdso_base = VDSO32_MBASE;
167 	} else {
168 		vdso_spec = &vdso64_spec;
169 		vdso_size = &vdso64_end - &vdso64_start;
170 		/*
171 		 * On 64bit we don't have a preferred map address. This
172 		 * allows get_unmapped_area to find an area near other mmaps
173 		 * and most likely share a SLB entry.
174 		 */
175 		vdso_base = 0;
176 	}
177 
178 	/* Add a page to the vdso size for the data page */
179 	vdso_size += PAGE_SIZE;
180 
181 	/*
182 	 * pick a base address for the vDSO in process space. We try to put it
183 	 * at vdso_base which is the "natural" base for it, but we might fail
184 	 * and end up putting it elsewhere.
185 	 * Add enough to the size so that the result can be aligned.
186 	 */
187 	vdso_base = get_unmapped_area(NULL, vdso_base,
188 				      vdso_size + ((VDSO_ALIGNMENT - 1) & PAGE_MASK),
189 				      0, 0);
190 	if (IS_ERR_VALUE(vdso_base))
191 		return vdso_base;
192 
193 	/* Add required alignment. */
194 	vdso_base = ALIGN(vdso_base, VDSO_ALIGNMENT);
195 
196 	/*
197 	 * Put vDSO base into mm struct. We need to do this before calling
198 	 * install_special_mapping or the perf counter mmap tracking code
199 	 * will fail to recognise it as a vDSO.
200 	 */
201 	mm->context.vdso = (void __user *)vdso_base + PAGE_SIZE;
202 
203 	/*
204 	 * our vma flags don't have VM_WRITE so by default, the process isn't
205 	 * allowed to write those pages.
206 	 * gdb can break that with ptrace interface, and thus trigger COW on
207 	 * those pages but it's then your responsibility to never do that on
208 	 * the "data" page of the vDSO or you'll stop getting kernel updates
209 	 * and your nice userland gettimeofday will be totally dead.
210 	 * It's fine to use that for setting breakpoints in the vDSO code
211 	 * pages though.
212 	 */
213 	vma = _install_special_mapping(mm, vdso_base, vdso_size,
214 				       VM_READ | VM_EXEC | VM_MAYREAD |
215 				       VM_MAYWRITE | VM_MAYEXEC, vdso_spec);
216 	return PTR_ERR_OR_ZERO(vma);
217 }
218 
219 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
220 {
221 	struct mm_struct *mm = current->mm;
222 	int rc;
223 
224 	mm->context.vdso = NULL;
225 
226 	if (!vdso_ready)
227 		return 0;
228 
229 	if (mmap_write_lock_killable(mm))
230 		return -EINTR;
231 
232 	rc = __arch_setup_additional_pages(bprm, uses_interp);
233 	if (rc)
234 		mm->context.vdso = NULL;
235 
236 	mmap_write_unlock(mm);
237 	return rc;
238 }
239 
240 #ifdef CONFIG_VDSO32
241 static void * __init find_section32(Elf32_Ehdr *ehdr, const char *secname,
242 				  unsigned long *size)
243 {
244 	Elf32_Shdr *sechdrs;
245 	unsigned int i;
246 	char *secnames;
247 
248 	/* Grab section headers and strings so we can tell who is who */
249 	sechdrs = (void *)ehdr + ehdr->e_shoff;
250 	secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
251 
252 	/* Find the section they want */
253 	for (i = 1; i < ehdr->e_shnum; i++) {
254 		if (strcmp(secnames+sechdrs[i].sh_name, secname) == 0) {
255 			if (size)
256 				*size = sechdrs[i].sh_size;
257 			return (void *)ehdr + sechdrs[i].sh_offset;
258 		}
259 	}
260 	*size = 0;
261 	return NULL;
262 }
263 
264 static Elf32_Sym * __init find_symbol32(struct lib32_elfinfo *lib,
265 					const char *symname)
266 {
267 	unsigned int i;
268 	char name[MAX_SYMNAME], *c;
269 
270 	for (i = 0; i < (lib->dynsymsize / sizeof(Elf32_Sym)); i++) {
271 		if (lib->dynsym[i].st_name == 0)
272 			continue;
273 		strlcpy(name, lib->dynstr + lib->dynsym[i].st_name,
274 			MAX_SYMNAME);
275 		c = strchr(name, '@');
276 		if (c)
277 			*c = 0;
278 		if (strcmp(symname, name) == 0)
279 			return &lib->dynsym[i];
280 	}
281 	return NULL;
282 }
283 
284 /* Note that we assume the section is .text and the symbol is relative to
285  * the library base
286  */
287 static unsigned long __init find_function32(struct lib32_elfinfo *lib,
288 					    const char *symname)
289 {
290 	Elf32_Sym *sym = find_symbol32(lib, symname);
291 
292 	if (sym == NULL) {
293 		printk(KERN_WARNING "vDSO32: function %s not found !\n",
294 		       symname);
295 		return 0;
296 	}
297 	return sym->st_value - VDSO32_LBASE;
298 }
299 
300 static int __init vdso_do_func_patch32(struct lib32_elfinfo *v32,
301 				       struct lib64_elfinfo *v64,
302 				       const char *orig, const char *fix)
303 {
304 	Elf32_Sym *sym32_gen, *sym32_fix;
305 
306 	sym32_gen = find_symbol32(v32, orig);
307 	if (sym32_gen == NULL) {
308 		printk(KERN_ERR "vDSO32: Can't find symbol %s !\n", orig);
309 		return -1;
310 	}
311 	if (fix == NULL) {
312 		sym32_gen->st_name = 0;
313 		return 0;
314 	}
315 	sym32_fix = find_symbol32(v32, fix);
316 	if (sym32_fix == NULL) {
317 		printk(KERN_ERR "vDSO32: Can't find symbol %s !\n", fix);
318 		return -1;
319 	}
320 	sym32_gen->st_value = sym32_fix->st_value;
321 	sym32_gen->st_size = sym32_fix->st_size;
322 	sym32_gen->st_info = sym32_fix->st_info;
323 	sym32_gen->st_other = sym32_fix->st_other;
324 	sym32_gen->st_shndx = sym32_fix->st_shndx;
325 
326 	return 0;
327 }
328 #else /* !CONFIG_VDSO32 */
329 static unsigned long __init find_function32(struct lib32_elfinfo *lib,
330 					    const char *symname)
331 {
332 	return 0;
333 }
334 
335 static int __init vdso_do_func_patch32(struct lib32_elfinfo *v32,
336 				       struct lib64_elfinfo *v64,
337 				       const char *orig, const char *fix)
338 {
339 	return 0;
340 }
341 #endif /* CONFIG_VDSO32 */
342 
343 
344 #ifdef CONFIG_PPC64
345 
346 static void * __init find_section64(Elf64_Ehdr *ehdr, const char *secname,
347 				  unsigned long *size)
348 {
349 	Elf64_Shdr *sechdrs;
350 	unsigned int i;
351 	char *secnames;
352 
353 	/* Grab section headers and strings so we can tell who is who */
354 	sechdrs = (void *)ehdr + ehdr->e_shoff;
355 	secnames = (void *)ehdr + sechdrs[ehdr->e_shstrndx].sh_offset;
356 
357 	/* Find the section they want */
358 	for (i = 1; i < ehdr->e_shnum; i++) {
359 		if (strcmp(secnames+sechdrs[i].sh_name, secname) == 0) {
360 			if (size)
361 				*size = sechdrs[i].sh_size;
362 			return (void *)ehdr + sechdrs[i].sh_offset;
363 		}
364 	}
365 	if (size)
366 		*size = 0;
367 	return NULL;
368 }
369 
370 static Elf64_Sym * __init find_symbol64(struct lib64_elfinfo *lib,
371 					const char *symname)
372 {
373 	unsigned int i;
374 	char name[MAX_SYMNAME], *c;
375 
376 	for (i = 0; i < (lib->dynsymsize / sizeof(Elf64_Sym)); i++) {
377 		if (lib->dynsym[i].st_name == 0)
378 			continue;
379 		strlcpy(name, lib->dynstr + lib->dynsym[i].st_name,
380 			MAX_SYMNAME);
381 		c = strchr(name, '@');
382 		if (c)
383 			*c = 0;
384 		if (strcmp(symname, name) == 0)
385 			return &lib->dynsym[i];
386 	}
387 	return NULL;
388 }
389 
390 /* Note that we assume the section is .text and the symbol is relative to
391  * the library base
392  */
393 static unsigned long __init find_function64(struct lib64_elfinfo *lib,
394 					    const char *symname)
395 {
396 	Elf64_Sym *sym = find_symbol64(lib, symname);
397 
398 	if (sym == NULL) {
399 		printk(KERN_WARNING "vDSO64: function %s not found !\n",
400 		       symname);
401 		return 0;
402 	}
403 	return sym->st_value - VDSO64_LBASE;
404 }
405 
406 static int __init vdso_do_func_patch64(struct lib32_elfinfo *v32,
407 				       struct lib64_elfinfo *v64,
408 				       const char *orig, const char *fix)
409 {
410 	Elf64_Sym *sym64_gen, *sym64_fix;
411 
412 	sym64_gen = find_symbol64(v64, orig);
413 	if (sym64_gen == NULL) {
414 		printk(KERN_ERR "vDSO64: Can't find symbol %s !\n", orig);
415 		return -1;
416 	}
417 	if (fix == NULL) {
418 		sym64_gen->st_name = 0;
419 		return 0;
420 	}
421 	sym64_fix = find_symbol64(v64, fix);
422 	if (sym64_fix == NULL) {
423 		printk(KERN_ERR "vDSO64: Can't find symbol %s !\n", fix);
424 		return -1;
425 	}
426 	sym64_gen->st_value = sym64_fix->st_value;
427 	sym64_gen->st_size = sym64_fix->st_size;
428 	sym64_gen->st_info = sym64_fix->st_info;
429 	sym64_gen->st_other = sym64_fix->st_other;
430 	sym64_gen->st_shndx = sym64_fix->st_shndx;
431 
432 	return 0;
433 }
434 
435 #endif /* CONFIG_PPC64 */
436 
437 
438 static __init int vdso_do_find_sections(struct lib32_elfinfo *v32,
439 					struct lib64_elfinfo *v64)
440 {
441 	void *sect;
442 
443 	/*
444 	 * Locate symbol tables & text section
445 	 */
446 
447 #ifdef CONFIG_VDSO32
448 	v32->dynsym = find_section32(v32->hdr, ".dynsym", &v32->dynsymsize);
449 	v32->dynstr = find_section32(v32->hdr, ".dynstr", NULL);
450 	if (v32->dynsym == NULL || v32->dynstr == NULL) {
451 		printk(KERN_ERR "vDSO32: required symbol section not found\n");
452 		return -1;
453 	}
454 	sect = find_section32(v32->hdr, ".text", NULL);
455 	if (sect == NULL) {
456 		printk(KERN_ERR "vDSO32: the .text section was not found\n");
457 		return -1;
458 	}
459 	v32->text = sect - vdso32_kbase;
460 #endif
461 
462 #ifdef CONFIG_PPC64
463 	v64->dynsym = find_section64(v64->hdr, ".dynsym", &v64->dynsymsize);
464 	v64->dynstr = find_section64(v64->hdr, ".dynstr", NULL);
465 	if (v64->dynsym == NULL || v64->dynstr == NULL) {
466 		printk(KERN_ERR "vDSO64: required symbol section not found\n");
467 		return -1;
468 	}
469 	sect = find_section64(v64->hdr, ".text", NULL);
470 	if (sect == NULL) {
471 		printk(KERN_ERR "vDSO64: the .text section was not found\n");
472 		return -1;
473 	}
474 	v64->text = sect - vdso64_kbase;
475 #endif /* CONFIG_PPC64 */
476 
477 	return 0;
478 }
479 
480 static __init void vdso_setup_trampolines(struct lib32_elfinfo *v32,
481 					  struct lib64_elfinfo *v64)
482 {
483 	/*
484 	 * Find signal trampolines
485 	 */
486 
487 #ifdef CONFIG_PPC64
488 	vdso64_rt_sigtramp = find_function64(v64, "__kernel_sigtramp_rt64");
489 #endif
490 	vdso32_sigtramp	   = find_function32(v32, "__kernel_sigtramp32");
491 	vdso32_rt_sigtramp = find_function32(v32, "__kernel_sigtramp_rt32");
492 }
493 
494 static __init int vdso_fixup_datapage(struct lib32_elfinfo *v32,
495 				       struct lib64_elfinfo *v64)
496 {
497 #ifdef CONFIG_VDSO32
498 	Elf32_Sym *sym32;
499 #endif
500 #ifdef CONFIG_PPC64
501 	Elf64_Sym *sym64;
502 
503        	sym64 = find_symbol64(v64, "__kernel_datapage_offset");
504 	if (sym64 == NULL) {
505 		printk(KERN_ERR "vDSO64: Can't find symbol "
506 		       "__kernel_datapage_offset !\n");
507 		return -1;
508 	}
509 	*((int *)(vdso64_kbase + sym64->st_value - VDSO64_LBASE)) =
510 		-PAGE_SIZE -
511 		(sym64->st_value - VDSO64_LBASE);
512 #endif /* CONFIG_PPC64 */
513 
514 #ifdef CONFIG_VDSO32
515 	sym32 = find_symbol32(v32, "__kernel_datapage_offset");
516 	if (sym32 == NULL) {
517 		printk(KERN_ERR "vDSO32: Can't find symbol "
518 		       "__kernel_datapage_offset !\n");
519 		return -1;
520 	}
521 	*((int *)(vdso32_kbase + (sym32->st_value - VDSO32_LBASE))) =
522 		-PAGE_SIZE -
523 		(sym32->st_value - VDSO32_LBASE);
524 #endif
525 
526 	return 0;
527 }
528 
529 
530 static __init int vdso_fixup_features(struct lib32_elfinfo *v32,
531 				      struct lib64_elfinfo *v64)
532 {
533 	unsigned long size;
534 	void *start;
535 
536 #ifdef CONFIG_PPC64
537 	start = find_section64(v64->hdr, "__ftr_fixup", &size);
538 	if (start)
539 		do_feature_fixups(cur_cpu_spec->cpu_features,
540 				  start, start + size);
541 
542 	start = find_section64(v64->hdr, "__mmu_ftr_fixup", &size);
543 	if (start)
544 		do_feature_fixups(cur_cpu_spec->mmu_features,
545 				  start, start + size);
546 
547 	start = find_section64(v64->hdr, "__fw_ftr_fixup", &size);
548 	if (start)
549 		do_feature_fixups(powerpc_firmware_features,
550 				  start, start + size);
551 
552 	start = find_section64(v64->hdr, "__lwsync_fixup", &size);
553 	if (start)
554 		do_lwsync_fixups(cur_cpu_spec->cpu_features,
555 				 start, start + size);
556 #endif /* CONFIG_PPC64 */
557 
558 #ifdef CONFIG_VDSO32
559 	start = find_section32(v32->hdr, "__ftr_fixup", &size);
560 	if (start)
561 		do_feature_fixups(cur_cpu_spec->cpu_features,
562 				  start, start + size);
563 
564 	start = find_section32(v32->hdr, "__mmu_ftr_fixup", &size);
565 	if (start)
566 		do_feature_fixups(cur_cpu_spec->mmu_features,
567 				  start, start + size);
568 
569 #ifdef CONFIG_PPC64
570 	start = find_section32(v32->hdr, "__fw_ftr_fixup", &size);
571 	if (start)
572 		do_feature_fixups(powerpc_firmware_features,
573 				  start, start + size);
574 #endif /* CONFIG_PPC64 */
575 
576 	start = find_section32(v32->hdr, "__lwsync_fixup", &size);
577 	if (start)
578 		do_lwsync_fixups(cur_cpu_spec->cpu_features,
579 				 start, start + size);
580 #endif
581 
582 	return 0;
583 }
584 
585 static __init int vdso_fixup_alt_funcs(struct lib32_elfinfo *v32,
586 				       struct lib64_elfinfo *v64)
587 {
588 	int i;
589 
590 	for (i = 0; i < ARRAY_SIZE(vdso_patches); i++) {
591 		struct vdso_patch_def *patch = &vdso_patches[i];
592 		int match = (cur_cpu_spec->cpu_features & patch->ftr_mask)
593 			== patch->ftr_value;
594 		if (!match)
595 			continue;
596 
597 		DBG("replacing %s with %s...\n", patch->gen_name,
598 		    patch->fix_name ? "NONE" : patch->fix_name);
599 
600 		/*
601 		 * Patch the 32 bits and 64 bits symbols. Note that we do not
602 		 * patch the "." symbol on 64 bits.
603 		 * It would be easy to do, but doesn't seem to be necessary,
604 		 * patching the OPD symbol is enough.
605 		 */
606 		vdso_do_func_patch32(v32, v64, patch->gen_name,
607 				     patch->fix_name);
608 #ifdef CONFIG_PPC64
609 		vdso_do_func_patch64(v32, v64, patch->gen_name,
610 				     patch->fix_name);
611 #endif /* CONFIG_PPC64 */
612 	}
613 
614 	return 0;
615 }
616 
617 
618 static __init int vdso_setup(void)
619 {
620 	struct lib32_elfinfo	v32;
621 	struct lib64_elfinfo	v64;
622 
623 	v32.hdr = vdso32_kbase;
624 	v64.hdr = vdso64_kbase;
625 	if (vdso_do_find_sections(&v32, &v64))
626 		return -1;
627 
628 	if (vdso_fixup_datapage(&v32, &v64))
629 		return -1;
630 
631 	if (vdso_fixup_features(&v32, &v64))
632 		return -1;
633 
634 	if (vdso_fixup_alt_funcs(&v32, &v64))
635 		return -1;
636 
637 	vdso_setup_trampolines(&v32, &v64);
638 
639 	return 0;
640 }
641 
642 /*
643  * Called from setup_arch to initialize the bitmap of available
644  * syscalls in the systemcfg page
645  */
646 static void __init vdso_setup_syscall_map(void)
647 {
648 	unsigned int i;
649 
650 	for (i = 0; i < NR_syscalls; i++) {
651 		if (sys_call_table[i] != (unsigned long)&sys_ni_syscall)
652 			vdso_data->syscall_map[i >> 5] |= 0x80000000UL >> (i & 0x1f);
653 		if (IS_ENABLED(CONFIG_COMPAT) &&
654 		    compat_sys_call_table[i] != (unsigned long)&sys_ni_syscall)
655 			vdso_data->compat_syscall_map[i >> 5] |= 0x80000000UL >> (i & 0x1f);
656 	}
657 }
658 
659 #ifdef CONFIG_PPC64
660 int vdso_getcpu_init(void)
661 {
662 	unsigned long cpu, node, val;
663 
664 	/*
665 	 * SPRG_VDSO contains the CPU in the bottom 16 bits and the NUMA node
666 	 * in the next 16 bits.  The VDSO uses this to implement getcpu().
667 	 */
668 	cpu = get_cpu();
669 	WARN_ON_ONCE(cpu > 0xffff);
670 
671 	node = cpu_to_node(cpu);
672 	WARN_ON_ONCE(node > 0xffff);
673 
674 	val = (cpu & 0xffff) | ((node & 0xffff) << 16);
675 	mtspr(SPRN_SPRG_VDSO_WRITE, val);
676 	get_paca()->sprg_vdso = val;
677 
678 	put_cpu();
679 
680 	return 0;
681 }
682 /* We need to call this before SMP init */
683 early_initcall(vdso_getcpu_init);
684 #endif
685 
686 static struct page ** __init vdso_setup_pages(void *start, void *end)
687 {
688 	int i;
689 	struct page **pagelist;
690 	int pages = (end - start) >> PAGE_SHIFT;
691 
692 	pagelist = kcalloc(pages + 1, sizeof(struct page *), GFP_KERNEL);
693 	if (!pagelist)
694 		panic("%s: Cannot allocate page list for VDSO", __func__);
695 
696 	pagelist[0] = virt_to_page(vdso_data);
697 
698 	for (i = 0; i < pages; i++)
699 		pagelist[i + 1] = virt_to_page(start + i * PAGE_SIZE);
700 
701 	return pagelist;
702 }
703 
704 static int __init vdso_init(void)
705 {
706 #ifdef CONFIG_PPC64
707 	/*
708 	 * Fill up the "systemcfg" stuff for backward compatibility
709 	 */
710 	strcpy((char *)vdso_data->eye_catcher, "SYSTEMCFG:PPC64");
711 	vdso_data->version.major = SYSTEMCFG_MAJOR;
712 	vdso_data->version.minor = SYSTEMCFG_MINOR;
713 	vdso_data->processor = mfspr(SPRN_PVR);
714 	/*
715 	 * Fake the old platform number for pSeries and add
716 	 * in LPAR bit if necessary
717 	 */
718 	vdso_data->platform = 0x100;
719 	if (firmware_has_feature(FW_FEATURE_LPAR))
720 		vdso_data->platform |= 1;
721 	vdso_data->physicalMemorySize = memblock_phys_mem_size();
722 	vdso_data->dcache_size = ppc64_caches.l1d.size;
723 	vdso_data->dcache_line_size = ppc64_caches.l1d.line_size;
724 	vdso_data->icache_size = ppc64_caches.l1i.size;
725 	vdso_data->icache_line_size = ppc64_caches.l1i.line_size;
726 	vdso_data->dcache_block_size = ppc64_caches.l1d.block_size;
727 	vdso_data->icache_block_size = ppc64_caches.l1i.block_size;
728 	vdso_data->dcache_log_block_size = ppc64_caches.l1d.log_block_size;
729 	vdso_data->icache_log_block_size = ppc64_caches.l1i.log_block_size;
730 #endif /* CONFIG_PPC64 */
731 
732 	/*
733 	 * Calculate the size of the 64 bits vDSO
734 	 */
735 	vdso64_pages = (&vdso64_end - &vdso64_start) >> PAGE_SHIFT;
736 	DBG("vdso64_kbase: %p, 0x%x pages\n", vdso64_kbase, vdso64_pages);
737 
738 	vdso32_kbase = &vdso32_start;
739 
740 	/*
741 	 * Calculate the size of the 32 bits vDSO
742 	 */
743 	vdso32_pages = (&vdso32_end - &vdso32_start) >> PAGE_SHIFT;
744 	DBG("vdso32_kbase: %p, 0x%x pages\n", vdso32_kbase, vdso32_pages);
745 
746 	vdso_setup_syscall_map();
747 
748 	/*
749 	 * Initialize the vDSO images in memory, that is do necessary
750 	 * fixups of vDSO symbols, locate trampolines, etc...
751 	 */
752 	if (vdso_setup()) {
753 		printk(KERN_ERR "vDSO setup failure, not enabled !\n");
754 		vdso32_pages = 0;
755 		vdso64_pages = 0;
756 		return 0;
757 	}
758 
759 	if (IS_ENABLED(CONFIG_VDSO32))
760 		vdso32_spec.pages = vdso_setup_pages(&vdso32_start, &vdso32_end);
761 
762 	if (IS_ENABLED(CONFIG_PPC64))
763 		vdso64_spec.pages = vdso_setup_pages(&vdso64_start, &vdso64_end);
764 
765 	smp_wmb();
766 	vdso_ready = 1;
767 
768 	return 0;
769 }
770 arch_initcall(vdso_init);
771