vdso.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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/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/vdso.h>
  22. /* Kernel-provided data used by the VDSO. */
  23. static union mips_vdso_data vdso_data __page_aligned_data;
  24. /*
  25. * Mapping for the VDSO data/GIC pages. The real pages are mapped manually, as
  26. * what we map and where within the area they are mapped is determined at
  27. * runtime.
  28. */
  29. static struct page *no_pages[] = { NULL };
  30. static struct vm_special_mapping vdso_vvar_mapping = {
  31. .name = "[vvar]",
  32. .pages = no_pages,
  33. };
  34. static void __init init_vdso_image(struct mips_vdso_image *image)
  35. {
  36. unsigned long num_pages, i;
  37. unsigned long data_pfn;
  38. BUG_ON(!PAGE_ALIGNED(image->data));
  39. BUG_ON(!PAGE_ALIGNED(image->size));
  40. num_pages = image->size / PAGE_SIZE;
  41. data_pfn = __phys_to_pfn(__pa_symbol(image->data));
  42. for (i = 0; i < num_pages; i++)
  43. image->mapping.pages[i] = pfn_to_page(data_pfn + i);
  44. }
  45. static int __init init_vdso(void)
  46. {
  47. init_vdso_image(&vdso_image);
  48. #ifdef CONFIG_MIPS32_O32
  49. init_vdso_image(&vdso_image_o32);
  50. #endif
  51. #ifdef CONFIG_MIPS32_N32
  52. init_vdso_image(&vdso_image_n32);
  53. #endif
  54. return 0;
  55. }
  56. subsys_initcall(init_vdso);
  57. void update_vsyscall(struct timekeeper *tk)
  58. {
  59. vdso_data_write_begin(&vdso_data);
  60. vdso_data.xtime_sec = tk->xtime_sec;
  61. vdso_data.xtime_nsec = tk->tkr_mono.xtime_nsec;
  62. vdso_data.wall_to_mono_sec = tk->wall_to_monotonic.tv_sec;
  63. vdso_data.wall_to_mono_nsec = tk->wall_to_monotonic.tv_nsec;
  64. vdso_data.cs_shift = tk->tkr_mono.shift;
  65. vdso_data.clock_mode = tk->tkr_mono.clock->archdata.vdso_clock_mode;
  66. if (vdso_data.clock_mode != VDSO_CLOCK_NONE) {
  67. vdso_data.cs_mult = tk->tkr_mono.mult;
  68. vdso_data.cs_cycle_last = tk->tkr_mono.cycle_last;
  69. vdso_data.cs_mask = tk->tkr_mono.mask;
  70. }
  71. vdso_data_write_end(&vdso_data);
  72. }
  73. void update_vsyscall_tz(void)
  74. {
  75. if (vdso_data.clock_mode != VDSO_CLOCK_NONE) {
  76. vdso_data.tz_minuteswest = sys_tz.tz_minuteswest;
  77. vdso_data.tz_dsttime = sys_tz.tz_dsttime;
  78. }
  79. }
  80. int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
  81. {
  82. struct mips_vdso_image *image = current->thread.abi->vdso;
  83. struct mm_struct *mm = current->mm;
  84. unsigned long gic_size, vvar_size, size, base, data_addr, vdso_addr;
  85. struct vm_area_struct *vma;
  86. struct resource gic_res;
  87. int ret;
  88. if (down_write_killable(&mm->mmap_sem))
  89. return -EINTR;
  90. /* Map delay slot emulation page */
  91. base = mmap_region(NULL, STACK_TOP, PAGE_SIZE,
  92. VM_READ|VM_WRITE|VM_EXEC|
  93. VM_MAYREAD|VM_MAYWRITE|VM_MAYEXEC,
  94. 0);
  95. if (IS_ERR_VALUE(base)) {
  96. ret = base;
  97. goto out;
  98. }
  99. /*
  100. * Determine total area size. This includes the VDSO data itself, the
  101. * data page, and the GIC user page if present. Always create a mapping
  102. * for the GIC user area if the GIC is present regardless of whether it
  103. * is the current clocksource, in case it comes into use later on. We
  104. * only map a page even though the total area is 64K, as we only need
  105. * the counter registers at the start.
  106. */
  107. gic_size = gic_present ? PAGE_SIZE : 0;
  108. vvar_size = gic_size + PAGE_SIZE;
  109. size = vvar_size + image->size;
  110. base = get_unmapped_area(NULL, 0, size, 0, 0);
  111. if (IS_ERR_VALUE(base)) {
  112. ret = base;
  113. goto out;
  114. }
  115. data_addr = base + gic_size;
  116. vdso_addr = data_addr + PAGE_SIZE;
  117. vma = _install_special_mapping(mm, base, vvar_size,
  118. VM_READ | VM_MAYREAD,
  119. &vdso_vvar_mapping);
  120. if (IS_ERR(vma)) {
  121. ret = PTR_ERR(vma);
  122. goto out;
  123. }
  124. /* Map GIC user page. */
  125. if (gic_size) {
  126. ret = gic_get_usm_range(&gic_res);
  127. if (ret)
  128. goto out;
  129. ret = io_remap_pfn_range(vma, base,
  130. gic_res.start >> PAGE_SHIFT,
  131. gic_size,
  132. pgprot_noncached(PAGE_READONLY));
  133. if (ret)
  134. goto out;
  135. }
  136. /* Map data page. */
  137. ret = remap_pfn_range(vma, data_addr,
  138. virt_to_phys(&vdso_data) >> PAGE_SHIFT,
  139. PAGE_SIZE, PAGE_READONLY);
  140. if (ret)
  141. goto out;
  142. /* Map VDSO image. */
  143. vma = _install_special_mapping(mm, vdso_addr, image->size,
  144. VM_READ | VM_EXEC |
  145. VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
  146. &image->mapping);
  147. if (IS_ERR(vma)) {
  148. ret = PTR_ERR(vma);
  149. goto out;
  150. }
  151. mm->context.vdso = (void *)vdso_addr;
  152. ret = 0;
  153. out:
  154. up_write(&mm->mmap_sem);
  155. return ret;
  156. }