optimize the menuconfig redrawing
[lunaix-os.git] / lunaix-os / arch / x86 / boot / x86_64 / boot64.S
1 #define __ASM__
2
3 #if defined(CONFIG_X86_BL_MB) || defined(CONFIG_X86_BL_MB2)
4 #include "sys/boot/multiboot.S.inc"
5 #endif
6
7 #include "sys/mm/mempart64.h"
8
9 .section .boot.bss
10     .align 8
11     __tmp_gdt:
12         .long 0x0
13         .long 0x0
14         .long 0x0000ffff
15         .long 0x00ef9a00
16         .long 0x0000ffff
17         .long 0x00cf9200
18
19 .section .boot.bss
20
21     .align 16
22         .skip 512, 0
23     __boot_stack_top:
24
25 .section .boot.bss
26
27     .align 4096
28     _tmp_l0:
29         .skip 4096
30     _tmp_l1:
31         .skip 4096
32
33 .section .boot.text
34     .global start_
35     .type start_, @function
36
37     start_: 
38         .code32
39         cld
40         cli
41
42         movl $__boot_stack_top, %esp
43         pushl $0
44         pushl %ebx
45
46         # first, setup a simple initial page table
47         # to enable transition to IA32e
48
49         # L0 linkage to L1, RWX
50         movl $0x3, %eax
51         movl $_tmp_l1, %ebx
52         orl  %eax, %ebx
53         movl %ebx, _tmp_l0
54         xorl %ebx, %ebx
55         movl %ebx, _tmp_l0 + 4
56         
57         # Entry 0
58         orl  $(1 << 7), %eax
59         movl %eax, _tmp_l1
60         movl %ebx, _tmp_l1 + 4
61
62         # Entry 1
63         addl $0x40000000, %eax
64         movl %eax, _tmp_l1 + 8
65         movl %ebx, _tmp_l1 + 12
66
67         # Entry 2
68         addl $0x40000000, %eax
69         movl %eax, _tmp_l1 + 16
70         movl %ebx, _tmp_l1 + 20
71
72         # Entry 3
73         addl $0x40000000, %eax
74         movl %eax, _tmp_l1 + 24
75         movl %ebx, _tmp_l1 + 28
76
77         movl $_tmp_l0, %eax
78         movl %eax, %cr3
79
80         # now, commencing transition
81
82         movl %cr4, %eax
83         orl  $(1 << 5), %eax    # PAE
84         movl %eax, %cr4
85
86         movl $0xc0000080, %ecx
87         rdmsr
88         orl $(1 << 8), %eax     # IA32_EFER.LME
89         orl $(1 << 11), %eax    # IA32_EFER.NXE 
90         wrmsr
91
92         movl %cr0, %eax
93         orl $(1 << 31), %eax    # PG
94         movl %eax, %cr0
95
96         jmp _ia32e_compat
97
98         # clear the pipeline, 
99         # although cpu might already cleared for us upon mode switching
100         .nop
101         .nop
102         .nop
103         .nop
104
105         # x86_64 compatibility mode
106         # load a temporary gdt for getting into long mode
107     _ia32e_compat:
108         .code32
109         subl $16, %esp
110         movl $__tmp_gdt, 2(%esp)
111         movw $23, (%esp)
112         lgdtl (%esp)
113
114         addl $16, %esp
115         
116         # do a far jump to switch cs
117         pushl $0x08
118         pushl $_ia32e
119         retf
120     
121     _ia32e:
122         .code64
123         movw $0x10, %cx
124         movw %cx, %ds
125         movw %cx, %ss
126
127         popq %rbx
128         movq %rbx, %rdi
129         call x86_init
130
131         movabsq $hhk_entry_, %rax
132         pushq %rax 
133         retq