| 1 |
/* -*- linux-c -*- |
| 2 |
* Systemtap Memory1 Test Module |
| 3 |
* Copyright (C) 2013 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/module.h> |
| 13 |
#include <linux/proc_fs.h> |
| 14 |
#include <linux/compiler.h> |
| 15 |
#include <linux/sched.h> |
| 16 |
#include <linux/uaccess.h> |
| 17 |
#include <linux/version.h> |
| 18 |
|
| 19 |
/* The purpose of this module is to provide a kernel string we can |
| 20 |
* safely read. We use a /proc file to trigger the function call from |
| 21 |
* user context. Then systemtap scripts set probes on the functions |
| 22 |
* and run tests to see if the expected output is received. This is |
| 23 |
* better than using the kernel because kernel internals frequently |
| 24 |
* change. */ |
| 25 |
|
| 26 |
static struct proc_dir_entry *stm_ctl = NULL; |
| 27 |
static char buffer[100] = ""; |
| 28 |
|
| 29 |
void noinline stp_memory1_set_str(const char *string, size_t bytes); |
| 30 |
void noinline stp_memory1_set_str(const char *string, size_t bytes) |
| 31 |
{ |
| 32 |
asm (""); |
| 33 |
memcpy(buffer, string, bytes); |
| 34 |
} |
| 35 |
|
| 36 |
char * noinline stp_memory1_get_str(void); |
| 37 |
char * noinline stp_memory1_get_str(void) |
| 38 |
{ |
| 39 |
asm (""); |
| 40 |
return buffer; |
| 41 |
} |
| 42 |
|
| 43 |
EXPORT_SYMBOL(stp_memory1_set_str); |
| 44 |
EXPORT_SYMBOL(stp_memory1_get_str); |
| 45 |
|
| 46 |
static ssize_t stm_write (struct file *file, const char __user *buf, |
| 47 |
size_t count, loff_t *ppos) |
| 48 |
{ |
| 49 |
char tmp[100]; |
| 50 |
size_t bytes; |
| 51 |
|
| 52 |
bytes = (count >= sizeof(tmp) - 1) ? sizeof(tmp) - 1 : count; |
| 53 |
|
| 54 |
if (copy_from_user(tmp, buf, bytes)) |
| 55 |
return -EFAULT; |
| 56 |
|
| 57 |
tmp[bytes] = '\0'; |
| 58 |
stp_memory1_set_str(tmp, bytes); |
| 59 |
return bytes; |
| 60 |
} |
| 61 |
|
| 62 |
static ssize_t stm_read(struct file *file, char __user *buffer, |
| 63 |
size_t buflen, loff_t *fpos) |
| 64 |
{ |
| 65 |
char *data = stp_memory1_get_str(); |
| 66 |
size_t bytes = strlen(data); |
| 67 |
|
| 68 |
if (buflen == 0 || *fpos >= bytes) |
| 69 |
return 0; |
| 70 |
|
| 71 |
bytes = min(bytes - (size_t)*fpos, buflen); |
| 72 |
if (copy_to_user(buffer, data + *fpos, bytes)) |
| 73 |
return -EFAULT; |
| 74 |
*fpos += bytes; |
| 75 |
return bytes; |
| 76 |
} |
| 77 |
|
| 78 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,6,0) |
| 79 |
static struct proc_ops stm_fops_cmd = { |
| 80 |
.proc_write = stm_write, |
| 81 |
.proc_read = stm_read, |
| 82 |
}; |
| 83 |
#else |
| 84 |
static struct file_operations stm_fops_cmd = { |
| 85 |
.owner = THIS_MODULE, |
| 86 |
.write = stm_write, |
| 87 |
.read = stm_read, |
| 88 |
}; |
| 89 |
#endif |
| 90 |
|
| 91 |
|
| 92 |
int init_module(void) |
| 93 |
{ |
| 94 |
stm_ctl = proc_create ("stap_memory1_test", 0666, NULL, &stm_fops_cmd); |
| 95 |
if (stm_ctl == NULL) |
| 96 |
return -1; |
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
void cleanup_module(void) |
| 101 |
{ |
| 102 |
if (stm_ctl) |
| 103 |
remove_proc_entry ("stap_memory1_test", NULL); |
| 104 |
} |
| 105 |
|
| 106 |
MODULE_DESCRIPTION("systemtap memory1 test module"); |
| 107 |
MODULE_LICENSE("GPL"); |