2017-11-05 01:56:29 -05:00
|
|
|
#include "memory.h"
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <kernelmq/stdlib.h>
|
|
|
|
|
|
|
|
#define FRAMES_COUNT (PAGE_DIR_SIZE * PAGE_TABLE_SIZE) // / sizeof(unsigned char))
|
|
|
|
|
|
|
|
static unsigned char frames[FRAMES_COUNT];
|
|
|
|
|
|
|
|
static void mark_used(unsigned long base, unsigned long limit);
|
|
|
|
|
|
|
|
void memory_initialize(const struct KernelMQ_Info *const kinfo)
|
|
|
|
{
|
|
|
|
kmemset(frames, 0, sizeof(frames));
|
|
|
|
|
2017-11-05 02:32:32 -05:00
|
|
|
mark_used(0, MEM_UPPER_BASE - 1);
|
|
|
|
|
2017-11-05 01:56:29 -05:00
|
|
|
mark_used(kinfo->kernel_phys_base, kinfo->kernel_phys_limit);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < kinfo->modules_count; ++i) {
|
|
|
|
const struct KernelMQ_Info_Module *const module = &kinfo->modules[i];
|
|
|
|
|
|
|
|
mark_used(module->base, module->limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < kinfo->areas_count; ++i) {
|
|
|
|
const struct KernelMQ_Info_Area *const area = &kinfo->areas[i];
|
|
|
|
|
|
|
|
if (!area->is_available) {
|
|
|
|
mark_used(area->base, area->limit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long memory_alloc_page()
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < FRAMES_COUNT; ++i) {
|
|
|
|
if (!frames[i]) {
|
2017-11-05 02:32:32 -05:00
|
|
|
frames[i] = 0xFF;
|
2017-11-05 01:56:29 -05:00
|
|
|
return i * PAGE_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-05 03:49:02 -05:00
|
|
|
unsigned long memory_alloc_big_page()
|
|
|
|
{
|
|
|
|
unsigned int start = 0;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < FRAMES_COUNT; ++i) {
|
|
|
|
if (frames[i]) {
|
|
|
|
start = i + 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (start % (PAGE_BIG_SIZE / PAGE_SIZE)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i - start + 1 == PAGE_BIG_SIZE / PAGE_SIZE) {
|
|
|
|
for (unsigned int j = start; j <= i; ++j) {
|
|
|
|
frames[j] = 0xFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
return start * PAGE_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-05 04:24:34 -05:00
|
|
|
// TODO: panic if not aligned
|
2017-11-05 03:08:33 -05:00
|
|
|
void memory_free_page(const unsigned long addr)
|
|
|
|
{
|
|
|
|
const unsigned long i = addr / PAGE_SIZE;
|
|
|
|
|
|
|
|
if (i >= FRAMES_COUNT) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
frames[i] = 0;
|
|
|
|
}
|
|
|
|
|
2017-11-05 04:24:34 -05:00
|
|
|
// TODO: panic if not aligned
|
2017-11-05 03:49:02 -05:00
|
|
|
void memory_free_big_page(const unsigned long addr)
|
|
|
|
{
|
|
|
|
const unsigned long start = addr / PAGE_SIZE;
|
|
|
|
const unsigned long end = start + PAGE_BIG_SIZE / PAGE_SIZE;
|
|
|
|
|
|
|
|
for (unsigned int i = start; i <= end && i < FRAMES_COUNT; ++i) {
|
|
|
|
frames[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-05 01:56:29 -05:00
|
|
|
void mark_used(const unsigned long base, const unsigned long limit)
|
|
|
|
{
|
2017-11-05 02:12:04 -05:00
|
|
|
const unsigned int start = base / PAGE_SIZE;
|
|
|
|
const unsigned int end = limit / PAGE_SIZE;
|
2017-11-05 01:56:29 -05:00
|
|
|
|
|
|
|
for (unsigned int i = start; i <= end; ++i) {
|
2017-11-05 02:32:32 -05:00
|
|
|
frames[i] = 0xFF;
|
2017-11-05 01:56:29 -05:00
|
|
|
}
|
|
|
|
}
|