vdso.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
  104. {
  105. return NULL;
  106. }
  107. int in_gate_area(struct mm_struct *mm, unsigned long address)
  108. {
  109. return 0;
  110. }
  111. int in_gate_area_no_mm(unsigned long address)
  112. {
  113. return 0;
  114. }
  115. int setup_vdso_pages(void)
  116. {
  117. struct page **pagelist;
  118. unsigned long pages;
  119. struct mm_struct *mm = current->mm;
  120. unsigned long vdso_base = 0;
  121. int retval = 0;
  122. if (!vdso_ready)
  123. return 0;
  124. mm->context.vdso_base = 0;
  125. pagelist = vdso_pagelist;
  126. pages = vdso_pages;
  127. #ifdef CONFIG_COMPAT
  128. if (is_compat_task()) {
  129. pagelist = vdso32_pagelist;
  130. pages = vdso32_pages;
  131. }
  132. #endif
  133. /*
  134. * vDSO has a problem and was disabled, just don't "enable" it for the
  135. * process.
  136. */
  137. if (pages == 0)
  138. return 0;
  139. vdso_base = get_unmapped_area(NULL, vdso_base,
  140. (pages << PAGE_SHIFT) +
  141. ((VDSO_ALIGNMENT - 1) & PAGE_MASK),
  142. 0, 0);
  143. if (IS_ERR_VALUE(vdso_base)) {
  144. retval = vdso_base;
  145. return retval;
  146. }
  147. /* Add required alignment. */
  148. vdso_base = ALIGN(vdso_base, VDSO_ALIGNMENT);
  149. /*
  150. * Put vDSO base into mm struct. We need to do this before calling
  151. * install_special_mapping or the perf counter mmap tracking code
  152. * will fail to recognise it as a vDSO (since arch_vma_name fails).
  153. */
  154. mm->context.vdso_base = vdso_base;
  155. /*
  156. * our vma flags don't have VM_WRITE so by default, the process isn't
  157. * allowed to write those pages.
  158. * gdb can break that with ptrace interface, and thus trigger COW on
  159. * those pages but it's then your responsibility to never do that on
  160. * the "data" page of the vDSO or you'll stop getting kernel updates
  161. * and your nice userland gettimeofday will be totally dead.
  162. * It's fine to use that for setting breakpoints in the vDSO code
  163. * pages though
  164. */
  165. retval = install_special_mapping(mm, vdso_base,
  166. pages << PAGE_SHIFT,
  167. VM_READ|VM_EXEC |
  168. VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
  169. pagelist);
  170. if (retval)
  171. mm->context.vdso_base = 0;
  172. return retval;
  173. }
  174. static __init int vdso_func(char *s)
  175. {
  176. return kstrtouint(s, 0, &vdso_enabled);
  177. }
  178. __setup("vdso=", vdso_func);