Re: [edk2-devel] Is Xcode5ExceptionHandlerAsm.nasm still needed?

2023-03-30 Thread Ard Biesheuvel
On Thu, 30 Mar 2023 at 18:54, Rebecca Cran  wrote:
>
> On 3/30/23 10:33 AM, Ard Biesheuvel wrote:
> > i'll respin the series I sent out earlier today with that if I manage
> > to build a working OVMF.fd with XCODE5
>
> Let me know if you'd like any help test building/running anything with
> XCODE5 on an Intel or ARM Mac.
>
> I have both a 2012 era Intel Mac Pro and an M1 MacBook Pro.
>

Ah excellent - thanks.

Once I make a bit more progress, I'll cc you [again] on my v2 series,
and I'd appreciate it if you could give that a spin.


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#102199): https://edk2.groups.io/g/devel/message/102199
Mute This Topic: https://groups.io/mt/97947477/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] Is Xcode5ExceptionHandlerAsm.nasm still needed?

2023-03-30 Thread Rebecca Cran

On 3/30/23 10:33 AM, Ard Biesheuvel wrote:

i'll respin the series I sent out earlier today with that if I manage
to build a working OVMF.fd with XCODE5


Let me know if you'd like any help test building/running anything with 
XCODE5 on an Intel or ARM Mac.


I have both a 2012 era Intel Mac Pro and an M1 MacBook Pro.


--
Rebecca Cran



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#102197): https://edk2.groups.io/g/devel/message/102197
Mute This Topic: https://groups.io/mt/97947477/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] Is Xcode5ExceptionHandlerAsm.nasm still needed?

2023-03-30 Thread Ard Biesheuvel
On Thu, 30 Mar 2023 at 18:33, Ard Biesheuvel  wrote:
>
> On Thu, 30 Mar 2023 at 17:04, Ard Biesheuvel  wrote:
> >
> > On Thu, 30 Mar 2023 at 16:25, Ni, Ray  wrote:
> > >
> > > I am afraid they are not template code.
> > > That means if nx is set for data section, they can not be executed.
> > >
> >
> > Currently, we fix up the entries by writing to the .text section at
> > runtime, so NX seems out of scope in any case.
> >
> > Someone should check Xcode5, but for LLD, adding  -Wl,-z,notext is
> > sufficient to work around the error.
>
> As far as I could figure out, '-read_only_relocs suppress' does the
> same thing as '-z notext' on the GNU linker, and permits the code to
> be built with the absolute relocations in the .text section.
>
> AFAICT, this means we don't need the runtime fixups, nor do we need to
> move that code out of .text
>
> i'll respin the series I sent out earlier today with that if I manage
> to build a working OVMF.fd with XCODE5

Hmm seems we are already using that for XCODE5.


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#102196): https://edk2.groups.io/g/devel/message/102196
Mute This Topic: https://groups.io/mt/97947477/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] Is Xcode5ExceptionHandlerAsm.nasm still needed?

2023-03-30 Thread Ard Biesheuvel
On Thu, 30 Mar 2023 at 17:04, Ard Biesheuvel  wrote:
>
> On Thu, 30 Mar 2023 at 16:25, Ni, Ray  wrote:
> >
> > I am afraid they are not template code.
> > That means if nx is set for data section, they can not be executed.
> >
>
> Currently, we fix up the entries by writing to the .text section at
> runtime, so NX seems out of scope in any case.
>
> Someone should check Xcode5, but for LLD, adding  -Wl,-z,notext is
> sufficient to work around the error.

As far as I could figure out, '-read_only_relocs suppress' does the
same thing as '-z notext' on the GNU linker, and permits the code to
be built with the absolute relocations in the .text section.

AFAICT, this means we don't need the runtime fixups, nor do we need to
move that code out of .text

i'll respin the series I sent out earlier today with that if I manage
to build a working OVMF.fd with XCODE5


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#102195): https://edk2.groups.io/g/devel/message/102195
Mute This Topic: https://groups.io/mt/97947477/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] Is Xcode5ExceptionHandlerAsm.nasm still needed?

