xref: /openbmc/linux/mm/rodata_test.c (revision f7c35abe)
1 /*
2  * rodata_test.c: functional test for mark_rodata_ro function
3  *
4  * (C) Copyright 2008 Intel Corporation
5  * Author: Arjan van de Ven <arjan@linux.intel.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; version 2
10  * of the License.
11  */
12 #include <linux/uaccess.h>
13 #include <asm/sections.h>
14 
15 const int rodata_test_data = 0xC3;
16 EXPORT_SYMBOL_GPL(rodata_test_data);
17 
18 void rodata_test(void)
19 {
20 	unsigned long start, end;
21 	int zero = 0;
22 
23 	/* test 1: read the value */
24 	/* If this test fails, some previous testrun has clobbered the state */
25 	if (!rodata_test_data) {
26 		pr_err("rodata_test: test 1 fails (start data)\n");
27 		return;
28 	}
29 
30 	/* test 2: write to the variable; this should fault */
31 	if (!probe_kernel_write((void *)&rodata_test_data,
32 						(void *)&zero, sizeof(zero))) {
33 		pr_err("rodata_test: test data was not read only\n");
34 		return;
35 	}
36 
37 	/* test 3: check the value hasn't changed */
38 	if (rodata_test_data == zero) {
39 		pr_err("rodata_test: test data was changed\n");
40 		return;
41 	}
42 
43 	/* test 4: check if the rodata section is PAGE_SIZE aligned */
44 	start = (unsigned long)__start_rodata;
45 	end = (unsigned long)__end_rodata;
46 	if (start & (PAGE_SIZE - 1)) {
47 		pr_err("rodata_test: start of .rodata is not page size aligned\n");
48 		return;
49 	}
50 	if (end & (PAGE_SIZE - 1)) {
51 		pr_err("rodata_test: end of .rodata is not page size aligned\n");
52 		return;
53 	}
54 
55 	pr_info("rodata_test: all tests were successful\n");
56 }
57