<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]--><style><!--
/* Font Definitions */
@font-face
        {font-family:"MS Gothic";
        panose-1:2 11 6 9 7 2 5 8 2 4;}
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}
@font-face
        {font-family:"\@MS Gothic";
        panose-1:2 11 6 9 7 2 5 8 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
pre
        {mso-style-priority:99;
        mso-style-link:"HTML Preformatted Char";
        margin:0in;
        font-size:10.0pt;
        font-family:"Courier New";}
span.HTMLPreformattedChar
        {mso-style-name:"HTML Preformatted Char";
        mso-style-priority:99;
        mso-style-link:"HTML Preformatted";
        font-family:Consolas;}
span.EmailStyle21
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;
        mso-ligatures:none;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal">How many different integer values are needed by FW for use of the csrrd instruction?<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">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></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">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></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Mike<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b>From:</b> devel@edk2.groups.io <devel@edk2.groups.io> <b>
On Behalf Of </b>Chao Li<br>
<b>Sent:</b> Wednesday, August 16, 2023 7:30 PM<br>
<b>To:</b> devel@edk2.groups.io; pedro.falcato@gmail.com<br>
<b>Cc:</b> Andrew (EFI) Fish <afish@apple.com>; Gao, Liming <gaoliming@byosoft.com.cn>; Feng, Bob C <bob.c.feng@intel.com>; Chen, Christine <yuwei.chen@intel.com><br>
<b>Subject:</b> Re: [edk2-devel] About EDK2 supports Self Modifying Code<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p><span style="font-size:10.0pt">Hi Pedro,</span><o:p></o:p></p>
<p><span style="font-size:10.0pt">Sorry for the late reply, I was a bit busy yesterday.</span><o:p></o:p></p>
<p><span style="font-size:10.0pt">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>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal"><span style="font-size:8.5pt">Thanks,<br>
Chao<o:p></o:p></span></p>
</div>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"MS Gothic"">在</span> 2023/8/15 23:35, Pedro Falcato
<span style="font-family:"MS Gothic"">写道</span>:<o:p></o:p></p>
</div>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<pre>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"><lichao@loongson.cn></a> wrote:<o:p></o:p></pre>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<pre><o:p> </o:p></pre>
<pre>Hi Andrew,<o:p></o:p></pre>
<pre><o:p> </o:p></pre>
<pre>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><o:p> </o:p></pre>
<pre>There isn't any. Actual use cases in something like a kernel are<o:p></o:p></pre>
<pre>heavily vetted and read-protected as soon as possible.<o:p></o:p></pre>
<pre><o:p> </o:p></pre>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<pre><o:p> </o:p></pre>
<pre>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><o:p> </o:p></pre>
<pre>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><o:p> </o:p></pre>
<pre>FYI, poking instructions willy-nilly is unsafe and unreliable (except<o:p></o:p></pre>
<pre>on x86 due to kludges, but then it's slow).<o:p></o:p></pre>
<pre><o:p> </o:p></pre>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<pre><o:p> </o:p></pre>
<pre>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><o:p> </o:p></pre>
<pre>So, I think I have following choice:<o:p></o:p></pre>
<pre><o:p> </o:p></pre>
<pre>Choice 1:<o:p></o:p></pre>
<pre><o:p> </o:p></pre>
<pre>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><o:p> </o:p></pre>
<pre>If you're going to use inline assembly, just expose them directly? I<o:p></o:p></pre>
<pre>don't see the problem there, I don't expect loongarch to be picked up<o:p></o:p></pre>
<pre>by visual studio any time soon.<o:p></o:p></pre>
<pre><o:p> </o:p></pre>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<pre><o:p> </o:p></pre>
<pre><o:p> </o:p></pre>
<pre>Choice 2:<o:p></o:p></pre>
<pre><o:p> </o:p></pre>
<pre>Use SMC.<o:p></o:p></pre>
<pre><o:p> </o:p></pre>
<pre><o:p> </o:p></pre>
<pre>Choice 3:<o:p></o:p></pre>
<pre><o:p> </o:p></pre>
<pre>Write a lot of similar CSR functions.<o:p></o:p></pre>
</blockquote>
<pre><o:p> </o:p></pre>
<pre>You /could/ use a GAS macro.<o:p></o:p></pre>
<pre><o:p> </o:p></pre>
<pre>.macro csr_write csr<o:p></o:p></pre>
<pre>.global CsrWrite\csr<o:p></o:p></pre>
<pre>CsrWrite\csr:<o:p></o:p></pre>
<pre>    csrw a0, \csr<o:p></o:p></pre>
<pre>    ret<o:p></o:p></pre>
<pre><o:p> </o:p></pre>
<pre>(this is riscv pseudo-asm but I know your arch is similar enough)<o:p></o:p></pre>
<pre><o:p> </o:p></pre>
</blockquote>
<div>
<p class="MsoNormal"></o:p></span></p>
</div>
</div>
</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/107837">View/Reply Online (#107837)</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>