diff --git a/kernel/arch/x86_64/architecture/cpu.d b/kernel/arch/x86_64/architecture/cpu.d index a7b8dd2..ca7f9aa 100644 --- a/kernel/arch/x86_64/architecture/cpu.d +++ b/kernel/arch/x86_64/architecture/cpu.d @@ -29,6 +29,9 @@ import kernel.system.definitions; import architecture.syscall; import architecture.vm; +// For stack tracing +import user.environment; + private { extern(C) { extern ubyte _stack; @@ -649,18 +652,35 @@ private: _stacks[identifier] = cast(void*)stackSpace + 4096; TSS.table.RSP0 = cast(void*)stackSpace + 4096; + StackFrame* curr = null; + asm { // Retrieve stack pointer, place in RAX mov RAX, RSP; // Get the page offset - and RAX, 0xFFF; + and RAX, Paging.PAGESIZE - 1; // Add this to the stackspace pointer add RAX, stackSpace; // Set stack pointer mov RSP, RAX; + + // Do the same for frame pointer + mov RAX, RBP; + and RAX, Paging.PAGESIZE - 1; + add RAX, stackSpace; + mov RBP, RAX; + + mov curr, RBP; + } + + while(isValidAddress(cast(ubyte*)curr.next) && cast(ulong)curr.next > Paging.PAGESIZE) { + kprintfln!("return addr: {x} rbp: {x}")(curr.returnAddr, curr); + curr.next = cast(StackFrame*)(cast(ulong)curr.next & (Paging.PAGESIZE - 1)); + curr.next = cast(StackFrame*)(cast(ulong)curr.next + stackSpace); + curr = curr.next; } return ErrorVal.Success; @@ -672,10 +692,10 @@ private: return ErrorVal.Fail; } -// uint pmu_info = cpuidAX(0xA); -// pmu_info &= 0xFF; + // uint pmu_info = cpuidAX(0xA); + // pmu_info &= 0xFF; -// kprintfln!("code: {x}\n")(pmu_info); + // kprintfln!("code: {x}\n")(pmu_info); return ErrorVal.Success; } diff --git a/kernel/arch/x86_64/boot/load.s b/kernel/arch/x86_64/boot/load.s index 8d350d2..3720650 100644 --- a/kernel/arch/x86_64/boot/load.s +++ b/kernel/arch/x86_64/boot/load.s @@ -74,11 +74,12 @@ long_entry: push rsi push rdi + ; clear rbp + xor rbp, rbp + ; call kmain call kmain - - ; we should not get here haltloop: @@ -137,6 +138,9 @@ long_entry_ap: push rax popf + ; clear rbp + xor rbp, rbp + ; call kmain call apEntry diff --git a/kernel/arch/x86_64/core/paging.d b/kernel/arch/x86_64/core/paging.d index c29be5d..4d1f229 100644 --- a/kernel/arch/x86_64/core/paging.d +++ b/kernel/arch/x86_64/core/paging.d @@ -40,15 +40,14 @@ void printStackTrace(StackFrame* start){ kprintfln!(" YOU LOOK SAD, SO I GOT YOU A STACK TRACE!")(); StackFrame* curr = start, limit = start; - ulong PAGESIZE = 4096; - limit += PAGESIZE; - limit = cast(StackFrame*) ( cast(ulong)limit & ~(PAGESIZE-1)); + limit += Paging.PAGESIZE; + limit = cast(StackFrame*) ( cast(ulong)limit & ~(Paging.PAGESIZE-1)); int count = 10; //&& curr < limit - while(cast(ulong)curr > PAGESIZE && count > 0 && isValidAddress(cast(ubyte*)curr)){ + while(cast(ulong)curr > Paging.PAGESIZE && count > 0 && isValidAddress(cast(ubyte*)curr)){ kprintfln!("return addr: {x} rbp: {x}")(curr.returnAddr, curr); curr = curr.next; count--; diff --git a/kernel/core/kmain.d b/kernel/core/kmain.d index 8436900..552efb7 100644 --- a/kernel/core/kmain.d +++ b/kernel/core/kmain.d @@ -131,12 +131,10 @@ extern(C) void kmain(int bootLoaderID, void *data) { auto fail = InitProcess.install(); Log.result(fail); - Date dt; Timing.currentDate(dt); kprintfln!("\nDate: {} {} {}")(dt.day, dt.month, dt.year); - if(fail != ErrorVal.Fail){ InitProcess.enterFromBSP(); } diff --git a/runtimes/mindrt/entry.d b/runtimes/mindrt/entry.d index 11050ea..0918a03 100644 --- a/runtimes/mindrt/entry.d +++ b/runtimes/mindrt/entry.d @@ -52,6 +52,9 @@ void start(){ asm { naked; + // zero rbp + xor RBP, RBP; + // load the addresses of the beginning and end of the BSS mov RDX, startBSS; //mov RDX, [RDX];