vdso.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (C) 2015 Imagination Technologies
  3. * Author: Alex Smith <alex.smith@imgtec.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. */
  10. #include <linux/binfmts.h>
  11. #include <linux/elf.h>
  12. #include <linux/err.h>
  13. #include <linux/init.h>
  14. #include <linux/ioport.h>
  15. #include <linux/irqchip/mips-gic.h>
  16. #include <linux/kernel.h>
  17. #include <linux/mm.h>
  18. #include <linux/sched.h>
  19. #include <linux/slab.h>
  20. #include <linux/timekeeper_internal.h>
  21. #include <asm/abi.h>
  22. #include <asm/page.h>
  23. #include <asm/vdso.h>
  24. /* Kernel-provided data used by the VDSO. */
  25. static union mips_vdso_data vdso_data __page_aligned_data;
  26. /*
  27. * Mapping for the VDSO data/GIC pages. The real pages are mapped manually, as
  28. * what we map and where within the area they are mapped is determined at
  29. * runtime.
  30. */
  31. static struct page *no_pages[] = { NULL };
  32. static struct vm_special_mapping vdso_vvar_mapping = {
  33. .name = "[vvar]",
  34. .pages = no_pages,
  35. };
  36. static void __init init_vdso_image(struct mips_vdso_image *image)
  37. {
  38. unsigned long num_pages, i;
  39. unsigned long data_pfn;
  40. BUG_ON(!PAGE_ALIGNED(image->data));
  41. BUG_ON(!PAGE_ALIGNED(image->size));
  42. num_pages = image->size / PAGE_SIZE;
  43. data_pfn = __phys_to_pfn(__pa_symbol(image->data));
  44. for (i = 0; i < num_pages; i++)
  45. image->mapping.pages[i] = pfn_to_page(data_pfn + i);
  46. }
  47. static int __init init_vdso(void)
  48. {
  49. init_vdso_image(&vdso_image);
  50. #ifdef CONFIG_MIPS32_O32
  51. init_vdso_image(&vdso_image_o32);
  52. #endif
  53. #ifdef CONFIG_MIPS32_N32
  54. init_vdso_image(&vdso_image_n32);
  55. #endif
  56. return 0;
  57. }
  58. subsys_initcall(init_vdso);
  59. void update_vsyscall(struct timekeeper *tk)
  60. {
  61. vdso_data_write_begin(&vdso_data);
  62. vdso_data.xtime_sec = tk->xtime_sec;
  63. vdso_data.xtime_nsec = tk->tkr_mono.xtime_nsec;
  64. vdso_data.wall_to_mono_sec = tk->wall_to_monotonic.tv_sec;
  65. vdso_data.wall_to_mono_nsec = tk->wall_to_monotonic.tv_nsec;
  66. vdso_data.cs_shift = tk->tkr_mono.shift;
  67. vdso_data.clock_mode = tk->tkr_mono.clock->archdata.vdso_clock_mode;
  68. if (vdso_data.clock_mode != VDSO_CLOCK_NONE) {
  69. vdso_data.cs_mult = tk->tkr_mono.mult;
  70. vdso_data.cs_cycle_last = tk->tkr_mono.cycle_last;
  71. vdso_data.cs_mask = tk->tkr_mono.mask;
  72. }
  73. vdso_data_write_end(&vdso_data);
  74. }
  75. void update_vsyscall_tz(void)
  76. {
  77. if (vdso_data.clock_mode != VDSO_CLOCK_NONE) {
  78. vdso_data.tz_minuteswest = sys_tz.tz_minuteswest;
  79. vdso_data.tz_dsttime = sys_tz.tz_dsttime;
  80. }
  81. }
  82. int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  83. {
  84. struct mips_vdso_image *image = current->thread.abi->vdso;
  85. struct mm_struct *mm = current->mm;
  86. unsigned long gic_size, vvar_size, size, base, data_addr, vdso_addr;
  87. struct vm_area_struct *vma;
  88. struct resource gic_res;
  89. int ret;
  90. if (down_write_killable(&mm->mmap_sem))
  91. return -EINTR;
  92. /* Map delay slot emulation page */
  93. base = mmap_region(NULL, STACK_TOP, PAGE_SIZE,
  94. VM_READ | VM_EXEC |
  95. VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
  96. 0);
  97. if (IS_ERR_VALUE(base)) {
  98. ret = base;
  99. goto out;
  100. }
  101. /*
  102. * Determine total area size. This includes the VDSO data itself, the
  103. * data page, and the GIC user page if present. Always create a mapping
  104. * for the GIC user area if the GIC is present regardless of whether it
  105. * is the current clocksource, in case it comes into use later on. We
  106. * only map a page even though the total area is 64K, as we only need
  107. * the counter registers at the start.
  108. */
  109. gic_size = gic_present ? PAGE_SIZE : 0;
  110. vvar_size = gic_size + PAGE_SIZE;
  111. size = vvar_size + image->size;
  112. /*
  113. * Find a region that's large enough for us to perform the
  114. * colour-matching alignment below.
  115. */
  116. if (cpu_has_dc_aliases)
  117. size += shm_align_mask + 1;
  118. base = get_unmapped_area(NULL, 0, size, 0, 0);
  119. if (IS_ERR_VALUE(base)) {
  120. ret = base;
  121. goto out;
  122. }
  123. /*
  124. * If we suffer from dcache aliasing, ensure that the VDSO data page
  125. * mapping is coloured the same as the kernel's mapping of that memory.
  126. * This ensures that when the kernel updates the VDSO data userland
  127. * will observe it without requiring cache invalidations.
  128. */
  129. if (cpu_has_dc_aliases) {
  130. base = __ALIGN_MASK(base, shm_align_mask);
  131. base += ((unsigned long)&vdso_data - gic_size) & shm_align_mask;
  132. }
  133. data_addr = base + gic_size;
  134. vdso_addr = data_addr + PAGE_SIZE;
  135. vma = _install_special_mapping(mm, base, vvar_size,
  136. VM_READ | VM_MAYREAD,
  137. &vdso_vvar_mapping);
  138. if (IS_ERR(vma)) {
  139. ret = PTR_ERR(vma);
  140. goto out;
  141. }
  142. /* Map GIC user page. */
  143. if (gic_size) {
  144. ret = gic_get_usm_range(&gic_res);
  145. if (ret)
  146. goto out;
  147. ret = io_remap_pfn_range(vma, base,
  148. gic_res.start >> PAGE_SHIFT,
  149. gic_size,
  150. pgprot_noncached(PAGE_READONLY));
  151. if (ret)
  152. goto out;
  153. }
  154. /* Map data page. */
  155. ret = remap_pfn_range(vma, data_addr,
  156. virt_to_phys(&vdso_data) >> PAGE_SHIFT,
  157. PAGE_SIZE, PAGE_READONLY);
  158. if (ret)
  159. goto out;
  160. /* Map VDSO image. */
  161. vma = _install_special_mapping(mm, vdso_addr, image->size,
  162. VM_READ | VM_EXEC |
  163. VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
  164. &image->mapping);
  165. if (IS_ERR(vma)) {
  166. ret = PTR_ERR(vma);
  167. goto out;
  168. }
  169. mm->context.vdso = (void *)vdso_addr;
  170. ret = 0;
  171. out:
  172. up_write(&mm->mmap_sem);
  173. return ret;
  174. }