/** * imr_fixup_memmap - Tear down IMRs used during bootup. * * BIOS and Grub both setup IMRs around compressed kernel, initrd memory * that need to be removed before the kernel hands out one of the IMR * encased addresses to a downstream DMA agent such as the SD or Ethernet. * IMRs on Galileo are setup to immediately reset the system on violation. * As a result if you're running a root filesystem from SD - you'll need * the boot-time IMRs torn down or you'll find seemingly random resets when * using your filesystem. * * @idev: pointer to imr_device structure. * @return: */ static void __init imr_fixup_memmap(struct imr_device *idev) { phys_addr_t base = virt_to_phys(&_text); size_t size = virt_to_phys(&__end_rodata) - base; unsigned long start, end; int i; int ret; /* Tear down all existing unlocked IMRs. */ for (i = 0; i < idev->max_imr; i++) imr_clear(i); start = (unsigned long)_text; end = (unsigned long)__end_rodata - 1; /* * Setup an unlocked IMR around the physical extent of the kernel * from the beginning of the .text secton to the end of the * .rodata section as one physically contiguous block. * * We don't round up @size since it is already PAGE_SIZE aligned. * See vmlinux.lds.S for details. */ ret = imr_add_range(base, size, IMR_CPU, IMR_CPU); if (ret < 0) { pr_err("unable to setup IMR for kernel: %zu KiB (%lx - %lx)\n", size / 1024, start, end); } else { pr_info("protecting kernel .text - .rodata: %zu KiB (%lx - %lx)\n", size / 1024, start, end); } }
/** * imr_self_test * * Verify IMR self_test with some simple tests to verify overlap, * zero sized allocations and 1 KiB sized areas. * */ static void __init imr_self_test(void) { phys_addr_t base = virt_to_phys((void *)ktla_ktva((unsigned long)_text)); size_t size = virt_to_phys(&__end_rodata) - base; const char *fmt_over = "overlapped IMR @ (0x%08lx - 0x%08lx)\n"; int ret; /* Test zero zero. */ ret = imr_add_range(0, 0, 0, 0, false); imr_self_test_result(ret < 0, "zero sized IMR\n"); /* Test exact overlap. */ ret = imr_add_range(base, size, IMR_CPU, IMR_CPU, false); imr_self_test_result(ret < 0, fmt_over, __va(base), __va(base + size)); /* Test overlap with base inside of existing. */ base += size - IMR_ALIGN; ret = imr_add_range(base, size, IMR_CPU, IMR_CPU, false); imr_self_test_result(ret < 0, fmt_over, __va(base), __va(base + size)); /* Test overlap with end inside of existing. */ base -= size + IMR_ALIGN * 2; ret = imr_add_range(base, size, IMR_CPU, IMR_CPU, false); imr_self_test_result(ret < 0, fmt_over, __va(base), __va(base + size)); /* Test that a 1 KiB IMR @ zero with read/write all will bomb out. */ ret = imr_add_range(0, IMR_ALIGN, IMR_READ_ACCESS_ALL, IMR_WRITE_ACCESS_ALL, false); imr_self_test_result(ret < 0, "1KiB IMR @ 0x00000000 - access-all\n"); /* Test that a 1 KiB IMR @ zero with CPU only will work. */ ret = imr_add_range(0, IMR_ALIGN, IMR_CPU, IMR_CPU, false); imr_self_test_result(ret >= 0, "1KiB IMR @ 0x00000000 - cpu-access\n"); if (ret >= 0) { ret = imr_remove_range(0, IMR_ALIGN); imr_self_test_result(ret == 0, "teardown - cpu-access\n"); } /* Test 2 KiB works. */ size = IMR_ALIGN * 2; ret = imr_add_range(0, size, IMR_READ_ACCESS_ALL, IMR_WRITE_ACCESS_ALL, false); imr_self_test_result(ret >= 0, "2KiB IMR @ 0x00000000\n"); if (ret >= 0) { ret = imr_remove_range(0, size); imr_self_test_result(ret == 0, "teardown 2KiB\n"); } }