qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Richard Henderson <richard.henderson@linaro.org>
To: Xiaojuan Yang <yangxiaojuan@loongson.cn>, qemu-devel@nongnu.org
Cc: mark.cave-ayland@ilande.co.uk, Song Gao <gaosong@loongson.cn>
Subject: Re: [RFC PATCH v7 07/29] target/loongarch: Add LoongArch CSR instruction
Date: Mon, 28 Mar 2022 12:34:13 -0600	[thread overview]
Message-ID: <aa21e33f-6729-d148-96e6-1f0617ddbc39@linaro.org> (raw)
In-Reply-To: <20220328125749.2918087-8-yangxiaojuan@loongson.cn>

On 3/28/22 06:57, Xiaojuan Yang wrote:
> +#define CSR_OFF(X)  \
> +           [LOONGARCH_CSR_##X] = offsetof(CPULoongArchState, CSR_##X)
> +#define CSR_OFF_ARRAY(X, N)  \
> +           [LOONGARCH_CSR_##X(N)] = offsetof(CPULoongArchState, CSR_##X[N])
> +
> +static const int csr_offsets[] = {

You cannot put a variable data definition into a header file like this.
It has put this data structure into every object file.

This belongs in csr_helper.c, probably.

You should add

   [LOONGARCH_CSR_CPUID] = offsetof(CPUState, cpu_index) - offsetof(ArchCPU, env),

rather than special-casing this in helper_csr_rdq.

> +static inline int cpu_csr_offset(unsigned csr_num)
> +{
> +    if (csr_num < ARRAY_SIZE(csr_offsets)) {
> +        return csr_offsets[csr_num];
> +    }
> +    return 0;
> +}

This does not need to be inline, and could easily live in csr_helper.c.

> +target_ulong helper_csr_rdq(CPULoongArchState *env, uint64_t csr)
> +{
> +    LoongArchCPU *cpu;
> +    int64_t v;
> +
> +    switch (csr) {
> +    case LOONGARCH_CSR_PGD:
> +        if (env->CSR_TLBRERA & 0x1) {
> +            v = env->CSR_TLBRBADV;
> +        } else {
> +            v = env->CSR_BADV;
> +        }
> +
> +        if ((v >> 63) & 0x1) {
> +            v = env->CSR_PGDH;
> +        } else {
> +            v = env->CSR_PGDL;
> +        }
> +        break;
> +    case LOONGARCH_CSR_CPUID:
> +        v = (env_cpu(env))->cpu_index;
> +        break;
> +    case LOONGARCH_CSR_TVAL:
> +        cpu = LOONGARCH_CPU(env_cpu(env));
> +        v = cpu_loongarch_get_constant_timer_ticks(cpu);
> +        break;
> +    default:
> +        break;
> +    }
> +
> +    return v;
> +}

You should have seen a compiler warning for 'v' uninitialized here, via the default path.

The default path should not be reachable, because of code in trans_csrrd, and so could be 
written as g_assert_not_reachable().  However, I strongly suggest you split this function 
so that you do not need a switch here at all.  With CPUID now handled via cpu_csr_offset, 
there are only two helpers needed.

> +target_ulong helper_csr_wrq(CPULoongArchState *env, target_ulong val,
> +                            uint64_t csr)
> +{
> +    LoongArchCPU *cpu;
> +    int64_t old_v = -1;
> +
> +    switch (csr) {
> +    case LOONGARCH_CSR_ESTAT:
> +        /* Only IS[1:0] can be write */

"can be written", and then again below.

> +        env->CSR_ESTAT = FIELD_DP64(env->CSR_ESTAT, CSR_ESTAT, IS, val & 0x3);
> +        break;
> +    case LOONGARCH_CSR_ASID:
> +        old_v = env->CSR_ASID;
> +        /* Only ASID filed of CSR_ASID can be write. */
> +        env->CSR_ASID = FIELD_DP64(env->CSR_ASID, CSR_ASID, ASID,
> +                                   val & R_CSR_ASID_ASID_MASK);
> +        if (old_v != val) {
> +            tlb_flush(env_cpu(env));
> +        }
> +        break;
> +    case LOONGARCH_CSR_TCFG:
> +        cpu = LOONGARCH_CPU(env_cpu(env));
> +        old_v = env->CSR_TCFG;
> +        cpu_loongarch_store_constant_timer_config(cpu, val);
> +        break;
> +    case LOONGARCH_CSR_TICLR:
> +        old_v = 0;
> +        env->CSR_ESTAT &= ~(1 << IRQ_TIMER);
> +        cpu_reset_interrupt(env_cpu(env), CPU_INTERRUPT_HARD);

Surely the TIMER irq is not the only interrupt.
The placement of the reset looks incorrect.

And again, I suggest that you *not* use a switch, but use separate helper functions.

> +target_ulong helper_csr_xchgq(CPULoongArchState *env, target_ulong new_val,
> +                              target_ulong mask, uint64_t csr_num)
> +{
> +    unsigned csr_offset = cpu_csr_offset(csr_num);
> +    if (csr_offset == 0) {
> +        /* Undefined CSR: read as 0, writes are ignored. */
> +        return 0;
> +    }
> +
> +    uint64_t *csr = (void *)env + csr_offset;
> +    uint64_t old_val = *csr;
> +
> +    new_val = (new_val & mask) | (old_val & ~mask);
> +
> +    if (csr_num == LOONGARCH_CSR_TCFG) {
> +        LoongArchCPU *cpu = LOONGARCH_CPU(env_cpu(env));
> +        cpu_loongarch_store_constant_timer_config(cpu, new_val);
> +    } else {
> +        *csr = new_val;

You're only handling one of the special cases from helper_csr_wrq, so I cannot believe 
this is correct.

I think you should not have a helper_csr_xchgq function, but reuse the read/write 
infrastructure from the other csr access instructions.  Note this would also fix...


> +static bool trans_csrwr(DisasContext *ctx, arg_csrwr *a)
> +{
> +    TCGv dest = gpr_dst(ctx, a->rd, EXT_NONE);
> +    TCGv src1 = gpr_src(ctx, a->rd, EXT_NONE);
> +
> +    if (check_plv(ctx) || ro_csr(a->csr)) {
> +        return false;
> +    }
> +
> +    unsigned csr_offset = cpu_csr_offset(a->csr);
> +    if (csr_offset == 0) {
> +        /* CSR is undefined: write ignored. */
> +        return true;
> +    }
> +
> +    if ((a->csr == LOONGARCH_CSR_ASID) || (a->csr == LOONGARCH_CSR_TCFG) ||
> +        (a->csr == LOONGARCH_CSR_TICLR) || (a->csr == LOONGARCH_CSR_ESTAT)) {
> +        gen_helper_csr_wrq(dest, cpu_env, src1, tcg_constant_i64(a->csr));

ASID change may result in page translation changes (which is why you did tlb_flush).  This 
also means that the page you are now executing could change translation, so you have to 
exit the translation block.

> +    } else {
> +        TCGv temp = tcg_temp_new();
> +        tcg_gen_ld_tl(temp, cpu_env, csr_offset);
> +        tcg_gen_st_tl(src1, cpu_env, csr_offset);
> +        tcg_gen_mov_tl(dest, temp);
> +        tcg_temp_free(temp);
> +
> +        /* Cpu state may be changed, need exit */
> +        if ((a->csr == LOONGARCH_CSR_CRMD) || (a->csr == LOONGARCH_CSR_EUEN)) {
> +            tcg_gen_movi_tl(cpu_pc, ctx->base.pc_next + 4);
> +            ctx->base.is_jmp = DISAS_EXIT;

... like this.

> +static bool trans_csrxchg(DisasContext *ctx, arg_csrxchg *a)
> +{
> +    TCGv dest = gpr_dst(ctx, a->rd, EXT_NONE);
> +    TCGv src1 = gpr_src(ctx, a->rd, EXT_NONE);
> +    TCGv src2 = gpr_src(ctx, a->rj, EXT_NONE);
> +
> +    if (check_plv(ctx) || ro_csr(a->csr)) {
> +        return false;
> +    }
> +    gen_helper_csr_xchgq(dest, cpu_env, src1, src2, tcg_constant_i64(a->csr));
> +    return true;
> +}

... back to xchg, you're not exiting the TB for any of the special cases above.


r~


  reply	other threads:[~2022-03-28 18:37 UTC|newest]

Thread overview: 71+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-28 12:57 [RFC PATCH v7 00/29] Add LoongArch softmmu support Xiaojuan Yang
2022-03-28 12:57 ` [RFC PATCH v7 01/29] target/loongarch: Add system emulation introduction Xiaojuan Yang
2022-03-28 12:57 ` [RFC PATCH v7 02/29] target/loongarch: Add CSRs definition Xiaojuan Yang
2022-03-28 19:16   ` Richard Henderson
2022-03-30 10:04     ` yangxiaojuan
2022-03-28 12:57 ` [RFC PATCH v7 03/29] target/loongarch: Add basic vmstate description of CPU Xiaojuan Yang
2022-03-28 12:57 ` [RFC PATCH v7 04/29] target/loongarch: Implement qmp_query_cpu_definitions() Xiaojuan Yang
2022-03-28 12:57 ` [RFC PATCH v7 05/29] target/loongarch: Add constant timer support Xiaojuan Yang
2022-03-28 19:46   ` Richard Henderson
2022-03-31  0:59     ` yangxiaojuan
2022-03-28 20:58   ` Richard Henderson
2022-03-28 12:57 ` [RFC PATCH v7 06/29] target/loongarch: Add MMU support for LoongArch CPU Xiaojuan Yang
2022-03-28 20:06   ` Richard Henderson
2022-03-28 12:57 ` [RFC PATCH v7 07/29] target/loongarch: Add LoongArch CSR instruction Xiaojuan Yang
2022-03-28 18:34   ` Richard Henderson [this message]
2022-03-30 10:01     ` yangxiaojuan
2022-03-30 13:46       ` Richard Henderson
2022-03-28 12:57 ` [RFC PATCH v7 08/29] target/loongarch: Add LoongArch IOCSR instruction Xiaojuan Yang
2022-03-28 18:55   ` Richard Henderson
2022-03-30 10:02     ` yangxiaojuan
2022-03-28 12:57 ` [RFC PATCH v7 09/29] target/loongarch: Add TLB instruction support Xiaojuan Yang
2022-03-28 20:12   ` Richard Henderson
2022-03-31  1:09     ` yangxiaojuan
2022-03-28 22:50   ` Richard Henderson
2022-03-28 12:57 ` [RFC PATCH v7 10/29] target/loongarch: Add other core instructions support Xiaojuan Yang
2022-03-28 20:16   ` Richard Henderson
2022-03-31  1:22     ` yangxiaojuan
2022-03-28 12:57 ` [RFC PATCH v7 11/29] target/loongarch: Add LoongArch interrupt and exception handle Xiaojuan Yang
2022-03-28 20:19   ` Richard Henderson
2022-03-31  1:29     ` yangxiaojuan
2022-03-28 12:57 ` [RFC PATCH v7 12/29] target/loongarch: Add timer related instructions support Xiaojuan Yang
2022-03-28 20:33   ` Richard Henderson
2022-03-28 12:57 ` [RFC PATCH v7 13/29] target/loongarch: Add gdb support Xiaojuan Yang
2022-03-28 20:35   ` Richard Henderson
2022-03-28 12:57 ` [RFC PATCH v7 14/29] hw/loongarch: Add support loongson3 virt machine type Xiaojuan Yang
2022-03-28 20:49   ` Richard Henderson
2022-03-28 21:02     ` Mark Cave-Ayland
2022-04-15  7:52       ` yangxiaojuan
2022-03-28 12:57 ` [RFC PATCH v7 15/29] hw/loongarch: Add LoongArch cpu interrupt support(CPUINTC) Xiaojuan Yang
2022-03-28 20:57   ` Richard Henderson
2022-03-28 12:57 ` [RFC PATCH v7 16/29] hw/loongarch: Add LoongArch ipi interrupt support(IPI) Xiaojuan Yang
2022-03-28 20:15   ` Mark Cave-Ayland
2022-03-31  1:16     ` yangxiaojuan
2022-03-28 22:04   ` Richard Henderson
2022-03-28 22:06   ` Richard Henderson
2022-03-28 12:57 ` [RFC PATCH v7 17/29] hw/intc: Add LoongArch ls7a interrupt controller support(PCH-PIC) Xiaojuan Yang
2022-03-28 20:18   ` Mark Cave-Ayland
2022-03-31  1:25     ` yangxiaojuan
2022-03-28 12:57 ` [RFC PATCH v7 18/29] hw/intc: Add LoongArch ls7a msi interrupt controller support(PCH-MSI) Xiaojuan Yang
2022-03-28 20:22   ` Mark Cave-Ayland
2022-03-28 12:57 ` [RFC PATCH v7 19/29] hw/intc: Add LoongArch extioi interrupt controller(EIOINTC) Xiaojuan Yang
2022-03-28 20:27   ` Mark Cave-Ayland
2022-03-28 22:43   ` Richard Henderson
2022-03-31 12:28     ` yangxiaojuan
2022-03-28 22:46   ` Richard Henderson
2022-03-28 12:57 ` [RFC PATCH v7 20/29] hw/loongarch: Add irq hierarchy for the system Xiaojuan Yang
2022-03-28 23:16   ` Richard Henderson
2022-03-28 12:57 ` [RFC PATCH v7 21/29] Enable common virtio pci support for LoongArch Xiaojuan Yang
2022-03-28 23:18   ` Richard Henderson
2022-03-28 12:57 ` [RFC PATCH v7 22/29] hw/loongarch: Add some devices support for 3A5000 Xiaojuan Yang
2022-03-28 12:57 ` [RFC PATCH v7 23/29] hw/loongarch: Add LoongArch ls7a rtc device support Xiaojuan Yang
2022-03-28 12:57 ` [RFC PATCH v7 24/29] hw/loongarch: Add default bios startup support Xiaojuan Yang
2022-03-29  3:27   ` Richard Henderson
2022-03-28 12:57 ` [RFC PATCH v7 25/29] hw/loongarch: Add -kernel and -initrd options support Xiaojuan Yang
2022-03-28 12:57 ` [RFC PATCH v7 26/29] hw/loongarch: Add LoongArch smbios support Xiaojuan Yang
2022-03-28 12:57 ` [RFC PATCH v7 27/29] hw/loongarch: Add LoongArch acpi support Xiaojuan Yang
2022-03-28 12:57 ` [RFC PATCH v7 28/29] hw/loongarch: Add fdt support Xiaojuan Yang
2022-03-28 12:57 ` [RFC PATCH v7 29/29] tests/tcg/loongarch64: Add hello/memory test in loongarch64 system Xiaojuan Yang
2022-03-29  3:29   ` Richard Henderson
2022-03-28 18:13 ` [RFC PATCH v7 00/29] Add LoongArch softmmu support Richard Henderson
2022-03-30  9:34   ` yangxiaojuan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aa21e33f-6729-d148-96e6-1f0617ddbc39@linaro.org \
    --to=richard.henderson@linaro.org \
    --cc=gaosong@loongson.cn \
    --cc=mark.cave-ayland@ilande.co.uk \
    --cc=qemu-devel@nongnu.org \
    --cc=yangxiaojuan@loongson.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).