xref: /openbmc/linux/arch/arm64/mm/extable.c (revision ee8ec048)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Based on arch/arm/mm/extable.c
4  */
5 
6 #include <linux/bitfield.h>
7 #include <linux/extable.h>
8 #include <linux/uaccess.h>
9 
10 #include <asm/asm-extable.h>
11 #include <asm/ptrace.h>
12 
13 typedef bool (*ex_handler_t)(const struct exception_table_entry *,
14 			     struct pt_regs *);
15 
16 static inline unsigned long
17 get_ex_fixup(const struct exception_table_entry *ex)
18 {
19 	return ((unsigned long)&ex->fixup + ex->fixup);
20 }
21 
22 static bool ex_handler_fixup(const struct exception_table_entry *ex,
23 			     struct pt_regs *regs)
24 {
25 	regs->pc = get_ex_fixup(ex);
26 	return true;
27 }
28 
29 static bool ex_handler_uaccess_err_zero(const struct exception_table_entry *ex,
30 					struct pt_regs *regs)
31 {
32 	int reg_err = FIELD_GET(EX_DATA_REG_ERR, ex->data);
33 	int reg_zero = FIELD_GET(EX_DATA_REG_ZERO, ex->data);
34 
35 	pt_regs_write_reg(regs, reg_err, -EFAULT);
36 	pt_regs_write_reg(regs, reg_zero, 0);
37 
38 	regs->pc = get_ex_fixup(ex);
39 	return true;
40 }
41 
42 static bool
43 ex_handler_load_unaligned_zeropad(const struct exception_table_entry *ex,
44 				  struct pt_regs *regs)
45 {
46 	int reg_data = FIELD_GET(EX_DATA_REG_DATA, ex->type);
47 	int reg_addr = FIELD_GET(EX_DATA_REG_ADDR, ex->type);
48 	unsigned long data, addr, offset;
49 
50 	addr = pt_regs_read_reg(regs, reg_addr);
51 
52 	offset = addr & 0x7UL;
53 	addr &= ~0x7UL;
54 
55 	data = *(unsigned long*)addr;
56 
57 #ifndef __AARCH64EB__
58 	data >>= 8 * offset;
59 #else
60 	data <<= 8 * offset;
61 #endif
62 
63 	pt_regs_write_reg(regs, reg_data, data);
64 
65 	regs->pc = get_ex_fixup(ex);
66 	return true;
67 }
68 
69 bool fixup_exception(struct pt_regs *regs)
70 {
71 	const struct exception_table_entry *ex;
72 
73 	ex = search_exception_tables(instruction_pointer(regs));
74 	if (!ex)
75 		return false;
76 
77 	switch (ex->type) {
78 	case EX_TYPE_FIXUP:
79 		return ex_handler_fixup(ex, regs);
80 	case EX_TYPE_BPF:
81 		return ex_handler_bpf(ex, regs);
82 	case EX_TYPE_UACCESS_ERR_ZERO:
83 		return ex_handler_uaccess_err_zero(ex, regs);
84 	case EX_TYPE_LOAD_UNALIGNED_ZEROPAD:
85 		return ex_handler_load_unaligned_zeropad(ex, regs);
86 	}
87 
88 	BUG();
89 }
90