vdso.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 <linux/compat.h>
  25. #include <asm/asm-offsets.h>
  26. #include <asm/pgtable.h>
  27. #include <asm/processor.h>
  28. #include <asm/mmu.h>
  29. #include <asm/mmu_context.h>
  30. #include <asm/sections.h>
  31. #include <asm/vdso.h>
  32. #include <asm/facility.h>
  33. #ifdef 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. extern char vdso64_start, vdso64_end;
  40. static void *vdso64_kbase = &vdso64_start;
  41. static unsigned int vdso64_pages;
  42. static struct page **vdso64_pagelist;
  43. /*
  44. * Should the kernel map a VDSO page into processes and pass its
  45. * address down to glibc upon exec()?
  46. */
  47. unsigned int __read_mostly vdso_enabled = 1;
  48. static int __init vdso_setup(char *s)
  49. {
  50. unsigned long val;
  51. int rc;
  52. rc = 0;
  53. if (strncmp(s, "on", 3) == 0)
  54. vdso_enabled = 1;
  55. else if (strncmp(s, "off", 4) == 0)
  56. vdso_enabled = 0;
  57. else {
  58. rc = kstrtoul(s, 0, &val);
  59. vdso_enabled = rc ? 0 : !!val;
  60. }
  61. return !rc;
  62. }
  63. __setup("vdso=", vdso_setup);
  64. /*
  65. * The vdso data page
  66. */
  67. static union {
  68. struct vdso_data data;
  69. u8 page[PAGE_SIZE];
  70. } vdso_data_store __page_aligned_data;
  71. struct vdso_data *vdso_data = &vdso_data_store.data;
  72. /*
  73. * Setup vdso data page.
  74. */
  75. static void vdso_init_data(struct vdso_data *vd)
  76. {
  77. vd->ectg_available = test_facility(31);
  78. }
  79. /*
  80. * Allocate/free per cpu vdso data.
  81. */
  82. #define SEGMENT_ORDER 2
  83. int vdso_alloc_per_cpu(struct _lowcore *lowcore)
  84. {
  85. unsigned long segment_table, page_table, page_frame;
  86. u32 *psal, *aste;
  87. int i;
  88. lowcore->vdso_per_cpu_data = __LC_PASTE;
  89. if (!vdso_enabled)
  90. return 0;
  91. segment_table = __get_free_pages(GFP_KERNEL, SEGMENT_ORDER);
  92. page_table = get_zeroed_page(GFP_KERNEL | GFP_DMA);
  93. page_frame = get_zeroed_page(GFP_KERNEL);
  94. if (!segment_table || !page_table || !page_frame)
  95. goto out;
  96. clear_table((unsigned long *) segment_table, _SEGMENT_ENTRY_EMPTY,
  97. PAGE_SIZE << SEGMENT_ORDER);
  98. clear_table((unsigned long *) page_table, _PAGE_INVALID,
  99. 256*sizeof(unsigned long));
  100. *(unsigned long *) segment_table = _SEGMENT_ENTRY + page_table;
  101. *(unsigned long *) page_table = _PAGE_PROTECT + page_frame;
  102. psal = (u32 *) (page_table + 256*sizeof(unsigned long));
  103. aste = psal + 32;
  104. for (i = 4; i < 32; i += 4)
  105. psal[i] = 0x80000000;
  106. lowcore->paste[4] = (u32)(addr_t) psal;
  107. psal[0] = 0x02000000;
  108. psal[2] = (u32)(addr_t) aste;
  109. *(unsigned long *) (aste + 2) = segment_table +
  110. _ASCE_TABLE_LENGTH + _ASCE_USER_BITS + _ASCE_TYPE_SEGMENT;
  111. aste[4] = (u32)(addr_t) psal;
  112. lowcore->vdso_per_cpu_data = page_frame;
  113. return 0;
  114. out:
  115. free_page(page_frame);
  116. free_page(page_table);
  117. free_pages(segment_table, SEGMENT_ORDER);
  118. return -ENOMEM;
  119. }
  120. void vdso_free_per_cpu(struct _lowcore *lowcore)
  121. {
  122. unsigned long segment_table, page_table, page_frame;
  123. u32 *psal, *aste;
  124. if (!vdso_enabled)
  125. return;
  126. psal = (u32 *)(addr_t) lowcore->paste[4];
  127. aste = (u32 *)(addr_t) psal[2];
  128. segment_table = *(unsigned long *)(aste + 2) & PAGE_MASK;
  129. page_table = *(unsigned long *) segment_table;
  130. page_frame = *(unsigned long *) page_table;
  131. free_page(page_frame);
  132. free_page(page_table);
  133. free_pages(segment_table, SEGMENT_ORDER);
  134. }
  135. static void vdso_init_cr5(void)
  136. {
  137. unsigned long cr5;
  138. if (!vdso_enabled)
  139. return;
  140. cr5 = offsetof(struct _lowcore, paste);
  141. __ctl_load(cr5, 5, 5);
  142. }
  143. /*
  144. * This is called from binfmt_elf, we create the special vma for the
  145. * vDSO and insert it into the mm struct tree
  146. */
  147. int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  148. {
  149. struct mm_struct *mm = current->mm;
  150. struct page **vdso_pagelist;
  151. unsigned long vdso_pages;
  152. unsigned long vdso_base;
  153. int rc;
  154. if (!vdso_enabled)
  155. return 0;
  156. /*
  157. * Only map the vdso for dynamically linked elf binaries.
  158. */
  159. if (!uses_interp)
  160. return 0;
  161. vdso_pagelist = vdso64_pagelist;
  162. vdso_pages = vdso64_pages;
  163. #ifdef CONFIG_COMPAT
  164. if (is_compat_task()) {
  165. vdso_pagelist = vdso32_pagelist;
  166. vdso_pages = vdso32_pages;
  167. }
  168. #endif
  169. /*
  170. * vDSO has a problem and was disabled, just don't "enable" it for
  171. * the process
  172. */
  173. if (vdso_pages == 0)
  174. return 0;
  175. current->mm->context.vdso_base = 0;
  176. /*
  177. * pick a base address for the vDSO in process space. We try to put
  178. * it at vdso_base which is the "natural" base for it, but we might
  179. * fail and end up putting it elsewhere.
  180. */
  181. down_write(&mm->mmap_sem);
  182. vdso_base = get_unmapped_area(NULL, 0, vdso_pages << PAGE_SHIFT, 0, 0);
  183. if (IS_ERR_VALUE(vdso_base)) {
  184. rc = vdso_base;
  185. goto out_up;
  186. }
  187. /*
  188. * Put vDSO base into mm struct. We need to do this before calling
  189. * install_special_mapping or the perf counter mmap tracking code
  190. * will fail to recognise it as a vDSO (since arch_vma_name fails).
  191. */
  192. current->mm->context.vdso_base = vdso_base;
  193. /*
  194. * our vma flags don't have VM_WRITE so by default, the process
  195. * isn't allowed to write those pages.
  196. * gdb can break that with ptrace interface, and thus trigger COW
  197. * on those pages but it's then your responsibility to never do that
  198. * on the "data" page of the vDSO or you'll stop getting kernel
  199. * updates and your nice userland gettimeofday will be totally dead.
  200. * It's fine to use that for setting breakpoints in the vDSO code
  201. * pages though.
  202. */
  203. rc = install_special_mapping(mm, vdso_base, vdso_pages << PAGE_SHIFT,
  204. VM_READ|VM_EXEC|
  205. VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
  206. vdso_pagelist);
  207. if (rc)
  208. current->mm->context.vdso_base = 0;
  209. out_up:
  210. up_write(&mm->mmap_sem);
  211. return rc;
  212. }
  213. const char *arch_vma_name(struct vm_area_struct *vma)
  214. {
  215. if (vma->vm_mm && vma->vm_start == vma->vm_mm->context.vdso_base)
  216. return "[vdso]";
  217. return NULL;
  218. }
  219. static int __init vdso_init(void)
  220. {
  221. int i;
  222. if (!vdso_enabled)
  223. return 0;
  224. vdso_init_data(vdso_data);
  225. #ifdef CONFIG_COMPAT
  226. /* Calculate the size of the 32 bit vDSO */
  227. vdso32_pages = ((&vdso32_end - &vdso32_start
  228. + PAGE_SIZE - 1) >> PAGE_SHIFT) + 1;
  229. /* Make sure pages are in the correct state */
  230. vdso32_pagelist = kzalloc(sizeof(struct page *) * (vdso32_pages + 1),
  231. GFP_KERNEL);
  232. BUG_ON(vdso32_pagelist == NULL);
  233. for (i = 0; i < vdso32_pages - 1; i++) {
  234. struct page *pg = virt_to_page(vdso32_kbase + i*PAGE_SIZE);
  235. ClearPageReserved(pg);
  236. get_page(pg);
  237. vdso32_pagelist[i] = pg;
  238. }
  239. vdso32_pagelist[vdso32_pages - 1] = virt_to_page(vdso_data);
  240. vdso32_pagelist[vdso32_pages] = NULL;
  241. #endif
  242. /* Calculate the size of the 64 bit vDSO */
  243. vdso64_pages = ((&vdso64_end - &vdso64_start
  244. + PAGE_SIZE - 1) >> PAGE_SHIFT) + 1;
  245. /* Make sure pages are in the correct state */
  246. vdso64_pagelist = kzalloc(sizeof(struct page *) * (vdso64_pages + 1),
  247. GFP_KERNEL);
  248. BUG_ON(vdso64_pagelist == NULL);
  249. for (i = 0; i < vdso64_pages - 1; i++) {
  250. struct page *pg = virt_to_page(vdso64_kbase + i*PAGE_SIZE);
  251. ClearPageReserved(pg);
  252. get_page(pg);
  253. vdso64_pagelist[i] = pg;
  254. }
  255. vdso64_pagelist[vdso64_pages - 1] = virt_to_page(vdso_data);
  256. vdso64_pagelist[vdso64_pages] = NULL;
  257. if (vdso_alloc_per_cpu(&S390_lowcore))
  258. BUG();
  259. vdso_init_cr5();
  260. get_page(virt_to_page(vdso_data));
  261. smp_mb();
  262. return 0;
  263. }
  264. early_initcall(vdso_init);