BarryServer : Git

All the code for all my projects
// BarryServer : Git / Nucleus / commit / 08afe80d1ca157b9cddb31ee48ab0e6e1823f559

// Related

Nucleus

Barry Object manager and heap in kernel library 08afe80 (3 years, 2 months ago)
diff --git a/include/nucleus/lib.h b/include/nucleus/lib.h
index a352728..c53f7b9 100644
--- a/include/nucleus/lib.h
+++ b/include/nucleus/lib.h
@@ -4,6 +4,9 @@
 #include <stdarg.h>
 #include <stddef.h>
 
+void *kmalloc(size_t size);
+void kfree(void *addr);
+
 int vsprintf(char *buf, const char *fmt, va_list args);
 int sprintf(char *buf, const char *fmt, ...);
 
diff --git a/include/nucleus/memory.h b/include/nucleus/memory.h
index c91e40c..057ff0b 100644
--- a/include/nucleus/memory.h
+++ b/include/nucleus/memory.h
@@ -59,9 +59,6 @@ page_dir_t clone_dir(void);
 void init_paging(void);
 void cpu_load_paging(void);
 
-void *kmalloc(size_t size);
-void kfree(void *addr);
-
 Page *create_page(ObjectList *cache, page_t frame, off_t offset);
 Page *find_page(ObjectList *cache, off_t offset);
 void install_page(uintptr_t addr, Page *page, int prot);
diff --git a/kernel/cpu.c b/kernel/cpu.c
index be56047..e7ab334 100644
--- a/kernel/cpu.c
+++ b/kernel/cpu.c
@@ -12,7 +12,7 @@
  */
 
 #include <nucleus/cpu.h>
-#include <nucleus/memory.h>
+#include <nucleus/lib.h>
 #include <nucleus/task.h>
 #include "desc.h"
 
diff --git a/kernel/idt.c b/kernel/idt.c
index dc596f4..0f2bdfd 100644
--- a/kernel/idt.c
+++ b/kernel/idt.c
@@ -43,13 +43,8 @@ void
 isr_handler(struct InterruptFrame frame)
 {
 	if (!interrupts[frame.intnum] && frame.intnum < 32)
-		panic("[CPU#%d] Failed to handle exception %d (%#.8x) @ %#.8x",
-		      cpu->id, frame.intnum, frame.err, frame.eip);
-
-	/* Run registered handler */
-	int_handler_t handler = interrupts[frame.intnum];
-	if (handler)
-		handler(&frame);
+		panic("Failed to handle exception %d (%#.8x) @ %#.8x",
+		      frame.intnum, frame.err, frame.eip);
 
 	/* Send EOI */
 	if (frame.intnum >= 40 && frame.intnum < 48)
@@ -60,6 +55,11 @@ isr_handler(struct InterruptFrame frame)
 	/* Send APIC EOI */
 	if (apic)
 		LAPIC(0xB0) = 0;
+
+	/* Run registered handler */
+	int_handler_t handler = interrupts[frame.intnum];
+	if (handler)
+		handler(&frame);
 }
 
 /* Register an exception handler */
diff --git a/kernel/main.c b/kernel/main.c
index 90c102d..ac38a8e 100644
--- a/kernel/main.c
+++ b/kernel/main.c
@@ -19,7 +19,6 @@
 #include "multiboot.h"
 
 extern char _bss[], _end[];
-void page_fault_handler(struct InterruptFrame *frame);
 void ipiq_handler(struct InterruptFrame *frame);
 void cpu_load(void);
 
@@ -39,25 +38,21 @@ kmain(struct MultibootInfo *mbinfo)
 	cpu_load();
 	register_ipi(0, ipiq_handler);
 
-	/* Initialise paging */
+	/* Initialise sub-systems */
 	init_paging();
-	/* Initialise multitasking */
 	init_tasking();
-	/* Initialise the VFS */
 	init_vfs();
 	/* Search ACPI tables */
 	init_acpi(ebda);
 	init_pci();
 
