4 * Copyright IBM Corp. 2008
5 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
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 only)
9 * as published by the Free Software Foundation.
12 #include <linux/module.h>
13 #include <linux/errno.h>
14 #include <linux/sched.h>
15 #include <linux/kernel.h>
17 #include <linux/smp.h>
18 #include <linux/stddef.h>
19 #include <linux/unistd.h>
20 #include <linux/slab.h>
21 #include <linux/user.h>
22 #include <linux/elf.h>
23 #include <linux/security.h>
24 #include <linux/bootmem.h>
26 #include <asm/pgtable.h>
27 #include <asm/system.h>
28 #include <asm/processor.h>
30 #include <asm/mmu_context.h>
31 #include <asm/sections.h>
34 #if defined(CONFIG_32BIT) || defined(CONFIG_COMPAT)
35 extern char vdso32_start
, vdso32_end
;
36 static void *vdso32_kbase
= &vdso32_start
;
37 static unsigned int vdso32_pages
;
38 static struct page
**vdso32_pagelist
;
42 extern char vdso64_start
, vdso64_end
;
43 static void *vdso64_kbase
= &vdso64_start
;
44 static unsigned int vdso64_pages
;
45 static struct page
**vdso64_pagelist
;
46 #endif /* CONFIG_64BIT */
49 * Should the kernel map a VDSO page into processes and pass its
50 * address down to glibc upon exec()?
52 unsigned int __read_mostly vdso_enabled
= 1;
54 static int __init
vdso_setup(char *s
)
56 vdso_enabled
= simple_strtoul(s
, NULL
, 0);
59 __setup("vdso=", vdso_setup
);
65 struct vdso_data data
;
67 } vdso_data_store
__attribute__((__section__(".data.page_aligned")));
68 struct vdso_data
*vdso_data
= &vdso_data_store
.data
;
71 * Setup vdso data page.
73 static void vdso_init_data(struct vdso_data
*vd
)
75 unsigned int facility_list
;
77 facility_list
= stfl();
78 vd
->ectg_available
= switch_amode
&& (facility_list
& 1);
83 * Setup per cpu vdso data page.
85 static void vdso_init_per_cpu_data(int cpu
, struct vdso_per_cpu_data
*vpcd
)
90 * Allocate/free per cpu vdso data.
93 #define SEGMENT_ORDER 2
95 #define SEGMENT_ORDER 1
98 int vdso_alloc_per_cpu(int cpu
, struct _lowcore
*lowcore
)
100 unsigned long segment_table
, page_table
, page_frame
;
104 lowcore
->vdso_per_cpu_data
= __LC_PASTE
;
106 if (!switch_amode
|| !vdso_enabled
)
109 segment_table
= __get_free_pages(GFP_KERNEL
, SEGMENT_ORDER
);
110 page_table
= get_zeroed_page(GFP_KERNEL
| GFP_DMA
);
111 page_frame
= get_zeroed_page(GFP_KERNEL
);
112 if (!segment_table
|| !page_table
|| !page_frame
)
115 clear_table((unsigned long *) segment_table
, _SEGMENT_ENTRY_EMPTY
,
116 PAGE_SIZE
<< SEGMENT_ORDER
);
117 clear_table((unsigned long *) page_table
, _PAGE_TYPE_EMPTY
,
118 256*sizeof(unsigned long));
120 *(unsigned long *) segment_table
= _SEGMENT_ENTRY
+ page_table
;
121 *(unsigned long *) page_table
= _PAGE_RO
+ page_frame
;
123 psal
= (u32
*) (page_table
+ 256*sizeof(unsigned long));
126 for (i
= 4; i
< 32; i
+= 4)
127 psal
[i
] = 0x80000000;
129 lowcore
->paste
[4] = (u32
)(addr_t
) psal
;
130 psal
[0] = 0x20000000;
131 psal
[2] = (u32
)(addr_t
) aste
;
132 *(unsigned long *) (aste
+ 2) = segment_table
+
133 _ASCE_TABLE_LENGTH
+ _ASCE_USER_BITS
+ _ASCE_TYPE_SEGMENT
;
134 aste
[4] = (u32
)(addr_t
) psal
;
135 lowcore
->vdso_per_cpu_data
= page_frame
;
137 vdso_init_per_cpu_data(cpu
, (struct vdso_per_cpu_data
*) page_frame
);
141 free_page(page_frame
);
142 free_page(page_table
);
143 free_pages(segment_table
, SEGMENT_ORDER
);
147 #ifdef CONFIG_HOTPLUG_CPU
148 void vdso_free_per_cpu(int cpu
, struct _lowcore
*lowcore
)
150 unsigned long segment_table
, page_table
, page_frame
;
153 if (!switch_amode
|| !vdso_enabled
)
156 psal
= (u32
*)(addr_t
) lowcore
->paste
[4];
157 aste
= (u32
*)(addr_t
) psal
[2];
158 segment_table
= *(unsigned long *)(aste
+ 2) & PAGE_MASK
;
159 page_table
= *(unsigned long *) segment_table
;
160 page_frame
= *(unsigned long *) page_table
;
162 free_page(page_frame
);
163 free_page(page_table
);
164 free_pages(segment_table
, SEGMENT_ORDER
);
166 #endif /* CONFIG_HOTPLUG_CPU */
168 static void __vdso_init_cr5(void *dummy
)
172 cr5
= offsetof(struct _lowcore
, paste
);
173 __ctl_load(cr5
, 5, 5);
176 static void vdso_init_cr5(void)
178 if (switch_amode
&& vdso_enabled
)
179 on_each_cpu(__vdso_init_cr5
, NULL
, 1);
181 #endif /* CONFIG_64BIT */
184 * This is called from binfmt_elf, we create the special vma for the
185 * vDSO and insert it into the mm struct tree
187 int arch_setup_additional_pages(struct linux_binprm
*bprm
, int uses_interp
)
189 struct mm_struct
*mm
= current
->mm
;
190 struct page
**vdso_pagelist
;
191 unsigned long vdso_pages
;
192 unsigned long vdso_base
;
198 * Only map the vdso for dynamically linked elf binaries.
203 vdso_base
= mm
->mmap_base
;
205 vdso_pagelist
= vdso64_pagelist
;
206 vdso_pages
= vdso64_pages
;
208 if (test_thread_flag(TIF_31BIT
)) {
209 vdso_pagelist
= vdso32_pagelist
;
210 vdso_pages
= vdso32_pages
;
214 vdso_pagelist
= vdso32_pagelist
;
215 vdso_pages
= vdso32_pages
;
219 * vDSO has a problem and was disabled, just don't "enable" it for
225 current
->mm
->context
.vdso_base
= 0;
228 * pick a base address for the vDSO in process space. We try to put
229 * it at vdso_base which is the "natural" base for it, but we might
230 * fail and end up putting it elsewhere.
232 down_write(&mm
->mmap_sem
);
233 vdso_base
= get_unmapped_area(NULL
, vdso_base
,
234 vdso_pages
<< PAGE_SHIFT
, 0, 0);
235 if (IS_ERR_VALUE(vdso_base
)) {
241 * our vma flags don't have VM_WRITE so by default, the process
242 * isn't allowed to write those pages.
243 * gdb can break that with ptrace interface, and thus trigger COW
244 * on those pages but it's then your responsibility to never do that
245 * on the "data" page of the vDSO or you'll stop getting kernel
246 * updates and your nice userland gettimeofday will be totally dead.
247 * It's fine to use that for setting breakpoints in the vDSO code
250 * Make sure the vDSO gets into every core dump.
251 * Dumping its contents makes post-mortem fully interpretable later
252 * without matching up the same kernel and hardware config to see
253 * what PC values meant.
255 rc
= install_special_mapping(mm
, vdso_base
, vdso_pages
<< PAGE_SHIFT
,
257 VM_MAYREAD
|VM_MAYWRITE
|VM_MAYEXEC
|
263 /* Put vDSO base into mm struct */
264 current
->mm
->context
.vdso_base
= vdso_base
;
266 up_write(&mm
->mmap_sem
);
270 up_write(&mm
->mmap_sem
);
274 const char *arch_vma_name(struct vm_area_struct
*vma
)
276 if (vma
->vm_mm
&& vma
->vm_start
== vma
->vm_mm
->context
.vdso_base
)
281 static int __init
vdso_init(void)
287 vdso_init_data(vdso_data
);
288 #if defined(CONFIG_32BIT) || defined(CONFIG_COMPAT)
289 /* Calculate the size of the 32 bit vDSO */
290 vdso32_pages
= ((&vdso32_end
- &vdso32_start
291 + PAGE_SIZE
- 1) >> PAGE_SHIFT
) + 1;
293 /* Make sure pages are in the correct state */
294 vdso32_pagelist
= kzalloc(sizeof(struct page
*) * (vdso32_pages
+ 1),
296 BUG_ON(vdso32_pagelist
== NULL
);
297 for (i
= 0; i
< vdso32_pages
- 1; i
++) {
298 struct page
*pg
= virt_to_page(vdso32_kbase
+ i
*PAGE_SIZE
);
299 ClearPageReserved(pg
);
301 vdso32_pagelist
[i
] = pg
;
303 vdso32_pagelist
[vdso32_pages
- 1] = virt_to_page(vdso_data
);
304 vdso32_pagelist
[vdso32_pages
] = NULL
;
308 /* Calculate the size of the 64 bit vDSO */
309 vdso64_pages
= ((&vdso64_end
- &vdso64_start
310 + PAGE_SIZE
- 1) >> PAGE_SHIFT
) + 1;
312 /* Make sure pages are in the correct state */
313 vdso64_pagelist
= kzalloc(sizeof(struct page
*) * (vdso64_pages
+ 1),
315 BUG_ON(vdso64_pagelist
== NULL
);
316 for (i
= 0; i
< vdso64_pages
- 1; i
++) {
317 struct page
*pg
= virt_to_page(vdso64_kbase
+ i
*PAGE_SIZE
);
318 ClearPageReserved(pg
);
320 vdso64_pagelist
[i
] = pg
;
322 vdso64_pagelist
[vdso64_pages
- 1] = virt_to_page(vdso_data
);
323 vdso64_pagelist
[vdso64_pages
] = NULL
;
325 if (vdso_alloc_per_cpu(0, &S390_lowcore
))
329 #endif /* CONFIG_64BIT */
331 get_page(virt_to_page(vdso_data
));
337 arch_initcall(vdso_init
);
339 int in_gate_area_no_task(unsigned long addr
)
344 int in_gate_area(struct task_struct
*task
, unsigned long addr
)
349 struct vm_area_struct
*get_gate_vma(struct task_struct
*tsk
)