Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Virtual Memory Mangement + other goodies #3

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ Craig Tiller
MWDD
Patrick Doane
William Warnecke
Gabriel Blanchard
66 changes: 49 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
all: all_targets check
all: all_targets

test: check

UNAME := $(shell uname)

Expand All @@ -9,20 +11,28 @@ TESTS :=
include ./module.mk
include boot/module.mk
include kernel/module.mk
include tools/module.mk

ifeq ($(UNAME),Darwin)
include tools/module.mk
else
include tools_linux/module.mk
endif

# Standard Settings
CC := gcc
COMMON_CFLAGS := -I . -std=c99
COMMON_CFLAGS += -Wall -Werror-implicit-function-declaration -Werror -Wno-array-bounds
Q := @
CC := $(Q)gcc
NASM := $(Q)nasm
COMMON_CFLAGS := -I . -I stdlib/ -std=c99
COMMON_CFLAGS += -Wall -Werror-implicit-function-declaration -Wno-array-bounds
COMMON_CFLAGS += -fno-common -fno-asynchronous-unwind-tables
COMMON_CFLAGS += -mno-red-zone
COMMON_CFLAGS += -O2
COMMON_CFLAGS += -mno-red-zone -fno-stack-protector -ffreestanding -nostdlib
COMMON_CFLAGS += -mcmodel=large -mno-sse -mno-sse2 -mno-sse3 -mno-3dnow
COMMON_CFLAGS += -g -O2

FREESTANDING_CFLAGS := -fno-builtin

# Cross Compiler
CROSS_CC := x86_64-elf-gcc
CROSS_CC := $(Q)x86_64-elf-gcc
CROSS_CFLAGS := -DCROSS $(FREESTANDING_CFLAGS) $(COMMON_CFLAGS)
CROSS_LD := x86_64-elf-ld

Expand All @@ -36,21 +46,31 @@ TEST_CFLAGS := -DTEST $(FREESTANDING_CFLAGS) $(COMMON_CFLAGS)
NATIVE_CFLAGS := -DNATIVE $(COMMON_CFLAGS)

%.bin: %.asm
nasm -f bin -o $@ $<
@echo 'NASM $< -> $@'
$(NASM) -f bin -o $@ $<

%.cross.o: %.asm
nasm -f elf64 -o $@ $<
%.o: %.asm
@echo 'NASM $< -> $@'
$(NASM) -f elf64 -o $@ $<

%.cross.o: %.c
%.o: %.S
@echo 'GAS $< -> $@'
$(CROSS_CC) -c $(CROSS_CFLAGS) -DASM=1 -o $@ $<

%.o: %.c
@echo 'CC $< -> $@'
$(CROSS_CC) -c -MD $(CROSS_CFLAGS) $< -o $@

%.host.o: %.c
%.o: %.c
@echo 'CC $< -> $@'
$(CC) -c -MD $(HOST_CFLAGS) $< -o $@

%.test.o: %.c
@echo 'CC $< -> $@'
$(CC) -c -MD $(TEST_CFLAGS) $< -o $@

%.native.o: %.c
@echo 'CC $< -> $@'
$(CC) -c -MMD $(NATIVE_CFLAGS) $< -o $@

%.exe.run: %.exe
Expand All @@ -71,10 +91,22 @@ usb_kernel_%: boot/loader.bin kernel/kernel.bin
cp boot/loader.bin $*:/
cp kernel/kernel.bin $*:/

ifeq ($(UNAME),Darwin)
install: os.img tools/img_edit.exe
tools/img_edit.exe create os.img boot/boot.bin
tools/img_edit.exe add os.img boot/loader.bin
tools/img_edit.exe add os.img kernel/kernel.bin
else
install: os.img
tools_linux/setup.sh
tools_linux/installBootLoader boot/boot.bin os.img -mbr
endif

