Files
linux-kernel-module-cheat/lkmc.c
Ciro Santilli 六四事件 法轮功 c8c4f89854 asm: make use regular asserts that will just work on baremetal
Previously had wonky line pointer in asm_main. New interface simpler and more portable.

Add tests for ASSERT_EQ_ and family in arm and aarch64, previously on x86_64.

ASSERT_EQ_ and family in ARM can now either take =123, =addr or var, before this
the = was added on macros so var was not possible.

Define the main function directly in assembly, the C driver was useless.
2019-05-23 00:00:00 +00:00

79 lines
1.9 KiB
C

/* https://github.com/cirosantilli/linux-kernel-module-cheat#lkmc-c */
#include <math.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <lkmc.h>
#define LKMC_ASSERT_EQ_DEFINE(bits) \
LKMC_ASSERT_EQ_DECLARE(bits) \
{ \
if (val1 != val2) { \
printf("%s failed\n", __func__); \
printf("val1 0x%" PRIX ## bits "\n", val1); \
printf("val2 0x%" PRIX ## bits "\n", val2); \
lkmc_assert_fail(line); \
} \
}
LKMC_ASSERT_EQ_DEFINE(32)
LKMC_ASSERT_EQ_DEFINE(64)
#undef ASSERT_EQ_DEFINE
void lkmc_assert_fail(uint32_t line) {
printf("error: assertion failed at line: %" PRIu32 "\n", line);
exit(1);
}
void lkmc_assert_memcmp(
const void *s1,
const void *s2,
size_t n,
uint32_t line
) {
size_t i;
uint8_t *s1b, *s2b;
uint8_t b1, b2;
s1b = (uint8_t *)s1;
s2b = (uint8_t *)s2;
for (i = 0; i < n; ++i) {
b1 = s1b[i];
b2 = s2b[i];
if (b1 != b2) {
printf(
"%s failed: "
"byte1, byte2, index: "
"0x%02" PRIX8 " 0x%02" PRIX8 " 0x%zX\n",
__func__,
b1,
b2,
i
);
lkmc_assert_fail(line);
}
}
}
void lkmc_baremetal_on_exit_callback(int status, void *arg) {
(void)arg;
if (status != 0) {
printf("lkmc_exit_status_%d\n", status);
}
}
#if defined(__aarch64__)
#define LKMC_SYSREG_READ_WRITE(type, name) \
type LKMC_CONCAT(LKMC_CONCAT(LKMC_SYSREG_SYMBOL_PREFIX, name), _read(void)) { \
type name; \
__asm__ __volatile__("mrs %0, " #name : "=r" (name) : : ); \
return name; \
} \
void LKMC_CONCAT(LKMC_CONCAT(LKMC_SYSREG_SYMBOL_PREFIX, name), _write(type name)) { \
__asm__ __volatile__("msr " #name ", %0" : : "r" (name) : ); \
}
LKMC_SYSREG_OPS
#undef LKMC_SYSREG_READ_WRITE
#endif