In my kernel, I try to update the ESP, but after the update works, the variables break, and the return statement doesn't work either.
I want to allocate one page per 4096 bytes for the stack. to do this, I call kmalloc which returns the available address of the 4096 byte page address.
Code: Select all
uint32_t* stack_pointer_esp = 0x0;
int a = 123;
dprintf("--------\n");
//read esp
__asm__ volatile (
"mov %%esp, %0" : "=r" (stack_pointer_esp)
);
dprintf("esp = 0x%X, a = %i\n", stack_pointer_esp, a);
//alloc 4096 bytes (physical address)
stack_pointer_esp = (uint32_t*)kmalloc(4096);
if ((uint32_t)stack_pointer_esp % 4096) {
dprintf("stack_pointer_esp address is not aligned!\n");
kfree((void*)stack_pointer_esp);
return false;
}
dprintf("stack_pointer_esp = 0x%X\n", stack_pointer_esp);
//set esp
__asm__ volatile (
"mov %0, %%esp" :: "r" (stack_pointer_esp)
);
//read esp
__asm__ volatile (
"mov %%esp, %0" : "=r" (stack_pointer_esp)
);
dprintf("1. esp = 0x%X, a = %i\n", stack_pointer_esp, a);
dprintf("2. esp = 0x%X, a = %i\n", stack_pointer_esp, a);
__asm__ volatile (
"mov %%esp, %0" : "=r" (stack_pointer_esp)
);
dprintf("3 (after read). esp = 0x%X, a = %i\n", stack_pointer_esp, a);
dprintf("4. esp = 0x%X, a = %i\n", stack_pointer_esp, a);
Code: Select all
--------
esp = 0x1147ba0, a = 123
stack_pointer_esp = 0x1148000
1. esp = 0x1148000, a = 123
2. esp = 0x1c, a = 28
3 (after read). esp = 0x1148000, a = 22
4. esp = 0x28, a = 40
--------
The register itself is not reset, this can be seen on the 3rd and 4th printing.
The variable a constantly changes its value, it is not clear why.
Why is this happening? If I don't change the ESP, then everything works as it should.