From nobody Fri Feb 02 05:49:16 2024 X-Original-To: freebsd-riscv@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4TR4bk10T1z59V0K for ; Fri, 2 Feb 2024 05:49:38 +0000 (UTC) (envelope-from leelin2602@gmail.com) Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1D4" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4TR4bj41hbz4S8T; Fri, 2 Feb 2024 05:49:37 +0000 (UTC) (envelope-from leelin2602@gmail.com) Authentication-Results: mx1.freebsd.org; none Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-6dddee3ba13so1234002b3a.1; Thu, 01 Feb 2024 21:49:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706852975; x=1707457775; darn=freebsd.org; h=mime-version:subject:references:in-reply-to:message-id:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=uEWQcerBgr5U3NhCdZaVckhv6TjCYP3Ep82iyWcT4hE=; b=L/6GmEo6SSjIVf7pKrFIQiK+8Xw+rs+7DEXe1Xo05Rj52y+oPNGufktRNR5rUT8rgh lIYiFYocKSzPpmi9w99saMRnp1MzjcugAAii/WxecOoStIJhP9cm8obH/ElAEu5DQUol jMlwUfGm+ao2ZKO7Mg0m8MpMuxaVyxR3aVv+CUMA+cl/5fnizy4dtctuGaBDNgYm98wL 83HT2sVUYC15qS/t8nK1NLAU/R0yGoFNI0Ig5BzjHbEKxFHG0HKBUe9giOdFGNO5+YP8 g0r50PGmy7khaq66jsBRbWzICiW3odPwITCkxKQQhnry35YGPEckB4rTTbpD+Fqxulra kifw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706852975; x=1707457775; h=mime-version:subject:references:in-reply-to:message-id:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=uEWQcerBgr5U3NhCdZaVckhv6TjCYP3Ep82iyWcT4hE=; b=RroPPoZv/kXYQDRYgivdB+sjFF3ql7fu91fQ11D23rfXHpag6LMiJtmdHarVoS95+5 JiQ1+CL2yThxx7wDlUf1iy0KebhHkC/eGChIaGik1z/bxmgfSlHEMhMuhti6Qk2rvoal PCYNAlt9iQoVsvoNeO64JlMx+jqSAdw3j6mr5FP9vIxtCnuseM03d3U5xv3/Zu8UzbSb dHcjKmaqs0kmZqu8bO1lBNXDRRcfWegx/zObtKJ8Eoy1KxO2AQ8ANIeJhNmZ7d9js6oA bedsOt3rbCISoZL1KlwPkFyKq9OSNTgKqrfLeL+UbMV8Ri5OKRwUUuSQ0I1Ug6JDyjRa K3wA== X-Gm-Message-State: AOJu0Yw5smDzpL+YJzYSmIAJQoB2St/3XgJ+1H0oNDvJ1WuJm5SZIOiW JCwFmtqZBvUDv77JH+mGrN2THgztDMoYlTLpj3LepqA3FK/Ny4yxMz34KESXCiY= X-Google-Smtp-Source: AGHT+IHUkqlRN3v9Q46U1ULsAeQ0S4m3VoCI4/SgN15wSLmnwmj/YDvpVHT/RlGmCwBa2Dg4xBiPOA== X-Received: by 2002:a05:6a00:88b:b0:6dd:a072:867 with SMTP id q11-20020a056a00088b00b006dda0720867mr1844038pfj.15.1706852974743; Thu, 01 Feb 2024 21:49:34 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCUQgrXcAbDBYicUW7LikZOE6Anbb3RklP61QtpoERX4wYkohESRaQEAxuxePgqNKRl3VwAkinOIDLUpsmCf9Jj32qM= Received: from [10.1.2.5] (123-193-26-19.dynamic.kbronet.com.tw. [123.193.26.19]) by smtp.gmail.com with ESMTPSA id z7-20020a630a47000000b005cfbdf71baasm793083pgk.47.2024.02.01.21.49.33 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 01 Feb 2024 21:49:34 -0800 (PST) Date: Fri, 2 Feb 2024 13:49:16 +0800 From: Lin Lee To: freebsd-riscv@freebsd.org, Mitchell Horne Message-ID: 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> <8867a483-7e92-4579-9236-32b4a704713e@Spark> <51285fdf-dbcf-4d71-82cb-f49353db95cd@freebsd.org> Subject: Re: A little bit wondering about how a syscall works X-Readdle-Message-ID: bd2bd011-be87-4f87-825b-dd533198bcc9@Spark List-Id: FreeBSD on the RISC-V instruction set architecture List-Archive: https://lists.freebsd.org/archives/freebsd-riscv List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-riscv@freebsd.org X-BeenThere: freebsd-riscv@freebsd.org MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="65bc826a_5236e554_c55" X-Rspamd-Queue-Id: 4TR4bj41hbz4S8T X-Spamd-Bar: ---- X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[]; ASN(0.00)[asn:15169, ipnet:2607:f8b0::/32, country:US] --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 = , 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 , > > 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 , > > > > wrote: > > > > > > > > > > Mitchell > > > > --65bc826a_5236e554_c55 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline
Hi,

But what I see in&=23160;https:= //github.com/freebsd/freebsd-src/blob/main/sys/kern/subr=5Fsyscall.c&= =23160;is(after removing some condition branching):

77 error =3D (p->p=5Fsysent->s= v=5Ffetch=5Fsyscall=5Fargs)(td);
78 se =3D sa->callp;
156&=23160;&=23160;error =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.&=23160;

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 initialized 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 sy= stem call
number, then use =60error =3D (se->sy=5Fcall)(td, sa->args)=60 to e= xecute 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.o= rg>,
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 the
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. Similarly, t= here is
an sv=5Fset=5Fsyscall=5Fretval hook, called by syscallret() when we are d= one
executing the system call.


One correction: the sv=5Fset=5Fsyscall=5Fretval hook is actually called a= t the
very end of syscallenter(), after the execution of the syscall has
completed.

Each process 'p' has a corresponding sysent= vec (p=5Fsysent). On the
riscv architecture there is currently only one registered systentvec,
elf64=5Ffreebsd=5Fsysvec, because we can only execute 64-bit =46reeBSD 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 handling
for these different types of executables, e.g. the system call table
is different for Linux processes (.sv=5Ftable =3D linux=5Fsysent).

You will see also that Linux processes have a different function for
sv=5Ffetch=5Fsyscall=5Fargs, take a look in sys/amd64/linux/linux=5Fsysve= c.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=40freebsd.org= >,
wrote:

Mitchell


--65bc826a_5236e554_c55--