vsyscall.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * linux/arch/x86_64/kernel/vsyscall.c
  3. *
  4. * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
  5. * Copyright 2003 Andi Kleen, SuSE Labs.
  6. *
  7. * Thanks to hpa@transmeta.com for some useful hint.
  8. * Special thanks to Ingo Molnar for his early experience with
  9. * a different vsyscall implementation for Linux/IA32 and for the name.
  10. *
  11. * vsyscall 1 is located at -10Mbyte, vsyscall 2 is located
  12. * at virtual address -10Mbyte+1024bytes etc... There are at max 4
  13. * vsyscalls. One vsyscall can reserve more than 1 slot to avoid
  14. * jumping out of line if necessary. We cannot add more with this
  15. * mechanism because older kernels won't return -ENOSYS.
  16. * If we want more than four we need a vDSO.
  17. *
  18. * Note: the concept clashes with user mode linux. If you use UML and
  19. * want per guest time just set the kernel.vsyscall64 sysctl to 0.
  20. */
  21. #include <linux/time.h>
  22. #include <linux/init.h>
  23. #include <linux/kernel.h>
  24. #include <linux/timer.h>
  25. #include <linux/seqlock.h>
  26. #include <linux/jiffies.h>
  27. #include <linux/sysctl.h>
  28. #include <linux/getcpu.h>
  29. #include <asm/vsyscall.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/page.h>
  32. #include <asm/fixmap.h>
  33. #include <asm/errno.h>
  34. #include <asm/io.h>
  35. #include <asm/segment.h>
  36. #include <asm/desc.h>
  37. #include <asm/topology.h>
  38. #define __vsyscall(nr) __attribute__ ((unused,__section__(".vsyscall_" #nr)))
  39. int __sysctl_vsyscall __section_sysctl_vsyscall = 1;
  40. seqlock_t __xtime_lock __section_xtime_lock = SEQLOCK_UNLOCKED;
  41. int __vgetcpu_mode __section_vgetcpu_mode;
  42. #include <asm/unistd.h>
  43. static __always_inline void timeval_normalize(struct timeval * tv)
  44. {
  45. time_t __sec;
  46. __sec = tv->tv_usec / 1000000;
  47. if (__sec) {
  48. tv->tv_usec %= 1000000;
  49. tv->tv_sec += __sec;
  50. }
  51. }
  52. static __always_inline void do_vgettimeofday(struct timeval * tv)
  53. {
  54. long sequence, t;
  55. unsigned long sec, usec;
  56. do {
  57. sequence = read_seqbegin(&__xtime_lock);
  58. sec = __xtime.tv_sec;
  59. usec = (__xtime.tv_nsec / 1000) +
  60. (__jiffies - __wall_jiffies) * (1000000 / HZ);
  61. if (__vxtime.mode != VXTIME_HPET) {
  62. t = get_cycles_sync();
  63. if (t < __vxtime.last_tsc)
  64. t = __vxtime.last_tsc;
  65. usec += ((t - __vxtime.last_tsc) *
  66. __vxtime.tsc_quot) >> 32;
  67. /* See comment in x86_64 do_gettimeofday. */
  68. } else {
  69. usec += ((readl((void __iomem *)
  70. fix_to_virt(VSYSCALL_HPET) + 0xf0) -
  71. __vxtime.last) * __vxtime.quot) >> 32;
  72. }
  73. } while (read_seqretry(&__xtime_lock, sequence));
  74. tv->tv_sec = sec + usec / 1000000;
  75. tv->tv_usec = usec % 1000000;
  76. }
  77. /* RED-PEN may want to readd seq locking, but then the variable should be write-once. */
  78. static __always_inline void do_get_tz(struct timezone * tz)
  79. {
  80. *tz = __sys_tz;
  81. }
  82. static __always_inline int gettimeofday(struct timeval *tv, struct timezone *tz)
  83. {
  84. int ret;
  85. asm volatile("vsysc2: syscall"
  86. : "=a" (ret)
  87. : "0" (__NR_gettimeofday),"D" (tv),"S" (tz) : __syscall_clobber );
  88. return ret;
  89. }
  90. static __always_inline long time_syscall(long *t)
  91. {
  92. long secs;
  93. asm volatile("vsysc1: syscall"
  94. : "=a" (secs)
  95. : "0" (__NR_time),"D" (t) : __syscall_clobber);
  96. return secs;
  97. }
  98. int __vsyscall(0) vgettimeofday(struct timeval * tv, struct timezone * tz)
  99. {
  100. if (!__sysctl_vsyscall)
  101. return gettimeofday(tv,tz);
  102. if (tv)
  103. do_vgettimeofday(tv);
  104. if (tz)
  105. do_get_tz(tz);
  106. return 0;
  107. }
  108. /* This will break when the xtime seconds get inaccurate, but that is
  109. * unlikely */
  110. time_t __vsyscall(1) vtime(time_t *t)
  111. {
  112. if (!__sysctl_vsyscall)
  113. return time_syscall(t);
  114. else if (t)
  115. *t = __xtime.tv_sec;
  116. return __xtime.tv_sec;
  117. }
  118. /* Fast way to get current CPU and node.
  119. This helps to do per node and per CPU caches in user space.
  120. The result is not guaranteed without CPU affinity, but usually
  121. works out because the scheduler tries to keep a thread on the same
  122. CPU.
  123. tcache must point to a two element sized long array.
  124. All arguments can be NULL. */
  125. long __vsyscall(2)
  126. vgetcpu(unsigned *cpu, unsigned *node, struct getcpu_cache *tcache)
  127. {
  128. unsigned int dummy, p;
  129. unsigned long j = 0;
  130. /* Fast cache - only recompute value once per jiffies and avoid
  131. relatively costly rdtscp/cpuid otherwise.
  132. This works because the scheduler usually keeps the process
  133. on the same CPU and this syscall doesn't guarantee its
  134. results anyways.
  135. We do this here because otherwise user space would do it on
  136. its own in a likely inferior way (no access to jiffies).
  137. If you don't like it pass NULL. */
  138. if (tcache && tcache->t0 == (j = __jiffies)) {
  139. p = tcache->t1;
  140. } else if (__vgetcpu_mode == VGETCPU_RDTSCP) {
  141. /* Load per CPU data from RDTSCP */
  142. rdtscp(dummy, dummy, p);
  143. } else {
  144. /* Load per CPU data from GDT */
  145. asm("lsl %1,%0" : "=r" (p) : "r" (__PER_CPU_SEG));
  146. }
  147. if (tcache) {
  148. tcache->t0 = j;
  149. tcache->t1 = p;
  150. }
  151. if (cpu)
  152. *cpu = p & 0xfff;
  153. if (node)
  154. *node = p >> 12;
  155. return 0;
  156. }
  157. long __vsyscall(3) venosys_1(void)
  158. {
  159. return -ENOSYS;
  160. }
  161. #ifdef CONFIG_SYSCTL
  162. #define SYSCALL 0x050f
  163. #define NOP2 0x9090
  164. /*
  165. * NOP out syscall in vsyscall page when not needed.
  166. */
  167. static int vsyscall_sysctl_change(ctl_table *ctl, int write, struct file * filp,
  168. void __user *buffer, size_t *lenp, loff_t *ppos)
  169. {
  170. extern u16 vsysc1, vsysc2;
  171. u16 __iomem *map1;
  172. u16 __iomem *map2;
  173. int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
  174. if (!write)
  175. return ret;
  176. /* gcc has some trouble with __va(__pa()), so just do it this
  177. way. */
  178. map1 = ioremap(__pa_symbol(&vsysc1), 2);
  179. if (!map1)
  180. return -ENOMEM;
  181. map2 = ioremap(__pa_symbol(&vsysc2), 2);
  182. if (!map2) {
  183. ret = -ENOMEM;
  184. goto out;
  185. }
  186. if (!sysctl_vsyscall) {
  187. writew(SYSCALL, map1);
  188. writew(SYSCALL, map2);
  189. } else {
  190. writew(NOP2, map1);
  191. writew(NOP2, map2);
  192. }
  193. iounmap(map2);
  194. out:
  195. iounmap(map1);
  196. return ret;
  197. }
  198. static int vsyscall_sysctl_nostrat(ctl_table *t, int __user *name, int nlen,
  199. void __user *oldval, size_t __user *oldlenp,
  200. void __user *newval, size_t newlen,
  201. void **context)
  202. {
  203. return -ENOSYS;
  204. }
  205. static ctl_table kernel_table2[] = {
  206. { .ctl_name = 99, .procname = "vsyscall64",
  207. .data = &sysctl_vsyscall, .maxlen = sizeof(int), .mode = 0644,
  208. .strategy = vsyscall_sysctl_nostrat,
  209. .proc_handler = vsyscall_sysctl_change },
  210. { 0, }
  211. };
  212. static ctl_table kernel_root_table2[] = {
  213. { .ctl_name = CTL_KERN, .procname = "kernel", .mode = 0555,
  214. .child = kernel_table2 },
  215. { 0 },
  216. };
  217. #endif
  218. static void __cpuinit write_rdtscp_cb(void *info)
  219. {
  220. write_rdtscp_aux((unsigned long)info);
  221. }
  222. void __cpuinit vsyscall_set_cpu(int cpu)
  223. {
  224. unsigned long *d;
  225. unsigned long node = 0;
  226. #ifdef CONFIG_NUMA
  227. node = cpu_to_node[cpu];
  228. #endif
  229. if (cpu_has(&cpu_data[cpu], X86_FEATURE_RDTSCP)) {
  230. void *info = (void *)((node << 12) | cpu);
  231. /* Can happen on preemptive kernel */
  232. if (get_cpu() == cpu)
  233. write_rdtscp_cb(info);
  234. #ifdef CONFIG_SMP
  235. else {
  236. /* the notifier is unfortunately not executed on the
  237. target CPU */
  238. smp_call_function_single(cpu,write_rdtscp_cb,info,0,1);
  239. }
  240. #endif
  241. put_cpu();
  242. }
  243. /* Store cpu number in limit so that it can be loaded quickly
  244. in user space in vgetcpu.
  245. 12 bits for the CPU and 8 bits for the node. */
  246. d = (unsigned long *)(cpu_gdt(cpu) + GDT_ENTRY_PER_CPU);
  247. *d = 0x0f40000000000ULL;
  248. *d |= cpu;
  249. *d |= (node & 0xf) << 12;
  250. *d |= (node >> 4) << 48;
  251. }
  252. static void __init map_vsyscall(void)
  253. {
  254. extern char __vsyscall_0;
  255. unsigned long physaddr_page0 = __pa_symbol(&__vsyscall_0);
  256. __set_fixmap(VSYSCALL_FIRST_PAGE, physaddr_page0, PAGE_KERNEL_VSYSCALL);
  257. }
  258. static int __init vsyscall_init(void)
  259. {
  260. BUG_ON(((unsigned long) &vgettimeofday !=
  261. VSYSCALL_ADDR(__NR_vgettimeofday)));
  262. BUG_ON((unsigned long) &vtime != VSYSCALL_ADDR(__NR_vtime));
  263. BUG_ON((VSYSCALL_ADDR(0) != __fix_to_virt(VSYSCALL_FIRST_PAGE)));
  264. BUG_ON((unsigned long) &vgetcpu != VSYSCALL_ADDR(__NR_vgetcpu));
  265. map_vsyscall();
  266. #ifdef CONFIG_SYSCTL
  267. register_sysctl_table(kernel_root_table2, 0);
  268. #endif
  269. return 0;
  270. }
  271. __initcall(vsyscall_init);