summaryrefslogtreecommitdiff
path: root/src/kernel/mem
diff options
context:
space:
mode:
authorAlex AUVOLAT <alexis211@gmail.com>2012-05-04 20:06:37 +0200
committerAlex AUVOLAT <alexis211@gmail.com>2012-05-04 20:06:37 +0200
commit277e4af4fa9e80816c809542d792ee6bebb7f202 (patch)
tree9abb7f207d185909427137e4861b81c057de1259 /src/kernel/mem
parente9683297bf480f9590b0e5796f4520fb430e2e03 (diff)
downloadTCE-277e4af4fa9e80816c809542d792ee6bebb7f202.tar.gz
TCE-277e4af4fa9e80816c809542d792ee6bebb7f202.zip
Migration to C++!
Diffstat (limited to 'src/kernel/mem')
-rw-r--r--src/kernel/mem/_dlmalloc.h3
-rw-r--r--src/kernel/mem/gdt.cpp (renamed from src/kernel/mem/gdt.c)16
-rw-r--r--src/kernel/mem/mem.cpp (renamed from src/kernel/mem/mem.c)10
-rw-r--r--src/kernel/mem/mem.h12
-rw-r--r--src/kernel/mem/paging.cpp (renamed from src/kernel/mem/paging.c)40
-rw-r--r--src/kernel/mem/paging.h26
-rw-r--r--src/kernel/mem/seg.cpp (renamed from src/kernel/mem/seg.c)50
-rw-r--r--src/kernel/mem/seg.h24
8 files changed, 91 insertions, 90 deletions
diff --git a/src/kernel/mem/_dlmalloc.h b/src/kernel/mem/_dlmalloc.h
index 7b2e2cc..a80273c 100644
--- a/src/kernel/mem/_dlmalloc.h
+++ b/src/kernel/mem/_dlmalloc.h
@@ -1,6 +1,7 @@
#ifndef DEF_DLMALLOC_H
#define DEF_DLMALLOC_H
+#include <types.h>
#include "lib/std.h"
#include <stdlib_common.h>
#include <string.h>
@@ -16,6 +17,8 @@
#define USE_LOCKS 2
+
+
/* Version identifier to allow people to support multiple versions */
#ifndef DLMALLOC_VERSION
#define DLMALLOC_VERSION 20805
diff --git a/src/kernel/mem/gdt.c b/src/kernel/mem/gdt.cpp
index 494aaf2..49fbd82 100644
--- a/src/kernel/mem/gdt.c
+++ b/src/kernel/mem/gdt.cpp
@@ -2,14 +2,14 @@
#include <stdlib_common.h>
#include <core/monitor.h>
-extern void gdt_flush(uint32_t); //ASM (imported from idt_.asm)
-extern void tss_flush();
+extern "C" void gdt_flush(uint32_t); //ASM (imported from idt_.asm)
+extern "C" void tss_flush();
#define GDT_ENTRIES 6 // The contents of each entry is defined in gdt_init.
-static struct tss_entry tss_entry;
-static struct gdt_entry gdt_entries[GDT_ENTRIES];
-static struct gdt_ptr gdt_ptr;
+static tss_entry tss_entry;
+static gdt_entry gdt_entries[GDT_ENTRIES];
+static gdt_ptr gdt_ptr;
/* This function is called by the task_switch function on each context switch.
It updates the TSS so that an interrupt will be handled on the correct kernel stack
@@ -33,11 +33,11 @@ static void gdt_setGate(int num, uint32_t base, uint32_t limit, uint8_t access,
/* For internal use only. Writes one entry of the GDT, that entry being a pointer to the TSS. */
static void gdt_writeTss(int num, uint32_t ss0, uint32_t esp0) {
uint32_t base = (uint32_t)&tss_entry;
- uint32_t limit = base + sizeof(struct tss_entry);
+ uint32_t limit = base + sizeof(tss_entry);
gdt_setGate(num, base, limit, 0xE9, 0);
- memset(&tss_entry, 0, sizeof(struct tss_entry));
+ memset(&tss_entry, 0, sizeof(tss_entry));
tss_entry.ss0 = ss0; tss_entry.esp0 = esp0;
@@ -47,7 +47,7 @@ static void gdt_writeTss(int num, uint32_t ss0, uint32_t esp0) {
/* Write data to the GDT and enable it. */
void gdt_init() {
- gdt_ptr.limit = (sizeof(struct gdt_entry) * GDT_ENTRIES) - 1;
+ gdt_ptr.limit = (sizeof(gdt_entry) * GDT_ENTRIES) - 1;
gdt_ptr.base = (uint32_t)&gdt_entries;
gdt_setGate(0, 0, 0, 0, 0); //Null segment
diff --git a/src/kernel/mem/mem.c b/src/kernel/mem/mem.cpp
index 3310b70..5e04de3 100644
--- a/src/kernel/mem/mem.c
+++ b/src/kernel/mem/mem.cpp
@@ -16,7 +16,7 @@
#define KHEAP_MAXSIZE 0x08000000
size_t mem_placementAddr;
-int _no_more_ksbrk = 0;
+bool _no_more_ksbrk = false;
// ******************************
@@ -79,7 +79,7 @@ void kfree_page(void* ptr) {
// *************************
void* ksbrk(size_t size) {
- if (_no_more_ksbrk == 0) { // ksbrk is NOT being called by dlmalloc
+ if (!_no_more_ksbrk) { // ksbrk is NOT being called by dlmalloc
if (size & 0x0FFF) {
size = (size & 0xFFFFF000) + 0x1000;
}
@@ -103,7 +103,7 @@ void* ksbrk(size_t size) {
er_end = mem_placementAddr;
if (er_end & 0x0FFF) er_end = (er_end & 0xFFFFF000) + 0x1000;
for (i = er_begin; i < er_end; i += 0x1000) {
- struct page *p = pagedir_getPage(kernel_pagedir, i, 1);
+ page *p = pagedir_getPage(kernel_pagedir, i, 1);
size_t f = frame_alloc();
page_map(p, f, 0, 0);
/* (DBG) monitor_write("<map "); monitor_writeHex(i); monitor_write(" ");
@@ -126,11 +126,11 @@ void* ksbrk(size_t size) {
void kbrk(void* ptr) {
monitor_write("<kbrk ");
- monitor_writeHex(ptr);
+ monitor_writeHex((uint32_t)ptr);
monitor_write(">\n");
if ((size_t)ptr > (size_t)&end) {
- ksbrk(ptr - (size_t)mem_placementAddr);
+ ksbrk((size_t)ptr - (size_t)mem_placementAddr);
} else {
PANIC("INVALID KBRK.");
}
diff --git a/src/kernel/mem/mem.h b/src/kernel/mem/mem.h
index cb9c396..085d9ce 100644
--- a/src/kernel/mem/mem.h
+++ b/src/kernel/mem/mem.h
@@ -11,19 +11,17 @@ void* kmalloc_page(size_t *phys);
void kfree_page(void* page);
// Internal, used by dlmalloc
-void* ksbrk(size_t size);
-void kbrk(void* ptr);
+extern "C" void* ksbrk(size_t size);
+extern "C" void kbrk(void* ptr);
#define kmalloc dlmalloc
#define kfree dlfree
-void kheap_init();
-
-extern int _no_more_ksbrk;
+extern bool _no_more_ksbrk;
extern size_t mem_placementAddr;
-extern void end; //Symbol defined by linker : end of kernel code
+extern "C" size_t end; //Symbol defined by linker : end of kernel code
-extern void k_highhalf_addr; // Symbol defined by linker : high half position of the kerne
+extern "C" size_t k_highhalf_addr; // Symbol defined by linker : high half position of the kerne
// Should be at 0xC0000000
#define K_HIGHHALF_ADDR ((size_t)(&k_highhalf_addr))
diff --git a/src/kernel/mem/paging.c b/src/kernel/mem/paging.cpp
index 0527f06..eb7e615 100644
--- a/src/kernel/mem/paging.c
+++ b/src/kernel/mem/paging.cpp
@@ -7,24 +7,24 @@
#include <core/sys.h>
#include <task/task.h>
-static struct bitset frames;
+static bitset frames;
-struct page_directory *kernel_pagedir, *current_pagedir;
+page_directory *kernel_pagedir, *current_pagedir;
/************************** PHYSICAL MEMORY ALLOCATION ************************/
/* Allocates a page of physical memory. */
uint32_t frame_alloc() {
- uint32_t free = bitset_firstFree(&frames);
+ uint32_t free = frames.firstFree();
if (free == (uint32_t) -1) {
PANIC("No more frames to allocate, system is out of memory!");
}
- bitset_set(&frames, free);
+ frames.set(free);
return free;
}
void frame_free(uint32_t id) {
- bitset_clear(&frames, id);
+ frames.clear(id);
}
/************************* PAGING INITIALIZATION *****************************/
@@ -38,11 +38,11 @@ void paging_init(size_t totalRam) {
uint32_t i;
frames.size = totalRam / 0x1000;
- frames.bits = ksbrk(INDEX_FROM_BIT(frames.size));
+ frames.bits = (uint32_t*)ksbrk(INDEX_FROM_BIT(frames.size));
- kernel_pagedir = ksbrk(sizeof(struct page_directory));
+ kernel_pagedir = (page_directory*)ksbrk(sizeof(page_directory));
kernel_pagedir->mappedSegs = 0;
- kernel_pagedir->tablesPhysical = kmalloc_page(&kernel_pagedir->physicalAddr);
+ kernel_pagedir->tablesPhysical = (uint32_t*)kmalloc_page(&kernel_pagedir->physicalAddr);
for (i = 0; i < 1024; i++) {
kernel_pagedir->tables[i] = 0;
kernel_pagedir->tablesPhysical[i] = 0;
@@ -76,7 +76,7 @@ void paging_cleanup() {
/* Switch to a page directory. Can be done if we are sure not to be interrupted by a task switch.
Example use for cross-memory space writing in linker/elf.c */
-void pagedir_switch(struct page_directory *pd) {
+void pagedir_switch(page_directory *pd) {
current_pagedir = pd;
asm volatile("mov %0, %%cr3" : : "r"(pd->physicalAddr));
uint32_t cr0;
@@ -86,11 +86,11 @@ void pagedir_switch(struct page_directory *pd) {
}
/* Creates a new page directory for a process, and maps the kernel page tables on it. */
-struct page_directory *pagedir_new() {
+page_directory *pagedir_new() {
uint32_t i;
- struct page_directory *pd = kmalloc(sizeof(struct page_directory));
- pd->tablesPhysical = kmalloc_page(&pd->physicalAddr);
+ page_directory *pd = new page_directory();
+ pd->tablesPhysical = (uint32_t*)kmalloc_page(&pd->physicalAddr);
pd->mappedSegs = 0;
for (i = 0; i < 1024; i++) {
@@ -106,7 +106,7 @@ struct page_directory *pagedir_new() {
}
/* Deletes a page directory, cleaning it up. */
-void pagedir_delete(struct page_directory *pd) {
+void pagedir_delete(page_directory *pd) {
uint32_t i;
//Unmap segments
while (pd->mappedSegs != 0) seg_unmap(pd->mappedSegs);
@@ -121,9 +121,9 @@ void pagedir_delete(struct page_directory *pd) {
/* Handle a paging fault. First, looks for the corresponding segment.
If the segment was found and it handles the fault, return normally.
Else, display informatinos and return an error. */
-uint32_t paging_fault(struct registers *regs) {
+uint32_t paging_fault(registers *regs) {
size_t addr;
- struct segment_map *seg = 0;
+ segment_map *seg = 0;
asm volatile("mov %%cr2, %0" : "=r"(addr));
seg = current_pagedir->mappedSegs;
@@ -153,14 +153,14 @@ uint32_t paging_fault(struct registers *regs) {
/* Gets the corresponding page in a page directory for a given address.
If make is set, the necessary page table can be created.
Can return 0 if make is not set. */
-struct page *pagedir_getPage(struct page_directory *pd, uint32_t address, int make) {
+page *pagedir_getPage(page_directory *pd, uint32_t address, int make) {
address /= 0x1000;
uint32_t table_idx = address / 1024;
if (pd->tables[table_idx]) {
return &pd->tables[table_idx]->pages[address % 1024];
} else if (make) {
- pd->tables[table_idx] = kmalloc_page(pd->tablesPhysical + table_idx);
+ pd->tables[table_idx] = (page_table*)kmalloc_page(pd->tablesPhysical + table_idx);
memset((uint8_t*)pd->tables[table_idx], 0, 0x1000);
pd->tablesPhysical[table_idx] |= 0x07;
@@ -176,7 +176,7 @@ struct page *pagedir_getPage(struct page_directory *pd, uint32_t address, int ma
}
/* Modifies a page structure so that it is mapped to a frame. */
-void page_map(struct page *page, uint32_t frame, uint32_t user, uint32_t rw) {
+void page_map(page *page, uint32_t frame, uint32_t user, uint32_t rw) {
if (page != 0 && page->frame == 0 && page->present == 0) {
page->present = 1;
page->rw = (rw ? 1 : 0);
@@ -186,7 +186,7 @@ void page_map(struct page *page, uint32_t frame, uint32_t user, uint32_t rw) {
}
/* Modifies a page structure so that it is no longer mapped to a frame. */
-void page_unmap(struct page *page) {
+void page_unmap(page *page) {
if (page != 0) {
page->frame = 0;
page->present = 0;
@@ -194,7 +194,7 @@ void page_unmap(struct page *page) {
}
/* Same as above but also frees the frame. */
-void page_unmapFree(struct page *page) {
+void page_unmapFree(page *page) {
if (page != 0) {
if (page->frame != 0) frame_free(page->frame);
page->frame = 0;
diff --git a/src/kernel/mem/paging.h b/src/kernel/mem/paging.h
index 2d5713f..f91fe37 100644
--- a/src/kernel/mem/paging.h
+++ b/src/kernel/mem/paging.h
@@ -18,32 +18,32 @@ struct page {
};
struct page_table {
- struct page pages[1024];
+ page pages[1024];
};
struct segment_map;
struct page_directory {
- struct page_table *tables[1024]; //Virtual addresses of page tables
+ page_table *tables[1024]; //Virtual addresses of page tables
uint32_t *tablesPhysical; //Pointer to the virtual address of the page directory (contain phys addr of pt)
uint32_t physicalAddr; //Physical address of info above
- struct segment_map *mappedSegs;
+ segment_map *mappedSegs;
};
-extern struct page_directory *kernel_pagedir, *current_pagedir;
+extern page_directory *kernel_pagedir, *current_pagedir;
uint32_t frame_alloc();
void frame_free(uint32_t id);
void paging_init(size_t totalRam);
void paging_cleanup();
-void pagedir_switch(struct page_directory *pd);
-struct page_directory *pagedir_new(); //Creates a brand new empty page directory for a process, with kernel pages
-void pagedir_delete(struct page_directory *pagedir);
-struct page *pagedir_getPage(struct page_directory *pd, uint32_t address, int make);
-void page_map(struct page *page, uint32_t frame, uint32_t user, uint32_t rw);
-void page_unmap(struct page *page);
-void page_unmapFree(struct page *page);
-
-uint32_t paging_fault(struct registers *regs); //returns a boolean : 1 if unhandled, 0 if ok
+void pagedir_switch(page_directory *pd);
+page_directory *pagedir_new(); //Creates a brand new empty page directory for a process, with kernel pages
+void pagedir_delete(page_directory *pagedir);
+page *pagedir_getPage(page_directory *pd, uint32_t address, int make);
+void page_map(page *page, uint32_t frame, uint32_t user, uint32_t rw);
+void page_unmap(page *page);
+void page_unmapFree(page *page);
+
+uint32_t paging_fault(registers *regs); //returns a boolean : 1 if unhandled, 0 if ok
#endif
diff --git a/src/kernel/mem/seg.c b/src/kernel/mem/seg.cpp
index 4a33db3..c29e94e 100644
--- a/src/kernel/mem/seg.c
+++ b/src/kernel/mem/seg.cpp
@@ -4,8 +4,8 @@
/* Call this function when mapping a segment to a page directory.
Calls the appropriate map method and updates the segment's and pagedir's information. */
-struct segment_map *seg_map(struct segment* seg, struct page_directory *pagedir, size_t offset) {
- struct segment_map *sm = seg->map(seg, pagedir, offset);
+segment_map *seg_map(segment* seg, page_directory *pagedir, size_t offset) {
+ segment_map *sm = seg->map(seg, pagedir, offset);
if (sm == 0) return 0;
seg->mappings++;
sm->seg = seg;
@@ -18,18 +18,18 @@ struct segment_map *seg_map(struct segment* seg, struct page_directory *pagedir,
/* Call this function when unmapping a segment from a page directory.
The segment will automatically be deleted if it is not mapped.
Calls the appropriate unmap method and updates the segment's and pagedir's information. */
-void seg_unmap(struct segment_map *map) {
+void seg_unmap(segment_map *map) {
map->seg->unmap(map);
if (map->pagedir->mappedSegs == map) {
map->pagedir->mappedSegs = map->pagedir->mappedSegs->next;
} else {
- struct segment_map *m = map->pagedir->mappedSegs;
+ segment_map *m = map->pagedir->mappedSegs;
while (m->next != 0 && m->next != map) m = m->next;
if (m->next == map) m->next = map->next;
}
map->seg->mappings--;
if (map->seg->mappings == 0) {
- map->seg->delete(map->seg);
+ map->seg->del(map->seg);
kfree(map->seg->seg_data);
kfree(map->seg);
}
@@ -38,37 +38,37 @@ void seg_unmap(struct segment_map *map) {
// ************************************ SIMPLESEG stuff *************
-static struct segment_map* simpleseg_map(struct segment* seg, struct page_directory* pagedir, size_t offset);
-static void simpleseg_unmap(struct segment_map*);
-static void simpleseg_delete(struct segment *seg);
-static int simpleseg_handleFault(struct segment_map* map, size_t addr, int write);
+static segment_map* simpleseg_map(segment* seg, page_directory* pagedir, size_t offset);
+static void simpleseg_unmap(segment_map*);
+static void simpleseg_delete(segment *seg);
+static int simpleseg_handleFault(segment_map* map, size_t addr, int write);
/* Call this when creating a simpleseg.
Creates the simpleseg structure and the segment structure and fills them up. */
-struct segment* simpleseg_make(size_t start, size_t len, int writable) {
- struct simpleseg *ss = kmalloc(sizeof(struct simpleseg));
- struct segment *se = kmalloc(sizeof(struct segment));
+segment* simpleseg_make(size_t start, size_t len, int writable) {
+ simpleseg *ss = new simpleseg();
+ segment *se = new segment();
se->seg_data = ss;
se->mappings = 0;
se->map = simpleseg_map;
se->unmap = simpleseg_unmap;
- se->delete = simpleseg_delete;
+ se->del = simpleseg_delete;
se->handle_fault = simpleseg_handleFault;
ss->writable = writable; ss->start = start; ss->len = len;
return se;
}
/* For internal use only. Called when a simpleseg is mapped to a pagedirectory. */
-struct segment_map* simpleseg_map(struct segment* seg, struct page_directory* pagedir, size_t offset) {
- struct segment_map *sm = kmalloc(sizeof(struct segment_map));
- sm->start = ((struct simpleseg*)(seg->seg_data))->start;
- sm->len = ((struct simpleseg*)(seg->seg_data))->len;
+segment_map* simpleseg_map(segment* seg, page_directory* pagedir, size_t offset) {
+ segment_map *sm = new segment_map();
+ sm->start = ((simpleseg*)(seg->seg_data))->start;
+ sm->len = ((simpleseg*)(seg->seg_data))->len;
return sm;
}
/* For internal use only. Called when a simpleseg is unmapped.
Frees all the allocated pages. */
-void simpleseg_unmap(struct segment_map* sm) {
+void simpleseg_unmap(segment_map* sm) {
size_t i;
for (i = sm->start; i < sm->start + sm->len; i += 0x1000) {
page_unmapFree(pagedir_getPage(sm->pagedir, i, 0));
@@ -76,28 +76,28 @@ void simpleseg_unmap(struct segment_map* sm) {
}
/* For internal use only. Handles a page fault. Can allocate and map a frame if necessary. */
-int simpleseg_handleFault(struct segment_map* sm, size_t addr, int write) {
- struct simpleseg *ss = sm->seg->seg_data;
+int simpleseg_handleFault(segment_map* sm, size_t addr, int write) {
+ simpleseg *ss = (simpleseg*)sm->seg->seg_data;
if (write && !ss->writable) return 1;
addr &= 0xFFFFF000;
- struct page *p = pagedir_getPage(sm->pagedir, addr, 1);
+ page *p = pagedir_getPage(sm->pagedir, addr, 1);
if (p->frame != 0) return 1;
page_map(p, frame_alloc(), 1, ss->writable);
return 0;
}
/* For internal use only. Called when the simpleseg is deleted. Does nothing. */
-void simpleseg_delete(struct segment* seg) {
+void simpleseg_delete(segment* seg) {
}
/* Call this to resize a simpleseg. Ajusts the size and frees pages if the new size is smaller.*/
-int simpleseg_resize(struct segment_map *map, size_t len) {
+int simpleseg_resize(segment_map *map, size_t len) {
size_t i;
if (map == 0) return -1;
- if (map->seg->delete != simpleseg_delete) return -2; //check segment is a simpleseg
+ if (map->seg->del != simpleseg_delete) return -2; //check segment is a simpleseg
- struct simpleseg *s = (struct simpleseg*)map->seg->seg_data;
+ simpleseg *s = (simpleseg*)map->seg->seg_data;
if (len & 0xFFF) len = (len & 0xFFFFF000) + 0x1000;
if (len < map->len) {
for (i = map->start + len; i < map->start + map->len; i += 0x1000) {
diff --git a/src/kernel/mem/seg.h b/src/kernel/mem/seg.h
index d37ba59..ea95dfa 100644
--- a/src/kernel/mem/seg.h
+++ b/src/kernel/mem/seg.h
@@ -12,23 +12,23 @@ struct segment {
int mappings;
// these 4 functions must not be used directly by anyone
- struct segment_map* (*map)(struct segment* seg, struct page_directory* pagedir, size_t offset);
- void (*unmap)(struct segment_map*);
- void (*delete)(struct segment* seg);
- int (*handle_fault)(struct segment_map* map, size_t addr, int write); //0 if ok, 1 if segfault
+ segment_map* (*map)(segment* seg, page_directory* pagedir, size_t offset);
+ void (*unmap)(segment_map*);
+ void (*del)(segment* seg);
+ int (*handle_fault)(segment_map* map, size_t addr, int write); //0 if ok, 1 if segfault
};
struct segment_map {
- struct segment* seg;
- struct page_directory* pagedir;
+ segment* seg;
+ page_directory* pagedir;
size_t start, len;
- struct segment_map *next;
+ segment_map *next;
};
//parameter offset in seg_map doesn't need to be used
-struct segment_map *seg_map(struct segment* seg, struct page_directory* pagedir, size_t offset);
-/* When unmapping a segment, the segment is deleted if it is not mapped anywhere anymore. */
-void seg_unmap(struct segment_map* map);
+segment_map *seg_map(segment* seg, page_directory* pagedir, size_t offset);
+// When unmapping a segment, the segment is deleted if it is not mapped anywhere anymore.
+void seg_unmap(segment_map* map);
/// ************************************* SIMPLESEG stuff *****************
@@ -37,7 +37,7 @@ struct simpleseg {
size_t start, len;
};
-struct segment* simpleseg_make(size_t start, size_t len, int writable);
-int simpleseg_resize(struct segment_map *map, size_t len);
+segment* simpleseg_make(size_t start, size_t len, int writable);
+int simpleseg_resize(segment_map *map, size_t len);
#endif