1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include "test_attach_probe.skel.h"
4 
5 #if defined(__powerpc64__) && defined(_CALL_ELF) && _CALL_ELF == 2
6 
7 #define OP_RT_RA_MASK   0xffff0000UL
8 #define LIS_R2          0x3c400000UL
9 #define ADDIS_R2_R12    0x3c4c0000UL
10 #define ADDI_R2_R2      0x38420000UL
11 
12 static ssize_t get_offset(ssize_t addr, ssize_t base)
13 {
14 	u32 *insn = (u32 *) addr;
15 
16 	/*
17 	 * A PPC64 ABIv2 function may have a local and a global entry
18 	 * point. We need to use the local entry point when patching
19 	 * functions, so identify and step over the global entry point
20 	 * sequence.
21 	 *
22 	 * The global entry point sequence is always of the form:
23 	 *
24 	 * addis r2,r12,XXXX
25 	 * addi  r2,r2,XXXX
26 	 *
27 	 * A linker optimisation may convert the addis to lis:
28 	 *
29 	 * lis   r2,XXXX
30 	 * addi  r2,r2,XXXX
31 	 */
32 	if ((((*insn & OP_RT_RA_MASK) == ADDIS_R2_R12) ||
33 	     ((*insn & OP_RT_RA_MASK) == LIS_R2)) &&
34 	    ((*(insn + 1) & OP_RT_RA_MASK) == ADDI_R2_R2))
35 		return (ssize_t)(insn + 2) - base;
36 	else
37 		return addr - base;
38 }
39 #else
40 #define get_offset(addr, base) (addr - base)
41 #endif
42 
43 ssize_t get_base_addr() {
44 	size_t start, offset;
45 	char buf[256];
46 	FILE *f;
47 
48 	f = fopen("/proc/self/maps", "r");
49 	if (!f)
50 		return -errno;
51 
52 	while (fscanf(f, "%zx-%*x %s %zx %*[^\n]\n",
53 		      &start, buf, &offset) == 3) {
54 		if (strcmp(buf, "r-xp") == 0) {
55 			fclose(f);
56 			return start - offset;
57 		}
58 	}
59 
60 	fclose(f);
61 	return -EINVAL;
62 }
63 
64 void test_attach_probe(void)
65 {
66 	int duration = 0;
67 	struct bpf_link *kprobe_link, *kretprobe_link;
68 	struct bpf_link *uprobe_link, *uretprobe_link;
69 	struct test_attach_probe* skel;
70 	size_t uprobe_offset;
71 	ssize_t base_addr;
72 
73 	base_addr = get_base_addr();
74 	if (CHECK(base_addr < 0, "get_base_addr",
75 		  "failed to find base addr: %zd", base_addr))
76 		return;
77 	uprobe_offset = get_offset((size_t)&get_base_addr, base_addr);
78 
79 	skel = test_attach_probe__open_and_load();
80 	if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
81 		return;
82 	if (CHECK(!skel->bss, "check_bss", ".bss wasn't mmap()-ed\n"))
83 		goto cleanup;
84 
85 	kprobe_link = bpf_program__attach_kprobe(skel->progs.handle_kprobe,
86 						 false /* retprobe */,
87 						 SYS_NANOSLEEP_KPROBE_NAME);
88 	if (CHECK(IS_ERR(kprobe_link), "attach_kprobe",
89 		  "err %ld\n", PTR_ERR(kprobe_link)))
90 		goto cleanup;
91 	skel->links.handle_kprobe = kprobe_link;
92 
93 	kretprobe_link = bpf_program__attach_kprobe(skel->progs.handle_kretprobe,
94 						    true /* retprobe */,
95 						    SYS_NANOSLEEP_KPROBE_NAME);
96 	if (CHECK(IS_ERR(kretprobe_link), "attach_kretprobe",
97 		  "err %ld\n", PTR_ERR(kretprobe_link)))
98 		goto cleanup;
99 	skel->links.handle_kretprobe = kretprobe_link;
100 
101 	uprobe_link = bpf_program__attach_uprobe(skel->progs.handle_uprobe,
102 						 false /* retprobe */,
103 						 0 /* self pid */,
104 						 "/proc/self/exe",
105 						 uprobe_offset);
106 	if (CHECK(IS_ERR(uprobe_link), "attach_uprobe",
107 		  "err %ld\n", PTR_ERR(uprobe_link)))
108 		goto cleanup;
109 	skel->links.handle_uprobe = uprobe_link;
110 
111 	uretprobe_link = bpf_program__attach_uprobe(skel->progs.handle_uretprobe,
112 						    true /* retprobe */,
113 						    -1 /* any pid */,
114 						    "/proc/self/exe",
115 						    uprobe_offset);
116 	if (CHECK(IS_ERR(uretprobe_link), "attach_uretprobe",
117 		  "err %ld\n", PTR_ERR(uretprobe_link)))
118 		goto cleanup;
119 	skel->links.handle_uretprobe = uretprobe_link;
120 
121 	/* trigger & validate kprobe && kretprobe */
122 	usleep(1);
123 
124 	if (CHECK(skel->bss->kprobe_res != 1, "check_kprobe_res",
125 		  "wrong kprobe res: %d\n", skel->bss->kprobe_res))
126 		goto cleanup;
127 	if (CHECK(skel->bss->kretprobe_res != 2, "check_kretprobe_res",
128 		  "wrong kretprobe res: %d\n", skel->bss->kretprobe_res))
129 		goto cleanup;
130 
131 	/* trigger & validate uprobe & uretprobe */
132 	get_base_addr();
133 
134 	if (CHECK(skel->bss->uprobe_res != 3, "check_uprobe_res",
135 		  "wrong uprobe res: %d\n", skel->bss->uprobe_res))
136 		goto cleanup;
137 	if (CHECK(skel->bss->uretprobe_res != 4, "check_uretprobe_res",
138 		  "wrong uretprobe res: %d\n", skel->bss->uretprobe_res))
139 		goto cleanup;
140 
141 cleanup:
142 	test_attach_probe__destroy(skel);
143 }
144