Merge tag 'pull-lu-20240526' of https://gitlab.com/rth7680/qemu into staging

target/i386: Introduce X86Access and use for xsave and friends
linux-user/i386: Fix allocation and alignment of fp state in signal frame

# -----BEGIN PGP SIGNATURE-----
#
# iQFRBAABCgA7FiEEekgeeIaLTbaoWgXAZN846K9+IV8FAmZT2GwdHHJpY2hhcmQu
# aGVuZGVyc29uQGxpbmFyby5vcmcACgkQZN846K9+IV87pQf9F/cmrKQG1mVWKmJd
# MI7l63lbxejdgAADv1nmro+oapCsJSaQeUSrYp904ydqJjVfBJkaoXfknGsvxrNA
# oW7nEuYt0sBKdaBUKhYpMOJ3ivfw7lVVMJmjNv9ngZRhW+WOoJrBHoleUkVLiM7D
# rxkMLL+LQ7BR9i0Lv1unorOkqUPGNOnEd45qRn6k1g/Qnqi8SNMzxFwO8+232u8m
# EG9un/oh4mKPyb5vSg3Y4JLg+yDKCRScBqBU1wcKFe1u+umBkv2BNcU+k62AJh1q
# bv8i1n+X/dFAd1aj0NEupi04EOZIof5m3T4YIWg7M4I94NiFWNZ18vgskkmiO+Mo
# 0KPd/A==
# =sYrE
# -----END PGP SIGNATURE-----
# gpg: Signature made Sun 26 May 2024 05:48:44 PM PDT
# gpg:                using RSA key 7A481E78868B4DB6A85A05C064DF38E8AF7E215F
# gpg:                issuer "richard.henderson@linaro.org"
# gpg: Good signature from "Richard Henderson <richard.henderson@linaro.org>" [ultimate]

