global page_directory global page_tables global paging_init global paging_finish global KERNEL_MAPPING KERNEL_MAPPING equ 0xC0000000 VGABUF equ 0x000B8000 %define mapped(addr) addr - KERNEL_MAPPING section .bss align 4096 page_directory: resb 1024 * 4 page_tables: resb 1024 * 1024 * 4 section .bootstrap.text align 8 paging_init: extern kernel_start extern kernel_end paging_load: mov edi, mapped(page_tables) mov esi, 0 mov ecx, 1023 paging_cmp: cmp esi, kernel_start jl paging_add cmp esi, mapped(kernel_end) jge paging_map mov edx, esi or edx, 0x003 mov [edi], edx paging_add: add esi, 4096 add edi, 4 loop paging_cmp paging_map: lea eax, mapped(page_directory) push eax extern setup_pgdir call mapped(setup_pgdir) add esp, 0x04 ; map VGA buffer mov dword [mapped(page_tables) + 1023 * 4], VGABUF + 0x003 ; map 8MB for the kernel mov dword [mapped(page_directory) + 0 * 4], mapped(page_tables) + 0x003 mov dword [mapped(page_directory) + 1 * 4], mapped(page_tables) + 0x400000000 + 0x003 mov dword [mapped(page_directory) + 768 * 4], mapped(page_tables) + 0x003 mov dword [mapped(page_directory) + 769 * 4], mapped(page_tables) + 0x400000000 + 0x003 ; tell the MMU where the page dir is mov ecx, mapped(page_directory) mov cr3, ecx ; finally init paging mov ecx, cr0 or ecx, 0x80010000 mov cr0, ecx ret section .text align 8 paging_finish: ; remove the 8MB ident mapping mov dword [page_directory + 0 * 4], 0 mov dword [page_directory + 1 * 4], 0 mov ecx, cr3 mov cr3, ecx ret