File: test_kexec_jump.c

package info (click to toggle)
linux 6.17.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,734,892 kB
  • sloc: ansic: 26,684,085; asm: 271,195; sh: 147,401; python: 75,980; makefile: 57,306; perl: 36,943; xml: 19,562; cpp: 5,899; yacc: 4,909; lex: 2,943; awk: 1,556; sed: 29; ruby: 25
file content (72 lines) | stat: -rw-r--r-- 1,475 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#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;
}