vdso.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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/kernel.h>
  16. #include <linux/mm.h>
  17. #include <linux/sched.h>
  18. #include <linux/slab.h>
  19. #include <linux/timekeeper_internal.h>
  20. #include <asm/abi.h>
  21. #include <asm/mips-cps.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, gic_pfn;
  87. struct vm_area_struct *vma;
  88. int ret;
  89. if (down_write_killable(&mm->mmap_sem))
  90. return -EINTR;
  91. /* Map delay slot emulation page */
  92. base = mmap_region(NULL, STACK_TOP, PAGE_SIZE,
  93. VM_READ|VM_WRITE|VM_EXEC|
  94. VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
  95. 0, NULL);
  96. if (IS_ERR_VALUE(base)) {
  97. ret = base;
  98. goto out;
  99. }
  100. /*
  101. * Determine total area size. This includes the VDSO data itself, the
  102. * data page, and the GIC user page if present. Always create a mapping
  103. * for the GIC user area if the GIC is present regardless of whether it
  104. * is the current clocksource, in case it comes into use later on. We
  105. * only map a page even though the total area is 64K, as we only need
  106. * the counter registers at the start.
  107. */
  108. gic_size = mips_gic_present() ? PAGE_SIZE : 0;
  109. vvar_size = gic_size + PAGE_SIZE;
  110. size = vvar_size + image->size;
  111. /*
  112. * Find a region that's large enough for us to perform the
  113. * colour-matching alignment below.
  114. */
  115. if (cpu_has_dc_aliases)
  116. size += shm_align_mask + 1;
  117. base = get_unmapped_area(NULL, 0, size, 0, 0);
  118. if (IS_ERR_VALUE(base)) {
  119. ret = base;
  120. goto out;
  121. }
  122. /*
  123. * If we suffer from dcache aliasing, ensure that the VDSO data page
  124. * mapping is coloured the same as the kernel's mapping of that memory.
  125. * This ensures that when the kernel updates the VDSO data userland
  126. * will observe it without requiring cache invalidations.
  127. */
  128. if (cpu_has_dc_aliases) {
  129. base = __ALIGN_MASK(base, shm_align_mask);
  130. base += ((unsigned long)&vdso_data - gic_size) & shm_align_mask;
  131. }
  132. data_addr = base + gic_size;
  133. vdso_addr = data_addr + PAGE_SIZE;
  134. vma = _install_special_mapping(mm, base, vvar_size,
  135. VM_READ | VM_MAYREAD,
  136. &vdso_vvar_mapping);
  137. if (IS_ERR(vma)) {
  138. ret = PTR_ERR(vma);
  139. goto out;
  140. }
  141. /* Map GIC user page. */
  142. if (gic_size) {
  143. gic_pfn = virt_to_phys(mips_gic_base + MIPS_GIC_USER_OFS) >> PAGE_SHIFT;
  144. ret = io_remap_pfn_range(vma, base, gic_pfn, gic_size,
  145. pgprot_noncached(PAGE_READONLY));
  146. if (ret)
  147. goto out;
  148. }
  149. /* Map data page. */
  150. ret = remap_pfn_range(vma, data_addr,
  151. virt_to_phys(&vdso_data) >> PAGE_SHIFT,
  152. PAGE_SIZE, PAGE_READONLY);
  153. if (ret)
  154. goto out;
  155. /* Map VDSO image. */
  156. vma = _install_special_mapping(mm, vdso_addr, image->size,
  157. VM_READ | VM_EXEC |
  158. VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
  159. &image->mapping);
  160. if (IS_ERR(vma)) {
  161. ret = PTR_ERR(vma);
  162. goto out;
  163. }
  164. mm->context.vdso = (void *)vdso_addr;
  165. ret = 0;
  166. out:
  167. up_write(&mm->mmap_sem);
  168. return ret;
  169. }