From nobody Thu Feb 23 16:50:59 2023 X-Original-To: dev-commits-src-all@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 4PMzYb2jpmz3sDMj; Thu, 23 Feb 2023 16:50:59 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (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 "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4PMzYb2Hdsz3j5n; Thu, 23 Feb 2023 16:50:59 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1677171059; 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; bh=ZF2hbTTiLd7Gn3S1lw+DHdwYUZZtff8YaI8O8PU3XRU=; b=YsjJDdI2kK9muCAC8KgmxTUXfZ5qsAzcU8NG+vrZS12ISXxd2niOp+pHhrA1foS7GEB43t ifTyRparPQ41tFy9LF/mWYfXKyi4zUUyJZtotLfJzqYtmS7HE0bD5nPww2f22T9WA9LvuF yNFozU7eS3SkQsPfxLevrHyJs6fH6LrhsPyRNkQIrAFIGYKnsOHx19jsr65eUpC6kMyLGp SU1UXYgJ+eosCj1YJ00FGmiQOj2HnsBfL5YGvy1nL3X/DPFbq+cMbC49P17n2xaYdhdTNP rpeXCbbIeYovXDonZrp5I93MqR4dkMFRBAMtAxTT/IqNHmfbSBmVPIS864GG4Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1677171059; 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; bh=ZF2hbTTiLd7Gn3S1lw+DHdwYUZZtff8YaI8O8PU3XRU=; b=Atya4/L+TQj7YPN91hokPn+H0MvCAde9BESZp0l6xKPDdnMHn/P2GzG9ZFnNapIWulEJ3K OXSyF9Lews2MugUpmuTwbEMhMJCwuiS7wHk+DrjGrYoaNVMONgXOIHWjkyzXWNba764ZNg THFaQPG96dlXHZmZV3GfmFTwd0Mo78WGs1c0D4IpojUq9PPOYzQRvkhl0nmBJUhO4uslr7 3ljshaNcf+KlzBLUyq4dOsZS/h9Ixlqn2xtM/qkMTDdSN+ps01HblTyVDkK5jT+qodOPzs t0K09HYbHyxEmCO99gThecfa8cGzK1TTca+gv2f8O8F86c99jw064eq8V+2Cew== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1677171059; a=rsa-sha256; cv=none; b=FPn3TT5Vsrj/Gtc8xul0miZTIsJWx/SENyx9PYowiGSxX4kH7S8JMrxaSWkalhgJDVds8F oVYatIK6ZrZkLMVwEjxZwe73uvNVPFwMrIRoi9wAV4blOjtTAI+IXpGAJntQYp5cbyFsn4 Guhau+mitGPHtuwFu1xo7UP6v/WFCtBECxIzC53LNB/WhThjg5MFxi4kpsCgRTeoxJVdGs DWRWIoBLXDXZx2wZ8SNf486oR8EaSJIlfEOQboDa6cxiP5qH4X6mj5gmUOIinSpRzhq+6q 1qPy4aAAIacQV8s1vO7toletmawg7YbVqqZTlFIEoxXNaXqt88skJvPiFgPQKg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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 did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4PMzYb13bbzqLX; Thu, 23 Feb 2023 16:50:59 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 31NGoxMZ086480; Thu, 23 Feb 2023 16:50:59 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 31NGoxWf086479; Thu, 23 Feb 2023 16:50:59 GMT (envelope-from git) Date: Thu, 23 Feb 2023 16:50:59 GMT Message-Id: <202302231650.31NGoxWf086479@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: =?utf-8?Q?Kornel=20Dul=C4=99ba?= Subject: git: 98c666cf8758 - main - arm: Fix initialization of VFP context List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kd X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 98c666cf875831f252ec5ee5dece2722f02e8706 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by kd: URL: https://cgit.FreeBSD.org/src/commit/?id=98c666cf875831f252ec5ee5dece2722f02e8706 commit 98c666cf875831f252ec5ee5dece2722f02e8706 Author: Kornel Dulęba AuthorDate: 2023-02-20 14:48:40 +0000 Commit: Kornel Dulęba CommitDate: 2023-02-23 16:50:26 +0000 arm: Fix initialization of VFP context Make sure that pcb_vfpsaved is always initialized. Create a vfp_new_thread helper that is heavily based on the arm64 logic. While here remove un unnecessary assigment and add an assertion to make sure that it's been properly initialized before we return from a VFP exception. Reported by: Mark Millard Tested by: Mark Millard Differential Revision: https://reviews.freebsd.org/D38698 --- sys/arm/arm/machdep.c | 1 + sys/arm/arm/vfp.c | 33 +++++++++++++++++++++++++++++++-- sys/arm/arm/vm_machdep.c | 10 +++++++--- sys/arm/include/vfp.h | 1 + 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/sys/arm/arm/machdep.c b/sys/arm/arm/machdep.c index 0c82190181be..8c0d79caffa8 100644 --- a/sys/arm/arm/machdep.c +++ b/sys/arm/arm/machdep.c @@ -380,6 +380,7 @@ init_proc0(vm_offset_t kstack) thread0.td_pcb->pcb_fpflags = 0; thread0.td_pcb->pcb_vfpcpu = -1; thread0.td_pcb->pcb_vfpstate.fpscr = VFPSCR_DN; + thread0.td_pcb->pcb_vfpsaved = &thread0.td_pcb->pcb_vfpstate; thread0.td_frame = &proc0_tf; pcpup->pc_curpcb = thread0.td_pcb; } diff --git a/sys/arm/arm/vfp.c b/sys/arm/arm/vfp.c index d51c4b6e0618..b417e02f6998 100644 --- a/sys/arm/arm/vfp.c +++ b/sys/arm/arm/vfp.c @@ -34,6 +34,7 @@ __FBSDID("$FreeBSD$"); #ifdef VFP #include #include +#include #include #include #include @@ -251,17 +252,45 @@ vfp_bounce(u_int addr, u_int insn, struct trapframe *frame, int code) curpcb = curthread->td_pcb; cpu = PCPU_GET(cpuid); if (curpcb->pcb_vfpcpu != cpu || curthread != PCPU_GET(fpcurthread)) { - if (curpcb->pcb_vfpsaved == NULL) - curpcb->pcb_vfpsaved = &curpcb->pcb_vfpstate; vfp_restore(curpcb->pcb_vfpsaved); curpcb->pcb_vfpcpu = cpu; PCPU_SET(fpcurthread, curthread); } critical_exit(); + + KASSERT(curpcb->pcb_vfpsaved == &curpcb->pcb_vfpstate, + ("Kernel VFP state in use when entering userspace")); + return (0); } +/* + * Update the VFP state for a forked process or new thread. The PCB will + * have been copied from the old thread. + * The code is heavily based on arm64 logic. + */ +void +vfp_new_thread(struct thread *newtd, struct thread *oldtd, bool fork) +{ + struct pcb *newpcb; + + newpcb = newtd->td_pcb; + + /* Kernel threads start with clean VFP */ + if ((oldtd->td_pflags & TDP_KTHREAD) != 0) { + newpcb->pcb_fpflags &= + ~(PCB_FP_STARTED | PCB_FP_KERN | PCB_FP_NOSAVE); + } else { + MPASS((newpcb->pcb_fpflags & (PCB_FP_KERN|PCB_FP_NOSAVE)) == 0); + if (!fork) { + newpcb->pcb_fpflags &= ~PCB_FP_STARTED; + } + } + + newpcb->pcb_vfpsaved = &newpcb->pcb_vfpstate; + newpcb->pcb_vfpcpu = UINT_MAX; +} /* * Restore the given state to the VFP hardware. */ diff --git a/sys/arm/arm/vm_machdep.c b/sys/arm/arm/vm_machdep.c index d899e2cd584b..144596162912 100644 --- a/sys/arm/arm/vm_machdep.c +++ b/sys/arm/arm/vm_machdep.c @@ -137,9 +137,9 @@ cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags) pcb2->pcb_regs.sf_sp = STACKALIGN(td2->td_frame); pcb2->pcb_regs.sf_tpidrurw = (register_t)get_tls(); - pcb2->pcb_vfpcpu = -1; - pcb2->pcb_vfpsaved = &pcb2->pcb_vfpstate; - pcb2->pcb_vfpstate.fpscr = initial_fpscr; +#ifdef VFP + vfp_new_thread(td2, td1, true); +#endif tf = td2->td_frame; tf->tf_spsr &= ~PSR_C; @@ -216,6 +216,10 @@ cpu_copy_thread(struct thread *td, struct thread *td0) td->td_frame->tf_spsr &= ~PSR_C; td->td_frame->tf_r0 = 0; +#ifdef VFP + vfp_new_thread(td, td0, false); +#endif + /* Setup to release spin count in fork_exit(). */ td->td_md.md_spinlock_count = 1; td->td_md.md_saved_cspr = PSR_SVC32_MODE; diff --git a/sys/arm/include/vfp.h b/sys/arm/include/vfp.h index e15e088970b5..5a27cc68afdd 100644 --- a/sys/arm/include/vfp.h +++ b/sys/arm/include/vfp.h @@ -157,6 +157,7 @@ struct vfp_state { void get_vfpcontext(struct thread *, mcontext_vfp_t *); void set_vfpcontext(struct thread *, mcontext_vfp_t *); void vfp_init(void); +void vfp_new_thread(struct thread*, struct thread*, bool); void vfp_store(struct vfp_state *, boolean_t); void vfp_discard(struct thread *); void vfp_restore_state(void);