Skip to contentSkip to conversation

Hexagon: fix lock, priv bugs

PATCH In review Brian Cain 2 participants 7 messages Oct 14 Arm/Amlogic Meson SoC support

lore
Share & tools
lore

Brian Cain

author
#0 lore raw

v2 cover Hexagon: fix lock, priv bugs

Testing with h2 and linux has uncovered some bugs in the way the locks are implemented and privileged instructions.

Note that this will be rebased to make adjustments when the *next_pc removals
land. See https://lore.kernel.org/qemu-devel/[email protected]/

In v2:
- now rebased after *next_pc state removal, hex_tlb_lock() now receives the packet PC, records that PC before halting the CPU, then exits TB.
- minivm test now preserves sparse test_mmu file in the archive, so extraction is fast, disk space required is small. Removed skipBigDataTest filter and subTest() hack.

v1: https://lore.kernel.org/qemu-devel/[email protected]/

Brian Cain (5):
target/hexagon: check privilege mode for system instructions target/hexagon: wake K0 and TLB lock waiters target/hexagon: halt on tlblock when the TLB lock is contended target/hexagon: set a started thread's enable bit when start retires tests/functional/hexagon: add MiniVM, lock tests

 target/hexagon/cpu.h                      |  1 +
 target/hexagon/translate.h                |  1 +
 target/hexagon/cpu.c                      |  2 +
 target/hexagon/cpu_helper.c               |  6 +++
 target/hexagon/hex_interrupts.c           |  8 +++-
 target/hexagon/hex_mmu.c                  |  6 +++
 target/hexagon/op_helper.c                |  2 +
 target/hexagon/translate.c                | 15 +++++++
 target/hexagon/gen_tcg_funcs.py           | 16 +++++---
 tests/functional/hexagon/meson.build      |  1 +
 tests/functional/hexagon/test_minivm.py   | 48 +++++++++++++++++++++++
 tests/functional/hexagon/test_systests.py |  3 ++
 12 files changed, 101 insertions(+), 8 deletions(-)
 create mode 100755 tests/functional/hexagon/test_minivm.py
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 1/5 target/hexagon: check privilege mode for system instructions

Fixes 9831d3cab168 ("target/hexagon: Add privilege check, use tag_ignore()")
---
 target/hexagon/cpu.h            |  1 +
 target/hexagon/translate.h      |  1 +
 target/hexagon/cpu.c            |  2 ++
 target/hexagon/translate.c      | 15 +++++++++++++++
 target/hexagon/gen_tcg_funcs.py | 16 ++++++++++------
 5 files changed, 29 insertions(+), 6 deletions(-)
target/hexagon/cpu.h +1 -0
index 65fd2b29830..649e33de020 100644
@@ -228,6 +228,7 @@ FIELD(TB_FLAGS, IS_TIGHT_LOOP, 0, 1)
 FIELD(TB_FLAGS, MMU_INDEX, 1, 3)
 FIELD(TB_FLAGS, PCYCLE_ENABLED, 4, 1)
 FIELD(TB_FLAGS, HVX_COPROC_ENABLED, 5, 1)
+FIELD(TB_FLAGS, CPU_MODE, 6, 2)
 
 G_NORETURN void hexagon_raise_exception_err(CPUHexagonState *env,
                                             uint32_t exception,
target/hexagon/translate.h +1 -0
index 2d1ed5208b7..15918f2c52c 100644
@@ -45,6 +45,7 @@ typedef struct DisasContext {
     DECLARE_BITMAP(gpr_uncond, TOTAL_PER_THREAD_REGS);
     bool implicit_usr_write;
 #ifndef CONFIG_USER_ONLY
+    uint32_t cpu_mode;
     int greg_log[GREG_WRITES_MAX];
     int greg_log_idx;
     DECLARE_BITMAP(gregs_written, NUM_GREGS);
target/hexagon/cpu.c +2 -0
index 6550e7fa800..f58e4596039 100644
@@ -353,6 +353,8 @@ static TCGTBCPUState hexagon_get_tb_cpu_state(CPUState *cs)
     hex_flags = FIELD_DP32(hex_flags, TB_FLAGS, HVX_COPROC_ENABLED,
                            cpu->hvx_ctx[0] &&
                            GET_SSR_FIELD(SSR_XE, env->t_sreg[HEX_SREG_SSR]));
+    hex_flags = FIELD_DP32(hex_flags, TB_FLAGS, CPU_MODE,
+                           get_cpu_mode(env));
 #else
     hex_flags = FIELD_DP32(hex_flags, TB_FLAGS, MMU_INDEX, MMU_USER_IDX);
 #endif
target/hexagon/translate.c +15 -0
index cd1abdf1461..72dd1fdbbb2 100644
@@ -1306,6 +1306,20 @@ static void decode_and_translate_packet(CPUHexagonState *env, DisasContext *ctx)
                                       HEX_CAUSE_REG_WRITE_CONFLICT);
             return;
         }
