From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from userp2130.oracle.com (userp2130.oracle.com [156.151.31.86]) by mx.groups.io with SMTP id smtpd.web12.31932.1613978394304557488 for ; Sun, 21 Feb 2021 23:19:54 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@oracle.com header.s=corp-2020-01-29 header.b=X3wTI3cx; spf=pass (domain: oracle.com, ip: 156.151.31.86, mailfrom: ankur.a.arora@oracle.com) Received: from pps.filterd (userp2130.oracle.com [127.0.0.1]) by userp2130.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 11M7FHiC133455; Mon, 22 Feb 2021 07:19:51 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2020-01-29; bh=aeYb9WJVc+e89pfAheC5+u1WtURwD/O9xE9kRRuwKoo=; b=X3wTI3cxoc//GwRM61ckuQOOXWoCsPaolzPojiuXXacX1Uw132kp4MlPsAf2TXb5gvpJ AKkzqfMpq13BpcdP9TWhEsD8AdxuuqIx4iY8tig1WLB8Hr51E7ChUcPBW8XVhALjItW+ dFX/w0pB/MzY+p6QjXEZm84AEcdmFPyxuSxjcA8taa6QfjWV4RsznwtOqb468u6LYEcn knTHcwl8Ls4b3PdWzdvg+W8pITtQRkA+lAECuOgW9hkHwOoeBwzYP8DkGuluQgXjWamy MS9CnxCn5nTegoFM4EkFWeM0bMfyXGd6GathEdz1F9amXr3TtOJ0peLNZg5OqLwjanjy LA== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by userp2130.oracle.com with ESMTP id 36tsuqtku3-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 22 Feb 2021 07:19:51 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 11M7GSLA067574; Mon, 22 Feb 2021 07:19:50 GMT Received: from nam11-dm6-obe.outbound.protection.outlook.com (mail-dm6nam11lp2168.outbound.protection.outlook.com [104.47.57.168]) by aserp3020.oracle.com with ESMTP id 36ucawnxq0-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 22 Feb 2021 07:19:50 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mA9u9CDd5n1fR0EAYPd/x7nqZ0iTKqlSnNQZLqd6xotZ3g94VQT3RU33ypL/pvD0AVANXCjo/ThcVu3GVDbXWLW8cpsQCOePC1ONujM/4p5AaSexUxoh31rMyhAnWaEAWNh782bO7z+sjIOfF9XQPYl7EsZR3P1OkG+L13T2/UCV4ho4fhhiRfNEwUItmlV3b/FiVqVjRgHSaOo9q/4pWGzmWL6kkG0UTiXFphIwgOIF7epVCzdP0wIrdZVT+/2xyjjykLc+ybaPsLFJr8E/VyUobBVdcVEh6pMhvF63ZEIhd/YHopGR+zo9k+ITueIhvg7PENHQKSn3wGY3kd4Chg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=aeYb9WJVc+e89pfAheC5+u1WtURwD/O9xE9kRRuwKoo=; b=WpPf/hVP1eYgsFNJbIx0avrK2CfV9Nb3BWmAfHzad/0RV6ofgbAds0+/JcnJrjrLBBPxGp1vf74nWyl4kbew34jdG/XItqM215S5wFkL8tfQ82AlHBbU3Q8jdux4+zjOj5PHkhkUU9Xq5hTphIgFUJXse/9YyPfo+9+fq1sFiMfvjqLn1iCbpqM+RRsETQP6NSjop93I4PuM0I5q283Q6aG55HEcihFDtF/mVF1sxgkWZsmBnOBndDvPOrEyY1T2eUC4ufjHVpmpLvHMAcdpw+DYKU8u2iCIIv3LnISjuaeKPrdvNsIeGH5/pO1ug40cFFXt+ZxwFBNsqp5RhgiWDg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=aeYb9WJVc+e89pfAheC5+u1WtURwD/O9xE9kRRuwKoo=; b=s8kZ4hQdbNmmA6LnPPj8EYRt+gtplpPeEyZRYXdblSZ88qvd9HUyUxM0ep6BQ9t1zq6tZS9QWxF1cpQfwgYizR8M5mCE1fGe5tjotjUuu6P2jWHvnkugw5Eg4+UgpiG4oBkVNlA2916ckgmY733CDwwvFeWX8Ruza9bzJRSr+FI= Authentication-Results: edk2.groups.io; dkim=none (message not signed) header.d=none;edk2.groups.io; dmarc=none action=none header.from=oracle.com; Received: from SJ0PR10MB4605.namprd10.prod.outlook.com (2603:10b6:a03:2d9::24) by BY5PR10MB3922.namprd10.prod.outlook.com (2603:10b6:a03:1fd::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3868.32; Mon, 22 Feb 2021 07:19:47 +0000 Received: from SJ0PR10MB4605.namprd10.prod.outlook.com ([fe80::a021:790:7ce6:6f16]) by SJ0PR10MB4605.namprd10.prod.outlook.com ([fe80::a021:790:7ce6:6f16%6]) with mapi id 15.20.3868.033; Mon, 22 Feb 2021 07:19:47 +0000 From: "Ankur Arora" To: devel@edk2.groups.io Cc: lersek@redhat.com, imammedo@redhat.com, boris.ostrovsky@oracle.com, Ankur Arora , Jordan Justen , Ard Biesheuvel , Aaron Young Subject: [PATCH v8 08/10] OvmfPkg/CpuHotplugSmm: add EjectCpu() Date: Sun, 21 Feb 2021 23:19:26 -0800 Message-Id: <20210222071928.1401820-9-ankur.a.arora@oracle.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210222071928.1401820-1-ankur.a.arora@oracle.com> References: <20210222071928.1401820-1-ankur.a.arora@oracle.com> X-Originating-IP: [148.87.23.11] X-ClientProxiedBy: MWHPR17CA0065.namprd17.prod.outlook.com (2603:10b6:300:93::27) To SJ0PR10MB4605.namprd10.prod.outlook.com (2603:10b6:a03:2d9::24) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from prion.us.oracle.com (148.87.23.11) by MWHPR17CA0065.namprd17.prod.outlook.com (2603:10b6:300:93::27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3868.27 via Frontend Transport; Mon, 22 Feb 2021 07:19:46 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 4c6e9a39-95b3-4863-4a97-08d8d7023c25 X-MS-TrafficTypeDiagnostic: BY5PR10MB3922: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:4303; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: WvYVoJh1TnC5Oo2oft+uVZhxPtm+owhwNcc2xUXJbTQG4YU+X6xf3/3tB2Hx3wT2QuU0izbbZn03ILsYDb5fvvhQ7m+9DwBetujPPq8+T+Lw4CA7fjmvYpazzClEufeM93HHB+KWTRjfp6gzRdnRPQYZaHBm0+foO979oqO9KF3e5kVsbiclOs2f/j4nwqJAV3++vIojXX0cQ/XyXMRC+PFS/5Z+IZRUYCmlI8yJpYITAoXG8tYXkZ0yLBrFJuMprtE7vT0GrWCuu7Jr3ogWS+KNoxUY4OTFr+S1NGLZHnvwvh/fs8DpxJAYNxYNtFPAAomyMSHNOBzp0jYtLW9yjjQuRa4xlDtx3vc6kYc/DikC3961+vWN8QzrkkOQfv0udVBEPR7aj7CbZ46QjIZj1FAaDRNfEcDHDh98lUfTwIDOTmHZnVw4NHmsPk4WmP7SHg2OmeTupLJOdEtdZZNJWn1E7dfxDBnr6Agf3LtKVeSyvFkzlq9zu+BnnFD+YqqzYwH0BxKk0BXa931UmH1f+eWOUU7TCwvwFLxNbPM/ihD2w5mwbUcKXIvgu+ihLT7gk9l3YCM8A0Gq/2nR0kuwC18YfgwvRAvalEnoOFrxmtc= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SJ0PR10MB4605.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(396003)(376002)(39860400002)(346002)(136003)(366004)(6666004)(6486002)(6916009)(2906002)(86362001)(52116002)(1076003)(7696005)(316002)(36756003)(107886003)(4326008)(26005)(478600001)(2616005)(8676002)(8936002)(966005)(16526019)(5660300002)(186003)(54906003)(103116003)(66476007)(66556008)(66946007)(83380400001)(30864003)(956004);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: =?us-ascii?Q?EDrBiyphfSp/noJjxurYp2QGvNS50cL5ffuEI0oW/M6fYcpv5GqcLwxUHPRY?= =?us-ascii?Q?LkNUlobrV3pe/acB1Aj+61CRJbRr3ZYMc1VFCPxYhJ8S7NhEqW4DmQDOX/UH?= =?us-ascii?Q?GPpoRxJXGkLZ1n/k5x4lTlWiKTparkAeQrOuHKITJpwOUZYuHBMD+s/KmsXb?= =?us-ascii?Q?m7X7Na02w2KVbzmyyz49bg5KHsYb5ova127ruop7gvoYSf3FWhgbfWQ1AFHO?= =?us-ascii?Q?twXonr8xK9GPDvU1lOpUO657H/mKXu/XtfwnAqBOyA/7DHiKG7MMK26dtdBM?= =?us-ascii?Q?lIylfDn7vkGNNdIXWMNT2MvXqwwLeF3TwcyTPSAY2pyke6rFrW5kFJzs/Nhg?= =?us-ascii?Q?qfrJBvICGe1FkxJMGittE1I69tZRYjyZSWdFCGPoYRTKEDThUrrf3QD8Zkd+?= =?us-ascii?Q?HwJIf4zLyNW+sh1RYgWQeN6/oYhXXagy0hlA/ZHdVjmKz6uRmv6Dh8FCS5Kd?= =?us-ascii?Q?wgla5AzbBJ4H2OlWHDrOZQ1LgSoVC+ZRPpXpQOJulC2iOMrbQtzHOsDT5Naw?= =?us-ascii?Q?W0pmT5vQhs00iKn/TTFrmGp3xKhHcE9CzfrtmPjGWlfOmPqMV0NlkLvjx4qi?= =?us-ascii?Q?+GhMClOjXH2ef0NIGr6OhETFivU6zSGYyC8J1jH6MG4BFU4fh/hdoMRt9GuZ?= =?us-ascii?Q?+bC5HbyAN5UkezH0is/fDwmWsXirFmXnwmBSIuaDqUTvP8Tnx5DVsv5GITf/?= =?us-ascii?Q?MB/j8C8dxrZAIu7DvvZDwgDnCLuJyIr964SAeGzFrnoVO9x46tvPQCwUmjOA?= =?us-ascii?Q?eju8b94qhQgwA65XhAQg15Cvjpalxz2Yap+dm8LR4igQuNYYhhPew9kRi0rH?= =?us-ascii?Q?/sdBckLgOmlhpkNwcOh9eHLQExmggJEt6p4hnROeqVodm14byK/M/Zs5CGpN?= =?us-ascii?Q?vGtXRE1NFBUgO6Qjr16hJd6uj56gvxDdSs1pvtBhD3HURa82LLIoPp5fxyh3?= =?us-ascii?Q?B+M+/DC+matw/BW/Y5ZkmJNEWEwDQCmwRrH3p6V6PQ+gwdVpBN1AXT7Zdlh0?= =?us-ascii?Q?J0rZeidmg2+iXdD/m8lsxOw+I4YFETJCnue4dObIvRqcrgezkCITsWv/CRUU?= =?us-ascii?Q?coGEhYIlJH6PTye3C+yfD9aQt3qhKWtJ9Gr2G21rLHW0fXI2Zv5TJGaTrrcU?= =?us-ascii?Q?U0T6zdHRiTQuHoMFkCOxbaJr549EcI3Dslypr0L3Y9LPwPKj+RPEb5y9YWRb?= =?us-ascii?Q?u4KSI2IqJL7WHlvUkQIbYREbOrmPJkQr9ounUT43otyxzdT1fGU0g41yLVlG?= =?us-ascii?Q?oO1/DUGICUVssY3zxeCTnkw5+dijvds2Sg2IFafH+J14YRaRT/xZY7z+pwgj?= =?us-ascii?Q?6jOYSkNs9m4XVgy4WoKwWcfu?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4c6e9a39-95b3-4863-4a97-08d8d7023c25 X-MS-Exchange-CrossTenant-AuthSource: SJ0PR10MB4605.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Feb 2021 07:19:47.3362 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: z79A0RWKVUHdin8eFACqDFFdsMhkYTjIqMT5DDS+D2aKkSvdDctbvMP1ao7YgtkK7u6z2M+khLHZWZoHZ0k5dN8Cuy92onMF+wcmJdDXFrI= X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR10MB3922 X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=9902 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 suspectscore=0 malwarescore=0 mlxlogscore=999 adultscore=0 bulkscore=0 mlxscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2102220064 X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=9902 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 spamscore=0 priorityscore=1501 impostorscore=0 bulkscore=0 mlxscore=0 malwarescore=0 clxscore=1015 phishscore=0 mlxlogscore=999 lowpriorityscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2102220064 Content-Transfer-Encoding: 8bit Content-Type: text/plain Add EjectCpu(), which handles the CPU ejection, and provides a holding area for said CPUs. It is called via SmmCpuFeaturesRendezvousExit(), at the tail end of the SMI handling. Also UnplugCpus() now stashes QEMU Selectors of CPUs which need to be ejected in CPU_HOT_EJECT_DATA.QemuSelectorMap. This is used by EjectCpu() to identify CPUs marked for ejection. Cc: Laszlo Ersek Cc: Jordan Justen Cc: Ard Biesheuvel Cc: Igor Mammedov Cc: Boris Ostrovsky Cc: Aaron Young Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3132 Signed-off-by: Ankur Arora --- Notes: Address these review comments from v6: (1) s/CpuEject/EjectCpu/g (2) Ensure that the added include is in sorted order. (3) Switch to a cheaper CpuSleep() based loop instead of CpuDeadLoop(). Also add the CpuLib LibraryClass. (4) Remove the nested else clause (5) Use Laszlo's much clearer comment when we try to map multiple QemuSelector to the same ProcessorNum. (6a) Fix indentation of the debug print in the block in (5). (6b,6c,6d) Fix printf types for ProcessorNum, use FMT_APIC_ID for APIC_ID and 0x%Lx for QemuSelector[]. () As discussed elsewhere add an DEBUG_INFO print logging the correspondence between ProcessorNum, APIC_ID, QemuSelector. (7a,7b) Use EFI_ALREADY_STARTED instead of EFI_INVALID_PARAMETER and document it in the UnplugCpus() comment block. () As discussed elsewhere, add the import statement for PcdCpuHotEjectDataAddress. (9) Use Laszlo's comment in the PcdGet64(PcdCpuHotEjectDataAddress) description block. (10) Change mCpuHotEjectData init state checks from ASSERT to ones consistent with similar checks for mCpuHotPlugData. (11-14) Get rid of mCpuHotEjectData init loop: moved to a prior patch so it can be done at allocation time. (15) s/SmmCpuFeaturesSmiRendezvousExit/SmmCpuFeaturesRendezvousExit/ (16,17) Document the ordering requirements of mCpuHotEjectData->Handler, and mCpuHotEjectData->QemuSelectorMap. Not addressed: (8) Not removing the EjectCount variable as I'd like to minimize stores/loads to CPU_HOT_EJECT_DATA->Handler and so would like to do this a single time at the end of the iteration. (It is safe to write multiple times to the handler in UnplugCpus() but given the ordering concerns around it, it seems cleaner to not access it unnecessarily.) OvmfPkg/CpuHotplugSmm/CpuHotplugSmm.inf | 2 + OvmfPkg/CpuHotplugSmm/CpuHotplug.c | 157 ++++++++++++++++++++++++++++++-- 2 files changed, 151 insertions(+), 8 deletions(-) diff --git a/OvmfPkg/CpuHotplugSmm/CpuHotplugSmm.inf b/OvmfPkg/CpuHotplugSmm/CpuHotplugSmm.inf index 04322b0d7855..ebcc7e2ac63a 100644 --- a/OvmfPkg/CpuHotplugSmm/CpuHotplugSmm.inf +++ b/OvmfPkg/CpuHotplugSmm/CpuHotplugSmm.inf @@ -40,6 +40,7 @@ [Packages] [LibraryClasses] BaseLib BaseMemoryLib + CpuLib DebugLib LocalApicLib MmServicesTableLib @@ -54,6 +55,7 @@ [Protocols] [Pcd] gUefiCpuPkgTokenSpaceGuid.PcdCpuHotPlugDataAddress ## CONSUMES + gUefiOvmfPkgTokenSpaceGuid.PcdCpuHotEjectDataAddress ## CONSUMES gUefiOvmfPkgTokenSpaceGuid.PcdQ35SmramAtDefaultSmbase ## CONSUMES [FeaturePcd] diff --git a/OvmfPkg/CpuHotplugSmm/CpuHotplug.c b/OvmfPkg/CpuHotplugSmm/CpuHotplug.c index f07b5072749a..851e2b28aad9 100644 --- a/OvmfPkg/CpuHotplugSmm/CpuHotplug.c +++ b/OvmfPkg/CpuHotplugSmm/CpuHotplug.c @@ -10,10 +10,12 @@ #include // ICH9_APM_CNT #include // QEMU_CPUHP_CMD_GET_PENDING #include // CpuDeadLoop() +#include // CpuSleep() #include // ASSERT() #include // gMmst #include // PcdGetBool() #include // SafeUintnSub() +#include // CPU_HOT_EJECT_DATA #include // EFI_MM_CPU_IO_PROTOCOL #include // EFI_SMM_CPU_SERVICE_PROTOCOL #include // EFI_STATUS @@ -32,11 +34,12 @@ STATIC EFI_MM_CPU_IO_PROTOCOL *mMmCpuIo; // STATIC EFI_SMM_CPU_SERVICE_PROTOCOL *mMmCpuService; // -// This structure is a communication side-channel between the +// These structures serve as communication side-channels between the // EFI_SMM_CPU_SERVICE_PROTOCOL consumer (i.e., this driver) and provider // (i.e., PiSmmCpuDxeSmm). // STATIC CPU_HOT_PLUG_DATA *mCpuHotPlugData; +STATIC CPU_HOT_EJECT_DATA *mCpuHotEjectData; // // SMRAM arrays for fetching the APIC IDs of processors with pending events (of // known event types), for the time of just one MMI. @@ -188,18 +191,72 @@ RevokeNewSlot: } /** + CPU Hot-eject handler, called from SmmCpuFeaturesRendezvousExit() + on each CPU at exit from SMM. + + If, the executing CPU is not being ejected, nothing to be done. + If, the executing CPU is being ejected, wait in a halted loop + until ejected. + + @param[in] ProcessorNum ProcessorNum denotes the CPU exiting SMM, + and will be used as an index into + CPU_HOT_EJECT_DATA->QemuSelectorMap. It is + identical to the processor handle number in + EFI_SMM_CPU_SERVICE_PROTOCOL. +**/ +VOID +EFIAPI +EjectCpu ( + IN UINTN ProcessorNum + ) +{ + UINT64 QemuSelector; + + QemuSelector = mCpuHotEjectData->QemuSelectorMap[ProcessorNum]; + if (QemuSelector == CPU_EJECT_QEMU_SELECTOR_INVALID) { + return; + } + + // + // CPU(s) being unplugged get here from SmmCpuFeaturesRendezvousExit() + // after having been cleared to exit the SMI by the BSP and thus have + // no SMM processing remaining. + // + // Given that we cannot allow them to escape to the guest, we pen them + // here until the BSP tells QEMU to unplug them. + // + for (;;) { + DisableInterrupts (); + CpuSleep (); + } +} + +/** Process to be hot-unplugged CPUs, per QemuCpuhpCollectApicIds(). For each such CPU, report the CPU to PiSmmCpuDxeSmm via - EFI_SMM_CPU_SERVICE_PROTOCOL. If the to be hot-unplugged CPU is - unknown, skip it silently. + EFI_SMM_CPU_SERVICE_PROTOCOL and stash the APIC ID for later ejection. + If the to be hot-unplugged CPU is unknown, skip it silently. + + Additonally, if we do stash any APIC IDs, also install a CPU eject handler + which would handle the ejection. @param[in] ToUnplugApicIds The APIC IDs of the CPUs that are about to be hot-unplugged. + @param[in] ToUnplugSelector The QEMU Selectors of the CPUs that are about to + be hot-unplugged. + @param[in] ToUnplugCount The number of filled-in APIC IDs in ToUnplugApicIds. + @retval EFI_ALREADY_STARTED For the ProcessorNum that + EFI_SMM_CPU_SERVICE_PROTOCOL had assigned to + one of the APIC ID in ToUnplugApicIds, + mCpuHotEjectData->QemuSelectorMap already has + the QemuSelector value stashed. (This should + never happen.) + @retval EFI_SUCCESS Known APIC IDs have been removed from SMM data structures. @@ -210,23 +267,36 @@ STATIC EFI_STATUS UnplugCpus ( IN APIC_ID *ToUnplugApicIds, + IN UINT32 *ToUnplugSelector, IN UINT32 ToUnplugCount ) { EFI_STATUS Status; UINT32 ToUnplugIdx; + UINT32 EjectCount; UINTN ProcessorNum; ToUnplugIdx = 0; + EjectCount = 0; while (ToUnplugIdx < ToUnplugCount) { APIC_ID RemoveApicId; + UINT32 QemuSelector; RemoveApicId = ToUnplugApicIds[ToUnplugIdx]; + QemuSelector = ToUnplugSelector[ToUnplugIdx]; // - // mCpuHotPlugData->ApicId maps ProcessorNum -> ApicId. Use it to find - // the ProcessorNum for the APIC ID to be removed. + // mCpuHotPlugData->ApicId maps ProcessorNum -> ApicId. Use RemoveApicId + // to find the corresponding ProcessorNum for the CPU to be removed. // + // With this we can establish a 3 way mapping: + // APIC_ID -- ProcessorNum -- QemuSelector + // + // We stash the ProcessorNum -> QemuSelector mapping so it can later be + // used for CPU hot-eject in SmmCpuFeaturesRendezvousExit() context (where + // we only have ProcessorNum available.) + // + for (ProcessorNum = 0; ProcessorNum < mCpuHotPlugData->ArrayLength; ProcessorNum++) { @@ -255,11 +325,64 @@ UnplugCpus ( return Status; } + if (mCpuHotEjectData->QemuSelectorMap[ProcessorNum] != + CPU_EJECT_QEMU_SELECTOR_INVALID) { + // + // mCpuHotEjectData->QemuSelectorMap[ProcessorNum] is set to + // CPU_EJECT_QEMU_SELECTOR_INVALID when mCpuHotEjectData->QemuSelectorMap + // is allocated, and once the subject processsor is ejected. + // + // Additionally, mMmCpuService->RemoveProcessor(ProcessorNum) invalidates + // mCpuHotPlugData->ApicId[ProcessorNum], so a given ProcessorNum can + // never match more than one APIC ID and by transitivity, more than one + // QemuSelector in a single invocation of UnplugCpus(). + // + DEBUG ((DEBUG_ERROR, "%a: ProcessorNum %Lu maps to QemuSelector 0x%Lx, " + "cannot also map to 0x%Lx\n", __FUNCTION__, (UINT64)ProcessorNum, + (UINT64)mCpuHotEjectData->QemuSelectorMap[ProcessorNum], QemuSelector)); + + Status = EFI_ALREADY_STARTED; + return Status; + } + + // + // Stash the QemuSelector so we can do the actual ejection later. + // + mCpuHotEjectData->QemuSelectorMap[ProcessorNum] = (UINT64)QemuSelector; + + DEBUG ((DEBUG_INFO, "%a: Started hot-unplug on ProcessorNum %Lu, APIC ID " + FMT_APIC_ID ", QemuSelector 0x%Lx\n", __FUNCTION__, (UINT64)ProcessorNum, + RemoveApicId, mCpuHotEjectData->QemuSelectorMap[ProcessorNum])); + + EjectCount++; ToUnplugIdx++; } + if (EjectCount != 0) { + // + // We have processors to be ejected; install the handler. + // + mCpuHotEjectData->Handler = EjectCpu; + + // + // The BSP, CPUs to be ejected dereference mCpuHotEjectData->Handler, and + // mCpuHotEjectData->QemuSelectorMap[] in SmmCpuFeaturesRendezvousExit(). + // + // Assignments to both of these are ordered-before the BSP's SMI exit signal + // which happens via a write to SMM_DISPATCHER_MP_SYNC_DATA->AllCpusInSync. + // Dereferences of both are ordered-after the synchronization via + // "AllCpusInSync". + // + // So we are guaranteed that the Handler would see the assignments above. + // However, add a MemoryFence() here in-lieu of a compiler barrier to + // ensure that the compiler doesn't monkey around with the stores. + // + MemoryFence (); + } + // - // We've removed this set of APIC IDs from SMM data structures. + // We've removed this set of APIC IDs from SMM data structures and + // have installed an ejection handler if needed. // return EFI_SUCCESS; } @@ -387,7 +510,7 @@ CpuHotplugMmi ( } if (ToUnplugCount > 0) { - Status = UnplugCpus (mToUnplugApicIds, ToUnplugCount); + Status = UnplugCpus (mToUnplugApicIds, mToUnplugSelector, ToUnplugCount); if (EFI_ERROR (Status)) { goto Fatal; } @@ -458,9 +581,14 @@ CpuHotplugEntry ( // // Our DEPEX on EFI_SMM_CPU_SERVICE_PROTOCOL guarantees that PiSmmCpuDxeSmm - // has pointed PcdCpuHotPlugDataAddress to CPU_HOT_PLUG_DATA in SMRAM. + // has pointed: + // - PcdCpuHotPlugDataAddress to CPU_HOT_PLUG_DATA in SMRAM, + // - PcdCpuHotEjectDataAddress to CPU_HOT_EJECT_DATA in SMRAM, if the + // possible CPU count is greater than 1. // mCpuHotPlugData = (VOID *)(UINTN)PcdGet64 (PcdCpuHotPlugDataAddress); + mCpuHotEjectData = (VOID *)(UINTN)PcdGet64 (PcdCpuHotEjectDataAddress); + if (mCpuHotPlugData == NULL) { Status = EFI_NOT_FOUND; DEBUG ((DEBUG_ERROR, "%a: CPU_HOT_PLUG_DATA: %r\n", __FUNCTION__, Status)); @@ -472,6 +600,19 @@ CpuHotplugEntry ( if (mCpuHotPlugData->ArrayLength == 1) { return EFI_UNSUPPORTED; } + + if (mCpuHotEjectData == NULL) { + Status = EFI_NOT_FOUND; + } else if (mCpuHotPlugData->ArrayLength != mCpuHotEjectData->ArrayLength) { + Status = EFI_INVALID_PARAMETER; + } else { + Status = EFI_SUCCESS; + } + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a: CPU_HOT_EJECT_DATA: %r\n", __FUNCTION__, Status)); + goto Fatal; + } + // // Allocate the data structures that depend on the possible CPU count. // -- 2.9.3