Remove directory cloning, add spawn() based multitasking stub
diff --git a/src/kernel/boot.s b/src/kernel/boot.s
index e235516..bf97a56 100644
--- a/src/kernel/boot.s
+++ b/src/kernel/boot.s
@@ -136,6 +136,7 @@
 	
 	add ebx, 0xC0000000			; Translate to virtual address
 	push ebx					; Holds multiboot header location
+	push esp 					; Initial kernel stack
 	call kmain
 
 .end:
diff --git a/src/kernel/io.c b/src/kernel/io.c
index 0cf04b2..baa050f 100644
--- a/src/kernel/io.c
+++ b/src/kernel/io.c
@@ -48,6 +48,11 @@
 	return dest;
 }
 
+void strcpy(char *dest, char *src)
+{
+	memcpy(dest, src, strlen(src) + 1);
+}
+
 uint strlen(char *a)
 {
 	int i = 0;
diff --git a/src/kernel/io.h b/src/kernel/io.h
index f251717..a7c1d8b 100644
--- a/src/kernel/io.h
+++ b/src/kernel/io.h
@@ -22,6 +22,7 @@
 /* Random string.h stuff, TODO: move to own header */
 void *memset(void *s, int c, size_t n);
 void *memcpy(void *dest, const void *src, size_t n);
+void strcpy(char *dest, char *src);
 int strcmp(char *a, char *b);
 uint strlen(char *a);
 
diff --git a/src/kernel/paging.c b/src/kernel/paging.c
index 62636b3..4d0aa94 100644
--- a/src/kernel/paging.c
+++ b/src/kernel/paging.c
@@ -10,7 +10,7 @@
 static ulong num_frames;
 
 static uint first_page_table[1024] __attribute__((aligned(4096)));
-static uint kernel_page_directory[1024] __attribute__((aligned(4096)));
+uint kernel_page_directory[1024] __attribute__((aligned(4096)));
 
 /* frame utils */
 
@@ -101,20 +101,39 @@
 	return page_table;
 }
 