2023-03-30 Thread Michael D Kinney
Hi Ray,

No sure if I recall all the details.  Andrew may know them much better than me.

The code gen for XCODE uses PIE and RIP relative addressing, which does not 
require the same number/type of relocation fixups.

Whenever there is a need to access a data element at an absolute address and 
the code is copied to
a new location the RIP relative address will not work.

And if you try to use absolute addressing, you will get an error from XCODE 
linker for an unsupported fixup type.

Mike

From: Ni, Ray 
Sent: Thursday, March 30, 2023 3:16 AM
To: Andrew Fish 
Cc: Kinney, Michael D ; devel@edk2.groups.io; Liu, 
Zhiguang 
Subject: Is Xcode5ExceptionHandlerAsm.nasm still needed?

Andrew,
In UefiCpuPkg\Library\CpuExceptionHandlerLib\X64\, there are two nasm files: 
ExceptionHandlerAsm.nasm and the other XCODE version.

The major diff between the two is the second operand in “mov rax, 
ASM_PFX(CommonInterruptEntry)” is patched at runtime by code, instead of 
relying on linker/loader to fix it.
Can I know more background why it’s needed for XCODE?

Given Apple is switching away from X86 CPU, is the XCODE version still needed?

+ Mike because I found another commit by you for bug: 565 – Fix X64 XCODE5/NASM 
compatibility issue in UefiCpuPkg MpInitLib 
(tianocore.org).

Thanks,
Ray


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#102194): https://edk2.groups.io/g/devel/message/102194
Mute This Topic: https://groups.io/mt/97947477/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: 
https://edk2.groups.io/g/devel/leave/9847357/21656/1706620634/xyzzy 
[arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] Is Xcode5ExceptionHandlerAsm.nasm still needed?

2023-03-30 Thread Ard Biesheuvel
On Thu, 30 Mar 2023 at 16:25, Ni, Ray  wrote:
>
> I am afraid they are not template code.
> That means if nx is set for data section, they can not be executed.
>

Currently, we fix up the entries by writing to the .text section at
runtime, so NX seems out of scope in any case.

Someone should check Xcode5, but for LLD, adding  -Wl,-z,notext is
sufficient to work around the error.


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#102189): https://edk2.groups.io/g/devel/message/102189
Mute This Topic: https://groups.io/mt/97947477/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] Is Xcode5ExceptionHandlerAsm.nasm still needed?

2023-03-30 Thread Ni, Ray
I am afraid they are not template code.
That means if nx is set for data section, they can not be executed.


thanks,
ray

From: Ard Biesheuvel 
Sent: Thursday, March 30, 2023 7:47:03 PM
To: devel@edk2.groups.io ; Ni, Ray ; 
Rebecca Cran 
Cc: Andrew Fish ; Kinney, Michael D 
; Liu, Zhiguang 
Subject: Re: [edk2-devel] Is Xcode5ExceptionHandlerAsm.nasm still needed?

(cc Rebecca)

On Thu, 30 Mar 2023 at 12:48, Ard Biesheuvel  wrote:
>
> On Thu, 30 Mar 2023 at 12:16, Ni, Ray  wrote:
> >
> > Andrew,
> >
> > In UefiCpuPkg\Library\CpuExceptionHandlerLib\X64\, there are two nasm 
> > files: ExceptionHandlerAsm.nasm and the other XCODE version.
> >
> >
> >
> > The major diff between the two is the second operand in “mov rax, 
> > ASM_PFX(CommonInterruptEntry)” is patched at runtime by code, instead of 
> > relying on linker/loader to fix it.
> >
> > Can I know more background why it’s needed for XCODE?
> >
> >
> >
> > Given Apple is switching away from X86 CPU, is the XCODE version still 
> > needed?
> >
> >
> >
> > + Mike because I found another commit by you for bug: 565 – Fix X64 
> > XCODE5/NASM compatibility issue in UefiCpuPkg MpInitLib (tianocore.org).
> >
> >
>
>
> Yes, we still need it, also for non-Xcode clang + lld
>
> The problem is that the little code templates use absolute addressing
> to refer to the jump targets. This is necessary because these
> templates are copied into the vector table, and so they are moved
> independently from the code they refer to, and so relative addressing
> is not an option here.
>
> One thing I haven't tried yet is to emit the template code into .data
> instead of .text, which /should/ be fine given that the template code
> is never executed directly, only the copied versions are executed.

