Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .code32
- .sect .text
- .align 4
- /* multiboot header */
- .int 0x1BADB002
- .int 0x03
- .int -(0x1BADB002 + 0x03)
- /* macro section */
- .macro NextLine
- /* set the "cursor" */
- movl %edi, %eax
- subl $0xB8000, %eax
- xorl %edx, %edx
- movl $160, %ecx
- divl %ecx
- subl %edx, %ecx
- movl %edi, %eax
- addl %ecx, %eax
- movl %eax, cursor
- .endm
- .globl _start
- _start:
- /* just a small 4k stack */
- leal stack, %esp
- movl %eax, multiboot_magic /* keep multiboot magic, if something like GRUB was used */
- /* clear screen */
- movl $(40 * 25), %ecx
- movl $0x07200720, %eax
- movl $0xB8000, %edi
- rep stosl
- /* just some basic info */
- leal msg_microcxx, %esi
- movl cursor, %edi
- movb $0x07, %ah
- info_loop:
- lodsb
- testb %al, %al
- jz info_next
- stosw
- jmp info_loop
- info_next:
- NextLine
- leal msg_detect, %esi
- movl cursor, %edi
- movb $0x07, %ah
- detect_loop:
- lodsb
- testb %al, %al
- jz detect_next
- stosw
- jmp detect_loop
- detect_next:
- NextLine
- /* check for multiboot magic number */
- movl multiboot_magic, %eax
- cmpl $0x2BADB002, %eax
- jne error_no_multiboot
- jmp success_multiboot
- /* multiboot found -> say it */
- success_multiboot:
- leal success_msg_multiboot, %esi
- movl cursor, %edi
- movb $0x02, %ah
- success_multiboot_loop:
- lodsb
- testb %al, %al
- jz success_multiboot_next
- stosw
- jmp success_multiboot_loop
- success_multiboot_next:
- movl %ebx, multiboot_pointer /* keep a pointer to the mulltiboot struct */
- NextLine
- /* check for CPUID opcode */
- pushfl
- popl %eax
- movl %eax, %ecx
- xorl $(1<<21), %eax
- pushl %eax
- popfl
- pushfl
- popl %eax
- pushl %ecx
- popfl
- xorl %eax, %ecx
- jz error_no_cpuid
- jmp success_cpuid
- /* CPUID opcode available -> say it */
- success_cpuid:
- leal success_msg_cpuid, %esi
- movl cursor, %edi
- movb $0x02, %ah
- success_cpuid_loop:
- lodsb
- testb %al, %al
- jz success_cpuid_next
- stosw
- jmp success_cpuid_loop
- success_cpuid_next:
- NextLine
- /* now check for CPUID extended functions */
- movl $0x80000000, %eax
- cpuid
- cmpl $0x80000001, %eax
- jb error_no_cpuid_ext
- jmp success_cpu_ext
- /* CPUID extended functions available -> say it */
- success_cpu_ext:
- leal success_msg_cpuid_ext, %esi
- movl cursor, %edi
- movb $0x02, %ah
- success_cpuid_ext_loop:
- lodsb
- testb %al, %al
- jz success_cpuid_ext_next
- stosw
- jmp success_cpuid_ext_loop
- success_cpuid_ext_next:
- NextLine
- /* finaly check for LongMode by using CPUID extended functions */
- movl $0x80000001, %eax
- cpuid
- testl $(1<<29), %edx
- jz error_no_longmode
- /* LongMode is available -> say it */
- success_longmode:
- leal success_msg_longmode, %esi
- movl cursor, %edi
- movb $0x02, %ah
- success_longmode_loop:
- lodsb
- testb %al, %al
- jz success_longmode_next
- stosw
- jmp success_longmode_loop
- success_longmode_next:
- NextLine
- /* announcing setup of page tables */
- leal msg_pagetables, %esi
- movl cursor, %edi
- movb $0x02, %ah
- pagetables_loop:
- lodsb
- testb %al, %al
- jz pagetables_next
- stosw
- jmp pagetables_loop
- pagetables_next:
- NextLine
- /* temporarily setting all page tables to 0 */
- movl $0x6000, %ecx
- leal pt_lvl4, %edi
- movb $0, %al
- rep stosb
- /* set the entry (0 and 768) in the level page table */
- leal pt_lvl3, %eax
- orl $7, %eax
- leal pt_lvl4, %ebx
- movl %eax, (%ebx)
- movl %eax, 0xFF8(%ebx)
- /* now insert 4 page dirs */
- leal pt_lvl2, %eax
- leal pt_lvl3, %edx
- orl $7, %eax
- movl $4, %ecx
- pt_pointer_loop:
- movl %eax, (%edx)
- addl $4096, %eax
- addl $8, %edx
- loop pt_pointer_loop
- movl $0x197, %eax
- leal pt_lvl2, %edx
- movl $2048, %ecx
- pt_loop:
- movl %eax, (%edx)
- addl $8, %edx
- addl $(2 * 1024 * 1024), %eax
- loop pt_loop
- /* now bring up PAE, but first announce it */
- leal msg_act_pae, %esi
- movl cursor, %edi
- movb $0x02, %ah
- act_pae_loop:
- lodsb
- testb %al, %al
- jz act_pae_next
- stosw
- jmp act_pae_loop
- act_pae_next:
- NextLine
- /* here is the activation */
- movl %cr4, %eax
- orl $0x20, %eax
- movl %eax, %cr4
- /* lvl4 page table to cr3 */
- leal pt_lvl4, %eax
- /* caching off */
- movl %eax, %cr3
- /* now bring up LongMode, but first announce it */
- leal msg_act_longmode, %esi
- movl cursor, %edi
- movb $0x02, %ah
- act_longmode_loop:
- lodsb
- testb %al, %al
- jz act_longmode_next
- stosw
- jmp act_longmode_loop
- act_longmode_next:
- NextLine
- /* here is the setup */
- movl $0xC0000080, %ecx
- rdmsr
- orl $0x100, %eax
- wrmsr
- /* and here is the final activation by turning on paging */
- movl %cr0, %eax
- orl $0x80000000, %eax
- movl %eax, %cr0
- /* finaly load the new GDT */
- lgdt gdtr_temp
- ljmp $0x18, $longmode
- jmp halt
- error_no_multiboot:
- leal error_msg_no_multiboot, %esi
- jmp print_err
- error_no_cpuid:
- leal error_msg_no_cpuid, %esi
- jmp print_err
- error_no_cpuid_ext:
- leal error_msg_no_cpuid_ext, %esi
- jmp print_err
- error_no_longmode:
- leal error_msg_no_longmode, %esi
- print_err:
- movl cursor, %edi
- movb $0x04, %ah
- print_err_loop:
- lodsb
- testb %al, %al
- jz halt
- stosw
- jmp print_err_loop
- halt:
- cli
- hlt
- halt_loop:
- jmp halt_loop
- .sect .data
- msg_microcxx:
- .asciz "### MicroCXX ###"
- msg_detect:
- .asciz "running basic hardware detection..."
- msg_pagetables:
- .asciz ". setting up some temporary pagetables"
- msg_act_pae:
- .asciz ". activating PAE"
- msg_act_longmode:
- .asciz ". activating LongMode"
- success_msg_multiboot:
- .asciz ". multiboot structure found"
- success_msg_cpuid:
- .asciz ". CPUID opcode available"
- success_msg_cpuid_ext:
- .asciz ". CPUID extended functions available"
- success_msg_longmode:
- .asciz ". LongMode available"
- error_msg_no_multiboot:
- .asciz "ERROR: no multiboot structure found -> HALT"
- error_msg_no_cpuid:
- .asciz "ERROR: CPUID opcode not available -> HALT"
- error_msg_no_cpuid_ext:
- .asciz "ERROR: CPUID extended functions not available -> HALT"
- error_msg_no_longmode:
- .asciz "ERROR: LongMode not available -> HALT"
- cursor:
- .int 0xB8000
- multiboot_magic:
- .int 0
- multiboot_pointer:
- .int 0
- .sect .bss
- .align 4096
- pt_lvl4:
- .space 4096
- .align 4096
- pt_lvl3:
- .space 4096
- .align 4096
- pt_lvl2:
- .space 4096 * 4
- .align 4096
- .space 4096
- stack:
- /* here is the 64bit code! */
- .code64
- .sect .text
- .extern loader64
- longmode:
- xorq %rdi, %rdi
- movl multiboot_pointer, %edi
- movl multiboot_magic, %esi
- movl cursor, %edx
- call loader64
- halt64:
- cli
- hlt
- halt64_loop:
- jmp halt64_loop
- .sect .data
- gdt_temp:
- /* NULL descriptor */
- .quad 0
- /* 32bit code descriptor */
- .word 0xFFFF, 0x0000
- .byte 0x00, 0x98, 0xCF, 0x00
- /* 32bit data descriptor */
- .word 0xFFFF, 0x0000
- .byte 0x00, 0x92, 0xCF, 0x00
- /* 64bit descriptor */
- .int 0x00000000
- .byte 0x00, 0x98, 0x20, 0x0
- gdtr_temp:
- .word 4 * 8
- .int gdt_temp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement