<div dir="ltr"><div><div><div><div><div>Hi all,<br><br></div>for learning purposes, I wanted to emulate &quot;lookup_address&quot; function for ARM platforms, but I&#39;m having some troubles which I don&#39;t know how to get over them.<br><br></div>I&#39;ll explain it:<br><br></div>I start by retrieving the value of swapper_pg_dir from TTBR1 register (during head.S executions the physical address of swapper_pg_dir is being saved in this register).<br></div>So far this works, I can compare it with gdb:<br><br></div>  (gdb) p swapper_pg_dir<br><pre class="gmail-programlisting" style="color:rgb(51,51,153);font-family:&quot;lucida sans typewriter&quot;,&quot;courier new&quot;,courier,monospace;font-size:0.9em;margin-top:0.4em;margin-bottom:0.2em;margin-left:0.5em;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;word-spacing:0px"><span style="background-color:rgb(0,0,0)"><span style="color:rgb(0,0,0)"><span style="background-color:rgb(255,255,255)">Cannot access memory at address 0xc0004000<br>(gdb) p init_mm.pgd<br>$5 = (pgd_t *) 0xc0004000<br>(gdb)</span></span><span style="background-color:rgb(0,0,0)"><span></span></span><span style="background-color:rgb(243,243,243)"><span></span></span><span style="background-color:rgb(255,255,255)"><span></span></span><br><br><br></span></pre><pre class="gmail-programlisting" style="font-family:&quot;lucida sans typewriter&quot;,&quot;courier new&quot;,courier,monospace;font-size:0.9em;margin-top:0.4em;margin-bottom:0.2em;margin-left:0.5em;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;word-spacing:0px"><span style="color:rgb(0,0,0)">And from my code:<br><br>static pgd_t *get_global_pgd (void)<br>{<br>        pgd_t *pgd;<br>        unsigned int ttb_reg;<br><br>        asm volatile (<br>        &quot;       mrc     p15, 0, %0, c2, c0, 1&quot;<br>        : &quot;=r&quot; (ttb_reg));<br><br><br>        ttb_reg &amp;= ~0x3fff;<br>        pgd = phys_to_virt (ttb_reg);<br>        return pgd;<br>}<br><br><br></span></pre><pre class="gmail-programlisting" style="font-family:&quot;lucida sans typewriter&quot;,&quot;courier new&quot;,courier,monospace;font-size:0.9em;margin-top:0.4em;margin-bottom:0.2em;margin-left:0.5em;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;word-spacing:0px"><span style="color:rgb(0,0,0)">and the output: <br><br>kernel: [18604.342604] mod: get_global_pgd: 0x0 - c0004000<br><br></span></pre><pre class="gmail-programlisting" style="font-family:&quot;lucida sans typewriter&quot;,&quot;courier new&quot;,courier,monospace;font-size:0.9em;margin-top:0.4em;margin-bottom:0.2em;margin-left:0.5em;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;word-spacing:0px"><span style="color:rgb(0,0,0)">So far, it&#39;s ok, but now the fun begins.<br></span></pre><pre class="gmail-programlisting" style="font-family:&quot;lucida sans typewriter&quot;,&quot;courier new&quot;,courier,monospace;font-size:0.9em;margin-top:0.4em;margin-bottom:0.2em;margin-left:0.5em;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;word-spacing:0px"><span style="color:rgb(0,0,0)">I&#39;m walking the page table to get the PTE related to an X address:<br><br>static pte_t *lookup_address (unsigned long addr)<br>{<br>        pgd_t *pgd;<br>        pud_t *pud;<br>        pmd_t *pmd;<br><br>        pgd = get_global_pgd() + pgd_index (addr);<br>        pud = pud_offset (pgd, addr);<br>        pmd = pmd_offset (pud, addr);<br>        if (pmd == NULL || pmd_none (*pmd)) {<br>                pr_info (&quot;%s: pmd == NULL\n&quot;, r2_devname);<br>                return NULL;<br>        }<br><br>        return pte_offset_kernel (pmd, addr);<br>}<br><br></span></pre><pre class="gmail-programlisting" style="font-family:&quot;lucida sans typewriter&quot;,&quot;courier new&quot;,courier,monospace;font-size:0.9em;margin-top:0.4em;margin-bottom:0.2em;margin-left:0.5em;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;word-spacing:0px"><span style="color:rgb(0,0,0)">After that, I&#39;d to check if the PTE has the PRESENT flag. Since ARM processor doesn&#39;t provide those flags Linux had to workaround that by adding <br></span></pre><pre class="gmail-programlisting" style="font-family:&quot;lucida sans typewriter&quot;,&quot;courier new&quot;,courier,monospace;font-size:0.9em;margin-top:0.4em;margin-bottom:0.2em;margin-left:0.5em;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;word-spacing:0px"><span style="color:rgb(0,0,0)">these flags: (f.i: <a href="http://lxr.free-electrons.com/source/arch/arm/include/asm/pgtable-2level.h#L123">http://lxr.free-electrons.com/source/arch/arm/include/asm/pgtable-2level.h#L123</a> )<br></span></pre><pre class="gmail-programlisting" style="font-family:&quot;lucida sans typewriter&quot;,&quot;courier new&quot;,courier,monospace;font-size:0.9em;margin-top:0.4em;margin-bottom:0.2em;margin-left:0.5em;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;word-spacing:0px"><span style="color:rgb(0,0,0)">I&#39;m checking the flags with <br><br></span></pre><pre class="gmail-programlisting" style="font-family:&quot;lucida sans typewriter&quot;,&quot;courier new&quot;,courier,monospace;font-size:0.9em;margin-top:0.4em;margin-bottom:0.2em;margin-left:0.5em;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;word-spacing:0px"><span style="color:rgb(0,0,0)">pte_present<br></span></pre><pre class="gmail-programlisting" style="font-family:&quot;lucida sans typewriter&quot;,&quot;courier new&quot;,courier,monospace;font-size:0.9em;margin-top:0.4em;margin-bottom:0.2em;margin-left:0.5em;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;word-spacing:0px"><span style="color:rgb(0,0,0)">or<br></span></pre><pre class="gmail-programlisting" style="font-family:&quot;lucida sans typewriter&quot;,&quot;courier new&quot;,courier,monospace;font-size:0.9em;margin-top:0.4em;margin-bottom:0.2em;margin-left:0.5em;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;word-spacing:0px"><span style="color:rgb(0,0,0)">pte_write<br><br></span></pre><pre class="gmail-programlisting" style="font-family:&quot;lucida sans typewriter&quot;,&quot;courier new&quot;,courier,monospace;font-size:0.9em;margin-top:0.4em;margin-bottom:0.2em;margin-left:0.5em;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;word-spacing:0px"><span style="color:rgb(0,0,0)">But I think I&#39;m getting the wrong PTE&#39;s because I&#39;ve tried with some address from /proc/kallsyms, and it&#39;s saying that the page is not present, and I&#39;m getting diferent values for different pages<br></span></pre><pre class="gmail-programlisting" style="font-family:&quot;lucida sans typewriter&quot;,&quot;courier new&quot;,courier,monospace;font-size:0.9em;margin-top:0.4em;margin-bottom:0.2em;margin-left:0.5em;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;word-spacing:0px"><span style="color:rgb(0,0,0)">in the same range.<br></span></pre><pre class="gmail-programlisting" style="font-family:&quot;lucida sans typewriter&quot;,&quot;courier new&quot;,courier,monospace;font-size:0.9em;margin-top:0.4em;margin-bottom:0.2em;margin-left:0.5em;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;word-spacing:0px"><span style="color:rgb(0,0,0)">For instance:<br><br><br>for: 0xc0009244:<br>bananapi kernel: [18604.419142] mod: pte_present: 0<br>bananapi kernel: [18604.428025] mod: pte_write: 0<br>bananapi kernel: [18604.436592] mod: pte_young: 2<br>bananapi kernel: [18604.445318] mod: pte_dirty: 0<br><br></span></pre><pre class="gmail-programlisting" style="font-family:&quot;lucida sans typewriter&quot;,&quot;courier new&quot;,courier,monospace;font-size:0.9em;margin-top:0.4em;margin-bottom:0.2em;margin-left:0.5em;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;word-spacing:0px"><span style="color:rgb(0,0,0)">and for 0xc0012904:<br><br></span></pre><pre class="gmail-programlisting" style="font-family:&quot;lucida sans typewriter&quot;,&quot;courier new&quot;,courier,monospace;font-size:0.9em;margin-top:0.4em;margin-bottom:0.2em;margin-left:0.5em;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;word-spacing:0px"><span style="color:rgb(0,0,0)"><br>bananapi kernel: [18883.095749] r2k: pte_present: 0<br>bananapi kernel: [18883.104607] r2k: pte_write: 1<br>bananapi kernel: [18883.113159] r2k: pte_young: 0<br>bananapi kernel: [18883.121670] r2k: pte_dirty: 0<br><br></span></pre><pre class="gmail-programlisting" style="font-family:&quot;lucida sans typewriter&quot;,&quot;courier new&quot;,courier,monospace;font-size:0.9em;margin-top:0.4em;margin-bottom:0.2em;margin-left:0.5em;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;word-spacing:0px"><span style="color:rgb(0,0,0)">It&#39;s saying that is not present, but I can read from those addresses with gdb:<br><br><br>(gdb) x/2x 0xc0009244<br>0xc0009244 &lt;vfp_flush_hwstate&gt;:        0xe92d4010        0xe1a04000<br>(gdb) x/2x 0xc0012904<br>0xc0012904 &lt;__readwrite_bug&gt;:        0xe1a01000        0xe30505ec<br>(gdb) <br><br></span></pre><pre class="gmail-programlisting" style="font-family:&quot;lucida sans typewriter&quot;,&quot;courier new&quot;,courier,monospace;font-size:0.9em;margin-top:0.4em;margin-bottom:0.2em;margin-left:0.5em;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;word-spacing:0px"><span style="color:rgb(0,0,0)">What am I doing wrong? <br><br></span></pre><pre class="gmail-programlisting" style="color:rgb(51,51,153);font-family:&quot;lucida sans typewriter&quot;,&quot;courier new&quot;,courier,monospace;font-size:0.9em;margin-top:0.4em;margin-bottom:0.2em;margin-left:0.5em;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;word-spacing:0px"><span style="color:rgb(0,0,0)">Thanks in advance</span><br></pre><pre class="gmail-programlisting" style="color:rgb(51,51,153);font-family:&quot;lucida sans typewriter&quot;,&quot;courier new&quot;,courier,monospace;font-size:0.9em;margin-top:0.4em;margin-bottom:0.2em;margin-left:0.5em;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;word-spacing:0px"><br><br></pre></div>