56cc3c4377fbae7c5e70ba15b1b26b8cbff7c2c1
[cascardo/linux.git] / arch / mips / kernel / vdso.c
1 /*
2  * Copyright (C) 2015 Imagination Technologies
3  * Author: Alex Smith <alex.smith@imgtec.com>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation;  either version 2 of the  License, or (at your
8  * option) any later version.
9  */
10
11 #include <linux/binfmts.h>
12 #include <linux/elf.h>
13 #include <linux/err.h>
14 #include <linux/init.h>
15 #include <linux/mm.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18
19 #include <asm/abi.h>
20 #include <asm/vdso.h>
21
22 /* Kernel-provided data used by the VDSO. */
23 static union mips_vdso_data vdso_data __page_aligned_data;
24
25 /*
26  * Mapping for the VDSO data pages. The real pages are mapped manually, as
27  * what we map and where within the area they are mapped is determined at
28  * runtime.
29  */
30 static struct page *no_pages[] = { NULL };
31 static struct vm_special_mapping vdso_vvar_mapping = {
32         .name = "[vvar]",
33         .pages = no_pages,
34 };
35
36 static void __init init_vdso_image(struct mips_vdso_image *image)
37 {
38         unsigned long num_pages, i;
39
40         BUG_ON(!PAGE_ALIGNED(image->data));
41         BUG_ON(!PAGE_ALIGNED(image->size));
42
43         num_pages = image->size / PAGE_SIZE;
44
45         for (i = 0; i < num_pages; i++) {
46                 image->mapping.pages[i] =
47                         virt_to_page(image->data + (i * PAGE_SIZE));
48         }
49 }
50
51 static int __init init_vdso(void)
52 {
53         init_vdso_image(&vdso_image);
54
55 #ifdef CONFIG_MIPS32_O32
56         init_vdso_image(&vdso_image_o32);
57 #endif
58
59 #ifdef CONFIG_MIPS32_N32
60         init_vdso_image(&vdso_image_n32);
61 #endif
62
63         return 0;
64 }
65 subsys_initcall(init_vdso);
66
67 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
68 {
69         struct mips_vdso_image *image = current->thread.abi->vdso;
70         struct mm_struct *mm = current->mm;
71         unsigned long base, vdso_addr;
72         struct vm_area_struct *vma;
73         int ret;
74
75         down_write(&mm->mmap_sem);
76
77         base = get_unmapped_area(NULL, 0, PAGE_SIZE + image->size, 0, 0);
78         if (IS_ERR_VALUE(base)) {
79                 ret = base;
80                 goto out;
81         }
82
83         vdso_addr = base + PAGE_SIZE;
84
85         vma = _install_special_mapping(mm, base, PAGE_SIZE,
86                                        VM_READ | VM_MAYREAD,
87                                        &vdso_vvar_mapping);
88         if (IS_ERR(vma)) {
89                 ret = PTR_ERR(vma);
90                 goto out;
91         }
92
93         /* Map data page. */
94         ret = remap_pfn_range(vma, base,
95                               virt_to_phys(&vdso_data) >> PAGE_SHIFT,
96                               PAGE_SIZE, PAGE_READONLY);
97         if (ret)
98                 goto out;
99
100         /* Map VDSO image. */
101         vma = _install_special_mapping(mm, vdso_addr, image->size,
102                                        VM_READ | VM_EXEC |
103                                        VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
104                                        &image->mapping);
105         if (IS_ERR(vma)) {
106                 ret = PTR_ERR(vma);
107                 goto out;
108         }
109
110         mm->context.vdso = (void *)vdso_addr;
111         ret = 0;
112
113 out:
114         up_write(&mm->mmap_sem);
115         return ret;
116 }