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,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 (;;);
}