Skip to content

Commit faa3e7d

Browse files
hcahcagregkh
authored andcommitted
s390: introduce proper type handling call_on_stack() macro
[ Upstream commit 41d71fe ] The existing CALL_ON_STACK() macro allows for subtle bugs: - There is no type checking of the function that is being called. That is: missing or too many arguments do not cause any compile error or warning. The same is true if the return type of the called function changes. This can lead to quite random bugs. - Sign and zero extension of arguments is missing. Given that the s390 C ABI requires that the caller of a function performs proper sign and zero extension this can also lead to subtle bugs. - If arguments to the CALL_ON_STACK() macros contain functions calls register corruption can happen due to register asm constructs being used. Therefore introduce a new call_on_stack() macro which is supposed to fix all these problems. Reviewed-by: Sven Schnelle <svens@linux.ibm.com> Signed-off-by: Heiko Carstens <hca@linux.ibm.com> Signed-off-by: Vasily Gorbik <gor@linux.ibm.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent be10fff commit faa3e7d

1 file changed

Lines changed: 97 additions & 0 deletions

File tree

‎arch/s390/include/asm/stacktrace.h‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,103 @@ struct stack_frame {
128128
r2; \
129129
})
130130

131+
#define CALL_LARGS_0(...) \
132+
long dummy = 0
133+
#define CALL_LARGS_1(t1, a1) \
134+
long arg1 = (long)(t1)(a1)
135+
#define CALL_LARGS_2(t1, a1, t2, a2) \
136+
CALL_LARGS_1(t1, a1); \
137+
long arg2 = (long)(t2)(a2)
138+
#define CALL_LARGS_3(t1, a1, t2, a2, t3, a3) \
139+
CALL_LARGS_2(t1, a1, t2, a2); \
140+
long arg3 = (long)(t3)(a3)
141+
#define CALL_LARGS_4(t1, a1, t2, a2, t3, a3, t4, a4) \
142+
CALL_LARGS_3(t1, a1, t2, a2, t3, a3); \
143+
long arg4 = (long)(t4)(a4)
144+
#define CALL_LARGS_5(t1, a1, t2, a2, t3, a3, t4, a4, t5, a5) \
145+
CALL_LARGS_4(t1, a1, t2, a2, t3, a3, t4, a4); \
146+
long arg5 = (long)(t5)(a5)
147+
148+
#define CALL_REGS_0 \
149+
register long r2 asm("2") = dummy
150+
#define CALL_REGS_1 \
151+
register long r2 asm("2") = arg1
152+
#define CALL_REGS_2 \
153+
CALL_REGS_1; \
154+
register long r3 asm("3") = arg2
155+
#define CALL_REGS_3 \
156+
CALL_REGS_2; \
157+
register long r4 asm("4") = arg3
158+
#define CALL_REGS_4 \
159+
CALL_REGS_3; \
160+
register long r5 asm("5") = arg4
161+
#define CALL_REGS_5 \
162+
CALL_REGS_4; \
163+
register long r6 asm("6") = arg5
164+
165+
#define CALL_TYPECHECK_0(...)
166+
#define CALL_TYPECHECK_1(t, a, ...) \
167+
typecheck(t, a)
168+
#define CALL_TYPECHECK_2(t, a, ...) \
169+
CALL_TYPECHECK_1(__VA_ARGS__); \
170+
typecheck(t, a)
171+
#define CALL_TYPECHECK_3(t, a, ...) \
172+
CALL_TYPECHECK_2(__VA_ARGS__); \
173+
typecheck(t, a)
174+
#define CALL_TYPECHECK_4(t, a, ...) \
175+
CALL_TYPECHECK_3(__VA_ARGS__); \
176+
typecheck(t, a)
177+
#define CALL_TYPECHECK_5(t, a, ...) \
178+
CALL_TYPECHECK_4(__VA_ARGS__); \
179+
typecheck(t, a)
180+
181+
#define CALL_PARM_0(...) void
182+
#define CALL_PARM_1(t, a, ...) t
183+
#define CALL_PARM_2(t, a, ...) t, CALL_PARM_1(__VA_ARGS__)
184+
#define CALL_PARM_3(t, a, ...) t, CALL_PARM_2(__VA_ARGS__)
185+
#define CALL_PARM_4(t, a, ...) t, CALL_PARM_3(__VA_ARGS__)
186+
#define CALL_PARM_5(t, a, ...) t, CALL_PARM_4(__VA_ARGS__)
187+
#define CALL_PARM_6(t, a, ...) t, CALL_PARM_5(__VA_ARGS__)
188+
189+
/*
190+
* Use call_on_stack() to call a function switching to a specified
191+
* stack. Proper sign and zero extension of function arguments is
192+
* done. Usage:
193+
*
194+
* rc = call_on_stack(nr, stack, rettype, fn, t1, a1, t2, a2, ...)
195+
*
196+
* - nr specifies the number of function arguments of fn.
197+
* - stack specifies the stack to be used.
198+
* - fn is the function to be called.
199+
* - rettype is the return type of fn.
200+
* - t1, a1, ... are pairs, where t1 must match the type of the first
201+
* argument of fn, t2 the second, etc. a1 is the corresponding
202+
* first function argument (not name), etc.
203+
*/
204+
#define call_on_stack(nr, stack, rettype, fn, ...) \
205+
({ \
206+
rettype (*__fn)(CALL_PARM_##nr(__VA_ARGS__)) = fn; \
207+
unsigned long frame = current_frame_address(); \
208+
unsigned long __stack = stack; \
209+
unsigned long prev; \
210+
CALL_LARGS_##nr(__VA_ARGS__); \
211+
CALL_REGS_##nr; \
212+
\
213+
CALL_TYPECHECK_##nr(__VA_ARGS__); \
214+
asm volatile( \
215+
" lgr %[_prev],15\n" \
216+
" lg 15,%[_stack]\n" \
217+
" stg %[_frame],%[_bc](15)\n" \
218+
" brasl 14,%[_fn]\n" \
219+
" lgr 15,%[_prev]\n" \
220+
: [_prev] "=&d" (prev), CALL_FMT_##nr \
221+
: [_stack] "R" (__stack), \
222+
[_bc] "i" (offsetof(struct stack_frame, back_chain)), \
223+
[_frame] "d" (frame), \
224+
[_fn] "X" (__fn) : CALL_CLOBBER_##nr); \
225+
(rettype)r2; \
226+
})
227+
131228
#define CALL_ON_STACK_NORETURN(fn, stack) \
132229
({ \
133230
asm volatile( \

0 commit comments

Comments
 (0)