Skip to content

Commit

Permalink
Cleaning up issues with RBP, setting RBP is 0 as cpu initialization a…
Browse files Browse the repository at this point in the history
…nd userspace init and updating all RBPs upon stack creation.
  • Loading branch information
wilkie committed Mar 4, 2011
1 parent 9b7151c commit 58a17e3
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 12 deletions.
28 changes: 24 additions & 4 deletions kernel/arch/x86_64/architecture/cpu.d
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
8 changes: 6 additions & 2 deletions kernel/arch/x86_64/boot/load.s
Expand Up @@ -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:
Expand Down Expand Up @@ -137,6 +138,9 @@ long_entry_ap:
push rax
popf

; clear rbp
xor rbp, rbp

; call kmain
call apEntry

Expand Down
7 changes: 3 additions & 4 deletions kernel/arch/x86_64/core/paging.d
Expand Up @@ -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--;
Expand Down
2 changes: 0 additions & 2 deletions kernel/core/kmain.d
Expand Up @@ -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();
}
Expand Down
3 changes: 3 additions & 0 deletions runtimes/mindrt/entry.d
Expand Up @@ -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];
Expand Down

0 comments on commit 58a17e3

Please sign in to comment.