From nobody Thu Feb 02 17:22:06 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 4P75FB6D68z3kNn1; Thu, 2 Feb 2023 17:22:06 +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 4P75FB5gxPz3vf6; Thu, 2 Feb 2023 17:22:06 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1675358526; 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=TE9ObSjOJJ/Ke6jgzCnGJRO6vBiL7p+FwfctdnwezuI=; b=HW5VxxcVI40BaqgPhg+eaG4aJctuYsg1q+HktcB+DIHA9eVYgRXQvhQSQbAkVmL9UewuAr O3HraIER4Sne+KAKA5Ogt4m5yP2fI7C9OhOk1F3pXtxaTNP/e0xM7YhPGuozYh1KCECIWH kKKl3P6jcODRB+l8hOnUew5URhuwx3kBEh5tygmgDm2P9VTI0/5iRL3EGCQucdAhKRj/7J 602DMM4oTf82M/ZLFGOAZNW5UOhYe5K3a8tldal4UPYEARtx2n8JDnlj3HSxQveQp7FY+k Yw+TGl5aNW+E6MXKzNCuYxIudp5MrFCWpVJg+DPUPMohKw09bS9All/X5SzlFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1675358526; 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=TE9ObSjOJJ/Ke6jgzCnGJRO6vBiL7p+FwfctdnwezuI=; b=QJEF07/HyzDlfAcDvsapMKboG1y/ZNQ4dk6VYXH74WyGjSEGJ9uWcEe4fCso/cShnZKnOi SWMIOktnJIHqjZy0JLKnHQVz07NMx5RDz99IV7YlDYlg/BEuaJQoYXXuI9Bw6nslqFHK0/ Qjqsdvglib+8WCvy7mN+e+YDOSDMBj4uVQMLVTyZGm6sY1Xzwo87K550V4W6ojWMMcNUyI yGBs4goZMwatGYkaK07G6MepakL8Z/spwJ/dmm7x//3pe5ez2mhYReZClIdPxP0QXbAVEq oxcfWA74XjEMagsC74VMHaC0xUaP1FT8QRYCKE8YIndIy0abjn8v2B4qRxCjBw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1675358526; a=rsa-sha256; cv=none; b=NpziC5AaqRYg1RsWhIYeZjDtLzdqDdtxXsaasJKpA3w3zIclQIbqHiOcXHyqkq4hINOV/S d0Mcp4VBgQ4vKuNfPJ3fJH9rUIkywrg1oi3qPlB328cvT9jPLv+E2ZgJHObooXI6woiN5j LQw17GKt9/ylcOvY6hUc6aANlczZasmwywd2wsRKk1732tyMF21uFAcyZGFoz2qubh/Iv2 BADsQshXLUQ2cQZwHEBeZfH7R8OPQCHeBTVz1/zVOh9eIhtn8u/JuHybitGmOpf6lZUuNt c34g6w9svLguKsB1a897Z9Wgdt0yROTxXIkBjoTmvV9Wn866aaxTtGNrtwRaKg== 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 4P75FB4mm4z12xP; Thu, 2 Feb 2023 17:22:06 +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 312HM6m1004596; Thu, 2 Feb 2023 17:22:06 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 312HM6Zx004595; Thu, 2 Feb 2023 17:22:06 GMT (envelope-from git) Date: Thu, 2 Feb 2023 17:22:06 GMT Message-Id: <202302021722.312HM6Zx004595@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Dmitry Chagin Subject: git: a95cb95e12e5 - main - linux(4): Preserve fpu fxsave state across signal delivery on amd64. 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: dchagin X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: a95cb95e12e537dbe70f9de18cc0fe98e4a5ebf5 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by dchagin: URL: https://cgit.FreeBSD.org/src/commit/?id=a95cb95e12e537dbe70f9de18cc0fe98e4a5ebf5 commit a95cb95e12e537dbe70f9de18cc0fe98e4a5ebf5 Author: Dmitry Chagin AuthorDate: 2023-02-02 17:21:37 +0000 Commit: Dmitry Chagin CommitDate: 2023-02-02 17:21:37 +0000 linux(4): Preserve fpu fxsave state across signal delivery on amd64. PR: 240768 Reviewed by: kib Differential Revision: https://reviews.freebsd.org/D38302 MFC after: 1 week --- sys/amd64/linux/linux_sysvec.c | 70 ++++++++++++++++++++++++++++++++++---- sys/x86/linux/linux_x86_sigframe.h | 7 ++-- 2 files changed, 68 insertions(+), 9 deletions(-) diff --git a/sys/amd64/linux/linux_sysvec.c b/sys/amd64/linux/linux_sysvec.c index 96cdf5e5c818..c75dc6a26437 100644 --- a/sys/amd64/linux/linux_sysvec.c +++ b/sys/amd64/linux/linux_sysvec.c @@ -319,21 +319,22 @@ int linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args) { struct proc *p; - struct l_ucontext uc; + struct l_rt_sigframe sf; struct l_sigcontext *context; struct trapframe *regs; + mcontext_t mc; unsigned long rflags; sigset_t bmask; - int error; + int error, i; ksiginfo_t ksi; regs = td->td_frame; - error = copyin((void *)regs->tf_rbx, &uc, sizeof(uc)); + error = copyin((void *)regs->tf_rbx, &sf, sizeof(sf)); if (error != 0) return (error); p = td->td_proc; - context = &uc.uc_mcontext; + context = &sf.sf_uc.uc_mcontext; rflags = context->sc_rflags; /* @@ -372,7 +373,7 @@ linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args) return (EINVAL); } - linux_to_bsd_sigset(&uc.uc_sigmask, &bmask); + linux_to_bsd_sigset(&sf.sf_uc.uc_sigmask, &bmask); kern_sigprocmask(td, SIG_SETMASK, &bmask, NULL, 0); regs->tf_rdi = context->sc_rdi; @@ -396,6 +397,37 @@ linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args) regs->tf_err = context->sc_err; regs->tf_rflags = rflags; + if (sf.sf_uc.uc_mcontext.sc_fpstate != NULL) { + struct savefpu *svfp = (struct savefpu *)mc.mc_fpstate; + + bzero(&mc, sizeof(mc)); + mc.mc_ownedfp = _MC_FPOWNED_FPU; + mc.mc_fpformat = _MC_FPFMT_XMM; + + svfp->sv_env.en_cw = sf.sf_fs.cwd; + svfp->sv_env.en_sw = sf.sf_fs.swd; + svfp->sv_env.en_tw = sf.sf_fs.twd; + svfp->sv_env.en_opcode = sf.sf_fs.fop; + svfp->sv_env.en_rip = sf.sf_fs.rip; + svfp->sv_env.en_rdp = sf.sf_fs.rdp; + svfp->sv_env.en_mxcsr = sf.sf_fs.mxcsr; + svfp->sv_env.en_mxcsr_mask = sf.sf_fs.mxcsr_mask; + /* FPU registers */ + for (i = 0; i < nitems(svfp->sv_fp); ++i) + bcopy(&sf.sf_fs.st[i], svfp->sv_fp[i].fp_acc.fp_bytes, + sizeof(svfp->sv_fp[i].fp_acc.fp_bytes)); + /* SSE registers */ + for (i = 0; i < nitems(svfp->sv_xmm); ++i) + bcopy(&sf.sf_fs.xmm[i], svfp->sv_xmm[i].xmm_bytes, + sizeof(svfp->sv_xmm[i].xmm_bytes)); + error = set_fpcontext(td, &mc, NULL, 0); + if (error != 0) { + uprintf("pid %d comm %s linux can't restore fpu state %d\n", + p->p_pid, p->p_comm, error); + return (error); + } + } + set_pcb_flags(td->td_pcb, PCB_FULL_IRET); return (EJUSTRETURN); } @@ -414,8 +446,10 @@ linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) struct sigacts *psp; caddr_t sp; struct trapframe *regs; + struct savefpu *svfp; + mcontext_t mc; int sig, code; - int oonstack, issiginfo; + int oonstack, issiginfo, i; td = curthread; p = td->td_proc; @@ -477,6 +511,29 @@ linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) mtx_unlock(&psp->ps_mtx); PROC_UNLOCK(p); + get_fpcontext(td, &mc, NULL, NULL); + KASSERT(mc.mc_fpformat != _MC_FPFMT_NODEV, ("fpu not present")); + svfp = (struct savefpu *)mc.mc_fpstate; + + sf.sf_fs.cwd = svfp->sv_env.en_cw; + sf.sf_fs.swd = svfp->sv_env.en_sw; + sf.sf_fs.twd = svfp->sv_env.en_tw; + sf.sf_fs.fop = svfp->sv_env.en_opcode; + sf.sf_fs.rip = svfp->sv_env.en_rip; + sf.sf_fs.rdp = svfp->sv_env.en_rdp; + sf.sf_fs.mxcsr = svfp->sv_env.en_mxcsr; + sf.sf_fs.mxcsr_mask = svfp->sv_env.en_mxcsr_mask; + /* FPU registers */ + for (i = 0; i < nitems(svfp->sv_fp); ++i) + bcopy(svfp->sv_fp[i].fp_acc.fp_bytes, &sf.sf_fs.st[i], + sizeof(svfp->sv_fp[i].fp_acc.fp_bytes)); + /* SSE registers */ + for (i = 0; i < nitems(svfp->sv_xmm); ++i) + bcopy(svfp->sv_xmm[i].xmm_bytes, &sf.sf_fs.xmm[i], + sizeof(svfp->sv_xmm[i].xmm_bytes)); + sf.sf_uc.uc_mcontext.sc_fpstate = (struct l_fpstate *)((caddr_t)sfp + + offsetof(struct l_rt_sigframe, sf_fs)); + /* Translate the signal. */ sig = bsd_to_linux_signal(sig); /* Fill in POSIX parts. */ @@ -490,6 +547,7 @@ linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) sigexit(td, SIGILL); } + fpstate_drop(td); /* Build the argument list for the signal handler. */ regs->tf_rdi = sig; /* arg 1 in %rdi */ regs->tf_rax = 0; diff --git a/sys/x86/linux/linux_x86_sigframe.h b/sys/x86/linux/linux_x86_sigframe.h index 75d9a104a345..74e7a36e2e71 100644 --- a/sys/x86/linux/linux_x86_sigframe.h +++ b/sys/x86/linux/linux_x86_sigframe.h @@ -138,10 +138,10 @@ struct l_fpstate { u_int64_t rdp; u_int32_t mxcsr; u_int32_t mxcsr_mask; - u_int32_t st_space[32]; - u_int32_t xmm_space[64]; + u_int8_t st[8][16]; + u_int8_t xmm[16][16]; u_int32_t reserved2[24]; -}; +} __aligned(16); struct l_sigcontext { l_ulong sc_r8; @@ -189,6 +189,7 @@ struct l_ucontext { struct l_rt_sigframe { struct l_ucontext sf_uc; struct l_siginfo sf_si; + struct l_fpstate sf_fs; }; #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */