18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef _SELFTESTS_POWERPC_BASIC_ASM_H
38c2ecf20Sopenharmony_ci#define _SELFTESTS_POWERPC_BASIC_ASM_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <ppc-asm.h>
68c2ecf20Sopenharmony_ci#include <asm/unistd.h>
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#define LOAD_REG_IMMEDIATE(reg, expr) \
98c2ecf20Sopenharmony_ci	lis	reg, (expr)@highest;	\
108c2ecf20Sopenharmony_ci	ori	reg, reg, (expr)@higher;	\
118c2ecf20Sopenharmony_ci	rldicr	reg, reg, 32, 31;	\
128c2ecf20Sopenharmony_ci	oris	reg, reg, (expr)@high;	\
138c2ecf20Sopenharmony_ci	ori	reg, reg, (expr)@l;
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci/*
168c2ecf20Sopenharmony_ci * Note: These macros assume that variables being stored on the stack are
178c2ecf20Sopenharmony_ci * doublewords, while this is usually the case it may not always be the
188c2ecf20Sopenharmony_ci * case for each use case.
198c2ecf20Sopenharmony_ci */
208c2ecf20Sopenharmony_ci#if defined(_CALL_ELF) && _CALL_ELF == 2
218c2ecf20Sopenharmony_ci#define STACK_FRAME_MIN_SIZE 32
228c2ecf20Sopenharmony_ci#define STACK_FRAME_TOC_POS  24
238c2ecf20Sopenharmony_ci#define __STACK_FRAME_PARAM(_param)  (32 + ((_param)*8))
248c2ecf20Sopenharmony_ci#define __STACK_FRAME_LOCAL(_num_params, _var_num)  \
258c2ecf20Sopenharmony_ci	((STACK_FRAME_PARAM(_num_params)) + ((_var_num)*8))
268c2ecf20Sopenharmony_ci#else
278c2ecf20Sopenharmony_ci#define STACK_FRAME_MIN_SIZE 112
288c2ecf20Sopenharmony_ci#define STACK_FRAME_TOC_POS  40
298c2ecf20Sopenharmony_ci#define __STACK_FRAME_PARAM(i)  (48 + ((i)*8))
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci/*
328c2ecf20Sopenharmony_ci * Caveat: if a function passed more than 8 doublewords, the caller will have
338c2ecf20Sopenharmony_ci * made more space... which would render the 112 incorrect.
348c2ecf20Sopenharmony_ci */
358c2ecf20Sopenharmony_ci#define __STACK_FRAME_LOCAL(_num_params, _var_num)  \
368c2ecf20Sopenharmony_ci	(112 + ((_var_num)*8))
378c2ecf20Sopenharmony_ci#endif
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci/* Parameter x saved to the stack */
408c2ecf20Sopenharmony_ci#define STACK_FRAME_PARAM(var)    __STACK_FRAME_PARAM(var)
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci/* Local variable x saved to the stack after x parameters */
438c2ecf20Sopenharmony_ci#define STACK_FRAME_LOCAL(num_params, var)    \
448c2ecf20Sopenharmony_ci	__STACK_FRAME_LOCAL(num_params, var)
458c2ecf20Sopenharmony_ci#define STACK_FRAME_LR_POS   16
468c2ecf20Sopenharmony_ci#define STACK_FRAME_CR_POS   8
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/*
498c2ecf20Sopenharmony_ci * It is very important to note here that _extra is the extra amount of
508c2ecf20Sopenharmony_ci * stack space needed. This space can be accessed using STACK_FRAME_PARAM()
518c2ecf20Sopenharmony_ci * or STACK_FRAME_LOCAL() macros.
528c2ecf20Sopenharmony_ci *
538c2ecf20Sopenharmony_ci * r1 and r2 are not defined in ppc-asm.h (instead they are defined as sp
548c2ecf20Sopenharmony_ci * and toc). Kernel programmers tend to prefer rX even for r1 and r2, hence
558c2ecf20Sopenharmony_ci * %1 and %r2. r0 is defined in ppc-asm.h and therefore %r0 gets
568c2ecf20Sopenharmony_ci * preprocessed incorrectly, hence r0.
578c2ecf20Sopenharmony_ci */
588c2ecf20Sopenharmony_ci#define PUSH_BASIC_STACK(_extra) \
598c2ecf20Sopenharmony_ci	mflr	r0; \
608c2ecf20Sopenharmony_ci	std	r0, STACK_FRAME_LR_POS(%r1); \
618c2ecf20Sopenharmony_ci	stdu	%r1, -(_extra + STACK_FRAME_MIN_SIZE)(%r1); \
628c2ecf20Sopenharmony_ci	mfcr	r0; \
638c2ecf20Sopenharmony_ci	stw	r0, STACK_FRAME_CR_POS(%r1); \
648c2ecf20Sopenharmony_ci	std	%r2, STACK_FRAME_TOC_POS(%r1);
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci#define POP_BASIC_STACK(_extra) \
678c2ecf20Sopenharmony_ci	ld	%r2, STACK_FRAME_TOC_POS(%r1); \
688c2ecf20Sopenharmony_ci	lwz	r0, STACK_FRAME_CR_POS(%r1); \
698c2ecf20Sopenharmony_ci	mtcr	r0; \
708c2ecf20Sopenharmony_ci	addi	%r1, %r1, (_extra + STACK_FRAME_MIN_SIZE); \
718c2ecf20Sopenharmony_ci	ld	r0, STACK_FRAME_LR_POS(%r1); \
728c2ecf20Sopenharmony_ci	mtlr	r0;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci#endif /* _SELFTESTS_POWERPC_BASIC_ASM_H */
75