xref: /openbmc/linux/arch/mips/kernel/vdso.c (revision 92b19ff5)
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 2009, 2010 Cavium Networks, Inc.
7  */
8 
9 
10 #include <linux/kernel.h>
11 #include <linux/err.h>
12 #include <linux/sched.h>
13 #include <linux/mm.h>
14 #include <linux/init.h>
15 #include <linux/binfmts.h>
16 #include <linux/elf.h>
17 #include <linux/vmalloc.h>
18 #include <linux/unistd.h>
19 #include <linux/random.h>
20 
21 #include <asm/vdso.h>
22 #include <asm/uasm.h>
23 #include <asm/processor.h>
24 
25 /*
26  * Including <asm/unistd.h> would give use the 64-bit syscall numbers ...
27  */
28 #define __NR_O32_sigreturn		4119
29 #define __NR_O32_rt_sigreturn		4193
30 #define __NR_N32_rt_sigreturn		6211
31 
32 static struct page *vdso_page;
33 
34 static void __init install_trampoline(u32 *tramp, unsigned int sigreturn)
35 {
36 	uasm_i_addiu(&tramp, 2, 0, sigreturn);	/* li v0, sigreturn */
37 	uasm_i_syscall(&tramp, 0);
38 }
39 
40 static int __init init_vdso(void)
41 {
42 	struct mips_vdso *vdso;
43 
44 	vdso_page = alloc_page(GFP_KERNEL);
45 	if (!vdso_page)
46 		panic("Cannot allocate vdso");
47 
48 	vdso = vmap(&vdso_page, 1, 0, PAGE_KERNEL);
49 	if (!vdso)
50 		panic("Cannot map vdso");
51 	clear_page(vdso);
52 
53 	install_trampoline(vdso->rt_signal_trampoline, __NR_rt_sigreturn);
54 #ifdef CONFIG_32BIT
55 	install_trampoline(vdso->signal_trampoline, __NR_sigreturn);
56 #else
57 	install_trampoline(vdso->n32_rt_signal_trampoline,
58 			   __NR_N32_rt_sigreturn);
59 	install_trampoline(vdso->o32_signal_trampoline, __NR_O32_sigreturn);
60 	install_trampoline(vdso->o32_rt_signal_trampoline,
61 			   __NR_O32_rt_sigreturn);
62 #endif
63 
64 	vunmap(vdso);
65 
66 	return 0;
67 }
68 subsys_initcall(init_vdso);
69 
70 static unsigned long vdso_addr(unsigned long start)
71 {
72 	unsigned long offset = 0UL;
73 
74 	if (current->flags & PF_RANDOMIZE) {
75 		offset = get_random_int();
76 		offset <<= PAGE_SHIFT;
77 		if (TASK_IS_32BIT_ADDR)
78 			offset &= 0xfffffful;
79 		else
80 			offset &= 0xffffffful;
81 	}
82 
83 	return STACK_TOP + offset;
84 }
85 
86 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
87 {
88 	int ret;
89 	unsigned long addr;
90 	struct mm_struct *mm = current->mm;
91 
92 	down_write(&mm->mmap_sem);
93 
94 	addr = vdso_addr(mm->start_stack);
95 
96 	addr = get_unmapped_area(NULL, addr, PAGE_SIZE, 0, 0);
97 	if (IS_ERR_VALUE(addr)) {
98 		ret = addr;
99 		goto up_fail;
100 	}
101 
102 	ret = install_special_mapping(mm, addr, PAGE_SIZE,
103 				      VM_READ|VM_EXEC|
104 				      VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
105 				      &vdso_page);
106 
107 	if (ret)
108 		goto up_fail;
109 
110 	mm->context.vdso = (void *)addr;
111 
112 up_fail:
113 	up_write(&mm->mmap_sem);
114 	return ret;
115 }
116 
117 const char *arch_vma_name(struct vm_area_struct *vma)
118 {
119 	if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
120 		return "[vdso]";
121 	return NULL;
122 }
123