loader.asm 3.6 KB

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