+#ifndef CONFIG_USER_ONLY
+        if (check_for_attrib(&ctx->pkt, A_PRIV) &&
+            ctx->cpu_mode != HEX_CPU_MODE_MONITOR) {
+            hex_gen_exception_end_tb(ctx, HEX_CAUSE_PRIV_USER_NO_SINSN);
+            ctx->base.pc_next += ctx->pkt.encod_pkt_size_in_bytes;
+            return;
+        }
+        if (check_for_attrib(&ctx->pkt, A_GUEST) &&
+            ctx->cpu_mode == HEX_CPU_MODE_USER) {
+            hex_gen_exception_end_tb(ctx, HEX_CAUSE_PRIV_USER_NO_GINSN);
+            ctx->base.pc_next += ctx->pkt.encod_pkt_size_in_bytes;
+            return;
+        }
+#endif
         gen_start_packet(ctx);
         for (i = 0; i < ctx->pkt.num_insns; i++) {
             ctx->insn = &ctx->pkt.insn[i];
@@ -1340,6 +1354,7 @@ static void hexagon_tr_init_disas_context(DisasContextBase *dcbase,
     ctx->hvx_coproc_enabled =
         FIELD_EX32(hex_flags, TB_FLAGS, HVX_COPROC_ENABLED);
     ctx->hvx_check_emitted = false;
+    ctx->cpu_mode = FIELD_EX32(hex_flags, TB_FLAGS, CPU_MODE);
 #endif
 }
 
target/hexagon/gen_tcg_funcs.py +10 -6
index b09ce7c285f..61effa38cfa 100755
@@ -58,16 +58,20 @@ def gen_tcg_func(f, tag, regs, imms):
 
     f.write("    Insn *insn G_GNUC_UNUSED = ctx->insn;\n")
 
-    if "A_PRIV" in hex_common.attribdict[tag]:
+    if (
+        "A_PRIV" in hex_common.attribdict[tag]
+        or "A_GUEST" in hex_common.attribdict[tag]
+    ):
         f.write(dedent("""\
 #ifdef CONFIG_USER_ONLY
-    hex_gen_exception_end_tb(ctx, HEX_CAUSE_PRIV_USER_NO_SINSN);
-#else
 """))
-    if "A_GUEST" in hex_common.attribdict[tag]:
+        if "A_PRIV" in hex_common.attribdict[tag]:
+            f.write("    hex_gen_exception_end_tb(ctx, "
+                    "HEX_CAUSE_PRIV_USER_NO_SINSN);\n")
+        else:
+            f.write("    hex_gen_exception_end_tb(ctx, "
+                    "HEX_CAUSE_PRIV_USER_NO_GINSN);\n")
         f.write(dedent("""\
-#ifdef CONFIG_USER_ONLY
-    hex_gen_exception_end_tb(ctx, HEX_CAUSE_PRIV_USER_NO_GINSN);
 #else
 """))
     if hex_common.need_ea(tag):
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH v2 2/5 target/hexagon: wake K0 and TLB lock waiters

In reply to Brian Cain

Wake the selected K0 or TLB lock waiter when ownership passes. Update its interrupt state and kick the vCPU.

---
 target/hexagon/cpu_helper.c     | 2 ++
 target/hexagon/hex_interrupts.c | 8 ++++++--
 target/hexagon/hex_mmu.c        | 3 +++
 target/hexagon/op_helper.c      | 2 ++
 4 files changed, 13 insertions(+), 2 deletions(-)
target/hexagon/cpu_helper.c +2 -0
index c2fc27f1c02..4e4a81c162d 100644
@@ -452,6 +452,7 @@ static uint32_t set_enable_mask(CPUHexagonState *env)
     thread_enabled_mask = GET_FIELD(MODECTL_E, modectl);
     thread_enabled_mask |= 0x1 << env->threadId;
     SET_SYSTEM_FIELD(env, HEX_SREG_MODECTL, MODECTL_E, thread_enabled_mask);
+    hex_interrupt_update(env);
     return thread_enabled_mask;
 }
 
