You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.0 KiB
C
75 lines
2.0 KiB
C
/* Copyright (C) 2021 fef <owo@fef.moe>. All rights reserved. */
|
|
|
|
#include <gay/kprintf.h>
|
|
#include <gay/mm.h>
|
|
#include <gay/types.h>
|
|
#include <gay/util.h>
|
|
|
|
extern void _image_start_phys;
|
|
extern void _image_end_phys;
|
|
|
|
/* these are initialized by pages_init() */
|
|
void *kheap_start;
|
|
void *kheap_end;
|
|
|
|
int kmalloc_init(uintptr_t _phys_start, uintptr_t _phys_end)
|
|
{
|
|
phys_start = _phys_start;
|
|
phys_end = _phys_end;
|
|
|
|
/*
|
|
* The kernel image is very likely gonna be within the physical memory
|
|
* range, so we're gonna need to do some cropping in order to not hand
|
|
* out pages that actually contain kernel code.
|
|
* Furthermore, somebody should probably clean up this mess somehow.
|
|
*/
|
|
uintptr_t image_start_phys = (uintptr_t)&_image_start_phys;
|
|
uintptr_t image_end_phys = (uintptr_t)&_image_end_phys;
|
|
if (phys_start < image_start_phys && phys_end > image_start_phys) {
|
|
if (image_start_phys - phys_start > phys_end - image_start_phys)
|
|
phys_end = image_start_phys;
|
|
else
|
|
phys_start = image_end_phys;
|
|
}
|
|
if (phys_start < image_end_phys && _phys_end > image_end_phys) {
|
|
if (image_end_phys - phys_start > phys_end - image_end_phys)
|
|
phys_end = image_start_phys;
|
|
else
|
|
phys_start = image_end_phys;
|
|
}
|
|
|
|
phys_start = align_ceil(phys_start, HUGEPAGE_SIZE);
|
|
/*
|
|
* This is intentionally not aligned to hugepages, because __early_get_page()
|
|
* shrinks it in single PAGE_SIZE steps whenever it is called anyway.
|
|
* I know, this is a terrible hack, but it will be aligned to a hugepage
|
|
* from within pages_init(), right after the entire physical memory has
|
|
* been mapped to the direct area (which is the only reason we need to
|
|
* be able to allocate pages before the page frame allocator is set up
|
|
* in the first place).
|
|
*/
|
|
phys_end = align_floor(phys_end, PAGE_SIZE);
|
|
|
|
int err = pages_init();
|
|
if (err)
|
|
return err;
|
|
|
|
slab_init();
|
|
return 0;
|
|
}
|
|
|
|
__weak void *malloc(usize size)
|
|
{
|
|
return kmalloc(size, M_KERN);
|
|
}
|
|
|
|
__weak void free(void *ptr)
|
|
{
|
|
kfree(ptr);
|
|
}
|
|
|
|
/*
|
|
* Looking for kmalloc() and kfree()?
|
|
* Those two are in slab.c for purely organizational reasons.
|
|
*/
|