From patchwork Thu May 23 20:44:07 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jan Bobek X-Patchwork-Id: 165073 Delivered-To: patch@linaro.org Received: by 2002:a92:9e1a:0:0:0:0:0 with SMTP id q26csp2705037ili; Thu, 23 May 2019 13:51:31 -0700 (PDT) X-Google-Smtp-Source: APXvYqw3m2g+jkpse+Dj+0kcC05JysF2SMLGW5SxR0xe5AxE/ghn+vYjQTh2QS3u5PvIHGvuARs9 X-Received: by 2002:a50:b4b8:: with SMTP id w53mr100279019edd.255.1558644691721; Thu, 23 May 2019 13:51:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558644691; cv=none; d=google.com; s=arc-20160816; b=m9+Zm4XVUtAPFCde8Syugg/zztlE1hK0zyLLjNBs4IYGUtvrvq3AYlI0wr1NLts1Jz tWlpOt7UOLbrxBIpmK7PlWBdJFDFUDlic/8Aq30K65wsVI+iMS4Gq5dRM2+dlN4/WpkR HbYPRXv90dTYfRe06MW/ulZ5l6fcG1cec5eKZ+cqvZexf3fXQqmfcE07uLSIk1FLZGOo 02jD8xsiElTtvJglbZOyiXy9FXxNXSR1SbodUlWJYr4sGlpNKypeLUA5gzT2QFtwQ9kg uSki49sv/pyEXn6SCW+YJHjOK15+OfiNMF8K/FDJrsLCe+YBj4hHlRIHUSjMNaW3iFnB IbYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature; bh=dgGhNtzAW4e5HQFtFo/WG0Nzt03w15qZgzxKHgtzV9k=; b=hrteh4WzppXmLmCQl6yBeT75I+hH4obnQqe/TQqvU1O8TENqvG3JrJm7zj3VQaNDPT u1jlwClxGKmA+boa2kmfavvuUFC27PYphrr5lUr4xxMe7cRt4QGGGrZ1+1r+mb/iHj84 PQ9Wj3yTzVKO9mObdiL0oI21t0Aqt/c5au3pOg8biNnUP3IUBcaSQuHsI/ZQj57YTca+ D4rlsZrK2xmbObil9mGOAu60nE6NAwTbPsa1t5QMtxZrrAhQnD6bM3vLk37mjP2FgVOY DXaflTfCEXTo7jj9b2GYh511IljtaswbXpJvFuDQ5OSOiQcuHkP92Qxr053QNUgt3MIP j6qQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=afTeAsOn; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id h15si222459eda.399.2019.05.23.13.51.31 for (version=TLS1 cipher=AES128-SHA bits=128/128); Thu, 23 May 2019 13:51:31 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=afTeAsOn; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from localhost ([127.0.0.1]:43166 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hTugU-0002kI-GP for patch@linaro.org; Thu, 23 May 2019 16:51:30 -0400 Received: from eggs.gnu.org ([209.51.188.92]:37454) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hTuZz-0006CI-43 for qemu-devel@nongnu.org; Thu, 23 May 2019 16:44:48 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hTuZv-0003Fq-Ly for qemu-devel@nongnu.org; Thu, 23 May 2019 16:44:45 -0400 Received: from mail-yb1-xb43.google.com ([2607:f8b0:4864:20::b43]:33775) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hTuZv-0003Ef-FY for qemu-devel@nongnu.org; Thu, 23 May 2019 16:44:43 -0400 Received: by mail-yb1-xb43.google.com with SMTP id w127so923378yba.0 for ; Thu, 23 May 2019 13:44:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=dgGhNtzAW4e5HQFtFo/WG0Nzt03w15qZgzxKHgtzV9k=; b=afTeAsOng5t/U9KgXwNn8SNdgxrlcz32rmOoAY5Rw7sypF3FK6Vh837DkrxnxV9JES g1TZBHxj1n3gSWL5XLtLyRzJclK865j8wnJhMvMn4K+udFufJ6haYeGbszLt6o0Qlpps 245sqEaw7kNJ3GOAegxGFekNmrGl2b31lxJzPmcpmR89sqxZwtQ8a4HWDcxFaZj/iWBa CwTPJR5iWF9pXEz2wv+uZmqZ37WVvQj2KlMdAk2DjQx83Yf8HZejhtmCa2Gap1EBNY3y kuM/nUJfhJXxWMVABKd+4DdRl31+P2gRK1Dp8ZLHLaG2ViLkOTf6kryYjtN/wnKYKjgs Nfaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=dgGhNtzAW4e5HQFtFo/WG0Nzt03w15qZgzxKHgtzV9k=; b=LA3DfYDv7h2oNxaqk1TqY79l/QRqz0tDG4sJKbnXEB01FVlxXb8BTHQydqJHGG+KSs p4H/nDn7i/x8MA/CreLHoEnrAAhKpOMORr1xrR/Va8hN0Hz8Ude+31B31S3hxBKyJ/rm skT0uugqOsbwRxUunrJrl5NsM3AO4Inp/CsH5pLf73Vo7p7NZfIagF9Vfatioe0dUVJR ctpkFWlI8MZzJ9T75ZWfdG6FQ4avCQggZfxWPb3WjN5YIJM2sul3iSu5zbaCZFqM6gpj zz1HqeZLilhzAi+CnmQKVkbDr+fsPFyI8QeAFf2ARhR8wmeb/Ntl/kzJMmIiQc76zOmW 5YMQ== X-Gm-Message-State: APjAAAVriKZ7RbAot9zCRxyLf9K8GLvYDlgf4g06UhzvLf12fpzX/Xx/ Gea+QkIaPOUeIbtnseuQ/awn0WmK X-Received: by 2002:a25:2487:: with SMTP id k129mr5834238ybk.91.1558644282316; Thu, 23 May 2019 13:44:42 -0700 (PDT) Received: from dionysus.attlocal.net (69-222-133-165.lightspeed.tukrga.sbcglobal.net. [69.222.133.165]) by smtp.gmail.com with ESMTPSA id p12sm87590ywg.72.2019.05.23.13.44.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 23 May 2019 13:44:41 -0700 (PDT) From: Jan Bobek To: qemu-devel@nongnu.org Date: Thu, 23 May 2019 16:44:07 -0400 Message-Id: <20190523204409.21068-10-jan.bobek@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190523204409.21068-1-jan.bobek@gmail.com> References: <20190523204409.21068-1-jan.bobek@gmail.com> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::b43 Subject: [Qemu-devel] [RISU v3 09/11] i386: Add avx512 state to reginfo_t X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?utf-8?q?Alex_Benn=C3=A9e?= , Richard Henderson Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" From: Richard Henderson The state expected for a given test must be specifically requested with the --xfeatures=mask command-line argument. This is recorded with the saved state so that it is obvious if the apprentice is given a different argument. Any features beyond what are present on the running cpu will read as zero. Signed-off-by: Richard Henderson --- risu_reginfo_i386.h | 14 +++ risu_reginfo_i386.c | 228 ++++++++++++++++++++++++++++++++++++++++++-- test_i386.S | 39 ++++++++ 3 files changed, 273 insertions(+), 8 deletions(-) -- 2.20.1 Reviewed-by: Alex Bennée diff --git a/risu_reginfo_i386.h b/risu_reginfo_i386.h index e350f01..b468f79 100644 --- a/risu_reginfo_i386.h +++ b/risu_reginfo_i386.h @@ -12,6 +12,10 @@ #ifndef RISU_REGINFO_I386_H #define RISU_REGINFO_I386_H +struct avx512_reg { + uint64_t q[8]; +}; + /* * This is the data structure we pass over the socket. * It is a simplified and reduced subset of what can @@ -19,7 +23,17 @@ */ struct reginfo { uint32_t faulting_insn; + uint32_t mxcsr; + uint64_t xfeatures; + gregset_t gregs; + +#ifdef __x86_64__ + struct avx512_reg vregs[32]; +#else + struct avx512_reg vregs[8]; +#endif + uint64_t kregs[8]; }; /* diff --git a/risu_reginfo_i386.c b/risu_reginfo_i386.c index c4dc14a..83f9541 100644 --- a/risu_reginfo_i386.c +++ b/risu_reginfo_i386.c @@ -11,19 +11,32 @@ #include #include +#include #include #include #include +#include #include "risu.h" #include "risu_reginfo_i386.h" -const struct option * const arch_long_opts; -const char * const arch_extra_help; +#include + +static uint64_t xfeatures = 3; /* SSE */ + +static const struct option extra_ops[] = { + {"xfeatures", required_argument, NULL, FIRST_ARCH_OPT }, + {0, 0, 0, 0} +}; + +const struct option * const arch_long_opts = extra_ops; +const char * const arch_extra_help + = " --xfeatures= Use features in mask for XSAVE\n"; void process_arch_opt(int opt, const char *arg) { - abort(); + assert(opt == FIRST_ARCH_OPT); + xfeatures = strtoull(arg, 0, 0); } const int reginfo_size(void) @@ -31,13 +44,37 @@ const int reginfo_size(void) return sizeof(struct reginfo); } +static void *xsave_feature_buf(struct _xstate *xs, int feature) +{ + unsigned int eax, ebx, ecx, edx; + int ok; + + /* + * Get the location of the XSAVE feature from the cpuid leaf. + * Given that we know the xfeature bit is set, this must succeed. + */ + ok = __get_cpuid_count(0xd, feature, &eax, &ebx, &ecx, &edx); + assert(ok); + + /* Sanity check that the frame stored by the kernel contains the data. */ + assert(xs->fpstate.sw_reserved.extended_size >= eax + ebx); + + return (void *)xs + ebx; +} + /* reginfo_init: initialize with a ucontext */ void reginfo_init(struct reginfo *ri, ucontext_t *uc) { - int i; + int i, nvecregs; + struct _fpstate *fp; + struct _xstate *xs; + uint64_t features; memset(ri, 0, sizeof(*ri)); + /* Require master and apprentice to be given the same arguments. */ + ri->xfeatures = xfeatures; + for (i = 0; i < NGREG; i++) { switch (i) { case REG_E(IP): @@ -79,12 +116,89 @@ void reginfo_init(struct reginfo *ri, ucontext_t *uc) * distinguish 'do compare' from 'stop'. */ ri->faulting_insn = *(uint32_t *)uc->uc_mcontext.gregs[REG_E(IP)]; + + /* + * FP state is omitted if unused (aka in init state). + * Use the struct for access to AVX state. + */ + + fp = (struct _fpstate *)uc->uc_mcontext.fpregs; + if (fp == NULL) { + return; + } + +#ifdef __x86_64__ + nvecregs = 16; +#else + /* We don't (currently) care about the 80387 state, only SSE+. */ + if (fp->magic != X86_FXSR_MAGIC) { + return; + } + nvecregs = 8; +#endif + + /* + * Now we know that _fpstate contains FXSAVE data. + */ + ri->mxcsr = fp->mxcsr; + + for (i = 0; i < nvecregs; ++i) { +#ifdef __x86_64__ + memcpy(&ri->vregs[i], &fp->xmm_space[i * 4], 16); +#else + memcpy(&ri->vregs[i], &fp->_xmm[i], 16); +#endif + } + + if (fp->sw_reserved.magic1 != FP_XSTATE_MAGIC1) { + return; + } + xs = (struct _xstate *)fp; + features = xfeatures & xs->xstate_hdr.xfeatures; + + /* + * Now we know that _fpstate contains XSAVE data. + */ + + if (features & (1 << 2)) { + /* YMM_Hi128 state */ + void *buf = xsave_feature_buf(xs, 2); + for (i = 0; i < nvecregs; ++i) { + memcpy(&ri->vregs[i].q[2], buf + 16 * i, 16); + } + } + + if (features & (1 << 5)) { + /* Opmask state */ + uint64_t *buf = xsave_feature_buf(xs, 5); + for (i = 0; i < 8; ++i) { + ri->kregs[i] = buf[i]; + } + } + + if (features & (1 << 6)) { + /* ZMM_Hi256 state */ + void *buf = xsave_feature_buf(xs, 6); + for (i = 0; i < nvecregs; ++i) { + memcpy(&ri->vregs[i].q[4], buf + 32 * i, 32); + } + } + +#ifdef __x86_64__ + if (features & (1 << 7)) { + /* Hi16_ZMM state */ + void *buf = xsave_feature_buf(xs, 7); + for (i = 0; i < 16; ++i) { + memcpy(&ri->vregs[i + 16], buf + 64 * i, 64); + } + } +#endif } /* reginfo_is_eq: compare the reginfo structs, returns nonzero if equal */ int reginfo_is_eq(struct reginfo *m, struct reginfo *a) { - return 0 == memcmp(m, a, sizeof(*m)); + return !memcmp(m, a, sizeof(*m)); } static const char *const regname[NGREG] = { @@ -126,28 +240,126 @@ static const char *const regname[NGREG] = { # define PRIxREG "%08x" #endif +static int get_nvecregs(uint64_t features) +{ +#ifdef __x86_64__ + return features & (1 << 7) ? 32 : 16; +#else + return 8; +#endif +} + +static int get_nvecquads(uint64_t features) +{ + if (features & (1 << 6)) { + return 8; + } else if (features & (1 << 2)) { + return 4; + } else { + return 2; + } +} + +static char get_vecletter(uint64_t features) +{ + if (features & (1 << 6 | 1 << 7)) { + return 'z'; + } else if (features & (1 << 2)) { + return 'y'; + } else { + return 'x'; + } +} + /* reginfo_dump: print state to a stream, returns nonzero on success */ int reginfo_dump(struct reginfo *ri, FILE *f) { - int i; + uint64_t features; + int i, j, n, w; + char r; + fprintf(f, " faulting insn %x\n", ri->faulting_insn); for (i = 0; i < NGREG; i++) { if (regname[i]) { fprintf(f, " %-6s: " PRIxREG "\n", regname[i], ri->gregs[i]); } } + + fprintf(f, " mxcsr : %x\n", ri->mxcsr); + fprintf(f, " xfeat : %" PRIx64 "\n", ri->xfeatures); + + features = ri->xfeatures; + n = get_nvecregs(features); + w = get_nvecquads(features); + r = get_vecletter(features); + + for (i = 0; i < n; i++) { + fprintf(f, " %cmm%-3d: ", r, i); + for (j = w - 1; j >= 0; j--) { + fprintf(f, "%016" PRIx64 "%c", + ri->vregs[i].q[j], j == 0 ? '\n' : ' '); + } + } + + if (features & (1 << 5)) { + for (i = 0; i < 8; i++) { + fprintf(f, " k%-5d: %016" PRIx64 "\n", i, ri->kregs[i]); + } + } + return !ferror(f); } int reginfo_dump_mismatch(struct reginfo *m, struct reginfo *a, FILE *f) { - int i; + int i, j, n, w; + uint64_t features; + char r; + + fprintf(f, "Mismatch (master v apprentice):\n"); + for (i = 0; i < NGREG; i++) { if (m->gregs[i] != a->gregs[i]) { assert(regname[i]); - fprintf(f, "Mismatch: %s: " PRIxREG " v " PRIxREG "\n", + fprintf(f, " %-6s: " PRIxREG " v " PRIxREG "\n", regname[i], m->gregs[i], a->gregs[i]); } } + + if (m->mxcsr != a->mxcsr) { + fprintf(f, " mxcsr : %x v %x\n", m->mxcsr, a->mxcsr); + } + if (m->xfeatures != a->xfeatures) { + fprintf(f, " xfeat : %" PRIx64 " v %" PRIx64 "\n", + m->xfeatures, a->xfeatures); + } + + features = m->xfeatures; + n = get_nvecregs(features); + w = get_nvecquads(features); + r = get_vecletter(features); + + for (i = 0; i < n; i++) { + if (memcmp(&m->vregs[i], &a->vregs[i], w * 8)) { + fprintf(f, " %cmm%-3d: ", r, i); + for (j = w - 1; j >= 0; j--) { + fprintf(f, "%016" PRIx64 "%c", + m->vregs[i].q[j], j == 0 ? '\n' : ' '); + } + fprintf(f, " v: "); + for (j = w - 1; j >= 0; j--) { + fprintf(f, "%016" PRIx64 "%c", + a->vregs[i].q[j], j == 0 ? '\n' : ' '); + } + } + } + + for (i = 0; i < 8; i++) { + if (m->kregs[i] != a->kregs[i]) { + fprintf(f, " k%-5d: %016" PRIx64 " v %016" PRIx64 "\n", + i, m->kregs[i], a->kregs[i]); + } + } + return !ferror(f); } diff --git a/test_i386.S b/test_i386.S index 456b99c..05344d7 100644 --- a/test_i386.S +++ b/test_i386.S @@ -12,6 +12,37 @@ /* A trivial test image for x86 */ /* Initialise the registers to avoid spurious mismatches */ + +#ifdef __x86_64__ +#define BASE %rax + lea 2f(%rip), BASE +#else +#define BASE %eax + call 1f +1: pop BASE + add $2f-1b, BASE +#endif + + movdqa 0(BASE), %xmm0 + movdqa 1*16(BASE), %xmm1 + movdqa 2*16(BASE), %xmm2 + movdqa 3*16(BASE), %xmm3 + movdqa 4*16(BASE), %xmm4 + movdqa 5*16(BASE), %xmm5 + movdqa 6*16(BASE), %xmm6 + movdqa 7*16(BASE), %xmm7 + +#ifdef __x86_64__ + movdqa 8*16(BASE), %xmm8 + movdqa 9*16(BASE), %xmm9 + movdqa 10*16(BASE), %xmm10 + movdqa 11*16(BASE), %xmm11 + movdqa 12*16(BASE), %xmm12 + movdqa 13*16(BASE), %xmm13 + movdqa 14*16(BASE), %xmm14 + movdqa 15*16(BASE), %xmm15 +#endif + xor %eax, %eax sahf /* init eflags */ @@ -39,3 +70,11 @@ /* exit test */ ud1 %ecx, %eax + + .p2align 16 +2: + .set i, 0 + .rept 256 + .byte i + .set i, i + 1 + .endr