loader.asm 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. .enable_long_mode:
  74. lgdt [gdt64_desc]
  75. mov ax, data64
  76. mov ds, ax
  77. mov es, ax
  78. mov fs, ax
  79. mov gs, ax
  80. mov ss, ax
  81. ; open PAE (page map level 3)
  82. mov eax, cr4
  83. bts eax, 5
  84. mov cr4, eax
  85. ; set Page Table Base Register
  86. ; mov eax, 0x00000000
  87. xor eax, eax
  88. mov cr3, eax
  89. ; enable long mode (must before paging)
  90. mov ecx, 0xc0000080
  91. rdmsr
  92. bts eax, 8
  93. wrmsr
  94. ; enable paging
  95. mov eax, cr0
  96. ; PE again
  97. or eax, 1
  98. bts eax, 31
  99. mov cr0, eax
  100. ; okay! now set segment registers and init long mode
  101. jmp code64:0x8000
  102. alignb 16
  103. gdt32:
  104. dd 0x00000000, 0x00000000
  105. gdt32_code:
  106. dd 0x0000ffff, 0x00cf9a00
  107. gdt32_data:
  108. dd 0x0000ffff, 0x00cf9200
  109. gdt32_len equ $ - gdt32
  110. gdt32_desc:
  111. dw gdt32_len - 1
  112. dd gdt32
  113. code32 equ gdt32_code - gdt32
  114. data32 equ gdt32_data - gdt32
  115. gdt64:
  116. dq 0x0000000000000000
  117. gdt64_code:
  118. dq 0x0020980000000000
  119. gdt64_data:
  120. dq 0x0000920000000000
  121. gdt64_len equ $ - gdt64
  122. gdt64_desc:
  123. dw gdt64_len - 1
  124. dd gdt64
  125. code64 equ gdt64_code - gdt64
  126. data64 equ gdt64_data - gdt64
  127. times 512 - ($ - $$) db 0x00