Skip to content

Commit

Permalink
Pmmngr bug (taking a copy instead of reference), performance enhancem…
Browse files Browse the repository at this point in the history
…ents, multithreading!
  • Loading branch information
solocle committed Apr 22, 2019
1 parent 85edb25 commit 9dbc552
Show file tree
Hide file tree
Showing 29 changed files with 817 additions and 74 deletions.
2 changes: 2 additions & 0 deletions Chaikrnl/Chaikrnl.vcxproj
Expand Up @@ -248,6 +248,7 @@
<ClCompile Include="pciexpress.cpp" />
<ClCompile Include="pmmngr.cpp" />
<ClCompile Include="scheduler.cpp" />
<ClCompile Include="semaphore.cpp" />
<ClCompile Include="spinlock.cpp" />
<ClCompile Include="uefihelper.cpp" />
</ItemGroup>
Expand All @@ -267,6 +268,7 @@
<ClInclude Include="pmmngr.h" />
<ClInclude Include="redblack.h" />
<ClInclude Include="scheduler.h" />
<ClInclude Include="semaphore.h" />
<ClInclude Include="uefihelper.h" />
</ItemGroup>
<ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions Chaikrnl/Chaikrnl.vcxproj.filters
Expand Up @@ -63,6 +63,9 @@
<ClCompile Include="scheduler.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="semaphore.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\Include\kernelinfo.h">
Expand Down Expand Up @@ -113,6 +116,9 @@
<ClInclude Include="scheduler.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="semaphore.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<NASM Include="arch\x64\asm_x64.asm">
Expand Down
59 changes: 46 additions & 13 deletions Chaikrnl/acpihelp.cpp
Expand Up @@ -9,6 +9,8 @@
#include <chaikrnl.h>
#include <stdheaders.h>
#include <pciexpress.h>
#include <scheduler.h>
#include <semaphore.h>

static void* RSDP = 0;
static acpi_system_timer sys_timer = nullptr;
Expand All @@ -24,11 +26,6 @@ void set_acpi_timer(acpi_system_timer timer)
}

void start_acpi_tables()
{
AcpiInitializeTables(0, 0, FALSE);
}

void startup_acpi()
{
ACPI_STATUS Status;
Status = AcpiInitializeSubsystem();
Expand All @@ -37,12 +34,25 @@ void startup_acpi()
kprintf(u"Could not initialize ACPI: %d\n", Status);
return;
}
AcpiInitializeTables(0, 0, FALSE);
}

void startup_acpi()
{
ACPI_STATUS Status;
Status = AcpiLoadTables();
if (ACPI_FAILURE(Status))
{
kprintf(u"Could not load tables: %d\n", Status);
return;
}
AcpiInstallInterface("ChaiOS");
AcpiInstallInterface("Windows 2015");
AcpiInstallInterface("Windows 2016");
AcpiInstallInterface("Windows 2017");
AcpiInstallInterface("Windows 2017.2");
AcpiInstallInterface("Windows 2018");
AcpiInstallInterface("Windows 2018.2");
Status = AcpiEnableSubsystem(ACPI_FULL_INITIALIZATION);
if (ACPI_FAILURE(Status))
{
Expand Down Expand Up @@ -133,18 +143,26 @@ CHAIKRNL_FUNC BOOLEAN AcpiOsWritable(void *Memory, ACPI_SIZE Length)
}
CHAIKRNL_FUNC ACPI_THREAD_ID AcpiOsGetThreadId()
{
return 1;
HTHREAD current = pcpu_data.runningthread;
return (ACPI_THREAD_ID)current;
}
CHAIKRNL_FUNC ACPI_STATUS AcpiOsExecute(ACPI_EXECUTE_TYPE Type, ACPI_OSD_EXEC_CALLBACK Function, void *Context)
{
Function(Context);
if (!isscheduler())
Function(Context);
else
{
HTHREAD thread = create_thread(Function, Context);
if (!thread)
return AE_NO_MEMORY;
}
return AE_OK;
}

static void Stall(uint32_t microseconds)
{
uint64_t current = AcpiOsGetTimer();
while ((AcpiOsGetTimer() - current)*10 < microseconds);
while ((AcpiOsGetTimer() - current)/10 < microseconds);
}

CHAIKRNL_FUNC void AcpiOsSleep(UINT64 Milliseconds)
Expand All @@ -160,35 +178,50 @@ CHAIKRNL_FUNC void AcpiOsStall(UINT32 Microseconds)

CHAIKRNL_FUNC ACPI_STATUS AcpiOsCreateMutex(ACPI_MUTEX *OutHandle)
{
return AE_OK;
return AcpiOsCreateSemaphore(1, 1, OutHandle);
}
CHAIKRNL_FUNC void AcpiOsDeleteMutex(ACPI_MUTEX Handle)
{

delete_semaphore(Handle);
}
CHAIKRNL_FUNC ACPI_STATUS AcpiOsAcquireMutex(ACPI_MUTEX Handle, UINT16 Timeout)
{
return AE_OK;
return AcpiOsWaitSemaphore(Handle, 1, Timeout);
}
CHAIKRNL_FUNC void AcpiOsReleaseMutex(ACPI_MUTEX Handle)
{

AcpiOsSignalSemaphore(Handle, 1);
}

CHAIKRNL_FUNC ACPI_STATUS AcpiOsCreateSemaphore(UINT32 MaxUnits, UINT32 InitialUnits, ACPI_SEMAPHORE *OutHandle)
{
*OutHandle = create_semaphore(InitialUnits);
if (!*OutHandle)
return AE_NO_MEMORY;
return AE_OK;
}
CHAIKRNL_FUNC ACPI_STATUS AcpiOsDeleteSemaphore(ACPI_SEMAPHORE Handle)
{
delete_semaphore(Handle);
return AE_OK;
}
CHAIKRNL_FUNC ACPI_STATUS AcpiOsWaitSemaphore(ACPI_SEMAPHORE Handle, UINT32 Units, UINT16 Timeout)
{
return AE_OK;
if (!Handle)
return AE_BAD_PARAMETER;
size_t tout = Timeout;
if (Timeout == UINT16_MAX)
tout = TIMEOUT_INFINITY;
if (wait_semaphore(Handle, Units, tout) == 1)
return AE_OK;
else
return AE_TIME;
}
CHAIKRNL_FUNC ACPI_STATUS AcpiOsSignalSemaphore(ACPI_SEMAPHORE Handle, UINT32 Units)
{
if (!Handle)
return AE_BAD_PARAMETER;
signal_semaphore(Handle, Units);
return AE_OK;
}
CHAIKRNL_FUNC ACPI_STATUS AcpiOsCreateLock(ACPI_SPINLOCK *OutHandle)
Expand Down
13 changes: 13 additions & 0 deletions Chaikrnl/arch/paging_x64.cpp
Expand Up @@ -2,6 +2,7 @@
#include <arch/cpu.h>
#include <kstdio.h>
#include <string.h>
#include <spinlock.h>

static void* pml4ptr = 0;
static size_t recursive_slot = 0;
Expand All @@ -19,6 +20,8 @@ typedef size_t PTAB_ENTRY;
#define PAGING_SIZEBIT 0x80
#define PAGING_NXE 0x8000000000000000

spinlock_t paging_lock;

static void* make_canonical(size_t addr)
{
if (addr & ((size_t)1 << 47))
Expand Down Expand Up @@ -240,6 +243,7 @@ bool check_free(void* vaddr, size_t length)

bool paging_map(void* vaddr, paddr_t paddr, size_t length, size_t attributes)
{
auto st = acquire_spinlock(paging_lock);
if (!check_free(vaddr, length))
return false;
size_t vptr = (size_t)vaddr;
Expand All @@ -249,19 +253,26 @@ bool paging_map(void* vaddr, paddr_t paddr, size_t length, size_t attributes)
if (paddr != PADDR_ALLOCATE)
{
if ((paddr & (PAGESIZE - 1)) != pgoffset)
{
release_spinlock(paging_lock, st);
return false;
}
paddr ^= pgoffset;
}

for (size_t i = 0; i < (length + PAGESIZE - 1) / PAGESIZE; ++i)
{
if (!paging_map(vaddr, paddr, attributes))
{
release_spinlock(paging_lock, st);
return false;
}

vaddr = raw_offset<void*>(vaddr, PAGESIZE);
if (paddr != PADDR_ALLOCATE)
paddr = raw_offset<paddr_t>(paddr, PAGESIZE);
}
release_spinlock(paging_lock, st);
return true;
}

Expand Down Expand Up @@ -352,6 +363,8 @@ void paging_initialize(void*& info)
//Set up PAT
uint64_t patvalue = 0x0007040600070406;
x64_wrmsr(MSR_IA32_PAT, patvalue);

paging_lock = create_spinlock();
}

void paging_boot_free()
Expand Down
3 changes: 2 additions & 1 deletion Chaikrnl/arch/x64/apic.cpp
Expand Up @@ -80,7 +80,6 @@ static void write_apic_register(uint16_t reg, uint64_t value)
}
static void apic_eoi()
{
arch_disable_interrupts();
write_apic_register(LAPIC_REGISTER_EOI, 0);
}