@@ -472,6 +473,7 @@ static uint32_t clear_enable_mask(CPUHexagonState *env)
     thread_enabled_mask = GET_FIELD(MODECTL_E, modectl);
     thread_enabled_mask &= ~(0x1 << env->threadId);
     SET_SYSTEM_FIELD(env, HEX_SREG_MODECTL, MODECTL_E, thread_enabled_mask);
+    hex_interrupt_update(env);
     return thread_enabled_mask;
 }
 static void do_start_thread(CPUState *cs, run_on_cpu_data tbd)
target/hexagon/hex_interrupts.c +6 -2
index ab66d548a1e..03e1f28da9b 100644
@@ -240,8 +240,10 @@ static bool hex_is_qualified_for_int(CPUHexagonState *env, int int_num)
     bool ssr_ie = get_ssr_ie(env);
     bool ssr_ex = get_ssr_ex(env);
     bool imask = get_imask_bit(env, int_num);
+    bool lock_pending = env->k0_lock_state != HEX_LOCK_UNLOCKED ||
+                        env->tlb_lock_state != HEX_LOCK_UNLOCKED;
 
-    return syscfg_gie && !iad && ssr_ie && !ssr_ex && !imask;
+    return syscfg_gie && !iad && ssr_ie && !ssr_ex && !imask && !lock_pending;
 }
 
 static void clear_pending_locks(CPUHexagonState *env)
@@ -257,7 +259,9 @@ static void clear_pending_locks(CPUHexagonState *env)
 
 static bool should_not_exec(CPUHexagonState *env)
 {
-    return (get_exe_mode(env) == HEX_EXE_MODE_WAIT);
+    return get_exe_mode(env) == HEX_EXE_MODE_WAIT ||
+           env->k0_lock_state == HEX_LOCK_WAITING ||
+           env->tlb_lock_state == HEX_LOCK_WAITING;
 }
 
 static void restore_state(CPUHexagonState *env, bool int_accepted)
target/hexagon/hex_mmu.c +3 -0
index 0ca52ed666d..637873b2668 100644
@@ -19,6 +19,7 @@
 #include "sys_macros.h"
 #include "hw/hexagon/hexagon_tlb.h"
 #include "hw/hexagon/hexagon_globalreg.h"
+#include "hex_interrupts.h"
 
 static inline void hex_log_tlbw(uint32_t index, uint64_t entry)
 {
@@ -271,6 +272,8 @@ void hex_tlb_unlock(CPUHexagonState *env)
         unlock_thread->tlb_lock_state = HEX_LOCK_QUEUED;
         SET_SYSCFG_FIELD(unlock_thread, SYSCFG_TLBLOCK, 1);
         cpu_interrupt(cs, CPU_INTERRUPT_TLB_UNLOCK);
+        hex_interrupt_update(unlock_thread);
+        qemu_cpu_kick(cs);
     }
 
     if (qemu_loglevel_mask(CPU_LOG_MMU)) {
target/hexagon/op_helper.c +2 -0
index accadd7927a..5bdb4a6e882 100644
@@ -1397,6 +1397,8 @@ static void hex_k0_unlock(CPUHexagonState *env)
         unlock_thread->k0_lock_state = HEX_LOCK_QUEUED;
         SET_SYSCFG_FIELD(unlock_thread, SYSCFG_K0LOCK, 1);
         cpu_interrupt(cs, CPU_INTERRUPT_K0_UNLOCK);
+        hex_interrupt_update(unlock_thread);
+        qemu_cpu_kick(cs);
     }
 
 }
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH v2 3/5 target/hexagon: halt on tlblock when the TLB lock is contended

