xref: /freebsd/sys/x86/include/pvclock.h (revision 942815c54820783d3d4f7f6faa71ab7919b5f0e5)
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  * $FreeBSD$
27  */
28 
29 #ifndef X86_PVCLOCK
30 #define X86_PVCLOCK
31 
32 #include <sys/types.h>
33 
34 #ifdef _KERNEL
35 #include <sys/timetc.h>
36 #endif /* _KERNEL */
37 
38 #define	PVCLOCK_CDEVNAME		"pvclock"
39 
40 struct pvclock_vcpu_time_info {
41 	uint32_t	version;
42 	uint32_t	pad0;
43 	uint64_t	tsc_timestamp;
44 	uint64_t	system_time;
45 	uint32_t	tsc_to_system_mul;
46 	int8_t		tsc_shift;
47 	uint8_t		flags;
48 	uint8_t		pad[2];
49 };
50 
51 #define PVCLOCK_FLAG_TSC_STABLE		0x01
52 #define PVCLOCK_FLAG_GUEST_PASUED	0x02
53 
54 /*
55  * Scale a 64-bit delta by scaling and multiplying by a 32-bit fraction,
56  * yielding a 64-bit result.
57  */
58 static inline uint64_t
59 pvclock_scale_delta(uint64_t delta, uint32_t mul_frac, int shift)
60 {
61 	uint64_t product;
62 
63 	if (shift < 0)
64 		delta >>= -shift;
65 	else
66 		delta <<= shift;
67 #if defined(__i386__)
68 	{
69 		uint32_t tmp1, tmp2;
70 
71 		/**
72 		 * For i386, the formula looks like:
73 		 *
74 		 *   lower = (mul_frac * (delta & UINT_MAX)) >> 32
75 		 *   upper = mul_frac * (delta >> 32)
76 		 *   product = lower + upper
77 		 */
78 		__asm__ (
79 			"mul  %5       ; "
80 			"mov  %4,%%eax ; "
81 			"mov  %%edx,%4 ; "
82 			"mul  %5       ; "
83 			"xor  %5,%5    ; "
84 			"add  %4,%%eax ; "
85 			"adc  %5,%%edx ; "
86 			: "=A" (product), "=r" (tmp1), "=r" (tmp2)
87 			: "a" ((uint32_t)delta), "1" ((uint32_t)(delta >> 32)),
88 			  "2" (mul_frac) );
89 	}
90 #elif defined(__amd64__)
91 	{
92 		unsigned long tmp;
93 
94 		__asm__ (
95 			"mulq %[mul_frac] ; shrd $32, %[hi], %[lo]"
96 			: [lo]"=a" (product), [hi]"=d" (tmp)
97 			: "0" (delta), [mul_frac]"rm"((uint64_t)mul_frac));
98 	}
99 #else
100 #error "pvclock: unsupported x86 architecture?"
101 #endif
102 	return (product);
103 }
104 
105 #ifdef _KERNEL
106 
107 typedef struct pvclock_wall_clock *pvclock_get_wallclock_t(void *arg);
108 
109 struct pvclock_wall_clock {
110 	uint32_t	version;
111 	uint32_t	sec;
112 	uint32_t	nsec;
113 };
114 
115 struct pvclock {
116 	/* Public; initialized by the caller of 'pvclock_init()': */
117 	pvclock_get_wallclock_t		*get_wallclock;
118 	void				*get_wallclock_arg;
119 	struct pvclock_vcpu_time_info	*timeinfos;
120 	bool				 stable_flag_supported;
121 
122 	/* Private; initialized by the 'pvclock' API: */
123 	bool				 vdso_force_unstable;
124 	bool				 vdso_enable_without_rdtscp;
125 	struct timecounter		 tc;
126 	struct cdev			*cdev;
127 };
128 
129 /*
130  * NOTE: 'pvclock_get_timecount()' and 'pvclock_get_wallclock()' are purely
131  * transitional; they should be removed after 'dev/xen/timer/timer.c' has been
132  * migrated to the 'struct pvclock' API.
133  */
134 void		pvclock_resume(void);
135 uint64_t	pvclock_tsc_freq(struct pvclock_vcpu_time_info *ti);
136 uint64_t	pvclock_get_timecount(struct pvclock_vcpu_time_info *ti);
137 void		pvclock_get_wallclock(struct pvclock_wall_clock *wc,
138 		    struct timespec *ts);
139 
140 void		pvclock_init(struct pvclock *pvc, device_t dev,
141 		    const char *tc_name, int tc_quality, u_int tc_flags);
142 void		pvclock_gettime(struct pvclock *pvc, struct timespec *ts);
143 int		pvclock_destroy(struct pvclock *pvc);
144 
145 #endif /* _KERNEL */
146 
147 #endif
148