2 * x86_64 boot and support code
4 * Copyright 2019, 2024 Linaro
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
9 * Unlike the i386 version we instead use Xen's PVHVM booting header
10 * which should drop us automatically into 32 bit mode ready to go. I've
11 * nabbed bits of the Linux kernel setup to achieve this.
13 * SPDX-License-Identifier: GPL-2.0-or-later
18 #define ELFNOTE_START(name, type, flags) \
19 .pushsection .note.name, flags,@note ; \
21 .long 2f - 1f /* namesz */ ; \
22 .long 4484f - 3f /* descsz */ ; \
32 #define ELFNOTE(name, type, desc) \
33 ELFNOTE_START(name, type, "") \
37 #define XEN_ELFNOTE_ENTRY 1
38 #define XEN_ELFNOTE_HYPERCALL_PAGE 2
39 #define XEN_ELFNOTE_VIRT_BASE 3
40 #define XEN_ELFNOTE_PADDR_OFFSET 4
41 #define XEN_ELFNOTE_PHYS32_ENTRY 18
43 #define __ASM_FORM(x) x
44 #define __ASM_SEL(a,b) __ASM_FORM(b)
45 #define _ASM_PTR __ASM_SEL(.long, .quad)
47 ELFNOTE(Xen, XEN_ELFNOTE_VIRT_BASE, _ASM_PTR 0x100000)
48 ELFNOTE(Xen, XEN_ELFNOTE_ENTRY, _ASM_PTR _start)
49 ELFNOTE(Xen, XEN_ELFNOTE_PHYS32_ENTRY, _ASM_PTR _start) /* entry == virtbase */
50 ELFNOTE(Xen, XEN_ELFNOTE_PADDR_OFFSET, _ASM_PTR 0)
53 * Entry point for PVH guests.
55 * Xen ABI specifies the following register state when we come here:
57 * - `ebx`: contains the physical memory address where the loader has placed
58 * the boot start info structure.
59 * - `cr0`: bit 0 (PE) must be set. All the other writable bits are cleared.
60 * - `cr4`: all bits are cleared.
61 * - `cs `: must be a 32-bit read/execute code segment with a base of ‘0’
62 * and a limit of ‘0xFFFFFFFF’. The selector value is unspecified.
63 * - `ds`, `es`: must be a 32-bit read/write data segment with a base of
64 * ‘0’ and a limit of ‘0xFFFFFFFF’. The selector values are all
66 * - `tr`: must be a 32-bit TSS (active) with a base of '0' and a limit
68 * - `eflags`: bit 17 (VM) must be cleared. Bit 9 (IF) must be cleared.
69 * Bit 8 (TF) must be cleared. Other bits are all unspecified.
71 * All other processor registers and flag bits are unspecified. The OS is in
72 * charge of setting up it's own stack, GDT and IDT.
91 /* Enable PAE mode (bit 5). */
96 #define MSR_EFER 0xc0000080 /* extended feature register */
98 /* Enable Long mode. */
112 /* Jump to 64-bit mode. */
124 /* don't worry about stack frame, assume everything is garbage when we return */
127 _exit: /* output any non-zero result in eax to isa-debug-exit device */
132 1: /* QEMU ACPI poweroff */
142 * x86_64 calling convention is rdi, rsi, rdx, rcx, r8, r9
145 /* Output a single character to serial port */
154 /* Interrupt Descriptor Table */
165 idt_06: .int 0, 0 /* intr_6_opcode, Invalid Opcode */
194 * Global Descriptor Table (GDT)
196 * This describes various memory areas (segments) through
197 * segment descriptors. In 32 bit mode each segment each
198 * segment is associated with segment registers which are
199 * implicitly (or explicitly) referenced depending on the
200 * instruction. However in 64 bit mode selectors are flat and
201 * segmented addressing isn't used.
206 .short gdt_en - gdt - 1
217 // Data ds:, ss:, es:, fs:, and gs:
229 .short gdt64_en - gdt64 - 1
261 .quad 0x1e7 | (i << 21)
267 .quad .Lpd + 7 + 0 * 4096 /* 0-1 GB */
268 .quad .Lpd + 7 + 1 * 4096 /* 1-2 GB */
269 .quad .Lpd + 7 + 2 * 4096 /* 2-3 GB */
270 .quad .Lpd + 7 + 3 * 4096 /* 3-4 GB */
274 .quad .Lpdp + 7 /* 0-512 GB */