linux/tools/testing/selftests/kexec/test_kexec_jump.c
David Woodhouse 7615b94b63 selftests/kexec: Add x86_64 selftest for kexec-jump and exception handling
Add a self test which exercises both the kexec-jump facility, and the
kexec exception handling.

Invoke a trivial payload which just does an int3 and returns,
flip-flopping its entry point for the next invocation between two
implementations of the same thing.

Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Cc: Brian Gerst <brgerst@gmail.com>
Cc: Juergen Gross <jgross@suse.com>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Ard Biesheuvel <ardb@kernel.org>
Link: https://lore.kernel.org/r/20250326142404.256980-5-dwmw2@infradead.org
2025-04-10 12:17:14 +02:00

72 lines
1.4 KiB
C

#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <linux/kexec.h>
#include <linux/reboot.h>
#include <sys/reboot.h>
#include <sys/syscall.h>
asm(
" .code64\n"
" .data\n"
"purgatory_start:\n"
// Trigger kexec debug exception handling
" int3\n"
// Set load address for next time
" leaq purgatory_start_b(%rip), %r11\n"
" movq %r11, 8(%rsp)\n"
// Back to Linux
" ret\n"
// Same again
"purgatory_start_b:\n"
// Trigger kexec debug exception handling
" int3\n"
// Set load address for next time
" leaq purgatory_start(%rip), %r11\n"
" movq %r11, 8(%rsp)\n"
// Back to Linux
" ret\n"
"purgatory_end:\n"
".previous"
);
extern char purgatory_start[], purgatory_end[];
int main (void)
{
struct kexec_segment segment = {};
int ret;
segment.buf = purgatory_start;
segment.bufsz = purgatory_end - purgatory_start;
segment.mem = (void *)0x400000;
segment.memsz = 0x1000;
ret = syscall(__NR_kexec_load, 0x400000, 1, &segment, KEXEC_PRESERVE_CONTEXT);
if (ret) {
perror("kexec_load");
exit(1);
}
ret = syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_KEXEC);
if (ret) {
perror("kexec reboot");
exit(1);
}
ret = syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_KEXEC);
if (ret) {
perror("kexec reboot");
exit(1);
}
printf("Success\n");
return 0;
}