* tag 'pull-lu-20240526' of https://gitlab.com/rth7680/qemu: (28 commits)
  target/i386: Pass host pointer and size to cpu_x86_{xsave,xrstor}
  target/i386: Pass host pointer and size to cpu_x86_{fxsave,fxrstor}
  target/i386: Pass host pointer and size to cpu_x86_{fsave,frstor}
  target/i386: Convert do_xrstor to X86Access
  target/i386: Convert do_xsave to X86Access
  linux-user/i386: Honor xfeatures in xrstor_sigcontext
  linux-user/i386: Fix allocation and alignment of fp state
  linux-user/i386: Return boolean success from xrstor_sigcontext
  linux-user/i386: Return boolean success from restore_sigcontext
  linux-user/i386: Fix -mregparm=3 for signal delivery
  linux-user/i386: Split out struct target_fregs_state
  linux-user/i386: Replace target_fpstate_fxsave with X86LegacyXSaveArea
  linux-user/i386: Remove xfeatures from target_fpstate_fxsave
  linux-user/i386: Drop xfeatures_size from sigcontext arithmetic
  target/i386: Add {hw,sw}_reserved to X86LegacyXSaveArea
  target/i386: Add rbfm argument to cpu_x86_{xsave,xrstor}
  target/i386: Split out do_xsave_chk
  target/i386: Convert do_xrstor_* to X86Access
  target/i386: Convert do_xsave_* to X86Access
  tagret/i386: Convert do_fxsave, do_fxrstor to X86Access
  ...

Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
This commit is contained in:
Richard Henderson
2024-05-26 17:51:00 -07:00
8 changed files with 1024 additions and 509 deletions
+400 -271
View File
File diff suppressed because it is too large Load Diff
+35 -22
View File
@@ -1425,23 +1425,34 @@ typedef struct {
*/
#define UNASSIGNED_APIC_ID 0xFFFFFFFF
typedef union X86LegacyXSaveArea {
struct {
uint16_t fcw;
uint16_t fsw;
uint8_t ftw;
uint8_t reserved;
uint16_t fpop;
uint64_t fpip;
uint64_t fpdp;
uint32_t mxcsr;
uint32_t mxcsr_mask;
FPReg fpregs[8];
uint8_t xmm_regs[16][16];
typedef struct X86LegacyXSaveArea {
uint16_t fcw;
uint16_t fsw;
uint8_t ftw;
uint8_t reserved;
uint16_t fpop;
union {
struct {
uint64_t fpip;
uint64_t fpdp;
};
struct {
uint32_t fip;
uint32_t fcs;
uint32_t foo;
uint32_t fos;
};
};
uint8_t data[512];
uint32_t mxcsr;
uint32_t mxcsr_mask;
FPReg fpregs[8];
uint8_t xmm_regs[16][16];
uint32_t hw_reserved[12];
uint32_t sw_reserved[12];
} X86LegacyXSaveArea;
QEMU_BUILD_BUG_ON(sizeof(X86LegacyXSaveArea) != 512);
typedef struct X86XSaveHeader {
uint64_t xstate_bv;
uint64_t xcomp_bv;
@@ -2255,15 +2266,17 @@ int cpu_x86_get_descr_debug(CPUX86State *env, unsigned int selector,
/* used for debug or cpu save/restore */
/* cpu-exec.c */
/* the following helpers are only usable in user mode simulation as
they can trigger unexpected exceptions */
/*
* The following helpers are only usable in user mode simulation.
* The host pointers should come from lock_user().
*/
void cpu_x86_load_seg(CPUX86State *s, X86Seg seg_reg, int selector);
void cpu_x86_fsave(CPUX86State *s, target_ulong ptr, int data32);
void cpu_x86_frstor(CPUX86State *s, target_ulong ptr, int data32);
void cpu_x86_fxsave(CPUX86State *s, target_ulong ptr);
void cpu_x86_fxrstor(CPUX86State *s, target_ulong ptr);
void cpu_x86_xsave(CPUX86State *s, target_ulong ptr);
void cpu_x86_xrstor(CPUX86State *s, target_ulong ptr);
void cpu_x86_fsave(CPUX86State *s, void *host, size_t len);
void cpu_x86_frstor(CPUX86State *s, void *host, size_t len);
void cpu_x86_fxsave(CPUX86State *s, void *host, size_t len);
void cpu_x86_fxrstor(CPUX86State *s, void *host, size_t len);
void cpu_x86_xsave(CPUX86State *s, void *host, size_t len, uint64_t rbfm);
bool cpu_x86_xrstor(CPUX86State *s, void *host, size_t len, uint64_t rbfm);
/* cpu.c */
void x86_cpu_vendor_words2str(char *dst, uint32_t vendor1,
+169
View File
@@ -0,0 +1,169 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* Access guest memory in blocks. */
#include "qemu/osdep.h"
#include "cpu.h"
#include "exec/cpu_ldst.h"
#include "exec/exec-all.h"
#include "access.h"
void access_prepare_mmu(X86Access *ret, CPUX86State *env,
vaddr vaddr, unsigned size,
MMUAccessType type, int mmu_idx, uintptr_t ra)
{
int size1, size2;
void *haddr1, *haddr2;
assert(size > 0 && size <= TARGET_PAGE_SIZE);
size1 = MIN(size, -(vaddr | TARGET_PAGE_MASK)),
size2 = size - size1;
memset(ret, 0, sizeof(*ret));
ret->vaddr = vaddr;
ret->size = size;
ret->size1 = size1;
ret->mmu_idx = mmu_idx;
ret->env = env;
ret->ra = ra;
haddr1 = probe_access(env, vaddr, size1, type, mmu_idx, ra);
ret->haddr1 = haddr1;
if (unlikely(size2)) {
haddr2 = probe_access(env, vaddr + size1, size2, type, mmu_idx, ra);
if (haddr2 == haddr1 + size1) {
ret->size1 = size;
} else {
#ifdef CONFIG_USER_ONLY
g_assert_not_reached();
#else
ret->haddr2 = haddr2;
#endif
}
}
}
void access_prepare(X86Access *ret, CPUX86State *env, vaddr vaddr,
unsigned size, MMUAccessType type, uintptr_t ra)
{
int mmu_idx = cpu_mmu_index(env_cpu(env), false);
access_prepare_mmu(ret, env, vaddr, size, type, mmu_idx, ra);
}
static void *access_ptr(X86Access *ac, vaddr addr, unsigned len)
{
vaddr offset = addr - ac->vaddr;
assert(addr >= ac->vaddr);
#ifdef CONFIG_USER_ONLY
assert(offset <= ac->size1 - len);
return ac->haddr1 + offset;
#else
if (likely(offset <= ac->size1 - len)) {
return ac->haddr1 + offset;
}
assert(offset <= ac->size - len);
/*
* If the address is not naturally aligned, it might span both pages.
* Only return ac->haddr2 if the area is entirely within the second page,
* otherwise fall back to slow accesses.
*/
if (likely(offset >= ac->size1)) {
return ac->haddr2 + (offset - ac->size1);
}
return NULL;
#endif
}
#ifdef CONFIG_USER_ONLY
# define test_ptr(p) true
#else
# define test_ptr(p) likely(p)
#endif
uint8_t access_ldb(X86Access *ac, vaddr addr)
{
void *p = access_ptr(ac, addr, sizeof(uint8_t));
if (test_ptr(p)) {
return ldub_p(p);
}
return cpu_ldub_mmuidx_ra(ac->env, addr, ac->mmu_idx, ac->ra);
}
uint16_t access_ldw(X86Access *ac, vaddr addr)
{
void *p = access_ptr(ac, addr, sizeof(uint16_t));
if (test_ptr(p)) {
return lduw_le_p(p);
}
return cpu_lduw_le_mmuidx_ra(ac->env, addr, ac->mmu_idx, ac->ra);
}
uint32_t access_ldl(X86Access *ac, vaddr addr)
{
void *p = access_ptr(ac, addr, sizeof(uint32_t));
if (test_ptr(p)) {
return ldl_le_p(p);
}
return cpu_ldl_le_mmuidx_ra(ac->env, addr, ac->mmu_idx, ac->ra);
}
uint64_t access_ldq(X86Access *ac, vaddr addr)
{
void *p = access_ptr(ac, addr, sizeof(uint64_t));
if (test_ptr(p)) {
return ldq_le_p(p);
}
return cpu_ldq_le_mmuidx_ra(ac->env, addr, ac->mmu_idx, ac->ra);
}
void access_stb(X86Access *ac, vaddr addr, uint8_t val)
{
void *p = access_ptr(ac, addr, sizeof(uint8_t));
if (test_ptr(p)) {
stb_p(p, val);
} else {
cpu_stb_mmuidx_ra(ac->env, addr, val, ac->mmu_idx, ac->ra);
}
}
void access_stw(X86Access *ac, vaddr addr, uint16_t val)
{
void *p = access_ptr(ac, addr, sizeof(uint16_t));
if (test_ptr(p)) {
stw_le_p(p, val);
} else {
cpu_stw_le_mmuidx_ra(ac->env, addr, val, ac->mmu_idx, ac->ra);
}
}
void access_stl(X86Access *ac, vaddr addr, uint32_t val)
{
void *p = access_ptr(ac, addr, sizeof(uint32_t));
if (test_ptr(p)) {
stl_le_p(p, val);
} else {
cpu_stl_le_mmuidx_ra(ac->env, addr, val, ac->mmu_idx, ac->ra);
}
}
void access_stq(X86Access *ac, vaddr addr, uint64_t val)
{
void *p = access_ptr(ac, addr, sizeof(uint64_t));
if (test_ptr(p)) {
stq_le_p(p, val);
} else {
cpu_stq_le_mmuidx_ra(ac->env, addr, val, ac->mmu_idx, ac->ra);
}
}
+40
View File
@@ -0,0 +1,40 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* Access guest memory in blocks. */
#ifndef X86_TCG_ACCESS_H
#define X86_TCG_ACCESS_H
/* An access covers at most sizeof(X86XSaveArea), at most 2 pages. */
typedef struct X86Access {
target_ulong vaddr;
void *haddr1;
void *haddr2;
uint16_t size;
uint16_t size1;
/*
* If we can't access the host page directly, we'll have to do I/O access
* via ld/st helpers. These are internal details, so we store the rest
* to do the access here instead of passing it around in the helpers.
*/
int mmu_idx;
CPUX86State *env;
uintptr_t ra;
} X86Access;
void access_prepare_mmu(X86Access *ret, CPUX86State *env,
vaddr vaddr, unsigned size,
MMUAccessType type, int mmu_idx, uintptr_t ra);
void access_prepare(X86Access *ret, CPUX86State *env, vaddr vaddr,
unsigned size, MMUAccessType type, uintptr_t ra);
uint8_t access_ldb(X86Access *ac, vaddr addr);
uint16_t access_ldw(X86Access *ac, vaddr addr);
uint32_t access_ldl(X86Access *ac, vaddr addr);
uint64_t access_ldq(X86Access *ac, vaddr addr);
void access_stb(X86Access *ac, vaddr addr, uint8_t val);
void access_stw(X86Access *ac, vaddr addr, uint16_t val);
void access_stl(X86Access *ac, vaddr addr, uint32_t val);
void access_stq(X86Access *ac, vaddr addr, uint64_t val);
#endif
File diff suppressed because it is too large Load Diff
+1
View File
@@ -1,4 +1,5 @@
i386_ss.add(when: 'CONFIG_TCG', if_true: files(
'access.c',
'bpt_helper.c',
'cc_helper.c',
'excp_helper.c',
+1
View File
@@ -13,6 +13,7 @@ X86_64_TESTS += vsyscall
X86_64_TESTS += noexec
X86_64_TESTS += cmpxchg
X86_64_TESTS += adox
X86_64_TESTS += test-1648
TESTS=$(MULTIARCH_TESTS) $(X86_64_TESTS) test-x86_64
else
TESTS=$(MULTIARCH_TESTS)
+33
View File
@@ -0,0 +1,33 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* See https://gitlab.com/qemu-project/qemu/-/issues/1648 */
#include <signal.h>
__attribute__((noinline))
void bar(void)
{
/* Success! Continue through sigreturn. */
}
/*
* Because of the change of ABI between foo and bar, the compiler is
* required to save XMM6-XMM15. The compiler will use MOVAPS or MOVDQA,
* which will trap if the stack frame is not 16 byte aligned.
*/
__attribute__((noinline, ms_abi))
void foo(void)
{
bar();
}
void sighandler(int num)
{
foo();
}
int main(void)
{
signal(SIGUSR1, sighandler);
raise(SIGUSR1);
return 0;
}