^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * rodata_test.c: functional test for mark_rodata_ro function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * (C) Copyright 2008 Intel Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Arjan van de Ven <arjan@linux.intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #define pr_fmt(fmt) "rodata_test: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/rodata_test.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <asm/sections.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) static const int rodata_test_data = 0xC3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) void rodata_test(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) unsigned long start, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) int zero = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /* test 1: read the value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) /* If this test fails, some previous testrun has clobbered the state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) if (!rodata_test_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) pr_err("test 1 fails (start data)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) /* test 2: write to the variable; this should fault */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (!copy_to_kernel_nofault((void *)&rodata_test_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) (void *)&zero, sizeof(zero))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) pr_err("test data was not read only\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) /* test 3: check the value hasn't changed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (rodata_test_data == zero) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) pr_err("test data was changed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /* test 4: check if the rodata section is PAGE_SIZE aligned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) start = (unsigned long)__start_rodata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) end = (unsigned long)__end_rodata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (start & (PAGE_SIZE - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) pr_err("start of .rodata is not page size aligned\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (end & (PAGE_SIZE - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) pr_err("end of .rodata is not page size aligned\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) pr_info("all tests were successful\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }