loader.lst 16 KB

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