2015-05-23 14:35:45 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010, Stefan Lankes, RWTH Aachen University
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* * Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this
|
|
|
|
* software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <hermit/stddef.h>
|
|
|
|
#include <hermit/stdlib.h>
|
|
|
|
#include <hermit/stdio.h>
|
|
|
|
#include <hermit/string.h>
|
|
|
|
#include <hermit/spinlock.h>
|
2016-10-03 23:15:09 +02:00
|
|
|
#include <hermit/memory.h>
|
2016-11-04 12:09:43 +01:00
|
|
|
#include <hermit/logging.h>
|
2015-05-23 14:35:45 +02:00
|
|
|
|
|
|
|
#include <asm/atomic.h>
|
|
|
|
#include <asm/page.h>
|
2016-08-26 23:53:22 +02:00
|
|
|
#include <asm/multiboot.h>
|
2015-05-23 14:35:45 +02:00
|
|
|
|
2017-04-18 00:26:38 +02:00
|
|
|
#define GAP_BELOW 0x100000ULL
|
|
|
|
|
2015-07-30 22:57:28 +02:00
|
|
|
extern uint64_t base;
|
|
|
|
extern uint64_t limit;
|
2015-05-23 14:35:45 +02:00
|
|
|
|
2015-07-30 22:12:34 +02:00
|
|
|
typedef struct free_list {
|
|
|
|
size_t start, end;
|
|
|
|
struct free_list* next;
|
|
|
|
struct free_list* prev;
|
|
|
|
} free_list_t;
|
|
|
|
|
2015-05-23 14:35:45 +02:00
|
|
|
/*
|
|
|
|
* Note that linker symbols are not variables, they have no memory allocated for
|
|
|
|
* maintaining a value, rather their address is their value.
|
|
|
|
*/
|
|
|
|
extern const void kernel_start;
|
|
|
|
|
2015-07-30 22:12:34 +02:00
|
|
|
static spinlock_t list_lock = SPINLOCK_INIT;
|
2015-05-23 14:35:45 +02:00
|
|
|
|
2016-08-26 23:53:22 +02:00
|
|
|
static free_list_t init_list = {0, 0, NULL, NULL};
|
2015-07-30 22:12:34 +02:00
|
|
|
static free_list_t* free_start = &init_list;
|
2015-05-23 14:35:45 +02:00
|
|
|
|
2015-07-30 22:57:28 +02:00
|
|
|
atomic_int64_t total_pages = ATOMIC_INIT(0);
|
|
|
|
atomic_int64_t total_allocated_pages = ATOMIC_INIT(0);
|
|
|
|
atomic_int64_t total_available_pages = ATOMIC_INIT(0);
|
2015-05-23 14:35:45 +02:00
|
|
|
|
|
|
|
size_t get_pages(size_t npages)
|
|
|
|
{
|
2015-07-30 22:12:34 +02:00
|
|
|
size_t i, ret = 0;
|
|
|
|
free_list_t* curr = free_start;
|
2015-05-23 14:35:45 +02:00
|
|
|
|
|
|
|
if (BUILTIN_EXPECT(!npages, 0))
|
|
|
|
return 0;
|
2015-07-30 22:57:28 +02:00
|
|
|
if (BUILTIN_EXPECT(npages > atomic_int64_read(&total_available_pages), 0))
|
2015-05-23 14:35:45 +02:00
|
|
|
return 0;
|
|
|
|
|
2015-07-30 22:12:34 +02:00
|
|
|
spinlock_lock(&list_lock);
|
|
|
|
|
|
|
|
while(curr) {
|
|
|
|
i = (curr->end - curr->start) / PAGE_SIZE;
|
|
|
|
if (i > npages) {
|
|
|
|
ret = curr->start;
|
|
|
|
curr->start += npages * PAGE_SIZE;
|
|
|
|
goto out;
|
|
|
|
} else if (i == npages) {
|
|
|
|
ret = curr->start;
|
|
|
|
if (curr->prev)
|
|
|
|
curr->prev = curr->next;
|
|
|
|
else
|
|
|
|
free_start = curr->next;
|
|
|
|
if (curr != &init_list)
|
|
|
|
kfree(curr);
|
|
|
|
goto out;
|
2015-05-23 14:35:45 +02:00
|
|
|
}
|
|
|
|
|
2015-07-30 22:12:34 +02:00
|
|
|
curr = curr->next;
|
|
|
|
}
|
|
|
|
out:
|
2016-11-04 12:09:43 +01:00
|
|
|
LOG_DEBUG("get_pages: ret 0%llx, curr->start 0x%llx, curr->end 0x%llx\n", ret, curr->start, curr->end);
|
2015-07-31 22:10:37 +02:00
|
|
|
|
2015-07-30 22:12:34 +02:00
|
|
|
spinlock_unlock(&list_lock);
|
2015-05-23 14:35:45 +02:00
|
|
|
|
2015-07-30 22:12:34 +02:00
|
|
|
if (ret) {
|
2015-07-30 22:57:28 +02:00
|
|
|
atomic_int64_add(&total_allocated_pages, npages);
|
|
|
|
atomic_int64_sub(&total_available_pages, npages);
|
2015-05-23 14:35:45 +02:00
|
|
|
}
|
|
|
|
|
2015-07-30 22:12:34 +02:00
|
|
|
return ret;
|
2015-05-23 14:35:45 +02:00
|
|
|
}
|
|
|
|
|
2016-10-03 23:15:09 +02:00
|
|
|
DEFINE_PER_CORE(size_t, ztmp_addr, 0);
|
|
|
|
|
|
|
|
size_t get_zeroed_page(void)
|
|
|
|
{
|
|
|
|
size_t phyaddr = get_page();
|
|
|
|
size_t viraddr;
|
|
|
|
uint8_t flags;
|
|
|
|
|
|
|
|
if (BUILTIN_EXPECT(!phyaddr, 0))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
flags = irq_nested_disable();
|
|
|
|
|
|
|
|
viraddr = per_core(ztmp_addr);
|
|
|
|
if (BUILTIN_EXPECT(!viraddr, 0))
|
|
|
|
{
|
|
|
|
viraddr = vma_alloc(PAGE_SIZE, VMA_READ|VMA_WRITE|VMA_CACHEABLE);
|
|
|
|
if (BUILTIN_EXPECT(!viraddr, 0))
|
|
|
|
goto novaddr;
|
|
|
|
|
2016-11-04 12:09:43 +01:00
|
|
|
LOG_DEBUG("Core %d uses 0x%zx as temporary address\n", CORE_ID, viraddr);
|
2016-10-03 23:15:09 +02:00
|
|
|
set_per_core(ztmp_addr, viraddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
__page_map(viraddr, phyaddr, 1, PG_GLOBAL|PG_RW|PG_PRESENT, 0);
|
|
|
|
|
|
|
|
memset((void*) viraddr, 0x00, PAGE_SIZE);
|
|
|
|
|
|
|
|
novaddr:
|
|
|
|
irq_nested_enable(flags);
|
|
|
|
|
|
|
|
return phyaddr;
|
|
|
|
}
|
|
|
|
|
2015-07-30 22:12:34 +02:00
|
|
|
/* TODO: reunion of elements is still missing */
|
2015-05-23 14:35:45 +02:00
|
|
|
int put_pages(size_t phyaddr, size_t npages)
|
|
|
|
{
|
2015-07-30 22:12:34 +02:00
|
|
|
free_list_t* curr = free_start;
|
2015-05-23 14:35:45 +02:00
|
|
|
|
|
|
|
if (BUILTIN_EXPECT(!phyaddr, 0))
|
|
|
|
return -EINVAL;
|
|
|
|
if (BUILTIN_EXPECT(!npages, 0))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2015-07-30 22:12:34 +02:00
|
|
|
spinlock_lock(&list_lock);
|
|
|
|
|
|
|
|
while(curr) {
|
|
|
|
if (phyaddr+npages*PAGE_SIZE == curr->start) {
|
|
|
|
curr->start = phyaddr;
|
|
|
|
goto out;
|
|
|
|
} else if (phyaddr == curr->end) {
|
|
|
|
curr->end += npages*PAGE_SIZE;
|
|
|
|
goto out;
|
|
|
|
} if (phyaddr > curr->end) {
|
|
|
|
free_list_t* n = kmalloc(sizeof(free_list_t));
|
|
|
|
|
|
|
|
if (BUILTIN_EXPECT(!n, 0))
|
|
|
|
goto out_err;
|
|
|
|
|
|
|
|
/* add new element */
|
|
|
|
n->start = phyaddr;
|
|
|
|
n->end = phyaddr + npages * PAGE_SIZE;
|
|
|
|
n->prev = curr;
|
|
|
|
n->next = curr->next;
|
|
|
|
curr->next = n;
|
2015-05-23 14:35:45 +02:00
|
|
|
}
|
2015-07-30 22:12:34 +02:00
|
|
|
|
|
|
|
curr = curr->next;
|
2015-05-23 14:35:45 +02:00
|
|
|
}
|
2015-07-30 22:12:34 +02:00
|
|
|
out:
|
|
|
|
spinlock_unlock(&list_lock);
|
|
|
|
|
2015-07-30 22:57:28 +02:00
|
|
|
atomic_int64_sub(&total_allocated_pages, npages);
|
|
|
|
atomic_int64_add(&total_available_pages, npages);
|
2015-05-23 14:35:45 +02:00
|
|
|
|
2015-07-30 22:12:34 +02:00
|
|
|
return 0;
|
2015-05-23 14:35:45 +02:00
|
|
|
|
2015-07-30 22:12:34 +02:00
|
|
|
out_err:
|
|
|
|
spinlock_unlock(&list_lock);
|
2015-05-23 14:35:45 +02:00
|
|
|
|
2015-07-30 22:12:34 +02:00
|
|
|
return -ENOMEM;
|
2015-05-23 14:35:45 +02:00
|
|
|
}
|
|
|
|
|
2016-07-27 22:30:00 +02:00
|
|
|
void* page_alloc(size_t sz, uint32_t flags)
|
|
|
|
{
|
|
|
|
size_t viraddr = 0;
|
|
|
|
size_t phyaddr;
|
2017-07-15 16:24:08 +02:00
|
|
|
uint32_t npages = PAGE_CEIL(sz) >> PAGE_BITS;
|
2016-07-27 22:30:00 +02:00
|
|
|
size_t pflags = PG_PRESENT|PG_GLOBAL|PG_XD;
|
|
|
|
|
|
|
|
if (BUILTIN_EXPECT(!npages, 0))
|
|
|
|
goto oom;
|
|
|
|
|
2017-07-15 16:24:08 +02:00
|
|
|
viraddr = vma_alloc(PAGE_CEIL(sz), flags);
|
2016-07-27 22:30:00 +02:00
|
|
|
if (BUILTIN_EXPECT(!viraddr, 0))
|
|
|
|
goto oom;
|
|
|
|
|
|
|
|
phyaddr = get_pages(npages);
|
|
|
|
if (BUILTIN_EXPECT(!phyaddr, 0))
|
|
|
|
{
|
|
|
|
vma_free(viraddr, viraddr+npages*PAGE_SIZE);
|
|
|
|
viraddr = 0;
|
|
|
|
goto oom;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & VMA_WRITE)
|
|
|
|
pflags |= PG_RW;
|
|
|
|
if (!(flags & VMA_CACHEABLE))
|
|
|
|
pflags |= PG_PCD;
|
|
|
|
|
|
|
|
int ret = page_map(viraddr, phyaddr, npages, pflags);
|
|
|
|
if (BUILTIN_EXPECT(ret, 0))
|
|
|
|
{
|
|
|
|
vma_free(viraddr, viraddr+npages*PAGE_SIZE);
|
|
|
|
put_pages(phyaddr, npages);
|
|
|
|
viraddr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
oom:
|
|
|
|
return (void*) viraddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void page_free(void* viraddr, size_t sz)
|
|
|
|
{
|
|
|
|
size_t phyaddr;
|
|
|
|
|
|
|
|
if (BUILTIN_EXPECT(!viraddr || !sz, 0))
|
|
|
|
return;
|
|
|
|
|
|
|
|
phyaddr = virt_to_phys((size_t)viraddr);
|
|
|
|
|
2017-07-15 16:24:08 +02:00
|
|
|
vma_free((size_t) viraddr, (size_t) viraddr + PAGE_CEIL(sz));
|
2016-07-27 22:30:00 +02:00
|
|
|
|
|
|
|
if (phyaddr)
|
2017-07-15 16:24:08 +02:00
|
|
|
put_pages(phyaddr, PAGE_CEIL(sz) >> PAGE_BITS);
|
2016-07-27 22:30:00 +02:00
|
|
|
}
|
|
|
|
|
2015-05-23 14:35:45 +02:00
|
|
|
int memory_init(void)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
// enable paging and map Multiboot modules etc.
|
|
|
|
ret = page_init();
|
|
|
|
if (BUILTIN_EXPECT(ret, 0)) {
|
2016-11-04 12:09:43 +01:00
|
|
|
LOG_ERROR("Failed to initialize paging!\n");
|
2015-05-23 14:35:45 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-11-04 12:09:43 +01:00
|
|
|
LOG_INFO("mb_info: 0x%zx\n", mb_info);
|
|
|
|
LOG_INFO("memory_init: base 0x%zx, image_size 0x%zx, limit 0x%zx\n", base, image_size, limit);
|
2015-05-24 09:57:34 +02:00
|
|
|
|
2016-08-26 23:53:22 +02:00
|
|
|
if (mb_info) {
|
|
|
|
if (mb_info->flags & MULTIBOOT_INFO_MEM_MAP) {
|
|
|
|
size_t end_addr, start_addr;
|
|
|
|
multiboot_memory_map_t* mmap = (multiboot_memory_map_t*) ((size_t) mb_info->mmap_addr);
|
|
|
|
multiboot_memory_map_t* mmap_end = (void*) ((size_t) mb_info->mmap_addr + mb_info->mmap_length);
|
|
|
|
|
|
|
|
// mark first available memory slot as free
|
|
|
|
for(; mmap < mmap_end; mmap = (multiboot_memory_map_t*) ((size_t) mmap + sizeof(uint32_t) + mmap->size)) {
|
|
|
|
if (mmap->type == MULTIBOOT_MEMORY_AVAILABLE) {
|
2017-07-15 16:24:08 +02:00
|
|
|
start_addr = PAGE_CEIL(mmap->addr);
|
|
|
|
end_addr = PAGE_FLOOR(mmap->addr + mmap->len);
|
2016-08-26 23:53:22 +02:00
|
|
|
|
2016-11-04 12:09:43 +01:00
|
|
|
LOG_INFO("Free region 0x%zx - 0x%zx\n", start_addr, end_addr);
|
2016-08-26 23:53:22 +02:00
|
|
|
|
2017-04-18 00:26:38 +02:00
|
|
|
if ((start_addr <= base) && (end_addr >= PAGE_2M_FLOOR((size_t) &kernel_start + image_size))) {
|
2017-07-15 16:24:08 +02:00
|
|
|
init_list.start = PAGE_2M_CEIL((size_t) &kernel_start + image_size);
|
2016-08-26 23:53:22 +02:00
|
|
|
init_list.end = end_addr;
|
|
|
|
|
2016-11-04 12:09:43 +01:00
|
|
|
LOG_INFO("Add region 0x%zx - 0x%zx\n", init_list.start, init_list.end);
|
2016-08-26 23:53:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// determine available memory
|
|
|
|
atomic_int64_add(&total_pages, (end_addr-start_addr) >> PAGE_BITS);
|
|
|
|
atomic_int64_add(&total_available_pages, (end_addr-start_addr) >> PAGE_BITS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!init_list.end)
|
|
|
|
goto oom;
|
|
|
|
} else {
|
|
|
|
goto oom;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// determine available memory
|
|
|
|
atomic_int64_add(&total_pages, (limit-base) >> PAGE_BITS);
|
|
|
|
atomic_int64_add(&total_available_pages, (limit-base) >> PAGE_BITS);
|
|
|
|
|
2017-07-15 16:24:08 +02:00
|
|
|
init_list.start = PAGE_2M_CEIL(base + image_size);
|
2017-04-23 00:30:15 +02:00
|
|
|
init_list.end = limit;
|
2016-08-26 23:53:22 +02:00
|
|
|
}
|
2015-05-23 14:35:45 +02:00
|
|
|
|
2015-08-28 07:59:02 +02:00
|
|
|
// determine allocated memory, we use 2MB pages to map the kernel
|
2017-07-15 16:24:08 +02:00
|
|
|
atomic_int64_add(&total_allocated_pages, PAGE_2M_CEIL(image_size) >> PAGE_BITS);
|
|
|
|
atomic_int64_sub(&total_available_pages, PAGE_2M_CEIL(image_size) >> PAGE_BITS);
|
2015-07-30 22:12:34 +02:00
|
|
|
|
2016-11-04 12:09:43 +01:00
|
|
|
LOG_INFO("free list starts at 0x%zx, limit 0x%zx\n", init_list.start, init_list.end);
|
2015-07-27 22:12:05 +02:00
|
|
|
|
2016-10-21 00:21:01 +02:00
|
|
|
// init high bandwidth memory subsystem
|
|
|
|
hbmemory_init();
|
|
|
|
|
2015-05-23 14:35:45 +02:00
|
|
|
ret = vma_init();
|
2015-07-30 22:12:34 +02:00
|
|
|
if (BUILTIN_EXPECT(ret, 0))
|
2016-11-04 12:09:43 +01:00
|
|
|
LOG_WARNING("Failed to initialize VMA regions: %d\n", ret);
|
2015-05-23 14:35:45 +02:00
|
|
|
|
2016-08-26 23:53:22 +02:00
|
|
|
// add missing free regions
|
|
|
|
if (mb_info) {
|
|
|
|
if (mb_info->flags & MULTIBOOT_INFO_MEM_MAP) {
|
|
|
|
free_list_t* last = &init_list;
|
|
|
|
size_t end_addr, start_addr;
|
|
|
|
multiboot_memory_map_t* mmap = (multiboot_memory_map_t*) ((size_t) mb_info->mmap_addr);
|
|
|
|
multiboot_memory_map_t* mmap_end = (void*) ((size_t) mb_info->mmap_addr + mb_info->mmap_length);
|
|
|
|
|
|
|
|
// mark available memory as free
|
|
|
|
for(; mmap < mmap_end; mmap = (multiboot_memory_map_t*) ((size_t) mmap + sizeof(uint32_t) + mmap->size))
|
|
|
|
{
|
|
|
|
if (mmap->type == MULTIBOOT_MEMORY_AVAILABLE) {
|
2017-07-15 16:24:08 +02:00
|
|
|
start_addr = PAGE_CEIL(mmap->addr);
|
|
|
|
end_addr = PAGE_FLOOR(mmap->addr + mmap->len);
|
2016-08-26 23:53:22 +02:00
|
|
|
|
2017-07-15 16:24:08 +02:00
|
|
|
if ((start_addr <= base) && (end_addr >= PAGE_2M_CEIL(base+image_size)))
|
2016-08-26 23:53:22 +02:00
|
|
|
end_addr = base;
|
|
|
|
|
|
|
|
// ignore everything below 1M => reserve for I/O devices
|
2017-04-18 00:26:38 +02:00
|
|
|
if ((start_addr < GAP_BELOW))
|
|
|
|
start_addr = GAP_BELOW;
|
|
|
|
|
|
|
|
if (start_addr < (size_t)mb_info)
|
2017-07-15 16:24:08 +02:00
|
|
|
start_addr = PAGE_CEIL((size_t)mb_info);
|
2017-04-18 00:26:38 +02:00
|
|
|
|
2017-05-31 22:08:21 +02:00
|
|
|
if ((mb_info->flags & MULTIBOOT_INFO_CMDLINE) && cmdline) {
|
|
|
|
if (start_addr < (size_t) cmdline+cmdsize)
|
2017-07-15 16:24:08 +02:00
|
|
|
start_addr = PAGE_CEIL((size_t) cmdline+cmdsize);
|
2017-04-18 00:26:38 +02:00
|
|
|
}
|
2016-08-26 23:53:22 +02:00
|
|
|
|
|
|
|
if (start_addr >= end_addr)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
last->next = kmalloc(sizeof(free_list_t));
|
|
|
|
if (BUILTIN_EXPECT(!last->next, 0))
|
|
|
|
goto oom;
|
|
|
|
|
2016-11-04 12:09:43 +01:00
|
|
|
LOG_INFO("Add region 0x%zx - 0x%zx\n", start_addr, end_addr);
|
2016-08-26 23:53:22 +02:00
|
|
|
|
|
|
|
last->next->prev = last;
|
|
|
|
last = last->next;
|
|
|
|
last->next = NULL;
|
|
|
|
last->start = start_addr;
|
|
|
|
last->end = end_addr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-23 14:35:45 +02:00
|
|
|
return ret;
|
2016-08-26 23:53:22 +02:00
|
|
|
|
|
|
|
oom:
|
2016-11-04 12:09:43 +01:00
|
|
|
LOG_ERROR("BUG: Failed to init mm!\n");
|
2016-08-26 23:53:22 +02:00
|
|
|
while(1) {HALT; }
|
2015-05-23 14:35:45 +02:00
|
|
|
}
|