<style>p {margin:0 0 14px 0}.default-font-1692411501025 {font-size: 14px;font-family: 宋体, arial, Verdana, sans-serif}</style><div class="default-font-1692411501025"><p>
        Hi Mike and Andrew,
</p>
<p>
        The CSR instructions integer value width is 14 bits, use for to access the CSR registers (Control And Status Registers).
</p>
<p>
        Just like Andrew saying, the CSR registers include status registers, exception registers, TLB registers, performance conters etc.
</p>
<p>
        Now, there are a few numbers CSR ineger value request by FW, but I don't known wether it will be use more numbers in future.
</p>
<p>
        There are three choices:
</p>
<p>
        <strong>Choices 1,</strong>
</p>
<p>
        As Andrew saying, create some fixed variable and use marco `FixedGetPcd32` to get the right value befor calling the CSR instructions, but our might be create a lot of fixed variable, because it is 14bits width.
</p>
<p>
        <br>
</p>
<p>
        <strong>Choices 2,</strong>
</p>
<p>
        As Mike saying, defined a limited APIs set, and if needs to more values, just adding them in future.
</p>
<p>
        <br>
</p>
<p>
        <strong>Choices 3,</strong>
</p>
<p>
        Defined some inline assembly macro and handled it in the preprocessing stage, just like defined the CSR inline assembly macro in MdePkg/Include/Registers/LoongArch64/Csr.h, and include it when request to access the CSR registers.
</p>
<p>
        <br>
</p>
<p>
        Hope you can give your suggestion again and I will wait for your relpy.
</p>
<br>
<br>
<blockquote name="replyContent" class="ReferenceQuote" style="padding-left:5px;margin-left:5px;border-left:#b6b6b6 2px solid;margin-right:0;">
        -----原始邮件-----<br>
<b>发件人:</b> <span id="rc_from">"Andrew Fish via groups.io" <afish=apple.com@groups.io></span><br>
<b>发送时间:</b> <span id="rc_senttime">2023-08-18 04:55:13 (星期五)</span><br>
<b>收件人:</b> devel@edk2.groups.io, "Mike Kinney" <michael.d.kinney@intel.com><br>
<b>抄送:</b> "lichao@loongson.cn" <lichao@loongson.cn>, "pedro.falcato@gmail.com" <pedro.falcato@gmail.com>, "Gao, Liming" <gaoliming@byosoft.com.cn>, "Feng, Bob C" <bob.c.feng@intel.com>, "Chen, Christine" <yuwei.chen@intel.com><br>
<b>主题:</b> Re: [edk2-devel] About EDK2 supports Self Modifying Code<br>
<br>
        <div 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>
                                        <div class="WordSection1" style="page:WordSection1;caret-color:#000000;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:#000000;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:#E1E1E1 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;">
                                                                <br>
                                                        </p>
                                                </div>
                                        </div>
                                </div>
                        </blockquote>
                </div>
<br>
                
        </div>
</blockquote></div><br><br><i style="font-variant-numeric: normal; font-variant-east-asian: normal; widows: 1; font-family: 宋体, Tahoma; font-size: 14px; line-height: 16.8px;"><font color="#c0c0c0"><span style="font-size: 10pt; font-family: 宋体;">本邮件及其附件含有龙芯中科的商业秘密信息,仅限于发送给上面地址中列出的个人或群组。禁止任何其他人以任何形式使用(包括但不限于全部或部</span><span style="font-size: 10pt; font-family: 宋体;">分地泄露、复制或散发)本邮件及其附件中的信息。如果您错收本邮件,请您立即电话或邮件通知发件人并删除本邮件。</span><span lang="EN-US" style="font-size: 10pt; font-family: Arial, sans-serif;"> <br>This email and its attachments contain confidential information from Loongson Technology , which is intended only for the person or entity whose address is listed above. Any use of the information contained herein in any way (including, but not limited to, total or partial disclosure, reproduction or dissemination) by persons other than the intended recipient(s) is prohibited. If you receive this email in error, please notify the sender by phone or email immediately and delete it. </span></font></i>

<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/107881">View/Reply Online (#107881)</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/unsub">Unsubscribe</a>

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