loader.asm 3.6 KB

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