Paging now works, heap still borked.

alentours-dev
q3k 2013-02-03 14:38:48 +01:00
parent c93a20ad03
commit 87907c0f75
11 changed files with 62 additions and 44 deletions

View File

@ -35,7 +35,11 @@ CXFLAGS+= -fno-stack-protector
LFLAGS:=-nostdlib -nostartfiles -nodefaultlibs
.PHONY: all clean kernel.bin emulate hdd.img
debug: CFLAGS += -DDEBUG -gdwarf-4 -fvar-tracking-assignments -O0
debug: CXFLAGS += -DDEBUG -gdwarf-4 -fvar-tracking-assignments -O0
debug: kernel.bin
.PHONY: all clean emulate hdd.img debug
obj/src/%.nao : src/%.asm
@echo "[i] Assembling $*.asm..."
@ -101,6 +105,10 @@ Lua: $(LUA)
kernel.bin: version-gen Tier0 Tier1 Lua Alentours
@echo "[i] Linking kernel.bin..."
@$(LD) -T src/kernel.ld -o kernel.bin $(TIER0) $(TIER1) $(ALENTOURS) -Lsrc/Lua -llua
@echo "[i] Creating debug symbols."
@cp kernel.bin ksyms.elf
@strip --only-keep-debug ksyms.elf
@strip --strip-debug --strip-unneeded kernel.bin
hdd.img: kernel.bin
@echo "[i] Creating HDD image..."
@ -113,10 +121,10 @@ hdd.img: kernel.bin
@mcopy -i hdd_temp.img dst/syslinux.cfg ::syslinux.cfg
@mv hdd_temp.img hdd.img
emulate-nohdd-debug: kernel.bin
emulate-nohdd-debug: debug
@echo "[i] Starting GDB..."
@echo -e "file kernel.bin\ntarget remote localhost:1234\n" > gdbcommands
@terminal -x /bin/bash -c "sleep 1 && gdb -x gdbcommands && rm gdbcommands" &
@echo -e "file kernel.bin\nsymbol-file ksyms.elf\ntarget remote localhost:1234\n" > gdbcommands
@terminal -x /bin/bash -c "sleep 1 && gdb -tui -x gdbcommands && rm gdbcommands" &
@echo "[i] Starting QEmu..."
@qemu-system-x86_64 --no-kvm -S -gdb tcp::1234 -d int -smp 4 -kernel ../Loader/loader.bin -initrd kernel.bin
@ -135,4 +143,4 @@ clean:
@make -C src/Lua clean
@if [ -e kernel.bin ] ; then rm kernel.bin ; fi
@if [ -e hdd_temp.img ] ; then rm hdd_temp.img ; fi
@if [ -e hdd.img ] ; then rm hdd.img; fi
@if [ -e hdd.img ] ; then rm hdd.img; fi

View File

