Debian Codegraph @

doc/SystemTap_Beginners_Guide/testsuite/systemtap.base/stap_hwbkpt_kmod.c

raw
1 /* -*- linux-c -*-
2 * Systemtap Test Module
3 * Copyright (C) 2017 Red Hat Inc.
4 *
5 * This file is part of systemtap, and is free software. You can
6 * redistribute it and/or modify it under the terms of the GNU General
7 * Public License (GPL); either version 2, or (at your option) any
8 * later version.
9 */
10
11 #include <linux/kernel.h>
12 #include <linux/version.h>
13 #include <linux/module.h>
14 #include <linux/proc_fs.h>
15 #include <linux/compiler.h>
16 #include <linux/sched.h>
17 #include <linux/uaccess.h>
18
19 /*
20 * The purpose of this module is to provide a memory location that
21 * will be modified from user context via a /proc file. Systemtap
22 * scripts set kernel.data probes on the memory location and run tests
23 * to see if the expected output is received. This is better than
24 * using the kernel's own memory location, since we can't determine
25 * when they will get read/written.
26 */
27
28 /************ Below are the functions to create this module ************/
29
30 static struct proc_dir_entry *stm_ctl = NULL;
31
32 // The memory location to probe/watch.
33 int stap_hwbkpt_data = -1;
34
35 static ssize_t stm_write_cmd (struct file *file, const char __user *buf,
36 size_t count, loff_t *ppos)
37 {
38 char type;
39
40 if (get_user(type, (char __user *)buf))
41 return -EFAULT;
42
43 switch (type) {
44 case '0':
45 case '1':
46 case '2':
47 stap_hwbkpt_data = __INT_MAX__;
48 break;
49 default:
50 printk ("stap_hwbkpt_kmod: invalid command type %d\n",
51 (int)type);
52 return -EINVAL;
53 }
54
55 return count;
56 }
57
58 static ssize_t stm_read_cmd(struct file *file, char __user *buffer,
59 size_t buflen, loff_t *fpos)
60 {
61 size_t bytes = sizeof(stap_hwbkpt_data);
62
63 if (buflen == 0 || *fpos >= bytes)
64 return 0;
65
66 bytes = min(bytes - (size_t)*fpos, buflen);
67 if (copy_to_user(buffer, &stap_hwbkpt_data + *fpos, bytes))
68 return -EFAULT;
69 *fpos += bytes;
70 return bytes;
71 }
72
73 #if LINUX_VERSION_CODE <= KERNEL_VERSION(5,6,0)
74 static struct file_operations stm_fops_cmd = {
75 .owner = THIS_MODULE,
76 .write = stm_write_cmd,
77 .read = stm_read_cmd,
78 };
79 #else
80 static struct proc_ops stm_fops_cmd = {
81 .proc_write = stm_write_cmd,
82 .proc_read = stm_read_cmd,
83 };
84 #endif
85
86 #define CMD_FILE "stap_hwbkpt_cmd"
87
88 int hwbkpt_init_module(void);
89 int hwbkpt_init_module(void)
90 {
91 stm_ctl = proc_create (CMD_FILE, 0666, NULL, &stm_fops_cmd);
92 if (stm_ctl == NULL)
93 return -1;
94 return 0;
95 }
96 module_init(hwbkpt_init_module);
97
98 void hwbkpt_cleanup_module(void);
99 void hwbkpt_cleanup_module(void)
100 {
101 if (stm_ctl)
102 remove_proc_entry (CMD_FILE, NULL);
103 }
104
105 module_exit(hwbkpt_cleanup_module);
106
107 MODULE_DESCRIPTION("systemtap test module");
108 MODULE_LICENSE("GPL");