Linux – How does a system call know where to put its parameters in a wrapper function?

How does a system call know where to put its parameters in a wrapper function?… here is a solution to the problem.

How does a system call know where to put its parameters in a wrapper function?

I’m trying to implement a system call in Linux (RedHat Enterprise 8), but I’m a bit confused about how it works. As far as I know, I implemented a wrapper in user mode that puts the system call number in eax, puts the parameters in ebx, ecx, edx, etc., and then calls the int 0x80 that calls the appropriate system call. My question is, since system calls are written in a similar way to regular C functions, how does it know which registers contain which parameters? Is it a convention, or is there a mechanism, and if so, where and how does it do it?

EDIT: This is a homework. I know there are system call macros that can do these things for me.

Solution

From Linux Journal article, bottom of page 2

Since the system call interface is exclusively register-parametered, six parameters at most can be used with a single system call. %eax is the syscall number; %ebx, %ecx, %edx, %esi, %edi and %ebp are the six generic registers used as param0-5; and %esp cannot be used because it’s overwritten by the kernel when it enters ring 0 (i.e., kernel mode).

Your C code may look like it’s making a system call, but it’s actually calling a function in libc. The function ensures that all parameters are in the correct registers and then interrupts.

Related Problems and Solutions