Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [extern _startup64]
  2.  
  3. %define KNL_HIGH_VMA 0xFFFFFFFFC0000000
  4.  
  5. %assign ALIGN    1<<0             ; align loaded modules on page boundaries
  6. %assign MEMINFO  1<<1             ; provide memory map
  7. %assign FLAGS    ALIGN | MEMINFO  ; this is the Multiboot 'flag' field
  8. %assign MAGIC    0x1BADB002       ; 'magic number' lets bootloader find the header
  9. %assign CHECKSUM -(MAGIC + FLAGS) ; checksum of above, to prove we are multiboot
  10.  
  11. section .multiboot
  12.     align 4
  13.     dd MAGIC
  14.     dd FLAGS
  15.     dd CHECKSUM
  16.  
  17. section .rodata
  18.     CODE_USER equ 0x40C3FA000000D090
  19.     DATA_USER equ 0x40C3F2000000D090
  20.     CODE_KERNEL equ 0x00CF9A000000FFFF
  21.     DATA_KERNEL equ 0x00CF92000000FFFF
  22.  
  23.     align 16
  24.  
  25.     gdt64:
  26.         dq 0
  27.         dq CODE_KERNEL
  28.         dq DATA_KERNEL
  29.         dq CODE_USER
  30.         dq DATA_USER
  31.     align 16
  32.  
  33.     global gdt_ptr64
  34.     gdt_ptr64:
  35.         dw $ - gdt64 - 1
  36.         dq gdt64
  37.  
  38.     global gdt_ptr32
  39.     gdt_ptr32:
  40.         dw $ - gdt64 - 1
  41.         dd gdt64 - KNL_HIGH_VMA
  42.  
  43. %macro gen_pd_2mb 3
  44.     %assign i %1
  45.     %rep %2
  46.         dq (i | 0x83)
  47.         %assign i i + 0x200000
  48.     %endrep
  49.     %rep %3
  50.         dq 0
  51.     %endrep
  52. %endmacro
  53.  
  54. section .rodata
  55.     align 4096
  56.     boot_pml4:
  57.         dq boot_pml3_1 + 3
  58.         times 255 dq 0
  59.         dq boot_pml3_phys + 3
  60.         times 254 dq 0
  61.         dq boot_pml3_2 + 3
  62.     boot_pml3_1:
  63.         dq boot_pml2 + 3
  64.         times 511 dq 0
  65.     boot_pml3_2:
  66.         times 510 dq 0
  67.         dq boot_pml2 + 3
  68.         dq 0
  69.     boot_pml3_phys:
  70.         dq boot_pml2_phys + 3
  71.         times 511 dq 0
  72.     boot_pml2:
  73.         gen_pd_2mb 0, 64, 0
  74.         times 448 dq 0
  75.     boot_pml2_phys:
  76.         gen_pd_2mb 0, 512, 0  
  77.  
  78. section .bss
  79.     align 16
  80.     stack_bottom:
  81.         resb 65536
  82.     stack_top:
  83.  
  84. section .text
  85.     align 4
  86.     global reset_seg
  87.     reset_seg:
  88.         mov ax, 0x10
  89.         mov ds, ax
  90.         mov es, ax
  91.         mov fs, ax
  92.         mov gs, ax
  93.         mov ss, ax
  94.  
  95. section .text
  96.     align 4
  97.     global data_seg
  98.     data_seg:
  99.         mov ax, 0x0
  100.         mov ds, ax
  101.         mov es, ax
  102.         mov fs, ax
  103.         mov gs, ax
  104.         mov ss, ax
  105.  
  106. section .text
  107.     [bits 32]
  108.     global _start
  109.  
  110.     _start:
  111.         cli
  112.         mov esp, stack_top
  113.  
  114.         lgdt [gdt_ptr32 - KNL_HIGH_VMA]
  115.  
  116.         mov edi, eax
  117.         mov esi, ebx
  118.  
  119.         mov eax, cr4
  120.         or eax, 0x000000A0
  121.         mov cr4, eax
  122.  
  123.         mov eax, boot_pml4
  124.         mov cr3, eax
  125.  
  126.         mov ecx, 0xC0000080
  127.         rdmsr
  128.         or eax, 0x00000901
  129.         wrmsr
  130.  
  131.         mov eax, cr0
  132.         or eax, (1 << 31)
  133.         mov cr0, eax       
  134.  
  135.         ;jmp 0x08:_mode64 - KNL_HIGH_VMA
  136.         mov eax, _mode64
  137.         push eax
  138.         push 0x08
  139.         retf
  140.  
  141.     [bits 64]
  142.     _mode64:
  143.         mov rax, _higher_half
  144.         jmp rax
  145.  
  146.     _higher_half:
  147.         lgdt [gdt_ptr64] ; hopefully these wont execute
  148.         jmp _startup64
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement