; Start the paging mechanism -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
SetupPaging:
; According to how much memory size calculation should be initialized and PDE and page table
Xor edx, edx
Mov eax, [dwMemSize]
Mov ebx, 400000 h; 400000 h=4 m=4096 * 1024, a page table corresponding to the memory size
Div ebx
Mov ecx, eax; The ecx as the number of page table, namely the number of the PDE should
The test edx, edx
Jz. No_remainder
Inc ecx. If the remainder of 0 to add a page table
No_remainder:
Mov [PageTableNumber], ecx; The staging page table number
; To simplify the process, all the linear address of corresponding physical address. And don't consider the memory is empty.
; First initialize the page directory
Mov ax, SelectorFlatRW
Mov es, ax
Mov edi, PageDirBase0; This section of the first address for PageDirBase0
Xor eax, eax
Mov eax, PageTblBase0 | PG_P | PG_USU | PG_RWW
1:
Stosd
The add eax, 4096; In order to simplify, and all page tables in memory is continuous.
1
loop.
; To initialize all page table
Mov eax, [PageTableNumber]; Page table number
Mov ebx, 1024; Each page table 1024 PTE
The mul ebx
Mov ecx, eax; PTE number=page table number * 1024
Mov edi, PageTblBase0; This section of the first address for PageTblBase0
Xor eax, eax
Mov eax, PG_P | PG_USU | PG_RWW
2:
Stosd
The add eax, 4096; Each page pointing to the space of 4 k
2
loop.
Mov eax, PageDirBase0
Mov cr3, eax,
Mov eax, cr0
The or eax, 80000000 h
Mov cr0, eax
JMP short. 3
3:
The nop
Ret
; Paging mechanism to start over -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
CodePudding user response:
CPU JMP when they transfer instruction to empty the command line like this, if the execution of an instruction directly, then the instruction may be in the mov cr0, eax instructions before they entered the line of the CPU, completed the instruction fetch and decoding process, the instruction if it contains the memory address, because at this time had not yet enabled pages, so the address is linear, and implement the mov cr0, opens the paging mechanism after eax, you should use the virtual memory address address at this moment, so you need to use a JMP instruction will have original instruction to empty into the line