vdso.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * vdso setup for s390
  3. *
  4. * Copyright IBM Corp. 2008
  5. * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License (version 2 only)
  9. * as published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/errno.h>
  13. #include <linux/sched.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mm.h>
  16. #include <linux/smp.h>
  17. #include <linux/stddef.h>
  18. #include <linux/unistd.h>
  19. #include <linux/slab.h>
  20. #include <linux/user.h>
  21. #include <linux/elf.h>
  22. #include <linux/security.h>
  23. #include <linux/bootmem.h>
  24. #include <asm/pgtable.h>
  25. #include <asm/system.h>
  26. #include <asm/processor.h>
  27. #include <asm/mmu.h>
  28. #include <asm/mmu_context.h>
  29. #include <asm/sections.h>
  30. #include <asm/vdso.h>
  31. /* Max supported size for symbol names */
  32. #define MAX_SYMNAME 64
  33. #if defined(CONFIG_32BIT) || defined(CONFIG_COMPAT)
  34. extern char vdso32_start, vdso32_end;
  35. static void *vdso32_kbase = &vdso32_start;
  36. static unsigned int vdso32_pages;
  37. static struct page **vdso32_pagelist;
  38. #endif
  39. #ifdef CONFIG_64BIT
  40. extern char vdso64_start, vdso64_end;
  41. static void *vdso64_kbase = &vdso64_start;
  42. static unsigned int vdso64_pages;
  43. static struct page **vdso64_pagelist;
  44. #endif /* CONFIG_64BIT */
  45. /*
  46. * Should the kernel map a VDSO page into processes and pass its
  47. * address down to glibc upon exec()?
  48. */
  49. unsigned int __read_mostly vdso_enabled = 1;
  50. static int __init vdso_setup(char *s)
  51. {
  52. vdso_enabled = simple_strtoul(s, NULL, 0);
  53. return 1;
  54. }
  55. __setup("vdso=", vdso_setup);
  56. /*
  57. * The vdso data page
  58. */
  59. static union {
  60. struct vdso_data data;
  61. u8 page[PAGE_SIZE];
  62. } vdso_data_store __attribute__((__section__(".data.page_aligned")));
  63. struct vdso_data *vdso_data = &vdso_data_store.data;
  64. /*
  65. * This is called from binfmt_elf, we create the special vma for the
  66. * vDSO and insert it into the mm struct tree
  67. */
  68. int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  69. {
  70. struct mm_struct *mm = current->mm;
  71. struct page **vdso_pagelist;
  72. unsigned long vdso_pages;
  73. unsigned long vdso_base;
  74. int rc;
  75. if (!vdso_enabled)
  76. return 0;
  77. /*
  78. * Only map the vdso for dynamically linked elf binaries.
  79. */
  80. if (!uses_interp)
  81. return 0;
  82. vdso_base = mm->mmap_base;
  83. #ifdef CONFIG_64BIT
  84. vdso_pagelist = vdso64_pagelist;
  85. vdso_pages = vdso64_pages;
  86. #ifdef CONFIG_COMPAT
  87. if (test_thread_flag(TIF_31BIT)) {
  88. vdso_pagelist = vdso32_pagelist;
  89. vdso_pages = vdso32_pages;
  90. }
  91. #endif
  92. #else
  93. vdso_pagelist = vdso32_pagelist;
  94. vdso_pages = vdso32_pages;
  95. #endif
  96. /*
  97. * vDSO has a problem and was disabled, just don't "enable" it for
  98. * the process
  99. */
  100. if (vdso_pages == 0)
  101. return 0;
  102. current->mm->context.vdso_base = 0;
  103. /*
  104. * pick a base address for the vDSO in process space. We try to put
  105. * it at vdso_base which is the "natural" base for it, but we might
  106. * fail and end up putting it elsewhere.
  107. */
  108. down_write(&mm->mmap_sem);
  109. vdso_base = get_unmapped_area(NULL, vdso_base,
  110. vdso_pages << PAGE_SHIFT, 0, 0);
  111. if (IS_ERR_VALUE(vdso_base)) {
  112. rc = vdso_base;
  113. goto out_up;
  114. }
  115. /*
  116. * our vma flags don't have VM_WRITE so by default, the process
  117. * isn't allowed to write those pages.
  118. * gdb can break that with ptrace interface, and thus trigger COW
  119. * on those pages but it's then your responsibility to never do that
  120. * on the "data" page of the vDSO or you'll stop getting kernel
  121. * updates and your nice userland gettimeofday will be totally dead.
  122. * It's fine to use that for setting breakpoints in the vDSO code
  123. * pages though
  124. *
  125. * Make sure the vDSO gets into every core dump.
  126. * Dumping its contents makes post-mortem fully interpretable later
  127. * without matching up the same kernel and hardware config to see
  128. * what PC values meant.
  129. */
  130. rc = install_special_mapping(mm, vdso_base, vdso_pages << PAGE_SHIFT,
  131. VM_READ|VM_EXEC|
  132. VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC|
  133. VM_ALWAYSDUMP,
  134. vdso_pagelist);
  135. if (rc)
  136. goto out_up;
  137. /* Put vDSO base into mm struct */
  138. current->mm->context.vdso_base = vdso_base;
  139. up_write(&mm->mmap_sem);
  140. return 0;
  141. out_up:
  142. up_write(&mm->mmap_sem);
  143. return rc;
  144. }
  145. const char *arch_vma_name(struct vm_area_struct *vma)
  146. {
  147. if (vma->vm_mm && vma->vm_start == vma->vm_mm->context.vdso_base)
  148. return "[vdso]";
  149. return NULL;
  150. }
  151. static int __init vdso_init(void)
  152. {
  153. int i;
  154. #if defined(CONFIG_32BIT) || defined(CONFIG_COMPAT)
  155. /* Calculate the size of the 32 bit vDSO */
  156. vdso32_pages = ((&vdso32_end - &vdso32_start
  157. + PAGE_SIZE - 1) >> PAGE_SHIFT) + 1;
  158. /* Make sure pages are in the correct state */
  159. vdso32_pagelist = kzalloc(sizeof(struct page *) * (vdso32_pages + 1),
  160. GFP_KERNEL);
  161. BUG_ON(vdso32_pagelist == NULL);
  162. for (i = 0; i < vdso32_pages - 1; i++) {
  163. struct page *pg = virt_to_page(vdso32_kbase + i*PAGE_SIZE);
  164. ClearPageReserved(pg);
  165. get_page(pg);
  166. vdso32_pagelist[i] = pg;
  167. }
  168. vdso32_pagelist[vdso32_pages - 1] = virt_to_page(vdso_data);
  169. vdso32_pagelist[vdso32_pages] = NULL;
  170. #endif
  171. #ifdef CONFIG_64BIT
  172. /* Calculate the size of the 64 bit vDSO */
  173. vdso64_pages = ((&vdso64_end - &vdso64_start
  174. + PAGE_SIZE - 1) >> PAGE_SHIFT) + 1;
  175. /* Make sure pages are in the correct state */
  176. vdso64_pagelist = kzalloc(sizeof(struct page *) * (vdso64_pages + 1),
  177. GFP_KERNEL);
  178. BUG_ON(vdso64_pagelist == NULL);
  179. for (i = 0; i < vdso64_pages - 1; i++) {
  180. struct page *pg = virt_to_page(vdso64_kbase + i*PAGE_SIZE);
  181. ClearPageReserved(pg);
  182. get_page(pg);
  183. vdso64_pagelist[i] = pg;
  184. }
  185. vdso64_pagelist[vdso64_pages - 1] = virt_to_page(vdso_data);
  186. vdso64_pagelist[vdso64_pages] = NULL;
  187. #endif /* CONFIG_64BIT */
  188. get_page(virt_to_page(vdso_data));
  189. smp_wmb();
  190. return 0;
  191. }
  192. arch_initcall(vdso_init);
  193. int in_gate_area_no_task(unsigned long addr)
  194. {
  195. return 0;
  196. }
  197. int in_gate_area(struct task_struct *task, unsigned long addr)
  198. {
  199. return 0;
  200. }
  201. struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
  202. {
  203. return NULL;
  204. }