* remove "\r" nonsense
[mascara-docs.git] / amd64 / pure64-0.5.0 / src / init_cpu.asm
blob2190aab2075a0a318d29cb77286d9fe96ee78185
1 ; =============================================================================
2 ; Pure64 -- a 64-bit OS loader written in Assembly for x86-64 systems
3 ; Copyright (C) 2008-2012 Return Infinity -- see LICENSE.TXT
5 ; INIT CPU
6 ; =============================================================================
9 init_cpu:
11 ; Check for Prefetcher and L2 Cache support
12 ; mov r15b, 1 ; Set MSR support to 1
13 ; xor eax, eax
14 ; mov al, 1 ; Access CPUID leaf 1
15 ; cpuid
16 ; shr rax, 8 ; Family now in AL (lowest 4 bits)
17 ; and al, 0x0F ; Clear the high 4 bits
18 ; cmp al, 0x0F
19 ; jne init_cpu_msrok ; If Family is not 0xF then jump
20 ; mov r15b, 0 ; If it is 0xF (Older P4/Xeon) then set MSR support to 0
21 ;init_cpu_msrok:
23 ; Disable Cache
24 mov rax, cr0
25 btr rax, 29 ; Clear No Write Thru (Bit 29)
26 bts rax, 30 ; Set Cache Disable (Bit 30)
27 mov cr0, rax
29 ; Flush Cache
30 wbinvd
32 ; Diable Paging Global Extensions
33 mov rax, cr4
34 btr rax, 7 ; Clear Paging Global Extensions (Bit 7)
35 mov cr4, rax
36 mov rax, cr3
37 mov cr3, rax
39 ; Skip next portion if MSR support doesn't exist
40 ; cmp r15b, 0
41 ; je init_cpu_skip1
43 ; Disable Prefetchers (Not supported on P4 or Atom)
44 ; mov ecx, 0x000001A0
45 ; rdmsr
46 ; or eax, 0x00080200 ; Set Hardware Prefetcher Disable (Bit 9) and Adjacent Cache Line Prefetch Disable (Bit 19)
47 ; or edx, 0x000000A0 ; Set DCU Prefetcher Disable (Bit 37) and IP Prefetcher Disable (Bit 39)
48 ; wrmsr
50 ; Disable L2 Cache (Not supported on P4 or Atom)
51 ; mov ecx, 0x0000011E ; Control register 3: used to configure the L2 Cache
52 ; rdmsr
53 ; btr eax, 8 ; Clear L2 Enabled (Bit 8)
54 ; wrmsr
56 ;init_cpu_skip1:
58 ; Disable MTRRs and Configure default memory type to UC
59 mov ecx, 0x000002FF
60 rdmsr
61 and eax, 0xFFFFF300 ; Clear MTRR Enable (Bit 11), Fixed Range MTRR Enable (Bit 10), and Default Memory Type (Bits 7:0) to UC (0x00)
62 wrmsr
64 ; Setup variable-size address ranges
65 ; Cache 0-64 MiB as type 6 (WB) cache
66 ; See example in Intel Volume 3A. Example Base and Mask Calculations
67 ; mov ecx, 0x00000200 ; MTRR_Phys_Base_MSR(0)
68 ; mov edx, 0x00000000 ; Base is EDX:EAX, 0x0000000000000006
69 ; mov eax, 0x00000006 ; Type 6 (write-back cache)
70 ; wrmsr
71 ; mov ecx, 0x00000201 ; MTRR_Phys_Mask_MSR(0)
72 ;; mov edx, 0x00000000 ; Mask is EDX:EAX, 0x0000000001000800 (Because bochs sucks)
73 ;; mov eax, 0x01000800 ; Bit 11 set for Valid
74 ; mov edx, 0x0000000F ; Mask is EDX:EAX, 0x0000000F80000800 (2 GiB)
75 ; mov eax, 0x80000800 ; Bit 11 set for Valid
76 ; wrmsr
78 ; MTRR notes:
79 ; Base 0x0000000000000000 = 0 MiB
80 ; Base 0x0000000080000000 = 2048 MiB, 2048 is 0x800
81 ; Base 0x0000000100000000 = 4096 MiB, 4096 is 0x1000
82 ; Mask 0x0000000F80000000 = 2048 MiB, 0xFFFFFFFFF - F80000000 = 7FFFFFFF = 2147483647 (~2 GiB)
83 ; Mask 0x0000000FC0000000 = 1024 MiB, 0xFFFFFFFFF - FC0000000 = 3FFFFFFF = 1073741823 (~1 GiB)
84 ; Mask 0x0000000FFC000000 = 64 MiB, 0xFFFFFFFFF - FFC000000 = 3FFFFFF = 67108863 (~64 MiB)
86 ; Enable MTRRs
87 mov ecx, 0x000002FF
88 rdmsr
89 bts eax, 11 ; Set MTRR Enable (Bit 11), Only enables Variable Range MTRR's
90 wrmsr
92 ; Flush Cache
93 wbinvd
95 ; Skip next portion if MSR support doesn't exist
96 ; cmp r15b, 0
97 ; je init_cpu_skip2
99 ; Enable L2 Cache (Not supported on P4 or Atom)
100 ; mov ecx, 0x0000011E ; Control register 3: used to configure the L2 Cache
101 ; rdmsr
102 ; bts eax, 8 ; Set L2 Enabled (Bit 8)
103 ; wrmsr
105 ; Enable Prefetchers (Not supported on P4 or Atom)
106 ; mov ecx, 0x000001A0
107 ; rdmsr
108 ; and eax, 0xFFF7FDFF ; Clear Hardware Prefetcher Disable (Bit 9) and Adjacent Cache Line Prefetch Disable (Bit 19)
109 ; and edx, 0xFFFFFFAF ; Clear DCU Prefetcher Disable (Bit 37) and IP Prefetcher Disable (Bit 39)
110 ; wrmsr
112 ;init_cpu_skip2:
114 ; Enable Cache
115 mov rax, cr0
116 btr rax, 29 ; Clear No Write Thru (Bit 29)
117 btr rax, 30 ; Clear CD (Bit 30)
118 mov cr0, rax
120 ; Enable Paging Global Extensions
121 ; mov rax, cr4
122 ; bts rax, 7 ; Set Paging Global Extensions (Bit 7)
123 ; mov cr4, rax
125 ; Enable Floating Point
126 mov rax, cr0
127 bts rax, 1 ; Set Monitor co-processor (Bit 1)
128 btr rax, 2 ; Clear Emulation (Bit 2)
129 mov cr0, rax
131 ; Enable SSE
132 mov rax, cr4
133 bts rax, 9 ; Set Operating System Support for FXSAVE and FXSTOR instructions (Bit 9)
134 bts rax, 10 ; Set Operating System Support for Unmasked SIMD Floating-Point Exceptions (Bit 10)
135 mov cr4, rax
137 ; Enable Math Co-processor
138 finit
140 ; Enable and Configure Local APIC
141 mov rsi, [os_LocalAPICAddress]
142 cmp rsi, 0x00000000
143 je noMP ; Skip MP init if we didn't get a valid LAPIC address
145 xor eax, eax ; Clear Task Priority (bits 7:4) and Priority Sub-Class (bits 3:0)
146 mov dword [rsi+0x80], eax ; Task Priority Register (TPR)
148 mov eax, 0x01000000 ; Set bits 31-24 for all cores to be in Group 1
149 mov dword [rsi+0xD0], eax ; Logical Destination Register
151 xor eax, eax
152 sub eax, 1 ; Set EAX to 0xFFFFFFFF; Bits 31-28 set for Flat Mode
153 mov dword [rsi+0xE0], eax ; Destination Format Register
155 mov eax, dword [rsi+0xF0] ; Spurious Interrupt Vector Register
156 mov al, 0xF8
157 bts eax, 8 ; Enable APIC (Set bit 8)
158 mov dword [rsi+0xF0], eax
160 mov eax, dword [rsi+0x320] ; LVT Timer Register
161 bts eax, 16 ; Set bit 16 for mask interrupts
162 mov dword [rsi+0x320], eax
164 ; mov eax, dword [rsi+0x350] ; LVT LINT0 Register
165 ; mov al, 0 ;Set interrupt vector (bits 7:0)
166 ; bts eax, 8 ;Delivery Mode (111b==ExtlNT] (bits 10:8)
167 ; bts eax, 9
168 ; bts eax, 10
169 ; bts eax, 15 ;bit15:Set trigger mode to Level (0== Edge, 1== Level)
170 ; btr eax, 16 ;bit16:unmask interrupts (0==Unmasked, 1== Masked)
171 ; mov dword [rsi+0x350], eax
173 ; mov eax, dword [rsi+0x360] ; LVT LINT1 Register
174 ; mov al, 0 ;Set interrupt vector (bits 7:0)
175 ; bts eax, 8 ;Delivery Mode (111b==ExtlNT] (bits 10:8)
176 ; bts eax, 9
177 ; bts eax, 10
178 ; bts eax, 15 ;bit15:Set trigger mode to Edge (0== Edge, 1== Level)
179 ; btr eax, 16 ;bit16:unmask interrupts (0==Unmasked, 1== Masked)
180 ; mov dword [rsi+0x360], eax
182 ; mov eax, dword [rsi+0x370] ; LVT Error Register
183 ; mov al, 0 ;Set interrupt vector (bits 7:0)
184 ; bts eax, 16 ;bit16:Mask interrupts (0==Unmasked, 1== Masked)
185 ; mov dword [rsi+0x370], eax
191 ; =============================================================================
192 ; EOF