xref: /linux/arch/arm64/mm/extable.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21d18c47cSCatalin Marinas /*
31d18c47cSCatalin Marinas  * Based on arch/arm/mm/extable.c
41d18c47cSCatalin Marinas  */
51d18c47cSCatalin Marinas 
62e77a62cSMark Rutland #include <linux/bitfield.h>
70edfa839SPaul Gortmaker #include <linux/extable.h>
81d18c47cSCatalin Marinas #include <linux/uaccess.h>
91d18c47cSCatalin Marinas 
10d6e2cc56SMark Rutland #include <asm/asm-extable.h>
112e77a62cSMark Rutland #include <asm/ptrace.h>
12d6e2cc56SMark Rutland 
13d6e2cc56SMark Rutland static inline unsigned long
get_ex_fixup(const struct exception_table_entry * ex)14d6e2cc56SMark Rutland get_ex_fixup(const struct exception_table_entry *ex)
15d6e2cc56SMark Rutland {
16d6e2cc56SMark Rutland 	return ((unsigned long)&ex->fixup + ex->fixup);
17d6e2cc56SMark Rutland }
18d6e2cc56SMark Rutland 
ex_handler_uaccess_err_zero(const struct exception_table_entry * ex,struct pt_regs * regs)192e77a62cSMark Rutland static bool ex_handler_uaccess_err_zero(const struct exception_table_entry *ex,
202e77a62cSMark Rutland 					struct pt_regs *regs)
212e77a62cSMark Rutland {
222e77a62cSMark Rutland 	int reg_err = FIELD_GET(EX_DATA_REG_ERR, ex->data);
232e77a62cSMark Rutland 	int reg_zero = FIELD_GET(EX_DATA_REG_ZERO, ex->data);
242e77a62cSMark Rutland 
252e77a62cSMark Rutland 	pt_regs_write_reg(regs, reg_err, -EFAULT);
262e77a62cSMark Rutland 	pt_regs_write_reg(regs, reg_zero, 0);
272e77a62cSMark Rutland 
282e77a62cSMark Rutland 	regs->pc = get_ex_fixup(ex);
292e77a62cSMark Rutland 	return true;
302e77a62cSMark Rutland }
312e77a62cSMark Rutland 
32753b3236SMark Rutland static bool
ex_handler_load_unaligned_zeropad(const struct exception_table_entry * ex,struct pt_regs * regs)33753b3236SMark Rutland ex_handler_load_unaligned_zeropad(const struct exception_table_entry *ex,
34753b3236SMark Rutland 				  struct pt_regs *regs)
35753b3236SMark Rutland {
363758a6c7SEvgenii Stepanov 	int reg_data = FIELD_GET(EX_DATA_REG_DATA, ex->data);
373758a6c7SEvgenii Stepanov 	int reg_addr = FIELD_GET(EX_DATA_REG_ADDR, ex->data);
38753b3236SMark Rutland 	unsigned long data, addr, offset;
39753b3236SMark Rutland 
40753b3236SMark Rutland 	addr = pt_regs_read_reg(regs, reg_addr);
41753b3236SMark Rutland 
42753b3236SMark Rutland 	offset = addr & 0x7UL;
43753b3236SMark Rutland 	addr &= ~0x7UL;
44753b3236SMark Rutland 
45753b3236SMark Rutland 	data = *(unsigned long*)addr;
46753b3236SMark Rutland 
47753b3236SMark Rutland #ifndef __AARCH64EB__
48753b3236SMark Rutland 	data >>= 8 * offset;
49753b3236SMark Rutland #else
50753b3236SMark Rutland 	data <<= 8 * offset;
51753b3236SMark Rutland #endif
52753b3236SMark Rutland 
53753b3236SMark Rutland 	pt_regs_write_reg(regs, reg_data, data);
54753b3236SMark Rutland 
55753b3236SMark Rutland 	regs->pc = get_ex_fixup(ex);
56753b3236SMark Rutland 	return true;
57753b3236SMark Rutland }
58753b3236SMark Rutland 
fixup_exception(struct pt_regs * regs)59e8c328d7SMark Rutland bool fixup_exception(struct pt_regs *regs)
601d18c47cSCatalin Marinas {
615d0e7905SMark Rutland 	const struct exception_table_entry *ex;
621d18c47cSCatalin Marinas 
635d0e7905SMark Rutland 	ex = search_exception_tables(instruction_pointer(regs));
645d0e7905SMark Rutland 	if (!ex)
65e8c328d7SMark Rutland 		return false;
661d18c47cSCatalin Marinas 
67d6e2cc56SMark Rutland 	switch (ex->type) {
68d6e2cc56SMark Rutland 	case EX_TYPE_BPF:
69d6e2cc56SMark Rutland 		return ex_handler_bpf(ex, regs);
702e77a62cSMark Rutland 	case EX_TYPE_UACCESS_ERR_ZERO:
71*4953fc3dSTong Tiangen 	case EX_TYPE_KACCESS_ERR_ZERO:
722e77a62cSMark Rutland 		return ex_handler_uaccess_err_zero(ex, regs);
73753b3236SMark Rutland 	case EX_TYPE_LOAD_UNALIGNED_ZEROPAD:
74753b3236SMark Rutland 		return ex_handler_load_unaligned_zeropad(ex, regs);
75d6e2cc56SMark Rutland 	}
7680083428SJean-Philippe Brucker 
77d6e2cc56SMark Rutland 	BUG();
781d18c47cSCatalin Marinas }
79