xref: /kvm-unit-tests/x86/cstart64.S (revision dca3f4c041143c8e8dc70c6890a19a5730310230)
1#include "apic-defs.h"
2#include "desc.h"
3#include "smp.h"
4
5ipi_vector = 0x20
6
7per_cpu_size = PER_CPU_SIZE
8max_cpus = MAX_TEST_CPUS
9
10.bss
11.align 4096
12	. = . + PER_CPU_SIZE * max_cpus
13stacktop:
14
15.data
16
17.align 4096
18ptl2:
19i = 0
20	.rept 512 * 4
21	.quad 0x1e7 | (i << 21)
22	i = i + 1
23	.endr
24
25.align 4096
26ptl3:
27	.quad ptl2 + 7 + 0 * 4096
28	.quad ptl2 + 7 + 1 * 4096
29	.quad ptl2 + 7 + 2 * 4096
30	.quad ptl2 + 7 + 3 * 4096
31
32.align 4096
33ptl4:
34	.quad ptl3 + 7
35
36.align 4096
37ptl5:
38	.quad ptl4 + 7
39
40.align 4096
41
42mb_boot_info:	.quad 0
43
44pt_root:	.quad ptl4
45
46#include "trampolines.S"
47
48.section .init
49.code32
50
51mb_magic = 0x1BADB002
52mb_flags = 0x0
53
54	# multiboot header
55	.long mb_magic, mb_flags, 0 - (mb_magic + mb_flags)
56mb_cmdline = 16
57
58.macro load_tss
59	movq %rsp, %rdi
60	call setup_tss
61	ltr %ax
62.endm
63
64.globl start
65start:
66	mov %ebx, mb_boot_info
67	mov $stacktop, %esp
68	setup_percpu_area
69	call prepare_64
70	jmpl $KERNEL_CS, $start64
71
72switch_to_5level:
73	/* Disable CR4.PCIDE */
74	mov %cr4, %eax
75	btr $17, %eax
76	mov %eax, %cr4
77
78	mov %cr0, %eax
79	btr $31, %eax
80	mov %eax, %cr0
81
82	mov $ptl5, %eax
83	mov %eax, pt_root
84
85	/* Enable CR4.LA57 */
86	mov %cr4, %eax
87	bts $12, %eax
88	mov %eax, %cr4
89
90	mov $KERNEL_DS, %ax
91	mov %ax, %ss
92
93	call enter_long_mode
94	jmpl $KERNEL_CS, $lvl5
95
96smp_stacktop:	.long stacktop - per_cpu_size
97
98.align 16
99
100gdt32:
101	.quad 0
102	.quad 0x00cf9b000000ffff // flat 32-bit code segment
103	.quad 0x00cf93000000ffff // flat 32-bit data segment
104gdt32_end:
105
106.code64
107start64:
108	call load_idt
109	load_tss
110	call reset_apic
111	call mask_pic_interrupts
112	call enable_apic
113	call save_id
114	mov mb_boot_info(%rip), %rbx
115	mov %rbx, %rdi
116	call setup_multiboot
117	call setup_libcflat
118	mov mb_cmdline(%rbx), %eax
119	mov %rax, __args(%rip)
120	call __setup_args
121
122	call bsp_rest_init
123
124	mov __argc(%rip), %edi
125	lea __argv(%rip), %rsi
126	lea __environ(%rip), %rdx
127	call main
128	mov %eax, %edi
129	call exit
130
131.globl setup_5level_page_table
132setup_5level_page_table:
133	/* Check if 5-level paging has already enabled */
134	mov %cr4, %rax
135	test $0x1000, %eax
136	jnz lvl5
137
138	pushq $32
139	pushq $switch_to_5level
140	lretq
141lvl5:
142	retq
143