vdso.c 8.2 KB

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