From nobody Thu Jun 08 22:33:45 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 4QcfBd36f4z4bmMg; Thu, 8 Jun 2023 22:33:45 +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 4QcfBd2bmMz3J31; Thu, 8 Jun 2023 22:33:45 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1686263625; 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=0+LDLuAAu0x+HMjdtDe9AllqQF0KUJL7b2nWO4dagb8=; b=UadFdEUJ4oRk5Hnsc5gFUm/rtpWTZ64eNd0cf4xS5E96PkN9fMaqomeBOMyIOFyTwnVQCS ATSsOIB4zas1+wiDCzk35jAINnGLyOx28KLy6/ZBcBqsAOjTzqdGo0ALPNKIP9YnoQWPkW OuTM0qPwQgNwrf7YHw3NJdroXXRDz8cLG3zlQ6nY5Tk/DKsR34AXjZMAQ8VSWIKBioPlrs /pldQ1WpOSlBFpW5bAiv9c02M9eILHZKtDcgnB44O91AxIbLCd+oRe6IH4z2W5K6pd9nWr eOzNJ5NlTFv8W1+smyAggwB6tdQSrfmjSrHmP3/LyBkQF8ftKG14VthBNHvITg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1686263625; 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=0+LDLuAAu0x+HMjdtDe9AllqQF0KUJL7b2nWO4dagb8=; b=m5fpMMdH1XW1ECw14z48eQ8QyLGZZXTrc9vR1mljDox3zKP9XerxGz+ZQ84El+jCj8rFmy mihK8ZswKE1rSPorAu7o6ZkGew3UNGHMNW1lcQQBef5hbzad+axHFaevgAa839YgljUW9g 9Iuw3RJ2qSRZUyneOQd2X9JKZt2HeBggBUQUYn67WYFZiEFtIHnic/pK6Bjh8OXIggfhN/ 3ik0UctH6PlfcXwrGAwDG/QuUqIxgnTqEzfI6TR4TtgmOtiAVMYCUUZJSsE0nHdjkRUYku 776DtTpzNtGxxvu2kzyRir2dnhByOj7/8E2JwyVYYbxV4V3bhsAymRTTNqlZkw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1686263625; a=rsa-sha256; cv=none; b=jBtRsFQq9z+7LrzlayDDXiD2OqlZlY50i6X94UmyXR8teCYYwNb1VL6i0JhKDi19osi095 qQGy36TG8rKbHs3JJe0xJE932DLQtIxKeDX+wnZ5U7RQxbRyCq7YRSje+y4iuZ9W4VBTP1 ZNF7CBoYvOg6Ny1eFnTQbvc77MifwpwflJ4fksCP/uOOr1s2uvnHdSUYDIWKVTDfMKQ13H lE2NbVPHNuNHG/jVXz5b7Z0Qm/vP+55NEc2shZ3BXyRKg7onwkf+fhr9g9Hz1wzFfyKyHM cs+GDwaGVKLsUj74h+VkUeS3Dl9w5otEWg+YC2veByIN5K5hVWSLzw8t1ez2JA== 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 4QcfBd1fFVz1BZR; Thu, 8 Jun 2023 22:33:45 +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 358MXjbC059685; Thu, 8 Jun 2023 22:33:45 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 358MXjXl059683; Thu, 8 Jun 2023 22:33:45 GMT (envelope-from git) Date: Thu, 8 Jun 2023 22:33:45 GMT Message-Id: <202306082233.358MXjXl059683@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: 920184ed6e43 - main - linux(4): In preparation for xsave refactor fxsave code 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: 920184ed6e43b19f4b71253b55fd3ba66a7f0bb2 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by dchagin: URL: https://cgit.FreeBSD.org/src/commit/?id=920184ed6e43b19f4b71253b55fd3ba66a7f0bb2 commit 920184ed6e43b19f4b71253b55fd3ba66a7f0bb2 Author: Dmitry Chagin AuthorDate: 2023-06-08 22:32:46 +0000 Commit: Dmitry Chagin CommitDate: 2023-06-08 22:32:46 +0000 linux(4): In preparation for xsave refactor fxsave code on amd64 Due to fxsave area is os independent reimplement fxsave handmade code using copying of a whole area. Reviewed by: kib Differential Revision: https://reviews.freebsd.org/D40443 MFC after: 2 weeks --- sys/amd64/linux/linux_sysvec.c | 126 ++++++++++++++++++++----------------- sys/x86/linux/linux_x86_sigframe.h | 9 ++- 2 files changed, 76 insertions(+), 59 deletions(-) diff --git a/sys/amd64/linux/linux_sysvec.c b/sys/amd64/linux/linux_sysvec.c index 547777127d2a..acc7593308af 100644 --- a/sys/amd64/linux/linux_sysvec.c +++ b/sys/amd64/linux/linux_sysvec.c @@ -79,6 +79,10 @@ __FBSDID("$FreeBSD$"); #include +_Static_assert(sizeof(struct l_fpstate) == + sizeof(__typeof(((mcontext_t *)0)->mc_fpstate)), + "fxsave area size incorrect"); + MODULE_VERSION(linux64, 1); #define LINUX_VDSOPAGE_SIZE PAGE_SIZE * 2 @@ -278,6 +282,31 @@ linux_exec_setregs(struct thread *td, struct image_params *imgp, fpstate_drop(td); } +static int +linux_fxrstor(struct thread *td, mcontext_t *mcp, struct l_sigcontext *sc) +{ + struct savefpu *fp = (struct savefpu *)&mcp->mc_fpstate[0]; + int error; + + error = copyin(PTRIN(sc->sc_fpstate), fp, sizeof(mcp->mc_fpstate)); + if (error != 0) + return (error); + bzero(&fp->sv_pad[0], sizeof(fp->sv_pad)); + return (set_fpcontext(td, mcp, NULL, 0)); +} + +static int +linux_copyin_fpstate(struct thread *td, struct l_ucontext *uc) +{ + mcontext_t mc; + + bzero(&mc, sizeof(mc)); + mc.mc_ownedfp = _MC_FPOWNED_FPU; + mc.mc_fpformat = _MC_FPFMT_XMM; + + return (linux_fxrstor(td, &mc, &uc->uc_mcontext)); +} + /* * Copied from amd64/amd64/machdep.c */ @@ -288,10 +317,9 @@ linux_rt_sigreturn(struct thread *td, struct linux_rt_sigreturn_args *args) struct l_rt_sigframe sf; struct l_sigcontext *context; struct trapframe *regs; - mcontext_t mc; unsigned long rflags; sigset_t bmask; - int error, i; + int error; ksiginfo_t ksi; regs = td->td_frame; @@ -363,41 +391,42 @@ 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); - } + error = linux_copyin_fpstate(td, &sf.sf_uc); + 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); } +static int +linux_fxsave(mcontext_t *mcp, void *ufp) +{ + struct l_fpstate *fx = (struct l_fpstate *)&mcp->mc_fpstate[0]; + + bzero(&fx->reserved2[0], sizeof(fx->reserved2)); + return (copyout(fx, ufp, sizeof(*fx))); +} + +static int +linux_copyout_fpstate(struct thread *td, struct l_ucontext *uc, char **sp) +{ + mcontext_t mc; + char *ufp = *sp; + + get_fpcontext(td, &mc, NULL, NULL); + KASSERT(mc.mc_fpformat != _MC_FPFMT_NODEV, ("fpu not present")); + + /* fxsave area */ + ufp -= sizeof(struct l_fpstate); + *sp = ufp = (char *)((unsigned long)ufp & ~0x3Ful); + + return (linux_fxsave(&mc, ufp)); +} + /* * copied from amd64/amd64/machdep.c * @@ -412,10 +441,8 @@ linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) struct sigacts *psp; char *sp; struct trapframe *regs; - struct savefpu *svfp; - mcontext_t mc; int sig, code; - int oonstack, issiginfo, i; + int oonstack, issiginfo; td = curthread; p = td->td_proc; @@ -447,6 +474,14 @@ linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) mtx_unlock(&psp->ps_mtx); PROC_UNLOCK(p); + if (linux_copyout_fpstate(td, &sf.sf_uc, &sp) != 0) { + uprintf("pid %d comm %s linux can't save fpu state, killing\n", + p->p_pid, p->p_comm); + PROC_LOCK(p); + sigexit(td, SIGILL); + } + sf.sf_uc.uc_mcontext.sc_fpstate = (register_t)sp; + /* Make room, keeping the stack aligned. */ sp -= sizeof(struct l_rt_sigframe); sfp = (struct l_rt_sigframe *)((unsigned long)sp & ~0xFul); @@ -477,29 +512,6 @@ linux_rt_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask) sf.sf_uc.uc_mcontext.sc_trapno = bsd_to_linux_trapcode(code); sf.sf_uc.uc_mcontext.sc_cr2 = (register_t)ksi->ksi_addr; - 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 *)((char *)sfp + - offsetof(struct l_rt_sigframe, sf_fs)); - /* Translate the signal. */ sig = bsd_to_linux_signal(sig); /* Fill in POSIX parts. */ diff --git a/sys/x86/linux/linux_x86_sigframe.h b/sys/x86/linux/linux_x86_sigframe.h index 74e7a36e2e71..e5687069651f 100644 --- a/sys/x86/linux/linux_x86_sigframe.h +++ b/sys/x86/linux/linux_x86_sigframe.h @@ -170,7 +170,13 @@ struct l_sigcontext { l_ulong sc_trapno; l_sigset_t sc_mask; l_ulong sc_cr2; - struct l_fpstate *sc_fpstate; + /* + * On Linux sc_fpstate is (struct l_fpstate *) or (struct l_xstate *) + * depending on the FP_XSTATE_MAGIC1 encoded in the sw_reserved + * bytes of (struct l_fpstate) and FP_XSTATE_MAGIC2 present at the end + * of extended memory layout. + */ + l_uintptr_t sc_fpstate; l_ulong sc_reserved1[8]; }; @@ -189,7 +195,6 @@ 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) */