Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 2 Feb 2024 13:49:16 +0800
From:      Lin Lee <leelin2602@gmail.com>
To:        freebsd-riscv@freebsd.org, Mitchell Horne <mhorne@freebsd.org>
Subject:   Re: A little bit wondering about how a syscall works
Message-ID:  <bd2bd011-be87-4f87-825b-dd533198bcc9@Spark>
In-Reply-To: <51285fdf-dbcf-4d71-82cb-f49353db95cd@freebsd.org>
References:  <9204b5c5-63bc-4c29-af19-1f1bb85f74e8@Spark> <8f66f3ed-2a49-4ebc-89eb-66c53e6d22bb@Spark> <9bfbdbe2-ff30-4c2f-abf3-2763aa433107@freebsd.org> <f2213278-0807-4a36-ab90-6d2a9555fc8c@Spark> <b4e54e75-fcfd-475c-b0f7-6be7a6cf5e84@freebsd.org> <8867a483-7e92-4579-9236-32b4a704713e@Spark> <51285fdf-dbcf-4d71-82cb-f49353db95cd@freebsd.org>

next in thread | previous in thread | raw e-mail | index | archive | help
--65bc826a_5236e554_c55
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Hi,

But what I see in=C2=A0https://github.com/freebsd/freebsd-src/blob/main/s=
ys/kern/subr=5Fsyscall.c=C2=A0is(after removing some condition branching)=
:

77 	error =3D (p->p=5Fsysent->sv=5Ffetch=5Fsyscall=5Fargs)(td);
78	se =3D sa->callp;
156=C2=A0=C2=A0error =3D (se->sy=5Fcall)(td, sa->args);

It seems that =60sv=5Fset=5Fsyscall=5Fretval hook=60 is called earlier th=
an syscall is executed.

Thank you so much for your replying.

Best Regards,
Lin Lee
On =46eb 2, 2024 at 12:19 AM +0800, Mitchell Horne <mhorne=40freebsd.org>=
, wrote:
> On 2/1/24 00:47, Lin Lee wrote:
> > Hi,
> >
> > So, if I understand correctly,
> >
> > Each thread's sv=5Ffetch=5Fsyscall=5Fargs hook function is initialize=
d as
> > cpu=5Ffetch=5Fsyscall=5Fargs(),
> >
> > And when it enter syscallenter, it first use =60error =3D
> > (p->p=5Fsysent->sv=5Ffetch=5Fsyscall=5Fargs)(td);=60 to read the syst=
em call
> > number, then use =60error =3D (se->sy=5Fcall)(td, sa->args)=60 to exe=
cute the
> > system call.
> >
> > Do I understand corrected=3F
> >
>
> That's right.
>
> > Thank you very much.
> >
> > Best Regards,
> > Lin Lee
> > On =46eb 1, 2024 at 12:27 AM +0800, Mitchell Horne <mhorne=40freebsd.=
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=5Ffetch=5Fsyscall=5Fargs be called=3F
> > > >
> > > > As the previous letter mentions, I traced the code and entered th=
e
> > > > elf=5Fmachdep.c.
> > > >
> > > > I have no idea if there are something to do between elf=5Fmachdep=
.c and
> > > > system calll.
> > > >
> > >
> > > The short answer is yes, it is related. In syscallenter() we have:
> > >
> > > error =3D (p->p=5Fsysent->sv=5Ffetch=5Fsyscall=5Fargs)(td);
> > >
> > > And as you saw, the sv=5Ffetch=5Fsyscall=5Fargs hook is set to
> > > cpu=5Ffetch=5Fsyscall=5Fargs() for elf64=5Ffreebsd=5Fsysvec. Simila=
rly, there is
> > > an sv=5Fset=5Fsyscall=5Fretval hook, called by syscallret() when we=
 are done
> > > executing the system call.
> > >
>
> One correction: the sv=5Fset=5Fsyscall=5Fretval 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=5Fsysent). On the=

> > > riscv architecture there is currently only one registered systentve=
c,
> > > elf64=5Ffreebsd=5Fsysvec, because we can only execute 64-bit =46ree=
BSD EL=46
> > > binaries on this platform.
> > >
> > > By contrast, on amd64 there are several registered sysentvecs. This=

> > > allows it to execute, for example, 32-bit =46reeBSD EL=46 binaries,=
 or
> > > 64-bit Linux EL=46 binaries. The sysentvec enables different handli=
ng
> > > for these different types of executables, e.g. the system call tabl=
e
> > > is different for Linux processes (.sv=5Ftable =3D linux=5Fsysent).
> > >
> > > You will see also that Linux processes have a different function fo=
r
> > > sv=5Ffetch=5Fsyscall=5Fargs, take a look in sys/amd64/linux/linux=5F=
sysvec.c.
> > >
> > > Mitchell
> > >
> > > > If not, when(how) the cpu=5Ffetch=5Fsyscall=5Fargs is called=3F
> > > >
> > > > Thank you very much.
> > > >
> > > > Best Regards,
> > > > Lin Lee
> > > > On Jan 31, 2024 at 1:17 AM +0800, Mitchell Horne <mhorne=40freebs=
d.org>,
> > > > wrote:
> > > > >
> > > > > Mitchell
> > >
>

--65bc826a_5236e554_c55
Content-Type: text/html; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