clean:
rm -f */*.bin */*.exe */*.exe.run */*.o */*.d
rm -f os.img

-include $(SOURCES:.c=.host.d)
-include $(SOURCES:.c=.cross.d)
-include $(SOURCES:.c=.test.d)
-include $(SOURCES:.c=.native.d)
#-include $(SOURCES:.c=.host.d)
#-include $(SOURCES:.c=.cross.d)
#-include $(SOURCES:.c=.test.d)
#-include $(SOURCES:.c=.native.d)
15 changes: 15 additions & 0 deletions acpi/acpi.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "console/console.h"
#include "intr/ioapic.h"
#include "intr/local_apic.h"
#include "mem/physmem.h"
#include "mem/vm.h"
#include "stdlib/string.h"

// ------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -116,6 +118,10 @@ static void AcpiParseApic(AcpiMadt *madt)
s_madt = madt;

ConsolePrint("Local APIC Address = 0x%08x\n", madt->localApicAddr);

page_map((paddr_t)madt->localApicAddr, (paddr_t)madt->localApicAddr);
page_map((paddr_t)&g_localApicAddr, (paddr_t)&g_localApicAddr);

g_localApicAddr = (u8 *)(uintptr_t)madt->localApicAddr;

u8 *p = (u8 *)(madt + 1);
Expand All @@ -127,6 +133,8 @@ static void AcpiParseApic(AcpiMadt *madt)
u8 type = header->type;
u8 length = header->length;

page_map((paddr_t)p, (paddr_t)p);

if (type == APIC_TYPE_LOCAL_APIC)
{
ApicLocalApic *s = (ApicLocalApic *)p;
Expand Down Expand Up @@ -172,10 +180,12 @@ static void AcpiParseDT(AcpiHeader *header)

if (signature == 0x50434146)
{
page_map((paddr_t)header, (paddr_t)header);
AcpiParseFacp((AcpiFadt *)header);
}
else if (signature == 0x43495041)
{
page_map((paddr_t)header, (paddr_t)header);
AcpiParseApic((AcpiMadt *)header);
}
}
Expand All @@ -189,6 +199,7 @@ static void AcpiParseRsdt(AcpiHeader *rsdt)
while (p < end)
{
u32 address = *p++;
page_map((paddr_t)address, (paddr_t)address);
AcpiParseDT((AcpiHeader *)(uintptr_t)address);
}
}
Expand All @@ -202,6 +213,7 @@ static void AcpiParseXsdt(AcpiHeader *xsdt)
while (p < end)
{
u64 address = *p++;
page_map((paddr_t)address, (paddr_t)address);
AcpiParseDT((AcpiHeader *)(uintptr_t)address);
}
}
Expand Down Expand Up @@ -238,6 +250,7 @@ static bool AcpiParseRsdp(u8 *p)
ConsolePrint("Version 1\n");

u32 rsdtAddr = *(u32 *)(p + 16);
page_map((paddr_t)rsdtAddr, (paddr_t)rsdtAddr);
AcpiParseRsdt((AcpiHeader *)(uintptr_t)rsdtAddr);
}
else if (revision == 2)
Expand All @@ -249,10 +262,12 @@ static bool AcpiParseRsdp(u8 *p)

if (xsdtAddr)
{
page_map((paddr_t)rsdtAddr, (paddr_t)xsdtAddr);
AcpiParseXsdt((AcpiHeader *)(uintptr_t)xsdtAddr);
}
else
{
page_map((paddr_t)rsdtAddr, (paddr_t)rsdtAddr);
AcpiParseRsdt((AcpiHeader *)(uintptr_t)rsdtAddr);
}
}
Expand Down
1 change: 1 addition & 0 deletions boot/defines.asm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
%define memory_map 0x5000
%define local_apic_address 0x6000
%define active_cpu_count 0x6008
%define new_vm_pml4 0x6010
%define boot_sector_base 0x7c00
%define temp_sector 0x7e00
%define loader_base 0x8000
Expand Down
26 changes: 18 additions & 8 deletions boot/loader.asm
Original file line number Diff line number Diff line change
Expand Up @@ -139,24 +139,34 @@ build_memory_map:
build_vm_tables:
; Define one 2MB page at memory address 0.

xor edi, edi
mov di, vm_pml4 ; PML4
mov ax, vm_pdp + 0xf
mov ax, vm_pdp + 0x3 ; Present + Read/Write
stosw
xor ax, ax
mov cx, 0x07ff
rep stosw

mov ax, vm_pd + 0xf ; PDP
mov ax, vm_pd + 0x3 ; Present + Read/Write
stosw
xor ax, ax
mov cx, 0x07ff
rep stosw

mov ax, 0x018f ; PD
stosw
xor ax, ax
mov cx, 0x07ff
rep stosw
; Build 512 Page Directory Entries
; Which should allow us early access to the first 1Gb of mem
mov edx, 0x0
build:
mov eax, 0x0083
mov esi, edx
inc edx
shl esi, 21
add eax, esi
stosd
xor eax, eax
stosd
cmp edx, 512
jne build

ret

Expand Down Expand Up @@ -424,7 +434,7 @@ ap32:
mov eax, 0x000000a0 ; Set PAE and PGE
mov cr4, eax

mov eax, vm_pml4 ; Assign PML4
mov eax, [new_vm_pml4] ; Assign PML4
mov cr3, eax

mov ecx, 0xc0000080 ; Read from EFER MSR
Expand Down
7 changes: 7 additions & 0 deletions console/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "console/console.h"
#include "console/cmd.h"
#include "cpu/smp.h"
#include "gfx/vga.h"
#include "input/keycode.h"
#include "mem/lowmem.h"
Expand Down Expand Up @@ -35,6 +36,8 @@ static uint s_lineIndex;
static HistoryLine s_history[MAX_HISTORY_SIZE];
static uint s_historyCount;

static lock_t console_lock;

// ------------------------------------------------------------------------------------------------
static void ConsoleUpdateInput()
{
Expand Down Expand Up @@ -163,6 +166,7 @@ static void ConsoleExec()
// ------------------------------------------------------------------------------------------------
void ConsoleInit()
{
spin_lock_init(&console_lock);
ConsoleUpdateInput();
}

Expand Down Expand Up @@ -215,10 +219,13 @@ void ConsolePrint(const char *fmt, ...)

char c;
char *s = buf;

spin_lock(&console_lock);
while ((c = *s++))
{
ConsolePutChar(c);
}
spin_unlock(&console_lock);
}

// ------------------------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion cpu/detect.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

#pragma once

#include "stdlib/types.h"
#include <types.h>

void CpuDetect();
Loading