From patchwork Wed Apr 21 23:12:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacob Xu X-Patchwork-Id: 12217145 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 56FFFC433B4 for ; Wed, 21 Apr 2021 23:13:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2E60161404 for ; Wed, 21 Apr 2021 23:13:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343678AbhDUXNr (ORCPT ); Wed, 21 Apr 2021 19:13:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60552 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244043AbhDUXNq (ORCPT ); Wed, 21 Apr 2021 19:13:46 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 378D5C06174A for ; Wed, 21 Apr 2021 16:13:11 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id c1-20020a5b0bc10000b02904e7c6399b20so17717471ybr.12 for ; Wed, 21 Apr 2021 16:13:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=c4DrM/LT5tBLj73VSLr9DqeLdFK8eZgbdj+R4jNuKjY=; b=jgc2UocMpPlGp2kfA6ChPksCj+wwHmHp3iEYMnY8C2SnStnb5WCsrGpBydK2xKNSq7 HEEmM80db+j2jxK7cyKx7ERmMrGsVehI8kUy0+UYzYmfAq7l65AJVCqfWGMvS6plxkfj w4QUl3ZKnxZ1GjlzF9ywldGXkT8f+Zmp95g0SUmOBaMGIzQUBpHQb1ijOkAvGHblQnzm PkejpsGcF4oqu+KyV/5UnQfIlb7H/sle26gjcFWzMMWoBrf/cKn6OQcSnGUZD0n6uEbL Kpg1Xeg+5Q+8aKLYBGZBY+APOI2z3QXTnLZDqFXfAAyDeMGCBXh00BsTYNcK6c4HJfC3 lrYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=c4DrM/LT5tBLj73VSLr9DqeLdFK8eZgbdj+R4jNuKjY=; b=te4jqXWlKG6L89bp4b8DfQTSBWueo9E6uk3safZ66MiK1qjGmdpDgaJVIgYJ9yjvUy wpCyqEU6Ao3aTPSuJ34+DBzDAbbgLY3BwmZWKLIbS/FD7ZPKXAzRSx08wY1PHBb3zb/K rZQDB6pjbSpeuQxzVkIWw9H9/Y29CIkdHu4Q512p8jj/lHLGoPuPk1HGOZvJnbxE3NP8 FCgJhByU7CEbjkTXIYXQNL63tYkVqJq4u5ZrnkZTmaPa2V5hnM8MXcR++3XIwbyIzAkZ MRVgK8RSMb5o+oXqrkYuoDoKGedraGWGSj83zkFpHKPnp+MZjBvplLsq29ThqH7BcRLq +Zvw== X-Gm-Message-State: AOAM530I9AiAS1lGqlDAfc2w6r7HT3nTpDxuWLxsm3MpikCNw0oybgjr U/5uL1SgxnUOLpcdsRCSQeM4GSa9OPN/Hg== X-Google-Smtp-Source: ABdhPJyrLYl2A/eXtMDi+Rb8poyahq1CpFQZkXLGakYoV2bGpySxhRJrrSOzEhvdqWqfGtW1DMm6ef5OH/WVGQ== X-Received: from mhmmm.sea.corp.google.com ([2620:15c:100:202:c919:a1bb:1eab:984c]) (user=jacobhxu job=sendgmr) by 2002:a5b:448:: with SMTP id s8mr523404ybp.363.1619046790504; Wed, 21 Apr 2021 16:13:10 -0700 (PDT) Date: Wed, 21 Apr 2021 16:12:57 -0700 Message-Id: <20210421231258.2583654-1-jacobhxu@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.31.1.368.gbe11c130af-goog Subject: [kvm-unit-tests PATCH 1/2] x86: add movups/movupd sse testcases to emulator.c From: Jacob Xu To: Paolo Bonzini , Jim Mattson Cc: kvm@vger.kernel.org, Jacob Xu Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Here we add movups/movupd tests corresponding to functionality introduced in commit 29916968c486 ("kvm: Add emulation for movups/movupd"). Signed-off-by: Jacob Xu --- x86/emulator.c | 44 ++++++++++++++++++++------------------------ 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/x86/emulator.c b/x86/emulator.c index 6100b6d..ff3c2bf 100644 --- a/x86/emulator.c +++ b/x86/emulator.c @@ -664,30 +664,26 @@ static bool sseeq(sse_union *v1, sse_union *v2) static __attribute__((target("sse2"))) void test_sse(sse_union *mem) { - sse_union v; - - write_cr0(read_cr0() & ~6); /* EM, TS */ - write_cr4(read_cr4() | 0x200); /* OSFXSR */ - v.u[0] = 1; v.u[1] = 2; v.u[2] = 3; v.u[3] = 4; - asm("movdqu %1, %0" : "=m"(*mem) : "x"(v.sse)); - report(sseeq(&v, mem), "movdqu (read)"); - mem->u[0] = 5; mem->u[1] = 6; mem->u[2] = 7; mem->u[3] = 8; - asm("movdqu %1, %0" : "=x"(v.sse) : "m"(*mem)); - report(sseeq(mem, &v), "movdqu (write)"); - - v.u[0] = 1; v.u[1] = 2; v.u[2] = 3; v.u[3] = 4; - asm("movaps %1, %0" : "=m"(*mem) : "x"(v.sse)); - report(sseeq(mem, &v), "movaps (read)"); - mem->u[0] = 5; mem->u[1] = 6; mem->u[2] = 7; mem->u[3] = 8; - asm("movaps %1, %0" : "=x"(v.sse) : "m"(*mem)); - report(sseeq(&v, mem), "movaps (write)"); - - v.u[0] = 1; v.u[1] = 2; v.u[2] = 3; v.u[3] = 4; - asm("movapd %1, %0" : "=m"(*mem) : "x"(v.sse)); - report(sseeq(mem, &v), "movapd (read)"); - mem->u[0] = 5; mem->u[1] = 6; mem->u[2] = 7; mem->u[3] = 8; - asm("movapd %1, %0" : "=x"(v.sse) : "m"(*mem)); - report(sseeq(&v, mem), "movapd (write)"); + sse_union v; + + write_cr0(read_cr0() & ~6); /* EM, TS */ + write_cr4(read_cr4() | 0x200); /* OSFXSR */ + +#define TEST_RW_SSE(insn) do { \ + v.u[0] = 1; v.u[1] = 2; v.u[2] = 3; v.u[3] = 4; \ + asm(insn " %1, %0" : "=m"(*mem) : "x"(v.sse)); \ + report(sseeq(&v, mem), insn " (read)"); \ + mem->u[0] = 5; mem->u[1] = 6; mem->u[2] = 7; mem->u[3] = 8; \ + asm(insn " %1, %0" : "=x"(v.sse) : "m"(*mem)); \ + report(sseeq(&v, mem), insn " (write)"); \ +} while (0) + + TEST_RW_SSE("movdqu"); + TEST_RW_SSE("movaps"); + TEST_RW_SSE("movapd"); + TEST_RW_SSE("movups"); + TEST_RW_SSE("movupd"); +#undef TEST_RW_SSE } static void test_mmx(uint64_t *mem) From patchwork Wed Apr 21 23:12:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacob Xu X-Patchwork-Id: 12217147 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 17500C433ED for ; Wed, 21 Apr 2021 23:13:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E656561409 for ; Wed, 21 Apr 2021 23:13:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343681AbhDUXNs (ORCPT ); Wed, 21 Apr 2021 19:13:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60554 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244043AbhDUXNr (ORCPT ); Wed, 21 Apr 2021 19:13:47 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F3DEEC06174A for ; Wed, 21 Apr 2021 16:13:13 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id e8-20020a2587480000b02904e5857564e2so17716242ybn.16 for ; Wed, 21 Apr 2021 16:13:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=uQidMMizh5NxKxSXlbJhlcp7tcm0hjWSy8HrT3lIpeE=; b=INJSifMcwnPsWZPNI/jT7ZAvRTZ/tfkmKOZn7uW3oGpNfvCLx22ybu2zkNQLgJIUzM Omk9eJyr8vyptqzA7/QKTM+HwwSvMbxYyKPsOnnuxmLUGic4hKQd9aoenkrfZAygzok/ s9ycGjbi/yCd3yXGDcHPTp0Hj/fBqVfkNoX9J8L6P1H+ASpQqmKBnbGDXz95EMWSByK6 X2XOIT6oNUrQR6TEnZdWJKogSoKgj4jfo4TwAuPx9m9Y7nxWFc00Kp8b+Tpw0kluD/Zy giPWtJJTqLgqokrCbBeRHbumVspqsHPBpEWTeohO8rdNx94mx6T2LscYOwymfzeFfNbx ktHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=uQidMMizh5NxKxSXlbJhlcp7tcm0hjWSy8HrT3lIpeE=; b=pPhthyMkweArCla0SpjJQsgPjIIH49As2JRTZCbaDLOvAPa0onKPH2/iw/NYSo9OZA dDepQKZ8ZTDdwErdbiJF3yy/oqoTTezWXVeIP6IipxXE29QfrFyegyAiHzKeaD/9M8v7 goKZ+LZNjInZRZsGGedRTSgRZXAfpRcRHpjihaya9bW2LCzTDBbOanvult44SMmS7SzG K1v9ZdBlLq4xC3znZtL1tflRhDiH+gPgdZKx+pTrgJ/IgoJMxdoQUMTOHHfaOeMKwRTp tOgvbF8KkYGoNZRiP95f3FIWlls44c3OLeYwnud9B+iGuDun2Y21pEeOCrZHQhPctL0v 9/Kg== X-Gm-Message-State: AOAM532vzZ/0seoCXu73oyddIkHlSPWHfTQj6WtinODkgMYz+LNdmoWA bte+N5sWcpAzzZxbRR5FVkRWTOoCjGWMCw== X-Google-Smtp-Source: ABdhPJwp8QLbrOw45m8Hx7ZOEraS54lylwCoysIY9ItTeVzfCbg4V8NWALr5CSBa4sk1y24FlAE50HTh7PClnA== X-Received: from mhmmm.sea.corp.google.com ([2620:15c:100:202:c919:a1bb:1eab:984c]) (user=jacobhxu job=sendgmr) by 2002:a25:da4a:: with SMTP id n71mr525076ybf.351.1619046793271; Wed, 21 Apr 2021 16:13:13 -0700 (PDT) Date: Wed, 21 Apr 2021 16:12:58 -0700 In-Reply-To: <20210421231258.2583654-1-jacobhxu@google.com> Message-Id: <20210421231258.2583654-2-jacobhxu@google.com> Mime-Version: 1.0 References: <20210421231258.2583654-1-jacobhxu@google.com> X-Mailer: git-send-email 2.31.1.368.gbe11c130af-goog Subject: [kvm-unit-tests PATCH 2/2] x86: add additional test cases for sse exceptions to emulator.c From: Jacob Xu To: Paolo Bonzini , Jim Mattson Cc: kvm@vger.kernel.org, Jacob Xu Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Add additional test cases for sse instructions for doing unaligned accesses and accesses that cross page boundaries. Signed-off-by: Jacob Xu --- x86/emulator.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/x86/emulator.c b/x86/emulator.c index ff3c2bf..9705073 100644 --- a/x86/emulator.c +++ b/x86/emulator.c @@ -686,6 +686,75 @@ static __attribute__((target("sse2"))) void test_sse(sse_union *mem) #undef TEST_RW_SSE } +static void unaligned_movaps_handler(struct ex_regs *regs) +{ + extern char unaligned_movaps_cont; + + ++exceptions; + regs->rip = (ulong)&unaligned_movaps_cont; +} + +static void cross_movups_handler(struct ex_regs *regs) +{ + extern char cross_movups_cont; + + ++exceptions; + regs->rip = (ulong)&cross_movups_cont; +} + +static __attribute__((target("sse2"))) void test_sse_exceptions(void *cross_mem) +{ + sse_union v; + sse_union *mem; + uint8_t *bytes = cross_mem; // aligned on PAGE_SIZE*2 + void *page2 = (void *)(&bytes[4096]); + struct pte_search search; + pteval_t orig_pte; + + // setup memory for unaligned access + mem = (sse_union *)(&bytes[8]); + + // test unaligned access for movups, movupd and movaps + v.u[0] = 1; v.u[1] = 2; v.u[2] = 3; v.u[3] = 4; + mem->u[0] = 5; mem->u[1] = 6; mem->u[2] = 7; mem->u[3] = 8; + asm("movups %1, %0" : "=m"(*mem) : "x"(v.sse)); + report(sseeq(&v, mem), "movups unaligned"); + + v.u[0] = 1; v.u[1] = 2; v.u[2] = 3; v.u[3] = 4; + mem->u[0] = 5; mem->u[1] = 6; mem->u[2] = 7; mem->u[3] = 8; + asm("movupd %1, %0" : "=m"(*mem) : "x"(v.sse)); + report(sseeq(&v, mem), "movupd unaligned"); + exceptions = 0; + handle_exception(GP_VECTOR, unaligned_movaps_handler); + asm("movaps %1, %0\n\t unaligned_movaps_cont:" + : "=m"(*mem) : "x"(v.sse)); + handle_exception(GP_VECTOR, 0); + report(exceptions == 1, "unaligned movaps exception"); + + // setup memory for cross page access + mem = (sse_union *)(&bytes[4096-8]); + v.u[0] = 1; v.u[1] = 2; v.u[2] = 3; v.u[3] = 4; + mem->u[0] = 5; mem->u[1] = 6; mem->u[2] = 7; mem->u[3] = 8; + + asm("movups %1, %0" : "=m"(*mem) : "x"(v.sse)); + report(sseeq(&v, mem), "movups unaligned crosspage"); + + // invalidate second page + search = find_pte_level(current_page_table(), page2, 1); + orig_pte = *search.pte; + install_pte(current_page_table(), 1, page2, 0, NULL); + invlpg(page2); + + exceptions = 0; + handle_exception(PF_VECTOR, cross_movups_handler); + asm("movups %1, %0\n\t cross_movups_cont:" : "=m"(*mem) : "x"(v.sse)); + handle_exception(PF_VECTOR, 0); + report(exceptions == 1, "movups crosspage exception"); + + // restore invalidated page + install_pte(current_page_table(), 1, page2, orig_pte, NULL); +} + static void test_mmx(uint64_t *mem) { uint64_t v; @@ -1057,6 +1126,7 @@ int main(void) void *mem; void *insn_page; void *insn_ram; + void *cross_mem; unsigned long t1, t2; setup_vm(); @@ -1070,6 +1140,7 @@ int main(void) install_page((void *)read_cr3(), IORAM_BASE_PHYS, mem + 4096); insn_page = alloc_page(); insn_ram = vmap(virt_to_phys(insn_page), 4096); + cross_mem = vmap(virt_to_phys(alloc_pages(2)), 2 * PAGE_SIZE); // test mov reg, r/m and mov r/m, reg t1 = 0x123456789abcdef; @@ -1102,6 +1173,7 @@ int main(void) test_imul(mem); test_muldiv(mem); test_sse(mem); + test_sse_exceptions(cross_mem); test_mmx(mem); test_rip_relative(mem, insn_ram); test_shld_shrd(mem);