<html xmlns=3D=22http://www.w3.org/1999/xhtml=22>;
<head>
<title></title>
</head>
<body>
<div name=3D=22messageBodySection=22>
<div dir=3D=22auto=22>Hi,<br />
<br />
But what I see in&=23160;<a href=3D=22https://github.com/freebsd/freebsd-=
src/blob/main/sys/kern/subr=5Fsyscall.c=22 target=3D=22=5Fblank=22>https:=
//github.com/freebsd/freebsd-src/blob/main/sys/kern/subr=5Fsyscall.c</a>&=
=23160;is(after removing some condition branching):<br />
<br />
<span style=3D=22white-space:pre=22>77 	error =3D (p-&gt;p=5Fsysent-&gt;s=
v=5Ffetch=5Fsyscall=5Fargs)(td);</span><br />
<span style=3D=22white-space:pre=22>78	se =3D sa-&gt;callp;</span><br />
156&=23160;&=23160;error =3D (se-&gt;sy=5Fcall)(td, sa-&gt;args);<br />
<br />
It seems that =60sv=5Fset=5Fsyscall=5Fretval hook=60 is called earlier th=
an syscall is executed.<br />
<br />
Thank you so much for your replying.&=23160;</div>
</div>
<div name=3D=22messageSignatureSection=22><br />
<div class=3D=22match=46ont=22>Best Regards,
<div dir=3D=22auto=22>Lin Lee</div>
</div>
</div>
<div name=3D=22messageReplySection=22>On =46eb 2, 2024 at 12:19 AM +0800,=
 Mitchell Horne &lt;mhorne=40freebsd.org&gt;, wrote:<br />
<blockquote type=3D=22cite=22 style=3D=22border-left-color: grey; border-=
left-width: thin; border-left-style: solid; margin: 5px 5px;padding-left:=
 10px;=22>On 2/1/24 00:47, Lin Lee wrote:<br />
<blockquote type=3D=22cite=22>Hi,<br />
<br />
So, if I understand correctly,<br />
<br />
Each thread's sv=5Ffetch=5Fsyscall=5Fargs hook function is initialized as=
<br />
cpu=5Ffetch=5Fsyscall=5Fargs(),<br />
<br />
And when it enter syscallenter, it first use =60error =3D<br />
(p-&gt;p=5Fsysent-&gt;sv=5Ffetch=5Fsyscall=5Fargs)(td);=60 to read the sy=
stem call<br />
number, then use =60error =3D (se-&gt;sy=5Fcall)(td, sa-&gt;args)=60 to e=
xecute the<br />
system call.<br />
<br />
Do I understand corrected=3F<br />
<br /></blockquote>
<br />
That's right.<br />
<br />
<blockquote type=3D=22cite=22>Thank you very much.<br />
<br />
Best Regards,<br />
Lin Lee<br />
On =46eb 1, 2024 at 12:27 AM +0800, Mitchell Horne &lt;mhorne=40freebsd.o=
rg&gt;,<br />
wrote:<br />
<blockquote type=3D=22cite=22>On 1/31/24 01:03, Lin Lee wrote:<br />
<blockquote type=3D=22cite=22>Hello Mitchell,<br />
<br />
Thank you for your kindly responding.<br />
<br />
Now I have still a question, when does the function<br />
cpu=5Ffetch=5Fsyscall=5Fargs be called=3F<br />
<br />
As the previous letter mentions, I traced the code and entered the<br />
elf=5Fmachdep.c.<br />
<br />
I have no idea if there are something to do between elf=5Fmachdep.c and<b=
r />
system calll.<br />
<br /></blockquote>
<br />
The short answer is yes, it is related. In syscallenter() we have:<br />
<br />
error =3D (p-&gt;p=5Fsysent-&gt;sv=5Ffetch=5Fsyscall=5Fargs)(td);<br />
<br />
And as you saw, the sv=5Ffetch=5Fsyscall=5Fargs hook is set to<br />
cpu=5Ffetch=5Fsyscall=5Fargs() for elf64=5Ffreebsd=5Fsysvec. Similarly, t=
here is<br />
an sv=5Fset=5Fsyscall=5Fretval hook, called by syscallret() when we are d=
one<br />
executing the system call.<br />
<br /></blockquote>
</blockquote>
<br />
One correction: the sv=5Fset=5Fsyscall=5Fretval hook is actually called a=
t the<br />
very end of syscallenter(), after the execution of the syscall has<br />
completed.<br />
<br />
<blockquote type=3D=22cite=22>
<blockquote type=3D=22cite=22>Each process 'p' has a corresponding sysent=
vec (p=5Fsysent). On the<br />
riscv architecture there is currently only one registered systentvec,<br =
/>
elf64=5Ffreebsd=5Fsysvec, because we can only execute 64-bit =46reeBSD EL=
=46<br />
binaries on this platform.<br />
<br />
By contrast, on amd64 there are several registered sysentvecs. This<br />=

allows it to execute, for example, 32-bit =46reeBSD EL=46 binaries, or<br=
 />
64-bit Linux EL=46 binaries. The sysentvec enables different handling<br =
/>
for these different types of executables, e.g. the system call table<br /=
>
is different for Linux processes (.sv=5Ftable =3D linux=5Fsysent).<br />
<br />
You will see also that Linux processes have a different function for<br /=
>
sv=5Ffetch=5Fsyscall=5Fargs, take a look in sys/amd64/linux/linux=5Fsysve=
c.c.<br />
<br />
Mitchell<br />
<br />
<blockquote type=3D=22cite=22>If not, when(how) the cpu=5Ffetch=5Fsyscall=
=5Fargs is called=3F<br />
<br />
Thank you very much.<br />
<br />
Best Regards,<br />
Lin Lee<br />
On Jan 31, 2024 at 1:17 AM +0800, Mitchell Horne &lt;mhorne=40freebsd.org=
&gt;,<br />
wrote:<br />
<blockquote type=3D=22cite=22><br />
Mitchell<br /></blockquote>
</blockquote>
<br /></blockquote>
</blockquote>
<br /></blockquote>
</div>
</body>
</html>

--65bc826a_5236e554_c55--




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?bd2bd011-be87-4f87-825b-dd533198bcc9>