fbcon: Remove dmi quirk table
[linux/fpc-iii.git] / kernel / test_kprobes.c
blobdd53e354f630d9251de443d6a9cd6632c3646211
1 /*
2 * test_kprobes.c - simple sanity test for *probes
4 * Copyright IBM Corp. 2008
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it would be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU General Public License for more details.
17 #define pr_fmt(fmt) "Kprobe smoke test: " fmt
19 #include <linux/kernel.h>
20 #include <linux/kprobes.h>
21 #include <linux/random.h>
23 #define div_factor 3
25 static u32 rand1, preh_val, posth_val;
26 static int errors, handler_errors, num_tests;
27 static u32 (*target)(u32 value);
28 static u32 (*target2)(u32 value);
30 static noinline u32 kprobe_target(u32 value)
32 return (value / div_factor);
35 static int kp_pre_handler(struct kprobe *p, struct pt_regs *regs)
37 if (preemptible()) {
38 handler_errors++;
39 pr_err("pre-handler is preemptible\n");
41 preh_val = (rand1 / div_factor);
42 return 0;
45 static void kp_post_handler(struct kprobe *p, struct pt_regs *regs,
46 unsigned long flags)
48 if (preemptible()) {
49 handler_errors++;
50 pr_err("post-handler is preemptible\n");
52 if (preh_val != (rand1 / div_factor)) {
53 handler_errors++;
54 pr_err("incorrect value in post_handler\n");
56 posth_val = preh_val + div_factor;
59 static struct kprobe kp = {
60 .symbol_name = "kprobe_target",
61 .pre_handler = kp_pre_handler,
62 .post_handler = kp_post_handler
65 static int test_kprobe(void)
67 int ret;
69 ret = register_kprobe(&kp);
70 if (ret < 0) {
71 pr_err("register_kprobe returned %d\n", ret);
72 return ret;
75 ret = target(rand1);
76 unregister_kprobe(&kp);
78 if (preh_val == 0) {
79 pr_err("kprobe pre_handler not called\n");
80 handler_errors++;
83 if (posth_val == 0) {
84 pr_err("kprobe post_handler not called\n");
85 handler_errors++;
88 return 0;
91 static noinline u32 kprobe_target2(u32 value)
93 return (value / div_factor) + 1;
96 static int kp_pre_handler2(struct kprobe *p, struct pt_regs *regs)
98 preh_val = (rand1 / div_factor) + 1;
99 return 0;
102 static void kp_post_handler2(struct kprobe *p, struct pt_regs *regs,
103 unsigned long flags)
105 if (preh_val != (rand1 / div_factor) + 1) {
106 handler_errors++;
107 pr_err("incorrect value in post_handler2\n");
109 posth_val = preh_val + div_factor;
112 static struct kprobe kp2 = {
113 .symbol_name = "kprobe_target2",
114 .pre_handler = kp_pre_handler2,
115 .post_handler = kp_post_handler2
118 static int test_kprobes(void)
120 int ret;
121 struct kprobe *kps[2] = {&kp, &kp2};
123 /* addr and flags should be cleard for reusing kprobe. */
124 kp.addr = NULL;
125 kp.flags = 0;
126 ret = register_kprobes(kps, 2);
127 if (ret < 0) {
128 pr_err("register_kprobes returned %d\n", ret);
129 return ret;
132 preh_val = 0;
133 posth_val = 0;
134 ret = target(rand1);
136 if (preh_val == 0) {
137 pr_err("kprobe pre_handler not called\n");
138 handler_errors++;
141 if (posth_val == 0) {
142 pr_err("kprobe post_handler not called\n");
143 handler_errors++;
146 preh_val = 0;
147 posth_val = 0;
148 ret = target2(rand1);
150 if (preh_val == 0) {
151 pr_err("kprobe pre_handler2 not called\n");
152 handler_errors++;
155 if (posth_val == 0) {
156 pr_err("kprobe post_handler2 not called\n");
157 handler_errors++;
160 unregister_kprobes(kps, 2);
161 return 0;
165 #if 0
166 static u32 jph_val;
168 static u32 j_kprobe_target(u32 value)
170 if (preemptible()) {
171 handler_errors++;
172 pr_err("jprobe-handler is preemptible\n");
174 if (value != rand1) {
175 handler_errors++;
176 pr_err("incorrect value in jprobe handler\n");
179 jph_val = rand1;
180 jprobe_return();
181 return 0;
184 static struct jprobe jp = {
185 .entry = j_kprobe_target,
186 .kp.symbol_name = "kprobe_target"
189 static int test_jprobe(void)
191 int ret;
193 ret = register_jprobe(&jp);
194 if (ret < 0) {
195 pr_err("register_jprobe returned %d\n", ret);
196 return ret;
199 ret = target(rand1);
200 unregister_jprobe(&jp);
201 if (jph_val == 0) {
202 pr_err("jprobe handler not called\n");
203 handler_errors++;
206 return 0;
209 static struct jprobe jp2 = {
210 .entry = j_kprobe_target,
211 .kp.symbol_name = "kprobe_target2"
214 static int test_jprobes(void)
216 int ret;
217 struct jprobe *jps[2] = {&jp, &jp2};
219 /* addr and flags should be cleard for reusing kprobe. */
220 jp.kp.addr = NULL;
221 jp.kp.flags = 0;
222 ret = register_jprobes(jps, 2);
223 if (ret < 0) {
224 pr_err("register_jprobes returned %d\n", ret);
225 return ret;
228 jph_val = 0;
229 ret = target(rand1);
230 if (jph_val == 0) {
231 pr_err("jprobe handler not called\n");
232 handler_errors++;
235 jph_val = 0;
236 ret = target2(rand1);
237 if (jph_val == 0) {
238 pr_err("jprobe handler2 not called\n");
239 handler_errors++;
241 unregister_jprobes(jps, 2);
243 return 0;
245 #else
246 #define test_jprobe() (0)
247 #define test_jprobes() (0)
248 #endif
249 #ifdef CONFIG_KRETPROBES
250 static u32 krph_val;
252 static int entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
254 if (preemptible()) {
255 handler_errors++;
256 pr_err("kretprobe entry handler is preemptible\n");
258 krph_val = (rand1 / div_factor);
259 return 0;
262 static int return_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
264 unsigned long ret = regs_return_value(regs);
266 if (preemptible()) {
267 handler_errors++;
268 pr_err("kretprobe return handler is preemptible\n");
270 if (ret != (rand1 / div_factor)) {
271 handler_errors++;
272 pr_err("incorrect value in kretprobe handler\n");
274 if (krph_val == 0) {
275 handler_errors++;
276 pr_err("call to kretprobe entry handler failed\n");
279 krph_val = rand1;
280 return 0;
283 static struct kretprobe rp = {
284 .handler = return_handler,
285 .entry_handler = entry_handler,
286 .kp.symbol_name = "kprobe_target"
289 static int test_kretprobe(void)
291 int ret;
293 ret = register_kretprobe(&rp);
294 if (ret < 0) {
295 pr_err("register_kretprobe returned %d\n", ret);
296 return ret;
299 ret = target(rand1);
300 unregister_kretprobe(&rp);
301 if (krph_val != rand1) {
302 pr_err("kretprobe handler not called\n");
303 handler_errors++;
306 return 0;
309 static int return_handler2(struct kretprobe_instance *ri, struct pt_regs *regs)
311 unsigned long ret = regs_return_value(regs);
313 if (ret != (rand1 / div_factor) + 1) {
314 handler_errors++;
315 pr_err("incorrect value in kretprobe handler2\n");
317 if (krph_val == 0) {
318 handler_errors++;
319 pr_err("call to kretprobe entry handler failed\n");
322 krph_val = rand1;
323 return 0;
326 static struct kretprobe rp2 = {
327 .handler = return_handler2,
328 .entry_handler = entry_handler,
329 .kp.symbol_name = "kprobe_target2"
332 static int test_kretprobes(void)
334 int ret;
335 struct kretprobe *rps[2] = {&rp, &rp2};
337 /* addr and flags should be cleard for reusing kprobe. */
338 rp.kp.addr = NULL;
339 rp.kp.flags = 0;
340 ret = register_kretprobes(rps, 2);
341 if (ret < 0) {
342 pr_err("register_kretprobe returned %d\n", ret);
343 return ret;
346 krph_val = 0;
347 ret = target(rand1);
348 if (krph_val != rand1) {
349 pr_err("kretprobe handler not called\n");
350 handler_errors++;
353 krph_val = 0;
354 ret = target2(rand1);
355 if (krph_val != rand1) {
356 pr_err("kretprobe handler2 not called\n");
357 handler_errors++;
359 unregister_kretprobes(rps, 2);
360 return 0;
362 #endif /* CONFIG_KRETPROBES */
364 int init_test_probes(void)
366 int ret;
368 target = kprobe_target;
369 target2 = kprobe_target2;
371 do {
372 rand1 = prandom_u32();
373 } while (rand1 <= div_factor);
375 pr_info("started\n");
376 num_tests++;
377 ret = test_kprobe();
378 if (ret < 0)
379 errors++;
381 num_tests++;
382 ret = test_kprobes();
383 if (ret < 0)
384 errors++;
386 num_tests++;
387 ret = test_jprobe();
388 if (ret < 0)
389 errors++;
391 num_tests++;
392 ret = test_jprobes();
393 if (ret < 0)
394 errors++;
396 #ifdef CONFIG_KRETPROBES
397 num_tests++;
398 ret = test_kretprobe();
399 if (ret < 0)
400 errors++;
402 num_tests++;
403 ret = test_kretprobes();
404 if (ret < 0)
405 errors++;
406 #endif /* CONFIG_KRETPROBES */
408 if (errors)
409 pr_err("BUG: %d out of %d tests failed\n", errors, num_tests);
410 else if (handler_errors)
411 pr_err("BUG: %d error(s) running handlers\n", handler_errors);
412 else
413 pr_info("passed successfully\n");
415 return 0;