loader.asm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. ; Copyright (C) 2021 Rain
  2. ; This file is part of XNIX.
  3. ; XNIX is free software: you can redistribute it and/or modify
  4. ; it under the terms of the GNU General Public License as published by
  5. ; the Free Software Foundation, either version 3 of the License, or
  6. ; (at your option) and later version.
  7. ; XNIX is distributed in the hope that it will be useful,
  8. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. ; GNU General Public License for more details.
  11. ; You should have received a copy of the GNU General Public License
  12. ; along with XNIX. If not, see <https://www.gnu.org/licenses/>.
  13. org 0x7e60
  14. %include "boot/paging.asm"
  15. ; yeah, it's really 0x7c00 because I don't want to use other memory.
  16. BOOTDRIVER equ 0x7c00
  17. VMODE equ 0x7c01
  18. SCREENX equ 0x7c02
  19. SCREENY equ 0x7c04
  20. FB_ADDR equ 0x7c06
  21. _start:
  22. xor ax, ax
  23. mov ds, ax
  24. mov es, ax
  25. mov ss, ax
  26. mov sp, 0x7c00
  27. ; put a message on screen
  28. mov di, msg
  29. call print
  30. .die: jmp .die
  31. .set_vga:
  32. ; get mode info
  33. mov ax, 0x4f01
  34. mov cx, 0x4118
  35. mov di, 0x7000
  36. int 0x10
  37. die: jc die
  38. mov al,[es:di]
  39. mov [VMODE], al
  40. mov ax, [es:di + 18]
  41. mov [SCREENX], ax
  42. mov ax, [es:di + 20]
  43. mov [SCREENY], ax
  44. mov eax, [es:di + 40]
  45. mov [FB_ADDR], eax
  46. mov ax, 0x4f02
  47. mov bx, 0x4118
  48. int 0x10
  49. .open_a20:
  50. ; disable interrupts before we initial done (enable in kernel)
  51. ; A20 line uses to control memory higher than 1MB. we can't access mem > 1MB when it's closing.
  52. ; So we must open it before switching to protect-mode.
  53. cli
  54. in al, 0x92
  55. or al, 0x02
  56. out 0x92, al
  57. .to_prot:
  58. ; switch to protect-mode.
  59. lgdt [gdt32_desc]
  60. ; CR0 (control register 0) uses to control CPU. It's first bit PE controls Protect-mode Enable.
  61. mov eax, cr0
  62. or eax, 1
  63. mov cr0, eax
  64. ; CPU is in protect-mode now, but segment registers didn't initialze.
  65. ; we cannot 'mov cs, xxx'
  66. jmp dword code32:init_prot
  67. print:
  68. pushaw
  69. ; message in register di, but lodsb uses si.
  70. mov si, di
  71. .loop:
  72. cld
  73. lodsb
  74. ; zero? -> return
  75. test al, al
  76. jz .ret
  77. mov ah, 0x0e
  78. mov bx, 0x000f
  79. int 0x10
  80. jmp .loop
  81. .ret:
  82. popaw
  83. ret
  84. bits 32
  85. init_prot:
  86. ; we set segment selectors for segment register (CS was set by far-jmp)
  87. mov ax, data32
  88. mov ds, ax
  89. mov es, ax
  90. mov fs, ax
  91. mov gs, ax
  92. mov ss, ax
  93. mov sp, 0x00007c00
  94. ; now we switch to long-mode
  95. .to_long_mode:
  96. ; clear page map 0 - 0x4000 (0x3000-0x4000 maybe not used)
  97. xor edi, edi
  98. ; mov edi, 0x00000000
  99. xor eax, eax
  100. ; store for 4096 times
  101. mov ecx, 4096
  102. cld
  103. rep stosd
  104. .init_paging:
  105. ; we set page map at 0x00000000 (a good address)
  106. mov dword [0x00000000], 0x00001000 | PAGE_USER
  107. ; mov dword [0x00000004], 0x00000000
  108. mov dword [0x00000800], 0x00001000 | PAGE_USER
  109. ; mov dword [0x00000804], 0x00000000
  110. ; and page directory pointer table
  111. mov dword [0x00001000], 0x00002000 | PAGE_USER
  112. ; mov dword [0x00001004], 0x00000000
  113. ; page directory entry (2 MB)
  114. mov dword [0x00002000], 0x00000000 | PAGE_KERNEL
  115. ; mov dword [0x00002004], 0x00000000
  116. mov dword [0x00002008], 0x00200000 | PAGE_KERNEL
  117. ; mov dword [0x0000200c], 0x00000000
  118. ; get frame buffer address and map it here.
  119. mov eax, [0x7c06]
  120. ; eax &= PAGE_SIZE
  121. and eax, ~(1048576 * 2 - 1)
  122. or eax, PAGE_KERNEL
  123. mov dword [0x00002010], eax
  124. ; mov dword [0x00002014], 0x00000000
  125. add eax, 1048576 * 2
  126. mov dword [0x00002018], eax
  127. add eax, 1048576 * 2
  128. mov dword [0x00002020], eax
  129. .enable_long_mode:
  130. lgdt [gdt64_desc]
  131. mov ax, data64
  132. mov ds, ax
  133. mov es, ax
  134. mov fs, ax
  135. mov gs, ax
  136. mov ss, ax
  137. ; open PAE (page map level 3)
  138. mov eax, cr4
  139. bts eax, 5
  140. mov cr4, eax
  141. ; set Page Table Base Register
  142. ; mov eax, 0x00000000
  143. xor eax, eax
  144. mov cr3, eax
  145. ; enable long mode (must before paging)
  146. mov ecx, 0xc0000080
  147. rdmsr
  148. bts eax, 8
  149. wrmsr
  150. ; enable paging
  151. mov eax, cr0
  152. ; PE again
  153. or eax, 1
  154. bts eax, 31
  155. mov cr0, eax
  156. ; okay! now set segment registers and init long mode
  157. jmp code64:0x8000
  158. msg: db "welcome to XNIX", 0x0d, 0x0a, 0x00
  159. alignb 16
  160. gdt32:
  161. dd 0x00000000, 0x00000000
  162. gdt32_code:
  163. dd 0x0000ffff, 0x00cf9a00
  164. gdt32_data:
  165. dd 0x0000ffff, 0x00cf9200
  166. gdt32_len equ $ - gdt32
  167. gdt32_desc:
  168. dw gdt32_len - 1
  169. dd gdt32
  170. code32 equ gdt32_code - gdt32
  171. data32 equ gdt32_data - gdt32
  172. gdt64:
  173. dq 0x0000000000000000
  174. gdt64_code:
  175. dq 0x0020980000000000
  176. gdt64_data:
  177. dq 0x0000920000000000
  178. gdt64_len equ $ - gdt64
  179. gdt64_desc:
  180. dw gdt64_len - 1
  181. dd gdt64
  182. code64 equ gdt64_code - gdt64
  183. data64 equ gdt64_data - gdt64