-	/* Enable userspace page-fault handler */
-	register_exception(14, page_fault_handler);
-
 	/* Mount drive */
 	mkdir("root", 0);
 	mount("/dev/hd0", "/root", "ext2fs", MS_RDONLY, NULL);
 	chroot("/root");
 	chdir("/");
 	mount("devfs", "/dev", "devfs", 0, NULL);
+	mount("tmpfs", "/tmp", "tmpfs", 0, NULL);
 
 	/* Start init */
 	char *argv[] = { "init", NULL };
diff --git a/kernel/uname.c b/kernel/uname.c
index a9ec735..c0bb421 100644
--- a/kernel/uname.c
+++ b/kernel/uname.c
@@ -9,7 +9,7 @@
 #include <nucleus/memory.h>
 
 static const char *SYSNAME = "Nucleus";
-static const char *RELEASE = "0.9.3";
+static const char *RELEASE = "0.9.4";
 static const char *VERSION = "SMP PREEMPT "__DATE__" "__TIME__;
 static const char *MACHINE = "x86";
 
@@ -17,17 +17,11 @@ static const char *MACHINE = "x86";
 int
 uname(struct utsname *buf)
 {
+	if (!buf || !verify_access(buf, sizeof(struct utsname), PROT_WRITE))
+		return -EFAULT;
 	strcpy(buf->sysname, SYSNAME);
 	strcpy(buf->release, RELEASE);
 	strcpy(buf->version, VERSION);
 	strcpy(buf->machine, MACHINE);
-}
-
-/* Kernel information system call */
-int
-sys_uname(struct utsname *buf)
-{
-	if (!buf || !verify_access(buf, sizeof(struct utsname), PROT_WRITE))
-		return -EFAULT;
-	uname(buf);
+	return 0;
 }
diff --git a/memory/heap.c b/lib/heap.c
similarity index 98%
rename from memory/heap.c
rename to lib/heap.c
index a0e696a..64e2d85 100644
--- a/memory/heap.c
+++ b/lib/heap.c
@@ -11,7 +11,7 @@
 #include <nucleus/lib.h>
 
 #define KHEAP_START 0x200000 /* 2MB */
-#define KHEAP_END   0x800000 /* 2MB */
+#define KHEAP_END   0x800000 /* 8MB */
 #define BLOCK_SIZE  16
 
 /* Structure for a memory block header */
diff --git a/object/list.c b/lib/object/list.c
similarity index 100%
rename from object/list.c
rename to lib/object/list.c
diff --git a/object/lock.c b/lib/object/lock.c
similarity index 100%
rename from object/lock.c
rename to lib/object/lock.c
diff --git a/object/manager.c b/lib/object/manager.c
similarity index 97%
rename from object/manager.c
rename to lib/object/manager.c
index 7e6da04..09d0b07 100644
--- a/object/manager.c
+++ b/lib/object/manager.c
@@ -87,6 +87,7 @@ copy(void *addr)
 	return child;
 }
 
