vdso.c 8.9 KB

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