Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [BITS 32]
  2.  
  3. MODULEALIGN     equ 1<<0
  4. MEMINFO         equ 1<<1
  5. AOUTKLUDGE      equ 1<<16
  6. FLAGS           equ MODULEALIGN | MEMINFO | AOUTKLUDGE
  7. MAGIC           equ 0x1badb002
  8. CHECKSUM        equ -(MAGIC + FLAGS)
  9.  
  10. section .text
  11. align 4
  12. MULTIBOOTHEADER:
  13. MultiBootHeader:
  14.         dd MAGIC
  15.         dd FLAGS
  16.         dd CHECKSUM
  17.  
  18.         dd MULTIBOOTHEADER - 0x00000010 + 0x00100000
  19.         dd $$ - 0x00000010 + 0x00100000
  20.         dd BOOTLOADER_END - 0x00000010 + 0x00100000
  21.         dd BSS_END - 0x00000010 + 0x00100000
  22.         dd START - 0x00000010 + 0x00100000
  23.  
  24. STACKSIZE equ 0x1000
  25.  
  26. START:
  27.         mov eax, cr0                                    ; disable old paging before we set up our new one
  28.         and eax, 01111111111111111111111111111111b
  29.         mov cr0, eax
  30.  
  31.         mov edi, 0x1000                                 ; clear space for the paging tables
  32.         mov cr3, edi                                    ; set cr3 to edi ?????????????
  33.         xor eax, eax
  34.         mov ecx, 4096
  35.         rep stosd
  36.         mov edi, cr3                                    ; edi now contains address of PML4T
  37.  
  38.         mov dword[edi], 0x2003                          ; set PML4T to point to PDPT
  39.         add edi, 0x1000
  40.  
  41.         mov dword[edi], 0x3003                          ; set PDPT to point to PDT
  42.         add edi, 0x1000
  43.  
  44.         mov dword[edi], 0x4003                          ; set PDT to point to PT
  45.         add edi, 0x1000
  46.  
  47.         mov ebx, 0x3
  48.         mov ecx, 512
  49.  
  50. .setEntry:                                              ; we want to map the first two MiB
  51.         mov dword[edi], ebx                             ; 0x0003 on first run i.e. present and r/w
  52.         add ebx, 0x1000                                 ; move to next page
  53.         add edi, 8                                      ; move to next entry in PT
  54.         loop .setEntry                                  ; and loop until we are done
  55.  
  56.         mov eax, cr4                                    ; enable PAE-paging
  57.         or eax, 1<<5
  58.         mov cr4, eax
  59.  
  60.         mov ecx, 0xc0000080                             ; set the LM-bit
  61.         rdmsr
  62.         or eax, 1<<8
  63.         wrmsr
  64.  
  65.         mov eax, cr0                                    ; re-enable paging from when we disabled it at the beginning
  66.         or eax, 1<<31
  67.         mov cr0, eax
  68.  
  69. ;       jmp $
  70.  
  71.         lgdt [GDT64.Pointer]                            ; load our simple GDT
  72.  
  73. ;       jmp $
  74.  
  75.         jmp GDT64.Code:start64                          ; and jump into 64-bit code
  76.  
  77. GDT64:                                                  ; Global Descriptor Table (64-bit).
  78. .Null: equ $ - GDT64                                    ; The null descriptor.
  79.         dw 0                                            ; Limit (low).
  80.         dw 0                                            ; Base (low).
  81.         db 0                                            ; Base (middle)
  82.         db 0                                            ; Access.
  83.         db 0                                            ; Granularity.
  84.         db 0                                            ; Base (high).
  85.         .Code: equ $ - GDT64                            ; The code descriptor.
  86.     dw 0                         ; Limit (low).
  87.     dw 0                         ; Base (low).
  88.     db 0                         ; Base (middle)
  89.     db 10011000b                 ; Access.
  90.     db 00100000b                 ; Granularity.
  91.     db 0                         ; Base (high).
  92. ;       dq 00000000b,0b,0b,1b,0b,0000b,1b,00b,1b,1b,0b,0b,0b,00000000b,00000000b,00000000b,00000000b,00000000b
  93. .Data: equ $ - GDT64                                    ; The data descriptor.
  94.         dq 0x0000900000000000
  95. .Pointer:                                               ; The GDT-pointer.
  96.         dw $ - GDT64 - 1                                ; Limit.
  97.         dq GDT64                                        ; Base.
  98.  
  99. [BITS 64]
  100. start64:
  101.         cli
  102.         mov ax, GDT64.Data
  103.         mov ds, ax
  104.         mov es, ax
  105.         mov fs, ax
  106.         mov gs, ax
  107.         mov edi, 0xb8000
  108.         mov rax, 0x1f201f201f201f20
  109.         mov ecx, 500
  110.         rep movsq
  111. stublet:
  112.         hlt
  113.         jmp stublet
  114.  
  115. BOOTLOADER_END:
  116. BSS_START:
  117. section .bss
  118. align 4
  119. stack:
  120.         resb STACKSIZE
  121. BSS_END:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement