vdso.c 2.6 KB

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