vsyscall_64.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
  3. * Copyright 2003 Andi Kleen, SuSE Labs.
  4. *
  5. * Thanks to hpa@transmeta.com for some useful hint.
  6. * Special thanks to Ingo Molnar for his early experience with
  7. * a different vsyscall implementation for Linux/IA32 and for the name.
  8. *
  9. * vsyscall 1 is located at -10Mbyte, vsyscall 2 is located
  10. * at virtual address -10Mbyte+1024bytes etc... There are at max 4
  11. * vsyscalls. One vsyscall can reserve more than 1 slot to avoid
  12. * jumping out of line if necessary. We cannot add more with this
  13. * mechanism because older kernels won't return -ENOSYS.
  14. * If we want more than four we need a vDSO.
  15. *
  16. * Note: the concept clashes with user mode linux. If you use UML and
  17. * want per guest time just set the kernel.vsyscall64 sysctl to 0.
  18. */
  19. #include <linux/time.h>
  20. #include <linux/init.h>
  21. #include <linux/kernel.h>
  22. #include <linux/timer.h>
  23. #include <linux/seqlock.h>
  24. #include <linux/jiffies.h>
  25. #include <linux/sysctl.h>
  26. #include <linux/clocksource.h>
  27. #include <linux/getcpu.h>
  28. #include <linux/cpu.h>
  29. #include <linux/smp.h>
  30. #include <linux/notifier.h>
  31. #include <asm/vsyscall.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/page.h>
  34. #include <asm/unistd.h>
  35. #include <asm/fixmap.h>
  36. #include <asm/errno.h>
  37. #include <asm/io.h>
  38. #include <asm/segment.h>
  39. #include <asm/desc.h>
  40. #include <asm/topology.h>
  41. #include <asm/vgtod.h>
  42. #define __vsyscall(nr) \
  43. __attribute__ ((unused, __section__(".vsyscall_" #nr))) notrace
  44. #define __syscall_clobber "r11","cx","memory"
  45. /*
  46. * vsyscall_gtod_data contains data that is :
  47. * - readonly from vsyscalls
  48. * - written by timer interrupt or systcl (/proc/sys/kernel/vsyscall64)
  49. * Try to keep this structure as small as possible to avoid cache line ping pongs
  50. */
  51. int __vgetcpu_mode __section_vgetcpu_mode;
  52. struct vsyscall_gtod_data __vsyscall_gtod_data __section_vsyscall_gtod_data =
  53. {
  54. .lock = SEQLOCK_UNLOCKED,
  55. .sysctl_enabled = 1,
  56. };
  57. void update_vsyscall_tz(void)
  58. {
  59. unsigned long flags;
  60. write_seqlock_irqsave(&vsyscall_gtod_data.lock, flags);
  61. /* sys_tz has changed */
  62. vsyscall_gtod_data.sys_tz = sys_tz;
  63. write_sequnlock_irqrestore(&vsyscall_gtod_data.lock, flags);
  64. }
  65. void update_vsyscall(struct timespec *wall_time, struct clocksource *clock)
  66. {
  67. unsigned long flags;
  68. write_seqlock_irqsave(&vsyscall_gtod_data.lock, flags);
  69. /* copy vsyscall data */
  70. vsyscall_gtod_data.clock.vread = clock->vread;
  71. vsyscall_gtod_data.clock.cycle_last = clock->cycle_last;
  72. vsyscall_gtod_data.clock.mask = clock->mask;
  73. vsyscall_gtod_data.clock.mult = clock->mult;
  74. vsyscall_gtod_data.clock.shift = clock->shift;
  75. vsyscall_gtod_data.wall_time_sec = wall_time->tv_sec;
  76. vsyscall_gtod_data.wall_time_nsec = wall_time->tv_nsec;
  77. vsyscall_gtod_data.wall_to_monotonic = wall_to_monotonic;
  78. write_sequnlock_irqrestore(&vsyscall_gtod_data.lock, flags);
  79. }
  80. /* RED-PEN may want to readd seq locking, but then the variable should be
  81. * write-once.
  82. */
  83. static __always_inline void do_get_tz(struct timezone * tz)
  84. {
  85. *tz = __vsyscall_gtod_data.sys_tz;
  86. }
  87. static __always_inline int gettimeofday(struct timeval *tv, struct timezone *tz)
  88. {
  89. int ret;
  90. asm volatile("syscall"
  91. : "=a" (ret)
  92. : "0" (__NR_gettimeofday),"D" (tv),"S" (tz)
  93. : __syscall_clobber );
  94. return ret;
  95. }
  96. static __always_inline long time_syscall(long *t)
  97. {
  98. long secs;
  99. asm volatile("syscall"
  100. : "=a" (secs)
  101. : "0" (__NR_time),"D" (t) : __syscall_clobber);
  102. return secs;
  103. }
  104. static __always_inline void do_vgettimeofday(struct timeval * tv)
  105. {
  106. cycle_t now, base, mask, cycle_delta;
  107. unsigned seq;
  108. unsigned long mult, shift, nsec;
  109. cycle_t (*vread)(void);
  110. do {
  111. seq = read_seqbegin(&__vsyscall_gtod_data.lock);
  112. vread = __vsyscall_gtod_data.clock.vread;
  113. if (unlikely(!__vsyscall_gtod_data.sysctl_enabled || !vread)) {
  114. gettimeofday(tv,NULL);
  115. return;
  116. }
  117. now = vread();
  118. base = __vsyscall_gtod_data.clock.cycle_last;
  119. mask = __vsyscall_gtod_data.clock.mask;
  120. mult = __vsyscall_gtod_data.clock.mult;
  121. shift = __vsyscall_gtod_data.clock.shift;
  122. tv->tv_sec = __vsyscall_gtod_data.wall_time_sec;
  123. nsec = __vsyscall_gtod_data.wall_time_nsec;
  124. } while (read_seqretry(&__vsyscall_gtod_data.lock, seq));
  125. /* calculate interval: */
  126. cycle_delta = (now - base) & mask;
  127. /* convert to nsecs: */
  128. nsec += (cycle_delta * mult) >> shift;
  129. while (nsec >= NSEC_PER_SEC) {
  130. tv->tv_sec += 1;
  131. nsec -= NSEC_PER_SEC;
  132. }
  133. tv->tv_usec = nsec / NSEC_PER_USEC;
  134. }
  135. int __vsyscall(0) vgettimeofday(struct timeval * tv, struct timezone * tz)
  136. {
  137. if (tv)
  138. do_vgettimeofday(tv);
  139. if (tz)
  140. do_get_tz(tz);
  141. return 0;
  142. }
  143. /* This will break when the xtime seconds get inaccurate, but that is
  144. * unlikely */
  145. time_t __vsyscall(1) vtime(time_t *t)
  146. {
  147. struct timeval tv;
  148. time_t result;
  149. if (unlikely(!__vsyscall_gtod_data.sysctl_enabled))
  150. return time_syscall(t);
  151. vgettimeofday(&tv, NULL);
  152. result = tv.tv_sec;
  153. if (t)
  154. *t = result;
  155. return result;
  156. }
  157. /* Fast way to get current CPU and node.
  158. This helps to do per node and per CPU caches in user space.
  159. The result is not guaranteed without CPU affinity, but usually
  160. works out because the scheduler tries to keep a thread on the same
  161. CPU.
  162. tcache must point to a two element sized long array.
  163. All arguments can be NULL. */
  164. long __vsyscall(2)
  165. vgetcpu(unsigned *cpu, unsigned *node, struct getcpu_cache *tcache)
  166. {
  167. unsigned int p;
  168. unsigned long j = 0;
  169. /* Fast cache - only recompute value once per jiffies and avoid
  170. relatively costly rdtscp/cpuid otherwise.
  171. This works because the scheduler usually keeps the process
  172. on the same CPU and this syscall doesn't guarantee its
  173. results anyways.
  174. We do this here because otherwise user space would do it on
  175. its own in a likely inferior way (no access to jiffies).
  176. If you don't like it pass NULL. */
  177. if (tcache && tcache->blob[0] == (j = __jiffies)) {
  178. p = tcache->blob[1];
  179. } else if (__vgetcpu_mode == VGETCPU_RDTSCP) {
  180. /* Load per CPU data from RDTSCP */
  181. native_read_tscp(&p);
  182. } else {
  183. /* Load per CPU data from GDT */
  184. asm("lsl %1,%0" : "=r" (p) : "r" (__PER_CPU_SEG));
  185. }
  186. if (tcache) {
  187. tcache->blob[0] = j;
  188. tcache->blob[1] = p;
  189. }
  190. if (cpu)
  191. *cpu = p & 0xfff;
  192. if (node)
  193. *node = p >> 12;
  194. return 0;
  195. }
  196. static long __vsyscall(3) venosys_1(void)
  197. {
  198. return -ENOSYS;
  199. }
  200. #ifdef CONFIG_SYSCTL
  201. static int
  202. vsyscall_sysctl_change(ctl_table *ctl, int write, struct file * filp,
  203. void __user *buffer, size_t *lenp, loff_t *ppos)
  204. {
  205. return proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
  206. }
  207. static ctl_table kernel_table2[] = {
  208. { .procname = "vsyscall64",
  209. .data = &vsyscall_gtod_data.sysctl_enabled, .maxlen = sizeof(int),
  210. .mode = 0644,
  211. .proc_handler = vsyscall_sysctl_change },
  212. {}
  213. };
  214. static ctl_table kernel_root_table2[] = {
  215. { .ctl_name = CTL_KERN, .procname = "kernel", .mode = 0555,
  216. .child = kernel_table2 },
  217. {}
  218. };
  219. #endif
  220. /* Assume __initcall executes before all user space. Hopefully kmod
  221. doesn't violate that. We'll find out if it does. */
  222. static 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. write_rdtscp_aux((node << 12) | cpu);
  231. /* Store cpu number in limit so that it can be loaded quickly
  232. in user space in vgetcpu.
  233. 12 bits for the CPU and 8 bits for the node. */
  234. d = 0x0f40000000000ULL;
  235. d |= cpu;
  236. d |= (node & 0xf) << 12;
  237. d |= (node >> 4) << 48;
  238. write_gdt_entry(get_cpu_gdt_table(cpu), GDT_ENTRY_PER_CPU, &d, DESCTYPE_S);
  239. }
  240. static void __cpuinit cpu_vsyscall_init(void *arg)
  241. {
  242. /* preemption should be already off */
  243. vsyscall_set_cpu(raw_smp_processor_id());
  244. }
  245. static int __cpuinit
  246. cpu_vsyscall_notifier(struct notifier_block *n, unsigned long action, void *arg)
  247. {
  248. long cpu = (long)arg;
  249. if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
  250. smp_call_function_single(cpu, cpu_vsyscall_init, NULL, 1);
  251. return NOTIFY_DONE;
  252. }
  253. void __init map_vsyscall(void)
  254. {
  255. extern char __vsyscall_0;
  256. unsigned long physaddr_page0 = __pa_symbol(&__vsyscall_0);
  257. /* Note that VSYSCALL_MAPPED_PAGES must agree with the code below. */
  258. __set_fixmap(VSYSCALL_FIRST_PAGE, physaddr_page0, PAGE_KERNEL_VSYSCALL);
  259. }
  260. static int __init vsyscall_init(void)
  261. {
  262. BUG_ON(((unsigned long) &vgettimeofday !=
  263. VSYSCALL_ADDR(__NR_vgettimeofday)));
  264. BUG_ON((unsigned long) &vtime != VSYSCALL_ADDR(__NR_vtime));
  265. BUG_ON((VSYSCALL_ADDR(0) != __fix_to_virt(VSYSCALL_FIRST_PAGE)));
  266. BUG_ON((unsigned long) &vgetcpu != VSYSCALL_ADDR(__NR_vgetcpu));
  267. #ifdef CONFIG_SYSCTL
  268. register_sysctl_table(kernel_root_table2);
  269. #endif
  270. on_each_cpu(cpu_vsyscall_init, NULL, 1);
  271. hotcpu_notifier(cpu_vsyscall_notifier, 0);
  272. return 0;
  273. }
  274. __initcall(vsyscall_init);