From patchwork Sun Aug 7 14:28:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Luczaj X-Patchwork-Id: 12938161 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 D678AC25B0C for ; Sun, 7 Aug 2022 14:29:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233954AbiHGO3F (ORCPT ); Sun, 7 Aug 2022 10:29:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39962 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232569AbiHGO3B (ORCPT ); Sun, 7 Aug 2022 10:29:01 -0400 Received: from mailtransmit05.runbox.com (mailtransmit05.runbox.com [IPv6:2a0c:5a00:149::26]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D7886413 for ; Sun, 7 Aug 2022 07:29:00 -0700 (PDT) Received: from mailtransmit02.runbox ([10.9.9.162] helo=aibo.runbox.com) by mailtransmit05.runbox.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1oKhH0-007poC-Q4; Sun, 07 Aug 2022 16:28:58 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rbox.co; s=selector1; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From; bh=UkcKvMcTp4LxHIs2u3g0+weq+MmQInuNd7XSB7FVqP8=; b=RO6mo4seTlJ4OUeqVQMZIPopJj F0I/bEXWT9n9t1phCwessKLGvHuuOsNgWxXZxsgi8fbuHWJEw8ks6Nl/JE1LKOKfiJtXIAzJWmmIz XXsF9dFUJ8xgSAE6r0eAiAB/lrEejlDT26vfOhO1OvAxCTd6GynoGXookyxD3yNhamyNs+jxDzlU+ EncODtffpehucaSiqxdIFr4PJgGdlsxI0f4sJdocf+Z6rKUN3Zk11PtgmXaUF118QEgMt63mx9X33 AJJlUxFuetATvxOaL4X65rQJ8rAeN7tKlLSBgfCbxJiQ9y9iUh8vkbTrXM9IJ8PL4fPn6vf5Bf+TV aw2p6l0w==; Received: from [10.9.9.74] (helo=submission03.runbox) by mailtransmit02.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1oKhH0-0007jO-C4; Sun, 07 Aug 2022 16:28:58 +0200 Received: by submission03.runbox with esmtpsa [Authenticated ID (604044)] (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) id 1oKhGn-0002az-0S; Sun, 07 Aug 2022 16:28:45 +0200 From: Michal Luczaj To: seanjc@google.com Cc: kvm@vger.kernel.org, pbonzini@redhat.com, shuah@kernel.org, linux-kselftest@vger.kernel.org, mhal@rbox.co Subject: [kvm-unit-tests PATCH v2 1/5] x86: emulator.c cleanup: Save and restore exception handlers Date: Sun, 7 Aug 2022 16:28:28 +0200 Message-Id: <20220807142832.1576-2-mhal@rbox.co> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220807142832.1576-1-mhal@rbox.co> References: <20220807142832.1576-1-mhal@rbox.co> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Users of handle_exception() should always save and restore the handlers. Suggested-by: Sean Christopherson Signed-off-by: Michal Luczaj Reviewed-by: Sean Christopherson --- v1 -> v2: No changes x86/emulator.c | 78 ++++++++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 37 deletions(-) diff --git a/x86/emulator.c b/x86/emulator.c index cd78e3c..769a049 100644 --- a/x86/emulator.c +++ b/x86/emulator.c @@ -710,6 +710,7 @@ static __attribute__((target("sse2"))) void test_sse_exceptions(void *cross_mem) void *page2 = (void *)(&bytes[4096]); struct pte_search search; pteval_t orig_pte; + handler old; // setup memory for unaligned access mem = (uint32_t *)(&bytes[8]); @@ -725,10 +726,10 @@ static __attribute__((target("sse2"))) void test_sse_exceptions(void *cross_mem) asm("movupd %1, %0" : "=m"(*mem) : "x"(vv) : "memory"); report(sseeq(v, mem), "movupd unaligned"); exceptions = 0; - handle_exception(GP_VECTOR, unaligned_movaps_handler); + old = handle_exception(GP_VECTOR, unaligned_movaps_handler); asm("movaps %1, %0\n\t unaligned_movaps_cont:" : "=m"(*mem) : "x"(vv)); - handle_exception(GP_VECTOR, 0); + handle_exception(GP_VECTOR, old); report(exceptions == 1, "unaligned movaps exception"); // setup memory for cross page access @@ -746,10 +747,10 @@ static __attribute__((target("sse2"))) void test_sse_exceptions(void *cross_mem) invlpg(page2); exceptions = 0; - handle_exception(PF_VECTOR, cross_movups_handler); + old = handle_exception(PF_VECTOR, cross_movups_handler); asm("movups %1, %0\n\t cross_movups_cont:" : "=m"(*mem) : "x"(vv) : "memory"); - handle_exception(PF_VECTOR, 0); + handle_exception(PF_VECTOR, old); report(exceptions == 1, "movups crosspage exception"); // restore invalidated page @@ -817,36 +818,38 @@ static void advance_rip_and_note_exception(struct ex_regs *regs) static void test_mmx_movq_mf(uint64_t *mem) { - /* movq %mm0, (%rax) */ - extern char movq_start, movq_end; - - uint16_t fcw = 0; /* all exceptions unmasked */ - write_cr0(read_cr0() & ~6); /* TS, EM */ - exceptions = 0; - handle_exception(MF_VECTOR, advance_rip_and_note_exception); - asm volatile("fninit; fldcw %0" : : "m"(fcw)); - asm volatile("fldz; fldz; fdivp"); /* generate exception */ - - rip_advance = &movq_end - &movq_start; - asm(KVM_FEP "movq_start: movq %mm0, (%rax); movq_end:"); - /* exit MMX mode */ - asm volatile("fnclex; emms"); - report(exceptions == 1, "movq mmx generates #MF"); - handle_exception(MF_VECTOR, 0); + /* movq %mm0, (%rax) */ + extern char movq_start, movq_end; + handler old; + + uint16_t fcw = 0; /* all exceptions unmasked */ + write_cr0(read_cr0() & ~6); /* TS, EM */ + exceptions = 0; + old = handle_exception(MF_VECTOR, advance_rip_and_note_exception); + asm volatile("fninit; fldcw %0" : : "m"(fcw)); + asm volatile("fldz; fldz; fdivp"); /* generate exception */ + + rip_advance = &movq_end - &movq_start; + asm(KVM_FEP "movq_start: movq %mm0, (%rax); movq_end:"); + /* exit MMX mode */ + asm volatile("fnclex; emms"); + report(exceptions == 1, "movq mmx generates #MF"); + handle_exception(MF_VECTOR, old); } static void test_jmp_noncanonical(uint64_t *mem) { extern char nc_jmp_start, nc_jmp_end; + handler old; *mem = 0x1111111111111111ul; exceptions = 0; rip_advance = &nc_jmp_end - &nc_jmp_start; - handle_exception(GP_VECTOR, advance_rip_and_note_exception); + old = handle_exception(GP_VECTOR, advance_rip_and_note_exception); asm volatile ("nc_jmp_start: jmp *%0; nc_jmp_end:" : : "m"(*mem)); report(exceptions == 1, "jump to non-canonical address"); - handle_exception(GP_VECTOR, 0); + handle_exception(GP_VECTOR, old); } static void test_movabs(uint64_t *mem) @@ -979,22 +982,23 @@ static void ss_bad_rpl(struct ex_regs *regs) static void test_sreg(volatile uint16_t *mem) { - u16 ss = read_ss(); + u16 ss = read_ss(); + handler old; - // check for null segment load - *mem = 0; - asm volatile("mov %0, %%ss" : : "m"(*mem)); - report(read_ss() == 0, "mov null, %%ss"); - - // check for exception when ss.rpl != cpl on null segment load - exceptions = 0; - handle_exception(GP_VECTOR, ss_bad_rpl); - *mem = 3; - asm volatile("mov %0, %%ss; ss_bad_rpl_cont:" : : "m"(*mem)); - report(exceptions == 1 && read_ss() == 0, - "mov null, %%ss (with ss.rpl != cpl)"); - handle_exception(GP_VECTOR, 0); - write_ss(ss); + // check for null segment load + *mem = 0; + asm volatile("mov %0, %%ss" : : "m"(*mem)); + report(read_ss() == 0, "mov null, %%ss"); + + // check for exception when ss.rpl != cpl on null segment load + exceptions = 0; + old = handle_exception(GP_VECTOR, ss_bad_rpl); + *mem = 3; + asm volatile("mov %0, %%ss; ss_bad_rpl_cont:" : : "m"(*mem)); + report(exceptions == 1 && read_ss() == 0, + "mov null, %%ss (with ss.rpl != cpl)"); + handle_exception(GP_VECTOR, old); + write_ss(ss); } static uint64_t usr_gs_mov(void) From patchwork Sun Aug 7 14:28:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Luczaj X-Patchwork-Id: 12938162 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 C6D41C25B0F for ; Sun, 7 Aug 2022 14:29:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234003AbiHGO3G (ORCPT ); Sun, 7 Aug 2022 10:29:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39994 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233849AbiHGO3E (ORCPT ); Sun, 7 Aug 2022 10:29:04 -0400 Received: from mailtransmit04.runbox.com (mailtransmit04.runbox.com [IPv6:2a0c:5a00:149::25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D4BA3645E for ; Sun, 7 Aug 2022 07:29:01 -0700 (PDT) Received: from mailtransmit02.runbox ([10.9.9.162] helo=aibo.runbox.com) by mailtransmit04.runbox.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1oKhH2-007tOs-3J for kvm@vger.kernel.org; Sun, 07 Aug 2022 16:29:00 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rbox.co; s=selector1; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From; bh=u4vadCSfYEZ1a+HytCJ3n6FqiT6eRdseH7TkDVqEX7k=; b=D6RBJ7lBe7x13pmoXGQZAahHNU Pa9p90H66OaKA6/kJEj/3hcbk+5q/MwF0dRbF71qKU0JPBfCzBvF09oozM99X5BaTg8sAjHq/oVt8 vGttvUO5M3LsmSeeWvVALUou3gGh9qmb0Q6P2GO1TG8yQPwhuWojQoF0aTdXf5H3cGAP4gyahCCx0 zRF5j9Uu3cJ486sm47W8Dc7uAfIkUBNAYfZHhSducCCDUTt3Gq6B+pnFelU7AgvGiJ8HeV2XWuD63 3rXKHBM9Hky4vRznmIXrUDTyGntZDET8nv0eK2hr6WSd5qOt76VpkUkS55wZGPhQ76k13dQzlbnB+ /PbCVZJw==; Received: from [10.9.9.74] (helo=submission03.runbox) by mailtransmit02.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1oKhH1-0007kL-Mb; Sun, 07 Aug 2022 16:28:59 +0200 Received: by submission03.runbox with esmtpsa [Authenticated ID (604044)] (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) id 1oKhGp-0002az-Dg; Sun, 07 Aug 2022 16:28:47 +0200 From: Michal Luczaj To: seanjc@google.com Cc: kvm@vger.kernel.org, pbonzini@redhat.com, shuah@kernel.org, linux-kselftest@vger.kernel.org, mhal@rbox.co Subject: [kvm-unit-tests PATCH v2 2/5] x86: emulator.c cleanup: Use ASM_TRY() for the UD_VECTOR cases Date: Sun, 7 Aug 2022 16:28:29 +0200 Message-Id: <20220807142832.1576-3-mhal@rbox.co> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220807142832.1576-1-mhal@rbox.co> References: <20220807142832.1576-1-mhal@rbox.co> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org For #UD handling use ASM_TRY() instead of handle_exception(). Suggested-by: Sean Christopherson Signed-off-by: Michal Luczaj --- v1 -> v2: Change `mov` to `movl` to silence the compiler warning x86/emulator.c | 42 ++++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/x86/emulator.c b/x86/emulator.c index 769a049..96d3ccd 100644 --- a/x86/emulator.c +++ b/x86/emulator.c @@ -17,10 +17,8 @@ static int exceptions; -/* Forced emulation prefix, used to invoke the emulator unconditionally. */ +/* Forced emulation prefix, used to invoke the emulator unconditionally. */ #define KVM_FEP "ud2; .byte 'k', 'v', 'm';" -#define KVM_FEP_LENGTH 5 -static int fep_available = 1; struct regs { u64 rax, rbx, rcx, rdx; @@ -1099,33 +1097,23 @@ static void test_simplealu(u32 *mem) report(*mem == 0x8400, "test"); } -static void illegal_movbe_handler(struct ex_regs *regs) -{ - extern char bad_movbe_cont; - - ++exceptions; - regs->rip = (ulong)&bad_movbe_cont; -} - static void test_illegal_movbe(void) { + unsigned int vector; + if (!this_cpu_has(X86_FEATURE_MOVBE)) { - report_skip("illegal movbe"); + report_skip("MOVBE unsupported by CPU"); return; } - exceptions = 0; - handle_exception(UD_VECTOR, illegal_movbe_handler); - asm volatile(".byte 0x0f; .byte 0x38; .byte 0xf0; .byte 0xc0;\n\t" - " bad_movbe_cont:" : : : "rax"); - report(exceptions == 1, "illegal movbe"); - handle_exception(UD_VECTOR, 0); -} + asm volatile(ASM_TRY("1f") + ".byte 0x0f; .byte 0x38; .byte 0xf0; .byte 0xc0;\n\t" + "1:" + : : : "memory", "rax"); -static void record_no_fep(struct ex_regs *regs) -{ - fep_available = 0; - regs->rip += KVM_FEP_LENGTH; + vector = exception_vector(); + report(vector == UD_VECTOR, + "Wanted #UD on MOVBE with /reg, got vector = %u", vector); } int main(void) @@ -1135,11 +1123,13 @@ int main(void) void *insn_ram; void *cross_mem; unsigned long t1, t2; + int fep_available = 0; setup_vm(); - handle_exception(UD_VECTOR, record_no_fep); - asm(KVM_FEP "nop"); - handle_exception(UD_VECTOR, 0); + asm volatile(ASM_TRY("1f") + KVM_FEP "movl $1, %[fep_available]\n\t" + "1:" + : [fep_available] "=m" (fep_available) : : "memory"); mem = alloc_vpages(2); install_page((void *)read_cr3(), IORAM_BASE_PHYS, mem); From patchwork Sun Aug 7 14:28:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Luczaj X-Patchwork-Id: 12938158 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 D8DDAC25B0D for ; Sun, 7 Aug 2022 14:29:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233818AbiHGO3D (ORCPT ); Sun, 7 Aug 2022 10:29:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39946 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230332AbiHGO3B (ORCPT ); Sun, 7 Aug 2022 10:29:01 -0400 Received: from mailtransmit05.runbox.com (mailtransmit05.runbox.com [IPv6:2a0c:5a00:149::26]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5524763CD; Sun, 7 Aug 2022 07:28:59 -0700 (PDT) Received: from mailtransmit03.runbox ([10.9.9.163] helo=aibo.runbox.com) by mailtransmit05.runbox.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1oKhGz-007po6-Go; Sun, 07 Aug 2022 16:28:57 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rbox.co; s=selector1; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From; bh=BNwp4N13pHNhXN8W8GMaMwTpbUSiiHRpPFgA+FJuxUE=; b=AnAO9Fl2VaoKjXPo7UrvI5KIJ9 16Haf3pDI++HPyqDCzEr9HGgvmEB84RPdR2IGbjja/VWT6zWcU+HbmX1wsXn5rHv2KnDtlKOPYPyH d4hcsvzmRG7YR+DEWKy+Rsis1ZeEpiz/bXsD23x9nbNwQIhHWXU4uTYaFK6zENZoOB9enF6piGH1M yXB/xLlS7D7FdPSuIK0Lqb07J7uuJOXNuScEv+nfmJqPYrntaDoNxHU2WUZ6O1BlgeJDlQ20clKlX W5klGMldLAREgUNJe6y8VzMkWrOz8EPfAaSncGidITWjpadJWH8wNjewQJmoXxtlw1PQsz7ZROtV+ Gz6xxE9w==; Received: from [10.9.9.74] (helo=submission03.runbox) by mailtransmit03.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1oKhGz-0005Nv-9S; Sun, 07 Aug 2022 16:28:57 +0200 Received: by submission03.runbox with esmtpsa [Authenticated ID (604044)] (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) id 1oKhGr-0002az-OH; Sun, 07 Aug 2022 16:28:49 +0200 From: Michal Luczaj To: seanjc@google.com Cc: kvm@vger.kernel.org, pbonzini@redhat.com, shuah@kernel.org, linux-kselftest@vger.kernel.org, mhal@rbox.co Subject: [kvm-unit-tests PATCH v2 3/5] x86: Introduce ASM_TRY_FEP() to handle exceptions thrown by FEP-triggered emulator Date: Sun, 7 Aug 2022 16:28:30 +0200 Message-Id: <20220807142832.1576-4-mhal@rbox.co> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220807142832.1576-1-mhal@rbox.co> References: <20220807142832.1576-1-mhal@rbox.co> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Paolo Bonzini TRY_ASM() mishandles exceptions thrown by the forced-emulation-triggered emulator. While the faulting address stored in the exception table points at forced emulation prefix, when an exceptions comes, RIP is 5 bytes (size of KVM_FEP) ahead and the exception ends up unhandled. Signed-off-by: Michal Luczaj --- For the sake of completeness, I've added `prefix` to 32-bit __TRY_ASM() as well. lib/x86/desc.h | 8 ++++++-- x86/emulator.c | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/x86/desc.h b/lib/x86/desc.h index 2a285eb..8854fcc 100644 --- a/lib/x86/desc.h +++ b/lib/x86/desc.h @@ -81,21 +81,25 @@ typedef struct __attribute__((packed)) { } tss64_t; #ifdef __x86_64 -#define ASM_TRY(catch) \ +#define __ASM_TRY(prefix, catch) \ "movl $0, %%gs:4 \n\t" \ ".pushsection .data.ex \n\t" \ ".quad 1111f, " catch "\n\t" \ ".popsection \n\t" \ + prefix \ "1111:" #else -#define ASM_TRY(catch) \ +#define __ASM_TRY(prefix, catch) \ "movl $0, %%gs:4 \n\t" \ ".pushsection .data.ex \n\t" \ ".long 1111f, " catch "\n\t" \ ".popsection \n\t" \ + prefix \ "1111:" #endif +#define ASM_TRY(catch) __ASM_TRY("", catch) + /* * selector 32-bit 64-bit * 0x00 NULL descriptor NULL descriptor diff --git a/x86/emulator.c b/x86/emulator.c index 96d3ccd..0eb7c1a 100644 --- a/x86/emulator.c +++ b/x86/emulator.c @@ -19,6 +19,7 @@ static int exceptions; /* Forced emulation prefix, used to invoke the emulator unconditionally. */ #define KVM_FEP "ud2; .byte 'k', 'v', 'm';" +#define ASM_TRY_FEP(catch) __ASM_TRY(KVM_FEP, catch) struct regs { u64 rax, rbx, rcx, rdx; From patchwork Sun Aug 7 14:28:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Luczaj X-Patchwork-Id: 12938159 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 B8516C3F6B0 for ; Sun, 7 Aug 2022 14:29:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233799AbiHGO3C (ORCPT ); Sun, 7 Aug 2022 10:29:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39948 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230020AbiHGO3B (ORCPT ); Sun, 7 Aug 2022 10:29:01 -0400 Received: from mailtransmit05.runbox.com (mailtransmit05.runbox.com [IPv6:2a0c:5a00:149::26]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 552DD63CF; Sun, 7 Aug 2022 07:28:59 -0700 (PDT) Received: from mailtransmit03.runbox ([10.9.9.163] helo=aibo.runbox.com) by mailtransmit05.runbox.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1oKhGy-007pnx-Di; Sun, 07 Aug 2022 16:28:56 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rbox.co; s=selector1; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From; bh=vW0skxzzr6i2Gd196B0hHDou57V5BdI+dFA4selfauQ=; b=EJTjvkZDsxTlToBRR34s7xt0PB K3lp/sNWUaBN0GZlgWJ6j7I/rwZqT+mOO2LdJScCIt2yKQNR4xEzO8BEr+MQACETh/C8Y9ybKtLr+ zUZGTJxsXnRedNAPYMneuL+GncYNyIekEWaXavyyLl51l7JR+gJZytNsW8vsOeResEJFtwxfDGdhM 0w122uYNhVQCPwXrOYTD++J8rX02iJxyph/9z1QDXlWQv4lIHjqqEgTN8imSRx49od3+uB9YoUBoT eMGiUtjKT1ZMZXXiNLznbulLZ0c7T4VSvtVo3HWXQj9Z1vmRZSAB/u2Ley9Bqr6uDmSLq9Vtm/OjS cX6Mz51A==; Received: from [10.9.9.74] (helo=submission03.runbox) by mailtransmit03.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1oKhGy-0005Np-10; Sun, 07 Aug 2022 16:28:56 +0200 Received: by submission03.runbox with esmtpsa [Authenticated ID (604044)] (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) id 1oKhGu-0002az-2f; Sun, 07 Aug 2022 16:28:52 +0200 From: Michal Luczaj To: seanjc@google.com Cc: kvm@vger.kernel.org, pbonzini@redhat.com, shuah@kernel.org, linux-kselftest@vger.kernel.org, mhal@rbox.co Subject: [kvm-unit-tests PATCH v2 4/5] x86: Dedup 32-bit vs. 64-bit ASM_TRY() by stealing kernel's __ASM_SEL() Date: Sun, 7 Aug 2022 16:28:31 +0200 Message-Id: <20220807142832.1576-5-mhal@rbox.co> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220807142832.1576-1-mhal@rbox.co> References: <20220807142832.1576-1-mhal@rbox.co> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Sean Christopherson Steal the kernel's __ASM_SEL() implementation and use it to consolidate ASM_TRY(). The only difference between the 32-bit and 64-bit versions is the size of the address stored in the table. No functional change intended. Signed-off-by: Michal Luczaj --- lib/x86/desc.h | 22 ++++++---------------- lib/x86/processor.h | 12 ++++++++++++ 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/lib/x86/desc.h b/lib/x86/desc.h index 8854fcc..5bb6fcb 100644 --- a/lib/x86/desc.h +++ b/lib/x86/desc.h @@ -80,23 +80,13 @@ typedef struct __attribute__((packed)) { u16 iomap_base; } tss64_t; -#ifdef __x86_64 -#define __ASM_TRY(prefix, catch) \ - "movl $0, %%gs:4 \n\t" \ - ".pushsection .data.ex \n\t" \ - ".quad 1111f, " catch "\n\t" \ - ".popsection \n\t" \ - prefix \ +#define __ASM_TRY(prefix, catch) \ + "movl $0, %%gs:4 \n\t" \ + ".pushsection .data.ex \n\t" \ + __ASM_SEL(.long, .quad) " 1111f, " catch "\n\t" \ + ".popsection \n\t" \ + prefix \ "1111:" -#else -#define __ASM_TRY(prefix, catch) \ - "movl $0, %%gs:4 \n\t" \ - ".pushsection .data.ex \n\t" \ - ".long 1111f, " catch "\n\t" \ - ".popsection \n\t" \ - prefix \ - "1111:" -#endif #define ASM_TRY(catch) __ASM_TRY("", catch) diff --git a/lib/x86/processor.h b/lib/x86/processor.h index 0324220..30e2de8 100644 --- a/lib/x86/processor.h +++ b/lib/x86/processor.h @@ -19,6 +19,18 @@ # define S "4" #endif +#ifdef __ASSEMBLY__ +#define __ASM_FORM(x, ...) x,## __VA_ARGS__ +#else +#define __ASM_FORM(x, ...) " " xstr(x,##__VA_ARGS__) " " +#endif + +#ifndef __x86_64__ +#define __ASM_SEL(a,b) __ASM_FORM(a) +#else +#define __ASM_SEL(a,b) __ASM_FORM(b) +#endif + #define DB_VECTOR 1 #define BP_VECTOR 3 #define UD_VECTOR 6 From patchwork Sun Aug 7 14:28:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Luczaj X-Patchwork-Id: 12938160 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 A5D61C25B06 for ; Sun, 7 Aug 2022 14:29:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233874AbiHGO3E (ORCPT ); Sun, 7 Aug 2022 10:29:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39956 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231958AbiHGO3B (ORCPT ); Sun, 7 Aug 2022 10:29:01 -0400 Received: from mailtransmit05.runbox.com (mailtransmit05.runbox.com [IPv6:2a0c:5a00:149::26]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5536963E6; Sun, 7 Aug 2022 07:28:59 -0700 (PDT) Received: from mailtransmit02.runbox ([10.9.9.162] helo=aibo.runbox.com) by mailtransmit05.runbox.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1oKhGy-007po1-Sd; Sun, 07 Aug 2022 16:28:56 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rbox.co; s=selector1; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From; bh=SkDyDNdlUoRerspo76l+6X16aQ22d3uzCxkG0Mkdu60=; b=AlYXYxAl5dI3DkFoJ8tTUwrC9Z /wJ02i4wsGSKCST4LbpK9CL5WwQbUm/abcF8xRMo8O170fofrAUKlfNMYLWR3+NgMmyenrTkuXNhI iQ5HC7FRvC+aSSqJauMAEqd8DHLyXHjbg5c0WB8gVunggNMnDslH3ruNsN98pntpj6JELgj2LzVqa fG+K1LmtEqQ0bXsev+RXzRTV5/xq8zR3D9t4VjM4E+trifSpAcmq/+EGPrJCBFzAhkWDvkbL+AEDt ir8793TcCGgpOn8ybmxq4MGgcAxL9dVQpoRvvX+Tngn0T1vNkWI4GvTgY5qSS3TiGU3cDI5RMGej4 r6MUeisQ==; Received: from [10.9.9.74] (helo=submission03.runbox) by mailtransmit02.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1oKhGy-0007jG-KC; Sun, 07 Aug 2022 16:28:56 +0200 Received: by submission03.runbox with esmtpsa [Authenticated ID (604044)] (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) id 1oKhGw-0002az-6x; Sun, 07 Aug 2022 16:28:54 +0200 From: Michal Luczaj To: seanjc@google.com Cc: kvm@vger.kernel.org, pbonzini@redhat.com, shuah@kernel.org, linux-kselftest@vger.kernel.org, mhal@rbox.co Subject: [kvm-unit-tests PATCH v2 5/5] x86: Test emulator's handling of LEA with /reg Date: Sun, 7 Aug 2022 16:28:32 +0200 Message-Id: <20220807142832.1576-6-mhal@rbox.co> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220807142832.1576-1-mhal@rbox.co> References: <20220807142832.1576-1-mhal@rbox.co> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org LEA with a register-direct source operand is illegal. Verify that the emulator raises #UD. Suggested-by: Sean Christopherson Signed-off-by: Michal Luczaj --- v1 -> v2: Use ASM_TRY_FEP() x86/emulator.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/x86/emulator.c b/x86/emulator.c index 0eb7c1a..dc91ce3 100644 --- a/x86/emulator.c +++ b/x86/emulator.c @@ -897,6 +897,20 @@ static void test_mov_dr(uint64_t *mem) report(rax == DR6_ACTIVE_LOW, "mov_dr6"); } +static void test_illegal_lea(void) +{ + unsigned int vector; + + asm volatile (ASM_TRY_FEP("1f") + ".byte 0x8d; .byte 0xc0\n\t" + "1:" + : : : "memory", "eax"); + + vector = exception_vector(); + report(vector == UD_VECTOR, + "Wanted #UD on LEA with /reg, got vector = %u", vector); +} + static void test_push16(uint64_t *mem) { uint64_t rsp1, rsp2; @@ -1188,6 +1202,7 @@ int main(void) test_smsw_reg(mem); test_nop(mem); test_mov_dr(mem); + test_illegal_lea(); } else { report_skip("skipping register-only tests, " "use kvm.force_emulation_prefix=1 to enable");