Advertisement
Guest User

multiboot 32->64 stub

a guest
Nov 5th, 2021
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.92 KB | None | 0 0
  1. .code32
  2. .sect .text
  3. .align 4
  4. /* multiboot header */
  5. .int 0x1BADB002
  6. .int 0x03
  7. .int -(0x1BADB002 + 0x03)
  8.  
  9. /* macro section */
  10. .macro NextLine
  11. /* set the "cursor" */
  12. movl %edi, %eax
  13. subl $0xB8000, %eax
  14. xorl %edx, %edx
  15. movl $160, %ecx
  16. divl %ecx
  17. subl %edx, %ecx
  18. movl %edi, %eax
  19. addl %ecx, %eax
  20. movl %eax, cursor
  21. .endm
  22.  
  23. .globl _start
  24. _start:
  25. /* just a small 4k stack */
  26. leal stack, %esp
  27. movl %eax, multiboot_magic /* keep multiboot magic, if something like GRUB was used */
  28.  
  29. /* clear screen */
  30. movl $(40 * 25), %ecx
  31. movl $0x07200720, %eax
  32. movl $0xB8000, %edi
  33. rep stosl
  34.  
  35. /* just some basic info */
  36. leal msg_microcxx, %esi
  37. movl cursor, %edi
  38. movb $0x07, %ah
  39. info_loop:
  40. lodsb
  41. testb %al, %al
  42. jz info_next
  43. stosw
  44. jmp info_loop
  45. info_next:
  46. NextLine
  47. leal msg_detect, %esi
  48. movl cursor, %edi
  49. movb $0x07, %ah
  50. detect_loop:
  51. lodsb
  52. testb %al, %al
  53. jz detect_next
  54. stosw
  55. jmp detect_loop
  56. detect_next:
  57. NextLine
  58.  
  59. /* check for multiboot magic number */
  60. movl multiboot_magic, %eax
  61. cmpl $0x2BADB002, %eax
  62. jne error_no_multiboot
  63. jmp success_multiboot
  64.  
  65. /* multiboot found -> say it */
  66. success_multiboot:
  67. leal success_msg_multiboot, %esi
  68. movl cursor, %edi
  69. movb $0x02, %ah
  70. success_multiboot_loop:
  71. lodsb
  72. testb %al, %al
  73. jz success_multiboot_next
  74. stosw
  75. jmp success_multiboot_loop
  76. success_multiboot_next:
  77. movl %ebx, multiboot_pointer /* keep a pointer to the mulltiboot struct */
  78. NextLine
  79.  
  80. /* check for CPUID opcode */
  81. pushfl
  82. popl %eax
  83. movl %eax, %ecx
  84. xorl $(1<<21), %eax
  85. pushl %eax
  86. popfl
  87. pushfl
  88. popl %eax
  89. pushl %ecx
  90. popfl
  91. xorl %eax, %ecx
  92. jz error_no_cpuid
  93. jmp success_cpuid
  94.  
  95. /* CPUID opcode available -> say it */
  96. success_cpuid:
  97. leal success_msg_cpuid, %esi
  98. movl cursor, %edi
  99. movb $0x02, %ah
  100. success_cpuid_loop:
  101. lodsb
  102. testb %al, %al
  103. jz success_cpuid_next
  104. stosw
  105. jmp success_cpuid_loop
  106. success_cpuid_next:
  107. NextLine
  108.  
  109. /* now check for CPUID extended functions */
  110. movl $0x80000000, %eax
  111. cpuid
  112. cmpl $0x80000001, %eax
  113. jb error_no_cpuid_ext
  114. jmp success_cpu_ext
  115.  
  116. /* CPUID extended functions available -> say it */
  117. success_cpu_ext:
  118. leal success_msg_cpuid_ext, %esi
  119. movl cursor, %edi
  120. movb $0x02, %ah
  121. success_cpuid_ext_loop:
  122. lodsb
  123. testb %al, %al
  124. jz success_cpuid_ext_next
  125. stosw
  126. jmp success_cpuid_ext_loop
  127. success_cpuid_ext_next:
  128. NextLine
  129.  
  130. /* finaly check for LongMode by using CPUID extended functions */
  131. movl $0x80000001, %eax
  132. cpuid
  133. testl $(1<<29), %edx
  134. jz error_no_longmode
  135.  
  136. /* LongMode is available -> say it */
  137. success_longmode:
  138. leal success_msg_longmode, %esi
  139. movl cursor, %edi
  140. movb $0x02, %ah
  141. success_longmode_loop:
  142. lodsb
  143. testb %al, %al
  144. jz success_longmode_next
  145. stosw
  146. jmp success_longmode_loop
  147. success_longmode_next:
  148. NextLine
  149.  
  150. /* announcing setup of page tables */
  151. leal msg_pagetables, %esi
  152. movl cursor, %edi
  153. movb $0x02, %ah
  154. pagetables_loop:
  155. lodsb
  156. testb %al, %al
  157. jz pagetables_next
  158. stosw
  159. jmp pagetables_loop
  160. pagetables_next:
  161. NextLine
  162.  
  163. /* temporarily setting all page tables to 0 */
  164. movl $0x6000, %ecx
  165. leal pt_lvl4, %edi
  166. movb $0, %al
  167. rep stosb
  168. /* set the entry (0 and 768) in the level page table */
  169. leal pt_lvl3, %eax
  170. orl $7, %eax
  171. leal pt_lvl4, %ebx
  172. movl %eax, (%ebx)
  173. movl %eax, 0xFF8(%ebx)
  174.  
  175. /* now insert 4 page dirs */
  176. leal pt_lvl2, %eax
  177. leal pt_lvl3, %edx
  178. orl $7, %eax
  179.  
  180. movl $4, %ecx
  181. pt_pointer_loop:
  182. movl %eax, (%edx)
  183. addl $4096, %eax
  184. addl $8, %edx
  185. loop pt_pointer_loop
  186.  
  187. movl $0x197, %eax
  188. leal pt_lvl2, %edx
  189. movl $2048, %ecx
  190. pt_loop:
  191. movl %eax, (%edx)
  192. addl $8, %edx
  193. addl $(2 * 1024 * 1024), %eax
  194. loop pt_loop
  195.  
  196. /* now bring up PAE, but first announce it */
  197. leal msg_act_pae, %esi
  198. movl cursor, %edi
  199. movb $0x02, %ah
  200. act_pae_loop:
  201. lodsb
  202. testb %al, %al
  203. jz act_pae_next
  204. stosw
  205. jmp act_pae_loop
  206. act_pae_next:
  207. NextLine
  208.  
  209. /* here is the activation */
  210. movl %cr4, %eax
  211. orl $0x20, %eax
  212. movl %eax, %cr4
  213.  
  214. /* lvl4 page table to cr3 */
  215. leal pt_lvl4, %eax
  216. /* caching off */
  217. movl %eax, %cr3
  218.  
  219. /* now bring up LongMode, but first announce it */
  220. leal msg_act_longmode, %esi
  221. movl cursor, %edi
  222. movb $0x02, %ah
  223. act_longmode_loop:
  224. lodsb
  225. testb %al, %al
  226. jz act_longmode_next
  227. stosw
  228. jmp act_longmode_loop
  229. act_longmode_next:
  230. NextLine
  231.  
  232. /* here is the setup */
  233. movl $0xC0000080, %ecx
  234. rdmsr
  235. orl $0x100, %eax
  236. wrmsr
  237. /* and here is the final activation by turning on paging */
  238. movl %cr0, %eax
  239. orl $0x80000000, %eax
  240. movl %eax, %cr0
  241. /* finaly load the new GDT */
  242. lgdt gdtr_temp
  243. ljmp $0x18, $longmode
  244.  
  245. jmp halt
  246.  
  247. error_no_multiboot:
  248. leal error_msg_no_multiboot, %esi
  249. jmp print_err
  250.  
  251. error_no_cpuid:
  252. leal error_msg_no_cpuid, %esi
  253. jmp print_err
  254.  
  255. error_no_cpuid_ext:
  256. leal error_msg_no_cpuid_ext, %esi
  257. jmp print_err
  258.  
  259. error_no_longmode:
  260. leal error_msg_no_longmode, %esi
  261.  
  262. print_err:
  263. movl cursor, %edi
  264. movb $0x04, %ah
  265. print_err_loop:
  266. lodsb
  267. testb %al, %al
  268. jz halt
  269. stosw
  270. jmp print_err_loop
  271.  
  272. halt:
  273. cli
  274. hlt
  275. halt_loop:
  276. jmp halt_loop
  277.  
  278. .sect .data
  279. msg_microcxx:
  280. .asciz "### MicroCXX ###"
  281. msg_detect:
  282. .asciz "running basic hardware detection..."
  283. msg_pagetables:
  284. .asciz ". setting up some temporary pagetables"
  285. msg_act_pae:
  286. .asciz ". activating PAE"
  287. msg_act_longmode:
  288. .asciz ". activating LongMode"
  289. success_msg_multiboot:
  290. .asciz ". multiboot structure found"
  291. success_msg_cpuid:
  292. .asciz ". CPUID opcode available"
  293. success_msg_cpuid_ext:
  294. .asciz ". CPUID extended functions available"
  295. success_msg_longmode:
  296. .asciz ". LongMode available"
  297. error_msg_no_multiboot:
  298. .asciz "ERROR: no multiboot structure found -> HALT"
  299. error_msg_no_cpuid:
  300. .asciz "ERROR: CPUID opcode not available -> HALT"
  301. error_msg_no_cpuid_ext:
  302. .asciz "ERROR: CPUID extended functions not available -> HALT"
  303. error_msg_no_longmode:
  304. .asciz "ERROR: LongMode not available -> HALT"
  305. cursor:
  306. .int 0xB8000
  307. multiboot_magic:
  308. .int 0
  309. multiboot_pointer:
  310. .int 0
  311.  
  312. .sect .bss
  313. .align 4096
  314. pt_lvl4:
  315. .space 4096
  316. .align 4096
  317. pt_lvl3:
  318. .space 4096
  319. .align 4096
  320. pt_lvl2:
  321. .space 4096 * 4
  322. .align 4096
  323. .space 4096
  324. stack:
  325.  
  326.  
  327.  
  328.  
  329. /* here is the 64bit code! */
  330. .code64
  331. .sect .text
  332. .extern loader64
  333. longmode:
  334. xorq %rdi, %rdi
  335. movl multiboot_pointer, %edi
  336. movl multiboot_magic, %esi
  337. movl cursor, %edx
  338. call loader64
  339.  
  340. halt64:
  341. cli
  342. hlt
  343. halt64_loop:
  344. jmp halt64_loop
  345.  
  346. .sect .data
  347. gdt_temp:
  348. /* NULL descriptor */
  349. .quad 0
  350. /* 32bit code descriptor */
  351. .word 0xFFFF, 0x0000
  352. .byte 0x00, 0x98, 0xCF, 0x00
  353. /* 32bit data descriptor */
  354. .word 0xFFFF, 0x0000
  355. .byte 0x00, 0x92, 0xCF, 0x00
  356. /* 64bit descriptor */
  357. .int 0x00000000
  358. .byte 0x00, 0x98, 0x20, 0x0
  359. gdtr_temp:
  360. .word 4 * 8
  361. .int gdt_temp
  362.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement