From 2d8e8cad33af4a5a2559b3e8b3fb16cce9cff6dc Mon Sep 17 00:00:00 2001
From: "kaf24@freefall.cl.cam.ac.uk" <kaf24@freefall.cl.cam.ac.uk>
Date: Wed, 20 Oct 2004 16:23:51 +0000
Subject: bitkeeper revision 1.1159.117.1 (41769117YL0J9d2bEpMGocvDP1rMdQ)

Fix the writable pagetable error paths. Some of them are genuine Xen
bugs if they trigger. Otehrs now properly clean wrpt state before
crashing the domain, so we don't leave any refcnts inconsistent.
---
 xen/arch/x86/memory.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

(limited to 'xen')

diff --git a/xen/arch/x86/memory.c b/xen/arch/x86/memory.c
index 0857d897e6..dd155a5ba5 100644
--- a/xen/arch/x86/memory.c
+++ b/xen/arch/x86/memory.c
@@ -1604,7 +1604,11 @@ void ptwr_flush(const int which)
     if ( unlikely(__get_user(pte, ptep)) )
     {
         MEM_LOG("ptwr: Could not read pte at %p\n", ptep);
-        domain_crash();
+        /*
+         * Really a bug. We could read this PTE during the initial fault,
+         * and pagetables can't have changed meantime. XXX Multi-proc guests?
+         */
+        BUG();
     }
     PTWR_PRINTK("[%c] disconnected_l1va at %p is %08lx\n",
                 PTWR_PRINT_WHICH, ptep, pte);
@@ -1627,7 +1631,11 @@ void ptwr_flush(const int which)
     if ( unlikely(__put_user(pte, ptep)) )
     {
         MEM_LOG("ptwr: Could not update pte at %p\n", ptep);
-        domain_crash();
+        /*
+         * Really a bug. We could write this PTE during the initial fault,
+         * and pagetables can't have changed meantime. XXX Multi-proc guests?
+         */
+        BUG();
     }
 
     /* Ensure that there are no stale writable mappings in any TLB. */
@@ -1668,6 +1676,12 @@ void ptwr_flush(const int which)
         if ( unlikely(!get_page_from_l1e(nl1e, d)) )
         {
             MEM_LOG("ptwr: Could not re-validate l1 page\n");
+            /*
+             * Make the remaining p.t's consistent before crashing, so the
+             * reference counts are correct.
+             */
+            memcpy(&pl1e[i], &ptwr_info[cpu].ptinfo[which].page[i],
+                   (ENTRIES_PER_L1_PAGETABLE - i) * sizeof(l1_pgentry_t));
             domain_crash();
         }
         
@@ -1781,6 +1795,9 @@ int ptwr_do_page_fault(unsigned long addr)
     {
         MEM_LOG("ptwr: Could not update pte at %p\n", (unsigned long *)
                 &linear_pg_table[addr>>PAGE_SHIFT]);
+        /* Toss the writable pagetable state and crash. */
+        unmap_domain_mem(ptwr_info[cpu].ptinfo[which].pl1e);
+        ptwr_info[cpu].ptinfo[which].l1va = 0;
         domain_crash();
     }
     
-- 
cgit v1.2.3


From 3e43b54eb94721befc816554a62fae76e11ce220 Mon Sep 17 00:00:00 2001
From: "cl349@freefall.cl.cam.ac.uk" <cl349@freefall.cl.cam.ac.uk>
Date: Wed, 20 Oct 2004 17:36:22 +0000
Subject: bitkeeper revision 1.1159.117.2 (4176a216CU3JD-3Q5NlBFwP8DCqzVg)

Additional fixes to the writable pagetable error paths.
---
 xen/arch/x86/memory.c | 2 ++
 xen/common/schedule.c | 6 +++---
 2 files changed, 5 insertions(+), 3 deletions(-)

(limited to 'xen')

diff --git a/xen/arch/x86/memory.c b/xen/arch/x86/memory.c
index dd155a5ba5..ffd1d6eacc 100644
--- a/xen/arch/x86/memory.c
+++ b/xen/arch/x86/memory.c
@@ -1682,6 +1682,8 @@ void ptwr_flush(const int which)
              */
             memcpy(&pl1e[i], &ptwr_info[cpu].ptinfo[which].page[i],
                    (ENTRIES_PER_L1_PAGETABLE - i) * sizeof(l1_pgentry_t));
+            unmap_domain_mem(pl1e);
+            ptwr_info[cpu].ptinfo[which].l1va = 0;
             domain_crash();
         }
         
diff --git a/xen/common/schedule.c b/xen/common/schedule.c
index db69f24362..251fc8b4f4 100644
--- a/xen/common/schedule.c
+++ b/xen/common/schedule.c
@@ -326,6 +326,9 @@ void __enter_scheduler(void)
     task_slice_t        next_slice;
     s32                 r_time;     /* time for new dom to run */
 
+    cleanup_writable_pagetable(
+        prev, PTWR_CLEANUP_ACTIVE | PTWR_CLEANUP_INACTIVE);
+
     perfc_incrc(sched_run);
     
     spin_lock_irq(&schedule_data[cpu].schedule_lock);
@@ -373,9 +376,6 @@ void __enter_scheduler(void)
     if ( unlikely(prev == next) )
         return;
     
-    cleanup_writable_pagetable(
-        prev, PTWR_CLEANUP_ACTIVE | PTWR_CLEANUP_INACTIVE);
-
     perfc_incrc(sched_ctx);
 
 #if defined(WAKE_HISTO)
-- 
cgit v1.2.3

