vdso.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 __init 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. struct vdso_per_cpu_data *vd;
  87. u32 *psal, *aste;
  88. int i;
  89. lowcore->vdso_per_cpu_data = __LC_PASTE;
  90. if (!vdso_enabled)
  91. return 0;
  92. segment_table = __get_free_pages(GFP_KERNEL, SEGMENT_ORDER);
  93. page_table = get_zeroed_page(GFP_KERNEL | GFP_DMA);
  94. page_frame = get_zeroed_page(GFP_KERNEL);
  95. if (!segment_table || !page_table || !page_frame)
  96. goto out;
  97. /* Initialize per-cpu vdso data page */
  98. vd = (struct vdso_per_cpu_data *) page_frame;
  99. vd->cpu_nr = lowcore->cpu_nr;
  100. vd->node_id = cpu_to_node(vd->cpu_nr);
  101. /* Set up access register mode page table */
  102. clear_table((unsigned long *) segment_table, _SEGMENT_ENTRY_EMPTY,
  103. PAGE_SIZE << SEGMENT_ORDER);
  104. clear_table((unsigned long *) page_table, _PAGE_INVALID,
  105. 256*sizeof(unsigned long));
  106. *(unsigned long *) segment_table = _SEGMENT_ENTRY + page_table;
  107. *(unsigned long *) page_table = _PAGE_PROTECT + page_frame;
  108. psal = (u32 *) (page_table + 256*sizeof(unsigned long));
  109. aste = psal + 32;
  110. for (i = 4; i < 32; i += 4)
  111. psal[i] = 0x80000000;
  112. lowcore->paste[4] = (u32)(addr_t) psal;
  113. psal[0] = 0x02000000;
  114. psal[2] = (u32)(addr_t) aste;
  115. *(unsigned long *) (aste + 2) = segment_table +
  116. _ASCE_TABLE_LENGTH + _ASCE_USER_BITS + _ASCE_TYPE_SEGMENT;
  117. aste[4] = (u32)(addr_t) psal;
  118. lowcore->vdso_per_cpu_data = page_frame;
  119. return 0;
  120. out:
  121. free_page(page_frame);
  122. free_page(page_table);
  123. free_pages(segment_table, SEGMENT_ORDER);
  124. return -ENOMEM;
  125. }
  126. void vdso_free_per_cpu(struct lowcore *lowcore)
  127. {
  128. unsigned long segment_table, page_table, page_frame;
  129. u32 *psal, *aste;
  130. if (!vdso_enabled)
  131. return;
  132. psal = (u32 *)(addr_t) lowcore->paste[4];
  133. aste = (u32 *)(addr_t) psal[2];
  134. segment_table = *(unsigned long *)(aste + 2) & PAGE_MASK;
  135. page_table = *(unsigned long *) segment_table;
  136. page_frame = *(unsigned long *) page_table;
  137. free_page(page_frame);
  138. free_page(page_table);
  139. free_pages(segment_table, SEGMENT_ORDER);
  140. }
  141. static void vdso_init_cr5(void)
  142. {
  143. unsigned long cr5;
  144. if (!vdso_enabled)
  145. return;
  146. cr5 = offsetof(struct lowcore, paste);
  147. __ctl_load(cr5, 5, 5);
  148. }
  149. /*
  150. * This is called from binfmt_elf, we create the special vma for the
  151. * vDSO and insert it into the mm struct tree
  152. */
  153. int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  154. {
  155. struct mm_struct *mm = current->mm;
  156. struct page **vdso_pagelist;
  157. unsigned long vdso_pages;
  158. unsigned long vdso_base;
  159. int rc;
  160. if (!vdso_enabled)
  161. return 0;
  162. /*
  163. * Only map the vdso for dynamically linked elf binaries.
  164. */
  165. if (!uses_interp)
  166. return 0;
  167. vdso_pagelist = vdso64_pagelist;
  168. vdso_pages = vdso64_pages;
  169. #ifdef CONFIG_COMPAT
  170. if (is_compat_task()) {
  171. vdso_pagelist = vdso32_pagelist;
  172. vdso_pages = vdso32_pages;
  173. }
  174. #endif
  175. /*
  176. * vDSO has a problem and was disabled, just don't "enable" it for
  177. * the process
  178. */
  179. if (vdso_pages == 0)
  180. return 0;
  181. current->mm->context.vdso_base = 0;
  182. /*
  183. * pick a base address for the vDSO in process space. We try to put
  184. * it at vdso_base which is the "natural" base for it, but we might
  185. * fail and end up putting it elsewhere.
  186. */
  187. if (down_write_killable(&mm->mmap_sem))
  188. return -EINTR;
  189. vdso_base = get_unmapped_area(NULL, 0, vdso_pages << PAGE_SHIFT, 0, 0);
  190. if (IS_ERR_VALUE(vdso_base)) {
  191. rc = vdso_base;
  192. goto out_up;
  193. }
  194. /*
  195. * Put vDSO base into mm struct. We need to do this before calling
  196. * install_special_mapping or the perf counter mmap tracking code
  197. * will fail to recognise it as a vDSO (since arch_vma_name fails).
  198. */
  199. current->mm->context.vdso_base = vdso_base;
  200. /*
  201. * our vma flags don't have VM_WRITE so by default, the process
  202. * isn't allowed to write those pages.
  203. * gdb can break that with ptrace interface, and thus trigger COW
  204. * on those pages but it's then your responsibility to never do that
  205. * on the "data" page of the vDSO or you'll stop getting kernel
  206. * updates and your nice userland gettimeofday will be totally dead.
  207. * It's fine to use that for setting breakpoints in the vDSO code
  208. * pages though.
  209. */
  210. rc = install_special_mapping(mm, vdso_base, vdso_pages << PAGE_SHIFT,
  211. VM_READ|VM_EXEC|
  212. VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
  213. vdso_pagelist);
  214. if (rc)
  215. current->mm->context.vdso_base = 0;
  216. out_up:
  217. up_write(&mm->mmap_sem);
  218. return rc;
  219. }
  220. const char *arch_vma_name(struct vm_area_struct *vma)
  221. {
  222. if (vma->vm_mm && vma->vm_start == vma->vm_mm->context.vdso_base)
  223. return "[vdso]";
  224. return NULL;
  225. }
  226. static int __init vdso_init(void)
  227. {
  228. int i;
  229. if (!vdso_enabled)
  230. return 0;
  231. vdso_init_data(vdso_data);
  232. #ifdef CONFIG_COMPAT
  233. /* Calculate the size of the 32 bit vDSO */
  234. vdso32_pages = ((&vdso32_end - &vdso32_start
  235. + PAGE_SIZE - 1) >> PAGE_SHIFT) + 1;
  236. /* Make sure pages are in the correct state */
  237. vdso32_pagelist = kzalloc(sizeof(struct page *) * (vdso32_pages + 1),
  238. GFP_KERNEL);
  239. BUG_ON(vdso32_pagelist == NULL);
  240. for (i = 0; i < vdso32_pages - 1; i++) {
  241. struct page *pg = virt_to_page(vdso32_kbase + i*PAGE_SIZE);
  242. ClearPageReserved(pg);
  243. get_page(pg);
  244. vdso32_pagelist[i] = pg;
  245. }
  246. vdso32_pagelist[vdso32_pages - 1] = virt_to_page(vdso_data);
  247. vdso32_pagelist[vdso32_pages] = NULL;
  248. #endif
  249. /* Calculate the size of the 64 bit vDSO */
  250. vdso64_pages = ((&vdso64_end - &vdso64_start
  251. + PAGE_SIZE - 1) >> PAGE_SHIFT) + 1;
  252. /* Make sure pages are in the correct state */
  253. vdso64_pagelist = kzalloc(sizeof(struct page *) * (vdso64_pages + 1),
  254. GFP_KERNEL);
  255. BUG_ON(vdso64_pagelist == NULL);
  256. for (i = 0; i < vdso64_pages - 1; i++) {
  257. struct page *pg = virt_to_page(vdso64_kbase + i*PAGE_SIZE);
  258. ClearPageReserved(pg);
  259. get_page(pg);
  260. vdso64_pagelist[i] = pg;
  261. }
  262. vdso64_pagelist[vdso64_pages - 1] = virt_to_page(vdso_data);
  263. vdso64_pagelist[vdso64_pages] = NULL;
  264. if (vdso_alloc_per_cpu(&S390_lowcore))
  265. BUG();
  266. vdso_init_cr5();
  267. get_page(virt_to_page(vdso_data));
  268. return 0;
  269. }
  270. early_initcall(vdso_init);