blob: 64df47148160239af2442b93ff4c32de941aa362 [file] [log] [blame]
Andi Kleen2aae9502007-07-21 17:10:01 +02001/*
Andi Kleen2aae9502007-07-21 17:10:01 +02002 * Copyright 2007 Andi Kleen, SUSE Labs.
3 * Subject to the GPL, v.2
Andy Lutomirski1c0c1b92014-09-23 10:50:57 -07004 *
5 * This contains most of the x86 vDSO kernel-side code.
Andi Kleen2aae9502007-07-21 17:10:01 +02006 */
7#include <linux/mm.h>
Alexey Dobriyan4e950f62007-07-30 02:36:13 +04008#include <linux/err.h>
Andi Kleen2aae9502007-07-21 17:10:01 +02009#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Andi Kleen2aae9502007-07-21 17:10:01 +020011#include <linux/init.h>
12#include <linux/random.h>
Jaswinder Singh Rajput3fa89ca2009-04-12 20:37:25 +053013#include <linux/elf.h>
Andy Lutomirskid4f829d2014-09-23 10:50:52 -070014#include <linux/cpu.h>
Andi Kleen2aae9502007-07-21 17:10:01 +020015#include <asm/vgtod.h>
16#include <asm/proto.h>
Roland McGrath7f3646a2008-01-30 13:30:41 +010017#include <asm/vdso.h>
Andy Lutomirski1c0c1b92014-09-23 10:50:57 -070018#include <asm/vvar.h>
Andy Lutomirskiaafade22011-07-21 15:47:10 -040019#include <asm/page.h>
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -070020#include <asm/hpet.h>
Andy Lutomirskid4f829d2014-09-23 10:50:52 -070021#include <asm/desc.h>
Roland McGrath7f3646a2008-01-30 13:30:41 +010022
Andy Lutomirskib4b541a2014-03-17 23:22:08 +010023#if defined(CONFIG_X86_64)
Andy Lutomirski3d7ee962014-05-05 12:19:32 -070024unsigned int __read_mostly vdso64_enabled = 1;
Andy Lutomirskib4b541a2014-03-17 23:22:08 +010025#endif
H. J. Lu1a21d4e2012-02-19 11:38:06 -080026
Andy Lutomirski6f121e52014-05-05 12:19:34 -070027void __init init_vdso_image(const struct vdso_image *image)
H. J. Lu1a21d4e2012-02-19 11:38:06 -080028{
H. J. Lu1a21d4e2012-02-19 11:38:06 -080029 int i;
Andy Lutomirski6f121e52014-05-05 12:19:34 -070030 int npages = (image->size) / PAGE_SIZE;
H. J. Lu1a21d4e2012-02-19 11:38:06 -080031
Andy Lutomirski6f121e52014-05-05 12:19:34 -070032 BUG_ON(image->size % PAGE_SIZE != 0);
33 for (i = 0; i < npages; i++)
Andy Lutomirskia62c34b2014-05-19 15:58:33 -070034 image->text_mapping.pages[i] =
35 virt_to_page(image->data + i*PAGE_SIZE);
H. J. Lu1a21d4e2012-02-19 11:38:06 -080036
Andy Lutomirski6f121e52014-05-05 12:19:34 -070037 apply_alternatives((struct alt_instr *)(image->data + image->alt),
38 (struct alt_instr *)(image->data + image->alt +
39 image->alt_len));
H. J. Lu1a21d4e2012-02-19 11:38:06 -080040}
Andy Lutomirski6f121e52014-05-05 12:19:34 -070041
Andi Kleen2aae9502007-07-21 17:10:01 +020042struct linux_binprm;
43
Andy Lutomirski394f56f2014-12-19 16:04:11 -080044/*
45 * Put the vdso above the (randomized) stack with another randomized
46 * offset. This way there is no hole in the middle of address space.
47 * To save memory make sure it is still in the same PTE as the stack
48 * top. This doesn't give that many random bits.
49 *
50 * Note that this algorithm is imperfect: the distribution of the vdso
51 * start address within a PMD is biased toward the end.
52 *
53 * Only used for the 64-bit and x32 vdsos.
54 */
Andi Kleen2aae9502007-07-21 17:10:01 +020055static unsigned long vdso_addr(unsigned long start, unsigned len)
56{
Jan Beulichd0936012014-07-03 15:35:07 +010057#ifdef CONFIG_X86_32
58 return 0;
59#else
Andi Kleen2aae9502007-07-21 17:10:01 +020060 unsigned long addr, end;
61 unsigned offset;
Andy Lutomirski394f56f2014-12-19 16:04:11 -080062
63 /*
64 * Round up the start address. It can start out unaligned as a result
65 * of stack start randomization.
66 */
67 start = PAGE_ALIGN(start);
68
69 /* Round the lowest possible end address up to a PMD boundary. */
70 end = (start + len + PMD_SIZE - 1) & PMD_MASK;
Ingo Molnard9517342009-02-20 23:32:28 +010071 if (end >= TASK_SIZE_MAX)
72 end = TASK_SIZE_MAX;
Andi Kleen2aae9502007-07-21 17:10:01 +020073 end -= len;
Andy Lutomirski394f56f2014-12-19 16:04:11 -080074
75 if (end > start) {
76 offset = get_random_int() % (((end - start) >> PAGE_SHIFT) + 1);
77 addr = start + (offset << PAGE_SHIFT);
78 } else {
79 addr = start;
80 }
Borislav Petkovdfb09f92011-08-05 15:15:08 +020081
82 /*
Andy Lutomirski394f56f2014-12-19 16:04:11 -080083 * Forcibly align the final address in case we have a hardware
84 * issue that requires alignment for performance reasons.
Borislav Petkovdfb09f92011-08-05 15:15:08 +020085 */
Michel Lespinassef99024722012-12-11 16:01:52 -080086 addr = align_vdso_addr(addr);
Borislav Petkovdfb09f92011-08-05 15:15:08 +020087
Andi Kleen2aae9502007-07-21 17:10:01 +020088 return addr;
Jan Beulichd0936012014-07-03 15:35:07 +010089#endif
Andi Kleen2aae9502007-07-21 17:10:01 +020090}
91
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -070092static int map_vdso(const struct vdso_image *image, bool calculate_addr)
Andi Kleen2aae9502007-07-21 17:10:01 +020093{
94 struct mm_struct *mm = current->mm;
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -070095 struct vm_area_struct *vma;
Andy Lutomirskie6577a72014-07-10 18:13:15 -070096 unsigned long addr, text_start;
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -070097 int ret = 0;
Andy Lutomirski1e844fb2014-05-19 15:58:31 -070098 static struct page *no_pages[] = {NULL};
Andy Lutomirskia62c34b2014-05-19 15:58:33 -070099 static struct vm_special_mapping vvar_mapping = {
100 .name = "[vvar]",
101 .pages = no_pages,
102 };
Andi Kleen2aae9502007-07-21 17:10:01 +0200103
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -0700104 if (calculate_addr) {
105 addr = vdso_addr(current->mm->start_stack,
Andy Lutomirskie6577a72014-07-10 18:13:15 -0700106 image->size - image->sym_vvar_start);
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -0700107 } else {
108 addr = 0;
109 }
Andi Kleen2aae9502007-07-21 17:10:01 +0200110
111 down_write(&mm->mmap_sem);
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -0700112
Andy Lutomirskie6577a72014-07-10 18:13:15 -0700113 addr = get_unmapped_area(NULL, addr,
114 image->size - image->sym_vvar_start, 0, 0);
Andi Kleen2aae9502007-07-21 17:10:01 +0200115 if (IS_ERR_VALUE(addr)) {
116 ret = addr;
117 goto up_fail;
118 }
119
Andy Lutomirskie6577a72014-07-10 18:13:15 -0700120 text_start = addr - image->sym_vvar_start;
121 current->mm->context.vdso = (void __user *)text_start;
Peter Zijlstraf7b6eb32009-06-05 14:04:51 +0200122
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -0700123 /*
124 * MAYWRITE to allow gdb to COW and set breakpoints
125 */
Andy Lutomirskia62c34b2014-05-19 15:58:33 -0700126 vma = _install_special_mapping(mm,
Andy Lutomirskie6577a72014-07-10 18:13:15 -0700127 text_start,
Andy Lutomirskia62c34b2014-05-19 15:58:33 -0700128 image->size,
129 VM_READ|VM_EXEC|
130 VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
131 &image->text_mapping);
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -0700132
Andy Lutomirskia62c34b2014-05-19 15:58:33 -0700133 if (IS_ERR(vma)) {
134 ret = PTR_ERR(vma);
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -0700135 goto up_fail;
Andy Lutomirskia62c34b2014-05-19 15:58:33 -0700136 }
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -0700137
138 vma = _install_special_mapping(mm,
Andy Lutomirskie6577a72014-07-10 18:13:15 -0700139 addr,
140 -image->sym_vvar_start,
Andy Lutomirskiac379832014-07-25 16:27:01 -0700141 VM_READ|VM_MAYREAD,
Andy Lutomirskia62c34b2014-05-19 15:58:33 -0700142 &vvar_mapping);
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -0700143
144 if (IS_ERR(vma)) {
145 ret = PTR_ERR(vma);
Andi Kleen2aae9502007-07-21 17:10:01 +0200146 goto up_fail;
Peter Zijlstraf7b6eb32009-06-05 14:04:51 +0200147 }
Andi Kleen2aae9502007-07-21 17:10:01 +0200148
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -0700149 if (image->sym_vvar_page)
150 ret = remap_pfn_range(vma,
Andy Lutomirskie6577a72014-07-10 18:13:15 -0700151 text_start + image->sym_vvar_page,
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -0700152 __pa_symbol(&__vvar_page) >> PAGE_SHIFT,
153 PAGE_SIZE,
154 PAGE_READONLY);
155
156 if (ret)
157 goto up_fail;
158
159#ifdef CONFIG_HPET_TIMER
160 if (hpet_address && image->sym_hpet_page) {
161 ret = io_remap_pfn_range(vma,
Andy Lutomirskie6577a72014-07-10 18:13:15 -0700162 text_start + image->sym_hpet_page,
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -0700163 hpet_address >> PAGE_SHIFT,
164 PAGE_SIZE,
165 pgprot_noncached(PAGE_READONLY));
166
167 if (ret)
168 goto up_fail;
169 }
170#endif
171
Andi Kleen2aae9502007-07-21 17:10:01 +0200172up_fail:
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -0700173 if (ret)
174 current->mm->context.vdso = NULL;
175
Andi Kleen2aae9502007-07-21 17:10:01 +0200176 up_write(&mm->mmap_sem);
177 return ret;
178}
179
Brian Gerstab8b82ee62015-06-22 07:55:15 -0400180#if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -0700181static int load_vdso32(void)
H. J. Lu1a21d4e2012-02-19 11:38:06 -0800182{
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -0700183 if (vdso32_enabled != 1) /* Other values all mean "disabled" */
184 return 0;
185
Andy Lutomirski0a6d1fa2015-10-05 17:47:56 -0700186 return map_vdso(&vdso_image_32, false);
H. J. Lu1a21d4e2012-02-19 11:38:06 -0800187}
188#endif
189
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -0700190#ifdef CONFIG_X86_64
191int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
192{
193 if (!vdso64_enabled)
194 return 0;
195
196 return map_vdso(&vdso_image_64, true);
197}
198
199#ifdef CONFIG_COMPAT
200int compat_arch_setup_additional_pages(struct linux_binprm *bprm,
201 int uses_interp)
202{
203#ifdef CONFIG_X86_X32_ABI
204 if (test_thread_flag(TIF_X32)) {
205 if (!vdso64_enabled)
206 return 0;
207
208 return map_vdso(&vdso_image_x32, true);
209 }
210#endif
Brian Gerstab8b82ee62015-06-22 07:55:15 -0400211#ifdef CONFIG_IA32_EMULATION
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -0700212 return load_vdso32();
Brian Gerstab8b82ee62015-06-22 07:55:15 -0400213#else
214 return 0;
215#endif
Andy Lutomirski18d0a6f2014-05-05 12:19:35 -0700216}
217#endif
218#else
219int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
220{
221 return load_vdso32();
222}
223#endif
224
225#ifdef CONFIG_X86_64
Andi Kleen2aae9502007-07-21 17:10:01 +0200226static __init int vdso_setup(char *s)
227{
Andy Lutomirski3d7ee962014-05-05 12:19:32 -0700228 vdso64_enabled = simple_strtoul(s, NULL, 0);
Andi Kleen2aae9502007-07-21 17:10:01 +0200229 return 0;
230}
231__setup("vdso=", vdso_setup);
Andy Lutomirskib4b541a2014-03-17 23:22:08 +0100232#endif
Andy Lutomirskid4f829d2014-09-23 10:50:52 -0700233
234#ifdef CONFIG_X86_64
Andy Lutomirski1c0c1b92014-09-23 10:50:57 -0700235static void vgetcpu_cpu_init(void *arg)
Andy Lutomirskid4f829d2014-09-23 10:50:52 -0700236{
Andy Lutomirski1c0c1b92014-09-23 10:50:57 -0700237 int cpu = smp_processor_id();
Andrew Mortona92f1012014-11-01 21:18:26 +0100238 struct desc_struct d = { };
Andy Lutomirskid4f829d2014-09-23 10:50:52 -0700239 unsigned long node = 0;
240#ifdef CONFIG_NUMA
241 node = cpu_to_node(cpu);
242#endif
243 if (cpu_has(&cpu_data(cpu), X86_FEATURE_RDTSCP))
244 write_rdtscp_aux((node << 12) | cpu);
245
246 /*
Andy Lutomirski25880152014-09-23 10:50:53 -0700247 * Store cpu number in limit so that it can be loaded
248 * quickly in user space in vgetcpu. (12 bits for the CPU
249 * and 8 bits for the node)
Andy Lutomirskid4f829d2014-09-23 10:50:52 -0700250 */
Andrew Mortona92f1012014-11-01 21:18:26 +0100251 d.limit0 = cpu | ((node & 0xf) << 12);
252 d.limit = node >> 4;
253 d.type = 5; /* RO data, expand down, accessed */
254 d.dpl = 3; /* Visible to user code */
255 d.s = 1; /* Not a system segment */
256 d.p = 1; /* Present */
257 d.d = 1; /* 32-bit */
Andy Lutomirskid4f829d2014-09-23 10:50:52 -0700258
259 write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_PER_CPU, &d, DESCTYPE_S);
260}
261
Andy Lutomirskid4f829d2014-09-23 10:50:52 -0700262static int
Andy Lutomirski1c0c1b92014-09-23 10:50:57 -0700263vgetcpu_cpu_notifier(struct notifier_block *n, unsigned long action, void *arg)
Andy Lutomirskid4f829d2014-09-23 10:50:52 -0700264{
265 long cpu = (long)arg;
266
267 if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
Andy Lutomirski1c0c1b92014-09-23 10:50:57 -0700268 smp_call_function_single(cpu, vgetcpu_cpu_init, NULL, 1);
Andy Lutomirskid4f829d2014-09-23 10:50:52 -0700269
270 return NOTIFY_DONE;
271}
272
Andy Lutomirski1c0c1b92014-09-23 10:50:57 -0700273static int __init init_vdso(void)
Andy Lutomirskid4f829d2014-09-23 10:50:52 -0700274{
Andy Lutomirski1c0c1b92014-09-23 10:50:57 -0700275 init_vdso_image(&vdso_image_64);
276
277#ifdef CONFIG_X86_X32_ABI
278 init_vdso_image(&vdso_image_x32);
279#endif
280
Andy Lutomirskid4f829d2014-09-23 10:50:52 -0700281 cpu_notifier_register_begin();
282
Andy Lutomirski1c0c1b92014-09-23 10:50:57 -0700283 on_each_cpu(vgetcpu_cpu_init, NULL, 1);
Andy Lutomirskid4f829d2014-09-23 10:50:52 -0700284 /* notifier priority > KVM */
Andy Lutomirski1c0c1b92014-09-23 10:50:57 -0700285 __hotcpu_notifier(vgetcpu_cpu_notifier, 30);
Andy Lutomirskid4f829d2014-09-23 10:50:52 -0700286
287 cpu_notifier_register_done();
288
289 return 0;
290}
Andy Lutomirski1c0c1b92014-09-23 10:50:57 -0700291subsys_initcall(init_vdso);
292#endif /* CONFIG_X86_64 */