vdso.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright 2012 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/binfmts.h>
  15. #include <linux/compat.h>
  16. #include <linux/elf.h>
  17. #include <linux/mm.h>
  18. #include <linux/pagemap.h>
  19. #include <asm/vdso.h>
  20. #include <asm/mman.h>
  21. #include <asm/sections.h>
  22. #include <arch/sim.h>
  23. /* The alignment of the vDSO. */
  24. #define VDSO_ALIGNMENT PAGE_SIZE
  25. static unsigned int vdso_pages;
  26. static struct page **vdso_pagelist;
  27. #ifdef CONFIG_COMPAT
  28. static unsigned int vdso32_pages;
  29. static struct page **vdso32_pagelist;
  30. #endif
  31. static int vdso_ready;
  32. /*
  33. * The vdso data page.
  34. */
  35. static union {
  36. struct vdso_data data;
  37. u8 page[PAGE_SIZE];
  38. } vdso_data_store __page_aligned_data;
  39. struct vdso_data *vdso_data = &vdso_data_store.data;
  40. static unsigned int __read_mostly vdso_enabled = 1;
  41. static struct page **vdso_setup(void *vdso_kbase, unsigned int pages)
  42. {
  43. int i;
  44. struct page **pagelist;
  45. pagelist = kzalloc(sizeof(struct page *) * (pages + 1), GFP_KERNEL);
  46. BUG_ON(pagelist == NULL);
  47. for (i = 0; i < pages - 1; i++) {
  48. struct page *pg = virt_to_page(vdso_kbase + i*PAGE_SIZE);
  49. ClearPageReserved(pg);
  50. pagelist[i] = pg;
  51. }
  52. pagelist[pages - 1] = virt_to_page(vdso_data);
  53. pagelist[pages] = NULL;
  54. return pagelist;
  55. }
  56. static int __init vdso_init(void)
  57. {
  58. int data_pages = sizeof(vdso_data_store) >> PAGE_SHIFT;
  59. /*
  60. * We can disable vDSO support generally, but we need to retain
  61. * one page to support the two-bundle (16-byte) rt_sigreturn path.
  62. */
  63. if (!vdso_enabled) {
  64. size_t offset = (unsigned long)&__vdso_rt_sigreturn;
  65. static struct page *sigret_page;
  66. sigret_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  67. BUG_ON(sigret_page == NULL);
  68. vdso_pagelist = &sigret_page;
  69. vdso_pages = 1;
  70. BUG_ON(offset >= PAGE_SIZE);
  71. memcpy(page_address(sigret_page) + offset,
  72. vdso_start + offset, 16);
  73. #ifdef CONFIG_COMPAT
  74. vdso32_pages = vdso_pages;
  75. vdso32_pagelist = vdso_pagelist;
  76. #endif
  77. vdso_ready = 1;
  78. return 0;
  79. }
  80. vdso_pages = (vdso_end - vdso_start) >> PAGE_SHIFT;
  81. vdso_pages += data_pages;
  82. vdso_pagelist = vdso_setup(vdso_start, vdso_pages);
  83. #ifdef CONFIG_COMPAT
  84. vdso32_pages = (vdso32_end - vdso32_start) >> PAGE_SHIFT;
  85. vdso32_pages += data_pages;
  86. vdso32_pagelist = vdso_setup(vdso32_start, vdso32_pages);
  87. #endif
  88. smp_wmb();
  89. vdso_ready = 1;
  90. return 0;
  91. }
  92. arch_initcall(vdso_init);
  93. const char *arch_vma_name(struct vm_area_struct *vma)
  94. {
  95. if (vma->vm_mm && vma->vm_start == VDSO_BASE)
  96. return "[vdso]";
  97. #ifndef __tilegx__
  98. if (vma->vm_start == MEM_USER_INTRPT)
  99. return "[intrpt]";
  100. #endif
  101. return NULL;
  102. }
  103. int setup_vdso_pages(void)
  104. {
  105. struct page **pagelist;
  106. unsigned long pages;
  107. struct mm_struct *mm = current->mm;
  108. unsigned long vdso_base = 0;
  109. int retval = 0;
  110. if (!vdso_ready)
  111. return 0;
  112. mm->context.vdso_base = 0;
  113. pagelist = vdso_pagelist;
  114. pages = vdso_pages;
  115. #ifdef CONFIG_COMPAT
  116. if (is_compat_task()) {
  117. pagelist = vdso32_pagelist;
  118. pages = vdso32_pages;
  119. }
  120. #endif
  121. /*
  122. * vDSO has a problem and was disabled, just don't "enable" it for the
  123. * process.
  124. */
  125. if (pages == 0)
  126. return 0;
  127. vdso_base = get_unmapped_area(NULL, vdso_base,
  128. (pages << PAGE_SHIFT) +
  129. ((VDSO_ALIGNMENT - 1) & PAGE_MASK),
  130. 0, 0);
  131. if (IS_ERR_VALUE(vdso_base)) {
  132. retval = vdso_base;
  133. return retval;
  134. }
  135. /* Add required alignment. */
  136. vdso_base = ALIGN(vdso_base, VDSO_ALIGNMENT);
  137. /*
  138. * Put vDSO base into mm struct. We need to do this before calling
  139. * install_special_mapping or the perf counter mmap tracking code
  140. * will fail to recognise it as a vDSO (since arch_vma_name fails).
  141. */
  142. mm->context.vdso_base = vdso_base;
  143. /*
  144. * our vma flags don't have VM_WRITE so by default, the process isn't
  145. * allowed to write those pages.
  146. * gdb can break that with ptrace interface, and thus trigger COW on
  147. * those pages but it's then your responsibility to never do that on
  148. * the "data" page of the vDSO or you'll stop getting kernel updates
  149. * and your nice userland gettimeofday will be totally dead.
  150. * It's fine to use that for setting breakpoints in the vDSO code
  151. * pages though
  152. */
  153. retval = install_special_mapping(mm, vdso_base,
  154. pages << PAGE_SHIFT,
  155. VM_READ|VM_EXEC |
  156. VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
  157. pagelist);
  158. if (retval)
  159. mm->context.vdso_base = 0;
  160. return retval;
  161. }
  162. static __init int vdso_func(char *s)
  163. {
  164. return kstrtouint(s, 0, &vdso_enabled);
  165. }
  166. __setup("vdso=", vdso_func);