GNU Linux-libre 5.4.274-gnu1
[releases.git] / arch / arm / kernel / perf_callchain.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * ARM callchain support
4  *
5  * Copyright (C) 2009 picoChip Designs, Ltd., Jamie Iles
6  * Copyright (C) 2010 ARM Ltd., Will Deacon <will.deacon@arm.com>
7  *
8  * This code is based on the ARM OProfile backtrace code.
9  */
10 #include <linux/perf_event.h>
11 #include <linux/uaccess.h>
12
13 #include <asm/stacktrace.h>
14
15 /*
16  * The registers we're interested in are at the end of the variable
17  * length saved register structure. The fp points at the end of this
18  * structure so the address of this struct is:
19  * (struct frame_tail *)(xxx->fp)-1
20  *
21  * This code has been adapted from the ARM OProfile support.
22  */
23 struct frame_tail {
24         struct frame_tail __user *fp;
25         unsigned long sp;
26         unsigned long lr;
27 } __attribute__((packed));
28
29 /*
30  * Get the return address for a single stackframe and return a pointer to the
31  * next frame tail.
32  */
33 static struct frame_tail __user *
34 user_backtrace(struct frame_tail __user *tail,
35                struct perf_callchain_entry_ctx *entry)
36 {
37         struct frame_tail buftail;
38         unsigned long err;
39
40         if (!access_ok(tail, sizeof(buftail)))
41                 return NULL;
42
43         pagefault_disable();
44         err = __copy_from_user_inatomic(&buftail, tail, sizeof(buftail));
45         pagefault_enable();
46
47         if (err)
48                 return NULL;
49
50         perf_callchain_store(entry, buftail.lr);
51
52         /*
53          * Frame pointers should strictly progress back up the stack
54          * (towards higher addresses).
55          */
56         if (tail + 1 >= buftail.fp)
57                 return NULL;
58
59         return buftail.fp - 1;
60 }
61
62 void
63 perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs)
64 {
65         struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
66         struct frame_tail __user *tail;
67
68         if (guest_cbs && guest_cbs->is_in_guest()) {
69                 /* We don't support guest os callchain now */
70                 return;
71         }
72
73         perf_callchain_store(entry, regs->ARM_pc);
74
75         if (!current->mm)
76                 return;
77
78         tail = (struct frame_tail __user *)regs->ARM_fp - 1;
79
80         while ((entry->nr < entry->max_stack) &&
81                tail && !((unsigned long)tail & 0x3))
82                 tail = user_backtrace(tail, entry);
83 }
84
85 /*
86  * Gets called by walk_stackframe() for every stackframe. This will be called
87  * whist unwinding the stackframe and is like a subroutine return so we use
88  * the PC.
89  */
90 static int
91 callchain_trace(struct stackframe *fr,
92                 void *data)
93 {
94         struct perf_callchain_entry_ctx *entry = data;
95         perf_callchain_store(entry, fr->pc);
96         return 0;
97 }
98
99 void
100 perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs)
101 {
102         struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
103         struct stackframe fr;
104
105         if (guest_cbs && guest_cbs->is_in_guest()) {
106                 /* We don't support guest os callchain now */
107                 return;
108         }
109
110         arm_get_current_stackframe(regs, &fr);
111         walk_stackframe(&fr, callchain_trace, entry);
112 }
113
114 unsigned long perf_instruction_pointer(struct pt_regs *regs)
115 {
116         struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
117
118         if (guest_cbs && guest_cbs->is_in_guest())
119                 return guest_cbs->get_guest_ip();
120
121         return instruction_pointer(regs);
122 }
123
124 unsigned long perf_misc_flags(struct pt_regs *regs)
125 {
126         struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
127         int misc = 0;
128
129         if (guest_cbs && guest_cbs->is_in_guest()) {
130                 if (guest_cbs->is_user_mode())
131                         misc |= PERF_RECORD_MISC_GUEST_USER;
132                 else
133                         misc |= PERF_RECORD_MISC_GUEST_KERNEL;
134         } else {
135                 if (user_mode(regs))
136                         misc |= PERF_RECORD_MISC_USER;
137                 else
138                         misc |= PERF_RECORD_MISC_KERNEL;
139         }
140
141         return misc;
142 }