summaryrefslogtreecommitdiff
path: root/arch/loongarch/kernel/ftrace.c
blob: 8c3ec1bc7aad39743b5451dd5b9b0c7807494220 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2022 Loongson Technology Corporation Limited
 */

#include <linux/init.h>
#include <linux/ftrace.h>
#include <linux/syscalls.h>
#include <linux/uaccess.h>

#include <asm/asm.h>
#include <asm/asm-offsets.h>
#include <asm/cacheflush.h>
#include <asm/inst.h>
#include <asm/loongarch.h>
#include <asm/syscall.h>

#include <asm-generic/sections.h>

#ifdef CONFIG_FUNCTION_GRAPH_TRACER

/*
 * As `call _mcount` follows LoongArch psABI, ra-saved operation and
 * stack operation can be found before this insn.
 */

static int ftrace_get_parent_ra_addr(unsigned long insn_addr, int *ra_off)
{
	int limit = 32;
	union loongarch_instruction *insn;

	insn = (union loongarch_instruction *)insn_addr;

	do {
		insn--;
		limit--;

		if (is_ra_save_ins(insn))
			*ra_off = -((1 << 12) - insn->reg2i12_format.immediate);

	} while (!is_stack_alloc_ins(insn) && limit);

	if (!limit)
		return -EINVAL;

	return 0;
}

void prepare_ftrace_return(unsigned long self_addr,
		unsigned long callsite_sp, unsigned long old)
{
	int ra_off;
	unsigned long return_hooker = (unsigned long)&return_to_handler;

	if (unlikely(ftrace_graph_is_dead()))
		return;

	if (unlikely(atomic_read(&current->tracing_graph_pause)))
		return;

	if (ftrace_get_parent_ra_addr(self_addr, &ra_off))
		goto out;

	if (!function_graph_enter(old, self_addr, 0, NULL))
		*(unsigned long *)(callsite_sp + ra_off) = return_hooker;

	return;

out:
	ftrace_graph_stop();
	WARN_ON(1);
}
#endif	/* CONFIG_FUNCTION_GRAPH_TRACER */