1 /* 2 * Copyright (C) 2004 Benjamin Herrenschmidt, IBM Corp. 3 * <benh@kernel.crashing.org> 4 * Copyright (C) 2012 ARM Limited 5 * Copyright (C) 2015 Regents of the University of California 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License version 2 as 9 * published by the Free Software Foundation. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #include <linux/mm.h> 21 #include <linux/slab.h> 22 #include <linux/binfmts.h> 23 #include <linux/err.h> 24 25 #include <asm/vdso.h> 26 27 extern char vdso_start[], vdso_end[]; 28 29 static unsigned int vdso_pages; 30 static struct page **vdso_pagelist; 31 32 /* 33 * The vDSO data page. 34 */ 35 static union { 36 struct vdso_data data; 37 u8 page[PAGE_SIZE]; 38 } vdso_data_store __page_aligned_data; 39 struct vdso_data *vdso_data = &vdso_data_store.data; 40 41 static int __init vdso_init(void) 42 { 43 unsigned int i; 44 45 vdso_pages = (vdso_end - vdso_start) >> PAGE_SHIFT; 46 vdso_pagelist = 47 kcalloc(vdso_pages + 1, sizeof(struct page *), GFP_KERNEL); 48 if (unlikely(vdso_pagelist == NULL)) { 49 pr_err("vdso: pagelist allocation failed\n"); 50 return -ENOMEM; 51 } 52 53 for (i = 0; i < vdso_pages; i++) { 54 struct page *pg; 55 56 pg = virt_to_page(vdso_start + (i << PAGE_SHIFT)); 57 vdso_pagelist[i] = pg; 58 } 59 vdso_pagelist[i] = virt_to_page(vdso_data); 60 61 return 0; 62 } 63 arch_initcall(vdso_init); 64 65 int arch_setup_additional_pages(struct linux_binprm *bprm, 66 int uses_interp) 67 { 68 struct mm_struct *mm = current->mm; 69 unsigned long vdso_base, vdso_len; 70 int ret; 71 72 vdso_len = (vdso_pages + 1) << PAGE_SHIFT; 73 74 down_write(&mm->mmap_sem); 75 vdso_base = get_unmapped_area(NULL, 0, vdso_len, 0, 0); 76 if (IS_ERR_VALUE(vdso_base)) { 77 ret = vdso_base; 78 goto end; 79 } 80 81 /* 82 * Put vDSO base into mm struct. We need to do this before calling 83 * install_special_mapping or the perf counter mmap tracking code 84 * will fail to recognise it as a vDSO (since arch_vma_name fails). 85 */ 86 mm->context.vdso = (void *)vdso_base; 87 88 ret = install_special_mapping(mm, vdso_base, vdso_len, 89 (VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC), 90 vdso_pagelist); 91 92 if (unlikely(ret)) 93 mm->context.vdso = NULL; 94 95 end: 96 up_write(&mm->mmap_sem); 97 return ret; 98 } 99 100 const char *arch_vma_name(struct vm_area_struct *vma) 101 { 102 if (vma->vm_mm && (vma->vm_start == (long)vma->vm_mm->context.vdso)) 103 return "[vdso]"; 104 return NULL; 105 } 106 107 /* 108 * Function stubs to prevent linker errors when AT_SYSINFO_EHDR is defined 109 */ 110 111 int in_gate_area_no_mm(unsigned long addr) 112 { 113 return 0; 114 } 115 116 int in_gate_area(struct mm_struct *mm, unsigned long addr) 117 { 118 return 0; 119 } 120 121 struct vm_area_struct *get_gate_vma(struct mm_struct *mm) 122 { 123 return NULL; 124 } 125