In reply to Brian Cain

Set PC to the tlblock instruction and leave the CPU loop when the lock is contended. This retries tlblock after ownership is passed and prevents execution without the lock.

---
 target/hexagon/hex_mmu.c | 3 +++
 1 file changed, 3 insertions(+)
target/hexagon/hex_mmu.c +3 -0
index 637873b2668..79a19797a05 100644
@@ -12,6 +12,7 @@
 #include "system/cpus.h"
 #include "internal.h"
 #include "exec/cpu-interrupt.h"
+#include "accel/tcg/cpu-loop.h"
 #include "cpu_helper.h"
 #include "exec/cputlb.h"
 #include "hex_mmu.h"
@@ -180,6 +181,7 @@ void hex_tlb_lock(CPUHexagonState *env, uint32_t PC)
                 ", thread may hang\n", PC);
             CPUState *cs = env_cpu(env);
             cpu_interrupt(cs, CPU_INTERRUPT_HALT);
+            cpu_loop_exit(cs);
             return;
         }
         env->tlb_lock_state = HEX_LOCK_WAITING;
@@ -191,6 +193,7 @@ void hex_tlb_lock(CPUHexagonState *env, uint32_t PC)
         env->gpr[HEX_REG_PC] = PC;
         CPUState *cs = env_cpu(env);
         cpu_interrupt(cs, CPU_INTERRUPT_HALT);
