qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Richard Henderson <richard.henderson@linaro.org>
To: Michael Clark <mjc@sifive.com>, qemu-devel@nongnu.org
Cc: Bastian Koppelmann <kbastian@mail.uni-paderborn.de>,
	Palmer Dabbelt <palmer@sifive.com>,
	Sagar Karandikar <sagark@eecs.berkeley.edu>,
	RISC-V Patches <patches@groups.riscv.org>
Subject: Re: [Qemu-devel] [PATCH v3 05/21] RISC-V CPU Helpers
Date: Thu, 11 Jan 2018 07:29:25 -0800	[thread overview]
Message-ID: <41a5696b-905d-9081-0b1d-cfe16d89f9cd@linaro.org> (raw)
In-Reply-To: <1515637324-96034-6-git-send-email-mjc@sifive.com>

On 01/10/2018 06:21 PM, Michael Clark wrote:
> +    int ptshift = (levels - 1) * ptidxbits;
> +    int i;
> +    for (i = 0; i < levels; i++, ptshift -= ptidxbits) {
> +        target_ulong idx = (addr >> (PGSHIFT + ptshift)) &
> +                           ((1 << ptidxbits) - 1);
> +
> +        /* check that physical address of PTE is legal */
> +        target_ulong pte_addr = base + idx * ptesize;
> +        target_ulong pte = ldq_phys(cs->as, pte_addr);
> +        target_ulong ppn = pte >> PTE_PPN_SHIFT;
> +
> +        if (PTE_TABLE(pte)) { /* next level of page table */
> +            base = ppn << PGSHIFT;
> +        } else if ((pte & PTE_U) ? (mode == PRV_S) && !sum : !(mode == PRV_S)) {
> +            break;
> +        } else if (!(pte & PTE_V) || (!(pte & PTE_R) && (pte & PTE_W))) {
> +            break;

It might be clearer to return TRANSLATION_FAILED directly here.

> +        } else if (access_type == MMU_INST_FETCH ? !(pte & PTE_X) :
> +                  access_type == MMU_DATA_LOAD ?  !(pte & PTE_R) &&
> +                  !(mxr && (pte & PTE_X)) : !((pte & PTE_R) && (pte & PTE_W))) {
> +            break;

It might be clearer to pre-compute the access required and the access granted
by this page table level.  E.g. before the loop

  access_requested = (access_type == MMU_INST_FETCH ? PTE_X
                      : access_type == MMU_DATA_LOAD ? PTE_R : PTE_W);

and then here

  access_granted = pte & (PTE_X | PTE_R | PTE_W);
  if (mxr && (pte & PTE_X)) {
    access_granted |= PTE_R;
  }
  if ((access_requested & access_granted) == 0) {
    return TRANSLATION_FAILED;
  }

> +            /* set accessed and possibly dirty bits.
> +               we only put it in the TLB if it has the right stuff */
> +            stq_phys(cs->as, pte_addr, ldq_phys(cs->as, pte_addr) | PTE_A |
> +                    ((access_type == MMU_DATA_STORE) * PTE_D));

Surely you only want to do this if the bits in question are not already set.

You want

    set_bits = PTE_A + (access_type == MMU_DATA_STORE) * PTE_D);
    if ((pte & set_bits) != set_bits) {
        stq_phys(...);
        pte |= set_bits;  /* see below */
    }

> +            /* we do not give all prots indicated by the PTE
> +             * this is because future accesses need to do things like set the
> +             * dirty bit on the PTE
> +             *
> +             * at this point, we assume that protection checks have occurred */
> +            if (mode == PRV_S) {
> +                if ((pte & PTE_X) && access_type == MMU_INST_FETCH) {
> +                    *prot |= PAGE_EXEC;
> +                } else if ((pte & PTE_W) && access_type == MMU_DATA_STORE) {
> +                    *prot |= PAGE_WRITE;
> +                } else if ((pte & PTE_R) && access_type == MMU_DATA_LOAD) {
> +                    *prot |= PAGE_READ;
> +                } else {
> +                    g_assert_not_reached();
> +                }
> +            } else {
> +                if ((pte & PTE_X) && access_type == MMU_INST_FETCH) {
> +                    *prot |= PAGE_EXEC;
> +                } else if ((pte & PTE_W) && access_type == MMU_DATA_STORE) {
> +                    *prot |= PAGE_WRITE;
> +                } else if ((pte & PTE_R) && access_type == MMU_DATA_LOAD) {
> +                    *prot |= PAGE_READ;
> +                } else {
> +                    g_assert_not_reached();
> +                }
> +            }

This is wrong.  First, it isn't taking MXR into account.  Second, you don't
want to only grant what is required by the access, but also what is allowable.
Otherwise we will keep coming back to this routine any different kind of
access.  Third, what has PRV_S got to do with it?

You want

  /* If the dirty bit is not yet set, remove write permission from
     the page so that we come back here to set it.  */
  if ((pte & PTE_D) == 0) {
    access_granted &= ~PTE_W;
  }

  *prot = (access_granted & PTE_X ? PAGE_EXEC : 0)
        | (access_granted & PTE_R ? PAGE_READ : 0)
        | (access_granted & PTE_W ? PAGE_WRITE : 0);


Did I miss the check for misaligned superpages (step 6 in 4.3.2
Virtual Address Translation Process)?


> +/*
> + * Handle writes to CSRs and any resulting special behavior
> + *
> + * Adapted from Spike's processor_t::set_csr
> + */
> +inline void csr_write_helper(CPURISCVState *env, target_ulong val_to_write,
> +        target_ulong csrno)

Again, drop the inline.  (This is a huge function anyway, why would you want that?)

> +inline target_ulong csr_read_helper(CPURISCVState *env, target_ulong csrno)

Likewise.

> +target_ulong helper_sret(CPURISCVState *env, target_ulong cpu_pc_deb)
> +{
> +    if (!(env->priv >= PRV_S)) {
> +        helper_raise_exception(env, RISCV_EXCP_ILLEGAL_INST);
> +    }

Again, I strongly suspect that you need to be using an unwinding exception
here, and elsewhere within this file.  Otherwise the PC against which the
exception is reported will be wrong.


r~

  reply	other threads:[~2018-01-11 15:29 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-11  2:21 [Qemu-devel] [PATCH v3 00/21] RISC-V QEMU Port Submission v3 Michael Clark
2018-01-11  2:21 ` [Qemu-devel] [PATCH v3 01/21] RISC-V Maintainers Michael Clark
2018-01-11  2:21 ` [Qemu-devel] [PATCH v3 02/21] RISC-V ELF Machine Definition Michael Clark
2018-01-11  2:21 ` [Qemu-devel] [PATCH v3 03/21] RISC-V CPU Core Definition Michael Clark
2018-01-11 14:32   ` Richard Henderson
2018-01-11 14:37   ` Richard Henderson
2018-01-11 17:55     ` Michael Clark
2018-01-12  3:03       ` Palmer Dabbelt
2018-01-11  2:21 ` [Qemu-devel] [PATCH v3 04/21] RISC-V Disassembler Michael Clark
2018-01-11 14:34   ` Richard Henderson
2018-01-11  2:21 ` [Qemu-devel] [PATCH v3 05/21] RISC-V CPU Helpers Michael Clark
2018-01-11 15:29   ` Richard Henderson [this message]
2018-01-11  2:21 ` [Qemu-devel] [PATCH v3 06/21] RISC-V FPU Support Michael Clark
2018-01-11 15:31   ` Richard Henderson
2018-01-11 18:09     ` Michael Clark
2018-01-11 20:01       ` Richard Henderson
2018-01-11  2:21 ` [Qemu-devel] [PATCH v3 07/21] RISC-V GDB Stub Michael Clark
2018-01-11 15:31   ` Richard Henderson
2018-01-11  2:21 ` [Qemu-devel] [PATCH v3 08/21] RISC-V TCG Code Generation Michael Clark
2018-01-11 15:47   ` Richard Henderson
2018-01-11 18:15     ` Michael Clark
2018-01-11 18:55       ` Michael Clark
2018-01-11  2:21 ` [Qemu-devel] [PATCH v3 09/21] RISC-V Physical Memory Protection Michael Clark
2018-01-11  2:21 ` [Qemu-devel] [PATCH v3 10/21] RISC-V Linux User Emulation Michael Clark
2018-01-11  2:21 ` [Qemu-devel] [PATCH v3 11/21] RISC-V HTIF Console Michael Clark
2018-01-11  2:21 ` [Qemu-devel] [PATCH v3 12/21] RISC-V HART Array Michael Clark
2018-01-11  2:21 ` [Qemu-devel] [PATCH v3 13/21] SiFive RISC-V CLINT Block Michael Clark
2018-01-11  2:21 ` [Qemu-devel] [PATCH v3 14/21] SiFive RISC-V PLIC Block Michael Clark
2018-01-11  9:10   ` Antony Pavlov
2018-01-11  2:21 ` [Qemu-devel] [PATCH v3 15/21] RISC-V Spike Machines Michael Clark
2018-01-11  2:21 ` [Qemu-devel] [PATCH v3 16/21] RISC-V VirtIO Machine Michael Clark
2018-01-11  2:22 ` [Qemu-devel] [PATCH v3 17/21] SiFive RISC-V UART Device Michael Clark
2018-01-11  2:22 ` [Qemu-devel] [PATCH v3 18/21] SiFive RISC-V PRCI Block Michael Clark
2018-01-11  2:22 ` [Qemu-devel] [PATCH v3 19/21] SiFive Freedom E300 RISC-V Machine Michael Clark
2018-01-12 10:13   ` Antony Pavlov
2018-01-11  2:22 ` [Qemu-devel] [PATCH v3 20/21] SiFive Freedom U500 " Michael Clark
2018-01-11  2:22 ` [Qemu-devel] [PATCH v3 21/21] RISC-V Build Infrastructure Michael Clark
2018-01-11 14:05   ` Eric Blake
2018-01-11 18:43     ` Michael Clark
2018-02-04 21:15       ` Michael Clark
2018-01-11  3:01 ` [Qemu-devel] [PATCH v3 00/21] RISC-V QEMU Port Submission v3 no-reply

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=41a5696b-905d-9081-0b1d-cfe16d89f9cd@linaro.org \
    --to=richard.henderson@linaro.org \
    --cc=kbastian@mail.uni-paderborn.de \
    --cc=mjc@sifive.com \
    --cc=palmer@sifive.com \
    --cc=patches@groups.riscv.org \
    --cc=qemu-devel@nongnu.org \
    --cc=sagark@eecs.berkeley.edu \
    /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).