Commit a5d3a7c2 authored by Kevin Wolf's avatar Kevin Wolf

Paging anschalten und die ersten paar Takte überleben

parent a509e2c6
......@@ -9,4 +9,10 @@ void pmm_init(struct multiboot_info* mb_info);
void* pmm_alloc(void);
void pmm_free(void* page);
#define PTE_PRESENT 0x1
#define PTE_WRITE 0x2
#define PTE_USER 0x4
void vmm_init(void);
#endif
......@@ -9,6 +9,7 @@ void init(struct multiboot_info *mb_info)
kprintf("Hello World!\n");
pmm_init(mb_info);
vmm_init();
init_gdt();
init_intr();
......
#include <stdint.h>
#include "mm.h"
struct vmm_context {
uint32_t* pagedir;
};
/*
* Dieser Speicherkontext wird nur waehrend der Initialisierung verwendet.
* Spaeter laeuft der Kernel immer im Kontext des aktuellen Prozesses.
*/
static struct vmm_context* kernel_context;
struct vmm_context* vmm_create_context(void)
{
struct vmm_context* context = pmm_alloc();
int i;
/* Page Directory anlegen und mit Nullen initialisieren */
context->pagedir = pmm_alloc();
for (i = 0; i < 1024; i++) {
context->pagedir[i] = 0;
}
return context;
}
int vmm_map_page(struct vmm_context* context, uintptr_t virt, uintptr_t phys)
{
uint32_t page_index = virt / 0x1000;
uint32_t pd_index = page_index / 1024;
uint32_t pt_index = page_index % 1024;
uint32_t* page_table;
int i;
/* Wir brauchen 4k-Alignment */
if ((virt & 0xFFF) || (phys & 0xFFF)) {
return -1;
}
/* Page Table heraussuchen bzw. anlegen */
if (context->pagedir[pd_index] & PTE_PRESENT) {
/* Page Table ist schon vorhanden */
page_table = (uint32_t*) (context->pagedir[pd_index] & ~0xFFF);
} else {
/* Neue Page Table muss angelegt werden */
page_table = pmm_alloc();
for (i = 0; i < 1024; i++) {
page_table[i] = 0;
}
context->pagedir[pd_index] =
(uint32_t) page_table | PTE_PRESENT | PTE_WRITE;
}
/* Neues Mapping in the Page Table eintragen */
page_table[pt_index] = phys | PTE_PRESENT | PTE_WRITE;
asm volatile("invlpg %0" : : "m" (*(char*)virt));
return 0;
}
void vmm_activate_context(struct vmm_context* context)
{
asm volatile("mov %0, %%cr3" : : "r" (context->pagedir));
}
void vmm_init(void)
{
uint32_t cr0;
int i;
/* Speicherkontext anlegen */
kernel_context = vmm_create_context();
/* Die ersten 4 MB an dieselbe physische wie virtuelle Adresse mappen */
for (i = 0; i < 4096 * 1024; i += 0x1000) {
vmm_map_page(kernel_context, i, i);
}
vmm_activate_context(kernel_context);
asm volatile("mov %%cr0, %0" : "=r" (cr0));
cr0 |= (1 << 31);
asm volatile("mov %0, %%cr0" : : "r" (cr0));
}
......@@ -150,7 +150,11 @@ void init_multitasking(struct multiboot_info* mb_info)
* erste davon nach 2 MB und erstellen dann einen neuen Task dafuer.
*/
struct multiboot_module* modules = mb_info->mbs_mods_addr;
init_elf((void*) modules[0].mod_start);
int i;
for (i = 0; i < mb_info->mbs_mods_count; i++) {
init_elf((void*) modules[i].mod_start);
}
}
}
......
#include <stdint.h>
uint16_t* videomem = (uint16_t*) 0xb8000;
int i = 0;
void _start(void)
{
int i;
for (i = 0; i < 5; i++) {
int j = i + 5;
for (; i < j; i++) {
asm("int $0x30" : : "a" (0), "b" ('0' + i));
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment