From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=134.134.136.65; helo=mga03.intel.com; envelope-from=jordan.l.justen@intel.com; receiver=edk2-devel@lists.01.org Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 4700E208AE908 for ; Sat, 16 Feb 2019 01:11:11 -0800 (PST) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 16 Feb 2019 01:11:11 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.58,376,1544515200"; d="scan'208";a="275525204" Received: from mmdandap-mobl1.amr.corp.intel.com (HELO localhost) ([10.254.8.66]) by orsmga004.jf.intel.com with ESMTP; 16 Feb 2019 01:11:10 -0800 MIME-Version: 1.0 In-Reply-To: References: <1274938c-dfdb-d6da-8caa-9fc6674eb1b1@Intel.com> Cc: "edk2-devel@lists.01.org" From: Jordan Justen To: "Ni, Ray" , Andrew Fish , Laszlo Ersek , Liu Yu Message-ID: <155030827034.1052.16419444135424097946@jljusten-skl> User-Agent: alot/0.8 Date: Sat, 16 Feb 2019 01:11:10 -0800 Subject: Re: EmulatorPkg Unix Host Segmentation fault. 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: Sat, 16 Feb 2019 09:11:12 -0000 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable On 2019-02-16 00:05:27, Ni, Ray wrote: > On 2/16/2019 3:43 PM, Ni, Ray wrote: > > (Sent third times to make sure Andrew and Laszlo are in the TO list.) > >=20 > > I also met this issue. > > I found three solutions: > > 1. Forcing PeiMain CC flag to "-O0" works. > > 2. Changing EmulatorPkg/Sec to not produce TemporaryRamSupportPpi also = > > works. > > 3. Implement the temporary migration routine as below in EmulatorPkg/Se= c=20 > > module. > >=20 > > EFI_STATUS > > EFIAPI > > SecTemporaryRamSupport ( > > =C2=A0 IN CONST EFI_PEI_SERVICES=C2=A0=C2=A0 **PeiServices, > > =C2=A0 IN EFI_PHYSICAL_ADDRESS=C2=A0=C2=A0=C2=A0=C2=A0 TemporaryMemory= Base, > > =C2=A0 IN EFI_PHYSICAL_ADDRESS=C2=A0=C2=A0=C2=A0=C2=A0 PermanentMemory= Base, > > =C2=A0 IN UINTN=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 CopySize > > =C2=A0 ) > > { > > =C2=A0 VOID=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 *OldHeap; > > =C2=A0 VOID=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 *NewHeap; > > =C2=A0 VOID=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 *OldStack; > > =C2=A0 VOID=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 *NewStack; > > =C2=A0 UINTN=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 StackMigrateOffset; > > =C2=A0 BASE_LIBRARY_JUMP_BUFFER=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0 JumpBuffer; > >=20 > > =C2=A0 DEBUG ((EFI_D_INFO, > > =C2=A0=C2=A0=C2=A0 "TemporaryRamMigration(0x%Lx, 0x%Lx, 0x%Lx)\n", > > =C2=A0=C2=A0=C2=A0 TemporaryMemoryBase, > > =C2=A0=C2=A0=C2=A0 PermanentMemoryBase, > > =C2=A0=C2=A0=C2=A0 (UINT64)CopySize > > =C2=A0=C2=A0=C2=A0 )); > >=20 > > =C2=A0 // > > =C2=A0 // Assume Host prepare the stack and heap in the temprary ram t= hat stack > > =C2=A0 // is below heap (stack is in smaller address). > > =C2=A0 // Stack/heap migration depends on the stack/heap location info= rmation > > =C2=A0 // in the temporary ram. > > =C2=A0 // > > =C2=A0 OldStack =3D (VOID*)(UINTN)TemporaryMemoryBase; > > =C2=A0 NewStack =3D (VOID*)((UINTN)PermanentMemoryBase); > >=20 > > =C2=A0 OldHeap =3D (VOID*)((UINTN)TemporaryMemoryBase + (CopySize >> 1= )); > > =C2=A0 NewHeap =3D (VOID*)((UINTN)PermanentMemoryBase + (CopySize >> 1= )); > >=20 > > =C2=A0 StackMigrateOffset =3D (UINTN)NewStack - (UINTN)OldStack; > >=20 > > =C2=A0 // > > =C2=A0 // Migrate Heap and Stack > > =C2=A0 // > > =C2=A0 CopyMem (NewHeap, OldHeap, CopySize >> 1); > > =C2=A0 CopyMem (NewStack, OldStack, CopySize >> 1); > >=20 > > =C2=A0 // > > =C2=A0 // Use SetJump()/LongJump() to switch to a new stack. > > =C2=A0 // > > =C2=A0 if (SetJump (&JumpBuffer) =3D=3D 0) { > > #if defined (MDE_CPU_IA32) > > =C2=A0=C2=A0=C2=A0 JumpBuffer.Esp =3D JumpBuffer.Esp + StackMigrateOff= set; > > =C2=A0=C2=A0=C2=A0 JumpBuffer.Ebp =3D JumpBuffer.Ebp + StackMigrateOff= set; > > #endif > > #if defined (MDE_CPU_X64) > > =C2=A0=C2=A0=C2=A0 JumpBuffer.Rsp =3D JumpBuffer.Rsp + StackMigrateOff= set; > > =C2=A0=C2=A0=C2=A0 JumpBuffer.Rbp =3D JumpBuffer.Rbp + StackMigrateOff= set; > > #endif > > =C2=A0=C2=A0=C2=A0 LongJump (&JumpBuffer, (UINTN)-1); > > =C2=A0 } > >=20 > > =C2=A0 ZeroMem ((VOID *)(UINTN) TemporaryMemoryBase, CopySize); > >=20 > > =C2=A0 return EFI_SUCCESS; > > } > >=20 > > Andrew, > > I'd like to know why you chose to produce the migration PPI from > > EmulatorPkg/Sec module. > > Based on PI spec and current PeiCore implementation, PeiCore can do the= =20 > > migration when PPI is absent. > >=20 > Study the PeiCore migration logic a bit more, I found since PeiCore=20 > knows the exact size of new stack in permanent memory, it migrates the=20 > old stack to the top of new stack. > But the migration logic in above C code (since it doesn't know the size=20 > of new stack, CopySize is the size of temporary memory) may copy the old = > stack to the middle in new stack. We had a fair amount of discussion about this in November. Search for "EmulatorPkg Unix" on https://lists.01.org/pipermail/edk2-devel/2018-November/thread.html. I have a branch related to that discussion: https://github.com/jljusten/edk2/tree/temp-ram-support2 There are essentially 2 parts to the branch. First is a fix for the PEI Core TemporaryRamMigration implementation. After the "MdeModulePkg/Core/Pei: Use assembly for X64 TemporaryRamMigration" patch on that branch, I was working on a new TemporaryRamMigration2 PPI that should fix the issues with TemporaryRamMigration. Of course, that would have to become part of the standard before EDK-II could add it. -Jordan