From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=2a00:1450:4864:20::542; helo=mail-ed1-x542.google.com; envelope-from=ard.biesheuvel@linaro.org; receiver=edk2-devel@lists.01.org Received: from mail-ed1-x542.google.com (mail-ed1-x542.google.com [IPv6:2a00:1450:4864:20::542]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 0E48A21193595 for ; Tue, 20 Nov 2018 06:33:05 -0800 (PST) Received: by mail-ed1-x542.google.com with SMTP id x30so2118871edx.2 for ; Tue, 20 Nov 2018 06:33:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=omYC6H03Zl6GRTSYqWgPpv2S7sGzXTD8F3QggdI55rI=; b=JoEfPZkdbPNkEP+IMsc+mGLmOurAwnsFl1xffPudAj2d2+TAmR1+7RxUBDKNMgKZwQ V3r8AUnvXGIdYZhtslsC6D/h6xXG/rnAcp89DV6kohBwK5GaYw+tN3lja0f1CGcdNmnM cQzUWHnd6+NC/lm3YJ5StHyBFe8XJq1qveDNo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=omYC6H03Zl6GRTSYqWgPpv2S7sGzXTD8F3QggdI55rI=; b=LMhpWsOFWerzQDL59UdYlnhMOXvT7m6bzRI4K2r6/8oUxfeq6wV6jVM4Ybh90c8h/D 4IM0nomf5RFPaXWW7F0NtmxPIcHiDRNzxUhkXBj1E8e/Ti4HNsfM/SOcXsITg0ZfBMcv C8cux+ApBPyrQyW6zZ/xezikRACg1HrdqEcXlV2nJOf3ayy9ZCrfCkqz2cn+phtxN405 5t4HgsiJ7GAJUt37J78nFfn5Q9Z0whDmO/p/wkyth7kypc5j1z1hIaMLQLa0dDToFk8d 7hXubKxV7jJqml0k8l2Ghz9+BqNCtJgqkfIAZ7sWkZLtxuDYMP8vEfVHSpZjMOUCZ2NT LwGA== X-Gm-Message-State: AA+aEWYaADJsAh0IIjHqliccA5kbOj2smEYtqW1LEs4Ma4fNJhtUzQPR 8/ibHAFveJfT7k/OdUPl90n1wR5slk1yxA== X-Google-Smtp-Source: AFSGD/VPju1cmkvXEvMjwA1CB32jc9X5wof2p04A5ZoAohD+YzkQN20V29DPmD8W1qsrgqu6TckDbQ== X-Received: by 2002:a50:89e1:: with SMTP id h30mr2519324edh.28.1542724383879; Tue, 20 Nov 2018 06:33:03 -0800 (PST) Received: from mba13.wifi.ns.nl (dhcp-077-251-017-237.chello.nl. [77.251.17.237]) by smtp.gmail.com with ESMTPSA id d2sm7817233eda.92.2018.11.20.06.33.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 20 Nov 2018 06:33:02 -0800 (PST) From: Ard Biesheuvel To: edk2-devel@lists.01.org Date: Tue, 20 Nov 2018 15:33:00 +0100 Message-Id: <20181120143300.26751-1-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.1 Subject: [PATCH] ArmPkg/ArmSmcPsciResetSystemLib: add missing call to ExitBootServices() X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 20 Nov 2018 14:33:06 -0000 Our poor man's implementation of EnterS3WithImmediateWake () currently sets a high TPL level to disable interrupts, and simply calls the PEI entrypoint again after disabling the MMU. Unfortunately, this is not sufficient: DMA capable devices such as network controllers or USB controllers may still be enabled and writing to memory, e.g., in response to incoming network packets. So instead, do the full ExitBootServices() dance: allocate space and get the memory map, call ExitBootServices(), and in case it fails, get the memory map again and call ExitBootServices() again. This ensures that all cleanup related to DMA capable devices is performed before doing the warm reset. Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Ard Biesheuvel --- ArmPkg/Include/Library/ArmLib.h | 1 + ArmPkg/Library/ArmSmcPsciResetSystemLib/ArmSmcPsciResetSystemLib.c | 51 ++++++++++++++++++-- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/ArmPkg/Include/Library/ArmLib.h b/ArmPkg/Include/Library/ArmLib.h index e46df447b33d..ffda50e9d767 100644 --- a/ArmPkg/Include/Library/ArmLib.h +++ b/ArmPkg/Include/Library/ArmLib.h @@ -59,6 +59,7 @@ typedef enum { typedef struct { EFI_PHYSICAL_ADDRESS PhysicalBase; + EFI_VIRTUAL_ADDRESS VirtualBase; UINT64 Length; ARM_MEMORY_REGION_ATTRIBUTES Attributes; } ARM_MEMORY_REGION_DESCRIPTOR; diff --git a/ArmPkg/Library/ArmSmcPsciResetSystemLib/ArmSmcPsciResetSystemLib.c b/ArmPkg/Library/ArmSmcPsciResetSystemLib/ArmSmcPsciResetSystemLib.c index 10ceafd14d5d..c9c42ab3b244 100644 --- a/ArmPkg/Library/ArmSmcPsciResetSystemLib/ArmSmcPsciResetSystemLib.c +++ b/ArmPkg/Library/ArmSmcPsciResetSystemLib/ArmSmcPsciResetSystemLib.c @@ -1,7 +1,7 @@ /** @file ResetSystemLib implementation using PSCI calls - Copyright (c) 2017, Linaro Ltd. All rights reserved.
+ Copyright (c) 2017 - 2018, Linaro Ltd. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License @@ -92,7 +92,13 @@ EnterS3WithImmediateWake ( VOID ) { - VOID (*Reset)(VOID); + VOID (*Reset)(VOID); + EFI_PHYSICAL_ADDRESS Alloc; + EFI_MEMORY_DESCRIPTOR *MemMap; + UINTN MemMapSize; + UINTN MapKey, DescriptorSize; + UINT32 DescriptorVersion; + EFI_STATUS Status; if (FeaturePcdGet (PcdArmReenterPeiForCapsuleWarmReboot) && !EfiAtRuntime ()) { @@ -103,7 +109,46 @@ EnterS3WithImmediateWake ( // Reset = (VOID (*)(VOID))(UINTN)FixedPcdGet64 (PcdFvBaseAddress); - gBS->RaiseTPL (TPL_HIGH_LEVEL); + // + // Obtain the size of the memory map + // + MemMapSize = 0; + Status = gBS->GetMemoryMap (&MemMapSize, MemMap, &MapKey, &DescriptorSize, + &DescriptorVersion); + ASSERT (Status == EFI_BUFFER_TOO_SMALL); + + // + // Add some slack to the allocation to cater for changes in the memory + // map if ExitBootServices () fails the first time around. + // + MemMapSize += SIZE_4KB; + Status = gBS->AllocatePages (AllocateAnyPages, EfiBootServicesData, + EFI_SIZE_TO_PAGES (MemMapSize), &Alloc); + ASSERT_EFI_ERROR (Status); + + MemMap = (EFI_MEMORY_DESCRIPTOR *)(UINTN)Alloc; + + Status = gBS->GetMemoryMap (&MemMapSize, MemMap, &MapKey, &DescriptorSize, + &DescriptorVersion); + ASSERT_EFI_ERROR (Status); + + Status = gBS->ExitBootServices (gImageHandle, MapKey); + if (EFI_ERROR (Status)) { + // + // ExitBootServices () may fail the first time around if an event fired + // right after the call to GetMemoryMap() which allocated or freed memory. + // Since that first call to ExitBootServices () will disarm the timer, + // this is guaranteed not to happen again, so one additional attempt + // should suffice. + // + Status = gBS->GetMemoryMap (&MemMapSize, MemMap, &MapKey, &DescriptorSize, + &DescriptorVersion); + ASSERT_EFI_ERROR (Status); + + Status = gBS->ExitBootServices (gImageHandle, MapKey); + ASSERT_EFI_ERROR (Status); + } + ArmDisableMmu (); Reset (); } -- 2.17.1