1 // SPDX-License-Identifier: GPL-2.0
3 * Author: Huacai Chen <chenhuacai@loongson.cn>
4 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
7 #include <linux/binfmts.h>
10 #include <linux/init.h>
11 #include <linux/ioport.h>
12 #include <linux/kernel.h>
14 #include <linux/random.h>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
17 #include <linux/timekeeper_internal.h>
21 #include <vdso/helpers.h>
22 #include <vdso/vsyscall.h>
23 #include <generated/vdso-offsets.h>
25 extern char vdso_start[], vdso_end[];
27 /* Kernel-provided data used by the VDSO. */
28 static union loongarch_vdso_data {
30 struct vdso_data data[CS_BASES];
31 } loongarch_vdso_data __page_aligned_data;
32 struct vdso_data *vdso_data = loongarch_vdso_data.data;
33 static struct page *vdso_pages[] = { NULL };
35 static int vdso_mremap(const struct vm_special_mapping *sm, struct vm_area_struct *new_vma)
37 current->mm->context.vdso = (void *)(new_vma->vm_start);
42 struct loongarch_vdso_info vdso_info = {
48 .mremap = vdso_mremap,
53 .offset_sigreturn = vdso_offset_sigreturn,
56 static int __init init_vdso(void)
60 BUG_ON(!PAGE_ALIGNED(vdso_info.vdso));
61 BUG_ON(!PAGE_ALIGNED(vdso_info.size));
63 pfn = __phys_to_pfn(__pa_symbol(vdso_info.vdso));
64 for (i = 0; i < vdso_info.size / PAGE_SIZE; i++)
65 vdso_info.code_mapping.pages[i] = pfn_to_page(pfn + i);
69 subsys_initcall(init_vdso);
71 static unsigned long vdso_base(void)
73 unsigned long base = STACK_TOP;
75 if (current->flags & PF_RANDOMIZE) {
76 base += get_random_int() & (VDSO_RANDOMIZE_SIZE - 1);
77 base = PAGE_ALIGN(base);
83 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
86 unsigned long vvar_size, size, data_addr, vdso_addr;
87 struct mm_struct *mm = current->mm;
88 struct vm_area_struct *vma;
89 struct loongarch_vdso_info *info = current->thread.vdso;
91 if (mmap_write_lock_killable(mm))
95 * Determine total area size. This includes the VDSO data itself
98 vvar_size = PAGE_SIZE;
99 size = vvar_size + info->size;
101 data_addr = get_unmapped_area(NULL, vdso_base(), size, 0, 0);
102 if (IS_ERR_VALUE(data_addr)) {
106 vdso_addr = data_addr + PAGE_SIZE;
108 vma = _install_special_mapping(mm, data_addr, vvar_size,
109 VM_READ | VM_MAYREAD,
110 &info->data_mapping);
116 /* Map VDSO data page. */
117 ret = remap_pfn_range(vma, data_addr,
118 virt_to_phys(vdso_data) >> PAGE_SHIFT,
119 PAGE_SIZE, PAGE_READONLY);
123 /* Map VDSO code page. */
124 vma = _install_special_mapping(mm, vdso_addr, info->size,
125 VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
126 &info->code_mapping);
132 mm->context.vdso = (void *)vdso_addr;
136 mmap_write_unlock(mm);