@ -23,6 +23,7 @@ typedef struct {
} T_HEAP_FOOTER;
typedef struct {
// wat
void **Array;
u64 Size;
u64 MaxSize;

View File

@ -85,10 +85,7 @@ void paging_kernel_initialize(u64 KernelVirtualStart, u64 KernelPhysicalStart, u
// The temporary page is a page you can use to access some temporary physical
// location. There is only one page, 4096 bytes large. Deal with it.
void paging_temp_page_setup(void);
const inline u64 paging_temp_page_get_virtual(void)
{
return 0xFFFFFFFF80000000 + 511 * 0x1000;
}
volatile const u64 paging_temp_page_get_virtual(void);
void paging_temp_page_set_physical(u64 Physical);
// We have to prepare for virtual memory allocation from 0xFFFFFFFF00000000
@ -102,6 +99,7 @@ void paging_temp_page_set_physical(u64 Physical);
void paging_scratch_initialize(void);
// Allocates 4096 of physical and virtual memory in the kernel scratch buffer.
// Warning, this memory cannot be freed.
void *paging_scratch_map(u64 Physical);
void *paging_scratch_allocate(void);
// A simple page map call. This does no checks! Triple faults ahoy.

View File

@ -9,7 +9,7 @@
#define PANIC_EX_HEX(m, R, h) kpanic_ex(m, 0, h, R)
#define ASSERT(m) kassert(m, __FILE__, __LINE__)
void kpanic_ex(const s8 *Error, const s8 *File, u32 Line, T_ISR_REGISTERS R);
void kpanic_ex(const s8 *Error, const s8 *File, u64 Line, T_ISR_REGISTERS R);
void kpanic(const s8 *Error, const s8 *File, u32 Line);
void kassert(u8 Value, const s8 *File, u32 Line);

View File

@ -5,13 +5,13 @@
#include "Tier0/interrupts.h"
#include "Tier0/panic.h"
#define APIC_SET32(field, value) do { *((u32 *)(g_APIC.LAPIC + field)) = (value);} while(0)
#define APIC_GET32(field) (*((u32*)(g_APIC.LAPIC + field)))
#define APIC_SET32(field, value) do { *((volatile u32 *)(g_APIC.LAPIC + field)) = (value);} while(0)
#define APIC_GET32(field) (*((volatile u32*)(g_APIC.LAPIC + field)))
#define APIC_SET32A(field, value) do { APIC_SET32(field, value); ASSERT(APIC_GET32(field) == (value));} while(0)
#define APIC_CALIBRATION_SAMPLES 8
struct {
void *LAPIC;
volatile void *LAPIC;
volatile u32 CalibrationCounter;
volatile u32 CalibrationSamples[APIC_CALIBRATION_SAMPLES];
u32 BusSpeed;
@ -64,19 +64,15 @@ void apic_enable_lapic(void)
system_msr_set(0x1B, APICMSR);
}
//u64 Virtual = paging_minivmm_allocate();
u64 Virtual = 0;
kprintf("[i] LAPIC will be @0x%x.\n", Virtual);
paging_map_page(Virtual, 0xFEE00000);
g_APIC.LAPIC = paging_scratch_map(0xFEE00000);
kprintf("[i] LAPIC will be @0x%x.\n", g_APIC.LAPIC);
// prepare interrupts ..
interrupts_setup_isr(39, (void *)apic_spurious_interrupt, E_INTERRUPTS_RING0);
interrupts_setup_isr(200, (void *)apic_timer_interrupt, E_INTERRUPTS_RING0);
g_APIC.LAPIC = (void *)Virtual;
// reset APIC to a kinda known state
APIC_SET32A(APIC_DFR, 0xFFFFFFFF);
APIC_SET32(APIC_DFR, 0xFFFFFFFF);
APIC_SET32(APIC_LDR, (APIC_GET32(APIC_LDR)&0x00FFFFFF)|1);
APIC_SET32A(APIC_LVTTimer, 0x10000); // mask bit
APIC_SET32A(APIC_LVTPerformanceCounter, 4 << 8); // NMI bit

View File

@ -73,12 +73,12 @@ T_HEAP *heap_create(u64 Size)
u64 Start = 0;
for (u32 i = 0; i < NumPages; i++)
{
// if (!Start)
// Start = paging_minivmm_allocate();
// else
// paging_minivmm_allocate();
if (!Start)
kprintf("%x\n", Start = (u64)paging_scratch_allocate());
else
kprintf("%x\n", paging_scratch_allocate());
}
kprintf("[i] Heap starts at 0x%x\n", Start);
//kprintf("[i] Heap starts at 0x%x\n", Start);
T_HEAP* Heap = (T_HEAP *)Start;
Start += sizeof(T_HEAP);
@ -86,7 +86,7 @@ T_HEAP *heap_create(u64 Size)
Heap->Index = heap_index_initialize((void*)Start, HEAP_INDEX_SIZE / 4);
Start += HEAP_INDEX_SIZE;
kprintf("[[i] Heap %x - %x\n", Start, Start + NumPages * 4096);
Heap->Start = Start;
Heap->End = Start + NumPages * 4096;
@ -96,7 +96,7 @@ T_HEAP *heap_create(u64 Size)
Hole->Hole = 1;
heap_index_insert(&Heap->Index, (void*)Hole);
//for (;;) {}
return Heap;
}
@ -190,7 +190,7 @@ void *heap_alloc(T_HEAP *Heap, u64 Size, u8 Aligned)
{
u64 RealSize = Size + sizeof(T_HEAP_HEADER) + sizeof(T_HEAP_FOOTER);
s64 Iterator = _heap_find_smallest_hole(Heap, RealSize, Aligned);
kprintf("%i\n", Iterator);
if (Iterator == -1)
{
u64 OldSize = Heap->End - Heap->Start;
@ -245,6 +245,7 @@ void *heap_alloc(T_HEAP *Heap, u64 Size, u8 Aligned)
Iterator);
u64 HoleStart = (u64)Header;
u64 HoleSize = Header->Size;
kprintf("%x %x, %x, %i\n", Heap, &Heap->Index, HoleStart, HoleSize);
if (HoleSize - RealSize < sizeof(T_HEAP_HEADER) + sizeof(T_HEAP_FOOTER))
{
@ -252,7 +253,7 @@ void *heap_alloc(T_HEAP *Heap, u64 Size, u8 Aligned)
RealSize = HoleSize;
}
if (Aligned && HoleStart & 0xFFFFF000)
if (Aligned && HoleStart & 0xFFFFFFFFFFFFF000)
{
u64 NewLocation = HoleStart + 0x1000 - (HoleStart & 0xFFF)
- sizeof(T_HEAP_HEADER);
@ -291,7 +292,7 @@ void *heap_alloc(T_HEAP *Heap, u64 Size, u8 Aligned)
T_HEAP_FOOTER *NewHoleFooter = (T_HEAP_FOOTER*)((u64)NewHoleHeader
+ NewHoleHeader->Size - sizeof(T_HEAP_FOOTER));
//for (;;) {}
if ((u64)NewHoleFooter < Heap->End)
{
NewHoleFooter->Magic = HEAP_FOOTER_MAGIC;

View File

@ -67,13 +67,8 @@ void kmain(u32 LoadContextAddress)
paging_temp_page_setup();
physmem_init();
paging_scratch_initialize();
void *a1 = paging_scratch_allocate();
void *a2 = paging_scratch_allocate();
kprintf("%x %x\n", a1, a2);
for (;;) {}
// Let's create a new kernel stack
//u64 StackVirtual = paging_minivmm_allocate();
u64 StackVirtual = 0;
u64 StackVirtual = (u64)paging_scratch_allocate();
kprintf("[i] New kernel stack 0x%x\n", StackVirtual);
// And now let's use it and forget ebp because we can.
@ -92,11 +87,13 @@ void kmain_newstack(void)
if (RSDPAddress == 0)
PANIC("ACPI not supported! What is this, 1999?");
smp_initialize();
//smp_initialize();
interrupts_init_simple();
exceptions_init_simple();
apic_enable_lapic();
heap_init_simple();
kprintf("%x\n", kmalloc(1337));
for (;;) {}
// enable FPU/SSE...
__asm__ volatile(
"movq %cr0, %rax;"

View File

@ -71,6 +71,11 @@ void paging_temp_page_set_physical(u64 Physical)
__asm__ volatile("invlpg %0" :: "m"(*(u64 *)g_KernelPaging.TempPageVirtual));
}
inline volatile const u64 paging_temp_page_get_virtual(void)
{
return 0xFFFFFFFF80000000 + 511 * 0x1000;
}
/*u8 paging_get_physical_ex(u64 Virtual, u64 *Physical, T_PAGING_ML4 *ML4)
{
if (Virtual < g_KernelPaging.KernelVirtualStart || Virtual > g_KernelPaging.KernelVirtualStart + g_KernelPaging.KernelSize)
@ -118,6 +123,9 @@ void paging_scratch_initialize(void)
ASSERT(ML4->Entries[ML4Entry].Present);
u64 aDPT = ML4->Entries[ML4Entry].Physical << 12;
kprintf("[i] DPT Physical 0x%x, ML4 index %i.\n", aDPT, ML4Entry);
kprintf("[i] Scratch DIR Physical 0x%x\n", DirPhysical);
T_PAGING_DPT *DPT = (T_PAGING_DPT *)aDPT;
u16 DPTEntry = PAGING_GET_DPT_INDEX(0xFFFFFFFF00000000);
@ -131,13 +139,13 @@ void paging_scratch_initialize(void)
g_PagingScratch.DirectoryPhysical = DirPhysical;
}
void *paging_scratch_allocate(void)
void *paging_scratch_map(u64 Physical)
{
if (g_PagingScratch.HeapSetUp)
PANIC("Trying to allocate unmanaged scratch after heap exists, abort!");
u64 Virtual = 0xFFFFFFFF00000000 + g_PagingScratch.UnmanagedSize;
u64 Physical = physmem_allocate_page() * 4096;
u16 DirEntry = PAGING_GET_DIR_INDEX(Virtual);
paging_temp_page_set_physical(g_PagingScratch.DirectoryPhysical);
@ -177,6 +185,12 @@ void *paging_scratch_allocate(void)
return (void *)Virtual;
}
void *paging_scratch_allocate(void)
{
u64 Physical = physmem_allocate_page() * 4096;
return paging_scratch_map(Physical);
}
void paging_map_page(u64 Virtual, u64 Physical)
{
T_PAGING_ML4 *ML4 = paging_get_ml4();

View File

@ -30,11 +30,11 @@ char *kpanic_get_random_message(void)
return KPANIC_HEADER0;
}
void kpanic_ex(const s8 *Error, const s8 *File, u32 Line, T_ISR_REGISTERS R)
void kpanic_ex(const s8 *Error, const s8 *File, u64 Line, T_ISR_REGISTERS R)
{
__asm__ volatile("cli");
kclear();
//kclear();
char *Message = kpanic_get_random_message();
@ -69,6 +69,8 @@ void kpanic_ex(const s8 *Error, const s8 *File, u32 Line, T_ISR_REGISTERS R)
kprintf("%x\n", Line);
}
for(;;) {}
// Dumping registers

View File

@ -72,6 +72,7 @@ void physmem_init(void)
Metadata->Bitmap[i] = 0;
// mask all the bits up to and including our metadata frame as used
kprintf("[i] Marking physical memory up to 0x%x (bit %i) as used.\n", MetadataFrame, PHYSMEM_ADDRESS_TO_BIT_NUMBER(MetadataFrame));
for (u32 i = 0; i <= PHYSMEM_ADDRESS_TO_BIT_NUMBER(MetadataFrame); i++)
{
u32 Bit = PHYSMEM_BIT_NUMBER_TO_BIT_IN_METADATA(i);
@ -89,6 +90,8 @@ void physmem_init(void)
g_PhysicalMemory.MemoryFree -= 4096;
}
}
g_PhysicalMemory.FirstMetadata = MetadataFrame;
}
@ -98,7 +101,7 @@ u64 physmem_allocate_page(void)
T_PHYSMEM_METADATA *Metadata = (T_PHYSMEM_METADATA *)paging_temp_page_get_virtual();
for (u32 i = 0; i < PHYSMEM_METADATA_COVERS_BITS; i++)
{
if (Metadata->Bitmap[i] != 0xFFFFFFFFFFFF)
if (Metadata->Bitmap[i] != 0xFFFFFFFFFFFFFFFF)
{
// scan the subbitmap
for (u8 j = 0; j < 64; j++)

View File

@ -44,8 +44,6 @@ SECTIONS
/DISCARD/ :
{
*(.comment)
*(.debug*)
*(.eh_frame)
}
}