Use vm_level to determine whether operation is for guest under host.

Signed-off-by: SignKirigami <[email protected]>
Signed-off-by: Hengyu Yu <[email protected]>
---
 .../tcg/insn_trans/trans_privileged.c.inc     | 34 ++++++++++++++
 target/loongarch/tcg/tlb_helper.c             | 46 +++++++++++++++----
 2 files changed, 70 insertions(+), 10 deletions(-)

diff --git a/target/loongarch/tcg/insn_trans/trans_privileged.c.inc 
b/target/loongarch/tcg/insn_trans/trans_privileged.c.inc
index db637249f1..5575bba9e1 100644
--- a/target/loongarch/tcg/insn_trans/trans_privileged.c.inc
+++ b/target/loongarch/tcg/insn_trans/trans_privileged.c.inc
@@ -322,6 +322,10 @@ static bool trans_invtlb(DisasContext *ctx, arg_invtlb *a)
         return false;
     }
 
+    if (!avail_LVZ(ctx) && a->imm > 0x6) {
+        return false;
+    }
+
     switch (a->imm) {
     case 0x0:
     case 0x1:
@@ -346,6 +350,36 @@ static bool trans_invtlb(DisasContext *ctx, arg_invtlb *a)
         gen_helper_invtlb_page_asid_or_g(tcg_env, rj, rk,
                                          tcg_constant_i32(0));
         break;
+    case 0x9:
+    case 0x10:
+    case 0x11:
+    case 0x12:
+    case 0x13:
+    case 0x14:
+    case 0x15:
+    case 0x16:
+        //TODO: refine 0x9-0x16
+        gen_helper_invtlb_all(tcg_env, rj, tcg_constant_i32(0),
+                              tcg_constant_i32(0));
+        break;
+    case 0xa:
+        gen_helper_invtlb_all_g(tcg_env, rj, tcg_constant_i32(1),
+                                tcg_constant_i32(1));
+        break;
+    case 0xb:
+        gen_helper_invtlb_all_g(tcg_env, rj, tcg_constant_i32(0),
+                                tcg_constant_i32(1));
+        break;
+    case 0xc:
+        gen_helper_invtlb_all_asid(tcg_env, rj, tcg_constant_i32(1));
+        break;
+    case 0xd:
+        gen_helper_invtlb_page_asid(tcg_env, rj, rk, tcg_constant_i32(1));
+        break;
+    case 0xe:
+        gen_helper_invtlb_page_asid_or_g(tcg_env, rj, rk,
+                                         tcg_constant_i32(1));
+        break;
     default:
         return false;
     }
