Index: kernel/arch/sparc64/src/mm/tlb.c
===================================================================
--- kernel/arch/sparc64/src/mm/tlb.c	(revision cfa70adda5479cb3ac144ad8b0e43184319bd909)
+++ kernel/arch/sparc64/src/mm/tlb.c	(revision fadd3817f5f589ea27d771621b7531c4ede1293c)
@@ -53,7 +53,7 @@
 static void dtlb_pte_copy(pte_t *t, bool ro);
 static void itlb_pte_copy(pte_t *t);
-static void do_fast_data_access_mmu_miss_fault(istate_t *istate, const char *str);
 static void do_fast_instruction_access_mmu_miss_fault(istate_t *istate, const char *str);
-static void do_fast_data_access_protection_fault(istate_t *istate, const char *str);
+static void do_fast_data_access_mmu_miss_fault(istate_t *istate, tlb_tag_access_reg_t tag, const char *str);
+static void do_fast_data_access_protection_fault(istate_t *istate, tlb_tag_access_reg_t tag, const char *str);
 
 char *context_encoding[] = {
@@ -214,11 +214,12 @@
 
 	tag.value = dtlb_tag_access_read();
-	va = tag.vpn * PAGE_SIZE;
+	va = tag.vpn << PAGE_WIDTH;
+
 	if (tag.context == ASID_KERNEL) {
 		if (!tag.vpn) {
 			/* NULL access in kernel */
-			do_fast_data_access_mmu_miss_fault(istate, __FUNCTION__);
-		}
-		do_fast_data_access_mmu_miss_fault(istate, "Unexpected kernel page fault.");
+			do_fast_data_access_mmu_miss_fault(istate, tag, __FUNCTION__);
+		}
+		do_fast_data_access_mmu_miss_fault(istate, tag, "Unexpected kernel page fault.");
 	}
 
@@ -239,5 +240,5 @@
 		page_table_unlock(AS, true);
 		if (as_page_fault(va, PF_ACCESS_READ, istate) == AS_PF_FAULT) {
-			do_fast_data_access_mmu_miss_fault(istate, __FUNCTION__);
+			do_fast_data_access_mmu_miss_fault(istate, tag, __FUNCTION__);
 		}
 	}
@@ -252,5 +253,5 @@
 
 	tag.value = dtlb_tag_access_read();
-	va = tag.vpn * PAGE_SIZE;
+	va = tag.vpn << PAGE_WIDTH;
 
 	page_table_lock(AS, true);
@@ -272,5 +273,5 @@
 		page_table_unlock(AS, true);
 		if (as_page_fault(va, PF_ACCESS_WRITE, istate) == AS_PF_FAULT) {
-			do_fast_data_access_protection_fault(istate, __FUNCTION__);
+			do_fast_data_access_protection_fault(istate, tag, __FUNCTION__);
 		}
 	}
@@ -312,12 +313,10 @@
 }
 
-void do_fast_data_access_mmu_miss_fault(istate_t *istate, const char *str)
-{
-	tlb_tag_access_reg_t tag;
+void do_fast_data_access_mmu_miss_fault(istate_t *istate, tlb_tag_access_reg_t tag, const char *str)
+{
 	uintptr_t va;
 	char *tpc_str = get_symtab_entry(istate->tpc);
 
-	tag.value = dtlb_tag_access_read();
-	va = tag.vpn * PAGE_SIZE;
+	va = tag.vpn << PAGE_WIDTH;
 
 	printf("Faulting page: %p, ASID=%d\n", va, tag.context);
@@ -326,12 +325,10 @@
 }
 
-void do_fast_data_access_protection_fault(istate_t *istate, const char *str)
-{
-	tlb_tag_access_reg_t tag;
+void do_fast_data_access_protection_fault(istate_t *istate, tlb_tag_access_reg_t tag, const char *str)
+{
 	uintptr_t va;
 	char *tpc_str = get_symtab_entry(istate->tpc);
 
-	tag.value = dtlb_tag_access_read();
-	va = tag.vpn * PAGE_SIZE;
+	va = tag.vpn << PAGE_WIDTH;
 
 	printf("Faulting page: %p, ASID=%d\n", va, tag.context);
@@ -375,14 +372,19 @@
 void tlb_invalidate_asid(asid_t asid)
 {
-	tlb_context_reg_t sc_save, ctx;
-	
-	ctx.v = sc_save.v = mmu_secondary_context_read();
+	tlb_context_reg_t pc_save, ctx;
+	
+	/* switch to nucleus because we are mapped by the primary context */
+	nucleus_enter();
+	
+	ctx.v = pc_save.v = mmu_primary_context_read();
 	ctx.context = asid;
-	mmu_secondary_context_write(ctx.v);
-	
-	itlb_demap(TLB_DEMAP_CONTEXT, TLB_DEMAP_SECONDARY, 0);
-	dtlb_demap(TLB_DEMAP_CONTEXT, TLB_DEMAP_SECONDARY, 0);
-	
-	mmu_secondary_context_write(sc_save.v);
+	mmu_primary_context_write(ctx.v);
+	
+	itlb_demap(TLB_DEMAP_CONTEXT, TLB_DEMAP_PRIMARY, 0);
+	dtlb_demap(TLB_DEMAP_CONTEXT, TLB_DEMAP_PRIMARY, 0);
+	
+	mmu_primary_context_write(pc_save.v);
+	
+	nucleus_leave();
 }
 
@@ -396,16 +398,21 @@
 {
 	int i;
-	tlb_context_reg_t sc_save, ctx;
-	
-	ctx.v = sc_save.v = mmu_secondary_context_read();
+	tlb_context_reg_t pc_save, ctx;
+	
+	/* switch to nucleus because we are mapped by the primary context */
+	nucleus_enter();
+	
+	ctx.v = pc_save.v = mmu_primary_context_read();
 	ctx.context = asid;
-	mmu_secondary_context_write(ctx.v);
+	mmu_primary_context_write(ctx.v);
 	
 	for (i = 0; i < cnt; i++) {
-		itlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_SECONDARY, page + i * PAGE_SIZE);
-		dtlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_SECONDARY, page + i * PAGE_SIZE);
-	}
-	
-	mmu_secondary_context_write(sc_save.v);
+		itlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_PRIMARY, page + i * PAGE_SIZE);
+		dtlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_PRIMARY, page + i * PAGE_SIZE);
+	}
+	
+	mmu_primary_context_write(pc_save.v);
+	
+	nucleus_leave();
 }
 
