David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 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> |
Prem Karat | ccd3988 | 2014-04-21 09:33:16 +0530 | [diff] [blame] | 19 | #include <linux/random.h> |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 20 | |
| 21 | #include <asm/vdso.h> |
| 22 | #include <asm/uasm.h> |
Prem Karat | ccd3988 | 2014-04-21 09:33:16 +0530 | [diff] [blame] | 23 | #include <asm/processor.h> |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 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 | |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 66 | return 0; |
| 67 | } |
David Daney | 1ed8453 | 2010-06-16 15:00:28 -0700 | [diff] [blame] | 68 | subsys_initcall(init_vdso); |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 69 | |
| 70 | static unsigned long vdso_addr(unsigned long start) |
| 71 | { |
Prem Karat | ccd3988 | 2014-04-21 09:33:16 +0530 | [diff] [blame] | 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; |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 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| |
Jason Baron | 909af76 | 2012-03-23 15:02:51 -0700 | [diff] [blame] | 104 | VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC, |
David Daney | c52d0d3 | 2010-02-18 16:13:04 -0800 | [diff] [blame] | 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 | } |