xref: /freebsd/sys/x86/include/pvclock.h (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*-
2  * Copyright (c) 2014, Bryan Venteicher <bryanv@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #ifndef X86_PVCLOCK
28 #define X86_PVCLOCK
29 
30 #include <sys/types.h>
31 
32 #ifdef _KERNEL
33 #include <sys/timetc.h>
34 #endif /* _KERNEL */
35 
36 #define	PVCLOCK_CDEVNAME		"pvclock"
37 
38 struct pvclock_vcpu_time_info {
39 	uint32_t	version;
40 	uint32_t	pad0;
41 	uint64_t	tsc_timestamp;
42 	uint64_t	system_time;
43 	uint32_t	tsc_to_system_mul;
44 	int8_t		tsc_shift;
45 	uint8_t		flags;
46 	uint8_t		pad[2];
47 };
48 
49 #define PVCLOCK_FLAG_TSC_STABLE		0x01
50 #define PVCLOCK_FLAG_GUEST_PASUED	0x02
51 
52 /*
53  * Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction,
54  * yielding a 64-bit result.
55  */
56 static inline uint64_t
57 pvclock_scale_delta(uint64_t delta, uint32_t mul_frac, int shift)
58 {
59 	uint64_t product;
60 
61 	if (shift < 0)
62 		delta >>= -shift;
63 	else
64 		delta <<= shift;
65 #if defined(__i386__)
66 	{
67 		uint32_t tmp1, tmp2;
68 
69 		/**
70 		 * For i386, the formula looks like:
71 		 *
72 		 *   lower = (mul_frac * (delta & UINT_MAX)) >> 32
73 		 *   upper = mul_frac * (delta >> 32)
74 		 *   product = lower + upper
75 		 */
76 		__asm__ (
77 			"mul  %5       ; "
78 			"mov  %4,%%eax ; "
79 			"mov  %%edx,%4 ; "
80 			"mul  %5       ; "
81 			"xor  %5,%5    ; "
82 			"add  %4,%%eax ; "
83 			"adc  %5,%%edx ; "
84 			: "=A" (product), "=r" (tmp1), "=r" (tmp2)
85 			: "a" ((uint32_t)delta), "1" ((uint32_t)(delta >> 32)),
86 			  "2" (mul_frac) );
87 	}
88 #elif defined(__amd64__)
89 	{
90 		unsigned long tmp;
91 
92 		__asm__ (
93 			"mulq %[mul_frac] ; shrd $32, %[hi], %[lo]"
94 			: [lo]"=a" (product), [hi]"=d" (tmp)
95 			: "0" (delta), [mul_frac]"rm"((uint64_t)mul_frac));
96 	}
97 #else
98 #error "pvclock: unsupported x86 architecture?"
99 #endif
100 	return (product);
101 }
102 
103 #ifdef _KERNEL
104 
105 typedef struct pvclock_wall_clock *pvclock_get_wallclock_t(void *arg);
106 
107 struct pvclock_wall_clock {
108 	uint32_t	version;
109 	uint32_t	sec;
110 	uint32_t	nsec;
111 };
112 
113 struct pvclock {
114 	/* Public; initialized by the caller of 'pvclock_init()': */
115 	pvclock_get_wallclock_t		*get_wallclock;
116 	void				*get_wallclock_arg;
117 	struct pvclock_vcpu_time_info	*timeinfos;
118 	bool				 stable_flag_supported;
119 
120 	/* Private; initialized by the 'pvclock' API: */
121 	bool				 vdso_force_unstable;
122 	bool				 vdso_enable_without_rdtscp;
123 	struct timecounter		 tc;
124 	struct cdev			*cdev;
125 };
126 
127 /*
128  * NOTE: 'pvclock_get_timecount()' and 'pvclock_get_wallclock()' are purely
129  * transitional; they should be removed after 'dev/xen/timer/timer.c' has been
130  * migrated to the 'struct pvclock' API.
131  */
132 void		pvclock_resume(void);
133 uint64_t	pvclock_tsc_freq(struct pvclock_vcpu_time_info *ti);
134 uint64_t	pvclock_get_timecount(struct pvclock_vcpu_time_info *ti);
135 void		pvclock_get_wallclock(struct pvclock_wall_clock *wc,
136 		    struct timespec *ts);
137 
138 void		pvclock_init(struct pvclock *pvc, device_t dev,
139 		    const char *tc_name, int tc_quality, u_int tc_flags);
140 void		pvclock_gettime(struct pvclock *pvc, struct timespec *ts);
141 int		pvclock_destroy(struct pvclock *pvc);
142 
143 #endif /* _KERNEL */
144 
145 #endif
146