[PATCH] target/loongarch: Replace sys_state with vm_level

Bibo Mao posted 1 patch 3 weeks, 6 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261026095922.241092-1-maobibo@loongson.cn
Maintainers: Song Gao <17746591750@163.com>, Bibo Mao <maobibo@loongson.cn>, Xianglai Li <lixianglai@loongson.cn>
There is a newer version of this series
target/loongarch/cpu.h | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
[PATCH] target/loongarch: Replace sys_state with vm_level
Posted by Bibo Mao 3 weeks, 6 days ago
Int type vm_level is more meaningful than pointer type sys_state,
and it is easy to get current mode from value of vm_level.

Here replace sys_state with vm_level, and use vm_level directly.

Signed-off-by: Bibo Mao <maobibo@loongson.cn>
---
 target/loongarch/cpu.h | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/target/loongarch/cpu.h b/target/loongarch/cpu.h
index 5cc2370da2..57cb893d95 100644
--- a/target/loongarch/cpu.h
+++ b/target/loongarch/cpu.h
@@ -429,7 +429,7 @@ typedef struct CPUArchState {
     /* Fields up to this point are cleared by a CPU reset */
     struct {} end_reset_fields;
 
-    CPUSysState sys_states[1];
+    CPUSysState sys_states[VM_LEVELS];
     uint32_t cpucfg[21];
     uint32_t pv_features;
     uint64_t vendor_id;
@@ -440,7 +440,7 @@ typedef struct CPUArchState {
     AddressSpace *address_space_iocsr;
     uint32_t mp_state;
 #endif
-    CPUSysState *sys_state;
+    int vm_level;
     unsigned int irq_nr;
 } CPULoongArchState;
 
@@ -510,12 +510,14 @@ struct LoongArchCPUClass {
 
 static inline CPUSysState *env_sys(CPULoongArchState *env)
 {
-    return env->sys_state;
+    return env->sys_states + env->vm_level;
 }
 
 static inline void set_sys(CPULoongArchState *env, int vm_level)
 {
-    env->sys_state = &env->sys_states[vm_level];
+    if (vm_level >= 0 && vm_level <  VM_LEVELS) {
+        env->vm_level = vm_level;
+    }
 }
 
 static inline CPUTimerState *env_timer(CPULoongArchState *env)

base-commit: d73149283205645625bfa9fe029cfb68e4c4a9ae
-- 
2.54.0
Re: [PATCH] target/loongarch: Replace sys_state with vm_level
Posted by Philippe Mathieu-Daudé 3 weeks, 6 days ago
On 26/10/26 10:59, Bibo Mao wrote:
> Int type vm_level is more meaningful than pointer type sys_state,
> and it is easy to get current mode from value of vm_level.
> 
> Here replace sys_state with vm_level, and use vm_level directly.
> 
> Signed-off-by: Bibo Mao <maobibo@loongson.cn>
> ---
>   target/loongarch/cpu.h | 10 ++++++----
>   1 file changed, 6 insertions(+), 4 deletions(-)


> -    CPUSysState sys_states[1];
> +    CPUSysState sys_states[VM_LEVELS];
>       uint32_t cpucfg[21];
>       uint32_t pv_features;
>       uint64_t vendor_id;
> @@ -440,7 +440,7 @@ typedef struct CPUArchState {
>       AddressSpace *address_space_iocsr;
>       uint32_t mp_state;
>   #endif
> -    CPUSysState *sys_state;
> +    int vm_level;
>       unsigned int irq_nr;
>   } CPULoongArchState;
>   
> @@ -510,12 +510,14 @@ struct LoongArchCPUClass {
>   
>   static inline CPUSysState *env_sys(CPULoongArchState *env)
>   {
> -    return env->sys_state;
> +    return env->sys_states + env->vm_level;

Correct but easier to reason as:

       return env->sys_states[env->vm_level];

With that:
Reviewed-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>

>   }

Re: [PATCH] target/loongarch: Replace sys_state with vm_level
Posted by Bibo Mao 3 weeks, 5 days ago

On 2026/10/26 下午6:09, Philippe Mathieu-Daudé wrote:
> On 26/10/26 10:59, Bibo Mao wrote:
>> Int type vm_level is more meaningful than pointer type sys_state,
>> and it is easy to get current mode from value of vm_level.
>>
>> Here replace sys_state with vm_level, and use vm_level directly.
>>
>> Signed-off-by: Bibo Mao <maobibo@loongson.cn>
>> ---
>>   target/loongarch/cpu.h | 10 ++++++----
>>   1 file changed, 6 insertions(+), 4 deletions(-)
> 
> 
>> -    CPUSysState sys_states[1];
>> +    CPUSysState sys_states[VM_LEVELS];
>>       uint32_t cpucfg[21];
>>       uint32_t pv_features;
>>       uint64_t vendor_id;
>> @@ -440,7 +440,7 @@ typedef struct CPUArchState {
>>       AddressSpace *address_space_iocsr;
>>       uint32_t mp_state;
>>   #endif
>> -    CPUSysState *sys_state;
>> +    int vm_level;
>>       unsigned int irq_nr;
>>   } CPULoongArchState;
>> @@ -510,12 +510,14 @@ struct LoongArchCPUClass {
>>   static inline CPUSysState *env_sys(CPULoongArchState *env)
>>   {
>> -    return env->sys_state;
>> +    return env->sys_states + env->vm_level;
> 
> Correct but easier to reason as:
> 
>        return env->sys_states[env->vm_level];
Sure, will change in this way with next version.

Regards
Bibo Mao
> 
> With that:
> Reviewed-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
> 
>>   }