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: peter.maydell@linaro.org, thuth@redhat.com,
	chenhuacai@loongson.cn, mst@redhat.com, philmd@redhat.com,
	mark.cave-ayland@ilande.co.uk, laurent@vivier.eu,
	peterx@redhat.com, f4bug@amsat.org, alistair.francis@wdc.com,
	maobibo@loongson.cn, gaosong@loongson.cn, pbonzini@redhat.com,
	bmeng.cn@gmail.com, alex.bennee@linaro.org,
	david@gibson.dropbear.id.au
Subject: Re: [PATCH 06/31] target/loongarch: Add mmu support for Loongarch CPU.
Date: Tue, 19 Oct 2021 14:11:06 -0700	[thread overview]
Message-ID: <538a03ec-a1cf-3b1d-e0c6-4bec54aad94c@linaro.org> (raw)
In-Reply-To: <1634628917-10031-7-git-send-email-yangxiaojuan@loongson.cn>

On 10/19/21 12:34 AM, Xiaojuan Yang wrote:
> @@ -272,6 +288,7 @@ static const struct SysemuCPUOps loongarch_sysemu_ops = {
>   #ifdef CONFIG_TCG
>   #include "hw/core/tcg-cpu-ops.h"
>   
> +#ifdef CONFIG_USER_ONLY
>   static bool loongarch_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
>                          MMUAccessType access_type, int mmu_idx,
>                          bool probe, uintptr_t retaddr)
> @@ -280,9 +297,14 @@ static bool loongarch_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
>       CPULoongArchState *env = &cpu->env;
>   
>       env->badaddr = address;
> -    cs->exception_index = EXCP_ADE;
> +    if (access_type == MMU_DATA_STORE) {
> +        cs->exception_index = EXCP_ADES;
> +    } else {
> +        cs->exception_index = EXCP_ADEL;
> +    }
>       do_raise_exception(env, cs->exception_index, retaddr);
>   }
> +#endif

It's too early to add this ifdef.  With what's upstream at the moment, you've broken 
loongarch-linux-user build by removing loongarch_cpu_tlb_fill.

There are patches out for review that would require tlb_fill be a system-only hook, but 
they have not landed yet.

> +#define LOONGARCH_HFLAG_KU     0x00003 /* kernel/user mode mask   */
> +#define LOONGARCH_HFLAG_UM     0x00003 /* user mode flag                     */
> +#define LOONGARCH_HFLAG_KM     0x00000 /* kernel mode flag                   */

I think you might as well represent all 3 priv levels: it's not a "kernel/user" mask.

> +#define EXCP_TLB_NOMATCH   0x1
> +#define EXCP_INST_NOTAVAIL 0x2 /* No valid instruction word for BadInstr */

These should be with the other EXCP values in the enum.
At the moment you're overlapping EXCP_ADES and EXCP_SYSCALL.

