From bd5b4f6f9899bbf9b346aa1f6c637cb97a1c8fd4 Mon Sep 17 00:00:00 2001
From: Ved Shanbhogue <ved@rivosinc.com>
Date: Thu, 26 Sep 2024 15:24:31 -0500
Subject: [PATCH] Fix PF vs. GPF priority

---
 iommu_ref_model/libiommu/src/iommu_two_stage_trans.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/iommu_ref_model/libiommu/src/iommu_two_stage_trans.c b/iommu_ref_model/libiommu/src/iommu_two_stage_trans.c
index b69437f..8740e7f 100644
--- a/iommu_ref_model/libiommu/src/iommu_two_stage_trans.c
+++ b/iommu_ref_model/libiommu/src/iommu_two_stage_trans.c
@@ -302,12 +302,12 @@ two_stage_address_translation(
     //    – If the comparison fails, return to step 2
     if ( (pte->A == 1) && ( (pte->D == 1) || (is_write == 0) || (pte->W == 0) ) ) goto step_8;
 
-    // If G-stage does not provide write permission then cause guest page fault
-    if ( gpte.W == 0 ) goto guest_page_fault;
-
     // A and/or D bit update needed
     if ( SADE == 0 ) goto page_fault;
 
+    // If G-stage does not provide write permission then cause guest page fault
+    if ( gpte.W == 0 ) goto guest_page_fault;
+
     // Count S/VS stage page walks
     count_events(PV, PID, PSCV, PSCID, DID, GV, GSCID, S_VS_PT_WALKS);
     amo_pte.raw = 0;