I had a quick go at this, and the change below appears to work: it
moves the template code into .data, and changes the absolute
references to relative ones in the code that executes from where it
gets loaded.

I'm not sure how to test this, though.



--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm
@@ -27,7 +27,6 @@ extern ASM_PFX(CommonExceptionHandler)
 SECTION .data

 DEFAULT REL
-SECTION .text

 ALIGN   8

@@ -51,6 +50,9 @@ HookAfterStubHeaderBegin:
 pushrax
 mov rax, HookAfterStubHeaderEnd
 jmp rax
+
+SECTION .text
+
 HookAfterStubHeaderEnd:
 mov rax, rsp
 and sp,  0xfff0; make sure 16-byte aligned for
exception context
@@ -276,8 +278,7 @@ DrFinish:
 ; and make sure RSP is 16-byte aligned
 ;
 sub rsp, 4 * 8 + 8
-mov rax, ASM_PFX(CommonExceptionHandler)
-callrax
+callASM_PFX(CommonExceptionHandler)
 add rsp, 4 * 8 + 8

 cli
@@ -384,10 +385,10 @@ DoIret:
 ; comments here for definition of address map
 global ASM_PFX(AsmGetTemplateAddressMap)
 ASM_PFX(AsmGetTemplateAddressMap):
-mov rax, AsmIdtVectorBegin
+lea rax, AsmIdtVectorBegin
 mov qword [rcx], rax
 mov qword [rcx + 0x8],  (AsmIdtVectorEnd - AsmIdtVectorBegin) / 32
-mov rax, HookAfterStubHeaderBegin
+lea rax, HookAfterStubHeaderBegin
 mov qword [rcx + 0x10], rax
 ret


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#102186): https://edk2.groups.io/g/devel/message/102186
Mute This Topic: https://groups.io/mt/97947477/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: 
https://edk2.groups.io/g/devel/leave/9847357/21656/1706620634/xyzzy 
[arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] Is Xcode5ExceptionHandlerAsm.nasm still needed?

2023-03-30 Thread Ard Biesheuvel
(cc Rebecca)

On Thu, 30 Mar 2023 at 12:48, Ard Biesheuvel  wrote:
>
> On Thu, 30 Mar 2023 at 12:16, Ni, Ray  wrote:
> >
> > Andrew,
> >
> > In UefiCpuPkg\Library\CpuExceptionHandlerLib\X64\, there are two nasm 
> > files: ExceptionHandlerAsm.nasm and the other XCODE version.
> >
> >
> >
> > The major diff between the two is the second operand in “mov rax, 
> > ASM_PFX(CommonInterruptEntry)” is patched at runtime by code, instead of 
> > relying on linker/loader to fix it.
> >
> > Can I know more background why it’s needed for XCODE?
> >
> >
> >
> > Given Apple is switching away from X86 CPU, is the XCODE version still 
> > needed?
> >
> >
> >
> > + Mike because I found another commit by you for bug: 565 – Fix X64 
> > XCODE5/NASM compatibility issue in UefiCpuPkg MpInitLib (tianocore.org).
> >
> >
>
>
> Yes, we still need it, also for non-Xcode clang + lld
>
> The problem is that the little code templates use absolute addressing
> to refer to the jump targets. This is necessary because these
> templates are copied into the vector table, and so they are moved
> independently from the code they refer to, and so relative addressing
> is not an option here.
>
> One thing I haven't tried yet is to emit the template code into .data
> instead of .text, which /should/ be fine given that the template code
> is never executed directly, only the copied versions are executed.

I had a quick go at this, and the change below appears to work: it
moves the template code into .data, and changes the absolute
references to relative ones in the code that executes from where it
gets loaded.

I'm not sure how to test this, though.



--- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm
+++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm
@@ -27,7 +27,6 @@ extern ASM_PFX(CommonExceptionHandler)
 SECTION .data

 DEFAULT REL
-SECTION .text

 ALIGN   8

@@ -51,6 +50,9 @@ HookAfterStubHeaderBegin:
 pushrax
 mov rax, HookAfterStubHeaderEnd
 jmp rax
+
+SECTION .text
+
 HookAfterStubHeaderEnd:
 mov rax, rsp
 and sp,  0xfff0; make sure 16-byte aligned for
exception context
@@ -276,8 +278,7 @@ DrFinish:
 ; and make sure RSP is 16-byte aligned
 ;
 sub rsp, 4 * 8 + 8
-mov rax, ASM_PFX(CommonExceptionHandler)
-callrax
+callASM_PFX(CommonExceptionHandler)
 add rsp, 4 * 8 + 8

 cli
@@ -384,10 +385,10 @@ DoIret:
 ; comments here for definition of address map
 global ASM_PFX(AsmGetTemplateAddressMap)
 ASM_PFX(AsmGetTemplateAddressMap):
-mov rax, AsmIdtVectorBegin
+lea rax, AsmIdtVectorBegin
 mov qword [rcx], rax
 mov qword [rcx + 0x8],  (AsmIdtVectorEnd - AsmIdtVectorBegin) / 32
-mov rax, HookAfterStubHeaderBegin
+lea rax, HookAfterStubHeaderBegin
 mov qword [rcx + 0x10], rax
 ret


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#102176): https://edk2.groups.io/g/devel/message/102176
Mute This Topic: https://groups.io/mt/97947477/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] Is Xcode5ExceptionHandlerAsm.nasm still needed?

2023-03-30 Thread Ard Biesheuvel
On Thu, 30 Mar 2023 at 12:16, Ni, Ray  wrote:
>
> Andrew,
>
> In UefiCpuPkg\Library\CpuExceptionHandlerLib\X64\, there are two nasm files: 
> ExceptionHandlerAsm.nasm and the other XCODE version.
>
>
>
> The major diff between the two is the second operand in “mov rax, 
> ASM_PFX(CommonInterruptEntry)” is patched at runtime by code, instead of 
> relying on linker/loader to fix it.
>
> Can I know more background why it’s needed for XCODE?
>
>
>
> Given Apple is switching away from X86 CPU, is the XCODE version still needed?
>
>
>
> + Mike because I found another commit by you for bug: 565 – Fix X64 
> XCODE5/NASM compatibility issue in UefiCpuPkg MpInitLib (tianocore.org).
>
>


Yes, we still need it, also for non-Xcode clang + lld

The problem is that the little code templates use absolute addressing
to refer to the jump targets. This is necessary because these
templates are copied into the vector table, and so they are moved
independently from the code they refer to, and so relative addressing
is not an option here.

One thing I haven't tried yet is to emit the template code into .data
instead of .text, which /should/ be fine given that the template code
is never executed directly, only the copied versions are executed.


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#102174): https://edk2.groups.io/g/devel/message/102174
Mute This Topic: https://groups.io/mt/97947477/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] Is Xcode5ExceptionHandlerAsm.nasm still needed?

2023-03-30 Thread Ni, Ray
Andrew,
In UefiCpuPkg\Library\CpuExceptionHandlerLib\X64\, there are two nasm files: 
ExceptionHandlerAsm.nasm and the other XCODE version.

The major diff between the two is the second operand in "mov rax, 
ASM_PFX(CommonInterruptEntry)" is patched at runtime by code, instead of 
relying on linker/loader to fix it.
Can I know more background why it's needed for XCODE?

Given Apple is switching away from X86 CPU, is the XCODE version still needed?

+ Mike because I found another commit by you for bug: 565 - Fix X64 XCODE5/NASM 
compatibility issue in UefiCpuPkg MpInitLib 
(tianocore.org).

Thanks,
Ray


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#102170): https://edk2.groups.io/g/devel/message/102170
Mute This Topic: https://groups.io/mt/97947477/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: 
https://edk2.groups.io/g/devel/leave/9847357/21656/1706620634/xyzzy 
[arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-