diff --git a/target/loongarch/tcg/tlb_helper.c 
b/target/loongarch/tcg/tlb_helper.c
index 4bf9dacd0c..1248b44a46 100644
--- a/target/loongarch/tcg/tlb_helper.c
+++ b/target/loongarch/tcg/tlb_helper.c
@@ -602,11 +602,17 @@ void HELPER(tlbflush)(CPULoongArchState *env)
 void HELPER(invtlb_all)(CPULoongArchState *env, target_ulong info, uint32_t op,
                        uint32_t vm_level)
 {
-    uint16_t gid = get_gid(env, env_vm_level(env));
+    uint16_t gid = vm_level ? (info & 0xff) : get_gid(env, env_vm_level(env));
+
+    if (vm_level && env_vm_level(env) == VM_LEVEL1) {
+        do_raise_exception(env, EXCCODE_IPE, GETPC());
+    }
+
+    vm_level |= env_vm_level(env) == VM_LEVEL1;
 
     for (int i = 0; i < LOONGARCH_TLB_MAX; i++) {
         LoongArchTLB *tlb = &env->tlb[i];
-        if ((op == 0 && env_vm_level(env) == 0) || tlb_entry_matches_gid(tlb, 
gid))
+        if ((op == 0 && vm_level == 0) || tlb_entry_matches_gid(tlb, gid))
             tlb->tlb_misc = FIELD_DP64(tlb->tlb_misc, TLB_MISC, E, 0);
     }
     tlb_flush(env_cpu(env));
@@ -615,7 +621,13 @@ void HELPER(invtlb_all)(CPULoongArchState *env, 
target_ulong info, uint32_t op,
 void HELPER(invtlb_all_g)(CPULoongArchState *env, target_ulong info, uint32_t 
g,
                          uint32_t vm_level)
 {
-    uint16_t gid = get_gid(env, env_vm_level(env));
+    uint16_t gid = vm_level ? (info & 0xff) : get_gid(env, env_vm_level(env));
+
+    if (vm_level && env_vm_level(env) == VM_LEVEL1) {
+        do_raise_exception(env, EXCCODE_IPE, GETPC());
+    }
+
+    vm_level |= env_vm_level(env) == VM_LEVEL1;
 
     for (int i = 0; i < LOONGARCH_TLB_MAX; i++) {
         LoongArchTLB *tlb = &env->tlb[i];
@@ -632,7 +644,12 @@ void HELPER(invtlb_all_asid)(CPULoongArchState *env, 
target_ulong info,
                             uint32_t vm_level)
 {
     uint16_t asid = info & R_CSR_ASID_ASID_MASK;
-    uint16_t gid = get_gid(env, env_vm_level(env));
+    uint16_t gid = vm_level ? ((info >> 16) & 0xff) : get_gid(env, 
env_vm_level(env));
+
+    if (vm_level) {
+        do_raise_exception(env, EXCCODE_IPE, GETPC());
+    }
+    vm_level |= env_vm_level(env);
 
     for (int i = 0; i < LOONGARCH_TLB_MAX; i++) {
         LoongArchTLB *tlb = &env->tlb[i];
@@ -651,14 +668,19 @@ void HELPER(invtlb_page_asid)(CPULoongArchState *env, 
target_ulong info,
 {
 
     uint16_t asid = info & R_CSR_ASID_ASID_MASK;
-    uint16_t gid = get_gid(env, env_vm_level(env));
+    uint16_t gid = vm_level ? ((info >> 16) & 0xff) : get_gid(env, 
env_vm_level(env));
     const LoongArchTLB *tlb;
     tlb_match func;
 
+    if (vm_level && env_vm_level(env)) {
+        do_raise_exception(env, EXCCODE_IPE, GETPC());
+    }
+    vm_level |= env_vm_level(env);
+
     func = tlb_match_asid;
-    tlb = loongarch_tlb_search_cb(env, addr, asid, func, env_vm_level(env), 
gid);
+    tlb = loongarch_tlb_search_cb(env, addr, asid, func, vm_level, gid);
     if (tlb) {
-        invalidate_tlb(env, tlb - env->tlb, env_vm_level(env));
+        invalidate_tlb(env, tlb - env->tlb, vm_level);
     }
 }
 
@@ -666,15 +688,19 @@ void HELPER(invtlb_page_asid_or_g)(CPULoongArchState 
*env, target_ulong info,
                                   target_ulong addr, uint32_t vm_level)
 {
     uint16_t asid = info & R_CSR_ASID_ASID_MASK;
-    uint16_t gid = get_gid(env, env_vm_level(env));
+    uint16_t gid = vm_level ? ((info >> 16) & 0xff) : get_gid(env, 
env_vm_level(env));
     const LoongArchTLB *tlb;
     tlb_match func;
 
     func = tlb_match_any;
+    if (vm_level) {
+        do_raise_exception(env, EXCCODE_IPE, GETPC());
+    }
+    vm_level |= env_vm_level(env);
     tlb = loongarch_tlb_search_cb(env, addr, asid, func,
-                                  env_vm_level(env), gid);
+                                  vm_level, gid);
     if (tlb) {
-        invalidate_tlb(env, tlb - env->tlb, env_vm_level(env));
+        invalidate_tlb(env, tlb - env->tlb, vm_level);
     }
 }
 
-- 
2.56.0


Reply via email to