vsyscall_64.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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) __attribute__ ((unused,__section__(".vsyscall_" #nr)))
  43. #define __syscall_clobber "r11","rcx","memory"
  44. #define __pa_vsymbol(x) \
  45. ({unsigned long v; \
  46. extern char __vsyscall_0; \
  47. asm("" : "=r" (v) : "0" (x)); \
  48. ((v - VSYSCALL_FIRST_PAGE) + __pa_symbol(&__vsyscall_0)); })
  49. /*
  50. * vsyscall_gtod_data contains data that is :
  51. * - readonly from vsyscalls
  52. * - writen by timer interrupt or systcl (/proc/sys/kernel/vsyscall64)
  53. * Try to keep this structure as small as possible to avoid cache line ping pongs
  54. */
  55. int __vgetcpu_mode __section_vgetcpu_mode;
  56. struct vsyscall_gtod_data __vsyscall_gtod_data __section_vsyscall_gtod_data =
  57. {
  58. .lock = SEQLOCK_UNLOCKED,
  59. .sysctl_enabled = 1,
  60. };
  61. void update_vsyscall(struct timespec *wall_time, struct clocksource *clock)
  62. {
  63. unsigned long flags;
  64. write_seqlock_irqsave(&vsyscall_gtod_data.lock, flags);
  65. /* copy vsyscall data */
  66. vsyscall_gtod_data.clock.vread = clock->vread;
  67. vsyscall_gtod_data.clock.cycle_last = clock->cycle_last;
  68. vsyscall_gtod_data.clock.mask = clock->mask;
  69. vsyscall_gtod_data.clock.mult = clock->mult;
  70. vsyscall_gtod_data.clock.shift = clock->shift;
  71. vsyscall_gtod_data.wall_time_sec = wall_time->tv_sec;
  72. vsyscall_gtod_data.wall_time_nsec = wall_time->tv_nsec;
  73. vsyscall_gtod_data.sys_tz = sys_tz;
  74. vsyscall_gtod_data.wall_time_nsec = wall_time->tv_nsec;
  75. vsyscall_gtod_data.wall_to_monotonic = wall_to_monotonic;
  76. write_sequnlock_irqrestore(&vsyscall_gtod_data.lock, flags);
  77. }
  78. /* RED-PEN may want to readd seq locking, but then the variable should be
  79. * write-once.
  80. */
  81. static __always_inline void do_get_tz(struct timezone * tz)
  82. {
  83. *tz = __vsyscall_gtod_data.sys_tz;
  84. }
  85. static __always_inline int gettimeofday(struct timeval *tv, struct timezone *tz)
  86. {
  87. int ret;
  88. asm volatile("vsysc2: syscall"
  89. : "=a" (ret)
  90. : "0" (__NR_gettimeofday),"D" (tv),"S" (tz)
  91. : __syscall_clobber );
  92. return ret;
  93. }
  94. static __always_inline long time_syscall(long *t)
  95. {
  96. long secs;
  97. asm volatile("vsysc1: syscall"
  98. : "=a" (secs)
  99. : "0" (__NR_time),"D" (t) : __syscall_clobber);
  100. return secs;
  101. }
  102. static __always_inline void do_vgettimeofday(struct timeval * tv)
  103. {
  104. cycle_t now, base, mask, cycle_delta;
  105. unsigned seq;
  106. unsigned long mult, shift, nsec;
  107. cycle_t (*vread)(void);
  108. do {
  109. seq = read_seqbegin(&__vsyscall_gtod_data.lock);
  110. vread = __vsyscall_gtod_data.clock.vread;
  111. if (unlikely(!__vsyscall_gtod_data.sysctl_enabled || !vread)) {
  112. gettimeofday(tv,NULL);
  113. return;
  114. }
  115. now = vread();
  116. base = __vsyscall_gtod_data.clock.cycle_last;
  117. mask = __vsyscall_gtod_data.clock.mask;
  118. mult = __vsyscall_gtod_data.clock.mult;
  119. shift = __vsyscall_gtod_data.clock.shift;
  120. tv->tv_sec = __vsyscall_gtod_data.wall_time_sec;
  121. nsec = __vsyscall_gtod_data.wall_time_nsec;
  122. } while (read_seqretry(&__vsyscall_gtod_data.lock, seq));
  123. /* calculate interval: */
  124. cycle_delta = (now - base) & mask;
  125. /* convert to nsecs: */
  126. nsec += (cycle_delta * mult) >> shift;
  127. while (nsec >= NSEC_PER_SEC) {
  128. tv->tv_sec += 1;
  129. nsec -= NSEC_PER_SEC;
  130. }
  131. tv->tv_usec = nsec / NSEC_PER_USEC;
  132. }
  133. int __vsyscall(0) vgettimeofday(struct timeval * tv, struct timezone * tz)
  134. {
  135. if (tv)
  136. do_vgettimeofday(tv);
  137. if (tz)
  138. do_get_tz(tz);
  139. return 0;
  140. }
  141. /* This will break when the xtime seconds get inaccurate, but that is
  142. * unlikely */
  143. time_t __vsyscall(1) vtime(time_t *t)
  144. {
  145. struct timeval tv;
  146. time_t result;
  147. if (unlikely(!__vsyscall_gtod_data.sysctl_enabled))
  148. return time_syscall(t);
  149. vgettimeofday(&tv, 0);
  150. result = tv.tv_sec;
  151. if (t)
  152. *t = result;
  153. return result;
  154. }
  155. /* Fast way to get current CPU and node.
  156. This helps to do per node and per CPU caches in user space.
  157. The result is not guaranteed without CPU affinity, but usually
  158. works out because the scheduler tries to keep a thread on the same
  159. CPU.
  160. tcache must point to a two element sized long array.
  161. All arguments can be NULL. */
  162. long __vsyscall(2)
  163. vgetcpu(unsigned *cpu, unsigned *node, struct getcpu_cache *tcache)
  164. {
  165. unsigned int dummy, p;
  166. unsigned long j = 0;
  167. /* Fast cache - only recompute value once per jiffies and avoid
  168. relatively costly rdtscp/cpuid otherwise.
  169. This works because the scheduler usually keeps the process
  170. on the same CPU and this syscall doesn't guarantee its
  171. results anyways.
  172. We do this here because otherwise user space would do it on
  173. its own in a likely inferior way (no access to jiffies).
  174. If you don't like it pass NULL. */
  175. if (tcache && tcache->blob[0] == (j = __jiffies)) {
  176. p = tcache->blob[1];
  177. } else if (__vgetcpu_mode == VGETCPU_RDTSCP) {
  178. /* Load per CPU data from RDTSCP */
  179. rdtscp(dummy, dummy, p);
  180. } else {
  181. /* Load per CPU data from GDT */
  182. asm("lsl %1,%0" : "=r" (p) : "r" (__PER_CPU_SEG));
  183. }
  184. if (tcache) {
  185. tcache->blob[0] = j;
  186. tcache->blob[1] = p;
  187. }
  188. if (cpu)
  189. *cpu = p & 0xfff;
  190. if (node)
  191. *node = p >> 12;
  192. return 0;
  193. }
  194. long __vsyscall(3) venosys_1(void)
  195. {
  196. return -ENOSYS;
  197. }
  198. #ifdef CONFIG_SYSCTL
  199. #define SYSCALL 0x050f
  200. #define NOP2 0x9090
  201. /*
  202. * NOP out syscall in vsyscall page when not needed.
  203. */
  204. static int vsyscall_sysctl_change(ctl_table *ctl, int write, struct file * filp,
  205. void __user *buffer, size_t *lenp, loff_t *ppos)
  206. {
  207. extern u16 vsysc1, vsysc2;
  208. u16 __iomem *map1;
  209. u16 __iomem *map2;
  210. int ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
  211. if (!write)
  212. return ret;
  213. /* gcc has some trouble with __va(__pa()), so just do it this
  214. way. */
  215. map1 = ioremap(__pa_vsymbol(&vsysc1), 2);
  216. if (!map1)
  217. return -ENOMEM;
  218. map2 = ioremap(__pa_vsymbol(&vsysc2), 2);
  219. if (!map2) {
  220. ret = -ENOMEM;
  221. goto out;
  222. }
  223. if (!vsyscall_gtod_data.sysctl_enabled) {
  224. writew(SYSCALL, map1);
  225. writew(SYSCALL, map2);
  226. } else {
  227. writew(NOP2, map1);
  228. writew(NOP2, map2);
  229. }
  230. iounmap(map2);
  231. out:
  232. iounmap(map1);
  233. return ret;
  234. }
  235. static int vsyscall_sysctl_nostrat(ctl_table *t, int __user *name, int nlen,
  236. void __user *oldval, size_t __user *oldlenp,
  237. void __user *newval, size_t newlen)
  238. {
  239. return -ENOSYS;
  240. }
  241. static ctl_table kernel_table2[] = {
  242. { .ctl_name = 99, .procname = "vsyscall64",
  243. .data = &vsyscall_gtod_data.sysctl_enabled, .maxlen = sizeof(int),
  244. .mode = 0644,
  245. .strategy = vsyscall_sysctl_nostrat,
  246. .proc_handler = vsyscall_sysctl_change },
  247. {}
  248. };
  249. static ctl_table kernel_root_table2[] = {
  250. { .ctl_name = CTL_KERN, .procname = "kernel", .mode = 0555,
  251. .child = kernel_table2 },
  252. {}
  253. };
  254. #endif
  255. /* Assume __initcall executes before all user space. Hopefully kmod
  256. doesn't violate that. We'll find out if it does. */
  257. static void __cpuinit vsyscall_set_cpu(int cpu)
  258. {
  259. unsigned long *d;
  260. unsigned long node = 0;
  261. #ifdef CONFIG_NUMA
  262. node = cpu_to_node[cpu];
  263. #endif
  264. if (cpu_has(&cpu_data[cpu], X86_FEATURE_RDTSCP))
  265. write_rdtscp_aux((node << 12) | cpu);
  266. /* Store cpu number in limit so that it can be loaded quickly
  267. in user space in vgetcpu.
  268. 12 bits for the CPU and 8 bits for the node. */
  269. d = (unsigned long *)(cpu_gdt(cpu) + GDT_ENTRY_PER_CPU);
  270. *d = 0x0f40000000000ULL;
  271. *d |= cpu;
  272. *d |= (node & 0xf) << 12;
  273. *d |= (node >> 4) << 48;
  274. }
  275. static void __cpuinit cpu_vsyscall_init(void *arg)
  276. {
  277. /* preemption should be already off */
  278. vsyscall_set_cpu(raw_smp_processor_id());
  279. }
  280. static int __cpuinit
  281. cpu_vsyscall_notifier(struct notifier_block *n, unsigned long action, void *arg)
  282. {
  283. long cpu = (long)arg;
  284. if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN)
  285. smp_call_function_single(cpu, cpu_vsyscall_init, NULL, 0, 1);
  286. return NOTIFY_DONE;
  287. }
  288. static void __init map_vsyscall(void)
  289. {
  290. extern char __vsyscall_0;
  291. unsigned long physaddr_page0 = __pa_symbol(&__vsyscall_0);
  292. /* Note that VSYSCALL_MAPPED_PAGES must agree with the code below. */
  293. __set_fixmap(VSYSCALL_FIRST_PAGE, physaddr_page0, PAGE_KERNEL_VSYSCALL);
  294. }
  295. static int __init vsyscall_init(void)
  296. {
  297. BUG_ON(((unsigned long) &vgettimeofday !=
  298. VSYSCALL_ADDR(__NR_vgettimeofday)));
  299. BUG_ON((unsigned long) &vtime != VSYSCALL_ADDR(__NR_vtime));
  300. BUG_ON((VSYSCALL_ADDR(0) != __fix_to_virt(VSYSCALL_FIRST_PAGE)));
  301. BUG_ON((unsigned long) &vgetcpu != VSYSCALL_ADDR(__NR_vgetcpu));
  302. map_vsyscall();
  303. #ifdef CONFIG_SYSCTL
  304. register_sysctl_table(kernel_root_table2);
  305. #endif
  306. on_each_cpu(cpu_vsyscall_init, NULL, 0, 1);
  307. hotcpu_notifier(cpu_vsyscall_notifier, 0);
  308. return 0;
  309. }
  310. __initcall(vsyscall_init);