From nobody Tue Jan 30 17:16:59 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 4TPX0G17QQz59CjY for ; Tue, 30 Jan 2024 17:17:02 +0000 (UTC) (envelope-from mhorne@freebsd.org) Received: from smtp.freebsd.org (smtp.freebsd.org [96.47.72.83]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "smtp.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4TPX0G0MMwz4J7l; Tue, 30 Jan 2024 17:17:02 +0000 (UTC) (envelope-from mhorne@freebsd.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1706635022; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:autocrypt:autocrypt; bh=u/Yp5MCIx1+zwmCCT4tFruZhejSshH6bj1TBBPfgHiw=; b=GvtjKh2oLdyH40hRHdAntVB28EO7RDpK1K/LYakIypDD3uqFs9IaAOqTgyKd+SHBlp/65K SEbRwzWwPpZOq55soWu9SXl7nOoqtSLxcq56TzH7M5UV5jGSp2a6KXxo0aTF6FVZ0sO4qC H7ApQtiio8KaCcO4reLan226rHqrld3pb9iApwrNJvrc28GZn8lcO/p8eounIdyBW01Drc bsUMMifd7lhC3xkXS4jfc22ZbX0pPLvZETvrhs4APa6eTbsOK2+xteF5RA0ReHDNbTkdbh RROK5RyNkFCBP2L9TDQAfGsV4rdkNDxy0ypcvuBbL/gr0rgj/f/NGryMtT2Fmg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1706635022; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:autocrypt:autocrypt; bh=u/Yp5MCIx1+zwmCCT4tFruZhejSshH6bj1TBBPfgHiw=; b=HOrR30e3YPBL0C4NEWwZbJnb1KzzbG/+bjVMUwVqMhQtsmaHljmWtLimsWbuADp0eeoLrV xC3HSumk8tUmY12PXU9wZtcrnzIccvFzBseNS4MWNRKWL4YWAIqsr7HCvIGcf0MAm3Cyxa fFIhpKiLehBFIuseAzDbk0A1DwS55UOrtFdDD8g+cYCQ+FGqZZLAGuOL0/FHfzLg2SeP07 s2Q/+lZbLP9ybOOVs83JWOXwqOgQE866jhbift8MaMMDae2JwvF9rnOp1te4WSkO2vmCjw UND1/7FY343kKueIg1c4cCHRtu5emzRq6pCdnbiiVxxKPZpHgbc7MGK2UOXBpA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1706635022; a=rsa-sha256; cv=none; b=V9D215yKh3UunGRLDM/0C9Zelg0/1gAmTVVOWLagB/Z2it46UaUUHTDmC+ACNBMsedInGo BANOnZz0YTXohCi7r5t2m0MmdHq78uh1pwNIajqtsqwksP+q0ppxafBhMTJ/eyq6v14WQ3 9mQvh6Fc1V5Tcvc4RZiZJnIqv7enAqNmTJ0AzYxF28IuU7sfZPs8pqBmPWApsNmoR0CA4T k3zDy4XlW5+PPlcBLx02mGIb2/bZajxgFbfbYfg3egV72VqrZEOzAZl0YJmgUNEKHfcsLH 2RAYgpQeRZrJlxgYHDpFD8GqROLE1MJptWHZn3BaeXEHajvaY42Gi4JbDds9iQ== Received: from [192.168.1.151] (host-173-212-76-127.public.eastlink.ca [173.212.76.127]) (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 did not present a certificate) (Authenticated sender: mhorne) by smtp.freebsd.org (Postfix) with ESMTPSA id 4TPX0F5P4mzG5M; Tue, 30 Jan 2024 17:17:01 +0000 (UTC) (envelope-from mhorne@freebsd.org) Message-ID: <9bfbdbe2-ff30-4c2f-abf3-2763aa433107@freebsd.org> Date: Tue, 30 Jan 2024 13:16:59 -0400 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 User-Agent: Mozilla Thunderbird Subject: Re: A little bit wondering about how a syscall works To: Lin Lee , freebsd-riscv@freebsd.org References: <9204b5c5-63bc-4c29-af19-1f1bb85f74e8@Spark> <8f66f3ed-2a49-4ebc-89eb-66c53e6d22bb@Spark> Content-Language: en-CA From: Mitchell Horne Autocrypt: addr=mhorne@freebsd.org; keydata= xsBNBFyS2dQBCADdiXBG8hBVLmYbxu7aSzbwLwUf3HkGFz3rooS1kwyy+SfmjZ4UKNnl9WMx WKrJ7OAZpiNH6bLQ5nsqfx09OnpWL8c/QuPbhNdUywQoqqYpRI0K8GEn//nS9Gs0KTYwVpWb XlrzP+jf3Uh/9L5mcQmStLIH4zaaqMYHW+pMuPrvBmLIHTvLj2QjOkxslrcUdord9uvxe5Ht LU8RuTpQpHOKz705Z9/v7twFdi2HtKzpLwO6SzVyu351di1J+GihsVpcT5josQV5cHbIP3Un x+kmtKBEEc/jl/zBglF7ruWUtwgbryID+2ZPEaO1Mj+RResX4LFVMusq3uUpWRb5WJXxABEB AAHNI01pdGNoZWxsIEhvcm5lIDxtaG9ybmVARnJlZUJTRC5vcmc+wsCUBBMBCgA+AhsDBQsJ CAcCBhUKCQgLAgQWAgMBAh4BAheAFiEEkp/cYPcfabAiQvACi/gnTOdUid8FAmIyDpUFCQtC z0EACgkQi/gnTOdUid8IsQf+N8IptrrCgifT5Z0/WUVFfnH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NJjOKtQ0bYGVgGqYOB/y2mTjVuKeU0eOkN Uw== In-Reply-To: <8f66f3ed-2a49-4ebc-89eb-66c53e6d22bb@Spark> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 1/30/24 10:00, Lin Lee wrote: > I am wondering about the function > ecall_handler(https://github.com/freebsd/freebsd-src/blob/main/sys/riscv/riscv/trap.c#L195 ), which later executing syscallenter, where &td->td_sa >  Is used. I have no idea where the value of td->td_sa is being set. It > is a little bit confusing. > The td_sa member is a struct syscall_args embedded within the larger struct thread. Therefore, it does not need to be set/allocated. The expression "&td->td_sa" obtains a pointer to the syscall_args structure belonging to the struct thread, pointed at by td. > Someone asks me to read > cpu_fetch_syscall_args(https://github.com/freebsd/freebsd-src/blob/main/sys/riscv/riscv/trap.c#L99 ), and it indeed explains how td->td_sa is being set. But however, I still have no idea how it works. > So in this function, we obtain the pointer to the syscall_args structure: struct syscall_args *sa; ... sa = &td->td_sa; From there, we update a few fields of "sa", most notably the "code" which is the actual syscall number we are trying to execute. sa->code = td->td_frame->tf_t[0]; This could be written identically as: td->td_sa.code = td->td_frame->tf_t[0]; Therefore we have updated the syscall information within the thread structure, pointed at by td. sa->code is then used as the index into the system call vector (sysvec), to decide which function we should call. Now, td_frame is the "trapframe", containing the CPU register contents at the time of the exception, generated by the system call instruction "ecall". So, CPU register t0 contains the syscall number, but where is this set? If we look in the libc sources, the part executed by userspace, we find the SYSCALL() macro. https://github.com/freebsd/freebsd-src/blob/7aa4e4eb36171d349c7f3948e16c09ff7540c241/lib/libc/riscv/SYS.h#L39 This short bit of assembly will: 1. Set the t0 register with the appropriate numeric constant (e.g. SYS_fork) 2. Execute the ecall instruction (which will trap into the kernel, and jump to the cpu_exception_handler assembly function in sys/riscv/riscv/exception.S) (How exactly this macro gets used within libc is a little magic, and outside the scope here.) Hope this helps! Mitchell > It is called as a function member in > elf64_freebsd_sysvec(https://github.com/freebsd/freebsd-src/blob/7aa4e4eb36171d349c7f3948e16c09ff7540c241/sys/riscv/riscv/elf_machdep.c#L61C25-L61C45 ), and I think there is nothing to do with syscall here. > > Could somebody please figure it out? > How does a system call works? Where the value of td->td_sa is being set. > > > Thank you very much > > Best Regards, > Lin Lee