aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMarvin Borner2020-06-01 00:48:12 +0200
committerMarvin Borner2020-06-01 00:48:12 +0200
commit448aa86d5aea864ac103808981a433e4f0816276 (patch)
tree428fb678eccc71ce7d22c78be1697682f56abfd4 /src
parent5d276b98a2bbd4b69402559ced60f43af268f77b (diff)
Aaaah so many bugs
Diffstat (limited to 'src')
-rw-r--r--src/kernel/fs/elf.c17
-rw-r--r--src/kernel/lib/memory.c23
-rw-r--r--src/kernel/memory/paging.c63
-rw-r--r--src/kernel/memory/paging.h4
-rw-r--r--src/kernel/net/rtl8139.c4
5 files changed, 62 insertions, 49 deletions
diff --git a/src/kernel/fs/elf.c b/src/kernel/fs/elf.c
index 46a4876..9d4dd60 100644
--- a/src/kernel/fs/elf.c
+++ b/src/kernel/fs/elf.c
@@ -26,11 +26,6 @@ int is_elf(struct elf_header *header)
struct process *elf_load(char *path)
{
log("ELF START");
- u32 **prev_dir;
- if (current_proc)
- prev_dir = current_proc->cr3;
- else
- prev_dir = page_directory;
u8 *file = read_file(path);
if (!file) {
@@ -52,13 +47,13 @@ struct process *elf_load(char *path)
strcpy(proc->name, path);
proc->registers.eip = header->entry;
- paging_switch_directory(proc->cr3);
+ //paging_switch_directory(proc->cr3);
u32 stk = (u32)valloc(PAGE_SIZE);
proc->registers.useresp = 0x40000000 - (PAGE_SIZE / 2);
proc->registers.ebp = proc->registers.useresp;
proc->registers.esp = proc->registers.useresp;
- paging_map(stk, 0x40000000 - PAGE_SIZE, PT_USER);
+ paging_map_user(stk, 0x40000000 - PAGE_SIZE);
for (int i = 0; i < header->phnum; i++, program_header++) {
switch (program_header->type) {
@@ -66,13 +61,10 @@ struct process *elf_load(char *path)
break;
case 1: {
u32 loc = (u32)valloc(PAGE_SIZE);
- warn("1");
- paging_map(loc, program_header->vaddr, PT_USER);
- warn("2");
+ paging_map_user(loc, program_header->vaddr);
memcpy((void *)program_header->vaddr,
((void *)((u32)file) + program_header->offset),
program_header->filesz);
- warn("3");
assert(program_header->filesz <= PAGE_SIZE);
break;
}
@@ -81,7 +73,6 @@ struct process *elf_load(char *path)
}
}
- paging_switch_directory(prev_dir);
- log("ELF END");
+ /* paging_switch_directory(page_tables); */
return proc;
} \ No newline at end of file
diff --git a/src/kernel/lib/memory.c b/src/kernel/lib/memory.c
index 04697a3..101bf17 100644
--- a/src/kernel/lib/memory.c
+++ b/src/kernel/lib/memory.c
@@ -85,20 +85,25 @@ void memory_mmap_init(struct multiboot_tag_mmap *tag)
for (mmap = ((struct multiboot_tag_mmap *)tag)->entries; (u8 *)mmap < (u8 *)tag + tag->size;
mmap = (multiboot_memory_map_t *)((u32)mmap +
((struct multiboot_tag_mmap *)tag)->entry_size)) {
+ debug("Found memory of type %d from 0x%x-0x%x: %dKiB", mmap->type, (u32)mmap->addr,
+ (u32)mmap->addr + (u32)mmap->len, mmap->len >> 10);
+ sum += mmap->len;
+
+ // Translate to pages
if (mmap->type == MULTIBOOT_MEMORY_AVAILABLE) {
- debug("Found free memory");
- paging_set_present(mmap->addr, mmap->len >> 12);
- sum += mmap->len;
+ paging_set_present(mmap->addr, mmap->len >> 13);
} else if (mmap->type == MULTIBOOT_MEMORY_RESERVED) {
- debug("Found reserved memory");
- paging_set_present(mmap->addr, mmap->len >> 12);
- paging_set_used(mmap->addr, mmap->len >> 12);
+ paging_set_present(mmap->addr, mmap->len >> 13);
+ paging_set_used(mmap->addr, mmap->len >> 13);
} else if (mmap->type == MULTIBOOT_MEMORY_ACPI_RECLAIMABLE) {
- debug("Found ACPI reclaimable memory");
+ paging_set_present(mmap->addr, mmap->len >> 13);
+ paging_set_used(mmap->addr, mmap->len >> 13);
} else if (mmap->type == MULTIBOOT_MEMORY_NVS) {
- debug("Found NVS memory");
+ paging_set_present(mmap->addr, mmap->len >> 13);
+ paging_set_used(mmap->addr, mmap->len >> 13);
} else if (mmap->type == MULTIBOOT_MEMORY_BADRAM) {
- warn("Found bad memory!");
+ paging_set_present(mmap->addr, mmap->len >> 13);
+ paging_set_used(mmap->addr, mmap->len >> 13);
}
}
total = sum >> 10; // I want kb
diff --git a/src/kernel/memory/paging.c b/src/kernel/memory/paging.c
index 5ed3fdd..806b61a 100644
--- a/src/kernel/memory/paging.c
+++ b/src/kernel/memory/paging.c
@@ -4,34 +4,36 @@
#include <stdint.h>
#include <system.h>
-u32 page_directory[1024] __attribute__((aligned(4096)));
+u32 *page_directory[1024] __attribute__((aligned(4096)));
u32 page_tables[1024][1024] __attribute__((aligned(4096)));
-void paging_init()
+void paging_init(u32 **tables)
{
- for (u32 i = 0; i < PAGE_COUNT; i++) {
- for (u32 j = 0; j < PAGE_COUNT; j++) {
+ for (u32 i = 0; i < PAGE_COUNT; i++)
+ tables[i] = page_tables[i];
+
+ for (u32 i = 0; i < PAGE_COUNT; i++)
+ page_directory[i] = (u32)tables[i] | PD_RW | PD_PRESENT | PD_USER;
+}
+
+void paging_kernel_init()
+{
+ for (u32 i = 0; i < PAGE_COUNT; i++)
+ for (u32 j = 0; j < PAGE_COUNT; j++)
page_tables[i][j] = ((j * PAGE_ALIGN) + (i * PAGE_SIZE)) | PT_RW | PT_USER;
- }
- }
- for (u32 i = 0; i < PAGE_COUNT; i++) {
+ for (u32 i = 0; i < PAGE_COUNT; i++)
page_directory[i] = (u32)page_tables[i] | PD_RW | PD_PRESENT | PD_USER;
- }
}
void paging_install()
{
- paging_init();
+ paging_kernel_init();
if (!memory_init())
paging_set_present(0, memory_get_all() >> 3);
paging_set_used(0, ((u32)&kernel_end >> 12) + 1);
- u32 cr4;
- asm volatile("mov %%cr4, %0" : "=r"(cr4));
- cr4 &= 0xffffffef;
- asm volatile("mov %0, %%cr4" ::"r"(cr4));
paging_enable();
log("Installed paging");
@@ -50,14 +52,15 @@ void paging_install()
u32 **paging_make_directory()
{
- u32 **dir = valloc(PAGE_COUNT * sizeof(*dir));
- dir[0] = valloc(PAGE_COUNT * PAGE_COUNT * sizeof(u32));
+ u32 **tables = valloc(PAGE_COUNT * sizeof(*tables));
+ tables[0] = valloc(PAGE_COUNT * PAGE_COUNT * sizeof(u32));
for (int i = 1; i < PAGE_COUNT; i++)
- dir[i] = dir[0] + i * PAGE_COUNT;
+ tables[i] = tables[0] + i * PAGE_COUNT;
- paging_init(dir);
+ paging_init(tables);
+ paging_enable();
- return dir;
+ return tables;
}
void paging_remove_directory(u32 **dir)
@@ -86,9 +89,11 @@ void paging_enable()
paging_enabled = 1;
}
-void paging_switch_directory(u32 **dir)
+void paging_switch_directory(u32 **tables)
{
- asm("mov %0, %%cr3" ::"r"(dir));
+ for (u32 i = 0; i < PAGE_COUNT; i++)
+ page_directory[i] = (u32)tables[i] | PD_RW | PD_PRESENT | PD_USER;
+ asm("mov %0, %%cr3" ::"r"(page_directory));
}
void invlpg(u32 addr)
@@ -104,6 +109,17 @@ void paging_map(u32 phy, u32 virt, u16 flags)
invlpg(virt);
}
+void paging_map_user(u32 phy, u32 virt)
+{
+ u32 pdi = virt >> 22;
+ /* u32 pti = virt >> 12 & 0x03FF; */
+ for (int i = 0; i < 1024; i++) {
+ page_tables[pdi][i] = phy | PT_RW | PT_PRESENT | PT_USER;
+ phy += 4096;
+ }
+ invlpg(virt);
+}
+
u32 paging_get_phys(u32 virt)
{
u32 pdi = virt >> 22;
@@ -120,6 +136,7 @@ u16 paging_get_flags(u32 virt)
void paging_set_flag_up(u32 virt, u32 count, u32 flag)
{
+ //debug("Setting flag %b for %d tables", flag, count);
u32 page_n = virt / PAGE_ALIGN;
for (u32 i = page_n; i < page_n + count; i++) {
page_tables[i / PAGE_COUNT][i % PAGE_COUNT] |= flag;
@@ -188,11 +205,9 @@ u32 paging_alloc_pages(u32 count)
u32 paging_get_used_pages()
{
u32 n = 0;
- for (u32 i = 0; i < PAGE_COUNT; i++) {
- for (u32 j = 0; j < PAGE_COUNT; j++) {
+ for (u32 i = 0; i < PAGE_COUNT; i++)
+ for (u32 j = 0; j < PAGE_COUNT; j++)
if (page_tables[i][j] & PT_USED)
n++;
- }
- }
return n;
} \ No newline at end of file
diff --git a/src/kernel/memory/paging.h b/src/kernel/memory/paging.h
index b7ea07a..1408d7c 100644
--- a/src/kernel/memory/paging.h
+++ b/src/kernel/memory/paging.h
@@ -25,7 +25,8 @@
#define PT_GLOBAL 1 << 8
#define PT_USED 1 << 9
-u32 page_directory[1024] __attribute__((aligned(4096)));
+u32 *page_directory[1024] __attribute__((aligned(4096)));
+u32 page_tables[1024][1024] __attribute__((aligned(4096)));
int paging_enabled;
void paging_install();
@@ -37,6 +38,7 @@ void paging_remove_directory(u32 **dir);
void paging_switch_directory(u32 **dir);
void paging_map(u32 phy, u32 virt, u16 flags);
+void paging_map_user(u32 phy, u32 virt);
u32 paging_get_phys(u32 virt);
u16 paging_get_flags(u32 virt);
u32 paging_get_used_pages();
diff --git a/src/kernel/net/rtl8139.c b/src/kernel/net/rtl8139.c
index bbee94f..f7e213b 100644
--- a/src/kernel/net/rtl8139.c
+++ b/src/kernel/net/rtl8139.c
@@ -61,8 +61,8 @@ int rtl8139_init(void)
// Get mac address
for (int i = 0; i < 6; ++i)
mac[i] = inb((u16)(rtl_iobase + 0x00 + i));
- debug("Mac address: %x:%x:%x:%x:%x:%x", mac[0], mac[1], mac[2], mac[3],
- mac[4], mac[5]);
+ debug("Mac address: %x:%x:%x:%x:%x:%x", mac[0], mac[1], mac[2], mac[3], mac[4],
+ mac[5]);
// Activate (turn on)
outb((u16)(rtl_iobase + 0x52), 0x0);