+/* Check how many times an object is referenced */
 refcount_t
 usage(void *addr)
 {
diff --git a/memory/fault.c b/memory/fault.c
index 80dc5e0..d8accb6 100644
--- a/memory/fault.c
+++ b/memory/fault.c
@@ -13,9 +13,10 @@
 #include <nucleus/lib.h>
 #include <nucleus/memory.h>
 #include <nucleus/task.h>
-#include "paging.h"
 #include "namespace.h"
 
+extern page_t zeroFrame;
+
 VMRegion *find_region(uintptr_t addr);
 void copy_page_frame(page_t src, page_t dest);
 
@@ -179,13 +180,15 @@ page_fault_handler(struct InterruptFrame *frame)
 	uint8_t write   = frame->err & (1 << 1);
 	uint8_t user    = frame->err & (1 << 2);
 
+	ASSERT(current && current->vm);
+
 	/* Iterate VM Regions */
 	VMRegion *region = find_region(addr);
 	/* Not in a region */
 	if (__builtin_expect(!region, 0)) {
 		page_t pg = get_page(addr);
-		panic("[CPU#%d] Page Fault [%d:%d] (%#.8x -> %#.8x [tbl:%d, pg:%d][%#.8x], %s, %s, %s)",
-		      cpu->id, current->tgid, current->tid, frame->eip,
+		panic("Page Fault [%d:%d] (%#.8x -> %#.8x [tbl:%d, pg:%d][%#.8x], %s, %s, %s)",
+		      current->tgid, current->tid, frame->eip,
 		      addr, (addr >> 12) / 1024, (addr >> 12) % 1024, pg,
 		      present ? "present" : "not present",
 		      write ? "write" : "read",
@@ -202,21 +205,3 @@ page_fault_handler(struct InterruptFrame *frame)
 	if (!present && !write)
 		return not_present_read(region, addr);
 }
-
-/* Early (pre-VFS/tasking) page fault handler */
-void
-early_page_fault_handler(struct InterruptFrame *frame)
-{
-	uintptr_t addr;
-	asm volatile("mov %%cr2, %0" : "=r" (addr));
-	uint8_t present = frame->err & (1 << 0);
-	uint8_t write   = frame->err & (1 << 1);
-	uint8_t user    = frame->err & (1 << 2);
-	if (!PAGE_ADDR(addr))
-		panic("Null dereference @ %#.8x -> %#.8x", frame->eip, addr);
-	ASSERT(!present);
-	kprintf("Allocating frame for %#.8x [%#.8x] CPU#%d",
-	        addr, frame->eip, cpu->id);
-	/* Allocate a page */
-	set_page(addr, alloc_frame() | PTE_PRESENT | PTE_WRITE | PTE_GLOBAL);
-}
diff --git a/memory/page.c b/memory/page.c
index 986b4d9..8c61aa6 100644
--- a/memory/page.c
+++ b/memory/page.c
@@ -6,9 +6,10 @@
 
 #include <nucleus/memory.h>
 #include <nucleus/object.h>
-#include "paging.h"
 #include "namespace.h"
 
+extern page_t zeroFrame;
+
 static void page_delete(Object *);
 
 /* Page object type */
diff --git a/memory/paging.c b/memory/paging.c
index c88eee1..e9a8a83 100644
--- a/memory/paging.c
+++ b/memory/paging.c
@@ -8,9 +8,9 @@
 #include <stdint.h>
 #include <nucleus/lib.h>
 #include <nucleus/memory.h>
-#include "paging.h"
 
 void copy_page_frame(uintptr_t src, uintptr_t dest);
+void page_fault_handler(struct InterruptFrame *frame);
 
 page_t zeroFrame;
 static page_dir_t kernelDir;
@@ -151,7 +151,7 @@ init_paging(void)
 	 */
 
 	/* Use kernel directory */
-	register_exception(14, early_page_fault_handler);
+	register_exception(14, page_fault_handler);
 	cpu_load_paging();
 
 	/* Allocate a kernel stack */
diff --git a/memory/paging.h b/memory/paging.h
deleted file mode 100644
index eb7c75f..0000000
--- a/memory/paging.h
+++ /dev/null
@@ -1,12 +0,0 @@
-#ifndef MEMORY_PAGING_H
-#define MEMORY_PAGING_H
-
-#include <stdint.h>
-#include <nucleus/cpu.h>
-#include <nucleus/memory.h>
-
-extern page_t zeroFrame;
-
-void early_page_fault_handler(struct InterruptFrame *frame);
-
-#endif
diff --git a/task/clone.c b/task/clone.c
index bd87f52..377b869 100644
--- a/task/clone.c
+++ b/task/clone.c
@@ -71,11 +71,5 @@ clone(int flags)
 	put(child);
 	exit_critical_section();
 end:
-	if (!tid && !current->inSyscall) {
-		outb(0x20, 0x20);
-		if (apic)
-			LAPIC(0xB0) = 0;
-	}
-
 	return tid;
 }
diff --git a/vfs/ext2fs/super.c b/vfs/ext2fs/super.c
index 5983c95..defa684 100644
--- a/vfs/ext2fs/super.c
+++ b/vfs/ext2fs/super.c
@@ -2,6 +2,7 @@
 #include <sys/file.h>
 #include <sys/mount.h>
 #include <sys/stat.h>
+#include <nucleus/lib.h>
 #include <nucleus/task.h>
 #include <nucleus/vfs.h>
 #include "ext2fs.h"