1 /* SPDX-License-Identifier: GPL-2.0+ OR MIT */
2 /**************************************************************************
3  *
4  * Copyright 2016-2021 VMware, Inc., Palo Alto, CA., USA
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************
27  *
28  * Based on code from vmware.c and vmmouse.c.
29  * Author:
30  *   Sinclair Yeh <syeh@vmware.com>
31  */
32 #ifndef _VMWGFX_MSG_X86_H
33 #define _VMWGFX_MSG_X86_H
34 
35 
36 #if defined(__i386__) || defined(__x86_64__)
37 
38 #include <asm/vmware.h>
39 
40 /**
41  * Hypervisor-specific bi-directional communication channel.  Should never
42  * execute on bare metal hardware.  The caller must make sure to check for
43  * supported hypervisor before using these macros.
44  *
45  * The last two parameters are both input and output and must be initialized.
46  *
47  * @cmd: [IN] Message Cmd
48  * @in_ebx: [IN] Message Len, through EBX
49  * @in_si: [IN] Input argument through SI, set to 0 if not used
50  * @in_di: [IN] Input argument through DI, set ot 0 if not used
51  * @flags: [IN] hypercall flags + [channel id]
52  * @magic: [IN] hypervisor magic value
53  * @eax: [OUT] value of EAX register
54  * @ebx: [OUT] e.g. status from an HB message status command
55  * @ecx: [OUT] e.g. status from a non-HB message status command
56  * @edx: [OUT] e.g. channel id
57  * @si:  [OUT]
58  * @di:  [OUT]
59  */
60 #define VMW_PORT(cmd, in_ebx, in_si, in_di,	\
61                  flags, magic,		\
62                  eax, ebx, ecx, edx, si, di)	\
63 ({						\
64         asm volatile (VMWARE_HYPERCALL :	\
65                 "=a"(eax),			\
66                 "=b"(ebx),			\
67                 "=c"(ecx),			\
68                 "=d"(edx),			\
69                 "=S"(si),			\
70                 "=D"(di) :			\
71                 "a"(magic),			\
72                 "b"(in_ebx),			\
73                 "c"(cmd),			\
74                 "d"(flags),			\
75                 "S"(in_si),			\
76                 "D"(in_di) :			\
77                 "memory");			\
78 })
79 
80 
81 /**
82  * Hypervisor-specific bi-directional communication channel.  Should never
83  * execute on bare metal hardware.  The caller must make sure to check for
84  * supported hypervisor before using these macros.
85  *
86  * The last 3 parameters are both input and output and must be initialized.
87  *
88  * @cmd: [IN] Message Cmd
89  * @in_ecx: [IN] Message Len, through ECX
90  * @in_si: [IN] Input argument through SI, set to 0 if not used
91  * @in_di: [IN] Input argument through DI, set to 0 if not used
92  * @flags: [IN] hypercall flags + [channel id]
93  * @magic: [IN] hypervisor magic value
94  * @bp:  [IN]
95  * @eax: [OUT] value of EAX register
96  * @ebx: [OUT] e.g. status from an HB message status command
97  * @ecx: [OUT] e.g. status from a non-HB message status command
98  * @edx: [OUT] e.g. channel id
99  * @si:  [OUT]
100  * @di:  [OUT]
101  */
102 #ifdef __x86_64__
103 
104 #define VMW_PORT_HB_OUT(cmd, in_ecx, in_si, in_di,	\
105                         flags, magic, bp,		\
106                         eax, ebx, ecx, edx, si, di)	\
107 ({							\
108         asm volatile ("push %%rbp;"			\
109                 "mov %12, %%rbp;"			\
110                 VMWARE_HYPERCALL_HB_OUT			\
111                 "pop %%rbp;" :				\
112                 "=a"(eax),				\
113                 "=b"(ebx),				\
114                 "=c"(ecx),				\
115                 "=d"(edx),				\
116                 "=S"(si),				\
117                 "=D"(di) :				\
118                 "a"(magic),				\
119                 "b"(cmd),				\
120                 "c"(in_ecx),				\
121                 "d"(flags),				\
122                 "S"(in_si),				\
123                 "D"(in_di),				\
124                 "r"(bp) :				\
125                 "memory", "cc");			\
126 })
127 
128 
129 #define VMW_PORT_HB_IN(cmd, in_ecx, in_si, in_di,	\
130                        flags, magic, bp,		\
131                        eax, ebx, ecx, edx, si, di)	\
132 ({							\
133         asm volatile ("push %%rbp;"			\
134                 "mov %12, %%rbp;"			\
135                 VMWARE_HYPERCALL_HB_IN			\
136                 "pop %%rbp" :				\
137                 "=a"(eax),				\
138                 "=b"(ebx),				\
139                 "=c"(ecx),				\
140                 "=d"(edx),				\
141                 "=S"(si),				\
142                 "=D"(di) :				\
143                 "a"(magic),				\
144                 "b"(cmd),				\
145                 "c"(in_ecx),				\
146                 "d"(flags),				\
147                 "S"(in_si),				\
148                 "D"(in_di),				\
149                 "r"(bp) :				\
150                 "memory", "cc");			\
151 })
152 
153 #elif defined(__i386__)
154 
155 /*
156  * In the 32-bit version of this macro, we store bp in a memory location
157  * because we've ran out of registers.
158  * Now we can't reference that memory location while we've modified
159  * %esp or %ebp, so we first push it on the stack, just before we push
160  * %ebp, and then when we need it we read it from the stack where we
161  * just pushed it.
162  */
163 #define VMW_PORT_HB_OUT(cmd, in_ecx, in_si, in_di,	\
164                         flags, magic, bp,		\
165                         eax, ebx, ecx, edx, si, di)	\
166 ({							\
167         asm volatile ("push %12;"			\
168                 "push %%ebp;"				\
169                 "mov 0x04(%%esp), %%ebp;"		\
170                 VMWARE_HYPERCALL_HB_OUT			\
171                 "pop %%ebp;"				\
172                 "add $0x04, %%esp;" :			\
173                 "=a"(eax),				\
174                 "=b"(ebx),				\
175                 "=c"(ecx),				\
176                 "=d"(edx),				\
177                 "=S"(si),				\
178                 "=D"(di) :				\
179                 "a"(magic),				\
180                 "b"(cmd),				\
181                 "c"(in_ecx),				\
182                 "d"(flags),				\
183                 "S"(in_si),				\
184                 "D"(in_di),				\
185                 "m"(bp) :				\
186                 "memory", "cc");			\
187 })
188 
189 
190 #define VMW_PORT_HB_IN(cmd, in_ecx, in_si, in_di,	\
191                        flags, magic, bp,		\
192                        eax, ebx, ecx, edx, si, di)	\
193 ({							\
194         asm volatile ("push %12;"			\
195                 "push %%ebp;"				\
196                 "mov 0x04(%%esp), %%ebp;"		\
197                 VMWARE_HYPERCALL_HB_IN			\
198                 "pop %%ebp;"				\
199                 "add $0x04, %%esp;" :			\
200                 "=a"(eax),				\
201                 "=b"(ebx),				\
202                 "=c"(ecx),				\
203                 "=d"(edx),				\
204                 "=S"(si),				\
205                 "=D"(di) :				\
206                 "a"(magic),				\
207                 "b"(cmd),				\
208                 "c"(in_ecx),				\
209                 "d"(flags),				\
210                 "S"(in_si),				\
211                 "D"(in_di),				\
212                 "m"(bp) :				\
213                 "memory", "cc");			\
214 })
215 #endif /* defined(__i386__) */
216 
217 #endif /* defined(__i386__) || defined(__x86_64__) */
218 
219 #endif /* _VMWGFX_MSG_X86_H */
220