Expand Down Expand Up @@ -124,6 +123,7 @@ static uint8_t apic_timer_interrupt(size_t vector, void* param)
static uint8_t pit_interrupt(size_t vector, void* param)
{
++pit_ticks;
scheduler_timer_tick();
return 1;
}

Expand Down Expand Up @@ -459,5 +459,6 @@ void x64_init_apic()
arch_register_interrupt_subsystem(INTERRUPT_SUBSYSTEM_IRQ, &apic_subsystem);
//Enable PIT
initialize_pit(1000);
scheduler_init(&apic_eoi);
}
}
25 changes: 20 additions & 5 deletions Chaikrnl/arch/x64/asm_x64.asm
Expand Up @@ -366,6 +366,7 @@ struc CONTEXT
.r13: resq 1
.r14: resq 1
.r15: resq 1
.rflags: resq 1
.floats: resy 10+1
.end:
endstruc
Expand All @@ -382,6 +383,10 @@ mov [rcx + CONTEXT.r12], r12
mov [rcx + CONTEXT.r13], r13
mov [rcx + CONTEXT.r14], r14
mov [rcx + CONTEXT.r15], r15
;RFLAGS
pushfq
pop rax
mov [rcx + CONTEXT.rflags], rax
mov rax, [qword x64_avx_level]
mov rdx, CONTEXT.floats
add rdx, rcx
Expand Down Expand Up @@ -469,21 +474,31 @@ vmovaps ymm14, [rdx+0x160]
vmovaps ymm15, [rdx+0x180]
.finish:
; Now returning
mov r9, 1
cmp r8, 0
cmove r8, r9
mov r9, [rcx + CONTEXT.rflags]
mov rsp, [rcx + CONTEXT.rsp]
mov rdx, [rcx + CONTEXT.rip]
cmp r8, 0
je .ret1
mov rax, r8
jmp rdx
.ret1:
mov rax, 1
push r9
popfq
jmp rdx

global x64_new_context
x64_new_context:
;RCX is new context, RDX is new stack, R8 is entry point
mov [rcx + CONTEXT.rsp], rdx
mov [rcx + CONTEXT.rip], r8
pushfq
pop rax
or rax, 0x200 ;Interrupts enabled
mov [rcx + CONTEXT.rflags], rax
ret

global x64_hlt
x64_hlt:
hlt
ret

section .data
Expand Down
6 changes: 6 additions & 0 deletions Chaikrnl/arch/x64/cpu_x64.cpp
Expand Up @@ -64,6 +64,7 @@ extern "C" size_t x64_context_size;
extern "C" int x64_save_context(context_t context);
extern "C" void x64_load_context(context_t context, int value);
extern "C" void x64_new_context(context_t context, void* stackptr, void* entry);
extern "C" void x64_hlt();

enum sregs {
SREG_CS,
Expand Down Expand Up @@ -952,6 +953,11 @@ void jump_context(context_t ctxt, int value)
x64_load_context(ctxt, value);
}

void arch_halt()
{
x64_hlt();
}

static const size_t PAGES_STACK = 16;

#include <liballoc.h>
Expand Down
1 change: 1 addition & 0 deletions Chaikrnl/arch/x64/interrupts_x64.asm
Expand Up @@ -113,6 +113,7 @@ SAVE_VOLATILE_REGISTERS
;Per CPU information
call swap_gs_ifpriv
call save_fpu_interrupt

;Now we pass the stack interrupt stack and vector
mov rcx, %2
mov rdx, rbp
Expand Down

0 comments on commit 9dbc552

Please sign in to comment.