Skip to content

Commit b60d35a

Browse files
committed
samples/ftrace: Add support for ftrace direct samples on powerpc
JIRA: https://issues.redhat.com/browse/RHEL-24555 Conflicts: changed context due to missing upstream commit 46e1879 ("powerpc: Fix stack protector Kconfig test for clang") commit 71db948 Author: Naveen N Rao <naveen@kernel.org> Date: Wed Oct 30 12:38:49 2024 +0530 samples/ftrace: Add support for ftrace direct samples on powerpc Add powerpc 32-bit and 64-bit samples for ftrace direct. This serves to show the sample instruction sequence to be used by ftrace direct calls to adhere to the ftrace ABI. On 64-bit powerpc, TOC setup requires some additional work. Signed-off-by: Naveen N Rao <naveen@kernel.org> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au> Link: https://patch.msgid.link/20241030070850.1361304-17-hbathini@linux.ibm.com Signed-off-by: Viktor Malik <vmalik@redhat.com>
1 parent ce194e9 commit b60d35a

File tree

6 files changed

+414
-5
lines changed

6 files changed

+414
-5
lines changed

arch/powerpc/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ config PPC
278278
select HAVE_REGS_AND_STACK_ACCESS_API
279279
select HAVE_RELIABLE_STACKTRACE
280280
select HAVE_RSEQ
281+
select HAVE_SAMPLE_FTRACE_DIRECT if HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
282+
select HAVE_SAMPLE_FTRACE_DIRECT_MULTI if HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
281283
select HAVE_SETUP_PER_CPU_AREA if PPC64
282284
select HAVE_SOFTIRQ_ON_OWN_STACK
283285
select HAVE_STACKPROTECTOR if PPC32 && $(cc-option,-mstack-protector-guard=tls -mstack-protector-guard-reg=r2)

samples/ftrace/ftrace-direct-modify.c

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include <linux/module.h>
33
#include <linux/kthread.h>
44
#include <linux/ftrace.h>
5-
#ifndef CONFIG_ARM64
5+
#if !defined(CONFIG_ARM64) && !defined(CONFIG_PPC32)
66
#include <asm/asm-offsets.h>
77
#endif
88

