tests/tcg/x86_64: add a PVH crt.o for x86_64 system tests
[qemu/ar7.git] / tests / tcg / x86_64 / system / boot.S
blob205cfbd3982300a90c0110e91adec58f4db91eff
1 /*
2  * x86_64 boot and support code
3  *
4  * Copyright 2019 Linaro
5  *
6  * This work is licensed under the terms of the GNU GPL, version 3 or later.
7  * See the COPYING file in the top-level directory.
8  *
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.
12  *
13  * SPDX-License-Identifier: GPL-3.0-or-later
14  */
16         .section .head
18 #define ELFNOTE_START(name, type, flags)        \
19 .pushsection .note.name, flags,@note    ;       \
20   .balign 4                             ;       \
21   .long 2f - 1f         /* namesz */    ;       \
22   .long 4484f - 3f      /* descsz */    ;       \
23   .long type                            ;       \
24 1:.asciz #name                          ;       \
25 2:.balign 4                             ;       \
28 #define ELFNOTE_END                             \
29 4484:.balign 4                          ;       \
30 .popsection                             ;
32 #define ELFNOTE(name, type, desc)               \
33         ELFNOTE_START(name, type, "")           \
34                 desc                    ;       \
35         ELFNOTE_END
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_FORM_RAW(x)     x
45 #define __ASM_FORM_COMMA(x) x,
46 #define __ASM_SEL(a,b)           __ASM_FORM(b)
47 #define __ASM_SEL_RAW(a,b)      __ASM_FORM_RAW(b)
48 #define _ASM_PTR        __ASM_SEL(.long, .quad)
50         ELFNOTE(Xen, XEN_ELFNOTE_VIRT_BASE,      _ASM_PTR 0x100000)
51         ELFNOTE(Xen, XEN_ELFNOTE_ENTRY,          _ASM_PTR _start)
52         ELFNOTE(Xen, XEN_ELFNOTE_PHYS32_ENTRY,   _ASM_PTR _start)    /* entry == virtbase */
53         ELFNOTE(Xen, XEN_ELFNOTE_PADDR_OFFSET,   _ASM_PTR 0)
55        /*
56         * Entry point for PVH guests.
57         *
58         * Xen ABI specifies the following register state when we come here:
59         *
60         * - `ebx`: contains the physical memory address where the loader has placed
61         *          the boot start info structure.
62         * - `cr0`: bit 0 (PE) must be set. All the other writeable bits are cleared.
63         * - `cr4`: all bits are cleared.
64         * - `cs `: must be a 32-bit read/execute code segment with a base of ‘0’
65         *          and a limit of ‘0xFFFFFFFF’. The selector value is unspecified.
66         * - `ds`, `es`: must be a 32-bit read/write data segment with a base of
67         *               ‘0’ and a limit of ‘0xFFFFFFFF’. The selector values are all
68         *               unspecified.
69         * - `tr`: must be a 32-bit TSS (active) with a base of '0' and a limit
70         *         of '0x67'.
71         * - `eflags`: bit 17 (VM) must be cleared. Bit 9 (IF) must be cleared.
72         *             Bit 8 (TF) must be cleared. Other bits are all unspecified.
73         *
74         * All other processor registers and flag bits are unspecified. The OS is in
75         * charge of setting up it's own stack, GDT and IDT.
76         */
77         .code32
78         .section .text
80 .global _start
81 _start:
82         cld
83         lgdt gdtr
85         ljmp $0x8,$.Lloadcs
86 .Lloadcs:
87         mov $0x10,%eax
88         mov %eax,%ds
89         mov %eax,%es
90         mov %eax,%fs
91         mov %eax,%gs
92         mov %eax,%ss
94         /* Enable PAE mode (bit 5). */
95         mov %cr4, %eax
96         btsl $5, %eax
97         mov %eax, %cr4
99 #define MSR_EFER                0xc0000080 /* extended feature register */
101         /* Enable Long mode. */
102         mov $MSR_EFER, %ecx
103         rdmsr
104         btsl $8, %eax
105         wrmsr
107         /* Enable paging */
108         mov $.Lpml4, %ecx
109         mov %ecx, %cr3
111         mov %cr0, %eax
112         btsl $31, %eax
113         mov %eax, %cr0
115         /* Jump to 64-bit mode. */
116         lgdt gdtr64
117         ljmp $0x8,$.Lenter64
119         .code64
120         .section .text
121 .Lenter64:
124         // Setup stack ASAP
125         movq $stack_end,%rsp
127         /* don't worry about stack frame, assume everthing is garbage when we return */
128         call main
130         /* output any non-zero result in eax to isa-debug-exit device */
131         test %al, %al
132         jz 1f
133         out %ax, $0xf4
135 1:      /* QEMU ACPI poweroff */
136         mov $0x604,%edx
137         mov $0x2000,%eax
138         out %ax,%dx
139         hlt
140         jmp 1b
142         /*
143          * Helper Functions
144          *
145          * x86_64 calling convention is rdi, rsi, rdx, rcx, r8, r9
146          */
148         /* Output a single character to serial port */
149         .global __sys_outc
150 __sys_outc:
151         pushq %rax
152         mov %rax, %rdx
153         out %al,$0xE9
154         popq %rax
155         ret
157         /* Interrupt Descriptor Table */
159         .section .data
160         .align 16
162 idt_00: .int 0, 0
163 idt_01: .int 0, 0
164 idt_02: .int 0, 0
165 idt_03: .int 0, 0
166 idt_04: .int 0, 0
167 idt_05: .int 0, 0
168 idt_06: .int 0, 0 /* intr_6_opcode, Invalid Opcode */
169 idt_07: .int 0, 0
170 idt_08: .int 0, 0
171 idt_09: .int 0, 0
172 idt_0A: .int 0, 0
173 idt_0B: .int 0, 0
174 idt_0C: .int 0, 0
175 idt_0D: .int 0, 0
176 idt_0E: .int 0, 0
177 idt_0F: .int 0, 0
178 idt_10: .int 0, 0
179 idt_11: .int 0, 0
180 idt_12: .int 0, 0
181 idt_13: .int 0, 0
182 idt_14: .int 0, 0
183 idt_15: .int 0, 0
184 idt_16: .int 0, 0
185 idt_17: .int 0, 0
186 idt_18: .int 0, 0
187 idt_19: .int 0, 0
188 idt_1A: .int 0, 0
189 idt_1B: .int 0, 0
190 idt_1C: .int 0, 0
191 idt_1D: .int 0, 0
192 idt_1E: .int 0, 0
193 idt_1F: .int 0, 0
196         /*
197          * Global Descriptor Table (GDT)
198          *
199          * This describes various memory areas (segments) through
200          * segment descriptors. In 32 bit mode each segment each
201          * segement is associated with segment registers which are
202          * implicitly (or explicitly) referenced depending on the
203          * instruction. However in 64 bit mode selectors are flat and
204          * segmented addressing isn't used.
205          */
206 gdt:
207         .short 0
208 gdtr:
209         .short gdt_en - gdt - 1
210         .int gdt
212         // Code cs:
213         .short 0xFFFF
214         .short 0
215         .byte 0
216         .byte 0x9b
217         .byte 0xCF
218         .byte 0
220         // Data  ds:, ss:, es:, fs:, and gs:
221         .short 0xFFFF
222         .short 0
223         .byte 0
224         .byte 0x93
225         .byte 0xCF
226         .byte 0
227 gdt_en:
229 gdt64:
230         .short 0
231 gdtr64:
232         .short gdt64_en - gdt64 - 1
233         .int gdt64
235         // Code
236         .short 0xFFFF
237         .short 0
238         .byte 0
239         .byte 0x9b
240         .byte 0xAF
241         .byte 0
243         // Data
244         .short 0xFFFF
245         .short 0
246         .byte 0
247         .byte 0x93
248         .byte 0xCF
249         .byte 0
250 gdt64_en:
252         .section .bss
253         .align 16
255 stack: .space 65536
256 stack_end:
258         .section .data
260 .align 4096
261 .Lpd:
262 i = 0
263         .rept 512 * 4
264         .quad 0x1e7 | (i << 21)
265         i = i + 1
266         .endr
268 .align 4096
269 .Lpdp:
270         .quad .Lpd + 7 + 0 * 4096 /* 0-1 GB */
271         .quad .Lpd + 7 + 1 * 4096 /* 1-2 GB */
272         .quad .Lpd + 7 + 2 * 4096 /* 2-3 GB */
273         .quad .Lpd + 7 + 3 * 4096 /* 3-4 GB */
275 .align 4096
276 .Lpml4:
277         .quad .Lpdp + 7 /* 0-512 GB */