vdso.c 8.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
M
Martin Schwidefsky 已提交
2 3 4 5 6 7 8
/*
 * vdso setup for s390
 *
 *  Copyright IBM Corp. 2008
 *  Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
 */

9
#include <linux/init.h>
M
Martin Schwidefsky 已提交
10 11 12 13 14 15 16 17 18 19 20 21
#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/smp.h>
#include <linux/stddef.h>
#include <linux/unistd.h>
#include <linux/slab.h>
#include <linux/user.h>
#include <linux/elf.h>
#include <linux/security.h>
#include <linux/bootmem.h>
H
Heiko Carstens 已提交
22
#include <linux/compat.h>
23
#include <asm/asm-offsets.h>
M
Martin Schwidefsky 已提交
24 25 26 27 28 29
#include <asm/pgtable.h>
#include <asm/processor.h>
#include <asm/mmu.h>
#include <asm/mmu_context.h>
#include <asm/sections.h>
#include <asm/vdso.h>
30
#include <asm/facility.h>
M
Martin Schwidefsky 已提交
31

H
Heiko Carstens 已提交
32
#ifdef CONFIG_COMPAT
M
Martin Schwidefsky 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
extern char vdso32_start, vdso32_end;
static void *vdso32_kbase = &vdso32_start;
static unsigned int vdso32_pages;
static struct page **vdso32_pagelist;
#endif

extern char vdso64_start, vdso64_end;
static void *vdso64_kbase = &vdso64_start;
static unsigned int vdso64_pages;
static struct page **vdso64_pagelist;

/*
 * Should the kernel map a VDSO page into processes and pass its
 * address down to glibc upon exec()?
 */
unsigned int __read_mostly vdso_enabled = 1;

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
static int vdso_fault(const struct vm_special_mapping *sm,
		      struct vm_area_struct *vma, struct vm_fault *vmf)
{
	struct page **vdso_pagelist;
	unsigned long vdso_pages;

	vdso_pagelist = vdso64_pagelist;
	vdso_pages = vdso64_pages;
#ifdef CONFIG_COMPAT
	if (is_compat_task()) {
		vdso_pagelist = vdso32_pagelist;
		vdso_pages = vdso32_pages;
	}
#endif

	if (vmf->pgoff >= vdso_pages)
		return VM_FAULT_SIGBUS;

	vmf->page = vdso_pagelist[vmf->pgoff];
	get_page(vmf->page);
	return 0;
}

static int vdso_mremap(const struct vm_special_mapping *sm,
		       struct vm_area_struct *vma)
{
	unsigned long vdso_pages;

	vdso_pages = vdso64_pages;
#ifdef CONFIG_COMPAT
	if (is_compat_task())
		vdso_pages = vdso32_pages;
#endif

	if ((vdso_pages << PAGE_SHIFT) != vma->vm_end - vma->vm_start)
		return -EINVAL;

	if (WARN_ON_ONCE(current->mm != vma->vm_mm))
		return -EFAULT;

	current->mm->context.vdso_base = vma->vm_start;
	return 0;
}

static const struct vm_special_mapping vdso_mapping = {
	.name = "[vdso]",
	.fault = vdso_fault,
	.mremap = vdso_mremap,
};

M
Martin Schwidefsky 已提交
100 101
static int __init vdso_setup(char *s)
{
102 103 104 105 106 107 108 109 110
	unsigned long val;
	int rc;

	rc = 0;
	if (strncmp(s, "on", 3) == 0)
		vdso_enabled = 1;
	else if (strncmp(s, "off", 4) == 0)
		vdso_enabled = 0;
	else {
111
		rc = kstrtoul(s, 0, &val);
112 113 114
		vdso_enabled = rc ? 0 : !!val;
	}
	return !rc;
M
Martin Schwidefsky 已提交
115 116 117 118 119 120 121 122 123
}
__setup("vdso=", vdso_setup);

/*
 * The vdso data page
 */
static union {
	struct vdso_data	data;
	u8			page[PAGE_SIZE];
124
} vdso_data_store __page_aligned_data;
M
Martin Schwidefsky 已提交
125 126
struct vdso_data *vdso_data = &vdso_data_store.data;

127 128 129
/*
 * Setup vdso data page.
 */
