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/time_namespace.h>
18 #include <linux/timekeeper_internal.h>
22 #include <vdso/helpers.h>
23 #include <vdso/vsyscall.h>
24 #include <generated/vdso-offsets.h>
26 extern char vdso_start[], vdso_end[];
28 /* Kernel-provided data used by the VDSO. */
31 struct vdso_data data[CS_BASES];
32 } generic_vdso_data __page_aligned_data;
35 u8 page[LOONGARCH_VDSO_DATA_SIZE];
36 struct loongarch_vdso_data vdata;
37 } loongarch_vdso_data __page_aligned_data;
39 static struct page *vdso_pages[] = { NULL };
40 struct vdso_data *vdso_data = generic_vdso_data.data;
41 struct vdso_pcpu_data *vdso_pdata = loongarch_vdso_data.vdata.pdata;
43 static int vdso_mremap(const struct vm_special_mapping *sm, struct vm_area_struct *new_vma)
45 current->mm->context.vdso = (void *)(new_vma->vm_start);
50 static vm_fault_t vvar_fault(const struct vm_special_mapping *sm,
51 struct vm_area_struct *vma, struct vm_fault *vmf)
54 struct page *timens_page = find_timens_vvar_page(vma);
57 case VVAR_GENERIC_PAGE_OFFSET:
59 pfn = sym_to_pfn(vdso_data);
61 pfn = page_to_pfn(timens_page);
64 case VVAR_TIMENS_PAGE_OFFSET:
66 * If a task belongs to a time namespace then a namespace specific
67 * VVAR is mapped with the VVAR_GENERIC_PAGE_OFFSET and the real
68 * VVAR page is mapped with the VVAR_TIMENS_PAGE_OFFSET offset.
69 * See also the comment near timens_setup_vdso_data().
72 return VM_FAULT_SIGBUS;
74 pfn = sym_to_pfn(vdso_data);
76 #endif /* CONFIG_TIME_NS */
77 case VVAR_LOONGARCH_PAGES_START ... VVAR_LOONGARCH_PAGES_END:
78 pfn = sym_to_pfn(&loongarch_vdso_data) + vmf->pgoff - VVAR_LOONGARCH_PAGES_START;
81 return VM_FAULT_SIGBUS;
84 return vmf_insert_pfn(vma, vmf->address, pfn);
87 struct loongarch_vdso_info vdso_info = {
93 .mremap = vdso_mremap,
99 .offset_sigreturn = vdso_offset_sigreturn,
102 static int __init init_vdso(void)
104 unsigned long i, cpu, pfn;
106 BUG_ON(!PAGE_ALIGNED(vdso_info.vdso));
107 BUG_ON(!PAGE_ALIGNED(vdso_info.size));
109 for_each_possible_cpu(cpu)
110 vdso_pdata[cpu].node = cpu_to_node(cpu);
112 pfn = __phys_to_pfn(__pa_symbol(vdso_info.vdso));
113 for (i = 0; i < vdso_info.size / PAGE_SIZE; i++)
114 vdso_info.code_mapping.pages[i] = pfn_to_page(pfn + i);
118 subsys_initcall(init_vdso);
120 #ifdef CONFIG_TIME_NS
121 struct vdso_data *arch_get_vdso_data(void *vvar_page)
123 return (struct vdso_data *)(vvar_page);
127 * The vvar mapping contains data for a specific time namespace, so when a
128 * task changes namespace we must unmap its vvar data for the old namespace.
129 * Subsequent faults will map in data for the new namespace.
131 * For more details see timens_setup_vdso_data().
133 int vdso_join_timens(struct task_struct *task, struct time_namespace *ns)
135 struct mm_struct *mm = task->mm;
136 struct vm_area_struct *vma;
138 VMA_ITERATOR(vmi, mm, 0);
141 for_each_vma(vmi, vma) {
142 if (vma_is_special_mapping(vma, &vdso_info.data_mapping))
145 mmap_read_unlock(mm);
151 static unsigned long vdso_base(void)
153 unsigned long base = STACK_TOP;
155 if (current->flags & PF_RANDOMIZE) {
156 base += get_random_u32_below(VDSO_RANDOMIZE_SIZE);
157 base = PAGE_ALIGN(base);
163 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
166 unsigned long size, data_addr, vdso_addr;
167 struct mm_struct *mm = current->mm;
168 struct vm_area_struct *vma;
169 struct loongarch_vdso_info *info = current->thread.vdso;
171 if (mmap_write_lock_killable(mm))
175 * Determine total area size. This includes the VDSO data itself
176 * and the data pages.
178 size = VVAR_SIZE + info->size;
180 data_addr = get_unmapped_area(NULL, vdso_base(), size, 0, 0);
181 if (IS_ERR_VALUE(data_addr)) {
186 vma = _install_special_mapping(mm, data_addr, VVAR_SIZE,
187 VM_READ | VM_MAYREAD | VM_PFNMAP,
188 &info->data_mapping);
194 vdso_addr = data_addr + VVAR_SIZE;
195 vma = _install_special_mapping(mm, vdso_addr, info->size,
196 VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
197 &info->code_mapping);
203 mm->context.vdso = (void *)vdso_addr;
207 mmap_write_unlock(mm);