+void unmap_page(uint *dir, void *virt)
+{
+	uint page = ((size_t)virt / 0x1000) % 1024;
+	uint *table = get_or_create_table(dir, (size_t)virt >> 22, false, false);
+
+	table[page] = 0;
+}
+
+void map_page_to(uint *dir, void *virt, void *frame_p, bool writable, bool user)
+{
+	uint page = ((size_t)virt / 0x1000) % 1024;
+	uint *table = get_or_create_table(dir, (size_t)virt >> 22, false, false);
+
+	table[page] = (((uint)frame_p) ^ 0xfff) | 1 | writable << 1 | user << 2;
+}
+
 void alloc_kernel_page(uint *virt)
 {
+	alloc_page(kernel_page_directory, virt);
+}
+
+void alloc_page(uint *dir, uint *virt)
+{
 	// Page number % pages per table
 	uint page = ((size_t)virt / 0x1000) % 1024;
-	uint *table = get_or_create_table(kernel_page_directory, (size_t)virt >> 22,
-									  false, false);
+	uint *table = get_or_create_table(dir, (size_t)virt >> 22, false, false);
 
 	alloc_frame(&table[page], false, false);
 }
 
 void alloc_kernel_page_range(uint *from, uint *to)
 {
-	uint f = (size_t)from / 0x1000,
-		t = (size_t)to / 0x1000;
+	uint f = (size_t)from / 0x1000, t = (size_t)to / 0x1000;
 
 	do
 	{
diff --git a/src/kernel/paging.h b/src/kernel/paging.h
index d7121a6..045b872 100644
--- a/src/kernel/paging.h
+++ b/src/kernel/paging.h
@@ -11,9 +11,12 @@
 /* defined in switch_table.s */
 extern uint load_page_directory(uint table_address);
 extern void enable_paging();
+extern uint kernel_page_directory[1024];
 
 void init_paging();
 
+void map_page_to(uint *dir, void *virt, void *frame_p, bool writable, bool user);
 void alloc_frame(uint *page_table_entry, bool user, bool writable);
+void alloc_page(uint *dir, uint *page);
 void alloc_kernel_page(uint *page);
 void page_fault(struct registers *regs);
diff --git a/src/kernel/task.c b/src/kernel/task.c
index b4c1b33..3d27850 100644
--- a/src/kernel/task.c
+++ b/src/kernel/task.c
@@ -3,59 +3,55 @@
 #include "io.h"
 #include "paging.h"
 
-uint clone_page_table(uint table)
+struct process processes[1024] = {0};
+struct ll_task_i *first_task = NULL, *last_task = NULL, *current_task = NULL;
+
+void init_tasks(uint kernel_esp, uint kernel_ebp, uint kernel_eip)
 {
-	// is this table a 4mb mapping OR is it not mapped?
-	// See Intel manual volume 3 figure 4-4.
+	asm volatile("cli");
 
-	if (table & 1 << 7 || !(table & 1))
-	{
-		// if so, return as is, 4mb mappings only used by kernel and should
-		// be mapped to the same location
-		return table;
-	}
-	else
-	{
-		// else, look for the used pages in this table, copy them to a new
-		// frame, and store them in a new table, then return that table
-		// TODO: add copy on write
+	processes[0] = (struct process){
+		.exists = true,
+		.id = 0,
+		.ring = 0,
+		.uid = 0,
+	};
+	strcpy(processes[0].name, "kernel");
 
-		uint new_p;
-		uint *new = kmalloc_ap(sizeof(uint[1024]), (void **)&new_p);
+	first_task = last_task = current_task = malloc(sizeof(struct ll_task_i));
 
-		// We don't care about the lower 12 bits, just want the address
-		uint *old_virt = PHYS_TO_VIRT((uint *)(table ^ 0xfff));
+	first_task->next = NULL;
+	first_task->task = (struct task){
+		.proc = &processes[0],
+		.esp = kernel_esp,
+		.ebp = kernel_ebp,
+		.eip = kernel_eip,
+		.kernel = true,
+		.page_directory = kernel_page_directory,
+	};
 
-		for (int i = 0; i < 1024; i++)
-		{
-			if (old_virt[i] & 1)
-			{
-				// If mapped
-
-				// again, don't care about bottom 12 bits
-				uchar *old_page = PHYS_TO_VIRT(old_virt[i] ^ 0xfff);
-
-				alloc_frame(&new[i], BOOL(old_virt[i] & 1 << 2),
-							BOOL(old_virt[i] & 1 << 1));
-
-				uchar *new_page = PHYS_TO_VIRT(new[i] ^ 0xfff);
-				memcpy(new_page, old_page, 0x1000);
-			}
-		}
-
-		return new_p | (table & 0xfff); // same lower 12 bits
-	}
+	asm volatile("sti");
 }
 
-uint *clone_page_directory(uint *dir)
+struct process *get_process(uint pid)
 {
-	uint new_p;
-	uint *new = kmalloc_ap(sizeof(uint[1024]), (void **)&new_p);
+	if (pid < 1024)
+		return &processes[pid];
+	else
+		return NULL;
+}
 
-	for (int i = 0; i < 1024; i++)
-	{
-		new[i] = clone_page_table(dir[i]);
-	}
+int get_task_id()
+{
+	return current_task->task.id;
+}
 
-	return new;
+int get_process_id()
+{
+	return current_task->task.proc->id;
+}
+
+void spawn_thread(void (* function)(void *data), void *data)
+{
+
 }
diff --git a/src/kernel/task.h b/src/kernel/task.h
index 0905a5f..e70fd6d 100644
--- a/src/kernel/task.h
+++ b/src/kernel/task.h
@@ -1,11 +1,43 @@
 #pragma once
 
 #include "kint.h"
+#include "registers.h"
 
-// NOTE: uses kmalloc_a for now, this is bad because we will probably
-// run out of memory pretty quick, but malloc() doesn't support alignment
-// yet, so here we are.
-// dir is the VIRTUAL address, return is also virtual
-uint *clone_page_directory(uint *dir);
-// here, both return and argument are PHYSICAL
-uint clone_page_table(uint table);
+struct process
+{
+	bool exists;
+	int id;
+	int ring;
+	int uid;
+	char name[32];
+};
+
+struct task
+{
+	int id;
+	struct process *proc;
+	uint stack_top_p; // stack frame PHYSICAL address
+	uint esp, ebp, eip;
+	uint *page_directory;
+	bool kernel;
+};
+
+struct ll_task_i
+{
+	struct ll_task_i *next;
+	struct task task;
+};
+
+// extern struct process processes[1024];
+// extern struct ll_task_i *first_task, *current_task;
+
+void init_tasks(uint kernel_esp, uint kernel_ebp, uint kernel_eip);
+struct process *get_process(uint pid);
+
+int get_process_id();
+int get_task_id();
+
+// For compatibility I guess
+#define getpid get_process_id
+
+void spawn_thread(void (* function)(void *data), void *data);