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. */
29 u8 page[VDSO_DATA_SIZE];
30 struct loongarch_vdso_data vdata;
31 } loongarch_vdso_data __page_aligned_data;
33 static struct page *vdso_pages[] = { NULL };
34 struct vdso_data *vdso_data = loongarch_vdso_data.vdata.data;
35 struct vdso_pcpu_data *vdso_pdata = loongarch_vdso_data.vdata.pdata;
37 static int vdso_mremap(const struct vm_special_mapping *sm, struct vm_area_struct *new_vma)
39 current->mm->context.vdso = (void *)(new_vma->vm_start);
44 struct loongarch_vdso_info vdso_info = {
50 .mremap = vdso_mremap,
55 .offset_sigreturn = vdso_offset_sigreturn,
58 static int __init init_vdso(void)
60 unsigned long i, cpu, pfn;
62 BUG_ON(!PAGE_ALIGNED(vdso_info.vdso));
63 BUG_ON(!PAGE_ALIGNED(vdso_info.size));
65 for_each_possible_cpu(cpu)
66 vdso_pdata[cpu].node = cpu_to_node(cpu);
68 pfn = __phys_to_pfn(__pa_symbol(vdso_info.vdso));
69 for (i = 0; i < vdso_info.size / PAGE_SIZE; i++)
70 vdso_info.code_mapping.pages[i] = pfn_to_page(pfn + i);
74 subsys_initcall(init_vdso);
76 static unsigned long vdso_base(void)
78 unsigned long base = STACK_TOP;
80 if (current->flags & PF_RANDOMIZE) {
81 base += get_random_u32_below(VDSO_RANDOMIZE_SIZE);
82 base = PAGE_ALIGN(base);
88 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
91 unsigned long vvar_size, size, data_addr, vdso_addr;
92 struct mm_struct *mm = current->mm;
93 struct vm_area_struct *vma;
94 struct loongarch_vdso_info *info = current->thread.vdso;
96 if (mmap_write_lock_killable(mm))
100 * Determine total area size. This includes the VDSO data itself
101 * and the data pages.
103 vvar_size = VDSO_DATA_SIZE;
104 size = vvar_size + info->size;
106 data_addr = get_unmapped_area(NULL, vdso_base(), size, 0, 0);
107 if (IS_ERR_VALUE(data_addr)) {
111 vdso_addr = data_addr + VDSO_DATA_SIZE;
113 vma = _install_special_mapping(mm, data_addr, vvar_size,
114 VM_READ | VM_MAYREAD,
115 &info->data_mapping);
121 /* Map VDSO data page. */
122 ret = remap_pfn_range(vma, data_addr,
123 virt_to_phys(&loongarch_vdso_data) >> PAGE_SHIFT,
124 vvar_size, PAGE_READONLY);
128 /* Map VDSO code page. */
129 vma = _install_special_mapping(mm, vdso_addr, info->size,
130 VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
131 &info->code_mapping);
137 mm->context.vdso = (void *)vdso_addr;
141 mmap_write_unlock(mm);