/* Copyright (C) 2021 Rain */
/* This file is part of Cunix. */
/*
Cunix is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) and later version.
*/
/*
Cunix is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
/*
You should have received a copy of the GNU General Public License
along with Cunix. If not, see .
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
struct mm mman;
errno_t mem_init(void) {
/* get E820 info (from BIOS) */
int i;
struct e820_entry *p;
p = E820_ADDR;
for (i = 0; i < 32; i++) {
if (p->type && p->type < 5) {
if (p->type == 1)
mman.total_ram += p->len;
p++;
continue;
}
break;
}
printf("total usable RAM: %d MB, total E820 entries: %d\n", mman.total_ram >> 20, i);
/* get total pages */
mman.total_pages = mman.total_ram >> PAGE_2M_SHIFT;
mman.pages_bitman.bitmap = (unsigned char *) (((__uint64_t) &_end) & PAGE_4K_MASK);
memset(mman.pages_bitman.bitmap, 0x00, mman.total_pages >> 3);
printf("total pages: %d pages bitmap: 0x%p\n", mman.total_pages, &_end, mman.pages_bitman.bitmap);
/* first page is in using and it never free */
mman.usable_pages = mman.total_pages - 1;
mman.e820 = E820_ADDR;
mman.e820_entries = i;
bitmap_set(mman.pages_bitman.bitmap, 0, 1);
return 0;
}
void *palloc(void) {
return (void *) (((__uint64_t) bitman_alloc(&mman.pages_bitman)) << PAGE_2M_SHIFT);
}
void pfree(void *addr) {
bitman_free(&mman.pages_bitman, (__uint32_t) (((__uint64_t) addr) >> PAGE_2M_SHIFT));
return;
}