2012-09-26 23:59:58 -04:00
|
|
|
/*
|
|
|
|
Copyright (C) 2012, Aaron Lindsay <aaron@aclindsay.com>
|
|
|
|
|
|
|
|
This file is part of Aedrix.
|
|
|
|
|
|
|
|
This program 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 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program 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 this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2012-09-17 23:27:11 -04:00
|
|
|
#include <list.h>
|
2013-01-01 23:42:45 -05:00
|
|
|
#include <frames.h>
|
2012-09-17 23:27:11 -04:00
|
|
|
#include <print.h>
|
2012-11-15 00:27:49 -05:00
|
|
|
#include <types.h>
|
2012-09-17 23:27:11 -04:00
|
|
|
|
2013-01-01 23:42:45 -05:00
|
|
|
struct dlist_node free_frames_list;
|
2012-09-17 23:27:11 -04:00
|
|
|
|
2013-01-01 23:42:45 -05:00
|
|
|
void frames_init() {
|
|
|
|
init_list(&free_frames_list);
|
2012-09-17 23:27:11 -04:00
|
|
|
}
|
|
|
|
|
2013-01-01 23:42:45 -05:00
|
|
|
/*
|
|
|
|
* Adds a page frame struct to the list of free page frames. Presupposes
|
|
|
|
* free_frames_list is a properly initialized list.
|
|
|
|
*/
|
|
|
|
static void insert_page_frame(struct frame *p) {
|
|
|
|
if (list_empty(&free_frames_list) || p->address < container(free_frames_list.next, struct frame, list)->address) {
|
|
|
|
insert_after(&free_frames_list, &p->list);
|
|
|
|
} else if (p->address > container(free_frames_list.prev, struct frame, list)->address) {
|
|
|
|
insert_before(&free_frames_list, &p->list);
|
2012-09-17 23:27:11 -04:00
|
|
|
} else {
|
2013-01-01 23:42:45 -05:00
|
|
|
struct frame *it;
|
|
|
|
for_each_list(it, &free_frames_list, struct frame, list) {
|
2012-09-17 23:27:11 -04:00
|
|
|
if (p->address < it->address) {
|
|
|
|
insert_before(&it->list, &p->list);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-01-01 23:42:45 -05:00
|
|
|
print("Error: failed to insert page frame\n");
|
2012-09-17 23:27:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-01 23:42:45 -05:00
|
|
|
/*
|
|
|
|
* Called to add a segment of memory to the frame allocation pool.
|
|
|
|
*/
|
|
|
|
static void add_physical_memory(void *start, void *end) {
|
2012-09-17 23:27:11 -04:00
|
|
|
unsigned int num_pages, usable_pages;
|
2013-01-01 23:42:45 -05:00
|
|
|
struct frame *p;
|
2012-09-17 23:27:11 -04:00
|
|
|
void *page;
|
|
|
|
|
2012-09-29 23:26:43 -04:00
|
|
|
//If region starts at 0x0, make it start at next page to not screw up null pointer detection, etc.
|
|
|
|
if (start == 0)
|
2013-01-01 23:53:18 -05:00
|
|
|
start = (char *)start + CONFIG_PAGE_SIZE;
|
2012-09-29 23:26:43 -04:00
|
|
|
|
2012-09-17 23:27:11 -04:00
|
|
|
//make sure both start and end address are aligned to the size of a page
|
2013-01-01 23:53:18 -05:00
|
|
|
if ((arch_uint_ptr)start % CONFIG_PAGE_SIZE != 0)
|
|
|
|
start = (char*)start + (CONFIG_PAGE_SIZE - ((arch_uint_ptr)start % CONFIG_PAGE_SIZE));
|
|
|
|
if (((arch_uint_ptr)end + 1) % CONFIG_PAGE_SIZE != 0)
|
|
|
|
end = (char*)end - ((arch_uint_ptr)end + 1) % CONFIG_PAGE_SIZE;
|
2012-09-25 22:44:19 -04:00
|
|
|
|
2013-01-01 23:53:18 -05:00
|
|
|
if ((char *)end + 1 - (char *)start < CONFIG_PAGE_SIZE<<1) {
|
|
|
|
print("Error: Supplied memory area(%x,%x) is smaller than the page size (%d)\n", (arch_uint_ptr)start, (arch_uint_ptr)end, CONFIG_PAGE_SIZE);
|
2012-09-17 23:27:11 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//reserve enough pages at the front of this chunk of memory to hold the
|
|
|
|
//page structs, then build the structs and add them to the list
|
|
|
|
|
|
|
|
//TODO we're potentially losing memory here because we're calculating
|
|
|
|
//the number of page structs we need even for those pages that will contain only page structs
|
2013-01-01 23:53:18 -05:00
|
|
|
num_pages = ((char *)end + 1 - (char *)start) / CONFIG_PAGE_SIZE;
|
|
|
|
usable_pages = num_pages - num_pages * sizeof(struct frame) / CONFIG_PAGE_SIZE;
|
|
|
|
if (num_pages * sizeof(struct frame) % CONFIG_PAGE_SIZE)
|
2012-09-17 23:27:11 -04:00
|
|
|
usable_pages--;
|
|
|
|
|
2013-01-01 23:42:45 -05:00
|
|
|
p = (struct frame *)start;
|
2013-01-01 23:53:18 -05:00
|
|
|
for (page = ((char *)start) + (num_pages - usable_pages)*CONFIG_PAGE_SIZE; page < end; page = (void *)(((char *)page) + CONFIG_PAGE_SIZE)) {
|
2012-09-17 23:27:11 -04:00
|
|
|
p->address = page;
|
2013-01-01 23:42:45 -05:00
|
|
|
insert_page_frame(p);
|
2012-09-17 23:27:11 -04:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-01 23:42:45 -05:00
|
|
|
/*
|
|
|
|
* Attempt to get 2^power contiguous page frames. Returns the frame struct of
|
|
|
|
* the first frame on success or a null pointer on failure.
|
|
|
|
*/
|
|
|
|
struct frame* get_free_frames(unsigned int power) {
|
2012-09-17 23:27:11 -04:00
|
|
|
unsigned int num_pages = 1<<power;
|
2013-01-01 23:42:45 -05:00
|
|
|
struct frame *it;
|
2012-09-17 23:27:11 -04:00
|
|
|
|
2013-01-01 23:42:45 -05:00
|
|
|
if (list_empty(&free_frames_list)) {
|
2012-09-17 23:27:11 -04:00
|
|
|
print("Error: Out of memory\n");
|
2013-01-01 23:42:45 -05:00
|
|
|
return (struct frame*)0;
|
2012-09-17 23:27:11 -04:00
|
|
|
}
|
|
|
|
|
2012-09-29 23:26:43 -04:00
|
|
|
if (!num_pages) {
|
|
|
|
print("Error: mm_get_free_pages must be called with power from 0 to 31, inclusive (power=%d)\n", power);
|
2013-01-01 23:42:45 -05:00
|
|
|
return (struct frame*)0;
|
2012-09-29 23:26:43 -04:00
|
|
|
}
|
|
|
|
|
2013-01-01 23:42:45 -05:00
|
|
|
for_each_list(it, &free_frames_list, struct frame, list) {
|
2012-09-17 23:27:11 -04:00
|
|
|
unsigned int curr_pages = 1;
|
2013-01-01 23:42:45 -05:00
|
|
|
struct frame *it2;
|
|
|
|
for (it2 = container(it->list.next, struct frame, list);
|
|
|
|
it2->list.next != &free_frames_list && curr_pages < num_pages;
|
|
|
|
it2 = container(it2->list.next, struct frame, list)) {
|
2013-01-01 23:53:18 -05:00
|
|
|
if ((char*)it2->address != (char*)container(it2->list.prev, struct frame, list)->address + CONFIG_PAGE_SIZE) {
|
2013-01-01 23:42:45 -05:00
|
|
|
it = it2; //fast-forward 'it' to start of next contiguous section of pages
|
2012-09-17 23:27:11 -04:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
curr_pages++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (curr_pages == num_pages) {
|
|
|
|
remove_splice(&it->list, it2->list.prev);
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
}
|
2013-01-01 23:42:45 -05:00
|
|
|
return (struct frame*)0;
|
2012-09-17 23:27:11 -04:00
|
|
|
}
|
|
|
|
|
2013-01-01 23:42:45 -05:00
|
|
|
/*
|
|
|
|
* Return pages allocated to the pool of unused pages.
|
|
|
|
*/
|
|
|
|
int put_free_frames(struct frame *f) {
|
|
|
|
struct frame *it;
|
|
|
|
for_each_list(it, &free_frames_list, struct frame, list) {
|
|
|
|
if (f->address < it->address) {
|
|
|
|
insert_splice_before(&it->list, &f->list, f->list.prev);
|
2012-09-17 23:27:11 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|