From 0c0d220d3eaf61d6ab0aca6d006c278edc5a0440 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Mon, 15 Mar 2021 22:41:31 +0200 Subject: Map .text and .rodata as read-only --- src/boot.S | 55 +++++++++++++++++++++++++++---------------------------- 1 file changed, 27 insertions(+), 28 deletions(-) (limited to 'src/boot.S') diff --git a/src/boot.S b/src/boot.S index e39883e..0451066 100644 --- a/src/boot.S +++ b/src/boot.S @@ -52,7 +52,24 @@ kernel_ptable0: */ .section .multiboot.text, "ax" .global VADDR_OFFSET -.set VADDR_OFFSET, 0xc0000000 - 0x400000 + .set VADDR_OFFSET, 0xc0000000 - 0x400000 + +.macro map begin, end, access + mov $\begin - VADDR_OFFSET, %esi +1: + cmpl $(\end - VADDR_OFFSET), %esi + jge 2f # move to next section + + movl %esi, %edx # map physical address + orl $\access, %edx # access bits + movl %edx, (%edi) + + addl $4096, %esi # Size of page is 4096 bytes. + addl $4, %edi # Size of entries in kernel_ptable0 is 4 bytes. + loop 1b # Loop to the next entry if we haven't finished. +2: +.endm + .global _start .type _start, @function _start: @@ -60,37 +77,19 @@ _start: # Physical address of kernel_ptable0. movl $(kernel_ptable0 - VADDR_OFFSET), %edi - - # Only map the kernel. - mov $_kernel_start - VADDR_OFFSET, %esi # Map 1024 pages movl $1024, %ecx -l_page_init: - cmpl $(_kernel_end - VADDR_OFFSET), %esi - jge map_table + map begin_text, end_text, 0x001 + map begin_rodata, end_rodata, 0x001 + map begin_data, end_data, 0x003 + map begin_bss, end_bss, 0x003 - /* TODO: Map physical address as "present, writable". Note that this maps .text and .rodata as writable. - Mind security and map them as non-writable. */ - movl %esi, %edx - orl $0x003, %edx - movl %edx, (%edi) - - # Size of page is 4096 bytes. - addl $4096, %esi - # Size of entries in kernel_ptable0 is 4 bytes. - addl $4, %edi - # Loop to the next entry if we haven't finished. - loop l_page_init - -map_table: -/* - Enabling paging does not change the next instruction, which continues to be physical. Therefore, map the kernel - to both its physical address and to the higher half. - Use the page table at: - - entry 1 starts 0x0040 0000 ends 0x007f ffff - - entry 768 starts 0xc000 0000 ends 0xc03f ffff - */ + # Enabling paging does not change the next instruction, which continues to be physical. Therefore, map the kernel + # to both its physical address and to the higher half. + # Use the page table at: + # - entry 1 starts 0x0040 0000 ends 0x007f ffff + # - entry 768 starts 0xc000 0000 ends 0xc03f ffff movl $(kernel_ptable0 - VADDR_OFFSET + 0x003), kernel_pagedir - VADDR_OFFSET + 1 * 4 movl $(kernel_ptable0 - VADDR_OFFSET + 0x003), kernel_pagedir - VADDR_OFFSET + 768 * 4 -- cgit v1.2.1