@@ -199,6 +199,89 @@ asm (
199199

200200
#endif /* CONFIG_LOONGARCH */
201201

202+
#ifdef CONFIG_PPC
203+
#include <asm/ppc_asm.h>
204+
205+
#ifdef CONFIG_PPC64
206+
#define STACK_FRAME_SIZE 48
207+
#else
208+
#define STACK_FRAME_SIZE 24
209+
#endif
210+
211+
#if defined(CONFIG_PPC64_ELF_ABI_V2) && !defined(CONFIG_PPC_KERNEL_PCREL)
212+
#define PPC64_TOC_SAVE_AND_UPDATE \
213+
" std 2, 24(1)\n" \
214+
" bcl 20, 31, 1f\n" \
215+
" 1: mflr 12\n" \
216+
" ld 2, (99f - 1b)(12)\n"
217+
#define PPC64_TOC_RESTORE \
218+
" ld 2, 24(1)\n"
219+
#define PPC64_TOC \
220+
" 99: .quad .TOC.@tocbase\n"
221+
#else
222+
#define PPC64_TOC_SAVE_AND_UPDATE ""
223+
#define PPC64_TOC_RESTORE ""
224+
#define PPC64_TOC ""
225+
#endif
226+
227+
#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE
228+
#define PPC_FTRACE_RESTORE_LR \
229+
PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
230+
" mtlr 0\n"
231+
#define PPC_FTRACE_RET \
232+
" blr\n"
233+
#else
234+
#define PPC_FTRACE_RESTORE_LR \
235+
PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
236+
" mtctr 0\n"
237+
#define PPC_FTRACE_RET \
238+
" mtlr 0\n" \
239+
" bctr\n"
240+
#endif
241+
242+
asm (
243+
" .pushsection .text, \"ax\", @progbits\n"
244+
" .type my_tramp1, @function\n"
245+
" .globl my_tramp1\n"
246+
" my_tramp1:\n"
247+
PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
248+
PPC_STLU" 1, -"__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
249+
" mflr 0\n"
250+
PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
251+
PPC_STLU" 1, -"__stringify(STACK_FRAME_SIZE)"(1)\n"
252+
PPC64_TOC_SAVE_AND_UPDATE
253+
" bl my_direct_func1\n"
254+
PPC64_TOC_RESTORE
255+
" addi 1, 1, "__stringify(STACK_FRAME_SIZE)"\n"
256+
PPC_FTRACE_RESTORE_LR
257+
" addi 1, 1, "__stringify(STACK_FRAME_MIN_SIZE)"\n"
258+
PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
259+
PPC_FTRACE_RET
260+
" .size my_tramp1, .-my_tramp1\n"
261+
262+
" .type my_tramp2, @function\n"
263+
" .globl my_tramp2\n"
264+
" my_tramp2:\n"
265+
PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
266+
PPC_STLU" 1, -"__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
267+
" mflr 0\n"
268+
PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
269+
PPC_STLU" 1, -"__stringify(STACK_FRAME_SIZE)"(1)\n"
270+
PPC64_TOC_SAVE_AND_UPDATE
271+
" bl my_direct_func2\n"
272+
PPC64_TOC_RESTORE
273+
" addi 1, 1, "__stringify(STACK_FRAME_SIZE)"\n"
274+
PPC_FTRACE_RESTORE_LR
275+
" addi 1, 1, "__stringify(STACK_FRAME_MIN_SIZE)"\n"
276+
PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
277+
PPC_FTRACE_RET
278+
PPC64_TOC
279+
" .size my_tramp2, .-my_tramp2\n"
280+
" .popsection\n"
281+
);
282+
283+
#endif /* CONFIG_PPC */
284+
202285
static struct ftrace_ops direct;
203286

204287
static unsigned long my_tramp = (unsigned long)my_tramp1;

samples/ftrace/ftrace-direct-multi-modify.c

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include <linux/module.h>
33
#include <linux/kthread.h>
44
#include <linux/ftrace.h>
5-
#ifndef CONFIG_ARM64
5+
#if !defined(CONFIG_ARM64) && !defined(CONFIG_PPC32)
66
#include <asm/asm-offsets.h>
77
#endif
88

@@ -225,6 +225,105 @@ asm (
225225

226226
#endif /* CONFIG_LOONGARCH */
227227

228+
#ifdef CONFIG_PPC
229+
#include <asm/ppc_asm.h>
230+
231+
#ifdef CONFIG_PPC64
232+
#define STACK_FRAME_SIZE 48
233+
#else
234+
#define STACK_FRAME_SIZE 24
235+
#endif
236+
237+
#if defined(CONFIG_PPC64_ELF_ABI_V2) && !defined(CONFIG_PPC_KERNEL_PCREL)
238+
#define PPC64_TOC_SAVE_AND_UPDATE \
239+
" std 2, 24(1)\n" \
240+
" bcl 20, 31, 1f\n" \
241+
" 1: mflr 12\n" \
242+
" ld 2, (99f - 1b)(12)\n"
243+
#define PPC64_TOC_RESTORE \
244+
" ld 2, 24(1)\n"
245+
#define PPC64_TOC \
246+
" 99: .quad .TOC.@tocbase\n"
247+
#else
248+
#define PPC64_TOC_SAVE_AND_UPDATE ""
249+
#define PPC64_TOC_RESTORE ""
250+
#define PPC64_TOC ""
251+
#endif
252+
253+
#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE
254+
#define PPC_FTRACE_RESTORE_LR \
255+
PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
256+
" mtlr 0\n"
257+
#define PPC_FTRACE_RET \
258+
" blr\n"
259+
#define PPC_FTRACE_RECOVER_IP \
260+
" lwz 8, 4(3)\n" \
261+
" li 9, 6\n" \
262+
" slw 8, 8, 9\n" \
263+
" sraw 8, 8, 9\n" \
264+
" add 3, 3, 8\n" \
265+
" addi 3, 3, 4\n"
266+
#else
267+
#define PPC_FTRACE_RESTORE_LR \
268+
PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
269+
" mtctr 0\n"
270+
#define PPC_FTRACE_RET \
271+
" mtlr 0\n" \
272+
" bctr\n"
273+
#define PPC_FTRACE_RECOVER_IP ""
274+
#endif
275+
276+
asm (
277+
" .pushsection .text, \"ax\", @progbits\n"
278+
" .type my_tramp1, @function\n"
279+
" .globl my_tramp1\n"
280+
" my_tramp1:\n"
281+
PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
282+
PPC_STLU" 1, -"__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
283+
" mflr 0\n"
284+
PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
285+
PPC_STLU" 1, -"__stringify(STACK_FRAME_SIZE)"(1)\n"
286+
PPC64_TOC_SAVE_AND_UPDATE
287+
PPC_STL" 3, "__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
288+
" mr 3, 0\n"
289+
PPC_FTRACE_RECOVER_IP
290+
" bl my_direct_func1\n"
291+
PPC_LL" 3, "__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
292+
PPC64_TOC_RESTORE
293+
" addi 1, 1, "__stringify(STACK_FRAME_SIZE)"\n"
294+
PPC_FTRACE_RESTORE_LR
295+
" addi 1, 1, "__stringify(STACK_FRAME_MIN_SIZE)"\n"
296+
PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
297+
PPC_FTRACE_RET
298+
" .size my_tramp1, .-my_tramp1\n"
299+
300+
" .type my_tramp2, @function\n"
301+
" .globl my_tramp2\n"
302+
" my_tramp2:\n"
303+
PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
304+
PPC_STLU" 1, -"__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
305+
" mflr 0\n"
306+
PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
307+
PPC_STLU" 1, -"__stringify(STACK_FRAME_SIZE)"(1)\n"
308+
PPC64_TOC_SAVE_AND_UPDATE
309+
PPC_STL" 3, "__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
310+
" mr 3, 0\n"
311+
PPC_FTRACE_RECOVER_IP
312+
" bl my_direct_func2\n"
313+
PPC_LL" 3, "__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
314+
PPC64_TOC_RESTORE
315+
" addi 1, 1, "__stringify(STACK_FRAME_SIZE)"\n"
316+
PPC_FTRACE_RESTORE_LR
317+
" addi 1, 1, "__stringify(STACK_FRAME_MIN_SIZE)"\n"
318+
PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
319+
PPC_FTRACE_RET
320+
PPC64_TOC
321+
" .size my_tramp2, .-my_tramp2\n"
322+
" .popsection\n"
323+
);
324+
325+
#endif /* CONFIG_PPC */
326+
228327
static unsigned long my_tramp = (unsigned long)my_tramp1;
229328
static unsigned long tramps[2] = {
230329
(unsigned long)my_tramp1,

samples/ftrace/ftrace-direct-multi.c

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <linux/mm.h> /* for handle_mm_fault() */
55
#include <linux/ftrace.h>
66
#include <linux/sched/stat.h>
7-
#ifndef CONFIG_ARM64
7+
#if !defined(CONFIG_ARM64) && !defined(CONFIG_PPC32)
88
#include <asm/asm-offsets.h>
99
#endif
1010

@@ -141,6 +141,83 @@ asm (
141141

142142
#endif /* CONFIG_LOONGARCH */
143143

144+
#ifdef CONFIG_PPC
145+
#include <asm/ppc_asm.h>
146+
147+
#ifdef CONFIG_PPC64
148+
#define STACK_FRAME_SIZE 48
149+
#else
150+
#define STACK_FRAME_SIZE 24
151+
#endif
152+
153+
#if defined(CONFIG_PPC64_ELF_ABI_V2) && !defined(CONFIG_PPC_KERNEL_PCREL)
154+
#define PPC64_TOC_SAVE_AND_UPDATE \
155+
" std 2, 24(1)\n" \
156+
" bcl 20, 31, 1f\n" \
157+
" 1: mflr 12\n" \
158+
" ld 2, (99f - 1b)(12)\n"
159+
#define PPC64_TOC_RESTORE \
160+
" ld 2, 24(1)\n"
161+
#define PPC64_TOC \
162+
" 99: .quad .TOC.@tocbase\n"
163+
#else
164+
#define PPC64_TOC_SAVE_AND_UPDATE ""
165+
#define PPC64_TOC_RESTORE ""
166+
#define PPC64_TOC ""
167+
#endif
168+
169+
#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE
170+
#define PPC_FTRACE_RESTORE_LR \
171+
PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
172+
" mtlr 0\n"
173+
#define PPC_FTRACE_RET \
174+
" blr\n"
175+
#define PPC_FTRACE_RECOVER_IP \
176+
" lwz 8, 4(3)\n" \
177+
" li 9, 6\n" \
178+
" slw 8, 8, 9\n" \
179+
" sraw 8, 8, 9\n" \
180+
" add 3, 3, 8\n" \
181+
" addi 3, 3, 4\n"
182+
#else
183+
#define PPC_FTRACE_RESTORE_LR \
184+
PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
185+
" mtctr 0\n"
186+
#define PPC_FTRACE_RET \
187+
" mtlr 0\n" \
188+
" bctr\n"
189+
#define PPC_FTRACE_RECOVER_IP ""
190+
#endif
191+
192+
asm (
193+
" .pushsection .text, \"ax\", @progbits\n"
194+
" .type my_tramp, @function\n"
195+
" .globl my_tramp\n"
196+
" my_tramp:\n"
197+
PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
198+
PPC_STLU" 1, -"__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
199+
" mflr 0\n"
200+
PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
201+
PPC_STLU" 1, -"__stringify(STACK_FRAME_SIZE)"(1)\n"
202+
PPC64_TOC_SAVE_AND_UPDATE
203+
PPC_STL" 3, "__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
204+
" mr 3, 0\n"
205+
PPC_FTRACE_RECOVER_IP
206+
" bl my_direct_func\n"
207+
PPC_LL" 3, "__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
208+
PPC64_TOC_RESTORE
209+
" addi 1, 1, "__stringify(STACK_FRAME_SIZE)"\n"
210+
PPC_FTRACE_RESTORE_LR
211+
" addi 1, 1, "__stringify(STACK_FRAME_MIN_SIZE)"\n"
212+
PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
213+
PPC_FTRACE_RET
214+
PPC64_TOC
215+
" .size my_tramp, .-my_tramp\n"
216+
" .popsection\n"
217+
);
218+
219+
#endif /* CONFIG_PPC */
220+
144221
static struct ftrace_ops direct;
145222

146223
static int __init ftrace_direct_multi_init(void)

0 commit comments

Comments
 (0)