Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
0003 
0004 #include <linux/binfmts.h>
0005 #include <linux/elf.h>
0006 #include <linux/err.h>
0007 #include <linux/mm.h>
0008 #include <linux/slab.h>
0009 
0010 #include <asm/page.h>
0011 #ifdef GENERIC_TIME_VSYSCALL
0012 #include <vdso/datapage.h>
0013 #else
0014 #include <asm/vdso.h>
0015 #endif
0016 
0017 extern char vdso_start[], vdso_end[];
0018 
0019 static unsigned int vdso_pages;
0020 static struct page **vdso_pagelist;
0021 
0022 /*
0023  * The vDSO data page.
0024  */
0025 static union {
0026     struct vdso_data    data;
0027     u8          page[PAGE_SIZE];
0028 } vdso_data_store __page_aligned_data;
0029 struct vdso_data *vdso_data = &vdso_data_store.data;
0030 
0031 static int __init vdso_init(void)
0032 {
0033     unsigned int i;
0034 
0035     vdso_pages = (vdso_end - vdso_start) >> PAGE_SHIFT;
0036     vdso_pagelist =
0037         kcalloc(vdso_pages + 1, sizeof(struct page *), GFP_KERNEL);
0038     if (unlikely(vdso_pagelist == NULL)) {
0039         pr_err("vdso: pagelist allocation failed\n");
0040         return -ENOMEM;
0041     }
0042 
0043     for (i = 0; i < vdso_pages; i++) {
0044         struct page *pg;
0045 
0046         pg = virt_to_page(vdso_start + (i << PAGE_SHIFT));
0047         vdso_pagelist[i] = pg;
0048     }
0049     vdso_pagelist[i] = virt_to_page(vdso_data);
0050 
0051     return 0;
0052 }
0053 arch_initcall(vdso_init);
0054 
0055 int arch_setup_additional_pages(struct linux_binprm *bprm,
0056     int uses_interp)
0057 {
0058     struct mm_struct *mm = current->mm;
0059     unsigned long vdso_base, vdso_len;
0060     int ret;
0061 
0062     vdso_len = (vdso_pages + 1) << PAGE_SHIFT;
0063 
0064     mmap_write_lock(mm);
0065     vdso_base = get_unmapped_area(NULL, 0, vdso_len, 0, 0);
0066     if (IS_ERR_VALUE(vdso_base)) {
0067         ret = vdso_base;
0068         goto end;
0069     }
0070 
0071     /*
0072      * Put vDSO base into mm struct. We need to do this before calling
0073      * install_special_mapping or the perf counter mmap tracking code
0074      * will fail to recognise it as a vDSO (since arch_vma_name fails).
0075      */
0076     mm->context.vdso = (void *)vdso_base;
0077 
0078     ret =
0079        install_special_mapping(mm, vdso_base, vdso_pages << PAGE_SHIFT,
0080         (VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC),
0081         vdso_pagelist);
0082 
0083     if (unlikely(ret)) {
0084         mm->context.vdso = NULL;
0085         goto end;
0086     }
0087 
0088     vdso_base += (vdso_pages << PAGE_SHIFT);
0089     ret = install_special_mapping(mm, vdso_base, PAGE_SIZE,
0090         (VM_READ | VM_MAYREAD), &vdso_pagelist[vdso_pages]);
0091 
0092     if (unlikely(ret))
0093         mm->context.vdso = NULL;
0094 end:
0095     mmap_write_unlock(mm);
0096     return ret;
0097 }
0098 
0099 const char *arch_vma_name(struct vm_area_struct *vma)
0100 {
0101     if (vma->vm_mm && (vma->vm_start == (long)vma->vm_mm->context.vdso))
0102         return "[vdso]";
0103     if (vma->vm_mm && (vma->vm_start ==
0104                (long)vma->vm_mm->context.vdso + PAGE_SIZE))
0105         return "[vdso_data]";
0106     return NULL;
0107 }