Advertisement
MichaelPetch

SO78304950 - boot.asm

Apr 10th, 2024 (edited)
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. KERNEL_STACKSIZE equ 2*1024*1024
  2.  
  3. section .text
  4.  
  5. bits 32
  6.  
  7. header_start:
  8. ; magic number
  9. dd 0xe85250d6
  10. ; architecture
  11. dd 0
  12. ; header length
  13. dd header_end - header_start
  14. ; checksum
  15. dd - (0xe85250d6 + 0 + (header_end - header_start))
  16. bootinfo_request_tag:
  17. dd 1
  18. dd bootinfo_request_tag_end - bootinfo_request_tag
  19. dd 6 ; memory map
  20. dd 1 ; boot command line
  21. dd 8 ; framebuffer info
  22. dd 12 ; efi64 system table pointer
  23. bootinfo_request_tag_end:
  24. framebuffer_tag:
  25. dw 5 ;type
  26. dw 1 ;flag
  27. dd 20 ;size
  28. dd 800 ; width
  29. dd 600 ; height
  30. dd 32 ; depth
  31. framebuffer_tag_end:
  32. dd 0
  33. end_tag:
  34. dd 0
  35. dd 8
  36. header_end:
  37.  
  38. bits 32
  39.  
  40. global start
  41.  
  42. start:
  43. mov esp, stack_top
  44. push ebx
  45.  
  46. call check_multiboot
  47. call check_cpuid
  48. call check_long_mode
  49.  
  50. call setup_page_tables
  51. call enable_paging
  52.  
  53. pop edi
  54. lgdt [gdt64.pointer]
  55. jmp gdt64.code_segment:long_mode_start
  56.  
  57. hlt
  58.  
  59. check_multiboot:
  60. cmp eax, 0x36d76289
  61. jne .no_multiboot
  62. ret
  63. .no_multiboot:
  64. mov al, "M"
  65. jmp error
  66.  
  67. check_cpuid:
  68. pushfd
  69. pop eax
  70. mov ecx, eax
  71. xor eax, 1 << 21
  72. push eax
  73. popfd
  74. pushfd
  75. pop eax
  76. push ecx
  77. popfd
  78. cmp eax, ecx
  79. je .no_cpuid
  80. ret
  81. .no_cpuid:
  82. mov al, "C"
  83. jmp error
  84.  
  85. check_long_mode:
  86. mov eax, 0x80000000
  87. cpuid
  88. cmp eax, 0x80000001
  89. jb .no_long_mode
  90.  
  91. mov eax, 0x80000001
  92. cpuid
  93. test edx, 1 << 29
  94. jz .no_long_mode
  95.  
  96. ret
  97. .no_long_mode:
  98. mov al, "L"
  99. jmp error
  100.  
  101. ; Identitiy map first 4GiB
  102. setup_page_tables:
  103. mov eax, page_table_l3
  104. or eax, 0b11 ; present, writable
  105. mov [page_table_l4], eax
  106.  
  107. mov eax, page_table_l2_0 ; Map 1st GiB
  108. or eax, 0b11 ; present, writable
  109. mov [page_table_l3+8*0], eax
  110. mov eax, page_table_l2_1 ; Map 2nd GiB
  111. or eax, 0b11 ; present, writable
  112. mov [page_table_l3+8*1], eax
  113. mov eax, page_table_l2_2 ; Map 3rd GiB
  114. or eax, 0b11 ; present, writable
  115. mov [page_table_l3+8*2], eax
  116. mov eax, page_table_l2_3 ; Map 4th GiB
  117. or eax, 0b11 ; present, writable
  118. mov [page_table_l3+8*3], eax
  119.  
  120. mov ecx, 0 ; counter
  121.  
  122. .loop:
  123.  
  124. mov eax, 0x200000 ; 2MiB
  125. mul ecx
  126. or eax, 0b10000011 ; present, writable, huge page
  127. mov [page_table_l2 + ecx * 8], eax
  128.  
  129. inc ecx ; increment counter
  130. cmp ecx, 512*4 ; checks if the whole table is mapped
  131. jne .loop ; if not, continue
  132.  
  133. ret
  134.  
  135. enable_paging:
  136. ; pass page table location to cpu
  137. mov eax, page_table_l4
  138. mov cr3, eax
  139.  
  140. ; enable PAE
  141. mov eax, cr4
  142. or eax, 1 << 5
  143. mov cr4, eax
  144.  
  145. ; enable long mode
  146. mov ecx, 0xC0000080
  147. rdmsr
  148. or eax, 1 << 8
  149. wrmsr
  150.  
  151. ; enable paging
  152. mov eax, cr0
  153. or eax, 1 << 31
  154. mov cr0, eax
  155.  
  156. ret
  157.  
  158. error:
  159. ; print "ERR: X" where X is the error code
  160. mov dword [0xb8000], 0x4f524f45
  161. mov dword [0xb8004], 0x4f3a4f52
  162. mov dword [0xb8008], 0x4f204f20
  163. mov byte [0xb800a], al
  164. hlt
  165.  
  166. bits 64
  167. long_mode_start:
  168. ; load null into all data segment registers
  169. mov ax, 0
  170. mov ss, ax
  171. mov ds, ax
  172. mov es, ax
  173. mov fs, ax
  174. mov gs, ax
  175.  
  176. mov rsp,KERNEL_STACK+KERNEL_STACKSIZE
  177.  
  178. ; Ensure top 32 bits of RDI are 0
  179. mov edi, edi
  180.  
  181. extern kernel_main
  182. ; 1st parameter in RDI contains ptr to multiboot tag structure
  183. call kernel_main
  184.  
  185. hlt
  186.  
  187. bits 32
  188. section .bss
  189. align 4096
  190. page_table_l4:
  191. resb 4096
  192. page_table_l3:
  193. resb 4096
  194.  
  195. page_table_l2:
  196. page_table_l2_0:
  197. resb 4096
  198. page_table_l2_1:
  199. resb 4096
  200. page_table_l2_2:
  201. resb 4096
  202. page_table_l2_3:
  203. resb 4096
  204.  
  205. stack_bottom:
  206. resb 4096 * 4
  207. stack_top:
  208.  
  209. bits 64
  210. KERNEL_STACK:
  211. resb KERNEL_STACKSIZE
  212.  
  213. bits 32
  214. section .rodata
  215. gdt64:
  216. dq 0 ; zero entry
  217. .code_segment: equ $ - gdt64
  218. dq (1 << 43) | (1 << 44) | (1 << 47) | (1 << 53) ; code segment
  219. .pointer:
  220. dw $ - gdt64 - 1 ; length
  221. dq gdt64 ; address
  222.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement