blob: 222e84e2432ed13ef1352ee82e9e4462a61e4496 [file] [log] [blame]
Arjan van de Venedeed302008-01-30 13:34:08 +01001/*
2 * test_rodata.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 */
Harvey Harrison7bfeab92008-02-12 12:12:01 -080012#include <asm/cacheflush.h>
Arjan van de Venedeed302008-01-30 13:34:08 +010013#include <asm/sections.h>
H. Peter Anvin5d6f8d72012-04-20 12:19:50 -070014#include <asm/asm.h>
Arjan van de Venedeed302008-01-30 13:34:08 +010015
16int rodata_test(void)
17{
18 unsigned long result;
19 unsigned long start, end;
20
21 /* test 1: read the value */
22 /* If this test fails, some previous testrun has clobbered the state */
23 if (!rodata_test_data) {
24 printk(KERN_ERR "rodata_test: test 1 fails (start data)\n");
25 return -ENODEV;
26 }
27
28 /* test 2: write to the variable; this should fault */
29 /*
30 * If this test fails, we managed to overwrite the data
31 *
32 * This is written in assembly to be able to catch the
33 * exception that is supposed to happen in the correct
34 * case
35 */
36
37 result = 1;
38 asm volatile(
39 "0: mov %[zero],(%[rodata_test])\n"
40 " mov %[zero], %[rslt]\n"
41 "1:\n"
42 ".section .fixup,\"ax\"\n"
43 "2: jmp 1b\n"
44 ".previous\n"
H. Peter Anvin5d6f8d72012-04-20 12:19:50 -070045 _ASM_EXTABLE(0b,2b)
Arjan van de Venedeed302008-01-30 13:34:08 +010046 : [rslt] "=r" (result)
47 : [rodata_test] "r" (&rodata_test_data), [zero] "r" (0UL)
48 );
49
50
51 if (!result) {
52 printk(KERN_ERR "rodata_test: test data was not read only\n");
53 return -ENODEV;
54 }
55
56 /* test 3: check the value hasn't changed */
57 /* If this test fails, we managed to overwrite the data */
58 if (!rodata_test_data) {
Masanari Iidad939be32015-02-27 23:52:31 +090059 printk(KERN_ERR "rodata_test: Test 3 fails (end data)\n");
Arjan van de Venedeed302008-01-30 13:34:08 +010060 return -ENODEV;
61 }
62 /* test 4: check if the rodata section is 4Kb aligned */
63 start = (unsigned long)__start_rodata;
64 end = (unsigned long)__end_rodata;
65 if (start & (PAGE_SIZE - 1)) {
66 printk(KERN_ERR "rodata_test: .rodata is not 4k aligned\n");
67 return -ENODEV;
68 }
69 if (end & (PAGE_SIZE - 1)) {
70 printk(KERN_ERR "rodata_test: .rodata end is not 4k aligned\n");
71 return -ENODEV;
72 }
73
74 return 0;
75}