loader.asm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 0x7e00
  14. %include "boot/paging.asm"
  15. _start:
  16. xor ax, ax
  17. mov ds, ax
  18. mov es, ax
  19. mov ss, ax
  20. mov sp, 0x7c00
  21. ; display message 'Starting Loader...'
  22. mov si, message
  23. call print
  24. .open_a20:
  25. ; disable interrupts before we initial done (enable in kernel)
  26. ; A20 line uses to control memory higher than 1MB. we can't access mem > 1MB when it's closing.
  27. ; So we must open it before switching to protect-mode.
  28. cli
  29. in al, 0x92
  30. or al, 0x02
  31. out 0x92, al
  32. .to_prot:
  33. ; switch to protect-mode.
  34. lgdt [gdt32_desc]
  35. ; CR0 (control register 0) uses to control CPU. It's first bit PE controls Protect-mode Enable.
  36. mov eax, cr0
  37. or eax, 1
  38. mov cr0, eax
  39. ; CPU is in protect-mode now, but segment registers didn't initialze.
  40. ; We cannot 'mov cx, xxx'
  41. jmp dword code32:init_prot
  42. print:
  43. cld
  44. lodsb
  45. test al,al
  46. jz .ret
  47. mov ah, 0x0e
  48. mov bx, 0x0f
  49. int 0x10
  50. jmp print
  51. .ret:
  52. ret
  53. bits 32
  54. init_prot:
  55. ; we set segment selectors for segment register (CS was set by far-jmp)
  56. mov ax, data32
  57. mov ds, ax
  58. mov es, ax
  59. mov fs, ax
  60. mov gs, ax
  61. mov ss, ax
  62. mov sp, 0x00007c00
  63. ; copy kernel to 0x100000
  64. mov esi, 0x8000
  65. mov edi, 0x100000
  66. mov ecx, 64 * 512
  67. call memcpy
  68. ; now we switch to long-mode
  69. .to_long_mode:
  70. ; clear page map 0 - 0x4000 (0x3000-0x4000 maybe not used)
  71. xor edi, edi
  72. ; mov edi, 0x00000000
  73. xor eax, eax
  74. ; store for 4096 times
  75. mov ecx, 4096
  76. cld
  77. rep stosd
  78. .init_paging:
  79. ; we set page map at 0x00000000 (a good address)
  80. mov dword [0x00000000], 0x00001000 | PAGE_USER
  81. ; mov dword [0x00000004], 0x00000000
  82. mov dword [0x00000800], 0x00001000 | PAGE_USER
  83. ; mov dword [0x00000804], 0x00000000
  84. ; and page directory pointer table
  85. mov dword [0x00001000], 0x00002000 | PAGE_USER
  86. ; mov dword [0x00001004], 0x00000000
  87. ; page directory entry (2 MB)
  88. mov dword [0x00002000], 0x00000000 | PAGE_KERNEL
  89. ; mov dword [0x00002004], 0x00000000
  90. mov dword [0x00002008], 0x00200000 | PAGE_KERNEL
  91. ; mov dword [0x0000200c], 0x00000000
  92. .enable_long_mode:
  93. lgdt [gdt64_desc]
  94. mov ax, data64
  95. mov ds, ax
  96. mov es, ax
  97. mov fs, ax
  98. mov gs, ax
  99. mov ss, ax
  100. ; open PAE (page map level 3)
  101. mov eax, cr4
  102. bts eax, 5
  103. mov cr4, eax
  104. ; set Page Table Base Register
  105. ; mov eax, 0x00000000
  106. xor eax, eax
  107. mov cr3, eax
  108. ; enable long mode (must before paging)
  109. mov ecx, 0xc0000080
  110. rdmsr
  111. bts eax, 8
  112. wrmsr
  113. ; enable paging
  114. mov eax, cr0
  115. ; PE again
  116. or eax, 1
  117. bts eax, 31
  118. mov cr0, eax
  119. ; okay! now set segment registers and init long mode
  120. jmp code64:0x100000
  121. memcpy:
  122. cld
  123. rep movsb
  124. ret
  125. message: db "Starting Loader", 0x0d, 0x0a, 0x00
  126. alignb 16
  127. gdt32:
  128. dd 0x00000000, 0x00000000
  129. gdt32_code:
  130. dd 0x0000ffff, 0x00cf9a00
  131. gdt32_data:
  132. dd 0x0000ffff, 0x00cf9200
  133. gdt32_len equ $ - gdt32
  134. gdt32_desc:
  135. dw gdt32_len - 1
  136. dd gdt32
  137. code32 equ gdt32_code - gdt32
  138. data32 equ gdt32_data - gdt32
  139. gdt64:
  140. dq 0x0000000000000000
  141. gdt64_code:
  142. dq 0x0020980000000000
  143. gdt64_data:
  144. dq 0x0000920000000000
  145. gdt64_len equ $ - gdt64
  146. gdt64_desc:
  147. dw gdt64_len - 1
  148. dd gdt64
  149. code64 equ gdt64_code - gdt64
  150. data64 equ gdt64_data - gdt64
  151. times 510 - ($ - $$) db 0x00
  152. db 0x55, 0xaa