From patchwork Mon Jul 25 20:13:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12928374 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 A8CB8C433EF for ; Mon, 25 Jul 2022 20:15:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237073AbiGYUPO (ORCPT ); Mon, 25 Jul 2022 16:15:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46064 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237126AbiGYUOu (ORCPT ); Mon, 25 Jul 2022 16:14:50 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3E48122B21 for ; Mon, 25 Jul 2022 13:13:41 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id s5-20020a17090a6e4500b001f25fb86516so2552757pjm.5 for ; Mon, 25 Jul 2022 13:13:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=yTSMINSAGLeRbYcxbuCirWnBX4rTdLH3q5UUnzFrRNw=; b=DawTGenuC+BZwAu62Gvsz1q/CG81tzes03ZUWZ8f1iG5AzzS632/8lWfZOB0HaaKIH U2/3Maq22l5b78j+pDDaJxmj1JZHRuz6m0GVA6/r0SsERVEe2trhIuha0NddU0b8U9ek TXfJXLgrb9fcKMH4Z8xJSiHFRaVjyTJCO1HwCHXbzlYbNXU1Q1LepY4DtYIX22wHc9MK IIYBYN0CaXGbXZEN9mkYAl14oJIOVsntj76RfZIlvNVJ7C317irs/MRes8CQPw3nxI/1 f5dMIgjpXVbMzm4EE3+Uk8xZIA2AbquyJiLiuDX3uHctZ8leq2ndv+Uvd3NVJMVxtVWb 4QZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=yTSMINSAGLeRbYcxbuCirWnBX4rTdLH3q5UUnzFrRNw=; b=jnwxXbtVGpvOqLOb1LKbOozAW26qmKbUFG+e9V2d8bKpf0AWqQ0sO6dSkyd9o971cP APg/+2Ot0lcFBsrh8xvg8cIOtg1Ocp1J5cReMZ357kAz5VPNImV9VkcJTMkKvZcnvs0W PBIZWUiGXQrWAkd8hlJEbQONqG8Sds/kMo9SwoVoHZJ2zGCmBUH8QEQcdmT73ylOmV8B R1x1LrEdZhctQge4Y0fhaOUimk62e8hOOSm759hxn05BsXA8iBMJ4Yf5E89bEHg5VP9J MGssBu1NgvS+VhHf0OMzasIjjEJnENtZj2FLup+LSNeG3W1OuQFhavscC2V0uFgyPE9L t+Qw== X-Gm-Message-State: AJIora8RC3nQ1E3Ylowqirtqv3z5Y4BhgdwmxoKcTkrN3jMJCb9e6Dz3 P2ccax1dbQSpvmpzvgHeJfJ3JlHfnnQ= X-Google-Smtp-Source: AGRyM1sunQTwpr85m9usBgVJdMGnK+z8TY+Wu9BiJbPsW/KglLzL0wC4POoU1nan17xtgdEm5x06oO54HF0= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:301f:b0:52a:ccdd:17f9 with SMTP id ay31-20020a056a00301f00b0052accdd17f9mr14183583pfb.82.1658780020783; Mon, 25 Jul 2022 13:13:40 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 25 Jul 2022 20:13:35 +0000 In-Reply-To: <20220725201336.2158604-1-seanjc@google.com> Message-Id: <20220725201336.2158604-2-seanjc@google.com> Mime-Version: 1.0 References: <20220725201336.2158604-1-seanjc@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [kvm-unit-tests PATCH 1/2] x86: apic: Play nice with x2APIC being enabled when getting "pre-boot" ID From: Sean Christopherson To: Paolo Bonzini Cc: kvm@vger.kernel.org, Sean Christopherson Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Retrieve the "pre-boot" APIC ID via the x2APIC interface if x2APIC is enabled instead of assuming that the APIC is always in xAPIC mode. EFI has a catch-22 where it needs the APID ID to initialize the per-vCPU GS.base, but calling reset_apic() = >disable_apic() needs GS.base to be correctly initialized in order to set the correct APIC ops. Play nice with either xAPIC or x2APIC so that EFI can be used for SMP tests, in particular the SVM INIT-SIPI tests which send APs back through the boot sequence while x2APIC is enabled. Alternatively, disabling x2APIC and updating the APIC ops could be split, but there's no obvious advantage in doing so. Retrieving the pre-boot APIC ID isn't a hot path, i.e. the cost of the RDMSR is likely negligible, and letting callers force xAPIC without updating the ops isn't any less fragile. Signed-off-by: Sean Christopherson --- lib/x86/apic.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/x86/apic.c b/lib/x86/apic.c index 5d4c776..eed93fa 100644 --- a/lib/x86/apic.c +++ b/lib/x86/apic.c @@ -56,11 +56,6 @@ static uint32_t xapic_id(void) return xapic_read(APIC_ID) >> 24; } -uint32_t pre_boot_apic_id(void) -{ - return xapic_id(); -} - static const struct apic_ops xapic_ops = { .reg_read = xapic_read, .reg_write = xapic_write, @@ -165,6 +160,15 @@ int enable_x2apic(void) } } +uint32_t pre_boot_apic_id(void) +{ + u32 msr_lo, msr_hi; + + asm ("rdmsr" : "=a"(msr_lo), "=d"(msr_hi) : "c"(MSR_IA32_APICBASE)); + + return (msr_lo & APIC_EXTD) ? x2apic_id() : xapic_id(); +} + void disable_apic(void) { wrmsr(MSR_IA32_APICBASE, rdmsr(MSR_IA32_APICBASE) & ~(APIC_EN | APIC_EXTD)); From patchwork Mon Jul 25 20:13:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12928375 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 1FB02C43334 for ; Mon, 25 Jul 2022 20:15:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237137AbiGYUPQ (ORCPT ); Mon, 25 Jul 2022 16:15:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46082 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236900AbiGYUOy (ORCPT ); Mon, 25 Jul 2022 16:14:54 -0400 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 9DCBE22B27 for ; Mon, 25 Jul 2022 13:13:42 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id b17-20020a170903229100b0016d3e892112so5624424plh.6 for ; Mon, 25 Jul 2022 13:13:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=JHVVEG+MppVPv9YMCBO9GV6xd0sX4BJVP9qlD8DC6ZE=; b=nHfLu5QLAumy2Xh3TBsU3IOJG1tRIYUyaU84j8Fk7QARs7//ZdluYtipUsg4RbImIA E34gZ7OBAjKdM03dV9iFRA5IIUmDuDbTUzNB7oPCY+S2DqyRiGbORkcct4OanAcMBVdi 78TEMg3DWmT+v4l/iNMgO27iNPphcZBuEE88A6fws8nFgpOO3Y3HcRpTlKQXMEVbolgw IR4bh2V2KdkQcZtcJiEfLndrzjD8pR2jrLc6LEBTqJUpDMnrgpo+Eypz28pUoyCFXMIm Xn5hYV47VEEnqjkn2Zmpfnh0BNo531iMvhOE3XdKyM48bi4hBGcDcPsmpWJpOEHd8KgL tFPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=JHVVEG+MppVPv9YMCBO9GV6xd0sX4BJVP9qlD8DC6ZE=; b=S70GBfF5oGKZtMfB4Owf9rKsRxo6QrtPNqtE0heucI+2r7i6zrdfWoM0n8TVlckhAj XizcOAPP5v7VdeoKtHLyeea+eIho0SjNHU65cjsLcOEXbQymd+7E/ebMK4NZkXJd6k7T SLAxQfZTEdyhgGanpUv8kMUdQSFQUb0cy/ie/tH6ZuW9VHd84JlitlvNYSDMFxvEubpO K9N/8WNSaVwoyeVo0QM8FDi2DB9NYhKxWGqMyug48P8nnASPZQevRiho2i71jvCMEWh7 5b7sXnQTg/hjW/FVPN0T/DD5nGIgAbTuUhYDJWb1Oy0nYLMXAB8LzcWSLyWucpl7Shiw KeVg== X-Gm-Message-State: AJIora8YHWdS5hgCf7HNDmawGekERXgXVyATPNA4aT6FIVBzY1OHRl7y lt+juQkFsk33mIhaDszd4ORxfd1Gb0c= X-Google-Smtp-Source: AGRyM1tYsPmrGTW2goNDKR08FrVJYxuwJ2OBLMiNvBEUl1qF7J2CLDFUt3IMF87WJoWzc6TZwe8sHSc8qnM= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:760f:b0:16c:ae59:c9b2 with SMTP id k15-20020a170902760f00b0016cae59c9b2mr13885686pll.0.1658780022201; Mon, 25 Jul 2022 13:13:42 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 25 Jul 2022 20:13:36 +0000 In-Reply-To: <20220725201336.2158604-1-seanjc@google.com> Message-Id: <20220725201336.2158604-3-seanjc@google.com> Mime-Version: 1.0 References: <20220725201336.2158604-1-seanjc@google.com> X-Mailer: git-send-email 2.37.1.359.gd136c6c3e2-goog Subject: [kvm-unit-tests PATCH 2/2] x86: cstart64: Put APIC into xAPIC after loading TSS From: Sean Christopherson To: Paolo Bonzini Cc: kvm@vger.kernel.org, Sean Christopherson Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Now that pre_boot_apic_id() works with either xAPIC or x2APIC, "reset" the APIC after configuring loading the TSS. Previously, load_tss() => setup_tss() needed to run after forcing the vCPU into xAPIC mode due to pre_boot_apic_id() assuming xAPIC. The order doesn't truly matter at this point, but loading the TSS first will allow sharing code with the EFI boot flow, which "needs" to load the TSS (more specifically, needs to configure GS.base) prior to forcing the vCPU into xAPIC (and thus setting the per-vCPU APIC ops). Signed-off-by: Sean Christopherson --- x86/cstart64.S | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/x86/cstart64.S b/x86/cstart64.S index 7272452..5269424 100644 --- a/x86/cstart64.S +++ b/x86/cstart64.S @@ -188,8 +188,8 @@ save_id: retq ap_start64: - call reset_apic load_tss + call reset_apic call enable_apic call save_id call enable_x2apic @@ -201,8 +201,8 @@ ap_start64: jmp 1b start64: - call reset_apic load_tss + call reset_apic call mask_pic_interrupts call enable_apic call save_id