Making a syscall from a C function causes problem
Posted: Fri Apr 03, 2020 1:10 pm
To give a more specific but still brief description of the problem: When I make any syscall from my C function - which itself is called from a userspace assembly procedure - the syscall returns to the C function, but then the C function doesn't return to the assembly procedure which called it.
Here's the code segment which calls the C function:
Here is the C function which is called:
And finally, here's a very minimal example for my syscall ISR which causes the issue:
As I step through the program to debug it in bochs, I monitor the stack. Here's what I notice:
At the beginning of the C function, the correct return address is at the top of the stack (happens to be 0x100755). So here, the stack looks like this:
When it returns to the C function it has popped the top two things from the stack, so the top now looks like this:
So, any idea what's going on here? I'm not so familiar with using C functions with assembly, and have quite likely made a beginner's mistake here.
Thanks a lot
Here's the code segment which calls the C function:
Code: Select all
welcome db "Welcome!",10,"This is a test", 0
extern entry_in_c
global userspace_entry
userspace_entry:
mov eax, 0
mov ebx, welcome
int 0x80
xchg bx, bx
call entry_in_c
jmp $
Code: Select all
void entry_in_c(void) {
asm("xchg %bx, %bx;\
mov $5, %eax;\
int $0x80;");
}
Code: Select all
isr_syscall:
cli
sti
iret
At the beginning of the C function, the correct return address is at the top of the stack (happens to be 0x100755). So here, the stack looks like this:
- (mem addr: value)
0x104ffb: 0x100755
- 0x104ff4: 0x0
0x104ffb: 0x100755
- 0x104fec: 0x100764
0x104ff0: 0x1b
0x104ff4: 0x202
0x104ff8: 0x104ff4 <- this is equal to ebp... why?
...
When it returns to the C function it has popped the top two things from the stack, so the top now looks like this:
- 0x104ff4: 0x202
0x104ff8: 0x104ff4
So, any idea what's going on here? I'm not so familiar with using C functions with assembly, and have quite likely made a beginner's mistake here.
Thanks a lot