vdso.c 5.0 KB

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