vdso.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. #if defined(CONFIG_32BIT) || defined(CONFIG_COMPAT)
  32. extern char vdso32_start, vdso32_end;
  33. static void *vdso32_kbase = &vdso32_start;
  34. static unsigned int vdso32_pages;
  35. static struct page **vdso32_pagelist;
  36. #endif
  37. #ifdef CONFIG_64BIT
  38. extern char vdso64_start, vdso64_end;
  39. static void *vdso64_kbase = &vdso64_start;
  40. static unsigned int vdso64_pages;
  41. static struct page **vdso64_pagelist;
  42. #endif /* CONFIG_64BIT */
  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. vdso_enabled = simple_strtoul(s, NULL, 0);
  51. return 1;
  52. }
  53. __setup("vdso=", vdso_setup);
  54. /*
  55. * The vdso data page
  56. */
  57. static union {
  58. struct vdso_data data;
  59. u8 page[PAGE_SIZE];
  60. } vdso_data_store __attribute__((__section__(".data.page_aligned")));
  61. struct vdso_data *vdso_data = &vdso_data_store.data;
  62. /*
  63. * Setup vdso data page.
  64. */
  65. static void vdso_init_data(struct vdso_data *vd)
  66. {
  67. unsigned int facility_list;
  68. facility_list = stfl();
  69. vd->ectg_available = switch_amode && (facility_list & 1);
  70. }
  71. #ifdef CONFIG_64BIT
  72. /*
  73. * Setup per cpu vdso data page.
  74. */
  75. static void vdso_init_per_cpu_data(int cpu, struct vdso_per_cpu_data *vpcd)
  76. {
  77. }
  78. /*
  79. * Allocate/free per cpu vdso data.
  80. */
  81. #ifdef CONFIG_64BIT
  82. #define SEGMENT_ORDER 2
  83. #else
  84. #define SEGMENT_ORDER 1
  85. #endif
  86. int vdso_alloc_per_cpu(int cpu, struct _lowcore *lowcore)
  87. {
  88. unsigned long segment_table, page_table, page_frame;
  89. u32 *psal, *aste;
  90. int i;
  91. lowcore->vdso_per_cpu_data = __LC_PASTE;
  92. if (!switch_amode || !vdso_enabled)
  93. return 0;
  94. segment_table = __get_free_pages(GFP_KERNEL, SEGMENT_ORDER);
  95. page_table = get_zeroed_page(GFP_KERNEL | GFP_DMA);
  96. page_frame = get_zeroed_page(GFP_KERNEL);
  97. if (!segment_table || !page_table || !page_frame)
  98. goto out;
  99. clear_table((unsigned long *) segment_table, _SEGMENT_ENTRY_EMPTY,
  100. PAGE_SIZE << SEGMENT_ORDER);
  101. clear_table((unsigned long *) page_table, _PAGE_TYPE_EMPTY,
  102. 256*sizeof(unsigned long));
  103. *(unsigned long *) segment_table = _SEGMENT_ENTRY + page_table;
  104. *(unsigned long *) page_table = _PAGE_RO + page_frame;
  105. psal = (u32 *) (page_table + 256*sizeof(unsigned long));
  106. aste = psal + 32;
  107. for (i = 4; i < 32; i += 4)
  108. psal[i] = 0x80000000;
  109. lowcore->paste[4] = (u32)(addr_t) psal;
  110. psal[0] = 0x20000000;
  111. psal[2] = (u32)(addr_t) aste;
  112. *(unsigned long *) (aste + 2) = segment_table +
  113. _ASCE_TABLE_LENGTH + _ASCE_USER_BITS + _ASCE_TYPE_SEGMENT;
  114. aste[4] = (u32)(addr_t) psal;
  115. lowcore->vdso_per_cpu_data = page_frame;
  116. vdso_init_per_cpu_data(cpu, (struct vdso_per_cpu_data *) page_frame);
  117. return 0;
  118. out:
  119. free_page(page_frame);
  120. free_page(page_table);
  121. free_pages(segment_table, SEGMENT_ORDER);
  122. return -ENOMEM;
  123. }
  124. void vdso_free_per_cpu(int cpu, struct _lowcore *lowcore)
  125. {
  126. unsigned long segment_table, page_table, page_frame;
  127. u32 *psal, *aste;
  128. if (!switch_amode || !vdso_enabled)
  129. return;
  130. psal = (u32 *)(addr_t) lowcore->paste[4];
  131. aste = (u32 *)(addr_t) psal[2];
  132. segment_table = *(unsigned long *)(aste + 2) & PAGE_MASK;
  133. page_table = *(unsigned long *) segment_table;
  134. page_frame = *(unsigned long *) page_table;
  135. free_page(page_frame);
  136. free_page(page_table);
  137. free_pages(segment_table, SEGMENT_ORDER);
  138. }
  139. static void __vdso_init_cr5(void *dummy)
  140. {
  141. unsigned long cr5;
  142. cr5 = offsetof(struct _lowcore, paste);
  143. __ctl_load(cr5, 5, 5);
  144. }
  145. static void vdso_init_cr5(void)
  146. {
  147. if (switch_amode && vdso_enabled)
  148. on_each_cpu(__vdso_init_cr5, NULL, 1);
  149. }
  150. #endif /* CONFIG_64BIT */
  151. /*
  152. * This is called from binfmt_elf, we create the special vma for the
  153. * vDSO and insert it into the mm struct tree
  154. */
  155. int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  156. {
  157. struct mm_struct *mm = current->mm;
  158. struct page **vdso_pagelist;
  159. unsigned long vdso_pages;
  160. unsigned long vdso_base;
  161. int rc;
  162. if (!vdso_enabled)
  163. return 0;
  164. /*
  165. * Only map the vdso for dynamically linked elf binaries.
  166. */
  167. if (!uses_interp)
  168. return 0;
  169. vdso_base = mm->mmap_base;
  170. #ifdef CONFIG_64BIT
  171. vdso_pagelist = vdso64_pagelist;
  172. vdso_pages = vdso64_pages;
  173. #ifdef CONFIG_COMPAT
  174. if (test_thread_flag(TIF_31BIT)) {
  175. vdso_pagelist = vdso32_pagelist;
  176. vdso_pages = vdso32_pages;
  177. }
  178. #endif
  179. #else
  180. vdso_pagelist = vdso32_pagelist;
  181. vdso_pages = vdso32_pages;
  182. #endif
  183. /*
  184. * vDSO has a problem and was disabled, just don't "enable" it for
  185. * the process
  186. */
  187. if (vdso_pages == 0)
  188. return 0;
  189. current->mm->context.vdso_base = 0;
  190. /*
  191. * pick a base address for the vDSO in process space. We try to put
  192. * it at vdso_base which is the "natural" base for it, but we might
  193. * fail and end up putting it elsewhere.
  194. */
  195. down_write(&mm->mmap_sem);
  196. vdso_base = get_unmapped_area(NULL, vdso_base,
  197. vdso_pages << PAGE_SHIFT, 0, 0);
  198. if (IS_ERR_VALUE(vdso_base)) {
  199. rc = vdso_base;
  200. goto out_up;
  201. }
  202. /*
  203. * our vma flags don't have VM_WRITE so by default, the process
  204. * isn't allowed to write those pages.
  205. * gdb can break that with ptrace interface, and thus trigger COW
  206. * on those pages but it's then your responsibility to never do that
  207. * on the "data" page of the vDSO or you'll stop getting kernel
  208. * updates and your nice userland gettimeofday will be totally dead.
  209. * It's fine to use that for setting breakpoints in the vDSO code
  210. * pages though
  211. *
  212. * Make sure the vDSO gets into every core dump.
  213. * Dumping its contents makes post-mortem fully interpretable later
  214. * without matching up the same kernel and hardware config to see
  215. * what PC values meant.
  216. */
  217. rc = install_special_mapping(mm, vdso_base, vdso_pages << PAGE_SHIFT,
  218. VM_READ|VM_EXEC|
  219. VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC|
  220. VM_ALWAYSDUMP,
  221. vdso_pagelist);
  222. if (rc)
  223. goto out_up;
  224. /* Put vDSO base into mm struct */
  225. current->mm->context.vdso_base = vdso_base;
  226. up_write(&mm->mmap_sem);
  227. return 0;
  228. out_up:
  229. up_write(&mm->mmap_sem);
  230. return rc;
  231. }
  232. const char *arch_vma_name(struct vm_area_struct *vma)
  233. {
  234. if (vma->vm_mm && vma->vm_start == vma->vm_mm->context.vdso_base)
  235. return "[vdso]";
  236. return NULL;
  237. }
  238. static int __init vdso_init(void)
  239. {
  240. int i;
  241. if (!vdso_enabled)
  242. return 0;
  243. vdso_init_data(vdso_data);
  244. #if defined(CONFIG_32BIT) || defined(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. #ifdef CONFIG_64BIT
  262. /* Calculate the size of the 64 bit vDSO */
  263. vdso64_pages = ((&vdso64_end - &vdso64_start
  264. + PAGE_SIZE - 1) >> PAGE_SHIFT) + 1;
  265. /* Make sure pages are in the correct state */
  266. vdso64_pagelist = kzalloc(sizeof(struct page *) * (vdso64_pages + 1),
  267. GFP_KERNEL);
  268. BUG_ON(vdso64_pagelist == NULL);
  269. for (i = 0; i < vdso64_pages - 1; i++) {
  270. struct page *pg = virt_to_page(vdso64_kbase + i*PAGE_SIZE);
  271. ClearPageReserved(pg);
  272. get_page(pg);
  273. vdso64_pagelist[i] = pg;
  274. }
  275. vdso64_pagelist[vdso64_pages - 1] = virt_to_page(vdso_data);
  276. vdso64_pagelist[vdso64_pages] = NULL;
  277. #ifndef CONFIG_SMP
  278. if (vdso_alloc_per_cpu(0, &S390_lowcore))
  279. BUG();
  280. #endif
  281. vdso_init_cr5();
  282. #endif /* CONFIG_64BIT */
  283. get_page(virt_to_page(vdso_data));
  284. smp_wmb();
  285. return 0;
  286. }
  287. arch_initcall(vdso_init);
  288. int in_gate_area_no_task(unsigned long addr)
  289. {
  290. return 0;
  291. }
  292. int in_gate_area(struct task_struct *task, unsigned long addr)
  293. {
  294. return 0;
  295. }
  296. struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
  297. {
  298. return NULL;
  299. }