From patchwork Tue Jan 25 20:31:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Lewis X-Patchwork-Id: 12724224 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 435BDC433F5 for ; Tue, 25 Jan 2022 20:31:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232078AbiAYUbp (ORCPT ); Tue, 25 Jan 2022 15:31:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37010 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232072AbiAYUbo (ORCPT ); Tue, 25 Jan 2022 15:31:44 -0500 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 70BC0C06173B for ; Tue, 25 Jan 2022 12:31:44 -0800 (PST) Received: by mail-pg1-x54a.google.com with SMTP id k13-20020a65434d000000b00342d8eb46b4so12331745pgq.23 for ; Tue, 25 Jan 2022 12:31:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=wkxsBL5x1pNRUbZ9MZHRKA3ddUsleEy1gx0hvtCSCA8=; b=Yeg9Xl5M94Mus86eYJ0AhQLtm+oQv4DurYsM+hLqOCI+rIJwbIlssxIs6k3nFG/gWC dnveMeuv9DSts6YAzamvqsetjWFO1E+Om1vKIJV3Cx9dEZvAO2guO2CCP+6tydG21xwa 7WsFpD2iPCzKVMsO1dJt8ZkadzcfofDN4w8SOro+NJViYTMziGCSRvcRCxZUwhu5BPv/ 0dhjcDXrpzZyiFuKzRqLh2jgq7tz9EGd2HHLjPxpoXvDcX18lwwzh/sYZ5LG8uYVEEeJ XoGWTz9kWnDk50ZLC371uW0Hw8+c3c9P2TgP3LP97YzZgQemXq5cxbYXMSc7AGLDn3aw L1cQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=wkxsBL5x1pNRUbZ9MZHRKA3ddUsleEy1gx0hvtCSCA8=; b=L753iRXGAurzFJC1Dk3rXVmbcnhQCm1umGe7BSSa6bziqspCqP8G8dT5W/WQJOHgfh IBK45TRu+VHHTZyWkkOzHBew7zUEN66dc9uSb6bHXVqd62HXO+SvrjBZDDWBSGE4I5ax 3U+pNoMvZEB1ZNuxI9nPiMHx1fMjb/R4KxnDHoRNEWQiEHq+enCApMhh6i3rzDucufem JeK+7icDBbXgHcP0hCp1Hgiv+enPsK7hVnv7MqHUalrK/Zt47nLMNB/3gMIOMOUswsao X6Tjhe6PkWS8gTYdcSgXN2qC2Mu31MmA0cub+q/+EECqWrecFimURgLufN4szTnmk7Ms RIpQ== X-Gm-Message-State: AOAM533oZ5L4eRvJw3fdaVvzLGYoaH91eyEiQ2+tIKj8l8wGFC5EVQb8 lOYExOL8Aah8ev1y8A1z5CsH4abKpSEQ/J2i/5KqpGa1T94A88H64w66veglLolZ8a/JC2Zv+ku WATV+yk/dFRgjqKBP/pBMBcE0WNihGDu/OYbajkMY+jWGa04E7djSi/NzZUaXDQs5121a X-Google-Smtp-Source: ABdhPJz8dhJbzY8Ovi1pKs+jwWRMC+cKcRppcvd+ziBjZisYVmAghFFzEWqrwCyrPnLsSZJ7gCybfo8iRUJiNZrw X-Received: from aaronlewis.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2675]) (user=aaronlewis job=sendgmr) by 2002:a05:6a00:1486:b0:4c8:ad4f:6b24 with SMTP id v6-20020a056a00148600b004c8ad4f6b24mr12619788pfu.16.1643142703856; Tue, 25 Jan 2022 12:31:43 -0800 (PST) Date: Tue, 25 Jan 2022 20:31:24 +0000 In-Reply-To: <20220125203127.1161838-1-aaronlewis@google.com> Message-Id: <20220125203127.1161838-2-aaronlewis@google.com> Mime-Version: 1.0 References: <20220125203127.1161838-1-aaronlewis@google.com> X-Mailer: git-send-email 2.35.0.rc0.227.g00780c9af4-goog Subject: [kvm-unit-tests PATCH v5 1/4] x86: Make exception_mnemonic() visible to the tests From: Aaron Lewis To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, jmattson@google.com, seanjc@google.com, Aaron Lewis Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org exception_mnemonic() is a useful function for more than just desc.c. Make it global, so it can be used in other KUT tests. Signed-off-by: Aaron Lewis Reviewed-by: Sean Christopherson --- lib/x86/desc.c | 2 +- lib/x86/desc.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/x86/desc.c b/lib/x86/desc.c index 16b7256..c2eb16e 100644 --- a/lib/x86/desc.c +++ b/lib/x86/desc.c @@ -91,7 +91,7 @@ struct ex_record { extern struct ex_record exception_table_start, exception_table_end; -static const char* exception_mnemonic(int vector) +const char* exception_mnemonic(int vector) { switch(vector) { case 0: return "#DE"; diff --git a/lib/x86/desc.h b/lib/x86/desc.h index 9b81da0..ad6277b 100644 --- a/lib/x86/desc.h +++ b/lib/x86/desc.h @@ -224,6 +224,7 @@ void set_intr_alt_stack(int e, void *fn); void print_current_tss_info(void); handler handle_exception(u8 v, handler fn); void unhandled_exception(struct ex_regs *regs, bool cpu); +const char* exception_mnemonic(int vector); bool test_for_exception(unsigned int ex, void (*trigger_func)(void *data), void *data); From patchwork Tue Jan 25 20:31:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Lewis X-Patchwork-Id: 12724226 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5B657C433F5 for ; Tue, 25 Jan 2022 20:31:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232086AbiAYUb4 (ORCPT ); Tue, 25 Jan 2022 15:31:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37060 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232090AbiAYUbz (ORCPT ); Tue, 25 Jan 2022 15:31:55 -0500 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A4E45C061744 for ; Tue, 25 Jan 2022 12:31:55 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id p7-20020a1709026b8700b0014a8d8fbf6fso5641174plk.23 for ; Tue, 25 Jan 2022 12:31:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=IraJ+i0C01TB7vYRSJOE9wmwStAuSFPD4ji/HUmRIn4=; b=PPEax5fHwKKcg6doh6wJCzEPrJcky9CqzawWtGLeUso61+IHvtMGxzLaybH1mDr/pK uEUPtsT/AAItFnz/1UkcPHBAalowREZ9fW9DuhIIM/dLKiAnv7UR4OBLM5riPan8NeYy HXZKyj8TEDw7W9TUERMGFcN0dxRC4gsTjNdUUAhteUG5JBGTwLFcA7kBaOLDroBwrxhI XHL/znW9IdhPY6BwypV3Z+93tXeCXtwBP4uE+bgDnVFxpmME6KevoMwgNMV3NtXfdd2w 8aKYO3fd1lwzhWst/yB8iXRqzmsKSPGjIA6YNGD9uE8yoFigfz7670EGK2XxiGi46+Tv fWiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=IraJ+i0C01TB7vYRSJOE9wmwStAuSFPD4ji/HUmRIn4=; b=O6ioJddiRfNIZcXit+xS6g5z0NUoFr3D5kplqcI2nB70m3jgAlTJYO9u7ivxZcGNAc o5hxKmdBi1lGRYXBrCmf6mlOjzgh8ANILaqaO3NSpdEGPIIh47SM42D46FDvn/IK1vWt 1/hJAZNg+aoT05gTPmPp6MqEO+vtM3tIxaOQmaduV8dEcfQxfvlz/q/jf5oMfnK5ho0x nUMRz4IgfHPk67PXS1oQx2Ld0jEYSb6NcrfCpqfBVpvueI+Uchx8pVXCZEYV326Ie0pK luGbJQDsI3clp1zMQgAiLl/KHSrE0xhp52sutB1UGPzS+uP9Imk06XobPVy0ZtgaSsY7 l4bA== X-Gm-Message-State: AOAM533EVHuBliX06THoT4xSW/Zt9fMcfyGc9CQwVAaVN0vAJwHF9qeG jo2qubACFk7XasEo1K9TPqZ3L4reM+fQDStJa/Ht8Bb8CoJJxKWRHQgp7acFI43TeHeL3bRzeNx Y1RQ9uElxdakpDdd+0HPm9pGXopnObaVt4apSroELFvF4KzkDp1dL5OYRWdVmhi+Gcfy3 X-Google-Smtp-Source: ABdhPJxLFmEGMReYxV0O9hgoScELh1L/xPGQT/Gy8dXjHbdaCAofOh1B2Aa3dplpycMRnKU7LwqeIJB8GDupcauX X-Received: from aaronlewis.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2675]) (user=aaronlewis job=sendgmr) by 2002:a17:902:9883:b0:14a:d16e:b480 with SMTP id s3-20020a170902988300b0014ad16eb480mr20304988plp.21.1643142714987; Tue, 25 Jan 2022 12:31:54 -0800 (PST) Date: Tue, 25 Jan 2022 20:31:25 +0000 In-Reply-To: <20220125203127.1161838-1-aaronlewis@google.com> Message-Id: <20220125203127.1161838-3-aaronlewis@google.com> Mime-Version: 1.0 References: <20220125203127.1161838-1-aaronlewis@google.com> X-Mailer: git-send-email 2.35.0.rc0.227.g00780c9af4-goog Subject: [kvm-unit-tests PATCH v5 2/4] x86: Add support for running a nested guest multiple times in one test From: Aaron Lewis To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, jmattson@google.com, seanjc@google.com, Aaron Lewis Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org KUT has a limit of only being able to run one nested guest per vmx test. This is limiting and not necessary. Add support for allowing a test to run guest code multiple times. Signed-off-by: Aaron Lewis --- x86/vmx.c | 19 +++++++++++++++++-- x86/vmx.h | 1 + 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/x86/vmx.c b/x86/vmx.c index f4fbb94..b2d8393 100644 --- a/x86/vmx.c +++ b/x86/vmx.c @@ -1884,15 +1884,30 @@ void test_add_teardown(test_teardown_func func, void *data) step->data = data; } +static void __test_set_guest(test_guest_func func) +{ + assert(current->v2); + v2_guest_main = func; +} + /* * Set the target of the first enter_guest call. Can only be called once per * test. Must be called before first enter_guest call. */ void test_set_guest(test_guest_func func) { - assert(current->v2); TEST_ASSERT_MSG(!v2_guest_main, "Already set guest func."); - v2_guest_main = func; + __test_set_guest(func); +} + +/* + * Set the target of the enter_guest call and reset the RIP so 'func' will + * start from the beginning. This can be called multiple times per test. + */ +void test_override_guest(test_guest_func func) +{ + __test_set_guest(func); + init_vmcs_guest(); } static void check_for_guest_termination(union exit_reason exit_reason) diff --git a/x86/vmx.h b/x86/vmx.h index 4423986..dabd5da 100644 --- a/x86/vmx.h +++ b/x86/vmx.h @@ -1055,6 +1055,7 @@ void hypercall(u32 hypercall_no); typedef void (*test_guest_func)(void); typedef void (*test_teardown_func)(void *data); void test_set_guest(test_guest_func func); +void test_override_guest(test_guest_func func); void test_add_teardown(test_teardown_func func, void *data); void test_skip(const char *msg); From patchwork Tue Jan 25 20:31:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Lewis X-Patchwork-Id: 12724227 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B998CC433F5 for ; Tue, 25 Jan 2022 20:32:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232091AbiAYUcF (ORCPT ); Tue, 25 Jan 2022 15:32:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37102 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232103AbiAYUcC (ORCPT ); Tue, 25 Jan 2022 15:32:02 -0500 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1FF37C061747 for ; Tue, 25 Jan 2022 12:32:02 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id q21-20020a170902edd500b0014ae79cc6d5so5639020plk.18 for ; Tue, 25 Jan 2022 12:32:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=8aDRqqZElF9pdZ78J9uxS24EVWOgt3ahYwBdFdRKzIk=; b=DczfssVyTXPsVHFs0USg59RHf83Wd6I9QtiwHpug4Oz4OcYsxbxofyvpick7EgD4Z0 IMJxSXdscz9CMQFIGb24Dol/B7cgIeeWZOy71YPQhaTjHBB40/Ss9Dm4ma/TBfYnDq84 i/8DD5STksX/QlyXp+vnrn+y3AEYI7z7RV+Qe9w2eb8i9h+/eNuZb0Qr5m1GrzVKQd+B R8gGa0R858ezoIw+Dljzi8+kL6Yq1HBKPcRUPr+8XAbKjH1Wg89d3m6LXsaCt75M3PyM Qe6+jCZw+s1hFr6zfGlk1h+O9cthiCSmiONIu99k4/ys/bvskQskSo9OJIql9aYW9B66 W7Yw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=8aDRqqZElF9pdZ78J9uxS24EVWOgt3ahYwBdFdRKzIk=; b=UJcMLwMwSUo0hu5/ybXLC/lfX0VoRxnWytR94spGAtQoW4M3bytF0dhCjuIqkhWpGj ZwVIorbbwM+0QflTCS4wb6Db8O10usDwS3PX4dhSQP/IcQXdhIrbiwj7lTpw6XAb69Jj pjidsXn/q68zqWMkctfNgW4naZnKGWnTjTadqAqHwuscxefH/yNgST914M6jmR0VmVUq u6QOvNVK9YQhgsphuBzGqEsCtZOUkrTzaE+l+IQ8yP6lWauuvdgDf/DYFQGYKg42+V5d cAgRt8w6i8gauOaCCCQeRGH65Whm0tkRSW+AL5Pa1r/Su+MHoaM/ewKpmmEv3X3oW0Ke D2PQ== X-Gm-Message-State: AOAM5309yoxgg2G2xLwkdFdjea1imyabBOh3pJTXvSfouPozLBopYikO pLQodThk+kURGt6iOHmHm7UsdS1YDP4U2P5q8zrqC8hCOyFhbVMJG817TLtbs+TgE0dE9RTC93Z 2jSMd9PduAiaO6pw2IpE2v3Cdcyrx8YuL3J3vOGSa6E+YrCDmOZQVAgQWYc5l3at7EFDd X-Google-Smtp-Source: ABdhPJxSo0Sl4Z/DohzSNw48b1g1bYBXSTQ5IgIRvVO7/uiuJQbZcE6fdyzczXX9yPRY9mBH7wiUZOlQivrKzwNv X-Received: from aaronlewis.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2675]) (user=aaronlewis job=sendgmr) by 2002:a17:903:11cf:b0:149:a17a:361b with SMTP id q15-20020a17090311cf00b00149a17a361bmr20723422plh.146.1643142721504; Tue, 25 Jan 2022 12:32:01 -0800 (PST) Date: Tue, 25 Jan 2022 20:31:26 +0000 In-Reply-To: <20220125203127.1161838-1-aaronlewis@google.com> Message-Id: <20220125203127.1161838-4-aaronlewis@google.com> Mime-Version: 1.0 References: <20220125203127.1161838-1-aaronlewis@google.com> X-Mailer: git-send-email 2.35.0.rc0.227.g00780c9af4-goog Subject: [kvm-unit-tests PATCH v5 3/4] x86: Add a helper to allow tests to signal completion without a vmcall() From: Aaron Lewis To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, jmattson@google.com, seanjc@google.com, Aaron Lewis Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Putting a vmcall() at the end of a nested test isn't always convenient to do, and isn't necessary. Add a helper to allow the nested test to make it possible to skip this requirement. Signed-off-by: Aaron Lewis --- x86/vmx.c | 5 +++++ x86/vmx.h | 1 + 2 files changed, 6 insertions(+) diff --git a/x86/vmx.c b/x86/vmx.c index b2d8393..51eed8c 100644 --- a/x86/vmx.c +++ b/x86/vmx.c @@ -1910,6 +1910,11 @@ void test_override_guest(test_guest_func func) init_vmcs_guest(); } +void test_set_guest_finished(void) +{ + guest_finished = 1; +} + static void check_for_guest_termination(union exit_reason exit_reason) { if (is_hypercall(exit_reason)) { diff --git a/x86/vmx.h b/x86/vmx.h index dabd5da..11cb665 100644 --- a/x86/vmx.h +++ b/x86/vmx.h @@ -1058,5 +1058,6 @@ void test_set_guest(test_guest_func func); void test_override_guest(test_guest_func func); void test_add_teardown(test_teardown_func func, void *data); void test_skip(const char *msg); +void test_set_guest_finished(void); #endif From patchwork Tue Jan 25 20:31:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Lewis X-Patchwork-Id: 12724228 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C1311C433EF for ; Tue, 25 Jan 2022 20:32:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232103AbiAYUcI (ORCPT ); Tue, 25 Jan 2022 15:32:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37128 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232094AbiAYUcH (ORCPT ); Tue, 25 Jan 2022 15:32:07 -0500 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EDC50C06173B for ; Tue, 25 Jan 2022 12:32:06 -0800 (PST) Received: by mail-pg1-x549.google.com with SMTP id f35-20020a631f23000000b0035ec54b3bbcso2996732pgf.0 for ; Tue, 25 Jan 2022 12:32:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=88jfaDj1il9xwABQu6LXGpSGhv+xtpdaHiWeEG67LVY=; b=okYA5H0p43iNISptw3Xv8jbZqsHIUpZHAK/xi0RfQM/MhC3vk40ByR9rh2Bulorp6c Iy/ert8hHbqSt9MduoGdTfT6goN5sJk50rLUlCTA7q4lbUqhO/zvlk75fbWxVAvyV9iG 4K1tCr6s/vueQkTJK2uxrQc8vLsg6svFFsVjp7fKmW0Ng+8RRiDkdoNDf/NUJYVxXqKq ajJD6b6ZLn+yqNslERr8Mp+M8n4KHmG1nhy7eKFM+HaP6R3/7D2awJ58exrqhUuvoDsN 8rVfZqjJWfqxtqAzisJSLre0LsHeLIx3rEgZReHn4xqAO/DyErsWPM1DZlX7tGrv4kYf 9QpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=88jfaDj1il9xwABQu6LXGpSGhv+xtpdaHiWeEG67LVY=; b=hn6cwLfjGopKnI768zPQD8LUHucILiB6ppHfqdKHBLEmHxlJNTjV4VGDlk14tNO/vY Tg0DvgS7j/Qu2oabFQjTJiQRlmPSpZiuos7WuuSnTIxpXyiB3AXhPEkGEn4M7GBFVtTd sPFVwYsywI/hDf3/WomCcMF/242segbF6vHU/ZlRfte5/eGMloY5TQY3Pxqx9ctEs665 wO0GuBSJVw+CteZ6+jXs9pxLCuDogayuPsIP4P75r5dt8kxr+GIxD5ikCJPhVCCbkSaU idXGPpTm3cdO+mAi6kNz4Otg6wpYCAV80L0c2Ml1rb8vSHCCahJ2mMib1nQvMT50koQI rhWQ== X-Gm-Message-State: AOAM531weQ+dFNEHeBHDCQKZh7kdQBOYTpcAKUkWc3smQeGPtOlwtCje rj/C3AUWcrSsxDeDJfmLPJsfw752m8khsGctQkm7SfJ7RH9B1PriCoQI1xNr4HybVPud66Moo9Y R8hxcN8IdXsaCHXLY+fNTTOOoNK3Sq6AYoyZCEDem5Y7IOjz7mGuPRxx3OT2Brevjs+JH X-Google-Smtp-Source: ABdhPJzspOH99CpfaZa0gp/EpXNI1SoU7hmZyMJsKeSuswUZWS4cmJKKxcdA+Bm1hoDyxkjjU3NMWRgrZxzKKCNh X-Received: from aaronlewis.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2675]) (user=aaronlewis job=sendgmr) by 2002:a17:902:8d83:b0:14b:4063:dbcd with SMTP id v3-20020a1709028d8300b0014b4063dbcdmr13170862plo.72.1643142726410; Tue, 25 Jan 2022 12:32:06 -0800 (PST) Date: Tue, 25 Jan 2022 20:31:27 +0000 In-Reply-To: <20220125203127.1161838-1-aaronlewis@google.com> Message-Id: <20220125203127.1161838-5-aaronlewis@google.com> Mime-Version: 1.0 References: <20220125203127.1161838-1-aaronlewis@google.com> X-Mailer: git-send-email 2.35.0.rc0.227.g00780c9af4-goog Subject: [kvm-unit-tests PATCH v5 4/4] x86: Add test coverage for nested_vmx_reflect_vmexit() testing From: Aaron Lewis To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, jmattson@google.com, seanjc@google.com, Aaron Lewis Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Add a framework and test cases to ensure exceptions that occur in L2 are forwarded to the correct place by nested_vmx_reflect_vmexit(). Add testing for exceptions: #GP, #UD, #DE, #DB, #BP, and #AC. Signed-off-by: Aaron Lewis --- x86/vmx_tests.c | 130 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) diff --git a/x86/vmx_tests.c b/x86/vmx_tests.c index 3d57ed6..796fd7b 100644 --- a/x86/vmx_tests.c +++ b/x86/vmx_tests.c @@ -21,6 +21,7 @@ #include "smp.h" #include "delay.h" #include "access.h" +#include "x86/usermode.h" #define VPID_CAP_INVVPID_TYPES_SHIFT 40 @@ -10701,6 +10702,134 @@ static void vmx_pf_vpid_test(void) __vmx_pf_vpid_test(invalidate_tlb_new_vpid, 1); } +static void vmx_l2_gp_test(void) +{ + *(volatile u64 *)NONCANONICAL = 0; +} + +static void vmx_l2_ud_test(void) +{ + asm volatile ("ud2"); +} + +static void vmx_l2_de_test(void) +{ + asm volatile ( + "xor %%eax, %%eax\n\t" + "xor %%ebx, %%ebx\n\t" + "xor %%edx, %%edx\n\t" + "idiv %%ebx\n\t" + ::: "eax", "ebx", "edx"); +} + +static void vmx_l2_bp_test(void) +{ + asm volatile ("int3"); +} + +static void vmx_l2_db_test(void) +{ + write_rflags(read_rflags() | X86_EFLAGS_TF); +} + +static uint64_t usermode_callback(void) +{ + /* + * Trigger an #AC by writing 8 bytes to a 4-byte aligned address. + * Disclaimer: It is assumed that the stack pointer is aligned + * on a 16-byte boundary as x86_64 stacks should be. + */ + asm volatile("movq $0, -0x4(%rsp)"); + + return 0; +} + +static void vmx_l2_ac_test(void) +{ + bool hit_ac = false; + + write_cr0(read_cr0() | X86_CR0_AM); + write_rflags(read_rflags() | X86_EFLAGS_AC); + + run_in_user(usermode_callback, AC_VECTOR, 0, 0, 0, 0, &hit_ac); + report(hit_ac, "Usermode #AC handled in L2"); + vmcall(); +} + +struct vmx_exception_test { + u8 vector; + void (*guest_code)(void); +}; + +struct vmx_exception_test vmx_exception_tests[] = { + { GP_VECTOR, vmx_l2_gp_test }, + { UD_VECTOR, vmx_l2_ud_test }, + { DE_VECTOR, vmx_l2_de_test }, + { DB_VECTOR, vmx_l2_db_test }, + { BP_VECTOR, vmx_l2_bp_test }, + { AC_VECTOR, vmx_l2_ac_test }, +}; + +static u8 vmx_exception_test_vector; + +static void vmx_exception_handler(struct ex_regs *regs) +{ + report(regs->vector == vmx_exception_test_vector, + "Handling %s in L2's exception handler", + exception_mnemonic(vmx_exception_test_vector)); + vmcall(); +} + +static void handle_exception_in_l2(u8 vector) +{ + handler old_handler = handle_exception(vector, vmx_exception_handler); + + vmx_exception_test_vector = vector; + + enter_guest(); + report(vmcs_read(EXI_REASON) == VMX_VMCALL, + "%s handled by L2", exception_mnemonic(vector)); + + handle_exception(vector, old_handler); +} + +static void handle_exception_in_l1(u32 vector) +{ + u32 old_eb = vmcs_read(EXC_BITMAP); + + vmcs_write(EXC_BITMAP, old_eb | (1u << vector)); + + enter_guest(); + + report((vmcs_read(EXI_REASON) == VMX_EXC_NMI) && + ((vmcs_read(EXI_INTR_INFO) & 0xff) == vector), + "%s handled by L1", exception_mnemonic(vector)); + + vmcs_write(EXC_BITMAP, old_eb); +} + +static void vmx_exception_test(void) +{ + struct vmx_exception_test *t; + int i; + + for (i = 0; i < ARRAY_SIZE(vmx_exception_tests); i++) { + t = &vmx_exception_tests[i]; + + /* + * Override the guest code before each run even though it's the + * same code, the VMCS guest state needs to be reinitialized. + */ + test_override_guest(t->guest_code); + handle_exception_in_l2(t->vector); + + test_override_guest(t->guest_code); + handle_exception_in_l1(t->vector); + } + + test_set_guest_finished(); +} + #define TEST(name) { #name, .v2 = name } /* name/init/guest_main/exit_handler/syscall_handler/guest_regs */ @@ -10810,5 +10939,6 @@ struct vmx_test vmx_tests[] = { TEST(vmx_pf_no_vpid_test), TEST(vmx_pf_invvpid_test), TEST(vmx_pf_vpid_test), + TEST(vmx_exception_test), { NULL, NULL, NULL, NULL, NULL, {0} }, };