Advertisement
Guest User

borked.asm

a guest
Dec 6th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. [BITS 32]
  2.  
  3. global start
  4.  
  5. MB_MAGIC equ 0x1BADB002
  6. MB_FLAGS equ (1 << 0) | (1 << 1)
  7. MB_CHECKSUM equ (0 - (MB_MAGIC + MB_FLAGS))
  8.  
  9. section .multiboot
  10. align 4
  11. dd MB_MAGIC
  12. dd MB_FLAGS
  13. dd MB_CHECKSUM
  14.  
  15. section .bss
  16. align 16
  17. stack_bottom:
  18. resb 4096*4096
  19. stack_top:
  20.  
  21. section .text
  22.  
  23. GDT64:
  24. .Null: equ $ - GDT64
  25. dw 0xFFFF ; Limit (low).
  26. dw 0 ; Base (low).
  27. db 0 ; Base (middle)
  28. db 0 ; Access.
  29. db 1 ; Granularity.
  30. db 0 ; Base (high).
  31. .Code: equ $ - GDT64 ; The code descriptor.
  32. dw 0 ; Limit (low).
  33. dw 0 ; Base (low).
  34. db 0 ; Base (middle)
  35. db 10011010b ; Access (exec/read).
  36. db 10101111b ; Granularity, 64 bits flag, limit19:16.
  37. db 0 ; Base (high).
  38. .Data: equ $ - GDT64 ; The data descriptor.
  39. dw 0 ; Limit (low).
  40. dw 0 ; Base (low).
  41. db 0 ; Base (middle)
  42. db 10010010b ; Access (read/write).
  43. db 00000000b ; Granularity.
  44. db 0 ; Base (high).
  45. .Pointer: ; The GDT-pointer.
  46. dw $ - GDT64 - 1 ; Limit.
  47. dq GDT64
  48.  
  49. start:
  50. jmp cpuid_check
  51. kernel:
  52. mov esp, stack_top
  53. call kernel_main
  54. jmp hang
  55.  
  56. cpuid_check:
  57. pushfd
  58. pop eax
  59. mov ecx, eax
  60.  
  61. xor eax, 1 << 21
  62. push eax
  63. popfd
  64.  
  65. pushfd
  66. pop eax
  67. push ecx
  68.  
  69. popfd
  70. xor ecx, eax ;cpuid is supported if jne
  71. jne no_cpuid
  72. jmp long_mode_check
  73.  
  74. no_cpuid:
  75. xor eax, eax
  76. jmp hang
  77.  
  78. long_mode_check:
  79. mov eax, 0x80000000
  80. cpuid
  81. cmp eax, 0x80000001
  82. jb no_long_mode
  83.  
  84. mov eax, 0x80000001
  85. cpuid
  86. test edx, 1 << 29
  87. jz no_long_mode
  88.  
  89. table_clear:
  90. mov edi, 0x1000
  91. mov cr3, edi
  92. xor eax, eax
  93. mov ecx, 4096
  94. rep stosd
  95. mov edi, cr3
  96.  
  97. set_tables:
  98. mov DWORD [edi], 0x2003
  99. add edi, 0x1000
  100. mov DWORD [edi], 0x3003
  101. add edi, 0x1000
  102. mov DWORD [edi], 0x4003
  103. add edi, 0x1000
  104. mov ebx, 0x00000003
  105. mov ecx, 512
  106. entry_set:
  107. mov [edi], ebx
  108. add ebx, 0x1000
  109. add edi, 8
  110. loop entry_set
  111. enable_paging:
  112. mov eax, cr4
  113. or eax, 1 << 5
  114. mov cr4, eax
  115.  
  116. long_mode_switch:
  117. mov ecx, 0xC0000080
  118. rdmsr
  119. or eax, 1 << 8
  120. mov cr0, eax
  121. lgdt [GDT64.Pointer]
  122. jmp GDT64.Code:kernel
  123.  
  124. no_long_mode:
  125. jmp hang
  126.  
  127. hang:
  128. cli
  129. hlt
  130.  
  131. hanging:
  132. jmp hanging
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement