loader.asm 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. ; Copyright (C) 2021 Rain
  2. ; This file is part of Cunix.
  3. ; Cunix is free software: you can redistribute it and/or modify
  4. ; it under the terms of the GNU General Public Licene and published by
  5. ; the Free Software Foundation, either version 3 of the License, or
  6. ; (at your option) any later version.
  7. ; Cunix 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 recceived a copy of the GNU General Public License
  12. ; along with Cunix. If not, see <https://www.gnu.org/licenses/>.
  13. org 0x8000
  14. align 8
  15. jmp _start
  16. CYLINDERS_READ equ 10
  17. SECTORS_READ equ CYLINDERS_READ * 2 * 18
  18. SIZE_READ equ SECTORS_READ * 512
  19. %include "arch/x86/segment.inc"
  20. %include "arch/x86/paging.inc"
  21. bits 16
  22. _start:
  23. ; enable A20 address 20 line
  24. .open_a20:
  25. in al, 0x92
  26. or al, 0x02
  27. out 0x92, al
  28. .to_protect_mode:
  29. cli
  30. lgdt [gdt32_descriptor]
  31. mov eax, cr0
  32. ; set CR0.bit 1 (Protect-mode Enable)
  33. or eax, 1
  34. mov cr0, eax
  35. jmp dword code32:init_protect_mode
  36. bits 32
  37. init_protect_mode:
  38. mov ax, 0x10
  39. mov ds, ax
  40. mov es, ax
  41. mov fs, ax
  42. mov gs, ax
  43. mov ss, ax
  44. mov esp, 0x00007c00
  45. .copy_kernel:
  46. mov esi, 0x8200
  47. mov edi, 0x100000
  48. mov ecx, SIZE_READ
  49. call memcpy
  50. .to_long_mode:
  51. ; page map at 0x7000
  52. ; set pagemap level-4
  53. mov dword [0x70000], 0x71000 | PAGE_USER
  54. mov dword [0x70004], 0x00000
  55. mov dword [0x70800], 0x70000 | PAGE_USER
  56. mov dword [0x70804], 0x00000
  57. ; ...and, page directory pointer table
  58. mov dword [0x71000], 0x72000 | PAGE_USER
  59. mov dword [0x71008], 0x00000
  60. ; then, page directory entry (2 MB size)
  61. mov dword [0x72000], 0x000000 | PAGE_KERNEL
  62. mov dword [0x72004], 0x000000
  63. mov dword [0x72008], 0x200000 | PAGE_KERNEL
  64. mov dword [0x7200c], 0x000000
  65. mov dword [0x72010], 0x400000 | PAGE_KERNEL
  66. mov dword [0x72014], 0x000000
  67. lgdt [gdt64_descriptor]
  68. mov ax, 0x10
  69. mov ds, ax
  70. mov es, ax
  71. mov fs, ax
  72. mov gs, ax
  73. mov ss, ax
  74. mov esp, 0x00007c00
  75. mov eax, cr4
  76. bts eax, 5
  77. mov cr4, eax
  78. ; page table
  79. mov eax, 0x70000
  80. mov cr3, eax
  81. ; enable long-mode before paging
  82. mov ecx, 0xc0000080
  83. rdmsr
  84. bts eax, 8
  85. wrmsr
  86. mov eax, cr0
  87. or eax, 1
  88. bts eax, 31
  89. mov cr0, eax
  90. mov ax, 0x10
  91. mov ds, ax
  92. mov es, ax
  93. mov fs, ax
  94. mov gs, ax
  95. mov ss, ax
  96. jmp code64:0x100000
  97. memcpy:
  98. ; if no this, loop will --ecx and ecx will be 0xffffffff.
  99. cmp ecx, 0
  100. jne .copy
  101. ret
  102. .copy:
  103. lodsb
  104. stosb
  105. loop .copy
  106. ret
  107. gdt32:
  108. dd 0x00000000, 0x00000000
  109. gdt32_code:
  110. dd 0x0000ffff, 0x00cf9a00
  111. gdt32_data:
  112. dd 0x0000ffff, 0x00cf9200
  113. gdt32_len equ $ - gdt32
  114. gdt32_descriptor:
  115. dw gdt32_len - 1
  116. dd gdt32
  117. code32 equ gdt32_code - gdt32
  118. data32 equ gdt32_data - gdt32
  119. gdt64:
  120. dq 0x0000000000000000
  121. gdt64_code:
  122. dq 0x0020980000000000
  123. gdt64_data:
  124. dq 0x0000920000000000
  125. gdt64_len equ $ - gdt64
  126. gdt64_descriptor:
  127. dw gdt64_len - 1
  128. dd gdt64
  129. code64 equ gdt64_code - gdt64
  130. data64 equ gdt64_data - gdt64
  131. times 512 - ($ - $$) db 0x00