xref: /kvm-unit-tests/x86/cstart64.S (revision 588887078688358e111e4582ccc23e548f7ad1a6)
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	call setup_idt
110	load_tss
111	call reset_apic
112	call mask_pic_interrupts
113	call enable_apic
114	call save_id
115	mov mb_boot_info(%rip), %rbx
116	mov %rbx, %rdi
117	call setup_multiboot
118	call setup_libcflat
119	mov mb_cmdline(%rbx), %eax
120	mov %rax, __args(%rip)
121	call __setup_args
122
123	call bsp_rest_init
124
125	mov __argc(%rip), %edi
126	lea __argv(%rip), %rsi
127	lea __environ(%rip), %rdx
128	call main
129	mov %eax, %edi
130	call exit
131
132.globl setup_5level_page_table
133setup_5level_page_table:
134	/* Check if 5-level paging has already enabled */
135	mov %cr4, %rax
136	test $0x1000, %eax
137	jnz lvl5
138
139	pushq $32
140	pushq $switch_to_5level
141	lretq
142lvl5:
143	retq
144