d='n18' href='#n18'>18</a>
<a id='n19' href='#n19'>19</a>
<a id='n20' href='#n20'>20</a>
<a id='n21' href='#n21'>21</a>
<a id='n22' href='#n22'>22</a>
<a id='n23' href='#n23'>23</a>
<a id='n24' href='#n24'>24</a>
<a id='n25' href='#n25'>25</a>
<a id='n26' href='#n26'>26</a>
<a id='n27' href='#n27'>27</a>
<a id='n28' href='#n28'>28</a>
<a id='n29' href='#n29'>29</a>
<a id='n30' href='#n30'>30</a>
<a id='n31' href='#n31'>31</a>
<a id='n32' href='#n32'>32</a>
<a id='n33' href='#n33'>33</a>
<a id='n34' href='#n34'>34</a>
</pre></td>
<td class='lines'><pre><code><style>pre { line-height: 125%; margin: 0; }
td.linenos pre { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
span.linenos { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
td.linenos pre.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight { background: #ffffff; }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */</style><div class="highlight"><pre><span></span><span class="ch">#!/bin/sh</span>

<span class="nv">BLKSZ</span><span class="o">=</span><span class="m">65536</span>

<span class="o">[</span> -f <span class="s2">&quot;</span><span class="nv">$1</span><span class="s2">&quot;</span> -a -f <span class="s2">&quot;</span><span class="nv">$2</span><span class="s2">&quot;</span> <span class="o">]</span> <span class="o">||</span> <span class="o">{</span>
	<span class="nb">echo</span> <span class="s2">&quot;Usage: </span><span class="nv">$0</span><span class="s2"> &lt;kernel image&gt; &lt;rootfs image&gt; [output file]&quot;</span>
	<span class="nb">exit</span> <span class="m">1</span>
<span class="o">}</span>

<span class="nv">IMAGE</span><span class="o">=</span><span class="si">${</span><span class="nv">3</span><span class="k">:-</span><span class="nv">openwrt</span><span class="p">-combined.img</span><span class="si">}</span>

<span class="c1"># Make sure provided images are 64k aligned.</span>
<span class="nv">kern</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">${</span><span class="nv">IMAGE</span><span class="si">}</span><span class="s2">.kernel&quot;</span>
<span class="nv">root</span><span class="o">=</span><span class="s2">&quot;</span><span class="si">${</span><span class="nv">IMAGE</span><span class="si">}</span><span class="s2">.rootfs&quot;</span>
dd <span class="k">if</span><span class="o">=</span><span class="s2">&quot;</span><span class="nv">$1</span><span class="s2">&quot;</span> <span class="nv">of</span><span class="o">=</span><span class="s2">&quot;</span><span class="nv">$kern</span><span class="s2">&quot;</span> <span class="nv">bs</span><span class="o">=</span><span class="nv">$BLKSZ</span> <span class="nv">conv</span><span class="o">=</span>sync <span class="m">2</span>&gt;/dev/null
dd <span class="k">if</span><span class="o">=</span><span class="s2">&quot;</span><span class="nv">$2</span><span class="s2">&quot;</span> <span class="nv">of</span><span class="o">=</span><span class="s2">&quot;</span><span class="nv">$root</span><span class="s2">&quot;</span> <span class="nv">bs</span><span class="o">=</span><span class="nv">$BLKSZ</span> <span class="nv">conv</span><span class="o">=</span>sync <span class="m">2</span>&gt;/dev/null

<span class="c1"># Calculate md5sum over combined kernel and rootfs image.</span>
<span class="nv">md5</span><span class="o">=</span><span class="k">$(</span>cat <span class="s2">&quot;</span><span class="nv">$kern</span><span class="s2">&quot;</span> <span class="s2">&quot;</span><span class="nv">$root</span><span class="s2">&quot;</span> <span class="p">|</span> mkhash md5<span class="k">)</span>

<span class="c1"># Write image header followed by kernel and rootfs image.</span>
<span class="c1"># The header is padded to 64k, format is:</span>
<span class="c1">#  CI               magic word (&quot;Combined Image&quot;)</span>
<span class="c1">#  &lt;kernel length&gt;  length of kernel encoded as zero padded 8 digit hex</span>
<span class="c1">#  &lt;rootfs length&gt;  length of rootfs encoded as zero padded 8 digit hex</span>
<span class="c1">#  &lt;md5sum&gt;         checksum of the combined kernel and rootfs image</span>
<span class="o">(</span> <span class="nb">printf</span> <span class="s2">&quot;CI%08x%08x%32s&quot;</span> <span class="se">\</span>
	<span class="k">$(</span>stat -c <span class="s2">&quot;%s&quot;</span> <span class="s2">&quot;</span><span class="nv">$kern</span><span class="s2">&quot;</span><span class="k">)</span> <span class="k">$(</span>stat -c <span class="s2">&quot;%s&quot;</span> <span class="s2">&quot;</span><span class="nv">$root</span><span class="s2">&quot;</span><span class="k">)</span> <span class="s2">&quot;</span><span class="si">${</span><span class="nv">md5</span><span class="p">%% *</span><span class="si">}</span><span class="s2">&quot;</span> <span class="p">|</span> <span class="se">\</span>
	dd <span class="nv">bs</span><span class="o">=</span><span class="nv">$BLKSZ</span> <span class="nv">conv</span><span class="o">=</span>sync<span class="p">;</span>
  cat <span class="s2">&quot;</span><span class="nv">$kern</span><span class="s2">&quot;</span> <span class="s2">&quot;</span><span class="nv">$root</span><span class="s2">&quot;</span>
<span class="o">)</span> &gt; <span class="si">${</span><span class="nv">IMAGE</span><span class="si">}</span> <span class="m">2</span>&gt;/dev/null

<span class="c1"># Clean up.</span>
rm -f <span class="s2">&quot;</span><span class="nv">$kern</span><span class="s2">&quot;</span> <span class="s2">&quot;</span><span class="nv">$root</span><span class="s2">&quot;</span>
</pre></div>
</code></pre></td></tr></table>
</div> <!-- class=content -->
<div class='footer'>generated by <a href='https://git.zx2c4.com/cgit/about/'>cgit v1.2.3</a> (<a href='https://git-scm.com/'>git 2.25.1</a>) at 2024-12-27 14:17:19 +0000</div>
</div> <!-- id=cgit -->
</body>
</html>