130
static void __init vdso_init_data(struct vdso_data *vd)
131
{
132
	vd->ectg_available = test_facility(31);
133 134 135 136 137 138 139
}

/*
 * Allocate/free per cpu vdso data.
 */
#define SEGMENT_ORDER	2

140 141 142 143 144 145 146 147 148 149 150 151 152 153
/*
 * The initial vdso_data structure for the boot CPU. Eventually
 * it is replaced with a properly allocated structure in vdso_init.
 * This is necessary because a valid S390_lowcore.vdso_per_cpu_data
 * pointer is required to be able to return from an interrupt or
 * program check. See the exit paths in entry.S.
 */
struct vdso_data boot_vdso_data __initdata;

void __init vdso_alloc_boot_cpu(struct lowcore *lowcore)
{
	lowcore->vdso_per_cpu_data = (unsigned long) &boot_vdso_data;
}

154
int vdso_alloc_per_cpu(struct lowcore *lowcore)
155 156
{
	unsigned long segment_table, page_table, page_frame;
157
	struct vdso_per_cpu_data *vd;
158 159

	segment_table = __get_free_pages(GFP_KERNEL, SEGMENT_ORDER);
160
	page_table = get_zeroed_page(GFP_KERNEL);
161 162 163
	page_frame = get_zeroed_page(GFP_KERNEL);
	if (!segment_table || !page_table || !page_frame)
		goto out;
164 165
	arch_set_page_dat(virt_to_page(segment_table), SEGMENT_ORDER);
	arch_set_page_dat(virt_to_page(page_table), 0);
166

167 168 169 170 171
	/* Initialize per-cpu vdso data page */
	vd = (struct vdso_per_cpu_data *) page_frame;
	vd->cpu_nr = lowcore->cpu_nr;
	vd->node_id = cpu_to_node(vd->cpu_nr);

172
	/* Set up page table for the vdso address space */
173 174
	memset64((u64 *)segment_table, _SEGMENT_ENTRY_EMPTY, _CRST_ENTRIES);
	memset64((u64 *)page_table, _PAGE_INVALID, PTRS_PER_PTE);
175 176

	*(unsigned long *) segment_table = _SEGMENT_ENTRY + page_table;
177
	*(unsigned long *) page_table = _PAGE_PROTECT + page_frame;
178

179
	lowcore->vdso_asce = segment_table +
180 181 182 183 184 185 186 187 188 189 190 191
		_ASCE_TABLE_LENGTH + _ASCE_USER_BITS + _ASCE_TYPE_SEGMENT;
	lowcore->vdso_per_cpu_data = page_frame;

	return 0;

out:
	free_page(page_frame);
	free_page(page_table);
	free_pages(segment_table, SEGMENT_ORDER);
	return -ENOMEM;
}

192
void vdso_free_per_cpu(struct lowcore *lowcore)
193 194 195
{
	unsigned long segment_table, page_table, page_frame;

196
	segment_table = lowcore->vdso_asce & PAGE_MASK;
197 198 199 200 201 202 203 204
	page_table = *(unsigned long *) segment_table;
	page_frame = *(unsigned long *) page_table;

	free_page(page_frame);
	free_page(page_table);
	free_pages(segment_table, SEGMENT_ORDER);
}

M
Martin Schwidefsky 已提交
205 206 207 208 209 210 211
/*
 * This is called from binfmt_elf, we create the special vma for the
 * vDSO and insert it into the mm struct tree
 */
