interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / x86 / swap_tramp.S

100 lines · 3898 bytes Raw Blame History
1/* swap_tramp.S -- AIENOS live kernel swap trampoline (position-independent).
2 *
3 * This code is copied to tramp_pa + 512 before the swap and executed there.
4 * It copies the new kernel's PE sections from the staging area to their
5 * linked virtual addresses (= physical, identity-mapped), then calls the new
6 * kernel's entry point.
7 *
8 * At entry (called with a single argument, Microsoft x64 ABI):
9 * rcx = tramp_pa (physical address of the tramp page; the kernel is built -mabi=ms, so
10 * the first argument of fn(tramp_pa) in swap_x86.c arrives in RCX)
11 *
12 * Tramp page layout (must match swap_x86.c constants):
13 * [0]: section table, 8 × 32 bytes
14 * each: {src_pa u64, dst_pa u64, len u64, flags u32, _pad u32}
15 * flags: 0=copy, 1=zero (BSS); sentinel: src_pa==0 && flags==0xFF
16 * [256]: entry_point u64 (the new image's PE entry, _start)
17 * [264]: stack top u64
18 * [272]: swap entry marker u64 (CK_X86_SWAP_ENTRY_MAGIC)
19 * [280]: struct ck_handoff copy (232 bytes)
20 * [512]: THIS CODE (position-independent)
21 *
22 * All state is taken from the tramp page; no external symbols are referenced
23 * after the copy overwrites the old kernel image. */
24
25#define TRAMP_SECT_OFF 0 /* section table */
26#define TRAMP_ENTRY_OFF 256 /* entry_point u64 */
27#define TRAMP_HAND_OFF 280 /* handoff copy */
28#define TRAMP_STACK_OFF 264 /* u64: top of the stack the new kernel starts on (swap_x86.c) */
29#define TRAMP_MAGIC_OFF 272 /* u64: CK_X86_SWAP_ENTRY_MAGIC (x86.h), the RCX the new _start sees */
30#define SECT_SZ 32 /* bytes per section table entry */
31
32 .text
33 .global ck_x86_swap_tramp
34 .global ck_x86_swap_tramp_end
35 .type ck_x86_swap_tramp, @function
36
37ck_x86_swap_tramp:
38 /* rcx = tramp_pa (Microsoft x64 first argument) */
39 movq %rcx, %r12 /* r12 = tramp_pa; our data base */
40 movq TRAMP_STACK_OFF(%r12), %rsp /* dedicated stack: the old kernel's stack may lie inside the new image */
41 cld
42 xorl %r13d, %r13d /* section index */
43
44.sect_loop:
45 cmpq $8, %r13
46 jge .copy_done
47 /* Compute address of section table entry: r12 + r13 * SECT_SZ */
48 movq %r13, %rcx
49 imulq $SECT_SZ, %rcx
50 leaq TRAMP_SECT_OFF(%r12,%rcx,1), %rbx
51 /* Check sentinel: flags byte (offset 24 in entry) == 0xFF */
52 movl 24(%rbx), %eax
53 cmpb $0xff, %al
54 je .copy_done
55 /* Also stop on src_pa == 0 && flags == 0 (end-of-table zero marker) */
56 movq 0(%rbx), %rsi /* src_pa */
57 movl 24(%rbx), %eax /* flags */
58 testq %rsi, %rsi
59 jnz .have_src
60 testl %eax, %eax
61 jz .copy_done /* src_pa=0, flags=0 => end */
62.have_src:
63 movq 8(%rbx), %rdi /* dst_pa */
64 movq 16(%rbx), %rcx /* len */
65 testl $1, %eax
66 jnz .do_zero
67.do_copy:
68 rep movsb
69 jmp .sect_next
70.do_zero:
71 xorl %eax, %eax
72 rep stosb
73.sect_next:
74 incq %r13
75 jmp .sect_loop
76
77.copy_done:
78 /* Flush caches so the copies are coherent before jumping. */
79 wbinvd
80 /* Call the new image's PE entry (_start, efi_entry_x86_64.S) the way UEFI would, Microsoft
81 * x64 convention (the kernel is built -mabi=ms): RCX = the swap marker in place of the
82 * ImageHandle, RDX = &handoff in place of the SystemTable, 32 bytes of shadow space. */
83 movq TRAMP_MAGIC_OFF(%r12), %rcx
84 leaq TRAMP_HAND_OFF(%r12), %rdx
85 movq TRAMP_ENTRY_OFF(%r12), %rax /* rax = entry_point */
86 subq $32, %rsp
87 callq *%rax
88 /* Should never return; halt if it does. */
89.halt:
90 cli
91 hlt
92 jmp .halt
93#if defined(CK_X86_SWAP_TRAMP_PAD)
94 /* QEMU lab test only (Makefile CK_X86_SWAP_TRAMP_PAD=1): a trampoline larger than its page,
95 * which ck_x86_swap_step must refuse before it stops a core or a device. */
96 .skip 4096, 0xcc
97#endif
98
99ck_x86_swap_tramp_end:
100 .size ck_x86_swap_tramp, . - ck_x86_swap_tramp