vdso.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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/init.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 vdso_fault(const struct vm_special_mapping *sm,
  49. struct vm_area_struct *vma, struct vm_fault *vmf)
  50. {
  51. struct page **vdso_pagelist;
  52. unsigned long vdso_pages;
  53. vdso_pagelist = vdso64_pagelist;
  54. vdso_pages = vdso64_pages;
  55. #ifdef CONFIG_COMPAT
  56. if (is_compat_task()) {
  57. vdso_pagelist = vdso32_pagelist;
  58. vdso_pages = vdso32_pages;
  59. }
  60. #endif
  61. if (vmf->pgoff >= vdso_pages)
  62. return VM_FAULT_SIGBUS;
  63. vmf->page = vdso_pagelist[vmf->pgoff];
  64. get_page(vmf->page);
  65. return 0;
  66. }
  67. static int vdso_mremap(const struct vm_special_mapping *sm,
  68. struct vm_area_struct *vma)
  69. {
  70. unsigned long vdso_pages;
  71. vdso_pages = vdso64_pages;
  72. #ifdef CONFIG_COMPAT
  73. if (is_compat_task())
  74. vdso_pages = vdso32_pages;
  75. #endif
  76. if ((vdso_pages << PAGE_SHIFT) != vma->vm_end - vma->vm_start)
  77. return -EINVAL;
  78. if (WARN_ON_ONCE(current->mm != vma->vm_mm))
  79. return -EFAULT;
  80. current->mm->context.vdso_base = vma->vm_start;
  81. return 0;
  82. }
  83. static const struct vm_special_mapping vdso_mapping = {
  84. .name = "[vdso]",
  85. .fault = vdso_fault,
  86. .mremap = vdso_mremap,
  87. };
  88. static int __init vdso_setup(char *s)
  89. {
  90. unsigned long val;
  91. int rc;
  92. rc = 0;
  93. if (strncmp(s, "on", 3) == 0)
  94. vdso_enabled = 1;
  95. else if (strncmp(s, "off", 4) == 0)
  96. vdso_enabled = 0;
  97. else {
  98. rc = kstrtoul(s, 0, &val);
  99. vdso_enabled = rc ? 0 : !!val;
  100. }
  101. return !rc;
  102. }
  103. __setup("vdso=", vdso_setup);
  104. /*
  105. * The vdso data page
  106. */
  107. static union {
  108. struct vdso_data data;
  109. u8 page[PAGE_SIZE];
  110. } vdso_data_store __page_aligned_data;
  111. struct vdso_data *vdso_data = &vdso_data_store.data;
  112. /*
  113. * Setup vdso data page.
  114. */
  115. static void __init vdso_init_data(struct vdso_data *vd)
  116. {
  117. vd->ectg_available = test_facility(31);
  118. }
  119. /*
  120. * Allocate/free per cpu vdso data.
  121. */
  122. #define SEGMENT_ORDER 2
  123. /*
  124. * The initial vdso_data structure for the boot CPU. Eventually
  125. * it is replaced with a properly allocated structure in vdso_init.
  126. * This is necessary because a valid S390_lowcore.vdso_per_cpu_data
  127. * pointer is required to be able to return from an interrupt or
  128. * program check. See the exit paths in entry.S.
  129. */
  130. struct vdso_data boot_vdso_data __initdata;
  131. void __init vdso_alloc_boot_cpu(struct lowcore *lowcore)
  132. {
  133. lowcore->vdso_per_cpu_data = (unsigned long) &boot_vdso_data;
  134. }
  135. int vdso_alloc_per_cpu(struct lowcore *lowcore)
  136. {
  137. unsigned long segment_table, page_table, page_frame;
  138. struct vdso_per_cpu_data *vd;
  139. segment_table = __get_free_pages(GFP_KERNEL, SEGMENT_ORDER);
  140. page_table = get_zeroed_page(GFP_KERNEL);
  141. page_frame = get_zeroed_page(GFP_KERNEL);
  142. if (!segment_table || !page_table || !page_frame)
  143. goto out;
  144. arch_set_page_dat(virt_to_page(segment_table), SEGMENT_ORDER);
  145. arch_set_page_dat(virt_to_page(page_table), 0);
  146. /* Initialize per-cpu vdso data page */
  147. vd = (struct vdso_per_cpu_data *) page_frame;
  148. vd->cpu_nr = lowcore->cpu_nr;
  149. vd->node_id = cpu_to_node(vd->cpu_nr);
  150. /* Set up page table for the vdso address space */
  151. memset64((u64 *)segment_table, _SEGMENT_ENTRY_EMPTY, _CRST_ENTRIES);
  152. memset64((u64 *)page_table, _PAGE_INVALID, PTRS_PER_PTE);
  153. *(unsigned long *) segment_table = _SEGMENT_ENTRY + page_table;
  154. *(unsigned long *) page_table = _PAGE_PROTECT + page_frame;
  155. lowcore->vdso_asce = segment_table +
  156. _ASCE_TABLE_LENGTH + _ASCE_USER_BITS + _ASCE_TYPE_SEGMENT;
  157. lowcore->vdso_per_cpu_data = page_frame;
  158. return 0;
  159. out:
  160. free_page(page_frame);
  161. free_page(page_table);
  162. free_pages(segment_table, SEGMENT_ORDER);
  163. return -ENOMEM;
  164. }
  165. void vdso_free_per_cpu(struct lowcore *lowcore)
  166. {
  167. unsigned long segment_table, page_table, page_frame;
  168. segment_table = lowcore->vdso_asce & PAGE_MASK;
  169. page_table = *(unsigned long *) segment_table;
  170. page_frame = *(unsigned long *) page_table;
  171. free_page(page_frame);
  172. free_page(page_table);
  173. free_pages(segment_table, SEGMENT_ORDER);
  174. }
  175. /*
  176. * This is called from binfmt_elf, we create the special vma for the
  177. * vDSO and insert it into the mm struct tree
  178. */
  179. int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  180. {
  181. struct mm_struct *mm = current->mm;
  182. struct vm_area_struct *vma;
  183. unsigned long vdso_pages;
  184. unsigned long vdso_base;
  185. int rc;
  186. if (!vdso_enabled)
  187. return 0;
  188. /*
  189. * Only map the vdso for dynamically linked elf binaries.
  190. */
  191. if (!uses_interp)
  192. return 0;
  193. vdso_pages = vdso64_pages;
  194. #ifdef CONFIG_COMPAT
  195. if (is_compat_task())
  196. vdso_pages = vdso32_pages;
  197. #endif
  198. /*
  199. * vDSO has a problem and was disabled, just don't "enable" it for
  200. * the process
  201. */
  202. if (vdso_pages == 0)
  203. return 0;
  204. /*
  205. * pick a base address for the vDSO in process space. We try to put
  206. * it at vdso_base which is the "natural" base for it, but we might
  207. * fail and end up putting it elsewhere.
  208. */
  209. if (down_write_killable(&mm->mmap_sem))
  210. return -EINTR;
  211. vdso_base = get_unmapped_area(NULL, 0, vdso_pages << PAGE_SHIFT, 0, 0);
  212. if (IS_ERR_VALUE(vdso_base)) {
  213. rc = vdso_base;
  214. goto out_up;
  215. }
  216. /*
  217. * our vma flags don't have VM_WRITE so by default, the process
  218. * isn't allowed to write those pages.
  219. * gdb can break that with ptrace interface, and thus trigger COW
  220. * on those pages but it's then your responsibility to never do that
  221. * on the "data" page of the vDSO or you'll stop getting kernel
  222. * updates and your nice userland gettimeofday will be totally dead.
  223. * It's fine to use that for setting breakpoints in the vDSO code
  224. * pages though.
  225. */
  226. vma = _install_special_mapping(mm, vdso_base, vdso_pages << PAGE_SHIFT,
  227. VM_READ|VM_EXEC|
  228. VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
  229. &vdso_mapping);
  230. if (IS_ERR(vma)) {
  231. rc = PTR_ERR(vma);
  232. goto out_up;
  233. }
  234. current->mm->context.vdso_base = vdso_base;
  235. rc = 0;
  236. out_up:
  237. up_write(&mm->mmap_sem);
  238. return rc;
  239. }
  240. static int __init vdso_init(void)
  241. {
  242. int i;
  243. vdso_init_data(vdso_data);
  244. #ifdef CONFIG_COMPAT
  245. /* Calculate the size of the 32 bit vDSO */
  246. vdso32_pages = ((&vdso32_end - &vdso32_start
  247. + PAGE_SIZE - 1) >> PAGE_SHIFT) + 1;
  248. /* Make sure pages are in the correct state */
  249. vdso32_pagelist = kzalloc(sizeof(struct page *) * (vdso32_pages + 1),
  250. GFP_KERNEL);
  251. BUG_ON(vdso32_pagelist == NULL);
  252. for (i = 0; i < vdso32_pages - 1; i++) {
  253. struct page *pg = virt_to_page(vdso32_kbase + i*PAGE_SIZE);
  254. ClearPageReserved(pg);
  255. get_page(pg);
  256. vdso32_pagelist[i] = pg;
  257. }
  258. vdso32_pagelist[vdso32_pages - 1] = virt_to_page(vdso_data);
  259. vdso32_pagelist[vdso32_pages] = NULL;
  260. #endif
  261. /* Calculate the size of the 64 bit vDSO */
  262. vdso64_pages = ((&vdso64_end - &vdso64_start
  263. + PAGE_SIZE - 1) >> PAGE_SHIFT) + 1;
  264. /* Make sure pages are in the correct state */
  265. vdso64_pagelist = kzalloc(sizeof(struct page *) * (vdso64_pages + 1),
  266. GFP_KERNEL);
  267. BUG_ON(vdso64_pagelist == NULL);
  268. for (i = 0; i < vdso64_pages - 1; i++) {
  269. struct page *pg = virt_to_page(vdso64_kbase + i*PAGE_SIZE);
  270. ClearPageReserved(pg);
  271. get_page(pg);
  272. vdso64_pagelist[i] = pg;
  273. }
  274. vdso64_pagelist[vdso64_pages - 1] = virt_to_page(vdso_data);
  275. vdso64_pagelist[vdso64_pages] = NULL;
  276. if (vdso_alloc_per_cpu(&S390_lowcore))
  277. BUG();
  278. get_page(virt_to_page(vdso_data));
  279. return 0;
  280. }
  281. early_initcall(vdso_init);