> @@ -130,7 +139,11 @@ void loongarch_cpu_list(void);
>   
>   static inline int cpu_mmu_index(CPULoongArchState *env, bool ifetch)
>   {
> +#ifdef CONFIG_USER_ONLY
>       return MMU_USER_IDX;
> +#else
> +    return env->CSR_CRMD & LOONGARCH_HFLAG_KU;

Better would be

     return FIELD_EX64(env->CSR_CRMD, CSR_CRMD, PLV);

since that's the field you're extracting from CRMD.

> +typedef struct ls3a5k_tlb_t ls3a5k_tlb_t;

Types should be in CamelCase, without _t suffix.

> +struct ls3a5k_tlb_t {
> +    target_ulong VPN;
> +    uint64_t PageMask;
> +    uint32_t PageSize;
> +    uint16_t ASID;
> +    unsigned int V0:1;     /* CSR_TLBLO[0] */
> +    unsigned int V1:1;
> +
> +    unsigned int D0:1;     /* CSR_TLBLO[1] */
> +    unsigned int D1:1;
> +
> +    unsigned int PLV0:2;   /* CSR_TLBLO[3:2] */
> +    unsigned int PLV1:2;
> +
> +    unsigned int MAT0:3;   /* CSR_TLBLO[5:4] */
> +    unsigned int MAT1:3;
> +
> +    unsigned int G:1;      /* CSR_TLBLO[6] */
> +
> +    uint64_t PPN0;         /* CSR_TLBLO[47:12] */
> +    uint64_t PPN1;
> +
> +    unsigned int NR0:1;    /* CSR_TLBLO[61] */
> +    unsigned int NR1:1;
> +
> +    unsigned int NX0:1;    /* CSR_TLBLO[62] */
> +    unsigned int NX1:1;
> +
> +    unsigned int NE:1;     /* CSR_TLBIDX[31] */
> +
> +    unsigned int RPLV0:1;
> +    unsigned int RPLV1:1;  /* CSR_TLBLO[63] */
> +};

It would be much better if you didn't use bitfields at all.  This was a bad idea when mips 
did it; let us not compound the error.

Just use the format defined by the architecture for the CSRs: a couple of uint64_t.  Use 
FIELD definitions to give the parts intelligible names.

> +typedef struct ls3a5k_tlb_t ls3a5k_tlb_t;
> +
> +struct CPULoongArchTLBContext {
> +    uint32_t nb_tlb;
> +    int (*map_address)(struct CPULoongArchState *env, hwaddr *physical,
> +                       int *prot, target_ulong address,
> +                       MMUAccessType access_type);
> +    struct {
> +        uint64_t     stlb_mask;
> +        uint32_t     stlb_size; /* at most : 8 * 256 = 2048 */
> +        uint32_t     mtlb_size; /* at most : 64 */
> +        ls3a5k_tlb_t tlb[LOONGARCH_TLB_MAX];
> +    } ls3a5k;
> +};

There's probably no point in using an indirect function call until you've got more than 
one mmu implementation.  You're copying too much from mips.

> +/* TLB state */
> +static int get_tlb(QEMUFile *f, void *pv, size_t size,
> +                   const VMStateField *field)
> +{
> +    ls3a5k_tlb_t *v = pv;
> +    uint32_t flags;
> +
> +    qemu_get_betls(f, &v->VPN);
> +    qemu_get_be64s(f, &v->PageMask);
> +    qemu_get_be32s(f, &v->PageSize);
> +    qemu_get_be16s(f, &v->ASID);
> +    qemu_get_be32s(f, &flags);
> +    v->RPLV1 = (flags >> 21) & 1;
> +    v->RPLV0 = (flags >> 20) & 1;
> +    v->PLV1 = (flags >> 18) & 3;
> +    v->PLV0 = (flags >> 16) & 3;
> +    v->NE = (flags >> 15) & 1;
> +    v->NR1 = (flags >> 14) & 1;
> +    v->NR0 = (flags >> 13) & 1;
> +    v->NX1 = (flags >> 12) & 1;
> +    v->NX0 = (flags >> 11) & 1;
> +    v->D1 = (flags >> 10) & 1;
> +    v->D0 = (flags >> 9) & 1;
> +    v->V1 = (flags >> 8) & 1;
> +    v->V0 = (flags >> 7) & 1;
> +    v->MAT1 = (flags >> 4) & 7;
> +    v->MAT0 = (flags >> 1) & 7;
> +    v->G = (flags >> 0) & 1;
> +    qemu_get_be64s(f, &v->PPN0);
> +    qemu_get_be64s(f, &v->PPN1);

Some of the ugly things that go away if you don't use bitfields.

> +const VMStateDescription vmstate_tlb = {
> +    .name = "cpu/tlb",
> +    .version_id = 2,
> +    .minimum_version_id = 2,

Too much copying again: version numbers do not start at 2.

> +void ls3a5k_mmu_init(CPULoongArchState *env)
> +{
> +    env->tlb = g_malloc0(sizeof(CPULoongArchTLBContext));

I think you should not make this a separate structure, and instead allocate this with 
CPULoongArchState.

> diff --git a/target/loongarch/translate.c b/target/loongarch/translate.c
> index bea290df66..0be29994f9 100644
> --- a/target/loongarch/translate.c
> +++ b/target/loongarch/translate.c
> @@ -61,9 +61,10 @@ static void loongarch_tr_init_disas_context(DisasContextBase *dcbase,
>   {
>       int64_t bound;
>       DisasContext *ctx = container_of(dcbase, DisasContext, base);
> +    CPULoongArchState *env = cs->env_ptr;
>   
>       ctx->page_start = ctx->base.pc_first & TARGET_PAGE_MASK;
> -    ctx->mem_idx = MMU_USER_IDX;
> +    ctx->mem_idx = cpu_mmu_index(env, false);

This is incorrect.  You want

     tb_flags = ctx->base.tb->flags;
     mem_idx = tb_flags & LOONGARCH_HFLAG_PRIV.

It is almost always incorrect to dereference env at this point.  Everything should have 
been encoded into tb_flags so that when we do the hashing of the TranslationBlocks we find 
the one that has been compiled for the correct privilege level, etc.


r~


  parent reply	other threads:[~2021-10-19 21:12 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-19  7:34 [PATCH 00/31] Add Loongarch softmmu support Xiaojuan Yang
2021-10-19  7:34 ` [PATCH 02/31] target/loongarch: Add CSR registers definition Xiaojuan Yang
2021-10-19 19:10   ` Richard Henderson
2021-10-19  7:34 ` [PATCH 03/31] target/loongarch: Set default csr values Xiaojuan Yang
2021-10-19 19:18   ` Richard Henderson
2021-10-19  7:34 ` [PATCH 04/31] target/loongarch: Add basic vmstate description of CPU Xiaojuan Yang
2021-10-19 19:35   ` Richard Henderson
2021-10-19  7:34 ` [PATCH 05/31] target/loongarch: Implement qmp_query_cpu_definitions() Xiaojuan Yang
2021-10-19 20:25   ` Richard Henderson
2021-10-19  7:34 ` [PATCH 08/31] target/loongarch: Add tlb instruction support Xiaojuan Yang
2021-10-20  4:19   ` Richard Henderson
2021-10-29  7:01     ` yangxiaojuan
2021-10-29 17:48       ` Richard Henderson
2021-10-19  7:34 ` [PATCH 09/31] target/loongarch: Add other core instructions support Xiaojuan Yang
2021-10-20  4:45   ` Richard Henderson
2021-10-19  7:34 ` [PATCH 10/31] target/loongarch: Add loongarch interrupt and exception handle Xiaojuan Yang
2021-10-20  4:59   ` Richard Henderson
2021-10-19  7:34 ` [PATCH 11/31] target/loongarch: Add stabletimer support Xiaojuan Yang
2021-10-19  7:34 ` [PATCH 12/31] target/loongarch: Add timer related instructions support Xiaojuan Yang
2021-10-20  5:17   ` Richard Henderson
2021-10-19  7:34 ` [PATCH 13/31] hw/pci-host: Add ls7a1000 PCIe Host bridge support for Loongson Platform Xiaojuan Yang
2021-10-19  7:35 ` [PATCH 14/31] hw/loongarch: Add a virt loongarch 3A5000 board support Xiaojuan Yang
2021-10-19  7:35 ` [PATCH 15/31] hw/loongarch: Add loongarch cpu interrupt support(CPUINTC) Xiaojuan Yang
2021-10-19  7:35 ` [PATCH 16/31] hw/loongarch: Add loongarch ipi interrupt support(IPI) Xiaojuan Yang
2021-10-19  7:35 ` [PATCH 17/31] hw/intc: Add loongarch ls7a interrupt controller support(PCH-PIC) Xiaojuan Yang
2021-10-19  7:35 ` [PATCH 18/31] hw/intc: Add loongarch ls7a msi interrupt controller support(PCH-MSI) Xiaojuan Yang
2021-10-19  7:35 ` [PATCH 19/31] hw/intc: Add loongarch extioi interrupt controller(EIOINTC) Xiaojuan Yang
2021-10-19  7:35 ` [PATCH 20/31] hw/loongarch: Add irq hierarchy for the system Xiaojuan Yang
2021-10-19 14:52 ` [PATCH 00/31] Add Loongarch softmmu support WANG Xuerui
     [not found]   ` <7d933f8d.228e.17c9b556e98.Coremail.yangxiaojuan@loongson.cn>
2021-10-20  5:11     ` WANG Xuerui
     [not found] ` <1634628917-10031-24-git-send-email-yangxiaojuan@loongson.cn>
2021-10-19 16:19   ` [PATCH 23/31] hw/loongarch: Add default bios startup support Michael S. Tsirkin
     [not found] ` <1634628917-10031-2-git-send-email-yangxiaojuan@loongson.cn>
2021-10-19 18:56   ` [PATCH 01/31] target/loongarch: Upate the README for the softmmu Richard Henderson
2021-10-22  2:25     ` yangxiaojuan
     [not found] ` <1634628917-10031-7-git-send-email-yangxiaojuan@loongson.cn>
2021-10-19 21:11   ` Richard Henderson [this message]
     [not found] ` <1634628917-10031-8-git-send-email-yangxiaojuan@loongson.cn>
2021-10-20  1:36   ` [PATCH 07/31] target/loongarch: Add loongarch csr/iocsr instruction support Richard Henderson
2021-10-29  6:26     ` yangxiaojuan
2021-10-29 17:38       ` Richard Henderson

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=538a03ec-a1cf-3b1d-e0c6-4bec54aad94c@linaro.org \
    --to=richard.henderson@linaro.org \
    --cc=alex.bennee@linaro.org \
    --cc=alistair.francis@wdc.com \
    --cc=bmeng.cn@gmail.com \
    --cc=chenhuacai@loongson.cn \
    --cc=david@gibson.dropbear.id.au \
    --cc=f4bug@amsat.org \
    --cc=gaosong@loongson.cn \
    --cc=laurent@vivier.eu \
    --cc=maobibo@loongson.cn \
    --cc=mark.cave-ayland@ilande.co.uk \
    --cc=mst@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=peterx@redhat.com \
    --cc=philmd@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=thuth@redhat.com \
    --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).