From patchwork Mon Feb 27 18:06:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 62085 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp2566702wrd; Mon, 27 Feb 2023 10:08:32 -0800 (PST) X-Google-Smtp-Source: AK7set/XgQi6yMa+KcBNer3gnrPZnqbudUm19rFwbH4A/k9aRB7sbFAbMvYIUUBdNuJscdWEBhM4 X-Received: by 2002:a05:6402:4d:b0:4ac:cb71:42c with SMTP id f13-20020a056402004d00b004accb71042cmr366896edu.37.1677521312193; Mon, 27 Feb 2023 10:08:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677521312; cv=none; d=google.com; s=arc-20160816; b=zIslZzXUBlcd0fQ6/AdamrUb1q+HA7hQBbvIcXr+U1Ulc0Boiq7hA0982dJWmjbEim aQxj2nXDy+PEigBvO9mFjI8GogdK3ee0tV5OhK/0n5mDeRvRuE5Xao/J9xZcZcmUTmqO Ma0DdjKJ4Gb307zx3X9mi2w8z5k+cLk+ETaNLcS7cQIs0d/iveK5K59Grk6xHaWSRdlZ nAVRmTsdc3UUDFVk28TOzJaiQhyJwpB4feI5x9XHHqRu4PaojTDtBhYqXlk7l172Sieh Xu3j/UKmLVeWVQkrN4yxN8wty/YV9QdtRPU+fwKdJgchxxMwQdSnh/PeegdCpT5NHyFW nvyA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:mime-version:date :dkim-signature; bh=CCs6UT8o6jWjsVnlpOlGQvwvfh2gZBztGVt5ejScdkA=; b=Yq9MAyAVI0B8bEsTt9HJr/7cz+E8VBURtPjtNf/3g5kNEkw9Sjvp+hR8EqTq7VSdt3 +spaHAJO+lbVr41te6o9XvFfYr3NqyVyHsPdwJh9GfvBeibpU5O9Dfu1UyBg5p6N3Qw0 KSJRMVP0DhuuL/SimpeH4WOd+PBOYJFxLnmNPEfXPxtlcWC4BmtHQlOXNQkOpHE7qqMd UgJ5S+BWNrWMzCj1d7xZ0IsgD7dDekuAiaXWqTV92wENjqoaScDKzTmgTbzScaQe5yE/ 4J8HHuwoHP2vyJNQ8+y0wBGyHzZcv6ozzIbfxyoSPYF4+xdKbBCOm6SLmD2wD4jdJZ3d iWLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=n1Hm70B2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j7-20020a17090686c700b008e1cbdcd3c6si116953ejy.462.2023.02.27.10.08.09; Mon, 27 Feb 2023 10:08:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=n1Hm70B2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229718AbjB0SGq (ORCPT + 99 others); Mon, 27 Feb 2023 13:06:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34352 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229868AbjB0SGo (ORCPT ); Mon, 27 Feb 2023 13:06:44 -0500 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3A8672737 for ; Mon, 27 Feb 2023 10:06:16 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-536a545bfbaso156191667b3.20 for ; Mon, 27 Feb 2023 10:06:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=CCs6UT8o6jWjsVnlpOlGQvwvfh2gZBztGVt5ejScdkA=; b=n1Hm70B2pVQ4JlAhGiUDyj4CEH8k0YYYiRK+CxFnN6Ks5AuOrDlHiK9oH4W9oR6D/Z IyAD9aW7E6kJPybduNEV2dOGlxSucL/cGgipuHi8rs8KB6gj2Mb7+ZMgxgc6HiW7t74w r6TtEub/Ib2L65WI0R/PZrqIF3GvfS5n64uVujKPFkeCh0/CDcBxibe+cUGdL4Gs4FSr rck6QYtvVtC6jUs3hMWknorvuedx97yfjeQ4/38KVfggUKU7rSIg1Xy33rKgKSCFTeU6 IDlFX/b8umtqUw/L8+NF64Erf7JpxHtUoWok77B/UBFxLIQPpMXOowJlWJIlZD2NtbcP nbNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=CCs6UT8o6jWjsVnlpOlGQvwvfh2gZBztGVt5ejScdkA=; b=URC67HUzsi/fo+87w/fCpqJLmxmt28sFLYFBJRVCezYjFhLRl3vP52y4NdDdNYEdnL 0933sb07Vn9ATTxuntrx6yyWLJMDq7iQyUk+Yt/8DW4M3t/FiPcDek/P3xz9n5oO2v8k lCaLKHnifXGVk+rxkBMRVAGHvwgOFLEzD17Np6g1js5DNy78U1wXkIogE1VI2S08vRpG kO/kamI34706oohsIKLeEmL/OpZuZK0Bc/g7ceCuNzwR1dTYVdebhLaCoGFZoXNpaeVW AmdJMvMD3cKLjyixsLSr83yiPWuJgQpJnkWxugbKFs8aUvpTmt84HIsUdwVKCmrui3mW 8hlw== X-Gm-Message-State: AO0yUKW+x4dG3sXMQFBAagXyqG8BoRePgj6QVbYqlZo5GuU9WkNaBvCQ fjFh4G5jkCSqhlOZPky39yl/Z9P4+R0LN4XdUA== X-Received: from ackerleytng-cloudtop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1f5f]) (user=ackerleytng job=sendgmr) by 2002:a05:6902:118c:b0:a06:538f:265f with SMTP id m12-20020a056902118c00b00a06538f265fmr8185254ybu.4.1677521173110; Mon, 27 Feb 2023 10:06:13 -0800 (PST) Date: Mon, 27 Feb 2023 18:06:01 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.39.2.722.g9855ee24e9-goog Message-ID: <20230227180601.104318-1-ackerleytng@google.com> Subject: [PATCH v2 1/1] KVM: selftests: Adjust VM's initial stack address to align with SysV ABI spec From: Ackerley Tng To: pbonzini@redhat.com, shuah@kernel.org, seanjc@google.com, dmatlack@google.com, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Cc: erdemaktas@google.com, vannapurve@google.com, sagis@google.com, mail@maciej.szmigiero.name, Ackerley Tng X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759008587272230407?= X-GMAIL-MSGID: =?utf-8?q?1759008587272230407?= Align the guest stack to match calling sequence requirements in section "The Stack Frame" of the System V ABI AMD64 Architecture Processor Supplement, which requires the value (%rsp + 8), NOT %rsp, to be a multiple of 16 when control is transferred to the function entry point. I.e. in a normal function call, %rsp needs to be 16-byte aligned _before_ CALL, not after. This fixes unexpected #GPs in guest code when the compiler uses SSE instructions, e.g. to initialize memory, as many SSE instructions require memory operands (including those on the stack) to be 16-byte-aligned. Signed-off-by: Ackerley Tng --- This patch is a follow-up from discussions at https://lore.kernel.org/lkml/20230121001542.2472357-9-ackerleytng@google.com/ v1 -> v2: Cleaned the patch up after getting comments from Sean in v1: https://lore.kernel.org/lkml/Y%2FfHLdvKHlK6D%2F1v@google.com/ Please also see https://lore.kernel.org/lkml/20230227174654.94641-1-ackerleytng@google.com/ regarding providing alignment macros for selftests. --- .../selftests/kvm/lib/x86_64/processor.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) -- 2.39.2.722.g9855ee24e9-goog diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c index ae1e573d94ce..a0669d31bb85 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c @@ -5,6 +5,7 @@ * Copyright (C) 2018, Google LLC. */ +#include "linux/bitmap.h" #include "test_util.h" #include "kvm_util.h" #include "processor.h" @@ -573,6 +574,21 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id, DEFAULT_GUEST_STACK_VADDR_MIN, MEM_REGION_DATA); + stack_vaddr += DEFAULT_STACK_PGS * getpagesize(); + + /* + * Align stack to match calling sequence requirements in section "The + * Stack Frame" of the System V ABI AMD64 Architecture Processor + * Supplement, which requires the value (%rsp + 8) to be a multiple of + * 16 when control is transferred to the function entry point. + * + * If this code is ever used to launch a vCPU with 32-bit entry point it + * may need to subtract 4 bytes instead of 8 bytes. + */ + TEST_ASSERT(IS_ALIGNED(stack_vaddr, PAGE_SIZE), + "__vm_vaddr_alloc() did not provide a page-aligned address"); + stack_vaddr -= 8; + vcpu = __vm_vcpu_add(vm, vcpu_id); vcpu_init_cpuid(vcpu, kvm_get_supported_cpuid()); vcpu_setup(vm, vcpu); @@ -580,7 +596,7 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id, /* Setup guest general purpose registers */ vcpu_regs_get(vcpu, ®s); regs.rflags = regs.rflags | 0x2; - regs.rsp = stack_vaddr + (DEFAULT_STACK_PGS * getpagesize()); + regs.rsp = stack_vaddr; regs.rip = (unsigned long) guest_code; vcpu_regs_set(vcpu, ®s);