Skip to content

Commit 90f73c2

Browse files
committed
target/loongarch: Add dynamic function access with CSR register
With CSR register, dynamic function access is used for CSR register access in TCG mode, so that csr info can be used by other modules. Signed-off-by: Bibo Mao <maobibo@loongson.cn>
1 parent cf86770 commit 90f73c2

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

target/loongarch/tcg/insn_trans/trans_privileged.c.inc

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ enum {
7676
#define CSR_OFF(NAME) \
7777
CSR_OFF_FLAGS(NAME, 0)
7878

79-
static const CSRInfo csr_info[] = {
79+
static CSRInfo csr_info[] = {
8080
CSR_OFF_FLAGS(CRMD, CSRFL_EXITTB),
8181
CSR_OFF(PRMD),
8282
CSR_OFF_FLAGS(EUEN, CSRFL_EXITTB),
@@ -160,9 +160,9 @@ static bool check_plv(DisasContext *ctx)
160160
return false;
161161
}
162162

163-
static const CSRInfo *get_csr(unsigned csr_num)
163+
static CSRInfo *get_csr(unsigned csr_num)
164164
{
165-
const CSRInfo *csr;
165+
CSRInfo *csr;
166166

167167
if (csr_num >= ARRAY_SIZE(csr_info)) {
168168
return NULL;
@@ -174,6 +174,37 @@ static const CSRInfo *get_csr(unsigned csr_num)
174174
return csr;
175175
}
176176

177+
static bool set_csr_trans_func(unsigned int csr_num, GenCSRRead readfn,
178+
GenCSRWrite writefn)
179+
{
180+
CSRInfo *csr;
181+
182+
csr = get_csr(csr_num);
183+
if (!csr) {
184+
return false;
185+
}
186+
187+
csr->readfn = readfn;
188+
csr->writefn = writefn;
189+
return true;
190+
}
191+
192+
#define SET_CSR_FUNC(NAME, read, write) \
193+
set_csr_trans_func(LOONGARCH_CSR_##NAME, read, write)
194+
195+
void loongarch_csr_translate_init(void)
196+
{
197+
SET_CSR_FUNC(ESTAT, NULL, gen_helper_csrwr_estat);
198+
SET_CSR_FUNC(ASID, NULL, gen_helper_csrwr_asid);
199+
SET_CSR_FUNC(PGD, gen_helper_csrrd_pgd, NULL);
200+
SET_CSR_FUNC(PWCL, NULL, gen_helper_csrwr_pwcl);
201+
SET_CSR_FUNC(CPUID, gen_helper_csrrd_cpuid, NULL);
202+
SET_CSR_FUNC(TCFG, NULL, gen_helper_csrwr_tcfg);
203+
SET_CSR_FUNC(TVAL, gen_helper_csrrd_tval, NULL);
204+
SET_CSR_FUNC(TICLR, NULL, gen_helper_csrwr_ticlr);
205+
}
206+
#undef SET_CSR_FUNC
207+
177208
static bool check_csr_flags(DisasContext *ctx, const CSRInfo *csr, bool write)
178209
{
179210
if ((csr->flags & CSRFL_READONLY) && write) {

target/loongarch/tcg/tcg_loongarch.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later */
2+
/*
3+
* QEMU LoongArch TCG interface
4+
*
5+
* Copyright (c) 2025 Loongson Technology Corporation Limited
6+
*/
7+
#ifndef TARGET_LOONGARCH_TCG_LOONGARCH_H
8+
#define TARGET_LOONGARCH_TCG_LOONGARCH_H
9+
10+
void loongarch_csr_translate_init(void);
11+
12+
#endif /* TARGET_LOONGARCH_TCG_LOONGARCH_H */

target/loongarch/tcg/translate.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "exec/log.h"
1717
#include "qemu/qemu-print.h"
1818
#include "fpu/softfloat.h"
19+
#include "tcg_loongarch.h"
1920
#include "translate.h"
2021
#include "internals.h"
2122
#include "vec.h"
@@ -358,4 +359,8 @@ void loongarch_translate_init(void)
358359
offsetof(CPULoongArchState, lladdr), "lladdr");
359360
cpu_llval = tcg_global_mem_new(tcg_env,
360361
offsetof(CPULoongArchState, llval), "llval");
362+
363+
#ifndef CONFIG_USER_ONLY
364+
loongarch_csr_translate_init();
365+
#endif
361366
}

0 commit comments

Comments
 (0)