Merge tag 'for-linville-20131001' of git://github.com/kvalo/ath
[cascardo/linux.git] / arch / tile / kernel / vdso.c
1 /*
2  * Copyright 2012 Tilera Corporation. All Rights Reserved.
3  *
4  *   This program is free software; you can redistribute it and/or
5  *   modify it under the terms of the GNU General Public License
6  *   as published by the Free Software Foundation, version 2.
7  *
8  *   This program is distributed in the hope that it will be useful, but
9  *   WITHOUT ANY WARRANTY; without even the implied warranty of
10  *   MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11  *   NON INFRINGEMENT.  See the GNU General Public License for
12  *   more details.
13  */
14
15 #include <linux/binfmts.h>
16 #include <linux/compat.h>
17 #include <linux/elf.h>
18 #include <linux/mm.h>
19 #include <linux/pagemap.h>
20
21 #include <asm/vdso.h>
22 #include <asm/mman.h>
23 #include <asm/sections.h>
24
25 #include <arch/sim.h>
26
27 /* The alignment of the vDSO. */
28 #define VDSO_ALIGNMENT  PAGE_SIZE
29
30
31 static unsigned int vdso_pages;
32 static struct page **vdso_pagelist;
33
34 #ifdef CONFIG_COMPAT
35 static unsigned int vdso32_pages;
36 static struct page **vdso32_pagelist;
37 #endif
38 static int vdso_ready;
39
40 /*
41  * The vdso data page.
42  */
43 static union {
44         struct vdso_data        data;
45         u8                      page[PAGE_SIZE];
46 } vdso_data_store __page_aligned_data;
47
48 struct vdso_data *vdso_data = &vdso_data_store.data;
49
50 static unsigned int __read_mostly vdso_enabled = 1;
51
52 static struct page **vdso_setup(void *vdso_kbase, unsigned int pages)
53 {
54         int i;
55         struct page **pagelist;
56
57         pagelist = kzalloc(sizeof(struct page *) * (pages + 1), GFP_KERNEL);
58         BUG_ON(pagelist == NULL);
59         for (i = 0; i < pages - 1; i++) {
60                 struct page *pg = virt_to_page(vdso_kbase + i*PAGE_SIZE);
61                 ClearPageReserved(pg);
62                 pagelist[i] = pg;
63         }
64         pagelist[pages - 1] = virt_to_page(vdso_data);
65         pagelist[pages] = NULL;
66
67         return pagelist;
68 }
69
70 static int __init vdso_init(void)
71 {
72         int data_pages = sizeof(vdso_data_store) >> PAGE_SHIFT;
73
74         /*
75          * We can disable vDSO support generally, but we need to retain
76          * one page to support the two-bundle (16-byte) rt_sigreturn path.
77          */
78         if (!vdso_enabled) {
79                 size_t offset = (unsigned long)&__vdso_rt_sigreturn;
80                 static struct page *sigret_page;
81                 sigret_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
82                 BUG_ON(sigret_page == NULL);
83                 vdso_pagelist = &sigret_page;
84                 vdso_pages = 1;
85                 BUG_ON(offset >= PAGE_SIZE);
86                 memcpy(page_address(sigret_page) + offset,
87                        vdso_start + offset, 16);
88 #ifdef CONFIG_COMPAT
89                 vdso32_pages = vdso_pages;
90                 vdso32_pagelist = vdso_pagelist;
91 #endif
92                 vdso_ready = 1;
93                 return 0;
94         }
95
96         vdso_pages = (vdso_end - vdso_start) >> PAGE_SHIFT;
97         vdso_pages += data_pages;
98         vdso_pagelist = vdso_setup(vdso_start, vdso_pages);
99
100 #ifdef CONFIG_COMPAT
101         vdso32_pages = (vdso32_end - vdso32_start) >> PAGE_SHIFT;
102         vdso32_pages += data_pages;
103         vdso32_pagelist = vdso_setup(vdso32_start, vdso32_pages);
104 #endif
105
106         smp_wmb();
107         vdso_ready = 1;
108
109         return 0;
110 }
111 arch_initcall(vdso_init);
112
113 const char *arch_vma_name(struct vm_area_struct *vma)
114 {
115         if (vma->vm_mm && vma->vm_start == VDSO_BASE)
116                 return "[vdso]";
117 #ifndef __tilegx__
118         if (vma->vm_start == MEM_USER_INTRPT)
119                 return "[intrpt]";
120 #endif
121         return NULL;
122 }
123
124 struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
125 {
126         return NULL;
127 }
128
129 int in_gate_area(struct mm_struct *mm, unsigned long address)
130 {
131         return 0;
132 }
133
134 int in_gate_area_no_mm(unsigned long address)
135 {
136         return 0;
137 }
138
139 int setup_vdso_pages(void)
140 {
141         struct page **pagelist;
142         unsigned long pages;
143         struct mm_struct *mm = current->mm;
144         unsigned long vdso_base = 0;
145         int retval = 0;
146
147         if (!vdso_ready)
148                 return 0;
149
150         mm->context.vdso_base = 0;
151
152         pagelist = vdso_pagelist;
153         pages = vdso_pages;
154 #ifdef CONFIG_COMPAT
155         if (is_compat_task()) {
156                 pagelist = vdso32_pagelist;
157                 pages = vdso32_pages;
158         }
159 #endif
160
161         /*
162          * vDSO has a problem and was disabled, just don't "enable" it for the
163          * process.
164          */
165         if (pages == 0)
166                 return 0;
167
168         vdso_base = get_unmapped_area(NULL, vdso_base,
169                                       (pages << PAGE_SHIFT) +
170                                       ((VDSO_ALIGNMENT - 1) & PAGE_MASK),
171                                       0, 0);
172         if (IS_ERR_VALUE(vdso_base)) {
173                 retval = vdso_base;
174                 return retval;
175         }
176
177         /* Add required alignment. */
178         vdso_base = ALIGN(vdso_base, VDSO_ALIGNMENT);
179
180         /*
181          * Put vDSO base into mm struct. We need to do this before calling
182          * install_special_mapping or the perf counter mmap tracking code
183          * will fail to recognise it as a vDSO (since arch_vma_name fails).
184          */
185         mm->context.vdso_base = vdso_base;
186
187         /*
188          * our vma flags don't have VM_WRITE so by default, the process isn't
189          * allowed to write those pages.
190          * gdb can break that with ptrace interface, and thus trigger COW on
191          * those pages but it's then your responsibility to never do that on
192          * the "data" page of the vDSO or you'll stop getting kernel updates
193          * and your nice userland gettimeofday will be totally dead.
194          * It's fine to use that for setting breakpoints in the vDSO code
195          * pages though
196          */
197         retval = install_special_mapping(mm, vdso_base,
198                                          pages << PAGE_SHIFT,
199                                          VM_READ|VM_EXEC |
200                                          VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
201                                          pagelist);
202         if (retval)
203                 mm->context.vdso_base = 0;
204
205         return retval;
206 }
207
208 static __init int vdso_func(char *s)
209 {
210         return kstrtouint(s, 0, &vdso_enabled);
211 }
212 __setup("vdso=", vdso_func);