<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body style="overflow-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;"><br><div><br><blockquote type="cite"><div>On Aug 17, 2023, at 12:53 PM, Michael D Kinney <michael.d.kinney@intel.com> wrote:</div><br class="Apple-interchange-newline"><div><meta charset="UTF-8"><div class="WordSection1" style="page: WordSection1; caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">How many different integer values are needed by FW for use of the csrrd instruction?<o:p></o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><o:p> </o:p></div></div></div></blockquote><div><br></div><div>MIke,</div><div><br></div><div>I’m no expert on this and I just tried to site read a specification for the 1st time….</div><div>It looks to me the like the arch spec does not say something like mm0 - mm7 it seems to imply mm0 - mmN (N is implementation defined). Some of these resources seemed to be debug registers and performance counters, so things that make a lot of sense to have a variable number defined by the implementation? </div><div><br></div><div>Thanks,</div><div><br></div><div>Andrew Fish</div><br><blockquote type="cite"><div><div class="WordSection1" style="page: WordSection1; caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;"><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">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.<o:p></o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><o:p> </o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">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.<o:p></o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><o:p> </o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;">Mike<o:p></o:p></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><o:p> </o:p></div><div><div style="border-width: 1pt medium medium; border-style: solid none none; border-color: rgb(225, 225, 225) currentcolor currentcolor; border-image: none; padding: 3pt 0in 0in;"><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><b>From:</b><span class="Apple-converted-space"> </span><a href="mailto:devel@edk2.groups.io" style="color: blue; text-decoration: underline;">devel@edk2.groups.io</a><span class="Apple-converted-space"> </span><<a href="mailto:devel@edk2.groups.io" style="color: blue; text-decoration: underline;">devel@edk2.groups.io</a>><span class="Apple-converted-space"> </span><b>On Behalf Of<span class="Apple-converted-space"> </span></b>Chao Li<br><b>Sent:</b><span class="Apple-converted-space"> </span>Wednesday, August 16, 2023 7:30 PM<br><b>To:</b><span class="Apple-converted-space"> </span><a href="mailto:devel@edk2.groups.io" style="color: blue; text-decoration: underline;">devel@edk2.groups.io</a>;<span class="Apple-converted-space"> </span><a href="mailto:pedro.falcato@gmail.com" style="color: blue; text-decoration: underline;">pedro.falcato@gmail.com</a><br><b>Cc:</b><span class="Apple-converted-space"> </span>Andrew (EFI) Fish <<a href="mailto:afish@apple.com" style="color: blue; text-decoration: underline;">afish@apple.com</a>>; Gao, Liming <<a href="mailto:gaoliming@byosoft.com.cn" style="color: blue; text-decoration: underline;">gaoliming@byosoft.com.cn</a>>; Feng, Bob C <<a href="mailto:bob.c.feng@intel.com" style="color: blue; text-decoration: underline;">bob.c.feng@intel.com</a>>; Chen, Christine <<a href="mailto:yuwei.chen@intel.com" style="color: blue; text-decoration: underline;">yuwei.chen@intel.com</a>><br><b>Subject:</b><span class="Apple-converted-space"> </span>Re: [edk2-devel] About EDK2 supports Self Modifying Code<o:p></o:p></div></div></div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><o:p> </o:p></div><p><span style="font-size: 10pt;">Hi Pedro,</span><o:p></o:p></p><p><span style="font-size: 10pt;">Sorry for the late reply, I was a bit busy yesterday.</span><o:p></o:p></p><p><span style="font-size: 10pt;">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.</span><o:p></o:p></p><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><o:p> </o:p></div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><span style="font-size: 8.5pt;">Thanks,<br>Chao<o:p></o:p></span></div></div></div><div><div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"><span style="font-family: "MS Gothic";">在</span><span class="Apple-converted-space"> </span>2023/8/15 23:35, Pedro Falcato<span class="Apple-converted-space"> </span><span style="font-family: "MS Gothic";">写道</span>:<o:p></o:p></div></div><blockquote style="margin-top: 5pt; margin-bottom: 5pt;"><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";">On Tue, Aug 15, 2023 at 9:20<span style="font-family: "Cambria Math", serif;"> </span>AM Chao Li <a href="mailto:lichao@loongson.cn" style="color: blue; text-decoration: underline;"><lichao@loongson.cn></a> wrote:<o:p></o:p></pre><blockquote style="margin-top: 5pt; margin-bottom: 5pt;"><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";"><o:p> </o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";">Hi Andrew,<o:p></o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";"><o:p> </o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";">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.<o:p></o:p></pre></blockquote><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";"><o:p> </o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";">There isn't any. Actual use cases in something like a kernel are<o:p></o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";">heavily vetted and read-protected as soon as possible.<o:p></o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";"><o:p> </o:p></pre><blockquote style="margin-top: 5pt; margin-bottom: 5pt;"><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";"><o:p> </o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";">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.<o:p></o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";"><o:p> </o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";">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.<o:p></o:p></pre></blockquote><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";"><o:p> </o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";">FYI, poking instructions willy-nilly is unsafe and unreliable (except<o:p></o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";">on x86 due to kludges, but then it's slow).<o:p></o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";"><o:p> </o:p></pre><blockquote style="margin-top: 5pt; margin-bottom: 5pt;"><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";"><o:p> </o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";">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.<o:p></o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";"><o:p> </o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";">So, I think I have following choice:<o:p></o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";"><o:p> </o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";">Choice 1:<o:p></o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";"><o:p> </o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";">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.<o:p></o:p></pre></blockquote><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";"><o:p> </o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";">If you're going to use inline assembly, just expose them directly? I<o:p></o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";">don't see the problem there, I don't expect loongarch to be picked up<o:p></o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";">by visual studio any time soon.<o:p></o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";"><o:p> </o:p></pre><blockquote style="margin-top: 5pt; margin-bottom: 5pt;"><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";"><o:p> </o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";"><o:p> </o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";">Choice 2:<o:p></o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";"><o:p> </o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";">Use SMC.<o:p></o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";"><o:p> </o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";"><o:p> </o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";">Choice 3:<o:p></o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";"><o:p> </o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";">Write a lot of similar CSR functions.<o:p></o:p></pre></blockquote><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";"><o:p> </o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";">You /could/ use a GAS macro.<o:p></o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";"><o:p> </o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";">.macro csr_write csr<o:p></o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";">.global CsrWrite\csr<o:p></o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";">CsrWrite\csr:<o:p></o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";">    csrw a0, \csr<o:p></o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";">    ret<o:p></o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";"><o:p> </o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";">(this is riscv pseudo-asm but I know your arch is similar enough)<o:p></o:p></pre><pre style="margin: 0in; font-size: 10pt; font-family: "Courier New";"><o:p> </o:p></pre></blockquote><div><p class="MsoNormal" style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;"></p></div></div></div></blockquote></div><br></body></html>


<div width="1" style="color:white;clear:both">_._,_._,_</div>
<hr>


Groups.io Links:<p>


  
    You receive all messages sent to this group.
  
  


<p>
<a target="_blank" href="https://edk2.groups.io/g/devel/message/107841">View/Reply Online (#107841)</a> |


  

|

  <a target="_blank" href="https://groups.io/mt/100751724/1813853">Mute This Topic</a>


| <a href="https://edk2.groups.io/g/devel/post">New Topic</a>

<br>




<a href="https://edk2.groups.io/g/devel/editsub/1813853">Your Subscription</a> |
<a href="mailto:devel+owner@edk2.groups.io">Contact Group Owner</a> |

<a href="https://edk2.groups.io/g/devel/leave/3943202/1813853/130120423/xyzzy">Unsubscribe</a>

 [edk2-devel-archive@redhat.com]<br>
<div width="1" style="color:white;clear:both">_._,_._,_</div>