RISC-V TIME CSR for privileged mode
Subsystems (2)
cover RISC-V TIME CSR for privileged mode
This series adds emulation of TIME CSRs for privileged mode. With this series, we see approximately 25+% improvement in hackbench numbers for non-virtualized (or Host) Linux and 40+% improvement in hackbench numbers for Guest/VM Linux.
These patches are based on mainline/alistair/riscv-hyp-ext-v0.5.1 branch of https://github.com/kvm-riscv/qemu.git and can be found in riscv_time_csr_v1 branch of same repo.
Anup Patel (2):
target/riscv: Emulate TIME CSRs for privileged mode hw/riscv: Provide rdtime callback for TCG in CLINT emulation
hw/riscv/sifive_clint.c | 1 + target/riscv/cpu.h | 5 +++ target/riscv/cpu_helper.c | 5 +++ target/riscv/csr.c | 80 +++++++++++++++++++++++++++++++++++++-- 4 files changed, 87 insertions(+), 4 deletions(-)
Signature · 1 line
2.17.1
PATCH 1/2 target/riscv: Emulate TIME CSRs for privileged mode
Currently, TIME CSRs are emulated only for user-only mode. This patch add TIME CSRs emulation for privileged mode.
For privileged mode, the TIME CSRs will return value provided by rdtime callback which is registered by QEMU machine/platform emulation (i.e. CLINT emulation). If rdtime callback is not available then the monitor (i.e. OpenSBI) will trap-n-emulate TIME CSRs in software.
We see 25+% performance improvement in hackbench numbers when TIME CSRs are not trap-n-emulated.
--- target/riscv/cpu.h | 5 +++ target/riscv/cpu_helper.c | 5 +++ target/riscv/csr.c | 80 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 86 insertions(+), 4 deletions(-)
target/riscv/cpu.h +5 -0
@@ -169,6 +169,7 @@ struct CPURISCVState {target_ulong htval;target_ulong htinst;target_ulong hgatp; +uint64_t htimedelta;/* Virtual CSRs */ target_ulong vsstatus;
@@ -204,6 +205,9 @@ struct CPURISCVState {/* physical memory protection */ pmp_table_t pmp_state; +/* machine specific rdtime callback */ +uint64_t (*rdtime_fn)(void ); +/* True if in debugger mode. */ bool debugger;#endif
@@ -325,6 +329,7 @@ void riscv_cpu_swap_hypervisor_regs(CPURISCVState *env);int riscv_cpu_claim_interrupts (RISCVCPU *cpu,uint32_t interrupts);uint32_t riscv_cpu_update_mip (RISCVCPU *cpu,uint32_t mask,uint32_t value);#define BOOL_TO_MASK (x) (-!!(x))/* helper for riscv_cpu_update_mip value */ +void riscv_cpu_set_rdtime_fn (CPURISCVState *env,uint64_t (*fn)(void ));#endif void riscv_cpu_set_mode (CPURISCVState *env,target_ulong newpriv);
target/riscv/cpu_helper.c +5 -0
@@ -250,6 +250,11 @@ uint32_t riscv_cpu_update_mip(RISCVCPU *cpu, uint32_t mask, uint32_t value)return old; } +void riscv_cpu_set_rdtime_fn (CPURISCVState *env,uint64_t (*fn)(void )) +{ + env->rdtime_fn = fn; +} +void riscv_cpu_set_mode (CPURISCVState *env,target_ulong newpriv) {if (newpriv >PRV_M ) {
target/riscv/csr.c +76 -4
108 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [PATCH 1/2] target/riscv: Emulate TIME CSRs for privileged mode
Quoted 71 lines · Anup Patel
Currently, TIME CSRs are emulated only for user-only mode. This patch add TIME CSRs emulation for privileged mode.
For privileged mode, the TIME CSRs will return value provided by rdtime callback which is registered by QEMU machine/platform emulation (i.e. CLINT emulation). If rdtime callback is not available then the monitor (i.e. OpenSBI) will trap-n-emulate TIME CSRs in software.
We see 25+% performance improvement in hackbench numbers when TIME CSRs are not trap-n-emulated.
Signed-off-by Anup Patel <[email protected]>--- target/riscv/cpu.h | 5 +++ target/riscv/cpu_helper.c | 5 +++ target/riscv/csr.c | 80 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 86 insertions(+), 4 deletions(-)target/riscv/cpu.h +5
-0@@ -169,6 +169,7 @@ struct CPURISCVState {target_ulong htval;target_ulong htinst;target_ulong hgatp; +uint64_t htimedelta;/* Virtual CSRs */ target_ulong vsstatus;@@ -204,6 +205,9 @@ struct CPURISCVState {/* physical memory protection */ pmp_table_t pmp_state; +/* machine specific rdtime callback */ +uint64_t (*rdtime_fn)(void ); +/* True if in debugger mode. */ bool debugger;#endif @@ -325,6 +329,7 @@ void riscv_cpu_swap_hypervisor_regs(CPURISCVState *env);int riscv_cpu_claim_interrupts (RISCVCPU *cpu,uint32_t interrupts);uint32_t riscv_cpu_update_mip (RISCVCPU *cpu,uint32_t mask,uint32_t value);#define BOOL_TO_MASK (x) (-!!(x))/* helper for riscv_cpu_update_mip value */ +void riscv_cpu_set_rdtime_fn (CPURISCVState *env,uint64_t (*fn)(void ));#endif void riscv_cpu_set_mode (CPURISCVState *env,target_ulong newpriv);target/riscv/cpu_helper.c +5
-0@@ -250,6 +250,11 @@ uint32_t riscv_cpu_update_mip(RISCVCPU *cpu, uint32_t mask, uint32_t value)return old; } +void riscv_cpu_set_rdtime_fn (CPURISCVState *env,uint64_t (*fn)(void )) +{ + env->rdtime_fn = fn; +} +void riscv_cpu_set_mode (CPURISCVState *env,target_ulong newpriv) {if (newpriv >PRV_M ) {target/riscv/csr.c +0
-0
target/riscv/csr.c static int read_timeh(CPURISCVState *env, int csrno, target_ulong *val) · line 246
Quoted 9 lines
target/riscv/csr.c +6
-0@@ -238,6 +238,30 @@ static int read_timeh(CPURISCVState *env, int csrno, target_ulong *val)#else /* CONFIG_USER_ONLY */ +static int read_time (CPURISCVState *env,int csrno,target_ulong *val) +{ +uint64_t delta =riscv_cpu_virt_enabled (env) ? env->htimedelta :0 ; + +if (!env->rdtime_fn) +return -1 ;
QEMU uses brackets around single line if statements (like Rust :) ). Can you add brackets to all of them?
After that:
Alistair
Quoted 106 lines
target/riscv/csr.c +70
-5+ + *val = env->rdtime_fn () + delta; +return 0 ; +} + +#if defined(TARGET_RISCV32 ) +static int read_timeh (CPURISCVState *env,int csrno,target_ulong *val) +{ +uint64_t delta =riscv_cpu_virt_enabled (env) ? env->htimedelta :0 ; + +if (!env->rdtime_fn) +return -1 ; + + *val = (env->rdtime_fn () + delta) >>32 ; +return 0 ; +} +#endif +/* Machine constants */ #define M_MODE_INTERRUPTS (MIP_MSIP | MIP_MTIP | MIP_MEIP)@@ -931,6 +955,52 @@ static int write_hgatp(CPURISCVState *env, int csrno, target_ulong val)return 0 ; } +static int read_htimedelta (CPURISCVState *env,int csrno,target_ulong *val) +{ +if (!env->rdtime_fn) +return -1 ; + +#if defined(TARGET_RISCV32 ) + *val = env->htimedelta &0xffffffff ; +#else + *val = env->htimedelta; +#endif +return 0 ; +} + +static int write_htimedelta (CPURISCVState *env,int csrno,target_ulong val) +{ +if (!env->rdtime_fn) +return -1 ; + +#if defined(TARGET_RISCV32 ) + env->htimedelta =deposit64 (env->htimedelta,0 ,32 , (uint64_t )val); +#else + env->htimedelta = val; +#endif +return 0 ; +} + +#if defined(TARGET_RISCV32 ) +static int read_htimedeltah (CPURISCVState *env,int csrno,target_ulong *val) +{ +if (!env->rdtime_fn) +return -1 ; + + *val = env->htimedelta >>32 ; +return 0 ; +} + +static int write_htimedeltah (CPURISCVState *env,int csrno,target_ulong val) +{ +if (!env->rdtime_fn) +return -1 ; + + env->htimedelta =deposit64 (env->htimedelta,32 ,32 , (uint64_t )val); +return 0 ; +} +#endif +/* Virtual CSR Registers */ static int read_vsstatus (CPURISCVState *env,int csrno,target_ulong *val) {@@ -1203,14 +1273,12 @@ static riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_INSTRETH] = { ctr, read_instreth }, #endif -/* User-level time CSRs are only available in linux-user -* In privileged mode, the monitor emulates these CSRs */ -#if defined(CONFIG_USER_ONLY ) + /* In privileged mode, the monitor will have to emulate TIME CSRs only if + * rdtime callback is not provided by machine/platform emulation */ [CSR_TIME] = { ctr, read_time }, #if defined(TARGET_RISCV32) [CSR_TIMEH] = { ctr, read_timeh },#endif -#endif #if !defined(CONFIG_USER_ONLY )/* Machine Timers and Counters */ @@ -1276,6 +1344,10 @@ static riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_HTVAL] = { hmode, read_htval, write_htval }, [CSR_HTINST] = { hmode, read_htinst, write_htinst }, [CSR_HGATP] = { hmode, read_hgatp, write_hgatp }, + [CSR_HTIMEDELTA] = { hmode, read_htimedelta, write_htimedelta }, +#if defined(TARGET_RISCV32) + [CSR_HTIMEDELTAH] = { hmode, read_htimedeltah, write_htimedeltah}, +#endif [CSR_VSSTATUS] = { hmode, read_vsstatus, write_vsstatus }, [CSR_VSIP] = { hmode, NULL, NULL, rmw_vsip }, --2.17.1
PATCH 2/2 hw/riscv: Provide rdtime callback for TCG in CLINT emulation
In reply to Anup PatelThis patch extends CLINT emulation to provide rdtime callback for TCG. This rdtime callback will be called wheneven TIME CSRs are read in privileged modes.
--- hw/riscv/sifive_clint.c | 1 + 1 file changed, 1 insertion(+)
hw/riscv/sifive_clint.c +1 -0
@@ -236,6 +236,7 @@ DeviceState *sifive_clint_create(hwaddr addr, hwaddr size, uint32_t num_harts,if (!env) {continue ; } +riscv_cpu_set_rdtime_fn (env, cpu_riscv_read_rtc); env->timer =timer_new_ns (QEMU_CLOCK_VIRTUAL , &sifive_clint_timer_cb, cpu); env->timecmp =0 ;
Signature · 1 line
2.17.1
Re: [PATCH 2/2] hw/riscv: Provide rdtime callback for TCG in CLINT emulation
Quoted 6 lines · Anup Patel
This patch extends CLINT emulation to provide rdtime callback for TCG. This rdtime callback will be called wheneven TIME CSRs are read in privileged modes.
Signed-off-by Anup Patel <[email protected]>
Alistair
Quoted 20 lines
--- hw/riscv/sifive_clint.c | 1 + 1 file changed, 1 insertion(+)hw/riscv/sifive_clint.c +1
-1@@ -236,6 +236,7 @@ DeviceState *sifive_clint_create(hwaddr addr, hwaddr size, uint32_t num_harts,if (!env) {continue ; } +riscv_cpu_set_rdtime_fn (env, cpu_riscv_read_rtc); env->timer =timer_new_ns (QEMU_CLOCK_VIRTUAL , &sifive_clint_timer_cb, cpu); env->timecmp =0 ; --2.17.1
This series doesn't seem to touch mcounteren.TM which should be hardwired to zero if no callback in provided, and writable otherwise. (I had a patch to do the hardwiring unconditionally, but it seems to have been accidentally dropped.) Other than that, I think the design is quite good.
Jonathan
Quoted 24 lines · Anup Patel
This series adds emulation of TIME CSRs for privileged mode. With this series, we see approximately 25+% improvement in hackbench numbers for non-virtualized (or Host) Linux and 40+% improvement in hackbench numbers for Guest/VM Linux.
These patches are based on mainline/alistair/riscv-hyp-ext-v0.5.1 branch of https://github.com/kvm-riscv/qemu.git and can be found in
riscv_time_csr_v1branch of same repo.Anup Patel (2):
target/riscv: Emulate TIME CSRs for privileged mode hw/riscv: Provide rdtime callback for TCG in CLINT emulationhw/riscv/sifive_clint.c | 1 + target/riscv/cpu.h | 5 +++ target/riscv/cpu_helper.c | 5 +++ target/riscv/csr.c | 80 +++++++++++++++++++++++++++++++++++++-- 4 files changed, 87 insertions(+), 4 deletions(-)--
2.17.1