vdso.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. BUG_ON(!PAGE_ALIGNED(image->data));
  38. BUG_ON(!PAGE_ALIGNED(image->size));
  39. num_pages = image->size / PAGE_SIZE;
  40. for (i = 0; i < num_pages; i++) {
  41. image->mapping.pages[i] =
  42. virt_to_page(image->data + (i * PAGE_SIZE));
  43. }
  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. down_write(&mm->mmap_sem);
  89. /*
  90. * Determine total area size. This includes the VDSO data itself, the
  91. * data page, and the GIC user page if present. Always create a mapping
  92. * for the GIC user area if the GIC is present regardless of whether it
  93. * is the current clocksource, in case it comes into use later on. We
  94. * only map a page even though the total area is 64K, as we only need
  95. * the counter registers at the start.
  96. */
  97. gic_size = gic_present ? PAGE_SIZE : 0;
  98. vvar_size = gic_size + PAGE_SIZE;
  99. size = vvar_size + image->size;
  100. base = get_unmapped_area(NULL, 0, size, 0, 0);
  101. if (IS_ERR_VALUE(base)) {
  102. ret = base;
  103. goto out;
  104. }
  105. data_addr = base + gic_size;
  106. vdso_addr = data_addr + PAGE_SIZE;
  107. vma = _install_special_mapping(mm, base, vvar_size,
  108. VM_READ | VM_MAYREAD,
  109. &vdso_vvar_mapping);
  110. if (IS_ERR(vma)) {
  111. ret = PTR_ERR(vma);
  112. goto out;
  113. }
  114. /* Map GIC user page. */
  115. if (gic_size) {
  116. ret = gic_get_usm_range(&gic_res);
  117. if (ret)
  118. goto out;
  119. ret = io_remap_pfn_range(vma, base,
  120. gic_res.start >> PAGE_SHIFT,
  121. gic_size,
  122. pgprot_noncached(PAGE_READONLY));
  123. if (ret)
  124. goto out;
  125. }
  126. /* Map data page. */
  127. ret = remap_pfn_range(vma, data_addr,
  128. virt_to_phys(&vdso_data) >> PAGE_SHIFT,
  129. PAGE_SIZE, PAGE_READONLY);
  130. if (ret)
  131. goto out;
  132. /* Map VDSO image. */
  133. vma = _install_special_mapping(mm, vdso_addr, image->size,
  134. VM_READ | VM_EXEC |
  135. VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
  136. &image->mapping);
  137. if (IS_ERR(vma)) {
  138. ret = PTR_ERR(vma);
  139. goto out;
  140. }
  141. mm->context.vdso = (void *)vdso_addr;
  142. ret = 0;
  143. out:
  144. up_write(&mm->mmap_sem);
  145. return ret;
  146. }