Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- BITS 16
- org 0x7e00
- section .code
- BITS 16
- cli
- jmp gdt32Setup
- align 8
- gdt32:
- .Null:
- dq 0000000000000000000000000000000000000000000000000000000000000000b
- .Code:
- ; dq 6 5 4 3 2 1 0
- ; dq 3210987654321098765432109876543210987654321098765432109876543210
- ; dq baseaddrGS00segl1plDXCRAbaseaddressbaseaddressbaSegmentlimitsegm
- dq 0000000011000000100110100000000000000000000000000000000011111111b
- .Data:
- ; dq 6 5 4 3 2 1 0
- ; dq 3210987654321098765432109876543210987654321098765432109876543210
- ; dq baseaddrGS00segl1plDXEWAbaseaddressbaseaddressbaSegmentlimitsegm
- dq 0000000011000000100100100000000000000000000000000000000011111111b
- gdt32Pointer:
- dw $ - gdt32 - 1
- dd gdt32
- gdt32Setup: ; PDP -> PD -> PT -> PE
- ; 500 1500 2500 3500
- ;1000 2000 3000 4000
- mov ax, 0x100
- mov ds, ax
- mov ax, 0x2000 | 0x1
- mov [ds:0], ax ; point to PD at 0x2000
- mov ax, 0x200 ; PD is at 0x2000
- mov ds, ax
- mov ax, 0x3000 | 0x3
- mov [ds:0], ax ; point to PT at 0x3000
- mov ebx, eax
- mov ax, 0x300 ; PT is at 0x3000
- mov ds, ax
- mov ax, 0x400 ; page entries start at 0x5000
- mov es, ax ; offset now 0x3500
- xor esi, esi ; starting address for identity mapping
- xor edi, edi ; offset of page table into 0x3000
- xor ebx, ebx ; offset of page directory into 0x2000
- mov ecx, 511 ; number of pages to assign (two megabytes for now)
- mov ebp, 0x4000 ; page table starts at 0x4500
- .nextPageTable32:
- mov eax, ebp
- or al, 111b ; set present, r/w, user flags
- mov [ds:ebx], eax ; mov our pointer to a page entry into the page table
- add ebx, 8
- add ebp, 8
- mov eax, esi
- or eax, 111b
- .nextPage32:
- mov [es:edi], eax
- add eax, 0x1000
- add esi, 0x1000
- add edi, 8
- sub ecx, 1
- jz .doneMapping32
- test edi, 0xfffff000
- je .nextPageTable32
- jmp .nextPage32
- .doneMapping32:
- mov ax, 0x0
- mov ds, ax
- lgdt [gdt32Pointer]
- mov eax, 0x1000
- mov cr3, eax
- mov eax, cr0
- or eax, 0x00000011 ; set CR0.PE
- mov cr0, eax ; enter protected mode
- jmp 1000b:now_in_prot ; SI=1, TI=0, RPL=0, i.e. 000000000001000b
- BITS 32
- now_in_prot:
- jmp start32 ; should be 0x7eb2
- ALIGN 8
- gdt64:
- .Null:
- dq 0000000000000000000000000000000000000000000000000000000000000000b
- .Code:
- ; dq 6 5 4 3 2 1 0
- ; dq 3210987654321098765432109876543210987654321098765432109876543210
- ; dq baseaddrGDLAsegl1pl11CRAbaseaddressbaseaddressbaSegmentlimitsegm
- ; dq _________0L_____Ppv11C__________________________________________
- dq 0000000000100000100110000000000000000000000000000000000000000000b
- .Data:
- ; dq 6 5 4 3 2 1 0
- ; dq 3210987654321098765432109876543210987654321098765432109876543210
- ; dq baseaddrGS00segl1plDXEWAbaseaddressbaseaddressbaSegmentlimitsegm
- ; dq ________________Ppv___W_________________________________________
- dq 0000000000000000100000100000000000000000000000000000000000000000b
- gdt64Pointer:
- dw $ - gdt64 - 1
- dq gdt64
- start32: ; setup a 64-bit PML4T etc.
- ; PML4T -> PDPT -> PDT -> PT
- ; 0x8000 0x9000 0xa000 0xb000
- mov ax, 0x10 ; select data segment
- mov ds, ax
- mov es, ax
- mov fs, ax
- mov gs, ax
- mov ss, ax
- mov edx, 0x8000
- mov ebx, 0x9000
- mov ecx, ebx ; ecx = phys address of PDPT
- or bl, 111b
- mov [edx], ebx ; move pointer to PDPT into PML4 i.e. 0x9007 intno 0x8000
- mov ebx, 0xa000 ;
- mov ebp, ebx ; ebp = phys address of PDT
- or bl, 111b
- mov [ecx], ebx ; move pointer to PDT into PDPT i.e. 0xa007 into 0x9000
- mov ecx, 0x511 ; allocate 2 megabytes of memory
- xor edx, edx ; page number
- mov esi, 0xa000 ; start address of page directory table
- mov edi, 0xb000 ; start address of page table
- .nextPageTable64:
- mov ebx, edi ; move address of page table into ebx
- or bl, 111b ; set user, write, present
- mov [esi], ebx ; move the PDTE into the PDT
- add esi, 8 ; next PDTE
- mov eax, edx ; move first page number into eax
- or eax, 111b ; set user, write, present
- .nextPage64:
- mov [edi], eax ; move page into page table
- add eax, 0x1000 ; next page
- add edx, 0x1000 ; next address to map
- add edi, 8 ; next address of page table entry
- sub ecx, 1
- je .doneMapping64
- test edi, 0xfffff000
- je .nextPageTable64
- jmp .nextPage64
- .doneMapping64:
- mov eax, cr4 ; enable PAE paging
- bts eax, 5
- mov cr4, eax
- xchg bx, bx
- mov eax, 0x8000 ; move pointer to PML4T
- mov cr3, eax ; store it
- mov ecx, 0xc0000080 ; EFER MSR number
- rdmsr ; read efer
- bts eax, 8 ; set LME=1
- wrmsr ; write efer
- mov eax, cr0
- bts eax, 31
- mov cr0, eax
- jmp 0x08:start64
- BITS 64
- start64: ; should be 0x7f82
- lgdt [gdt64Pointer]
- mov rax, 0x8000
- mov cr3, rax
- halt:
- hlt
- jmp halt
- <bochs:2> info gdt
- Global Descriptor Table (base=0x0000000000007ec8, limit=23):
- GDT[0x00]=??? descriptor hi=0x00000000, lo=0x00000000
- GDT[0x01]=Code segment, base=0x00000000, limit=0x00000000, Execute-Only, 16-bit
- GDT[0x02]=LDT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement