@samitouri / QOSamiQemu / commits / 4a5d2c649d

target/loongarch: Add macro CSR_OFFSET and CPU_CSR_OFFSET

Instruction rdtime is to read TSC timestamp and logic vCPU id, it is also used by Linux user mode emulation. However function get_csr_offset() cannot be called in user mode emulation, here macro CSR_OFFSET and CPU_CSR_OFFSET are added. The added macro can be called in both user emulation and system emulation. 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: <20260605083910.175647-1-maobibo@loongson.cn> Signed-off-by: Song Gao <gaosong@loongson.cn>

Bibo Mao committed Jun 5, 2026 at 16:39 UTC 4a5d2c649dabee3d1cbfd2e91353e5a94576a871
3 files changed +10 -3
target/loongarch/csr.c
+2 -2
@@ -9,14 +9,14 @@
9 #define CSR_OFF_FUNCS(NAME, FL, RD, WR) \
10 [LOONGARCH_CSR_##NAME] = { \
11 .name = (stringify(NAME)), \
12 - .offset = offsetof(CPULoongArchState, CSR_##NAME), \
12 + .offset = CSR_OFFSET(CSR_##NAME), \
13 .flags = FL, .readfn = RD, .writefn = WR \
14 }
15
16 #define CSR_OFF_ARRAY(NAME, N) \
17 [LOONGARCH_CSR_##NAME(N)] = { \
18 .name = (stringify(NAME##N)), \
19 - .offset = offsetof(CPULoongArchState, CSR_##NAME[N]), \
19 + .offset = CSR_OFFSET(CSR_##NAME[N]), \
20 .flags = CSRFL_BASIC, .readfn = NULL, .writefn = NULL \
21 }
22
target/loongarch/csr.h
+3
@@ -8,6 +8,9 @@
8
9 #include "cpu-csr.h"
10
11 +#define CSR_OFFSET(id) offsetof(CPULoongArchState, id)
12 +#define CPU_CSR_OFFSET(id, vm_level) CSR_OFFSET(id)
13 +
14 typedef void (*GenCSRFunc)(void);
15 enum {
16 CSRFL_READONLY = (1 << 0),
target/loongarch/tcg/insn_trans/trans_extra.c.inc
+5 -1
@@ -3,6 +3,7 @@
3 * Copyright (c) 2021 Loongson Technology Corporation Limited
4 */
5
6 +#include "csr.h"
7 static bool trans_break(DisasContext *ctx, arg_break *a)
8 {
9 generate_exception(ctx, EXCCODE_BRK);
@@ -46,13 +47,16 @@ static bool gen_rdtime(DisasContext *ctx, arg_rr *a,
47 {
48 TCGv dst1 = gpr_dst(ctx, a->rd, EXT_NONE);
49 TCGv dst2 = gpr_dst(ctx, a->rj, EXT_NONE);
50 + tcg_target_long offset;
51
52 translator_io_start(&ctx->base);
53 gen_helper_rdtime_d(dst1, tcg_env);
54 if (word) {
55 tcg_gen_sextract_tl(dst1, dst1, high ? 32 : 0, 32);
56 }
55 - tcg_gen_ld_i64(dst2, tcg_env, offsetof(CPULoongArchState, CSR_TID));
57 +
58 + offset = CPU_CSR_OFFSET(CSR_TID, 0);
59 + tcg_gen_ld_i64(dst2, tcg_env, offset);
60
61 return true;
62 }