[edk2-devel] About EDK2 supports Self Modifying Code

Michael D Kinney michael.d.kinney at intel.com
Thu Aug 17 19:53:50 UTC 2023


How many different integer values are needed by FW for use of the csrrd instruction?

There are examples of access functions on x86 for things like mm0, mm1, mm2, …, mm7 and cs, ds, es, ss, fs, gs.  These are implemented as different BaseLib APIs because they would also require SMC to do in a single API.

If there is a small number of csrrd index values that need to be accessed, and they have standard names, then perhaps you could define a set of APIs to access those registers.

Mike

From: devel at edk2.groups.io <devel at edk2.groups.io> On Behalf Of Chao Li
Sent: Wednesday, August 16, 2023 7:30 PM
To: devel at edk2.groups.io; pedro.falcato at gmail.com
Cc: Andrew (EFI) Fish <afish at apple.com>; Gao, Liming <gaoliming at byosoft.com.cn>; Feng, Bob C <bob.c.feng at intel.com>; Chen, Christine <yuwei.chen at intel.com>
Subject: Re: [edk2-devel] About EDK2 supports Self Modifying Code


Hi Pedro,

Sorry for the late reply, I was a bit busy yesterday.

I think the better way is to use inline asm, because this issue must has to be dealt with in preprocessing stage, because in other stages, it has no chance to get immediate value except using SMC. But then we should ask to the MdePkg maintainer if it is OK.

Thanks,
Chao
在 2023/8/15 23:35, Pedro Falcato 写道:

On Tue, Aug 15, 2023 at 9:20 AM Chao Li <lichao at loongson.cn><mailto:lichao at loongson.cn> wrote:



Hi Andrew,



Yes, you are right, I also think that SMC is a bit flawed in terms of security, but can we use some security mechanism to protect the SMC, like encryption and decryption? Sorry, I'm not consider mature enough about SMC security.



There isn't any. Actual use cases in something like a kernel are

heavily vetted and read-protected as soon as possible.





I can tell you real problem, there are some CSR instructions in LoongArch64 that can only accept immediate value, for example: `csrrd $a0, 0x1`, the 0x1 is the selection of CSR register number, it can't use the registers to select. This operation should be in the MdePkg base library.



I know that .c or .h files in MdePkg shouldn't depend on a single compiler feature, so I can't use the GNU AT&T style inline ASM function(AT&T style inline supports input parameters being immedite value, use "i" option). In this case, I think using SMC can handle this, that is use register transfer the CSR registers selection, and dynamically modify CSR instructions during execution phase with reference to transfer register value, this way is depend on the .text section or target memory is executable and writable.



FYI, poking instructions willy-nilly is unsafe and unreliable (except

on x86 due to kludges, but then it's slow).





The problem of immediate values can only be handled by preprocessing stage or using SMC, otherwise I can only write a lot of similar functions and use `switch case` to call them. This method will cause the program size to expand a lot.



So, I think I have following choice:



Choice 1:



Use AT&T style inline function, and create a file named: CsrOperationGcc.c, and other future compiler feature-dependent files will be named: CsrOperationClang.c, CsrOperationXlang.c and so on.



If you're going to use inline assembly, just expose them directly? I

don't see the problem there, I don't expect loongarch to be picked up

by visual studio any time soon.







Choice 2:



Use SMC.





Choice 3:



Write a lot of similar CSR functions.



You /could/ use a GAS macro.



.macro csr_write csr

.global CsrWrite\csr

CsrWrite\csr:

    csrw a0, \csr

    ret



(this is riscv pseudo-asm but I know your arch is similar enough)





-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#107837): https://edk2.groups.io/g/devel/message/107837
Mute This Topic: https://groups.io/mt/100751724/1813853
Group Owner: devel+owner at edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/leave/3943202/1813853/130120423/xyzzy [edk2-devel-archive at redhat.com]
-=-=-=-=-=-=-=-=-=-=-=-


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listman.redhat.com/archives/edk2-devel-archive/attachments/20230817/ec5d01d8/attachment-0001.htm>


More information about the edk2-devel-archive mailing list