Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     BITS 16
  2.     org 0x7e00
  3.  
  4. section .code
  5. BITS 16
  6.     cli
  7.     jmp gdt32Setup
  8.  
  9. align 8
  10. gdt32:
  11. .Null:
  12.     dq 0000000000000000000000000000000000000000000000000000000000000000b
  13. .Code:
  14. ;   dq    6         5         4         3         2         1         0
  15. ;   dq 3210987654321098765432109876543210987654321098765432109876543210
  16. ;   dq baseaddrGS00segl1plDXCRAbaseaddressbaseaddressbaSegmentlimitsegm
  17.     dq 0000000011000000100110100000000000000000000000000000000011111111b
  18. .Data:
  19. ;   dq    6         5         4         3         2         1         0
  20. ;   dq 3210987654321098765432109876543210987654321098765432109876543210
  21. ;   dq baseaddrGS00segl1plDXEWAbaseaddressbaseaddressbaSegmentlimitsegm
  22.     dq 0000000011000000100100100000000000000000000000000000000011111111b
  23. gdt32Pointer:
  24.     dw  $ - gdt32 - 1
  25.     dd  gdt32
  26.  
  27. gdt32Setup:                 ; PDP -> PD -> PT -> PE
  28.                         ; 500   1500  2500  3500
  29.                         ;1000   2000  3000  4000
  30.     mov ax, 0x100
  31.     mov ds, ax
  32.     mov ax, 0x2000 | 0x1
  33.     mov [ds:0], ax              ; point to PD at 0x2000
  34.  
  35.     mov ax, 0x200               ; PD is at 0x2000
  36.     mov ds, ax
  37.     mov ax, 0x3000 | 0x3
  38.     mov [ds:0], ax              ; point to PT at 0x3000
  39.     mov ebx, eax
  40.  
  41.     mov ax, 0x300               ; PT is at 0x3000
  42.     mov ds, ax
  43.     mov ax, 0x400               ; page entries start at 0x5000
  44.     mov es, ax              ; offset now 0x3500
  45.  
  46.     xor esi, esi                ; starting address for identity mapping
  47.     xor edi, edi                ; offset of page table into 0x3000
  48.     xor ebx, ebx                ; offset of page directory into 0x2000
  49.     mov ecx, 511                ; number of pages to assign (two megabytes for now)
  50.     mov ebp, 0x4000             ; page table starts at 0x4500
  51. .nextPageTable32:
  52.     mov eax, ebp
  53.     or al, 111b             ; set present, r/w, user flags
  54.     mov [ds:ebx], eax           ; mov our pointer to a page entry into the page table
  55.     add ebx, 8
  56.     add ebp, 8
  57.     mov eax, esi
  58.     or eax, 111b
  59. .nextPage32:
  60.     mov [es:edi], eax
  61.     add eax, 0x1000
  62.     add esi, 0x1000
  63.     add edi, 8
  64.     sub ecx, 1
  65.     jz .doneMapping32
  66.     test edi, 0xfffff000
  67.     je .nextPageTable32
  68.     jmp .nextPage32
  69.  
  70. .doneMapping32:
  71.     mov ax, 0x0
  72.     mov ds, ax
  73.     lgdt [gdt32Pointer]
  74.  
  75.     mov eax, 0x1000
  76.     mov cr3, eax
  77.  
  78.     mov eax, cr0
  79.     or eax, 0x00000011      ; set CR0.PE
  80.     mov cr0, eax            ; enter protected mode
  81.  
  82.     jmp 1000b:now_in_prot       ; SI=1, TI=0, RPL=0, i.e. 000000000001000b
  83.  
  84. BITS 32
  85. now_in_prot:
  86.     jmp start32         ; should be 0x7eb2
  87.  
  88. ALIGN 8
  89. gdt64:
  90. .Null:
  91.     dq 0000000000000000000000000000000000000000000000000000000000000000b
  92. .Code:
  93. ;   dq    6         5         4         3         2         1         0
  94. ;   dq 3210987654321098765432109876543210987654321098765432109876543210
  95. ;   dq baseaddrGDLAsegl1pl11CRAbaseaddressbaseaddressbaSegmentlimitsegm
  96. ;   dq _________0L_____Ppv11C__________________________________________
  97.     dq 0000000000100000100110000000000000000000000000000000000000000000b
  98. .Data:
  99. ;   dq    6         5         4         3         2         1         0
  100. ;   dq 3210987654321098765432109876543210987654321098765432109876543210
  101. ;   dq baseaddrGS00segl1plDXEWAbaseaddressbaseaddressbaSegmentlimitsegm
  102. ;   dq ________________Ppv___W_________________________________________
  103.     dq 0000000000000000100000100000000000000000000000000000000000000000b
  104.  
  105. gdt64Pointer:
  106.     dw  $ - gdt64 - 1
  107.     dq  gdt64
  108.  
  109.  
  110.  
  111. start32:                ; setup a 64-bit PML4T etc.
  112.                     ; PML4T -> PDPT -> PDT -> PT
  113.                     ; 0x8000  0x9000 0xa000 0xb000
  114.     mov ax, 0x10            ; select data segment
  115.     mov ds, ax
  116.     mov es, ax
  117.     mov fs, ax
  118.     mov gs, ax
  119.     mov ss, ax
  120.  
  121.     mov edx, 0x8000
  122.     mov ebx, 0x9000
  123.  
  124.     mov ecx, ebx            ; ecx = phys address of PDPT
  125.     or bl, 111b
  126.     mov [edx], ebx          ; move pointer to PDPT into PML4 i.e. 0x9007 intno 0x8000
  127.  
  128.     mov ebx, 0xa000         ;
  129.     mov ebp, ebx            ; ebp = phys address of PDT
  130.     or bl, 111b
  131.     mov [ecx], ebx          ; move pointer to PDT into PDPT i.e. 0xa007 into 0x9000
  132.  
  133.     mov ecx, 0x511          ; allocate 2 megabytes of memory
  134.     xor edx, edx            ; page number
  135.     mov esi, 0xa000         ; start address of page directory table
  136.     mov edi, 0xb000         ; start address of page table
  137.  
  138. .nextPageTable64:
  139.     mov ebx, edi            ; move address of page table into ebx
  140.     or bl, 111b         ; set user, write, present
  141.     mov [esi], ebx          ; move the PDTE into the PDT
  142.     add esi, 8          ; next PDTE
  143.     mov eax, edx            ; move first page number into eax
  144.     or eax, 111b            ; set user, write, present
  145. .nextPage64:
  146.     mov [edi], eax          ; move page into page table
  147.     add eax, 0x1000         ; next page
  148.     add edx, 0x1000         ; next address to map
  149.     add edi, 8          ; next address of page table entry
  150.     sub ecx, 1
  151.     je .doneMapping64
  152.     test edi, 0xfffff000
  153.     je .nextPageTable64
  154.     jmp .nextPage64
  155.  
  156. .doneMapping64:
  157.     mov eax, cr4            ; enable PAE paging
  158.     bts eax, 5
  159.     mov cr4, eax
  160.  
  161.     xchg bx, bx
  162.  
  163.     mov eax, 0x8000         ; move pointer to PML4T
  164.     mov cr3, eax            ; store it
  165.  
  166.     mov ecx, 0xc0000080     ; EFER MSR number
  167.     rdmsr               ; read efer
  168.     bts eax, 8          ; set LME=1
  169.     wrmsr               ; write efer
  170.  
  171.     mov eax, cr0
  172.     bts eax, 31
  173.     mov cr0, eax
  174.  
  175.     jmp 0x08:start64
  176.  
  177. BITS 64
  178. start64:                ; should be 0x7f82
  179.     lgdt [gdt64Pointer]
  180.     mov rax, 0x8000
  181.     mov cr3, rax
  182.  
  183. halt:
  184.     hlt
  185.     jmp halt
  186.  
  187. <bochs:2> info gdt
  188. Global Descriptor Table (base=0x0000000000007ec8, limit=23):
  189. GDT[0x00]=??? descriptor hi=0x00000000, lo=0x00000000
  190. GDT[0x01]=Code segment, base=0x00000000, limit=0x00000000, Execute-Only, 16-bit
  191. GDT[0x02]=LDT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement