From nobody Thu Feb 01 04:47:50 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 4TQRH65gRKz592jt for ; Thu, 1 Feb 2024 04:48:02 +0000 (UTC) (envelope-from leelin2602@gmail.com) Received: from mail-oi1-x22c.google.com (mail-oi1-x22c.google.com [IPv6:2607:f8b0:4864:20::22c]) (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 4TQRH65Bdvz4qXM; Thu, 1 Feb 2024 04:48:02 +0000 (UTC) (envelope-from leelin2602@gmail.com) Authentication-Results: mx1.freebsd.org; none Received: by mail-oi1-x22c.google.com with SMTP id 5614622812f47-3be9e11ee59so265430b6e.1; Wed, 31 Jan 2024 20:48:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706762881; x=1707367681; 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=ixzgqkGCd13CzqN+XJuPcpwVKqOBMNnFka4nA9vC7H0=; b=iN0+oaMj135P38MeogD+FjrHl0HbwFyUalNCddKcXXfAKDOoMKsV2ZbMJHkijcT1WJ 3BrWVYvWhRMlKoBXSwOJdFDNcQxHgO7IsPMpJrsrjePk/jnMIifkUUb+77ciSR2mJYW7 3K4FiZtgqkdwbVzPOe490D1vfw52yNhRQgD6ZpuQJKxQc21tvY7lZMXdYA0gV7G/ILQt tDYHI7Qs4c2kiVAQKtPGNJU48a4wBOH2JUx7ti/y4VeTwaDma8lfe/qU9XhKgZ8gRKEx Nadgg8ymxvSD5IFl4DbTk2hYB9a5dr1dZvtvFhxasYBnrzjNarY33Rsrfqmffqb7Jq+g 4vIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706762881; x=1707367681; 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=ixzgqkGCd13CzqN+XJuPcpwVKqOBMNnFka4nA9vC7H0=; b=LhyPe4GzazUhBP42BZgqv6YlDhuuBoiwZBjOYFt/bJCAhFmOHzBKkSVp1PRQ8tOrtq 7J3tUoueJIPm9yqJR5f5dSGqB+3nkjiA+/clwXrhFsAZlaBaRNW6VC7ePctocsCCDRNx 3+Vc/+jJ3Rn/dJNdW1jSna5ZluPsNCyooVkj2FnTBfrINk6Lin5LQtyiRsFhlTXchKzX MZgrGKDfdFNNiHy6nVq/FiMUnE6uuDyyHjBWo45VEySbj/MPlpVAmp3GXHbY+P/E62f4 Suo+vvOPIdL8XDbjE07Eu415EbQ246xSzLkKvOKD/eAUxHLkXydWGJaYb5U3n2HM00lr kPeg== X-Gm-Message-State: AOJu0YwfLZL4uKgJ5Xy4af98SQWtF04QM8GY7xyaxlpEQoEQYjPRktMQ gwt1Jco4lnlIBTlEVzFT5bJhUJXrYMv0R2MDigopFvhoSROM6Crm2XSkmae1 X-Google-Smtp-Source: AGHT+IFXnQGjde4FeKw+/t3Xavm/t4iUNAiMMwYUITYsF51btMeOsS1fUFc4VhkDB6FMZbAItIr8Vw== X-Received: by 2002:a05:6808:1201:b0:3be:685e:b209 with SMTP id a1-20020a056808120100b003be685eb209mr1151550oil.35.1706762880534; Wed, 31 Jan 2024 20:48:00 -0800 (PST) Received: from [172.20.10.2] (114-137-55-104.emome-ip.hinet.net. [114.137.55.104]) by smtp.gmail.com with ESMTPSA id jw3-20020a056a00928300b006dbd2231184sm10727735pfb.70.2024.01.31.20.47.58 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 31 Jan 2024 20:48:00 -0800 (PST) Date: Thu, 1 Feb 2024 12:47:50 +0800 From: Lin Lee To: freebsd-riscv@freebsd.org, Mitchell Horne Message-ID: <8867a483-7e92-4579-9236-32b4a704713e@Spark> In-Reply-To: References: <9204b5c5-63bc-4c29-af19-1f1bb85f74e8@Spark> <8f66f3ed-2a49-4ebc-89eb-66c53e6d22bb@Spark> <9bfbdbe2-ff30-4c2f-abf3-2763aa433107@freebsd.org> Subject: Re: A little bit wondering about how a syscall works X-Readdle-Message-ID: 8867a483-7e92-4579-9236-32b4a704713e@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="65bb227b_41b5c1d5_c55" X-Rspamd-Queue-Id: 4TQRH65Bdvz4qXM 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] --65bb227b_41b5c1d5_c55 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Content-Disposition: inline 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? Thank you very much. Best Regards, Lin Lee On Feb 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_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. > > 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 , > > wrote: > > > > > > Mitchell > --65bb227b_41b5c1d5_c55 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline
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=5Fsyse= nt->sv=5Ffetch=5Fsyscall=5Fargs)(td);=60 to read the system call numbe= r, then use =60error =3D (se->sy=5Fcall)(td, sa->args)=60 to execut= e the system call.

Do I understand corrected=3F

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 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=5Ffetc= h=5Fsyscall=5Fargs() for elf64=5Ffreebsd=5Fsysvec. Similarly, there is an= sv=5Fset=5Fsyscall=5Fretval hook, called by syscallret() when we are don= e executing the system call.

Each process 'p' has a corresponding sysentvec (p=5Fsysent). On the riscv= architecture there is currently only one registered systentvec, elf64=5F= freebsd=5Fsysvec, because we can only execute 64-bit =46reeBSD EL=46 bina= ries on this platform.

By contrast, on amd64 there are several registered sysentvecs. This allow= s 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 f= or Linux processes (.sv=5Ftable =3D linux=5Fsysent).

You will see also that Linux processes have a different function for sv=5F= fetch=5Fsyscall=5Fargs, take a look in sys/amd64/linux/linux=5Fsysvec.c.<= br />
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

--65bb227b_41b5c1d5_c55--