vdso.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * vDSO implementation for Hexagon
  4. *
  5. * Copyright (c) 2011, The Linux Foundation. All rights reserved.
  6. */
  7. #include <linux/err.h>
  8. #include <linux/mm.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/binfmts.h>
  11. #include <asm/elf.h>
  12. #include <asm/vdso.h>
  13. static struct page *vdso_page;
  14. /* Create a vDSO page holding the signal trampoline.
  15. * We want this for a non-executable stack.
  16. */
  17. static int __init vdso_init(void)
  18. {
  19. struct hexagon_vdso *vdso;
  20. vdso_page = alloc_page(GFP_KERNEL);
  21. if (!vdso_page)
  22. panic("Cannot allocate vdso");
  23. vdso = vmap(&vdso_page, 1, 0, PAGE_KERNEL);
  24. if (!vdso)
  25. panic("Cannot map vdso");
  26. clear_page(vdso);
  27. /* Install the signal trampoline; currently looks like this:
  28. * r6 = #__NR_rt_sigreturn;
  29. * trap0(#1);
  30. */
  31. vdso->rt_signal_trampoline[0] = __rt_sigtramp_template[0];
  32. vdso->rt_signal_trampoline[1] = __rt_sigtramp_template[1];
  33. vunmap(vdso);
  34. return 0;
  35. }
  36. arch_initcall(vdso_init);
  37. /*
  38. * Called from binfmt_elf. Create a VMA for the vDSO page.
  39. */
  40. int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  41. {
  42. int ret;
  43. unsigned long vdso_base;
  44. struct vm_area_struct *vma;
  45. struct mm_struct *mm = current->mm;
  46. static struct vm_special_mapping vdso_mapping = {
  47. .name = "[vdso]",
  48. };
  49. if (mmap_write_lock_killable(mm))
  50. return -EINTR;
  51. /* Try to get it loaded right near ld.so/glibc. */
  52. vdso_base = STACK_TOP;
  53. vdso_base = get_unmapped_area(NULL, vdso_base, PAGE_SIZE, 0, 0);
  54. if (IS_ERR_VALUE(vdso_base)) {
  55. ret = vdso_base;
  56. goto up_fail;
  57. }
  58. /* MAYWRITE to allow gdb to COW and set breakpoints. */
  59. vdso_mapping.pages = &vdso_page;
  60. vma = _install_special_mapping(mm, vdso_base, PAGE_SIZE,
  61. VM_READ|VM_EXEC|
  62. VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
  63. &vdso_mapping);
  64. ret = PTR_ERR(vma);
  65. if (IS_ERR(vma))
  66. goto up_fail;
  67. mm->context.vdso = (void *)vdso_base;
  68. ret = 0;
  69. up_fail:
  70. mmap_write_unlock(mm);
  71. return ret;
  72. }
  73. const char *arch_vma_name(struct vm_area_struct *vma)
  74. {
  75. if (vma->vm_mm && vma->vm_start == (long)vma->vm_mm->context.vdso)
  76. return "[vdso]";
  77. return NULL;
  78. }