summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/kernel/Makefile2
-rw-r--r--src/kernel/core/loader_.asm22
-rw-r--r--src/kernel/lib/cpp.cpp14
-rw-r--r--src/kernel/link.ld6
-rw-r--r--src/kernel/linker/elf.cpp4
-rw-r--r--src/kernel/mem/paging.cpp6
-rw-r--r--src/kernel/mem/seg.cpp98
-rw-r--r--src/kernel/mem/seg.h41
-rw-r--r--src/kernel/task/task.cpp9
9 files changed, 127 insertions, 75 deletions
diff --git a/src/kernel/Makefile b/src/kernel/Makefile
index 6060911..a1acc49 100644
--- a/src/kernel/Makefile
+++ b/src/kernel/Makefile
@@ -2,7 +2,7 @@ Out = kernel.elf
Obj = core/loader_.o core/kmain.o core/sys.o \
core/monitor.o task/timer.o \
task/idt.o task/idt_.o task/task.o task/task_.o task/syscall.o task/sched.o \
- lib/bitset.o lib/std.o \
+ lib/bitset.o lib/std.o lib/cpp.o \
mem/mem.o mem/paging.o mem/gdt.o mem/_dlmalloc.o mem/seg.o \
linker/elf.o
diff --git a/src/kernel/core/loader_.asm b/src/kernel/core/loader_.asm
index 07de52c..28d1d2f 100644
--- a/src/kernel/core/loader_.asm
+++ b/src/kernel/core/loader_.asm
@@ -2,6 +2,7 @@
[GLOBAL loader] ; making entry point visible to linker
[EXTERN kmain] ; kmain is defined in kmain.c
[EXTERN tasking_tmpStack] ; a temporary 4k stack used by tasking, and used when setting up kernel stuff
+EXTERN start_ctors, end_ctors, start_dtors, end_dtors
; setting up the Multiboot header - see GRUB docs for details
MODULEALIGN equ 1<<0 ; align loaded modules on page boundaries
@@ -36,9 +37,30 @@ higherhalf: ; now we're running in higher half
add ebx, k_highhalf_addr ; update the MB info structure so that it is in the new seg
push ebx ; pass Multiboot info structure
+static_ctors_loop:
+ mov ebx, start_ctors
+ jmp .test
+.body:
+ call [ebx]
+ add ebx,4
+.test:
+ cmp ebx, end_ctors
+ jb .body
+
call kmain ; call kernel proper
cli ; disable interupts
+
+static_dtors_loop:
+ mov ebx, start_dtors
+ jmp .test
+.body:
+ call [ebx]
+ add ebx,4
+.test:
+ cmp ebx, end_dtors
+ jb .body
+
hang:
hlt ; halt machine should kernel return
jmp hang
diff --git a/src/kernel/lib/cpp.cpp b/src/kernel/lib/cpp.cpp
new file mode 100644
index 0000000..d97e51a
--- /dev/null
+++ b/src/kernel/lib/cpp.cpp
@@ -0,0 +1,14 @@
+#include "cpp.h"
+#include <core/sys.h>
+
+//Enables pure virtual functions
+extern "C" void __cxa_pure_virtual() {
+ //do nothing (or not)
+ PANIC("Called pure virtual function.");
+}
+
+//Enables global objects
+void *__dso_handle;
+extern "C" int __cxa_atexit(void (*f)(void*), void *p, void *d) { return 0; }
+
+
diff --git a/src/kernel/link.ld b/src/kernel/link.ld
index fcdb0bb..00f4dff 100644
--- a/src/kernel/link.ld
+++ b/src/kernel/link.ld
@@ -20,6 +20,12 @@ SECTIONS{
}
.data ALIGN (0x1000) : AT(ADDR(.data) - k_highhalf_addr) {
+ start_ctors = .;
+ *(.ctor*)
+ end_ctors = .;
+ start_dtors = .;
+ *(.dtor*)
+ end_dtors = .;
*(.data)
}
diff --git a/src/kernel/linker/elf.cpp b/src/kernel/linker/elf.cpp
index c94e5c0..9477e8a 100644
--- a/src/kernel/linker/elf.cpp
+++ b/src/kernel/linker/elf.cpp
@@ -17,14 +17,14 @@ thread_entry elf_load(uint8_t *data, process* process) {
size_t dataseg = 0;
if (elf_check(data)) return 0;
- page_directory *r = current_pagedir;
cli();
+ page_directory *r = current_pagedir;
pagedir_switch(process->pagedir);
phdr = (elf_phdr*)((uint8_t*)(data + ehdr->e_phoff));
for (i = 0; i < ehdr->e_phnum; i++) {
if (phdr[i].p_type == PT_LOAD) {
- seg_map(simpleseg_make(phdr[i].p_vaddr, phdr[i].p_memsz, (phdr[i].p_flags & PF_W) != 0), process->pagedir, 0);
+ (new simpleseg(phdr[i].p_vaddr, phdr[i].p_memsz, (phdr[i].p_flags & PF_W) != 0))->map(process->pagedir, 0);
memcpy((uint8_t*)phdr[i].p_vaddr, data + phdr[i].p_offset, phdr[i].p_filesz);
if (phdr[i].p_memsz > phdr[i].p_filesz) {
memset((uint8_t*)phdr[i].p_vaddr + phdr[i].p_memsz, 0, phdr[i].p_memsz - phdr[i].p_filesz);
diff --git a/src/kernel/mem/paging.cpp b/src/kernel/mem/paging.cpp
index eb7e615..496b70c 100644
--- a/src/kernel/mem/paging.cpp
+++ b/src/kernel/mem/paging.cpp
@@ -109,7 +109,7 @@ page_directory *pagedir_new() {
void pagedir_delete(page_directory *pd) {
uint32_t i;
//Unmap segments
- while (pd->mappedSegs != 0) seg_unmap(pd->mappedSegs);
+ while (pd->mappedSegs != 0) pd->mappedSegs->seg->unmap(pd->mappedSegs);
//Cleanup page tables
for (i = 0; i < FIRST_KERNEL_PAGETABLE; i++) {
kfree_page(pd->tables[i]);
@@ -133,7 +133,9 @@ uint32_t paging_fault(registers *regs) {
}
if (seg != 0) {
- if (seg->seg->handle_fault(seg, addr, (regs->err_code & 0x2) && (regs->eip < K_HIGHHALF_ADDR)) != 0) seg = 0;
+ if (seg->seg->handle_fault(seg, addr, (regs->err_code & 0x2) && (regs->eip < K_HIGHHALF_ADDR)) != 0) {
+ seg = 0;
+ }
}
if (seg == 0) {
diff --git a/src/kernel/mem/seg.cpp b/src/kernel/mem/seg.cpp
index c29e94e..06209a1 100644
--- a/src/kernel/mem/seg.cpp
+++ b/src/kernel/mem/seg.cpp
@@ -1,14 +1,22 @@
#include "seg.h"
#include "mem.h"
#include <core/sys.h>
+#include <lib/cpp.h>
+
+segment::segment() {
+ mappings = 0;
+}
+segment::~segment() {
+}
/* 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. */
-segment_map *seg_map(segment* seg, page_directory *pagedir, size_t offset) {
- segment_map *sm = seg->map(seg, pagedir, offset);
+segment_map *segment::map(page_directory *pagedir, size_t offset) {
+ segment_map *sm = i_map(pagedir, offset);
if (sm == 0) return 0;
- seg->mappings++;
- sm->seg = seg;
+ mappings++;
+
+ sm->seg = this;
sm->pagedir = pagedir;
sm->next = pagedir->mappedSegs;
pagedir->mappedSegs = sm;
@@ -18,57 +26,48 @@ segment_map *seg_map(segment* seg, page_directory *pagedir, size_t offset) {
/* 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(segment_map *map) {
- map->seg->unmap(map);
+void segment::unmap(segment_map *map) {
+ ASSERT(this == map->seg);
+ i_unmap(map);
if (map->pagedir->mappedSegs == map) {
- map->pagedir->mappedSegs = map->pagedir->mappedSegs->next;
+ map->pagedir->mappedSegs = map->next;
} else {
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->del(map->seg);
- kfree(map->seg->seg_data);
- kfree(map->seg);
+ delete map;
+ mappings--;
+ if (mappings == 0) {
+ delete this;
}
- kfree (map);
}
// ************************************ SIMPLESEG stuff *************
-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. */
-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->del = simpleseg_delete;
- se->handle_fault = simpleseg_handleFault;
- ss->writable = writable; ss->start = start; ss->len = len;
- return se;
+simpleseg::simpleseg(size_t s, size_t l, int w) : segment() {
+ writable = w;
+ start = s;
+ len = l;
+}
+
+simpleseg::~simpleseg() {
}
/* For internal use only. Called when a simpleseg is mapped to a pagedirectory. */
-segment_map* simpleseg_map(segment* seg, page_directory* pagedir, size_t offset) {
+segment_map* simpleseg::i_map(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;
+ sm->start = start;
+ sm->len = len;
return sm;
}
/* For internal use only. Called when a simpleseg is unmapped.
Frees all the allocated pages. */
-void simpleseg_unmap(segment_map* sm) {
+void simpleseg::i_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,38 +75,35 @@ void simpleseg_unmap(segment_map* sm) {
}
/* For internal use only. Handles a page fault. Can allocate and map a frame if necessary. */
-int simpleseg_handleFault(segment_map* sm, size_t addr, int write) {
- simpleseg *ss = (simpleseg*)sm->seg->seg_data;
- if (write && !ss->writable) return 1;
+int simpleseg::handle_fault(segment_map* sm, size_t addr, int write) {
+ ASSERT(sm->seg == this);
+
+ if (write && !writable) return 1;
addr &= 0xFFFFF000;
page *p = pagedir_getPage(sm->pagedir, addr, 1);
if (p->frame != 0) return 1;
- page_map(p, frame_alloc(), 1, ss->writable);
+ page_map(p, frame_alloc(), 1, writable);
return 0;
}
-/* For internal use only. Called when the simpleseg is deleted. Does nothing. */
-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(segment_map *map, size_t len) {
+int simpleseg::resize(segment_map *map, size_t nlen) {
+ ASSERT(this == map->seg);
+
size_t i;
if (map == 0) return -1;
- if (map->seg->del != simpleseg_delete) return -2; //check segment is a simpleseg
- 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) {
+ if (nlen & 0xFFF) nlen = (nlen & 0xFFFFF000) + 0x1000;
+ if (nlen < map->len) {
+ for (i = map->start + nlen; i < map->start + map->len; i += 0x1000) {
page_unmapFree(pagedir_getPage(map->pagedir, i, 0));
}
- map->len = len;
- s->len = len;
- } else if (len > map->len) {
- map->len = len;
- s->len = len;
+ map->len = nlen;
+ len = nlen;
+ } else if (nlen > map->len) {
+ map->len = nlen;
+ len = nlen;
}
return 0;
}
diff --git a/src/kernel/mem/seg.h b/src/kernel/mem/seg.h
index ea95dfa..bec0d8c 100644
--- a/src/kernel/mem/seg.h
+++ b/src/kernel/mem/seg.h
@@ -8,14 +8,22 @@
struct segment_map;
struct segment {
- void* seg_data;
+ private:
+ // these functions must not be used directly by anyone
+ virtual segment_map* i_map(page_directory* pagedir, size_t offset) = 0;
+ virtual void i_unmap(segment_map*) = 0;
+
+ public:
int mappings;
- // these 4 functions must not be used directly by anyone
- 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
+ segment();
+ virtual ~segment();
+
+ virtual int handle_fault(segment_map* map, size_t addr, int write) = 0; //0 if ok, 1 if segfault
+ //parameter offset in seg_map doesn't need to be used
+ segment_map *map(page_directory* pagedir, size_t offset);
+ // When unmapping a segment, the segment is deleted if it is not mapped anywhere anymore.
+ void unmap(segment_map* map);
};
struct segment_map {
@@ -25,19 +33,22 @@ struct segment_map {
segment_map *next;
};
-//parameter offset in seg_map doesn't need to be used
-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 *****************
-struct simpleseg {
+struct simpleseg : segment {
int writable;
size_t start, len;
-};
-segment* simpleseg_make(size_t start, size_t len, int writable);
-int simpleseg_resize(segment_map *map, size_t len);
+ simpleseg(size_t start, size_t len, int writable);
+ virtual ~simpleseg();
+
+ virtual int handle_fault(segment_map* map, size_t addr, int write); //0 if ok, 1 if segfault
+
+ int resize(segment_map *map, size_t len);
+
+ private:
+ virtual segment_map* i_map(page_directory* pagedir, size_t offset);
+ virtual void i_unmap(segment_map*);
+};
#endif
diff --git a/src/kernel/task/task.cpp b/src/kernel/task/task.cpp
index 96864da..51219e0 100644
--- a/src/kernel/task/task.cpp
+++ b/src/kernel/task/task.cpp
@@ -287,7 +287,7 @@ process::process(process* parent, uint32_t uid, uint32_t privilege) {
stack = 0;
if (privilege >= PL_USER) { //We are running in user mode
size_t stacksBottom = K_HIGHHALF_ADDR - 0x01000000;
- seg_map(simpleseg_make(stacksBottom, USER_STACK_SIZE, 1), pagedir, 0);
+ (new simpleseg(stacksBottom, USER_STACK_SIZE, 1))->map(pagedir, 0);
stack = stacksBottom + USER_STACK_SIZE - 4;
}
@@ -350,9 +350,9 @@ size_t process_sbrk(size_t size) {
size_t end = start + size;
if (end & 0x0FFF) end = (end & 0xFFFFF000) + 0x1000;
- segment *s = simpleseg_make(start, end - start, 1);
+ segment *s = new simpleseg(start, end - start, 1);
if (s == 0) return -5;
- p->dataseg = seg_map(s, p->pagedir, 0);
+ p->dataseg = s->map(p->pagedir, 0);
if (p->dataseg == 0) return -1;
ret = start;
@@ -363,7 +363,8 @@ size_t process_sbrk(size_t size) {
if (end <= start) return -1;
if (end & 0x0FFF) end = (end & 0xFFFFF000) + 0x1000;
- simpleseg_resize(p->dataseg, end - start);
+ simpleseg *s = (simpleseg*)(p->dataseg->seg);
+ s->resize(p->dataseg, end - start);
ret = p->data;
p->data += size;