I'm trying to implement a handler for system calls in Pintos. Before the interrupt is raised the arguments for the system calls are pushed in the following way:
/* Invokes syscall NUMBER, passing argument ARG0, and returns the
return value as an `int'. */
#define syscall1(NUMBER, ARG0) \
({ \
int retval; \
asm volatile \
("pushl %[arg0]; pushl %[number]; int $0x30; addl $8, %%esp" \
: "=a" (retval) \
: [number] "i" (NUMBER), \
[arg0] "g" (ARG0) \
: "memory"); \
retval; \
})
/* Invokes syscall NUMBER, passing arguments ARG0 and ARG1, and
returns the return value as an `int'. */
#define syscall2(NUMBER, ARG0, ARG1) \
({ \
int retval; \
asm volatile \
("pushl %[arg1]; pushl %[arg0]; " \
"pushl %[number]; int $0x30; addl $12, %%esp" \
: "=a" (retval) \
: [number] "i" (NUMBER), \
[arg0] "g" (ARG0), \
[arg1] "g" (ARG1) \
: "memory"); \
retval; \
})
/* Invokes syscall NUMBER, passing arguments ARG0, ARG1, and
ARG2, and returns the return value as an `int'. */
#define syscall3(NUMBER, ARG0, ARG1, ARG2) \
({ \
int retval; \
asm volatile \
("pushl %[arg2]; pushl %[arg1]; pushl %[arg0]; " \
"pushl %[number]; int $0x30; addl $16, %%esp" \
: "=a" (retval) \
: [number] "i" (NUMBER), \
[arg0] "g" (ARG0), \
[arg1] "g" (ARG1), \
[arg2] "g" (ARG2) \
: "memory"); \
retval; \
})
I have available a struct which contains all the registers that were pushed, but also a pointer to the user-level stack (onto which the system call number and the arguments were pushed).
/* Interrupt stack frame. */
struct intr_frame
{
/* Pushed by intr_entry in intr-stubs.S.
These are the interrupted task's saved registers. */
uint32_t edi; /* Saved EDI. */
uint32_t esi; /* Saved ESI. */
uint32_t ebp; /* Saved EBP. */
uint32_t esp_dummy; /* Not used. */
uint32_t ebx; /* Saved EBX. */
uint32_t edx; /* Saved EDX. */
uint32_t ecx; /* Saved ECX. */
uint32_t eax; /* Saved EAX. */
uint16_t gs, :16; /* Saved GS segment register. */
uint16_t fs, :16; /* Saved FS segment register. */
uint16_t es, :16; /* Saved ES segment register. */
uint16_t ds, :16; /* Saved DS segment register. */
/* Pushed by intrNN_stub in intr-stubs.S. */
uint32_t vec_no; /* Interrupt vector number. */
/* Sometimes pushed by the CPU,
otherwise for consistency pushed as 0 by intrNN_stub.
The CPU puts it just under `eip', but we move it here. */
uint32_t error_code; /* Error code. */
/* Pushed by intrNN_stub in intr-stubs.S.
This frame pointer eases interpretation of backtraces. */
void *frame_pointer; /* Saved EBP (frame pointer). */
/* Pushed by the CPU.
These are the interrupted task's saved registers. */
void (*eip) (void); /* Next instruction to execute. */
uint16_t cs, :16; /* Code segment for eip. */
uint32_t eflags; /* Saved CPU flags. */
void *esp; /* Saved stack pointer. */
uint16_t ss, :16; /* Data segment for esp. */
};
I now want to get these arguments. All the pointers on the stack are 4-byte in size, so I thought that I could simply cast an argument (the dereferenced pointer) to the corresponding type, then increase the stack pointer by 4 and cast the next pointer.
I have the following question:
The pushl instruction pushes values onto the stack correct? So I should be able to get those values simply by de-referencing the pointer to the stack? E.g to get the first argument (assuming this is an int) I would use (int) *(f->esp + 4), where f is a pointer to a struct intr_frame and I add 4 because the system call number is the first element on the stack. Now the problem is that pointer arithmetic on void pointers is not allowed in C and the arguments can be of different type, so can anybody give any suggestions on how to pop these arguments from the stack?
Yes, you can get the parameter values by dereferencing the user esp. Just as with any void *, you must cast it to a suitable pointer type before dereferencing it or indexing it. In this case, uint32_t * would be appropriate, so you would use
*(((uint32_t *)f->esp) + 1)
Note the +1 instead of +4, since the index is scaled by the size of the object pointed to. If you want to use the actual byte offset, you would need two casts
*(uint32_t *)(((uint8_t *)f->esp) + 4)