loader.asm 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. ; now we switch to long-mode
  64. .to_long_mode:
  65. .init_paging:
  66. ; we set page map at 0x00000000 (a good address)
  67. mov dword [0x00000000], 0x00001000 | PAGE_USER
  68. mov dword [0x00000004], 0x00000000
  69. mov dword [0x00000800], 0x00001000 | PAGE_USER
  70. mov dword [0x00000804], 0x00000000
  71. ; and page directory pointer table
  72. mov dword [0x00001000], 0x00002000 | PAGE_USER
  73. mov dword [0x00001004], 0x00000000
  74. ; page directory entry (2 MB)
  75. mov dword [0x00002000], 0x00000000 | PAGE_KERNEL
  76. mov dword [0x00002004], 0x00000000
  77. mov dword [0x00002008], 0x00200000 | PAGE_KERNEL
  78. mov dword [0x0000200c], 0x00000000
  79. .enable_long_mode:
  80. lgdt [gdt64_desc]
  81. mov ax, data64
  82. mov ds, ax
  83. mov es, ax
  84. mov fs, ax
  85. mov gs, ax
  86. mov ss, ax
  87. ; open PAE (page map level 3)
  88. mov eax, cr4
  89. bts eax, 5
  90. mov cr4, eax
  91. ; set Page Table Base Register
  92. ; mov eax, 0x00000000
  93. xor eax, eax
  94. mov cr3, eax
  95. ; enable long mode (must before paging)
  96. mov ecx, 0xc0000080
  97. rdmsr
  98. bts eax, 8
  99. wrmsr
  100. ; enable paging
  101. mov eax, cr0
  102. ; PE again
  103. or eax, 1
  104. bts eax, 31
  105. mov cr0, eax
  106. ; okay! now set segment registers and init long mode
  107. jmp dword code64:init_long_mode
  108. bits 64
  109. init_long_mode:
  110. mov ax, data64
  111. mov es, ax
  112. mov ds, ax
  113. mov fs, ax
  114. mov gs, ax
  115. mov ss, ax
  116. .die: hlt
  117. jmp .die
  118. message: db "Starting Loader", 0x0d, 0x0a, 0x00
  119. alignb 16
  120. gdt32:
  121. dd 0x00000000, 0x00000000
  122. gdt32_code:
  123. dd 0x0000ffff, 0x00cf9a00
  124. gdt32_data:
  125. dd 0x0000ffff, 0x00cf9200
  126. gdt32_len equ $ - gdt32
  127. gdt32_desc:
  128. dw gdt32_len - 1
  129. dd gdt32
  130. code32 equ gdt32_code - gdt32
  131. data32 equ gdt32_data - gdt32
  132. gdt64:
  133. dq 0x0000000000000000
  134. gdt64_code:
  135. dq 0x0020980000000000
  136. gdt64_data:
  137. dq 0x0000920000000000
  138. gdt64_len equ $ - gdt64
  139. gdt64_desc:
  140. dw gdt64_len - 1
  141. dd gdt64
  142. code64 equ gdt64_code - gdt64
  143. data64 equ gdt64_data - gdt64
  144. times 510 - ($ - $$) db 0x00
  145. db 0x55, 0xaa