vdso.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
  3. #include <linux/binfmts.h>
  4. #include <linux/elf.h>
  5. #include <linux/err.h>
  6. #include <linux/mm.h>
  7. #include <linux/slab.h>
  8. #include <asm/page.h>
  9. #ifdef GENERIC_TIME_VSYSCALL
  10. #include <vdso/datapage.h>
  11. #else
  12. #include <asm/vdso.h>
  13. #endif
  14. extern char vdso_start[], vdso_end[];
  15. static unsigned int vdso_pages;
  16. static struct page **vdso_pagelist;
  17. /*
  18. * The vDSO data page.
  19. */
  20. static union {
  21. struct vdso_data data;
  22. u8 page[PAGE_SIZE];
  23. } vdso_data_store __page_aligned_data;
  24. struct vdso_data *vdso_data = &vdso_data_store.data;
  25. static int __init vdso_init(void)
  26. {
  27. unsigned int i;
  28. vdso_pages = (vdso_end - vdso_start) >> PAGE_SHIFT;
  29. vdso_pagelist =
  30. kcalloc(vdso_pages + 1, sizeof(struct page *), GFP_KERNEL);
  31. if (unlikely(vdso_pagelist == NULL)) {
  32. pr_err("vdso: pagelist allocation failed\n");
  33. return -ENOMEM;
  34. }
  35. for (i = 0; i < vdso_pages; i++) {
  36. struct page *pg;
  37. pg = virt_to_page(vdso_start + (i << PAGE_SHIFT));
  38. vdso_pagelist[i] = pg;
  39. }
  40. vdso_pagelist[i] = virt_to_page(vdso_data);
  41. return 0;
  42. }
  43. arch_initcall(vdso_init);
  44. int arch_setup_additional_pages(struct linux_binprm *bprm,
  45. int uses_interp)
  46. {
  47. struct mm_struct *mm = current->mm;
  48. unsigned long vdso_base, vdso_len;
  49. int ret;
  50. vdso_len = (vdso_pages + 1) << PAGE_SHIFT;
  51. mmap_write_lock(mm);
  52. vdso_base = get_unmapped_area(NULL, 0, vdso_len, 0, 0);
  53. if (IS_ERR_VALUE(vdso_base)) {
  54. ret = vdso_base;
  55. goto end;
  56. }
  57. /*
  58. * Put vDSO base into mm struct. We need to do this before calling
  59. * install_special_mapping or the perf counter mmap tracking code
  60. * will fail to recognise it as a vDSO (since arch_vma_name fails).
  61. */
  62. mm->context.vdso = (void *)vdso_base;
  63. ret =
  64. install_special_mapping(mm, vdso_base, vdso_pages << PAGE_SHIFT,
  65. (VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC),
  66. vdso_pagelist);
  67. if (unlikely(ret)) {
  68. mm->context.vdso = NULL;
  69. goto end;
  70. }
  71. vdso_base += (vdso_pages << PAGE_SHIFT);
  72. ret = install_special_mapping(mm, vdso_base, PAGE_SIZE,
  73. (VM_READ | VM_MAYREAD), &vdso_pagelist[vdso_pages]);
  74. if (unlikely(ret))
  75. mm->context.vdso = NULL;
  76. end:
  77. mmap_write_unlock(mm);
  78. return ret;
  79. }
  80. const char *arch_vma_name(struct vm_area_struct *vma)
  81. {
  82. if (vma->vm_mm && (vma->vm_start == (long)vma->vm_mm->context.vdso))
  83. return "[vdso]";
  84. if (vma->vm_mm && (vma->vm_start ==
  85. (long)vma->vm_mm->context.vdso + PAGE_SIZE))
  86. return "[vdso_data]";
  87. return NULL;
  88. }