1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
/*
* Copyright (C) 2006 Giridhar Pemmasani
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#ifdef CONFIG_X86_64
/* Windows functions must have 32 bytes of shadow space for arguments
* above return address, irrespective of number of args. So argc >= 4
*/
#define alloc_win_stack_frame(argc) \
"sub $(" #argc "+1)*8, %%rsp\n\t"
#define free_win_stack_frame(argc) \
"add $(" #argc "+1)*8, %%rsp\n\t"
/* m is index of Windows arg required; Windows arg 1 should be at
* 0(%rsp), arg 2 at 8(%rsp) and so on after the frame is allocated.
*/
#define lin2win_win_arg(m) "(" #m "-1)*8(%%rsp)"
/* args for Windows function must be in clobber / output list */
#define outputs() \
"=a" (_ret), "=c" (_dummy), "=d" (_dummy), \
"=r" (r8), "=r" (r9), "=r" (r10), "=r" (r11)
#define clobbers() "cc"
#define LIN2WIN0(func) \
({ \
u64 _ret, _dummy; \
register u64 r8 __asm__("r8"); \
register u64 r9 __asm__("r9"); \
register u64 r10 __asm__("r10"); \
register u64 r11 __asm__("r11"); \
__asm__ __volatile__( \
alloc_win_stack_frame(4) \
"callq *%[fptr]\n\t" \
free_win_stack_frame(4) \
: outputs() \
: [fptr] "r" (func) \
: clobbers()); \
_ret; \
})
#define LIN2WIN1(func, arg1) \
({ \
u64 _ret, _dummy; \
register u64 r8 __asm__("r8"); \
register u64 r9 __asm__("r9"); \
register u64 r10 __asm__("r10"); \
register u64 r11 __asm__("r11"); \
__asm__ __volatile__( \
alloc_win_stack_frame(4) \
"callq *%[fptr]\n\t" \
free_win_stack_frame(4) \
: outputs() \
: "c" (arg1), [fptr] "r" (func) \
: clobbers()); \
_ret; \
})
#define LIN2WIN2(func, arg1, arg2) \
({ \
u64 _ret, _dummy; \
register u64 r8 __asm__("r8"); \
register u64 r9 __asm__("r9"); \
register u64 r10 __asm__("r10"); \
register u64 r11 __asm__("r11"); \
__asm__ __volatile__( \
alloc_win_stack_frame(4) \
"callq *%[fptr]\n\t" \
free_win_stack_frame(4) \
: outputs() \
: "c" (arg1), "d" (arg2), [fptr] "r" (func) \
: clobbers()); \
_ret; \
})
#define LIN2WIN3(func, arg1, arg2, arg3) \
({ \
u64 _ret, _dummy; \
register u64 r8 __asm__("r8") = (u64)arg3; \
register u64 r9 __asm__("r9"); \
register u64 r10 __asm__("r10"); \
register u64 r11 __asm__("r11"); \
__asm__ __volatile__( \
alloc_win_stack_frame(4) \
"callq *%[fptr]\n\t" \
free_win_stack_frame(4) \
: outputs() \
: "c" (arg1), "d" (arg2), "r" (r8), \
[fptr] "r" (func) \
: clobbers()); \
_ret; \
})
#define LIN2WIN4(func, arg1, arg2, arg3, arg4) \
({ \
u64 _ret, _dummy; \
register u64 r8 __asm__("r8") = (u64)arg3; \
register u64 r9 __asm__("r9") = (u64)arg4; \
register u64 r10 __asm__("r10"); \
register u64 r11 __asm__("r11"); \
__asm__ __volatile__( \
alloc_win_stack_frame(4) \
"callq *%[fptr]\n\t" \
free_win_stack_frame(4) \
: outputs() \
: "c" (arg1), "d" (arg2), "r" (r8), "r" (r9), \
[fptr] "r" (func) \
: clobbers()); \
_ret; \
})
#define LIN2WIN5(func, arg1, arg2, arg3, arg4, arg5) \
({ \
u64 _ret, _dummy; \
register u64 r8 __asm__("r8") = (u64)arg3; \
register u64 r9 __asm__("r9") = (u64)arg4; \
register u64 r10 __asm__("r10"); \
register u64 r11 __asm__("r11"); \
__asm__ __volatile__( \
alloc_win_stack_frame(5) \
"movq %[rarg5], " lin2win_win_arg(5) "\n\t" \
"callq *%[fptr]\n\t" \
free_win_stack_frame(5) \
: outputs() \
: "c" (arg1), "d" (arg2), "r" (r8), "r" (r9), \
[rarg5] "ri" ((u64)arg5), \
[fptr] "r" (func) \
: clobbers()); \
_ret; \
})
#define LIN2WIN6(func, arg1, arg2, arg3, arg4, arg5, arg6) \
({ \
u64 _ret, _dummy; \
register u64 r8 __asm__("r8") = (u64)arg3; \
register u64 r9 __asm__("r9") = (u64)arg4; \
register u64 r10 __asm__("r10"); \
register u64 r11 __asm__("r11"); \
__asm__ __volatile__( \
alloc_win_stack_frame(6) \
"movq %[rarg5], " lin2win_win_arg(5) "\n\t" \
"movq %[rarg6], " lin2win_win_arg(6) "\n\t" \
"callq *%[fptr]\n\t" \
free_win_stack_frame(6) \
: outputs() \
: "c" (arg1), "d" (arg2), "r" (r8), "r" (r9), \
[rarg5] "ri" ((u64)arg5), [rarg6] "ri" ((u64)arg6), \
[fptr] "r" (func) \
: clobbers()); \
_ret; \
})
#else // CONFIG_X86_64
#define LIN2WIN1(func, arg1) \
({ \
TRACE6("calling %p", func); \
func(arg1); \
})
#define LIN2WIN2(func, arg1, arg2) \
({ \
TRACE6("calling %p", func); \
func(arg1, arg2); \
})
#define LIN2WIN3(func, arg1, arg2, arg3) \
({ \
TRACE6("calling %p", func); \
func(arg1, arg2, arg3); \
})
#define LIN2WIN4(func, arg1, arg2, arg3, arg4) \
({ \
TRACE6("calling %p", func); \
func(arg1, arg2, arg3, arg4); \
})
#define LIN2WIN5(func, arg1, arg2, arg3, arg4, arg5) \
({ \
TRACE6("calling %p", func); \
func(arg1, arg2, arg3, arg4, arg5); \
})
#define LIN2WIN6(func, arg1, arg2, arg3, arg4, arg5, arg6) \
({ \
TRACE6("calling %p", func); \
func(arg1, arg2, arg3, arg4, arg5, arg6); \
})
#endif // CONFIG_X86_64
|