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
31static unsigned int vdso_pages;
32static struct page **vdso_pagelist;
33
34#ifdef CONFIG_COMPAT
35static unsigned int vdso32_pages;
36static struct page **vdso32_pagelist;
37#endif
38static int vdso_ready;
39
40/*
41 * The vdso data page.
42 */
43static union {
44	struct vdso_data	data;
45	u8			page[PAGE_SIZE];
46} vdso_data_store __page_aligned_data;
47
48struct vdso_data *vdso_data = &vdso_data_store.data;
49
50static unsigned int __read_mostly vdso_enabled = 1;
51
52static 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
70static 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}
111arch_initcall(vdso_init);
112
113const 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
124int setup_vdso_pages(void)
125{
126	struct page **pagelist;
127	unsigned long pages;
128	struct mm_struct *mm = current->mm;
129	unsigned long vdso_base = 0;
130	int retval = 0;
131
132	if (!vdso_ready)
133		return 0;
134
135	mm->context.vdso_base = 0;
136
137	pagelist = vdso_pagelist;
138	pages = vdso_pages;
139#ifdef CONFIG_COMPAT
140	if (is_compat_task()) {
141		pagelist = vdso32_pagelist;
142		pages = vdso32_pages;
143	}
144#endif
145
146	/*
147	 * vDSO has a problem and was disabled, just don't "enable" it for the
148	 * process.
149	 */
150	if (pages == 0)
151		return 0;
152
153	vdso_base = get_unmapped_area(NULL, vdso_base,
154				      (pages << PAGE_SHIFT) +
155				      ((VDSO_ALIGNMENT - 1) & PAGE_MASK),
156				      0, 0);
157	if (IS_ERR_VALUE(vdso_base)) {
158		retval = vdso_base;
159		return retval;
160	}
161
162	/* Add required alignment. */
163	vdso_base = ALIGN(vdso_base, VDSO_ALIGNMENT);
164
165	/*
166	 * Put vDSO base into mm struct. We need to do this before calling
167	 * install_special_mapping or the perf counter mmap tracking code
168	 * will fail to recognise it as a vDSO (since arch_vma_name fails).
169	 */
170	mm->context.vdso_base = vdso_base;
171
172	/*
173	 * our vma flags don't have VM_WRITE so by default, the process isn't
174	 * allowed to write those pages.
175	 * gdb can break that with ptrace interface, and thus trigger COW on
176	 * those pages but it's then your responsibility to never do that on
177	 * the "data" page of the vDSO or you'll stop getting kernel updates
178	 * and your nice userland gettimeofday will be totally dead.
179	 * It's fine to use that for setting breakpoints in the vDSO code
180	 * pages though
181	 */
182	retval = install_special_mapping(mm, vdso_base,
183					 pages << PAGE_SHIFT,
184					 VM_READ|VM_EXEC |
185					 VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
186					 pagelist);
187	if (retval)
188		mm->context.vdso_base = 0;
189
190	return retval;
191}
192
193static __init int vdso_func(char *s)
194{
195	return kstrtouint(s, 0, &vdso_enabled);
196}
197__setup("vdso=", vdso_func);
198