Advertisement
lfed255-dev

loader.asm

May 11th, 2019
1,041
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. cat loader.asm
  2. ; GRUB MULTIBOOT 1
  3. MAGIC equ 0x1BADB002 ; multiboot 2 - 0xe85250d6 ; multiboot 1 - 0x1BADB002
  4. ; ARCH equ 0 ; i386
  5. MEMINFO equ 1<<1
  6. MBALIGN equ 1<<0
  7. FLAGS equ 0 | MBALIGN | MEMINFO
  8. HLEN equ __boot_header_end - __boot_header
  9. CHECKSUM equ 0x100000000 - (MAGIC + FLAGS + HLEN)
  10.  
  11. STACK_SIZE equ 60000 ;4096 * 1024 * 1024 + 400
  12.  
  13. ; mem
  14.  
  15. VM_BASE equ 0xC0000000
  16. PDE_INDEX   equ (VM_BASE >> 22)
  17. PSE_BIT     equ 0x00000010
  18. PG_BIT      equ 0x80000000
  19.  
  20.  
  21.  
  22.  
  23. bits 32
  24.  
  25. section .bss
  26.  
  27. align 4
  28.  
  29. stack_end:
  30.         resb STACK_SIZE
  31. stack_top:
  32.  
  33. section .lowerhalf
  34.  
  35. align 4
  36.  
  37. __boot_header:
  38.         dd MAGIC
  39. ;       dd ARCH
  40.         dd FLAGS
  41.         dd -(MAGIC + FLAGS)
  42.         dd HLEN
  43.         dd CHECKSUM
  44.  
  45. ;       dw 0
  46. ;       dw 0
  47. ;       dd 8
  48. __boot_header_end:
  49.  
  50. global TEMP_PG_DIR
  51.  
  52. align 4096
  53. TEMP_PG_DIR:
  54.         ; Map the first 4mb physical memory to first 4mb virtual memory. Otherwise, when paging is enabled, eip points to, 0x100004 for example, and MMU is not gonna know how to translate
  55.     ; this address into phsyical mem address, because our PDE doesn't tell MMU how to find it.
  56.     dd 0x00000083
  57.     times(PDE_INDEX - 1) dd 0
  58.     dd 0x00000083
  59.     times(1024 - PDE_INDEX - 1) dd 0
  60.  
  61. align 4
  62.  
  63. global _loader
  64.  
  65. extern kmain
  66. extern set_up_gdt
  67.  
  68. _loader:
  69. set_up_mem:
  70.     ; update page directory address, since eax and ebx is in use, have to use ecx or other register
  71.     mov ecx, TEMP_PG_DIR
  72.     mov cr3, ecx
  73.  
  74.     ; Enable 4mb pages
  75.     mov ecx, cr4;
  76.     or ecx, PSE_BIT
  77.     mov cr4, ecx
  78.  
  79.     ; Set PG bit, enable paging
  80.     mov ecx, cr0
  81.     or ecx, PG_BIT
  82.     mov cr0, ecx
  83.  
  84.  
  85.  
  86.         finit
  87.         ; sti               - DO NOT enable interrupts until IDT is created!
  88.         mov esp,stack_top
  89.  
  90.         push ebx
  91.         push eax
  92.  
  93.         call set_up_gdt
  94.         call kmain
  95.  
  96.         cli
  97. .stop:
  98.         hlt
  99.         jmp .stop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement