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::444; helo=mail-wr1-x444.google.com; envelope-from=leif.lindholm@linaro.org; receiver=edk2-devel@lists.01.org Received: from mail-wr1-x444.google.com (mail-wr1-x444.google.com [IPv6:2a00:1450:4864:20::444]) (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 E957521B02822 for ; Tue, 20 Nov 2018 06:48:57 -0800 (PST) Received: by mail-wr1-x444.google.com with SMTP id e3-v6so2273485wrs.5 for ; Tue, 20 Nov 2018 06:48:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=3214BW/E+GK/ORv9Nl3KqfAO2a7EEqB76LJd1x7Sc7o=; b=NIyGtvkQ+EYpEZSTBKzASwrRugjuXdfDQT+DHEztYgd1iir1rRn6UG58fYWEh63gsO roNHtF3xKr8FDuiFgNPx9pkLfoDZxvXdrstgzqn1zDUmOo8lFLoe60JXvaDooKb0MqlX u5LawX8b3SKop2ktcm2QZOd0hRUPVWsQdL+T4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=3214BW/E+GK/ORv9Nl3KqfAO2a7EEqB76LJd1x7Sc7o=; b=pPsd/3Dh3jP19JqShD/iVH/OPRlKNaiZ87Y4vO9ubfxBbuHxRRiaRHBF9IuClRpTNi D60gHjYBkggFFmHmN01NPWbfq/4wDFc4QgMl9sy+2PWVa+NQY5ycHS4VQom4CnNs8joZ vOGgIZbv2ZZ++Q8RNRXe9jcPJbxsqTPw2Soma+00RRA9u34s1EmT5+xOyAXdBGFigdfQ HDrdLo+jJ4LTeyRJwoOPFW/tMd16WXJvJMSeqDg4g+CIQLCEdqlYHPX2C4wgjcQUDSvf c9d2zaZ3r9mqtxtclL12a5F/LXIq6rvjBMGEC+XZrMZ50E4WdufsWQ3qjeSutki19Q/7 hSFQ== X-Gm-Message-State: AA+aEWbdlLs6B1L3r3KIYxe+Uab1Ase7LAVJINFVO8AYdQ09xH0WpOe6 eG+4UM4p+9V1v+sU5JniWHnA0JPx+yM= X-Google-Smtp-Source: AFSGD/UjIqnUk+MPShmtBPcWEmt43irjBhiiSliWXV0JZWgngRTrVWHiAcTl4M78P1IC9H4THvUpSg== X-Received: by 2002:adf:bb44:: with SMTP id x4mr2346811wrg.24.1542725336394; Tue, 20 Nov 2018 06:48:56 -0800 (PST) Received: from bivouac.eciton.net (bivouac.eciton.net. [2a00:1098:0:86:1000:23:0:2]) by smtp.gmail.com with ESMTPSA id x12sm20836382wrt.20.2018.11.20.06.48.55 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 20 Nov 2018 06:48:55 -0800 (PST) Date: Tue, 20 Nov 2018 14:48:54 +0000 From: Leif Lindholm To: Ard Biesheuvel Cc: edk2-devel@lists.01.org Message-ID: <20181120144854.2zjhyr3qrcm6hzhb@bivouac.eciton.net> References: <20181120143300.26751-1-ard.biesheuvel@linaro.org> MIME-Version: 1.0 In-Reply-To: <20181120143300.26751-1-ard.biesheuvel@linaro.org> User-Agent: NeoMutt/20170113 (1.7.2) Subject: Re: [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:48:58 -0000 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Tue, Nov 20, 2018 at 03:33:00PM +0100, Ard Biesheuvel wrote: > 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; Without the above hunk: Reviewed-by: Leif Lindholm > 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 >