All of lore.kernel.org
 help / color / mirror / Atom feed
From: Gary Guo <gary@garyguo.net>
To: linux-riscv@lists.infradead.org
Cc: Gary Guo <gary@garyguo.net>
Subject: [PATCH] riscv: enable TIME CSR in U mode
Date: Tue, 16 Feb 2021 22:12:08 +0000	[thread overview]
Message-ID: <20210216221208.2860-1-gary@garyguo.net> (raw)

After ad5d112 we let the user mode to use rdtime directly for time
access. This works if the hardware does not implement the TIME CSR
and traps to the firmware. The spec however does allow a hardware
implementation to redirect the CSR access to the memory-mapped MTIME
CSR, and if this is performed it will check {M,S}COUNTEREN CSR to
see if it is allowed.

To prevent unhandled illegal instruction fault on these platforms,
we can request these CSRs to be enabled in U-mode. For platforms
that does not support the TIME CSR to MTIME CSR conversion, they
will hardwire corresponding bit in COUNTEREN to zero and this would
be a no-op.

Signed-off-by: Gary Guo <gary@garyguo.net>
---
 arch/riscv/kernel/head.S | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/arch/riscv/kernel/head.S b/arch/riscv/kernel/head.S
index 0a4e81b8dc79..5a7a62190342 100644
--- a/arch/riscv/kernel/head.S
+++ b/arch/riscv/kernel/head.S
@@ -190,6 +190,9 @@ ENTRY(_start_kernel)
 .align 2
 pmp_done:
 
+	/* Allow user-mode to access time CSR */
+	csrw, CSR_MCOUNTEREN, 2
+
 	/*
 	 * The hartid in a0 is expected later on, and we have no firmware
 	 * to hand it to us.
@@ -197,6 +200,9 @@ pmp_done:
 	csrr a0, CSR_MHARTID
 #endif /* CONFIG_RISCV_M_MODE */
 
+	/* Allow user-mode to access time CSR */
+	csrw CSR_SCOUNTEREN, 2
+
 	/* Load the global pointer */
 .option push
 .option norelax
-- 
2.20.1


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

             reply	other threads:[~2021-02-16 22:12 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-16 22:12 Gary Guo [this message]
2021-03-04 17:35 ` [PATCH] riscv: enable TIME CSR in U mode Nick Kossifidis
2021-03-16  6:24   ` Gary Guo
2021-03-16 20:52     ` Nick Kossifidis
2021-03-17  2:50       ` Atish Patra

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=20210216221208.2860-1-gary@garyguo.net \
    --to=gary@garyguo.net \
    --cc=linux-riscv@lists.infradead.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.