int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
{
	struct mm_struct *mm = current->mm;
212
	struct vm_area_struct *vma;
M
Martin Schwidefsky 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226
	unsigned long vdso_pages;
	unsigned long vdso_base;
	int rc;

	if (!vdso_enabled)
		return 0;
	/*
	 * Only map the vdso for dynamically linked elf binaries.
	 */
	if (!uses_interp)
		return 0;

	vdso_pages = vdso64_pages;
#ifdef CONFIG_COMPAT
227
	if (is_compat_task())
M
Martin Schwidefsky 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241
		vdso_pages = vdso32_pages;
#endif
	/*
	 * vDSO has a problem and was disabled, just don't "enable" it for
	 * the process
	 */
	if (vdso_pages == 0)
		return 0;

	/*
	 * pick a base address for the vDSO in process space. We try to put
	 * it at vdso_base which is the "natural" base for it, but we might
	 * fail and end up putting it elsewhere.
	 */
242 243
	if (down_write_killable(&mm->mmap_sem))
		return -EINTR;
244
	vdso_base = get_unmapped_area(NULL, 0, vdso_pages << PAGE_SHIFT, 0, 0);
M
Martin Schwidefsky 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257
	if (IS_ERR_VALUE(vdso_base)) {
		rc = vdso_base;
		goto out_up;
	}

	/*
	 * our vma flags don't have VM_WRITE so by default, the process
	 * isn't allowed to write those pages.
	 * gdb can break that with ptrace interface, and thus trigger COW
	 * on those pages but it's then your responsibility to never do that
	 * on the "data" page of the vDSO or you'll stop getting kernel
	 * updates and your nice userland gettimeofday will be totally dead.
	 * It's fine to use that for setting breakpoints in the vDSO code
258
	 * pages though.
M
Martin Schwidefsky 已提交
259
	 */
260 261 262 263 264 265 266 267 268 269 270 271
	vma = _install_special_mapping(mm, vdso_base, vdso_pages << PAGE_SHIFT,
				       VM_READ|VM_EXEC|
				       VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
				       &vdso_mapping);
	if (IS_ERR(vma)) {
		rc = PTR_ERR(vma);
		goto out_up;
	}

	current->mm->context.vdso_base = vdso_base;
	rc = 0;

M
Martin Schwidefsky 已提交
272 273 274 275 276 277 278 279 280
out_up:
	up_write(&mm->mmap_sem);
	return rc;
}

static int __init vdso_init(void)
{
	int i;

281
	vdso_init_data(vdso_data);
H
Heiko Carstens 已提交
282
#ifdef CONFIG_COMPAT
M
Martin Schwidefsky 已提交
283 284 285 286 287
	/* Calculate the size of the 32 bit vDSO */
	vdso32_pages = ((&vdso32_end - &vdso32_start
			 + PAGE_SIZE - 1) >> PAGE_SHIFT) + 1;

	/* Make sure pages are in the correct state */
K
Kees Cook 已提交
288
	vdso32_pagelist = kcalloc(vdso32_pages + 1, sizeof(struct page *),
M
Martin Schwidefsky 已提交
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
				  GFP_KERNEL);
	BUG_ON(vdso32_pagelist == NULL);
	for (i = 0; i < vdso32_pages - 1; i++) {
		struct page *pg = virt_to_page(vdso32_kbase + i*PAGE_SIZE);
		ClearPageReserved(pg);
		get_page(pg);
		vdso32_pagelist[i] = pg;
	}
	vdso32_pagelist[vdso32_pages - 1] = virt_to_page(vdso_data);
	vdso32_pagelist[vdso32_pages] = NULL;
#endif

	/* Calculate the size of the 64 bit vDSO */
	vdso64_pages = ((&vdso64_end - &vdso64_start
			 + PAGE_SIZE - 1) >> PAGE_SHIFT) + 1;

	/* Make sure pages are in the correct state */
K
Kees Cook 已提交
306
	vdso64_pagelist = kcalloc(vdso64_pages + 1, sizeof(struct page *),
M
Martin Schwidefsky 已提交
307 308 309 310 311 312 313 314 315 316
				  GFP_KERNEL);
	BUG_ON(vdso64_pagelist == NULL);
	for (i = 0; i < vdso64_pages - 1; i++) {
		struct page *pg = virt_to_page(vdso64_kbase + i*PAGE_SIZE);
		ClearPageReserved(pg);
		get_page(pg);
		vdso64_pagelist[i] = pg;
	}
	vdso64_pagelist[vdso64_pages - 1] = virt_to_page(vdso_data);
	vdso64_pagelist[vdso64_pages] = NULL;
M
Martin Schwidefsky 已提交
317
	if (vdso_alloc_per_cpu(&S390_lowcore))
H
Heiko Carstens 已提交
318
		BUG();
M
Martin Schwidefsky 已提交
319 320 321 322 323

	get_page(virt_to_page(vdso_data));

	return 0;
}
M
Martin Schwidefsky 已提交
324
early_initcall(vdso_init);