target/loongarch: Add wrapper function get_csr_offset()
Add wrapper function get_csr_offset(), it is to get offset from structure CPULoongArchState. There is no function change, and it is used for future LVZ feature. Signed-off-by: Bibo Mao <maobibo@loongson.cn> Reviewed-by: Philippe Mathieu-Daudé <philmd@mailo.com> Tested-by: Song Gao <gaosong@loongson.cn> Message-ID: <20260605083904.175636-1-maobibo@loongson.cn> Signed-off-by: Song Gao <gaosong@loongson.cn>
Bibo Mao committed
Jun 5, 2026 at 16:39 UTC
b1a48299dc0a449f0c7e414cc3d11bd78f053c00
3 files changed
+17
-7
target/loongarch/cpu.c
+2
-2
@@ -761,7 +761,7 @@ static void loongarch_cpu_dump_csr(CPUState *cs, FILE *f)
761
{
762
#ifndef CONFIG_USER_ONLY
763
CPULoongArchState *env = cpu_env(cs);
764
- CSRInfo *csr_info;
764
+ const CSRInfo *csr_info;
765
int64_t *addr;
766
int i, j, len, col = 0;
767
@@ -783,7 +783,7 @@ static void loongarch_cpu_dump_csr(CPUState *cs, FILE *f)
783
qemu_fprintf(f, " CSR%03d:", col);
784
}
785
786
- addr = (void *)env + csr_info->offset;
786
+ addr = (void *)env + get_csr_offset(csr_info, 0);
787
qemu_fprintf(f, " %s ", csr_info->name);
788
len = strlen(csr_info->name);
789
for (; len < 6; len++) {
target/loongarch/csr.h
+4
@@ -27,4 +27,8 @@ typedef struct {
27
28
CSRInfo *get_csr(unsigned int csr_num);
29
bool set_csr_flag(unsigned int csr_num, int flag);
30
+static inline unsigned int get_csr_offset(const CSRInfo *csr, int vm_level)
31
+{
32
+ return csr->offset;
33
+}
34
#endif /* TARGET_LOONGARCH_CSR_H */
target/loongarch/tcg/insn_trans/trans_privileged.c.inc
+11
-5
@@ -106,6 +106,7 @@ static bool trans_csrrd(DisasContext *ctx, arg_csrrd *a)
106
TCGv dest;
107
const CSRInfo *csr;
108
GenCSRRead readfn;
109
+ tcg_target_long offset;
110
111
if (check_plv(ctx)) {
112
return false;
@@ -121,7 +122,8 @@ static bool trans_csrrd(DisasContext *ctx, arg_csrrd *a)
122
if (readfn) {
123
readfn(dest, tcg_env);
124
} else {
124
- tcg_gen_ld_tl(dest, tcg_env, csr->offset);
125
+ offset = get_csr_offset(csr, 0);
126
+ tcg_gen_ld_tl(dest, tcg_env, offset);
127
}
128
}
129
gen_set_gpr(a->rd, dest, EXT_NONE);
@@ -133,6 +135,7 @@ static bool trans_csrwr(DisasContext *ctx, arg_csrwr *a)
135
TCGv dest, src1;
136
const CSRInfo *csr;
137
GenCSRWrite writefn;
138
+ tcg_target_long offset;
139
140
if (check_plv(ctx)) {
141
return false;
@@ -154,8 +157,9 @@ static bool trans_csrwr(DisasContext *ctx, arg_csrwr *a)
157
writefn(dest, tcg_env, src1);
158
} else {
159
dest = tcg_temp_new();
157
- tcg_gen_ld_tl(dest, tcg_env, csr->offset);
158
- tcg_gen_st_tl(src1, tcg_env, csr->offset);
160
+ offset = get_csr_offset(csr, 0);
161
+ tcg_gen_ld_tl(dest, tcg_env, offset);
162
+ tcg_gen_st_tl(src1, tcg_env, offset);
163
}
164
gen_set_gpr(a->rd, dest, EXT_NONE);
165
return true;
@@ -166,6 +170,7 @@ static bool trans_csrxchg(DisasContext *ctx, arg_csrxchg *a)
170
TCGv src1, mask, oldv, newv, temp;
171
const CSRInfo *csr;
172
GenCSRWrite writefn;
173
+ tcg_target_long offset;
174
175
if (check_plv(ctx)) {
176
return false;
@@ -191,7 +196,8 @@ static bool trans_csrxchg(DisasContext *ctx, arg_csrxchg *a)
196
newv = tcg_temp_new();
197
temp = tcg_temp_new();
198
194
- tcg_gen_ld_tl(oldv, tcg_env, csr->offset);
199
+ offset = get_csr_offset(csr, 0);
200
+ tcg_gen_ld_tl(oldv, tcg_env, offset);
201
tcg_gen_and_tl(newv, src1, mask);
202
tcg_gen_andc_tl(temp, oldv, mask);
203
tcg_gen_or_tl(newv, newv, temp);
@@ -200,7 +206,7 @@ static bool trans_csrxchg(DisasContext *ctx, arg_csrxchg *a)
206
if (writefn) {
207
writefn(oldv, tcg_env, newv);
208
} else {
203
- tcg_gen_st_tl(newv, tcg_env, csr->offset);
209
+ tcg_gen_st_tl(newv, tcg_env, offset);
210
}
211
gen_set_gpr(a->rd, oldv, EXT_NONE);
212
return true;