From 61a9e1c96598dcff002b676137f36f81d4eb74a8 Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Fri, 26 Aug 2022 13:56:39 +0200 Subject: [PATCH] Page calculation --- Makefile | 25 ++- mbr.asm => stage1/main.asm | 5 +- bios_print.asm => stage1/print.asm | 0 stage2/main.asm | 94 ++++++++++ stage2/mmap.asm | 60 +++++++ stage2/paging.asm | 76 +++++++++ setup.asm => stage2/vesa.asm | 199 ++-------------------- colors.asm => stage3/colors.asm | 0 framebuffer.asm => stage3/framebuffer.asm | 18 +- main.asm => stage3/main.asm | 8 +- memory.asm => stage3/memory.asm | 0 stage3/paging.asm | 199 ++++++++++++++++++++++ 12 files changed, 477 insertions(+), 207 deletions(-) rename mbr.asm => stage1/main.asm (96%) rename bios_print.asm => stage1/print.asm (100%) create mode 100644 stage2/main.asm create mode 100644 stage2/mmap.asm create mode 100644 stage2/paging.asm rename setup.asm => stage2/vesa.asm (59%) rename colors.asm => stage3/colors.asm (100%) rename framebuffer.asm => stage3/framebuffer.asm (88%) rename main.asm => stage3/main.asm (83%) rename memory.asm => stage3/memory.asm (100%) create mode 100644 stage3/paging.asm diff --git a/Makefile b/Makefile index 850f293..22c47b6 100644 --- a/Makefile +++ b/Makefile @@ -1,30 +1,29 @@ -OBJS = main.o framebuffer.o memory.o -DEV=/dev/sdb +STAGE3 = stage3/main.o stage3/framebuffer.o stage3/memory.o stage3/paging.o nyax.img: stage1.out stage2.out stage3.out cat stage{1,2,3}.out > nyax.img -stage1.out: mbr.asm stage2.out stage3.out - nasm -f bin mbr.asm -o stage1.out \ +stage1.out: stage1/main.asm stage1/print.asm stage2.out stage3.out + nasm -f bin stage1/main.asm -o stage1.out \ -dKSIZE=$$(du -cb stage{2,3}.out | tail -n1 | cut -f1) -stage2.out: setup.asm - nasm -f bin setup.asm -o stage2.out +stage2.out: stage2/main.asm stage2/mmap.asm stage2/paging.asm stage2/vesa.asm stage1/print.asm + nasm -f bin stage2/main.asm -o stage2.out -stage3.out: $(OBJS) stage2.out - ld -o stage3.out --oformat binary $(OBJS) \ +stage3.out: $(STAGE3) stage2.out + ld -o stage3.out --oformat binary $(STAGE3) \ -Ttext $$(printf "%x\n" $$(echo $$(du -b stage2.out | cut -f1)+32256 | bc)) -%.o: %.asm +stage3/%.o: stage3/%.asm nasm -f elf64 $< -o $@ -.PHONY: run +.PHONY: run clean flash + run: nyax.img bochs -q -.PHONY: clean clean: - rm -rf *.o *.out *.img + rm -rf stage3/*.o *.out *.img flash: nyax.img - dd if=./nyax.img of=$(DEV) + dd if=nyax.img of=$(DEV) diff --git a/mbr.asm b/stage1/main.asm similarity index 96% rename from mbr.asm rename to stage1/main.asm index 6434276..97ff7c7 100644 --- a/mbr.asm +++ b/stage1/main.asm @@ -49,13 +49,12 @@ load_stages: .fail: mov ebx, .fail_msg call print_str - jmp $ + hlt .msg: db "loading stage2 and stage3 from disk", 10, 13, 0 .fail_msg: db "disk failure, try rebooting", 10, 13, 0 - -%include "bios_print.asm" +%include "stage1/print.asm" times 510-($-$$) db 0 dw 0xAA55 diff --git a/bios_print.asm b/stage1/print.asm similarity index 100% rename from bios_print.asm rename to stage1/print.asm diff --git a/stage2/main.asm b/stage2/main.asm new file mode 100644 index 0000000..1e3ab94 --- /dev/null +++ b/stage2/main.asm @@ -0,0 +1,94 @@ +[org 0x7E00] + +%define PAGETABLE 0x1000 +%define VESAINFO 0x0500 +%define VESAMODE VESAINFO+512 +%define OWNMODE VESAMODE+256 +%define GFXINFO PAGETABLE-10 +%define MEMMAPCNT GFXINFO-2 +%define MEMMAP 0x500 + +setup: + ; print message + mov ebx, .msg + call print_str + + ; setup VESA + ; call vesa + + ; get extended memory map + call mmap + + ; build page table + call paging + + ; jump into long mode + jmp 0x0008:long_mode + +.msg: + db 10, 13, "nyax stage2", 10, 13, 0 + +%include "stage2/vesa.asm" +%include "stage2/mmap.asm" +%include "stage2/paging.asm" +%include "stage1/print.asm" + +; modify eax, ebx, ecx, edx +print_hex: + mov ebx, 0x10 + jmp print_num +print_dec: + mov ebx, 10 +print_num: + xor ecx, ecx +.convert: + inc ecx + xor edx, edx + div ebx + cmp dl, 10 + jb .digit + add dl, 'A'-10 + jmp .next +.digit: + add dl, '0' +.next: + push dx + cmp eax, 0 + jne .convert +.print: + cmp ecx, 0 + je .return + dec ecx + pop ax + mov ah, 0x0E + int 0x10 + jmp .print +.return: + ret + +newline: + mov al, 10 + call print_chr + + mov al, 13 + call print_chr + + ret + +print_chr: + mov ah, 0x0E + int 0x10 + ret + +[bits 64] + +long_mode: + ; setup segment registers + mov ax, 0x0010 + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax + mov ss, ax + + ; kernel begins here diff --git a/stage2/mmap.asm b/stage2/mmap.asm new file mode 100644 index 0000000..15c81f8 --- /dev/null +++ b/stage2/mmap.asm @@ -0,0 +1,60 @@ +%define MAPMAGIC 0x534D4150 + +mmap: + mov ebx, .msg + call print_str + + xor ebx, ebx ; counter for interrupt + mov di, MEMMAP + +.loop: + ; issue an INT 0x15, EAX = 0xE820 interrupt + mov eax, 0xE820 + mov ecx, 24 + mov edx, MAPMAGIC + int 0x15 + + cmp eax, MAPMAGIC ; detect failure + jne .fail + + mov eax, [di+16] + cmp eax, 2 + ja .next + + mov eax, [di+4] + cmp eax, 0 + jne .keep + + mov eax, [di+0] + cmp eax, 0x100000 + jb .next + +.keep: + add di, 24 + +.next: + cmp ebx, 0 + jne .loop + + mov ax, di + sub ax, MEMMAP + xor dx, dx + mov bx, 24 + div bx + mov [MEMMAPCNT], ax + + mov eax, MEMMAPCNT + call print_dec + call newline + ;jmp $ + + ret + +.fail: + mov ebx, .fail_msg + call print_str + jmp $ + +.msg: db "getting extended memory map", 10, 13, 0 +.fail_msg: db "memory map failure", 10, 13, 0 + diff --git a/stage2/paging.asm b/stage2/paging.asm new file mode 100644 index 0000000..9254d86 --- /dev/null +++ b/stage2/paging.asm @@ -0,0 +1,76 @@ +paging: + ; print message + mov ebx, .msg + call print_str + + ; clear 4 levels of page maps + mov di, PAGETABLE+0x0000 +.clr_buf: + mov byte[di], 0 + inc di + cmp di, PAGETABLE+0x4000 + jne .clr_buf + + ; init 3 page map levels + mov dword[PAGETABLE+0x0000], PAGETABLE+0x1003 + mov dword[PAGETABLE+0x1000], PAGETABLE+0x2003 + mov dword[PAGETABLE+0x2000], PAGETABLE+0x3003 + + ; fill up level 4 page map + mov eax, 3 + mov di, PAGETABLE+0x3000 +.build_pt: + mov [di], eax + add eax, 0x1000 + add di, 8 + cmp eax, 0x100000 + jb .build_pt + ; enable paging and long mode + + mov di, PAGETABLE + + mov al, 0xFF + out 0xA1, al + out 0x21, al + + nop + nop + + lidt [.idt] + + mov eax, 0b10100000 + mov cr4, eax + + mov edx, edi + mov cr3, edx + + mov ecx, 0xC0000080 + rdmsr + + or eax, 0x00000100 + wrmsr + + mov ebx, cr0 + or ebx, 0x80000001 + mov cr0, ebx + + lgdt [.gdt_pointer] + + ret + +.gdt: + dq 0 + dq 0x00209A0000000000 + dq 0x0000920000000000 + dw 0 + +.gdt_pointer: + dw $ - .gdt - 1 + dd .gdt + +.idt: + dw 0 + dd 0 + +.msg: + db "building page table", 10, 13, 0 diff --git a/setup.asm b/stage2/vesa.asm similarity index 59% rename from setup.asm rename to stage2/vesa.asm index 0351501..5335896 100644 --- a/setup.asm +++ b/stage2/vesa.asm @@ -1,32 +1,3 @@ -[org 0x7E00] - -%define PAGETABLE 0x1000 -%define VESAINFO 0x0500 -%define VESAMODE VESAINFO+512 -%define OWNMODE VESAMODE+256 -%define GFXINFO 0x500 - -setup: - ; print message - mov ebx, .msg - call print_str - - ; setup VESA - call vesa - - ; get extended memory map - call mmap - - ; build page table - call paging - - ; jump into long mode - jmp 0x0008:long_mode - -.msg: - db 10, 13, "nyax stage2", 10, 13, 0 - - vesa: ; print message mov ebx, .msg @@ -79,7 +50,7 @@ vesa: cmp al, 32 jne .mode_next - push ebx ; print_num and print_str modify ebx + push ebx ; print_dec and print_str modify ebx mov eax, esi mov ebx, 12 @@ -89,9 +60,6 @@ vesa: mov [edi+10], cx ; copy mode - mov eax, edi - call print_num - ; print selector mov al, '[' call print_chr @@ -111,14 +79,14 @@ vesa: movzx eax, word[VESAMODE+18] ; copy width mov [edi+2], ax - call print_num + call print_dec mov al, 'x' call print_chr movzx eax, word[VESAMODE+20] ; copy height mov [edi+4], ax - call print_num + call print_dec call newline mov eax, [VESAMODE+40] ; copy framebuffer @@ -160,10 +128,6 @@ vesa: jmp .input .valid: - mov eax, edi - call print_num - call newline - ; convert selected number to address mov eax, edi mov ebx, 12 @@ -171,18 +135,25 @@ vesa: add eax, OWNMODE ; copy to final gfx info location - mov ebx, [eax] - mov [GFXINFO], ebx + mov ebx, [eax+0] + mov [GFXINFO+0], ebx mov ebx, [eax+4] mov [GFXINFO+4], ebx - mov bx, [eax+6] - mov [GFXINFO+6], bx + mov bx, [eax+8] + mov [GFXINFO+8], bx + + ;mov edi, eax + ;mov eax, [edi+6] + ;call print_hex + ;call newline + ;mov eax, edi + ;jmp $ ; set mode mov bx, [eax+10] ; video mode in bx (first 13 bits) - or bx, 0b0100000000000000 ; set bit 14: enable linear frame buffer + or bx, 1 << 14 ; set bit 14: enable linear frame buffer and bx, 0b0111111111111111 ; clear deprecated bit 15 mov ax, 0x4F02 ; set VBE mode int 0x10 @@ -215,143 +186,3 @@ vesa: .fail: call print_str jmp $ - - -mmap: - mov ebx, .msg - call print_str - - ret - -.msg: db "getting extended memory map", 10, 13, 0 - - -paging: - ; print message - mov ebx, .msg - call print_str - - ; clear 4 levels of page maps - mov di, PAGETABLE+0x0000 -.clr_buf: - mov byte[di], 0 - inc di - cmp di, PAGETABLE+0x4000 - jne .clr_buf - - ; init 3 page map levels - mov dword[PAGETABLE+0x0000], PAGETABLE+0x1003 - mov dword[PAGETABLE+0x1000], PAGETABLE+0x2003 - mov dword[PAGETABLE+0x2000], PAGETABLE+0x3003 - - ; fill up level 4 page map - mov eax, 3 - mov di, PAGETABLE+0x3000 -.build_pt: - mov [di], eax - add eax, 0x1000 - add di, 8 - cmp eax, 0x100000 - jb .build_pt - - ; enable paging and long mode - - mov di, PAGETABLE - - mov al, 0xFF - out 0xA1, al - out 0x21, al - - nop - nop - - lidt [.idt] - - mov eax, 0b10100000 - mov cr4, eax - - mov edx, edi - mov cr3, edx - - mov ecx, 0xC0000080 - rdmsr - - or eax, 0x00000100 - wrmsr - - mov ebx, cr0 - or ebx, 0x80000001 - mov cr0, ebx - - lgdt [.gdt_pointer] - - ret - -.gdt: - dq 0 - dq 0x00209A0000000000 - dq 0x0000920000000000 - dw 0 - -.gdt_pointer: - dw $ - .gdt - 1 - dd .gdt - -.idt: - dw 0 - dd 0 - -.msg: - db "building page table", 10, 13, 0 - - -%include "bios_print.asm" - -; uses eax, ebx, ecx, edx -print_num: - mov ebx, 10 - xor ecx, ecx -.convert: - inc ecx - xor edx, edx - div ebx - add dl, '0' - push dx - cmp eax, 0 - jne .convert -.print: - cmp ecx, 0 - je .return - dec ecx - pop ax - mov ah, 0x0E - int 0x10 - jmp .print -.return: - ret - - -newline: - mov al, 10 - call print_chr - - mov al, 13 - call print_chr - - ret - -print_chr: - mov ah, 0x0E - int 0x10 - ret - -[bits 64] - -long_mode: - ; setup segment registers - mov ax, 0x0010 - mov ds, ax - mov es, ax - mov fs, ax - mov gs, ax - mov ss, ax diff --git a/colors.asm b/stage3/colors.asm similarity index 100% rename from colors.asm rename to stage3/colors.asm diff --git a/framebuffer.asm b/stage3/framebuffer.asm similarity index 88% rename from framebuffer.asm rename to stage3/framebuffer.asm index 4b73998..6ba7441 100644 --- a/framebuffer.asm +++ b/stage3/framebuffer.asm @@ -1,5 +1,5 @@ -%include "colors.asm" -global print_chr, print_str, print_num, clear_screen +%include "stage3/colors.asm" +global print_chr, print_str, print_dec, print_hex, clear_screen, newline extern memcpy section .data @@ -114,15 +114,25 @@ print_str: .return: ret +print_hex: + mov rsi, 0x10 + jmp print_num +print_dec: + mov rsi, 10 print_num: mov rax, rdi - mov r10, 10 xor rcx, rcx .convert: inc rcx xor rdx, rdx - div r10 + div rsi + cmp dl, 10 + jb .digit + add dl, 'A'-10 + jmp .next +.digit: add dl, '0' +.next: push rdx cmp rax, 0 jne .convert diff --git a/main.asm b/stage3/main.asm similarity index 83% rename from main.asm rename to stage3/main.asm index a327628..7e1ecbd 100644 --- a/main.asm +++ b/stage3/main.asm @@ -1,5 +1,5 @@ global _start -extern print_str, print_num, print_chr, clear_screen +extern print_str, print_dec, print_chr, clear_screen, paging section .data @@ -16,6 +16,8 @@ section .text _start: call clear_screen + call paging + mov rdi, headline call print_str @@ -23,12 +25,12 @@ _start: call print_str xor rdi, rdi - .loop: +.loop: push rdi mov dil, 13 call print_chr mov rdi, [rsp] - call print_num + call print_dec pop rdi inc rdi jmp .loop diff --git a/memory.asm b/stage3/memory.asm similarity index 100% rename from memory.asm rename to stage3/memory.asm diff --git a/stage3/paging.asm b/stage3/paging.asm new file mode 100644 index 0000000..3b4d364 --- /dev/null +++ b/stage3/paging.asm @@ -0,0 +1,199 @@ +global paging +extern print_hex, print_chr, newline, print_dec, print_str +global paging + +section .data + +pagebuf: + .ptr: dq 0x5000 + .size: dq 0x3000 + .used: dq 0 + +section .text +alloc: + mov rdi, .msg + call print_str + jmp $ +.msg: db "cock", 10, 0 + +tables: +; level 4 + mov rax, 0xfff + not rax ; offset mask + + mov rbx, -1 ; low bits mask + shl rbx, 3 ; + + xor rcx, rcx + + mov r14, r10 + mov r13, r10 + mov r12, r10 + mov r11, r10 + + not rcx ; negate remainder mask + and r14, rcx ; apply remainder mask + mov rcx, -1 ; reset remainder mask + shl rcx, 12+9+9+9 ; update remainder mask + + shr r14, 12+9+9+9-3 ; divide + and r14, rbx ; clear lower bits + + mov rdx, 0x1000 ; offset + and rdx, rax ; offset mask + add r14, rdx ; add offset + + not rcx ; negate remainder mask + and r13, rcx ; apply remainder mask + mov rcx, -1 ; reset remainder mask + shl rcx, 12+9+9 ; update remainder mask + + shr r13, 12+9+9-3 ; divide + and r13, rbx ; clear lower bits + + mov rdx, [r14] ; offset + jnz .exist3 + call alloc +.exist3: + and rdx, rax ; offset mask + add r13, rdx ; add offset + + + not rcx ; negate remainder mask + and r12, rcx ; apply remainder mask + mov rcx, -1 ; reset remainder mask + shl rcx, 12+9 ; update remainder mask + + shr r12, 12+9-3 ; divide + and r12, rbx ; clear lower bits + + mov rdx, [r13] ; offset + jnz .exist2 + call alloc +.exist2: + and rdx, rax ; offset mask + add r12, rdx ; add offset + + + not rcx ; negate remainder mask + and r11, rcx ; apply remainder mask + + mov rcx, -1 ; reset remainder mask + shl rcx, 12 ; update remainder mask + + shr r11, 12-3 ; divide + and r11, rbx ; clear lower bits + + mov rdx, [r12] ; offset + jnz .exist1 + call alloc +.exist1: + and rdx, rax ; offset mask + add r11, rdx ; add offset + + ret + +; level1 + mov rax, r11 + xor rdx, rdx + mov rbx, 8 + mul rbx + mov r11, rax + add r11, [r12] + sub r11, 3 + + ret + +space: + mov dil, ' ' + jmp print_chr + +paging: + mov r8, 0x0500 ; start of extended memory map + movzx r9, word[0x1000-10-2] ; number of map entries + + mov r15, pagebuf + +.loop: + ;mov r10, [r8] + ;call tables + + mov r10, 0xfffff + call tables + + mov rdi, r14 + call print_hex + call space + + mov rdi, r13 + call print_hex + call space + + mov rdi, r12 + call print_hex + call space + + mov rdi, r11 + call print_hex + call space + + mov rdi, [r11] + call print_hex + + jmp $ + + mov rdi, r12 + call print_hex + call space + + mov rdi, r11 + call print_hex + call space + + mov rdi, r10 + call print_hex + call space + + + + call newline + + jmp $ + + ;jmp $ + + ;mov rcx, 1 << 63 + ;or rdi, rcx + ;call print_hex + + ;mov dil, ' ' + ;call print_chr + + ;mov rax, [rsp] + ;mov rdi, [rax+8] + + ;mov rcx, 1 << 63 + ;or rdi, rcx + ;call print_hex + + ;mov dil, ' ' + ;call print_chr + + ; mov rax, [rsp] + ;xor rdi, rdi + ;mov edi, [rax+16] + ;call print_dec + + ;call newline + + ;pop rax + add r8, 24 + + ;pop rbx + + dec r9 + jnz .loop + + jmp $ + + ret -- 2.44.0