extable.c 1.4 KB
Newer Older
H
Huacai Chen 已提交
1 2 3 4
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
 */
5
#include <linux/bitfield.h>
H
Huacai Chen 已提交
6 7
#include <linux/extable.h>
#include <linux/uaccess.h>
8 9 10 11 12 13 14 15
#include <asm/asm-extable.h>
#include <asm/branch.h>

static inline unsigned long
get_ex_fixup(const struct exception_table_entry *ex)
{
	return ((unsigned long)&ex->fixup + ex->fixup);
}
H
Huacai Chen 已提交
16

17 18 19 20 21 22 23
static inline void regs_set_gpr(struct pt_regs *regs,
				unsigned int offset, unsigned long val)
{
	if (offset && offset <= MAX_REG_OFFSET)
		*(unsigned long *)((unsigned long)regs + offset) = val;
}

24 25
static bool ex_handler_fixup(const struct exception_table_entry *ex,
			     struct pt_regs *regs)
H
Huacai Chen 已提交
26
{
27
	regs->csr_era = get_ex_fixup(ex);
H
Huacai Chen 已提交
28

29 30 31
	return true;
}

32 33 34 35 36 37 38 39 40 41 42 43
static bool ex_handler_uaccess_err_zero(const struct exception_table_entry *ex,
					struct pt_regs *regs)
{
	int reg_err = FIELD_GET(EX_DATA_REG_ERR, ex->data);
	int reg_zero = FIELD_GET(EX_DATA_REG_ZERO, ex->data);

	regs_set_gpr(regs, reg_err * sizeof(unsigned long), -EFAULT);
	regs_set_gpr(regs, reg_zero * sizeof(unsigned long), 0);
	regs->csr_era = get_ex_fixup(ex);

	return true;
}
44 45 46 47

bool fixup_exception(struct pt_regs *regs)
{
	const struct exception_table_entry *ex;
H
Huacai Chen 已提交
48

49 50 51
	ex = search_exception_tables(exception_era(regs));
	if (!ex)
		return false;
H
Huacai Chen 已提交
52

53 54 55
	switch (ex->type) {
	case EX_TYPE_FIXUP:
		return ex_handler_fixup(ex, regs);
56 57
	case EX_TYPE_UACCESS_ERR_ZERO:
		return ex_handler_uaccess_err_zero(ex, regs);
58 59 60
	}

	BUG();
H
Huacai Chen 已提交
61
}