/* Copyright (C) 2021 Rain */
/* This file is part of XNIX. */
/*
XNIX 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.
*/
/*
XNIX 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 XNIX. If not, see .
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
struct sysmm *mm;
struct mempool pool;
void mm_init(void) {
mm = (struct sysmm *) PAGE_4K_ALIGN((__uint64_t) &_end);
struct e820_entry *e820 = E820_ADDR;
struct zone *zones = (struct zone *) (((char *) mm) + sizeof(struct sysmm));
int i;
__uint64_t total_ram = 0;
struct e820_entry *e = e820;
memset(mm, 0x00, sizeof(struct sysmm));
for (i = 0; i < 32; i++) {
if (!e->type || e->type >= E820_INV)
break;
printk(0x00ffffff, 0x00000000, "address %q, length %q KB, type %d\n", e->addr, e->len >> 10, e->type);
/* create a `struct zone` for this */
#define z (zones + mm->total_zones)
z->type = e->type;
z->total_pages = e->len >> PAGE_SHIFT;
z->start_addr = e->addr;
z->end_addr = e->addr + e->len;
z->size = e->len;
z->using = 0;
mm->total_zones++;
if (e->type == E820_RAM)
total_ram += e->len;
mm->total_pages += e->len >> PAGE_SHIFT;
e++;
}
printk(0x00ffffff, 0x00000000, "total usable ram: %d MB. \n", total_ram >> 20);
printk(0x00ffffff, 0x00000000, "total pages: %d (includes unusable pages) = %d MB\n", mm->total_pages, mm->total_pages << PAGE_SHIFT >> 20);
printk(0x00ffffff, 0x00000000, "total zones: %d\n", mm->total_zones);
/* setup page bitmap */
/* first we should allocate some memory for it */
/* at end of zones */
mm->bitmap = (__uint8_t *) (zones + mm->total_zones);
/* set all bits. if this bit points to a RAM page, clear it */
/* it always not bad. */
#define BYTESNEED ((mm->total_pages >> 3) + 1)
memset(mm->bitmap, 0xff, BYTESNEED);
e = e820;
for (i = 0; i < mm->total_zones; i++) {
#undef z
#define z (zones + i)
if (z->type == E820_RAM) {
/* clear bits in bitmap */
int j;
__uint64_t start_page = PAGE_ALIGN(z->start_addr) >> PAGE_SHIFT;
for (j = start_page; j < start_page + z->total_pages; j++)
bitmap_set(mm->bitmap, j, 0);
}
}
/*
* the first and second page is for kernel,
* and the 3rd, 4th, 5th are for frame buffer (they setup in boot/loader.asm).
* */
/* 0MB */
bitmap_set(mm->bitmap, 0, 1);
/* 2MB */
bitmap_set(mm->bitmap, 1, 1);
/* so next free page is 10MB (0xa00000) */
/* now we setup that memory pool */
/* default pool size: 2MB (1 page) */
pool.size = 0x200000;
pool.used = 0;
pool.entries = 0x200000 >> POOL_ENTRY_SHIFT;
/* 32 bytes per bit */
/* we don't want use alloc_page() here, because it just want
* 4 KB, but alloc_page() gives us 2MB.
*
* so we must find a place to store this.
*
* and after that we can use kmalloc() to alloc this.
* (this is only for starting-up)
*
* it's at the end of syslog-buffer.
* */
pool.bitmap = (__uint8_t *) (SYSLOG_BUFFER + 32768);
pool.page = alloc_page();
pool.next = NULL;
return;
#undef z
}
/* find an empty page table entry, and return its number */
void *mapping(void *addr, __uint64_t attr) {
__uint64_t i;
/* well... */
#define PML4E_ADDR ((__uint64_t *) 0x0000)
#define PDPT_ADDR ((__uint64_t *) 0x1000)
#define PT_ADDR ((__uint64_t *) 0x2000);
__uint64_t *pml4e = PML4E_ADDR;
__uint64_t *pdpt = PDPT_ADDR;
__uint64_t *pt = PT_ADDR;
#define TABLE_ENTRIES 512
for (i = 0; ((__uint64_t *) pt)[i]; i++) {
if (i > TABLE_ENTRIES) {
/* reload pt */
pt = (__uint64_t *) *(++pdpt);
if (pdpt == PDPT_ADDR + TABLE_ENTRIES) {
pdpt = (__uint64_t *) *(++pml4e);
pt = (__uint64_t *) *pdpt;
if (pml4e == PML4E_ADDR + TABLE_ENTRIES)
return 0;
}
}
}
/* map it with attribute attr */
pt[i] = (__uint64_t) addr | attr;
/* get virtual address */
#define PT_NR i
#define PDPT_NR ((pdpt - PDPT_ADDR) >> 3)
#define PML4E_NR ((pml4e - PML4E_ADDR) >> 3)
#define TABLE_SHIFT 9ul
__uint64_t vaddr = (PT_NR << PAGE_SHIFT)
+ (PDPT_NR << TABLE_SHIFT << PAGE_SHIFT)
+ (PML4E_NR << TABLE_SHIFT << PAGE_SHIFT << TABLE_SHIFT << PAGE_SHIFT);
return (void *) vaddr;
#undef TABLE_SHIFT
#undef PML4E_NR
#undef PDPT_NR
#undef PT_NR
#undef TABLE_ENTRIES
#undef PT_ADDR
#undef PDPT_ADDR
#undef PML4E_ADDR
}
void *alloc_page(void) {
__uint64_t i;
for (i = 0; bitmap_read(mm->bitmap, i); i++) {
if (i > mm->total_pages)
return NULL;
}
bitmap_set(mm->bitmap, i, 1);
/* we must map it to page-table */
void *addr = mapping((void *) (i << PAGE_SHIFT), PATT_KERNEL);
if (!addr) {
bitmap_set(mm->bitmap, i, 0);
return NULL;
}
return addr;
}
void free_page(void *p) {
__uint64_t i;
i = (__uint64_t) p >> PAGE_SHIFT;
bitmap_set(mm->bitmap, i, 0);
return;
}
void *kmalloc(struct mempool *pool, __uint64_t size) {
if (!size || size > pool->size - pool->used)
return NULL;
__uint64_t points = ((size + 63) & ~63) >> POOL_ENTRY_SHIFT;
__uint64_t i;
if (points == 1) {
for (i = 0; bitmap_read(pool->bitmap, i); i++) {
if (i > pool->entries)
return NULL;
}
bitmap_set(pool->bitmap, i, 1);
pool->used++;
return pool->page + (i << POOL_ENTRY_SHIFT);
}
/* it's not so good... */
for (i = 0; i < pool->entries; i++) {
repeat:
/* if all 0... */
__uint64_t j;
for (j = 0; j < points; j++) {
if (bitmap_read(pool->bitmap, i + j)) {
i++;
goto repeat;
}
}
/* it will here when we found enough */
for (j = 0; j < points; j++)
bitmap_set(pool->bitmap, j, 1);
pool->used += points;
return pool->page + (i << POOL_ENTRY_SHIFT);
}
return NULL;
}
void kfree(struct mempool *pool, void *p, __uint64_t size) {
__uint64_t points = ((size + 63) & ~63) >> POOL_ENTRY_SHIFT;
__uint64_t i;
i = (p - pool->page) >> POOL_ENTRY_SHIFT;
for (; points; points--, i++)
bitmap_set(pool->bitmap, i, 0);
pool->used -= points;
return;
}