| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- // SPDX-License-Identifier: GPL-2.0-only
- /*
- * Copyright 2007 Andi Kleen, SUSE Labs.
- *
- * This contains most of the x86 vDSO kernel-side code.
- */
- #include <linux/mm.h>
- #include <linux/err.h>
- #include <linux/sched.h>
- #include <linux/sched/task_stack.h>
- #include <linux/slab.h>
- #include <linux/init.h>
- #include <linux/random.h>
- #include <linux/elf.h>
- #include <linux/cpu.h>
- #include <linux/ptrace.h>
- #include <linux/time_namespace.h>
- #include <asm/pvclock.h>
- #include <asm/vgtod.h>
- #include <asm/proto.h>
- #include <asm/vdso.h>
- #include <asm/vvar.h>
- #include <asm/tlb.h>
- #include <asm/page.h>
- #include <asm/desc.h>
- #include <asm/cpufeature.h>
- #include <clocksource/hyperv_timer.h>
- #undef _ASM_X86_VVAR_H
- #define EMIT_VVAR(name, offset) \
- const size_t name ## _offset = offset;
- #include <asm/vvar.h>
- struct vdso_data *arch_get_vdso_data(void *vvar_page)
- {
- return (struct vdso_data *)(vvar_page + _vdso_data_offset);
- }
- #undef EMIT_VVAR
- DEFINE_VVAR(struct vdso_data, _vdso_data);
- DEFINE_VVAR_SINGLE(struct vdso_rng_data, _vdso_rng_data);
- unsigned int vclocks_used __read_mostly;
- #if defined(CONFIG_X86_64)
- unsigned int __read_mostly vdso64_enabled = 1;
- #endif
- int __init init_vdso_image(const struct vdso_image *image)
- {
- BUILD_BUG_ON(VDSO_CLOCKMODE_MAX >= 32);
- BUG_ON(image->size % PAGE_SIZE != 0);
- apply_alternatives((struct alt_instr *)(image->data + image->alt),
- (struct alt_instr *)(image->data + image->alt +
- image->alt_len));
- return 0;
- }
- static const struct vm_special_mapping vvar_mapping;
- struct linux_binprm;
- static vm_fault_t vdso_fault(const struct vm_special_mapping *sm,
- struct vm_area_struct *vma, struct vm_fault *vmf)
- {
- const struct vdso_image *image = vma->vm_mm->context.vdso_image;
- if (!image || (vmf->pgoff << PAGE_SHIFT) >= image->size)
- return VM_FAULT_SIGBUS;
- vmf->page = virt_to_page(image->data + (vmf->pgoff << PAGE_SHIFT));
- get_page(vmf->page);
- return 0;
- }
- static void vdso_fix_landing(const struct vdso_image *image,
- struct vm_area_struct *new_vma)
- {
- #if defined CONFIG_X86_32 || defined CONFIG_IA32_EMULATION
- if (in_ia32_syscall() && image == &vdso_image_32) {
- struct pt_regs *regs = current_pt_regs();
- unsigned long vdso_land = image->sym_int80_landing_pad;
- unsigned long old_land_addr = vdso_land +
- (unsigned long)current->mm->context.vdso;
- /* Fixing userspace landing - look at do_fast_syscall_32 */
- if (regs->ip == old_land_addr)
- regs->ip = new_vma->vm_start + vdso_land;
- }
- #endif
- }
- static int vdso_mremap(const struct vm_special_mapping *sm,
- struct vm_area_struct *new_vma)
- {
- const struct vdso_image *image = current->mm->context.vdso_image;
- vdso_fix_landing(image, new_vma);
- current->mm->context.vdso = (void __user *)new_vma->vm_start;
- return 0;
- }
- #ifdef CONFIG_TIME_NS
- /*
- * The vvar page layout depends on whether a task belongs to the root or
- * non-root time namespace. Whenever a task changes its namespace, the VVAR
- * page tables are cleared and then they will re-faulted with a
- * corresponding layout.
- * See also the comment near timens_setup_vdso_data() for details.
- */
- int vdso_join_timens(struct task_struct *task, struct time_namespace *ns)
- {
- struct mm_struct *mm = task->mm;
- struct vm_area_struct *vma;
- VMA_ITERATOR(vmi, mm, 0);
- mmap_read_lock(mm);
- for_each_vma(vmi, vma) {
- if (vma_is_special_mapping(vma, &vvar_mapping))
- zap_vma_pages(vma);
- }
- mmap_read_unlock(mm);
- return 0;
- }
- #endif
- static vm_fault_t vvar_fault(const struct vm_special_mapping *sm,
- struct vm_area_struct *vma, struct vm_fault *vmf)
- {
- const struct vdso_image *image = vma->vm_mm->context.vdso_image;
- unsigned long pfn;
- long sym_offset;
- if (!image)
- return VM_FAULT_SIGBUS;
- sym_offset = (long)(vmf->pgoff << PAGE_SHIFT) +
- image->sym_vvar_start;
- /*
- * Sanity check: a symbol offset of zero means that the page
- * does not exist for this vdso image, not that the page is at
- * offset zero relative to the text mapping. This should be
- * impossible here, because sym_offset should only be zero for
- * the page past the end of the vvar mapping.
- */
- if (sym_offset == 0)
- return VM_FAULT_SIGBUS;
- if (sym_offset == image->sym_vvar_page) {
- struct page *timens_page = find_timens_vvar_page(vma);
- pfn = __pa_symbol(&__vvar_page) >> PAGE_SHIFT;
- /*
- * If a task belongs to a time namespace then a namespace
- * specific VVAR is mapped with the sym_vvar_page offset and
- * the real VVAR page is mapped with the sym_timens_page
- * offset.
- * See also the comment near timens_setup_vdso_data().
- */
- if (timens_page) {
- unsigned long addr;
- vm_fault_t err;
- /*
- * Optimization: inside time namespace pre-fault
- * VVAR page too. As on timens page there are only
- * offsets for clocks on VVAR, it'll be faulted
- * shortly by VDSO code.
- */
- addr = vmf->address + (image->sym_timens_page - sym_offset);
- err = vmf_insert_pfn(vma, addr, pfn);
- if (unlikely(err & VM_FAULT_ERROR))
- return err;
- pfn = page_to_pfn(timens_page);
- }
- return vmf_insert_pfn(vma, vmf->address, pfn);
- } else if (sym_offset == image->sym_pvclock_page) {
- struct pvclock_vsyscall_time_info *pvti =
- pvclock_get_pvti_cpu0_va();
- if (pvti && vclock_was_used(VDSO_CLOCKMODE_PVCLOCK)) {
- return vmf_insert_pfn_prot(vma, vmf->address,
- __pa(pvti) >> PAGE_SHIFT,
- pgprot_decrypted(vma->vm_page_prot));
- }
- } else if (sym_offset == image->sym_hvclock_page) {
- pfn = hv_get_tsc_pfn();
- if (pfn && vclock_was_used(VDSO_CLOCKMODE_HVCLOCK))
- return vmf_insert_pfn(vma, vmf->address, pfn);
- } else if (sym_offset == image->sym_timens_page) {
- struct page *timens_page = find_timens_vvar_page(vma);
- if (!timens_page)
- return VM_FAULT_SIGBUS;
- pfn = __pa_symbol(&__vvar_page) >> PAGE_SHIFT;
- return vmf_insert_pfn(vma, vmf->address, pfn);
- }
- return VM_FAULT_SIGBUS;
- }
- static const struct vm_special_mapping vdso_mapping = {
- .name = "[vdso]",
- .fault = vdso_fault,
- .mremap = vdso_mremap,
- };
- static const struct vm_special_mapping vvar_mapping = {
- .name = "[vvar]",
- .fault = vvar_fault,
- };
- /*
- * Add vdso and vvar mappings to current process.
- * @image - blob to map
- * @addr - request a specific address (zero to map at free addr)
- */
- static int map_vdso(const struct vdso_image *image, unsigned long addr)
- {
- struct mm_struct *mm = current->mm;
- struct vm_area_struct *vma;
- unsigned long text_start;
- int ret = 0;
- if (mmap_write_lock_killable(mm))
- return -EINTR;
- addr = get_unmapped_area(NULL, addr,
- image->size - image->sym_vvar_start, 0, 0);
- if (IS_ERR_VALUE(addr)) {
- ret = addr;
- goto up_fail;
- }
- text_start = addr - image->sym_vvar_start;
- /*
- * MAYWRITE to allow gdb to COW and set breakpoints
- */
- vma = _install_special_mapping(mm,
- text_start,
- image->size,
- VM_READ|VM_EXEC|
- VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
- &vdso_mapping);
- if (IS_ERR(vma)) {
- ret = PTR_ERR(vma);
- goto up_fail;
- }
- vma = _install_special_mapping(mm,
- addr,
- -image->sym_vvar_start,
- VM_READ|VM_MAYREAD|VM_IO|VM_DONTDUMP|
- VM_PFNMAP,
- &vvar_mapping);
- if (IS_ERR(vma)) {
- ret = PTR_ERR(vma);
- do_munmap(mm, text_start, image->size, NULL);
- } else {
- current->mm->context.vdso = (void __user *)text_start;
- current->mm->context.vdso_image = image;
- }
- up_fail:
- mmap_write_unlock(mm);
- return ret;
- }
- int map_vdso_once(const struct vdso_image *image, unsigned long addr)
- {
- struct mm_struct *mm = current->mm;
- struct vm_area_struct *vma;
- VMA_ITERATOR(vmi, mm, 0);
- mmap_write_lock(mm);
- /*
- * Check if we have already mapped vdso blob - fail to prevent
- * abusing from userspace install_special_mapping, which may
- * not do accounting and rlimit right.
- * We could search vma near context.vdso, but it's a slowpath,
- * so let's explicitly check all VMAs to be completely sure.
- */
- for_each_vma(vmi, vma) {
- if (vma_is_special_mapping(vma, &vdso_mapping) ||
- vma_is_special_mapping(vma, &vvar_mapping)) {
- mmap_write_unlock(mm);
- return -EEXIST;
- }
- }
- mmap_write_unlock(mm);
- return map_vdso(image, addr);
- }
- #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
- static int load_vdso32(void)
- {
- if (vdso32_enabled != 1) /* Other values all mean "disabled" */
- return 0;
- return map_vdso(&vdso_image_32, 0);
- }
- #endif
- #ifdef CONFIG_X86_64
- int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
- {
- if (!vdso64_enabled)
- return 0;
- return map_vdso(&vdso_image_64, 0);
- }
- #ifdef CONFIG_COMPAT
- int compat_arch_setup_additional_pages(struct linux_binprm *bprm,
- int uses_interp, bool x32)
- {
- #ifdef CONFIG_X86_X32_ABI
- if (x32) {
- if (!vdso64_enabled)
- return 0;
- return map_vdso(&vdso_image_x32, 0);
- }
- #endif
- #ifdef CONFIG_IA32_EMULATION
- return load_vdso32();
- #else
- return 0;
- #endif
- }
- #endif
- #else
- int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
- {
- return load_vdso32();
- }
- #endif
- bool arch_syscall_is_vdso_sigreturn(struct pt_regs *regs)
- {
- #if defined(CONFIG_X86_32) || defined(CONFIG_IA32_EMULATION)
- const struct vdso_image *image = current->mm->context.vdso_image;
- unsigned long vdso = (unsigned long) current->mm->context.vdso;
- if (in_ia32_syscall() && image == &vdso_image_32) {
- if (regs->ip == vdso + image->sym_vdso32_sigreturn_landing_pad ||
- regs->ip == vdso + image->sym_vdso32_rt_sigreturn_landing_pad)
- return true;
- }
- #endif
- return false;
- }
- #ifdef CONFIG_X86_64
- static __init int vdso_setup(char *s)
- {
- vdso64_enabled = simple_strtoul(s, NULL, 0);
- return 1;
- }
- __setup("vdso=", vdso_setup);
- #endif /* CONFIG_X86_64 */
|