Debug code for new ops

Change-Id: Ia29bba5195e74e74b1e79019237aeaa79686e41c
diff --git a/arch/arm64/include/asm/kvm_asm.h b/arch/arm64/include/asm/kvm_asm.h
index 60e834c..cd8a2e5 100644
--- a/arch/arm64/include/asm/kvm_asm.h
+++ b/arch/arm64/include/asm/kvm_asm.h
@@ -97,6 +97,7 @@
 	__KVM_HOST_SMCCC_FUNC___pkvm_rb_swap_reader_page,
 	__KVM_HOST_SMCCC_FUNC___pkvm_rb_update_footers,
 	__KVM_HOST_SMCCC_FUNC___pkvm_enable_event,
+	__KVM_HOST_SMCCC_FUNC___pkvm_host_page_prot_XXX,
 #ifdef CONFIG_ANDROID_ARM64_WORKAROUND_DMA_BEYOND_POC
 	__KVM_HOST_SMCCC_FUNC___pkvm_host_set_stage2_memattr,
 #endif
diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
index 995c3a8..605673c 100644
--- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c
+++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
@@ -1272,6 +1272,15 @@
 }
 #endif
 
+static void handle___pkvm_host_page_prot_XXX(struct kvm_cpu_context *host_ctxt)
+{
+	DECLARE_REG(u64, pfn, host_ctxt, 1);
+	DECLARE_REG(enum kvm_pgtable_prot, prot, host_ctxt, 2);
+	DECLARE_REG(u64, nr_pages, host_ctxt, 3);
+
+	cpu_reg(host_ctxt, 1) = module_change_host_page_prot_range(pfn, prot, nr_pages);
+}
+
 typedef void (*hcall_t)(struct kvm_cpu_context *);
 
 #define HANDLE_FUNC(x)	[__KVM_HOST_SMCCC_FUNC_##x] = (hcall_t)handle_##x
@@ -1322,6 +1331,7 @@
 	HANDLE_FUNC(__pkvm_rb_swap_reader_page),
 	HANDLE_FUNC(__pkvm_rb_update_footers),
 	HANDLE_FUNC(__pkvm_enable_event),
+	HANDLE_FUNC(__pkvm_host_page_prot_XXX),
 #ifdef CONFIG_ANDROID_ARM64_WORKAROUND_DMA_BEYOND_POC
 	HANDLE_FUNC(__pkvm_host_set_stage2_memattr),
 #endif
diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
index b3920a3..31b6e702 100644
--- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c
+++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
@@ -23,6 +23,8 @@
 #include <nvhe/mm.h>
 #include <nvhe/modules.h>
 
+#include "../debug-pl011.h"
+
 #define KVM_HOST_S2_FLAGS (KVM_PGTABLE_S2_NOFWB | KVM_PGTABLE_S2_IDMAP)
 
 struct host_mmu host_mmu;
diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c
index 64387388..07f29d5 100644
--- a/arch/arm64/kvm/hyp/pgtable.c
+++ b/arch/arm64/kvm/hyp/pgtable.c
@@ -11,6 +11,11 @@
 #include <asm/kvm_pgtable.h>
 #include <asm/stage2_pgtable.h>
 
+#ifdef __KVM_NVHE_HYPERVISOR__
+int xxxx;
+#include <asm/kvm_mmu.h>
+#include "debug-pl011.h"
+#endif
 
 #define KVM_PTE_LEAF_ATTR_S2_PERMS	(KVM_PTE_LEAF_ATTR_LO_S2_S2AP_R | \
 					 KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W | \
@@ -648,6 +653,13 @@
 static bool stage2_pte_needs_update(struct kvm_pgtable *pgt,
 				    kvm_pte_t old, kvm_pte_t new)
 {
+#ifdef __KVM_NVHE_HYPERVISOR__
+	if (xxxx) {
+		hyp_putx64(old);
+		hyp_putx64(new);
+	}
+#endif
+
 	/* Following filter logic applies only to guest stage-2 entries. */
 	if (pgt->flags & KVM_PGTABLE_S2_IDMAP)
 		return true;
@@ -655,6 +667,12 @@
 	if (!kvm_pte_valid(old) || !kvm_pte_valid(new))
 		return true;
 
+#ifdef __KVM_NVHE_HYPERVISOR__
+	if (xxxx) {
+		hyp_putx64(old^new);
+		hyp_putx64(KVM_PTE_LEAF_ATTR_S2_PERMS);
+	}
+#endif
 	return ((old ^ new) & (~KVM_PTE_LEAF_ATTR_S2_PERMS));
 }
 
@@ -824,6 +842,12 @@
 	}
 
 	ret = stage2_map_walker_try_leaf(addr, end, level, ptep, data);
+#ifdef __KVM_NVHE_HYPERVISOR__
+	if (xxxx) {
+		hyp_putx32(level);
+		hyp_putx32(ret);
+	}
+#endif
 	if (ret != -E2BIG)
 		return ret;
 
diff --git a/arch/arm64/kvm/pkvm.c b/arch/arm64/kvm/pkvm.c
index 10b899483..b409be8 100644
--- a/arch/arm64/kvm/pkvm.c
+++ b/arch/arm64/kvm/pkvm.c
@@ -536,6 +536,33 @@
 }
 device_initcall_sync(finalize_pkvm);
 
+static int test_memattr(void)
+{
+	int ret;
+	void *v = alloc_pages_exact(64*PAGE_SIZE, GFP_KERNEL);
+	u64 pfn = page_to_pfn(virt_to_page(v));
+	int i;
+	enum kvm_pgtable_prot prot_r = KVM_PGTABLE_PROT_R;
+	enum kvm_pgtable_prot prot_rwx = KVM_PGTABLE_PROT_RWX;
+	printk("%s %p %llx\n", __FUNCTION__, v, pfn);
+	for (i = 0; i < 10; i++) {
+		*(volatile int *)v = 23;
+		ret = kvm_call_hyp_nvhe(__pkvm_host_page_prot_XXX, pfn, prot_r, 64);
+		WARN_ON(ret);
+		printk("[%d]\n", *(volatile int *)v);
+		ret = kvm_call_hyp_nvhe(__pkvm_host_page_prot_XXX, pfn, prot_rwx, 64);
+		WARN_ON(ret);
+	}
+
+	printk("End\n");
+	ret = kvm_call_hyp_nvhe(__pkvm_host_page_prot_XXX, pfn, prot_r, 1);
+	WARN_ON(ret);
+	*(volatile int *)v = 23;
+
+	return ret;
+}
+late_initcall_sync(test_memattr);
+
 static int pkvm_vm_ioctl_set_fw_ipa(struct kvm *kvm, u64 ipa)
 {
 	int ret = 0;