+        cpu_loop_exit(cs);
     } else {
         env->tlb_lock_count++;
         env->tlb_lock_state = HEX_LOCK_OWNER;
Signature · 1 line
2.34.1
author
#4 lore raw

PATCH v2 4/5 target/hexagon: set a started thread's enable bit when start retires

In reply to Brian Cain

Set MODECTL.E when start retires rather than when the target thread runs. This makes the thread state visible to software immediately after start.

---
 target/hexagon/cpu_helper.c | 4 ++++
 1 file changed, 4 insertions(+)
target/hexagon/cpu_helper.c +4 -0
index 4e4a81c162d..689f79ca934 100644
@@ -496,6 +496,9 @@ static void do_start_thread(CPUState *cs, run_on_cpu_data tbd)
 void hexagon_start_threads(CPUHexagonState *current_env, uint32_t mask)
 {
     CPUState *cs;
+
+    BQL_LOCK_GUARD();
+
     CPU_FOREACH(cs) {
         CPUHexagonState *env = cpu_env(cs);
         if (!(mask & (0x1 << env->threadId))) {
@@ -503,6 +506,7 @@ void hexagon_start_threads(CPUHexagonState *current_env, uint32_t mask)
         }
 
         if (current_env->threadId != env->threadId) {
+            set_enable_mask(env);
             async_safe_run_on_cpu(cs, do_start_thread, RUN_ON_CPU_NULL);
         }
     }
Signature · 1 line
2.34.1
author
#5 lore raw

PATCH v2 5/5 tests/functional/hexagon: add MiniVM, lock tests

In reply to Brian Cain

Add the lock_verify test case to verify the k0/tlblock behavior, add the minivm tests to cover start() and more.

---
 tests/functional/hexagon/meson.build      |  1 +
 tests/functional/hexagon/test_minivm.py   | 48 +++++++++++++++++++++++
 tests/functional/hexagon/test_systests.py |  3 ++
 3 files changed, 52 insertions(+)
 create mode 100755 tests/functional/hexagon/test_minivm.py
tests/functional/hexagon/meson.build +1 -0
index 44053e64676..e182bc98a06 100644
@@ -3,6 +3,7 @@
 tests_hexagon_system_thorough = [
   'arch_tests',
   'systests',
+  'minivm',
 ]
 
 test_hexagon_timeouts = {
tests/functional/hexagon/test_minivm.py new file +48 -0

48 lines not on this page · raw message

tests/functional/hexagon/test_systests.py +3 -0
index 20c58f84f67..0489db0af91 100755
@@ -108,5 +108,8 @@ def test_hvx_multi(self):
     def test_standalone_vec(self):
         self.run_exit_zero("standalone_vec")
 
+    def test_lock_verify(self):
+        self.run_exit_zero("lock_verify")
+
 if __name__ == "__main__":
     QemuSystemTest.main()
Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH v2 5/5] tests/functional/hexagon: add MiniVM, lock tests

Quoted 83 lines · Brian Cain

Add the lock_verify test case to verify the k0/tlblock behavior, add the minivm tests to cover start() and more.

---
 tests/functional/hexagon/meson.build      |  1 +
 tests/functional/hexagon/test_minivm.py   | 48 +++++++++++++++++++++++
 tests/functional/hexagon/test_systests.py |  3 ++
 3 files changed, 52 insertions(+)
 create mode 100755 tests/functional/hexagon/test_minivm.py
tests/functional/hexagon/meson.build +1 -0
index 44053e64676..e182bc98a06 100644
@@ -3,6 +3,7 @@
 tests_hexagon_system_thorough = [
   'arch_tests',
   'systests',
+  'minivm',
 ]
 
 test_hexagon_timeouts = {
tests/functional/hexagon/test_minivm.py new file +48 -0
new file mode 100755
index 00000000000..115f3cf3710
@@ -0,0 +1,48 @@
+#!/usr/bin/env python3
+#
+# Copyright(c) 2024-2025 Qualcomm Innovation Center, Inc. All Rights Reserved.
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+from qemu_test import QemuSystemTest, Asset
+
+class MiniVMTest(QemuSystemTest):
+    '''
+    minivm is a Hexagon hypervisor that implements the Hexagon VM
+    specification.  These test cases boot minivm and then load test cases
+    to the address specified by MiniVMTest.GUEST_ENTRY and
+    execute a minvm-guest program to exercise minivm virtualization
+    features.
+    '''
+    timeout = 180
+    GUEST_ENTRY = 0xc0000000
+    ASSET_TARBALL = Asset(
+        'https://github.com/qualcomm/qemu-hexagon-testing/releases/'
+        'download/v0.2.21/hexagon_minivm.tar.gz',
+        '5980740c5fa505f55cacd1646088831202df4ccbf9e2ddc34b690673574169ba')
+
+    def run_minivm_test(self, test_case):
+        self.set_machine('sim')
+        self.archive_extract(self.ASSET_TARBALL)
+        rootfs_path = self.scratch_file('hexagon-unknown-linux-musl-rootfs')
+        kernel_path = f'{rootfs_path}/boot/minivm'
+
+        vm = self.get_vm(name=test_case)
+        vm.add_args('-kernel', kernel_path, '-device',
+                    f'loader,addr={hex(self.GUEST_ENTRY)},'
+                    f'file={rootfs_path}/boot/{test_case}')
+        vm.launch()
+        vm.wait()
+        self.assertEqual(vm.exitcode(), 0)
+
+    def test_mmu(self):
+        self.run_minivm_test('test_mmu')
+
+    def test_interrupts(self):
+        self.run_minivm_test('test_interrupts')
+
+    def test_processors(self):
+        self.run_minivm_test('test_processors')
+
+if __name__ == '__main__':
+    QemuSystemTest.main()
tests/functional/hexagon/test_systests.py +0 -0
index 20c58f84f67..0489db0af91 100755

tests/functional/hexagon/test_systests.py def test_hvx_multi(self): · line 115

Quoted 8 lines
tests/functional/hexagon/test_systests.py +3 -0
@@ -108,5 +108,8 @@ def test_hvx_multi(self):
     def test_standalone_vec(self):
         self.run_exit_zero("standalone_vec")
 
+    def test_lock_verify(self):
+        self.run_exit_zero("lock_verify")
+
 if __name__ == "__main__":
     QemuSystemTest.main()

Looks good now, that's a great size reduction indeed!

Regards,
Pierrick