18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Definitions for use by exception code on Book3-E
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (C) 2008 Ben. Herrenschmidt (benh@kernel.crashing.org), IBM Corp.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci#ifndef _ASM_POWERPC_EXCEPTION_64E_H
88c2ecf20Sopenharmony_ci#define _ASM_POWERPC_EXCEPTION_64E_H
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci/*
118c2ecf20Sopenharmony_ci * SPRGs usage an other considerations...
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * Since TLB miss and other standard exceptions can be interrupted by
148c2ecf20Sopenharmony_ci * critical exceptions which can themselves be interrupted by machine
158c2ecf20Sopenharmony_ci * checks, and since the two later can themselves cause a TLB miss when
168c2ecf20Sopenharmony_ci * hitting the linear mapping for the kernel stacks, we need to be a bit
178c2ecf20Sopenharmony_ci * creative on how we use SPRGs.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * The base idea is that we have one SRPG reserved for critical and one
208c2ecf20Sopenharmony_ci * for machine check interrupts. Those are used to save a GPR that can
218c2ecf20Sopenharmony_ci * then be used to get the PACA, and store as much context as we need
228c2ecf20Sopenharmony_ci * to save in there. That includes saving the SPRGs used by the TLB miss
238c2ecf20Sopenharmony_ci * handler for linear mapping misses and the associated SRR0/1 due to
248c2ecf20Sopenharmony_ci * the above re-entrancy issue.
258c2ecf20Sopenharmony_ci *
268c2ecf20Sopenharmony_ci * So here's the current usage pattern. It's done regardless of which
278c2ecf20Sopenharmony_ci * SPRGs are user-readable though, thus we might have to change some of
288c2ecf20Sopenharmony_ci * this later. In order to do that more easily, we use special constants
298c2ecf20Sopenharmony_ci * for naming them
308c2ecf20Sopenharmony_ci *
318c2ecf20Sopenharmony_ci * WARNING: Some of these SPRGs are user readable. We need to do something
328c2ecf20Sopenharmony_ci * about it as some point by making sure they can't be used to leak kernel
338c2ecf20Sopenharmony_ci * critical data
348c2ecf20Sopenharmony_ci */
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define PACA_EXGDBELL PACA_EXGEN
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci/* We are out of SPRGs so we save some things in the PACA. The normal
398c2ecf20Sopenharmony_ci * exception frame is smaller than the CRIT or MC one though
408c2ecf20Sopenharmony_ci */
418c2ecf20Sopenharmony_ci#define EX_R1		(0 * 8)
428c2ecf20Sopenharmony_ci#define EX_CR		(1 * 8)
438c2ecf20Sopenharmony_ci#define EX_R10		(2 * 8)
448c2ecf20Sopenharmony_ci#define EX_R11		(3 * 8)
458c2ecf20Sopenharmony_ci#define EX_R14		(4 * 8)
468c2ecf20Sopenharmony_ci#define EX_R15		(5 * 8)
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/*
498c2ecf20Sopenharmony_ci * The TLB miss exception uses different slots.
508c2ecf20Sopenharmony_ci *
518c2ecf20Sopenharmony_ci * The bolted variant uses only the first six fields,
528c2ecf20Sopenharmony_ci * which in combination with pgd and kernel_pgd fits in
538c2ecf20Sopenharmony_ci * one 64-byte cache line.
548c2ecf20Sopenharmony_ci */
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci#define EX_TLB_R10	( 0 * 8)
578c2ecf20Sopenharmony_ci#define EX_TLB_R11	( 1 * 8)
588c2ecf20Sopenharmony_ci#define EX_TLB_R14	( 2 * 8)
598c2ecf20Sopenharmony_ci#define EX_TLB_R15	( 3 * 8)
608c2ecf20Sopenharmony_ci#define EX_TLB_R16	( 4 * 8)
618c2ecf20Sopenharmony_ci#define EX_TLB_CR	( 5 * 8)
628c2ecf20Sopenharmony_ci#define EX_TLB_R12	( 6 * 8)
638c2ecf20Sopenharmony_ci#define EX_TLB_R13	( 7 * 8)
648c2ecf20Sopenharmony_ci#define EX_TLB_DEAR	( 8 * 8) /* Level 0 and 2 only */
658c2ecf20Sopenharmony_ci#define EX_TLB_ESR	( 9 * 8) /* Level 0 and 2 only */
668c2ecf20Sopenharmony_ci#define EX_TLB_SRR0	(10 * 8)
678c2ecf20Sopenharmony_ci#define EX_TLB_SRR1	(11 * 8)
688c2ecf20Sopenharmony_ci#define EX_TLB_R7	(12 * 8)
698c2ecf20Sopenharmony_ci#define EX_TLB_SIZE	(13 * 8)
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci#define	START_EXCEPTION(label)						\
728c2ecf20Sopenharmony_ci	.globl exc_##label##_book3e;					\
738c2ecf20Sopenharmony_ciexc_##label##_book3e:
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci/* TLB miss exception prolog
768c2ecf20Sopenharmony_ci *
778c2ecf20Sopenharmony_ci * This prolog handles re-entrancy (up to 3 levels supported in the PACA
788c2ecf20Sopenharmony_ci * though we currently don't test for overflow). It provides you with a
798c2ecf20Sopenharmony_ci * re-entrancy safe working space of r10...r16 and CR with r12 being used
808c2ecf20Sopenharmony_ci * as the exception area pointer in the PACA for that level of re-entrancy
818c2ecf20Sopenharmony_ci * and r13 containing the PACA pointer.
828c2ecf20Sopenharmony_ci *
838c2ecf20Sopenharmony_ci * SRR0 and SRR1 are saved, but DEAR and ESR are not, since they don't apply
848c2ecf20Sopenharmony_ci * as-is for instruction exceptions. It's up to the actual exception code
858c2ecf20Sopenharmony_ci * to save them as well if required.
868c2ecf20Sopenharmony_ci */
878c2ecf20Sopenharmony_ci#define TLB_MISS_PROLOG							    \
888c2ecf20Sopenharmony_ci	mtspr	SPRN_SPRG_TLB_SCRATCH,r12;				    \
898c2ecf20Sopenharmony_ci	mfspr	r12,SPRN_SPRG_TLB_EXFRAME;				    \
908c2ecf20Sopenharmony_ci	std	r10,EX_TLB_R10(r12);					    \
918c2ecf20Sopenharmony_ci	mfcr	r10;							    \
928c2ecf20Sopenharmony_ci	std	r11,EX_TLB_R11(r12);					    \
938c2ecf20Sopenharmony_ci	mfspr	r11,SPRN_SPRG_TLB_SCRATCH;				    \
948c2ecf20Sopenharmony_ci	std	r13,EX_TLB_R13(r12);					    \
958c2ecf20Sopenharmony_ci	mfspr	r13,SPRN_SPRG_PACA;					    \
968c2ecf20Sopenharmony_ci	std	r14,EX_TLB_R14(r12);					    \
978c2ecf20Sopenharmony_ci	addi	r14,r12,EX_TLB_SIZE;					    \
988c2ecf20Sopenharmony_ci	std	r15,EX_TLB_R15(r12);					    \
998c2ecf20Sopenharmony_ci	mfspr	r15,SPRN_SRR1;						    \
1008c2ecf20Sopenharmony_ci	std	r16,EX_TLB_R16(r12);					    \
1018c2ecf20Sopenharmony_ci	mfspr	r16,SPRN_SRR0;						    \
1028c2ecf20Sopenharmony_ci	std	r10,EX_TLB_CR(r12);					    \
1038c2ecf20Sopenharmony_ci	std	r11,EX_TLB_R12(r12);					    \
1048c2ecf20Sopenharmony_ci	mtspr	SPRN_SPRG_TLB_EXFRAME,r14;				    \
1058c2ecf20Sopenharmony_ci	std	r15,EX_TLB_SRR1(r12);					    \
1068c2ecf20Sopenharmony_ci	std	r16,EX_TLB_SRR0(r12);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci/* And these are the matching epilogs that restores things
1098c2ecf20Sopenharmony_ci *
1108c2ecf20Sopenharmony_ci * There are 3 epilogs:
1118c2ecf20Sopenharmony_ci *
1128c2ecf20Sopenharmony_ci * - SUCCESS       : Unwinds one level
1138c2ecf20Sopenharmony_ci * - ERROR         : restore from level 0 and reset
1148c2ecf20Sopenharmony_ci * - ERROR_SPECIAL : restore from current level and reset
1158c2ecf20Sopenharmony_ci *
1168c2ecf20Sopenharmony_ci * Normal errors use ERROR, that is, they restore the initial fault context
1178c2ecf20Sopenharmony_ci * and trigger a fault. However, there is a special case for linear mapping
1188c2ecf20Sopenharmony_ci * errors. Those should basically never happen, but if they do happen, we
1198c2ecf20Sopenharmony_ci * want the error to point out the context that did that linear mapping
1208c2ecf20Sopenharmony_ci * fault, not the initial level 0 (basically, we got a bogus PGF or something
1218c2ecf20Sopenharmony_ci * like that). For userland errors on the linear mapping, there is no
1228c2ecf20Sopenharmony_ci * difference since those are always level 0 anyway
1238c2ecf20Sopenharmony_ci */
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci#define TLB_MISS_RESTORE(freg)						    \
1268c2ecf20Sopenharmony_ci	ld	r14,EX_TLB_CR(r12);					    \
1278c2ecf20Sopenharmony_ci	ld	r10,EX_TLB_R10(r12);					    \
1288c2ecf20Sopenharmony_ci	ld	r15,EX_TLB_SRR0(r12);					    \
1298c2ecf20Sopenharmony_ci	ld	r16,EX_TLB_SRR1(r12);					    \
1308c2ecf20Sopenharmony_ci	mtspr	SPRN_SPRG_TLB_EXFRAME,freg;				    \
1318c2ecf20Sopenharmony_ci	ld	r11,EX_TLB_R11(r12);					    \
1328c2ecf20Sopenharmony_ci	mtcr	r14;							    \
1338c2ecf20Sopenharmony_ci	ld	r13,EX_TLB_R13(r12);					    \
1348c2ecf20Sopenharmony_ci	ld	r14,EX_TLB_R14(r12);					    \
1358c2ecf20Sopenharmony_ci	mtspr	SPRN_SRR0,r15;						    \
1368c2ecf20Sopenharmony_ci	ld	r15,EX_TLB_R15(r12);					    \
1378c2ecf20Sopenharmony_ci	mtspr	SPRN_SRR1,r16;						    \
1388c2ecf20Sopenharmony_ci	ld	r16,EX_TLB_R16(r12);					    \
1398c2ecf20Sopenharmony_ci	ld	r12,EX_TLB_R12(r12);					    \
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci#define TLB_MISS_EPILOG_SUCCESS						    \
1428c2ecf20Sopenharmony_ci	TLB_MISS_RESTORE(r12)
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci#define TLB_MISS_EPILOG_ERROR						    \
1458c2ecf20Sopenharmony_ci	addi	r12,r13,PACA_EXTLB;					    \
1468c2ecf20Sopenharmony_ci	TLB_MISS_RESTORE(r12)
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci#define TLB_MISS_EPILOG_ERROR_SPECIAL					    \
1498c2ecf20Sopenharmony_ci	addi	r11,r13,PACA_EXTLB;					    \
1508c2ecf20Sopenharmony_ci	TLB_MISS_RESTORE(r11)
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci#define SET_IVOR(vector_number, vector_offset)	\
1538c2ecf20Sopenharmony_ci	LOAD_REG_ADDR(r3,interrupt_base_book3e);\
1548c2ecf20Sopenharmony_ci	ori	r3,r3,vector_offset@l;		\
1558c2ecf20Sopenharmony_ci	mtspr	SPRN_IVOR##vector_number,r3;
1568c2ecf20Sopenharmony_ci/*
1578c2ecf20Sopenharmony_ci * powerpc relies on return from interrupt/syscall being context synchronising
1588c2ecf20Sopenharmony_ci * (which rfi is) to support ARCH_HAS_MEMBARRIER_SYNC_CORE without additional
1598c2ecf20Sopenharmony_ci * synchronisation instructions.
1608c2ecf20Sopenharmony_ci */
1618c2ecf20Sopenharmony_ci#define RFI_TO_KERNEL							\
1628c2ecf20Sopenharmony_ci	rfi
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci#define RFI_TO_USER							\
1658c2ecf20Sopenharmony_ci	rfi
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci#endif /* _ASM_POWERPC_EXCEPTION_64E_H */
1688c2ecf20Sopenharmony_ci
169