|
|
@@ -1,285 +0,0 @@
|
|
|
- 1 ; Copyright (C) 2021 Rain
|
|
|
- 2
|
|
|
- 3 ; This file is part of XNIX.
|
|
|
- 4 ; XNIX is free software: you can redistribute it and/or modify
|
|
|
- 5 ; it under the terms of the GNU General Public License as published by
|
|
|
- 6 ; the Free Software Foundation, either version 3 of the License, or
|
|
|
- 7 ; (at your option) and later version.
|
|
|
- 8
|
|
|
- 9 ; XNIX is distributed in the hope that it will be useful,
|
|
|
- 10 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
- 11 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
- 12 ; GNU General Public License for more details.
|
|
|
- 13
|
|
|
- 14 ; You should have received a copy of the GNU General Public License
|
|
|
- 15 ; along with XNIX. If not, see <https://www.gnu.org/licenses/>.
|
|
|
- 16
|
|
|
- 17
|
|
|
- 18
|
|
|
- 19
|
|
|
- 20 org 0x7e00
|
|
|
- 21
|
|
|
- 22
|
|
|
- 23 %include "boot/paging.asm"
|
|
|
- 24 <1> ; Copyright (C) 2021 Rain
|
|
|
- 25 <1>
|
|
|
- 26 <1> ; This file is part of XNIX.
|
|
|
- 27 <1> ; XNIX is free software: you can redistribute it and/or modify
|
|
|
- 28 <1> ; it under the terms of the GNU General Public License as published by
|
|
|
- 29 <1> ; the Free Software Foundation, either version 3 of the License, or
|
|
|
- 30 <1> ; (at your option) and later version.
|
|
|
- 31 <1>
|
|
|
- 32 <1> ; XNIX is distributed in the hope that it will be useful,
|
|
|
- 33 <1> ; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
- 34 <1> ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
- 35 <1> ; GNU General Public License for more details.
|
|
|
- 36 <1>
|
|
|
- 37 <1> ; You should have received a copy of the GNU General Public License
|
|
|
- 38 <1> ; along with XNIX. If not, see <https://www.gnu.org/licenses/>.
|
|
|
- 39 <1>
|
|
|
- 40 <1>
|
|
|
- 41 <1>
|
|
|
- 42 <1>
|
|
|
- 43 <1> PATT_PRESENT equ 0x0001
|
|
|
- 44 <1> PATT_RDWR equ 0x0002
|
|
|
- 45 <1> PATT_USER equ 0x0004
|
|
|
- 46 <1> PATT_CACHEMEM equ 0x0008
|
|
|
- 47 <1> PATT_NOCACHE equ 0x0010
|
|
|
- 48 <1> PATT_ACCESSED equ 0x0020
|
|
|
- 49 <1> PATT_DIRTY equ 0x0040
|
|
|
- 50 <1> PATT_PAT equ 0x0080
|
|
|
- 51 <1> PATT_GLOBAL equ 0x0100
|
|
|
- 52 <1>
|
|
|
- 53 <1> ; 0x83 it is.
|
|
|
- 54 <1> PAGE_KERNEL equ PATT_PRESENT + PATT_RDWR + PATT_PAT
|
|
|
- 55 <1>
|
|
|
- 56 <1> ; this is linux's 007. (8-based 007)
|
|
|
- 57 <1> PAGE_USER equ PATT_PRESENT + PATT_RDWR + PATT_USER
|
|
|
- 58 <1>
|
|
|
- 24
|
|
|
- 25
|
|
|
- 26 _start:
|
|
|
- 27 00000000 31C0 xor ax, ax
|
|
|
- 28
|
|
|
- 29 00000002 8ED8 mov ds, ax
|
|
|
- 30 00000004 8EC0 mov es, ax
|
|
|
- 31
|
|
|
- 32 00000006 8ED0 mov ss, ax
|
|
|
- 33 00000008 BC007C mov sp, 0x7c00
|
|
|
- 34
|
|
|
- 35 ; display message 'Starting Loader...'
|
|
|
- 36
|
|
|
- 37 0000000B BE[EC00] mov si, message
|
|
|
- 38 0000000E E81E00 call print
|
|
|
- 39
|
|
|
- 40 .open_a20:
|
|
|
- 41 ; disable interrupts before we initial done (enable in kernel)
|
|
|
- 42
|
|
|
- 43 ; A20 line uses to control memory higher than 1MB. we can't access mem > 1MB when it's closing.
|
|
|
- 44 ; So we must open it before switching to protect-mode.
|
|
|
- 45
|
|
|
- 46 00000011 FA cli
|
|
|
- 47
|
|
|
- 48 00000012 E492 in al, 0x92
|
|
|
- 49 00000014 0C02 or al, 0x02
|
|
|
- 50 00000016 E692 out 0x92, al
|
|
|
- 51
|
|
|
- 52 .to_prot:
|
|
|
- 53 ; switch to protect-mode.
|
|
|
- 54
|
|
|
- 55 00000018 0F0116[1801] lgdt [gdt32_desc]
|
|
|
- 56
|
|
|
- 57 ; CR0 (control register 0) uses to control CPU. It's first bit PE controls Protect-mode Enable.
|
|
|
- 58 0000001D 0F20C0 mov eax, cr0
|
|
|
- 59 00000020 6683C801 or eax, 1
|
|
|
- 60 00000024 0F22C0 mov cr0, eax
|
|
|
- 61
|
|
|
- 62 ; CPU is in protect-mode now, but segment registers didn't initialze.
|
|
|
- 63
|
|
|
- 64 ; We cannot 'mov cx, xxx'
|
|
|
- 65
|
|
|
- 66 00000027 66EA[3F000000]0800 jmp dword code32:init_prot
|
|
|
- 67
|
|
|
- 68
|
|
|
- 69 print:
|
|
|
- 70 0000002F FC cld
|
|
|
- 71 00000030 AC lodsb
|
|
|
- 72
|
|
|
- 73 00000031 84C0 test al,al
|
|
|
- 74 00000033 7409 jz .ret
|
|
|
- 75
|
|
|
- 76 00000035 B40E mov ah, 0x0e
|
|
|
- 77 00000037 BB0F00 mov bx, 0x0f
|
|
|
- 78 0000003A CD10 int 0x10
|
|
|
- 79
|
|
|
- 80 0000003C EBF1 jmp print
|
|
|
- 81
|
|
|
- 82 .ret:
|
|
|
- 83 0000003E C3 ret
|
|
|
- 84
|
|
|
- 85
|
|
|
- 86 bits 32
|
|
|
- 87
|
|
|
- 88 init_prot:
|
|
|
- 89 ; we set segment selectors for segment register (CS was set by far-jmp)
|
|
|
- 90 0000003F 66B81000 mov ax, data32
|
|
|
- 91
|
|
|
- 92 00000043 8ED8 mov ds, ax
|
|
|
- 93 00000045 8EC0 mov es, ax
|
|
|
- 94 00000047 8EE0 mov fs, ax
|
|
|
- 95 00000049 8EE8 mov gs, ax
|
|
|
- 96
|
|
|
- 97 0000004B 8ED0 mov ss, ax
|
|
|
- 98
|
|
|
- 99 0000004D 66BC007C mov sp, 0x00007c00
|
|
|
- 100
|
|
|
- 101 ; copy kernel to 0x100000
|
|
|
- 102
|
|
|
- 103 00000051 BE00800000 mov esi, 0x8000
|
|
|
- 104 00000056 BF00001000 mov edi, 0x100000
|
|
|
- 105
|
|
|
- 106 0000005B B900800000 mov ecx, 64 * 512
|
|
|
- 107
|
|
|
- 108 00000060 E883000000 call memcpy
|
|
|
- 109
|
|
|
- 110 ; now we switch to long-mode
|
|
|
- 111
|
|
|
- 112 .to_long_mode:
|
|
|
- 113 ; clear page map 0 - 0x4000 (0x3000-0x4000 maybe not used)
|
|
|
- 114 00000065 31FF xor edi, edi
|
|
|
- 115 ; mov edi, 0x00000000
|
|
|
- 116
|
|
|
- 117 00000067 31C0 xor eax, eax
|
|
|
- 118 ; store for 4096 times
|
|
|
- 119 00000069 B900100000 mov ecx, 4096
|
|
|
- 120
|
|
|
- 121 0000006E FC cld
|
|
|
- 122 0000006F F3AB rep stosd
|
|
|
- 123
|
|
|
- 124 .init_paging:
|
|
|
- 125 ; we set page map at 0x00000000 (a good address)
|
|
|
- 126
|
|
|
- 127 00000071 C70500000000071000- mov dword [0x00000000], 0x00001000 | PAGE_USER
|
|
|
- 127 0000007A 00
|
|
|
- 128 ; mov dword [0x00000004], 0x00000000
|
|
|
- 129
|
|
|
- 130 0000007B C70500080000071000- mov dword [0x00000800], 0x00001000 | PAGE_USER
|
|
|
- 130 00000084 00
|
|
|
- 131 ; mov dword [0x00000804], 0x00000000
|
|
|
- 132
|
|
|
- 133 ; and page directory pointer table
|
|
|
- 134
|
|
|
- 135 00000085 C70500100000072000- mov dword [0x00001000], 0x00002000 | PAGE_USER
|
|
|
- 135 0000008E 00
|
|
|
- 136 ; mov dword [0x00001004], 0x00000000
|
|
|
- 137
|
|
|
- 138 ; page directory entry (2 MB)
|
|
|
- 139
|
|
|
- 140 0000008F C70500200000830000- mov dword [0x00002000], 0x00000000 | PAGE_KERNEL
|
|
|
- 140 00000098 00
|
|
|
- 141 ; mov dword [0x00002004], 0x00000000
|
|
|
- 142
|
|
|
- 143 00000099 C70508200000830020- mov dword [0x00002008], 0x00200000 | PAGE_KERNEL
|
|
|
- 143 000000A2 00
|
|
|
- 144 ; mov dword [0x0000200c], 0x00000000
|
|
|
- 145
|
|
|
- 146 .enable_long_mode:
|
|
|
- 147 000000A3 0F0115[36010000] lgdt [gdt64_desc]
|
|
|
- 148
|
|
|
- 149 000000AA 66B81000 mov ax, data64
|
|
|
- 150
|
|
|
- 151 000000AE 8ED8 mov ds, ax
|
|
|
- 152 000000B0 8EC0 mov es, ax
|
|
|
- 153 000000B2 8EE0 mov fs, ax
|
|
|
- 154 000000B4 8EE8 mov gs, ax
|
|
|
- 155 000000B6 8ED0 mov ss, ax
|
|
|
- 156
|
|
|
- 157 ; open PAE (page map level 3)
|
|
|
- 158 000000B8 0F20E0 mov eax, cr4
|
|
|
- 159 000000BB 0FBAE805 bts eax, 5
|
|
|
- 160 000000BF 0F22E0 mov cr4, eax
|
|
|
- 161
|
|
|
- 162 ; set Page Table Base Register
|
|
|
- 163
|
|
|
- 164 ; mov eax, 0x00000000
|
|
|
- 165 000000C2 31C0 xor eax, eax
|
|
|
- 166 000000C4 0F22D8 mov cr3, eax
|
|
|
- 167
|
|
|
- 168 ; enable long mode (must before paging)
|
|
|
- 169
|
|
|
- 170 000000C7 B9800000C0 mov ecx, 0xc0000080
|
|
|
- 171 000000CC 0F32 rdmsr
|
|
|
- 172
|
|
|
- 173 000000CE 0FBAE808 bts eax, 8
|
|
|
- 174 000000D2 0F30 wrmsr
|
|
|
- 175
|
|
|
- 176 ; enable paging
|
|
|
- 177
|
|
|
- 178 000000D4 0F20C0 mov eax, cr0
|
|
|
- 179
|
|
|
- 180 ; PE again
|
|
|
- 181 000000D7 83C801 or eax, 1
|
|
|
- 182 000000DA 0FBAE81F bts eax, 31
|
|
|
- 183
|
|
|
- 184 000000DE 0F22C0 mov cr0, eax
|
|
|
- 185
|
|
|
- 186 ; okay! now set segment registers and init long mode
|
|
|
- 187
|
|
|
- 188 000000E1 EA000010000800 jmp code64:0x100000
|
|
|
- 189
|
|
|
- 190 memcpy:
|
|
|
- 191 000000E8 FC cld
|
|
|
- 192 000000E9 F3A4 rep movsb
|
|
|
- 193
|
|
|
- 194 000000EB C3 ret
|
|
|
- 195
|
|
|
- 196
|
|
|
- 197
|
|
|
- 198 000000EC 5374617274696E6720- message: db "Starting Loader", 0x0d, 0x0a, 0x00
|
|
|
- 198 000000F5 4C6F616465720D0A00
|
|
|
- 199
|
|
|
- 200
|
|
|
- 201 000000FE ???? alignb 16
|
|
|
- 202
|
|
|
- 203 gdt32:
|
|
|
- 204 00000100 0000000000000000 dd 0x00000000, 0x00000000
|
|
|
- 205
|
|
|
- 206 gdt32_code:
|
|
|
- 207 00000108 FFFF0000009ACF00 dd 0x0000ffff, 0x00cf9a00
|
|
|
- 208
|
|
|
- 209 gdt32_data:
|
|
|
- 210 00000110 FFFF00000092CF00 dd 0x0000ffff, 0x00cf9200
|
|
|
- 211
|
|
|
- 212 gdt32_len equ $ - gdt32
|
|
|
- 213
|
|
|
- 214 gdt32_desc:
|
|
|
- 215 00000118 1700 dw gdt32_len - 1
|
|
|
- 216 0000011A [00010000] dd gdt32
|
|
|
- 217
|
|
|
- 218 code32 equ gdt32_code - gdt32
|
|
|
- 219 data32 equ gdt32_data - gdt32
|
|
|
- 220
|
|
|
- 221
|
|
|
- 222
|
|
|
- 223 gdt64:
|
|
|
- 224 0000011E 0000000000000000 dq 0x0000000000000000
|
|
|
- 225
|
|
|
- 226 gdt64_code:
|
|
|
- 227 00000126 0000000000982000 dq 0x0020980000000000
|
|
|
- 228
|
|
|
- 229 gdt64_data:
|
|
|
- 230 0000012E 0000000000920000 dq 0x0000920000000000
|
|
|
- 231
|
|
|
- 232 gdt64_len equ $ - gdt64
|
|
|
- 233
|
|
|
- 234 gdt64_desc:
|
|
|
- 235 00000136 1700 dw gdt64_len - 1
|
|
|
- 236 00000138 [1E010000] dd gdt64
|
|
|
- 237
|
|
|
- 238 code64 equ gdt64_code - gdt64
|
|
|
- 239 data64 equ gdt64_data - gdt64
|
|
|
- 240
|
|
|
- 241
|
|
|
- 242 0000013C 00<rep C2h> times 510 - ($ - $$) db 0x00
|
|
|
- 243 000001FE 55AA db 0x55, 0xaa
|
|
|
- 244
|