Working i386 paging and liballoc

This commit is contained in:
2025-12-07 00:53:33 +01:00
commit de5350010b
75 changed files with 6716 additions and 0 deletions

View File

@@ -0,0 +1 @@
*.o

View File

@@ -0,0 +1,95 @@
/*
Copyright 2025 Kamil Kowalczyk
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define F_MEMALIGN (1<<0)
#define F_MEMINFO (1<<1)
#define FLAGS (F_MEMALIGN | F_MEMINFO)
#define MAGIC (0x1BADB002)
#define CHECKSUM (-(MAGIC + FLAGS))
#define VIRT_BASE 0xC0000000
.section .multiboot, "aw"
.align 4
.long MAGIC
.long FLAGS
.long CHECKSUM
.section .bss
.align 16
__stack_bottom:
.skip 16384
__stack_top:
.section .boot, "ax"
.global _start
_start:
movl $(__init_pd - VIRT_BASE), %ecx
movl %ecx, %cr3
movl %cr4, %ecx
orl $0x10, %ecx
movl %ecx, %cr4
movl %cr0, %ecx
orl $0x80000000, %ecx
movl %ecx, %cr0
movl $__kernel_higherhalf, %ecx
jmp *%ecx
.section .text, "ax"
__kernel_higherhalf:
mov $__stack_top, %esp
push %ebx
xorl %ebp, %ebp
call bootmain
1:
hlt
jmp 1b
.section .data
.align 4096
.global __init_pd
__init_pd:
.long 0b10000011
.rept 768-1
.long 0
.endr
.long (0 << 22) | 0b10000011
.long (1 << 22) | 0b10000011
.long (2 << 22) | 0b10000011
.long (3 << 22) | 0b10000011
.rept 256-4
.long 0
.endr

View File

@@ -0,0 +1,103 @@
/*
Copyright 2025 Kamil Kowalczyk
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
“AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <libk/types.h>
#include <libk/util.h>
#include <libk/string.h>
#include <sys/cpu.h>
#include <sys/multiboot.h>
#include <sys/serialdbg.h>
#include <sys/mm.h>
#include <mm/pmm.h>
#include <mm/bba.h>
#include <mm/liballoc.h>
#include <config.h>
extern byte_t __kernel_end;
static void dump_multiboot_header(void) {
struct multiboot *mb = multiboot_get();
dbgf("multiboot=%p\n", mb);
dbgf("multiboot sanity dump:\n");
dbgf("flags=%08x, mem_low=%08x, mem_up=%08x\n",
mb->flags, mb->mem_low, mb->mem_up);
}
static void pmm_init1(void) {
struct multiboot *mb = multiboot_get();
dbgf("__kernel_end=%p, %p\n", &__kernel_end, (uptr_t)&__kernel_end - VIRT_BASE);
usize_t usable_ram_bytes = mb->mem_up * 1024;
dbgf("usable ram = %zu\n", usable_ram_bytes);
uptr_t ram_start = ALIGN_UP((uptr_t)&__kernel_end - VIRT_BASE, 0x1000);
uptr_t ram_end = ALIGN_DOWN(0x100000 + usable_ram_bytes, 0x1000);
usize_t block_size = CFG_I386_PC_PMM_BLOCK_SIZE;
usize_t block_count = (ram_end - ram_start) / block_size;
usize_t pmm_bm_bytes = ALIGN_UP((block_count + 7) / 8, block_size);
uptr_t pmm_bm_baseptr = ram_start;
uptr_t pmm_baseptr = ram_start + pmm_bm_bytes;
usize_t remaining_blocks = (ram_end - pmm_baseptr) / block_size;
dbgf("ram_start=%p, pmm_bm_baseptr=%p, pmm_baseptr=%p\n",
ram_start, pmm_bm_baseptr, pmm_baseptr);
pmm_init(pmm_baseptr, pmm_bm_baseptr, remaining_blocks, block_size);
}
void bootmain(void *mbootptr) {
multiboot_set((struct multiboot *)VIRT(mbootptr));
cpu_init();
#if CFG_I386_PC_DEBUG == CFG_I386_PC_DEBUG_SERIAL
serialdbg_init();
dbgf("dbgf() test...\n");
#endif
dump_multiboot_header();
pmm_init1();
bba_init();
mm_init();
char *string = malloc(12);
memset(string, 0, 12);
strncpy(string, "Hello world", 12);
dbgf("string = %s\n", string);
free(string);
for (;;);
}

View File

@@ -0,0 +1,8 @@
dir_boot := $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
c_files += $(dir_boot)/bootmain.c
S_files += $(dir_boot)/_start.S
o_files += $(dir_boot)/bootmain.o \
$(dir_boot)/_start.o