Re: A little bit wondering about how a syscall works
Date: Fri, 02 Feb 2024 05:49:16 UTC
Hi, But what I see in https://github.com/freebsd/freebsd-src/blob/main/sys/kern/subr_syscall.c is(after removing some condition branching): 77 error = (p->p_sysent->sv_fetch_syscall_args)(td); 78 se = sa->callp; 156 error = (se->sy_call)(td, sa->args); It seems that `sv_set_syscall_retval hook` is called earlier than syscall is executed. Thank you so much for your replying. Best Regards, Lin Lee On Feb 2, 2024 at 12:19 AM +0800, Mitchell Horne <mhorne@freebsd.org>, wrote: > On 2/1/24 00:47, Lin Lee wrote: > > Hi, > > > > So, if I understand correctly, > > > > Each thread's sv_fetch_syscall_args hook function is initialized as > > cpu_fetch_syscall_args(), > > > > And when it enter syscallenter, it first use `error = > > (p->p_sysent->sv_fetch_syscall_args)(td);` to read the system call > > number, then use `error = (se->sy_call)(td, sa->args)` to execute the > > system call. > > > > Do I understand corrected? > > > > That's right. > > > Thank you very much. > > > > Best Regards, > > Lin Lee > > On Feb 1, 2024 at 12:27 AM +0800, Mitchell Horne <mhorne@freebsd.org>, > > wrote: > > > On 1/31/24 01:03, Lin Lee wrote: > > > > Hello Mitchell, > > > > > > > > Thank you for your kindly responding. > > > > > > > > Now I have still a question, when does the function > > > > cpu_fetch_syscall_args be called? > > > > > > > > As the previous letter mentions, I traced the code and entered the > > > > elf_machdep.c. > > > > > > > > I have no idea if there are something to do between elf_machdep.c and > > > > system calll. > > > > > > > > > > The short answer is yes, it is related. In syscallenter() we have: > > > > > > error = (p->p_sysent->sv_fetch_syscall_args)(td); > > > > > > And as you saw, the sv_fetch_syscall_args hook is set to > > > cpu_fetch_syscall_args() for elf64_freebsd_sysvec. Similarly, there is > > > an sv_set_syscall_retval hook, called by syscallret() when we are done > > > executing the system call. > > > > > One correction: the sv_set_syscall_retval hook is actually called at the > very end of syscallenter(), after the execution of the syscall has > completed. > > > > Each process 'p' has a corresponding sysentvec (p_sysent). On the > > > riscv architecture there is currently only one registered systentvec, > > > elf64_freebsd_sysvec, because we can only execute 64-bit FreeBSD ELF > > > binaries on this platform. > > > > > > By contrast, on amd64 there are several registered sysentvecs. This > > > allows it to execute, for example, 32-bit FreeBSD ELF binaries, or > > > 64-bit Linux ELF binaries. The sysentvec enables different handling > > > for these different types of executables, e.g. the system call table > > > is different for Linux processes (.sv_table = linux_sysent). > > > > > > You will see also that Linux processes have a different function for > > > sv_fetch_syscall_args, take a look in sys/amd64/linux/linux_sysvec.c. > > > > > > Mitchell > > > > > > > If not, when(how) the cpu_fetch_syscall_args is called? > > > > > > > > Thank you very much. > > > > > > > > Best Regards, > > > > Lin Lee > > > > On Jan 31, 2024 at 1:17 AM +0800, Mitchell Horne <mhorne@freebsd.org>, > > > > wrote: > > > > > > > > > > Mitchell > > > >