target/riscv: Expose time CSRs when allowed by [m|s]counteren
Subsystems (2)
PATCH v3 1/1 target/riscv: Expose time CSRs when allowed by [m|s]counteren
Currently mcounteren.TM acts as though it is hardwired to zero, even though QEMU allows it to be set. This change resolves the issue by allowing reads to the time and timeh control registers when running in a privileged mode where such accesses are allowed.
The frequency of the time register is stored in the time_freq field of each hart so that it is accessible during CSR reads, but must be the same across all harts. Each board can initialize it to a custom value, although all currently use a 10 MHz frequency.
--- hw/riscv/riscv_hart.c | 4 ++++ hw/riscv/sifive_clint.c | 30 ++++++++++++++++++++++-------- hw/riscv/sifive_e.c | 2 ++ hw/riscv/sifive_u.c | 4 +++- hw/riscv/spike.c | 6 +++++- hw/riscv/virt.c | 4 +++- include/hw/riscv/riscv_hart.h | 1 + include/hw/riscv/sifive_clint.h | 4 ---- include/hw/riscv/sifive_e.h | 4 ++++ include/hw/riscv/sifive_u.h | 1 + include/hw/riscv/spike.h | 1 + include/hw/riscv/virt.h | 1 + target/riscv/cpu.h | 2 ++ target/riscv/csr.c | 17 +++++++++++------ 14 files changed, 60 insertions(+), 21 deletions(-)
hw/riscv/riscv_hart.c +4 -0
@@ -19,6 +19,7 @@*/ #include "qemu/osdep.h" +#include "qemu/timer.h" #include "qapi/error.h" #include "hw/sysbus.h" #include "target/riscv/cpu.h"
@@ -27,6 +28,8 @@static Property riscv_harts_props[] = {DEFINE_PROP_UINT32 ("num-harts" , RISCVHartArrayState, num_harts,1 ),DEFINE_PROP_STRING ("cpu-type" , RISCVHartArrayState, cpu_type), +DEFINE_PROP_UINT64 ("timebase-frequency" , RISCVHartArrayState, time_freq, +NANOSECONDS_PER_SECOND ),DEFINE_PROP_END_OF_LIST (), };
@@ -49,6 +52,7 @@ static void riscv_harts_realize(DeviceState *dev, Error **errp)sizeof (RISCVCPU ), s->cpu_type, &error_abort, NULL); s->harts[n].env.mhartid = n; + s->harts[n].env.time_freq = s->time_freq;qemu_register_reset (riscv_harts_cpu_reset, &s->harts[n]);object_property_set_bool (OBJECT (&s->harts[n]), true,"realized" , &err);
hw/riscv/sifive_clint.c +22 -8
@@ -26,10 +26,10 @@#include "hw/riscv/sifive_clint.h" #include "qemu/timer.h" -static uint64_t cpu_riscv_read_rtc (void ) +static uint64_t cpu_riscv_read_rtc (CPURISCVState *env) {return muldiv64 (qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ), -SIFIVE_CLINT_TIMEBASE_FREQ ,NANOSECONDS_PER_SECOND ); + env->time_freq,NANOSECONDS_PER_SECOND ); }/*
@@ -41,7 +41,7 @@ static void sifive_clint_write_timecmp(RISCVCPU *cpu, uint64_t value)uint64_t next;uint64_t diff; -uint64_t rtc_r =cpu_riscv_read_rtc (); +uint64_t rtc_r =cpu_riscv_read_rtc (&cpu->env); cpu->env.timecmp = value;if (cpu->env.timecmp <= rtc_r) {
@@ -56,7 +56,7 @@ static void sifive_clint_write_timecmp(RISCVCPU *cpu, uint64_t value) diff = cpu->env.timecmp - rtc_r;/* back to ns (note args switched in muldiv64) */ next =qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) + -muldiv64 (diff,NANOSECONDS_PER_SECOND ,SIFIVE_CLINT_TIMEBASE_FREQ ); +muldiv64 (diff,NANOSECONDS_PER_SECOND , cpu->env.time_freq);timer_mod (cpu->env.timer, next); }
@@ -107,11 +107,25 @@ static uint64_t sifive_clint_read(void *opaque, hwaddr addr, unsigned size)return 0 ; } }else if (addr == clint->time_base) { -/* time_lo */ -return cpu_riscv_read_rtc () &0xFFFFFFFF ; +/* All harts must have the same time frequency, so just use hart 0 */ +CPUState *cpu =qemu_get_cpu (0 ); +CPURISCVState *env = cpu ? cpu->env_ptr :NULL ; +if (!env) { +error_report ("clint: hart 0 not valid?!" ); + }else { +/* time_lo */ +return cpu_riscv_read_rtc (env) &0xFFFFFFFF ; + } }else if (addr == clint->time_base +4 ) { -/* time_hi */ -return (cpu_riscv_read_rtc () >>32 ) &0xFFFFFFFF ; +/* All harts must have the same time frequency, so just use hart 0 */ +CPUState *cpu =qemu_get_cpu (0 ); +CPURISCVState *env = cpu ? cpu->env_ptr :NULL ; +if (!env) { +error_report ("clint: hart 0 not valid?!" ); + }else { +/* time_hi */ +return (cpu_riscv_read_rtc (env) >>32 ) &0xFFFFFFFF ; + } }error_report ("clint: invalid read: %08x" , (uint32_t )addr);
hw/riscv/sifive_e.c +2 -0
@@ -146,6 +146,8 @@ static void riscv_sifive_e_soc_init(Object *obj) &error_abort);object_property_set_int (OBJECT (&s->cpus), smp_cpus,"num-harts" , &error_abort); +object_property_set_int (OBJECT (&s->cpus),SIFIVE_E_TIMEBASE_FREQ , +"timebase-frequency" , &error_abort);sysbus_init_child_obj (obj,"riscv.sifive.e.gpio0" , &s->gpio,sizeof (s->gpio),TYPE_SIFIVE_GPIO );
hw/riscv/sifive_u.c +3 -1
@@ -116,7 +116,7 @@ static void create_fdt(SiFiveUState *s, const struct MemmapEntry *memmap,qemu_fdt_add_subnode (fdt,"/cpus" );qemu_fdt_setprop_cell (fdt,"/cpus" ,"timebase-frequency" , -SIFIVE_CLINT_TIMEBASE_FREQ ); +SIFIVE_U_TIMEBASE_FREQ );qemu_fdt_setprop_cell (fdt,"/cpus" ,"#size-cells" ,0x0 );qemu_fdt_setprop_cell (fdt,"/cpus" ,"#address-cells" ,0x1 );
@@ -329,6 +329,8 @@ static void riscv_sifive_u_soc_init(Object *obj) &error_abort);object_property_set_int (OBJECT (&s->cpus), smp_cpus,"num-harts" , &error_abort); +object_property_set_int (OBJECT (&s->cpus),SIFIVE_U_TIMEBASE_FREQ , +"timebase-frequency" , &error_abort);sysbus_init_child_obj (obj,"gem" , &s->gem,sizeof (s->gem),TYPE_CADENCE_GEM );
hw/riscv/spike.c +5 -1
@@ -106,7 +106,7 @@ static void create_fdt(SpikeState *s, const struct MemmapEntry *memmap,qemu_fdt_add_subnode (fdt,"/cpus" );qemu_fdt_setprop_cell (fdt,"/cpus" ,"timebase-frequency" , -SIFIVE_CLINT_TIMEBASE_FREQ ); +SPIKE_TIMEBASE_FREQ );qemu_fdt_setprop_cell (fdt,"/cpus" ,"#size-cells" ,0x0 );qemu_fdt_setprop_cell (fdt,"/cpus" ,"#address-cells" ,0x1 );
@@ -180,6 +180,8 @@ static void spike_board_init(MachineState *machine) &error_abort);object_property_set_int (OBJECT (&s->soc), smp_cpus,"num-harts" , &error_abort); +object_property_set_int (OBJECT (&s->soc),SPIKE_TIMEBASE_FREQ , +"timebase-frequency" , &error_abort);object_property_set_bool (OBJECT (&s->soc), true,"realized" , &error_abort);
@@ -268,6 +270,8 @@ static void spike_v1_10_0_board_init(MachineState *machine) &error_abort);object_property_set_int (OBJECT (&s->soc), smp_cpus,"num-harts" , &error_abort); +object_property_set_int (OBJECT (&s->soc),SPIKE_TIMEBASE_FREQ , +"timebase-frequency" , &error_abort);object_property_set_bool (OBJECT (&s->soc), true,"realized" , &error_abort);
hw/riscv/virt.c +3 -1
@@ -185,7 +185,7 @@ static void *create_fdt(RISCVVirtState *s, const struct MemmapEntry *memmap,qemu_fdt_add_subnode (fdt,"/cpus" );qemu_fdt_setprop_cell (fdt,"/cpus" ,"timebase-frequency" , -SIFIVE_CLINT_TIMEBASE_FREQ ); +VIRT_TIMEBASE_FREQ );qemu_fdt_setprop_cell (fdt,"/cpus" ,"#size-cells" ,0x0 );qemu_fdt_setprop_cell (fdt,"/cpus" ,"#address-cells" ,0x1 );
@@ -403,6 +403,8 @@ static void riscv_virt_board_init(MachineState *machine) &error_abort);object_property_set_int (OBJECT (&s->soc), smp_cpus,"num-harts" , &error_abort); +object_property_set_int (OBJECT (&s->soc),VIRT_TIMEBASE_FREQ , +"timebase-frequency" , &error_abort);object_property_set_bool (OBJECT (&s->soc), true,"realized" , &error_abort);
include/hw/riscv/riscv_hart.h +1 -0
@@ -33,6 +33,7 @@ typedef struct RISCVHartArrayState {/*< public >*/ uint32_t num_harts;char *cpu_type; +uint64_t time_freq;RISCVCPU *harts; } RISCVHartArrayState;
include/hw/riscv/sifive_clint.h +0 -4
@@ -47,8 +47,4 @@ enum {SIFIVE_TIME_BASE =0xBFF8 }; -enum { -SIFIVE_CLINT_TIMEBASE_FREQ =10000000 -}; -#endif
include/hw/riscv/sifive_e.h +4 -0
@@ -71,6 +71,10 @@ enum { SIFIVE_E_GPIO0_IRQ0 = 8 }; +enum { +SIFIVE_E_TIMEBASE_FREQ =10000000 , +}; +#define SIFIVE_E_PLIC_HART_CONFIG "M"#define SIFIVE_E_PLIC_NUM_SOURCES 127#define SIFIVE_E_PLIC_NUM_PRIORITIES 7
include/hw/riscv/sifive_u.h +1 -0
@@ -63,6 +63,7 @@ enum { };enum { +SIFIVE_U_TIMEBASE_FREQ =10000000 ,SIFIVE_U_CLOCK_FREQ =1000000000 ,SIFIVE_U_GEM_CLOCK_FREQ =125000000 };
include/hw/riscv/spike.h +1 -0
@@ -36,6 +36,7 @@ enum { };enum { +SPIKE_TIMEBASE_FREQ =10000000 ,SPIKE_CLOCK_FREQ =1000000000 };
include/hw/riscv/virt.h +1 -0
@@ -53,6 +53,7 @@ enum { };enum { +VIRT_TIMEBASE_FREQ =10000000 ,VIRT_CLOCK_FREQ =1000000000 };
target/riscv/cpu.h +2 -0
@@ -175,7 +175,9 @@ struct CPURISCVState {/* temporary htif regs */ uint64_t mfromhost;uint64_t mtohost; +uint64_t timecmp; +uint64_t time_freq;/* physical memory protection */ pmp_table_t pmp_state;
target/riscv/csr.c +11 -6
@@ -191,22 +191,31 @@ static int read_instreth(CPURISCVState *env, int csrno, target_ulong *val) }#endif /* TARGET_RISCV32 */ -#if defined(CONFIG_USER_ONLY )static int read_time (CPURISCVState *env,int csrno,target_ulong *val) { +#if !defined(CONFIG_USER_ONLY ) + *val =muldiv64 (qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ), + env->time_freq,NANOSECONDS_PER_SECOND ); +#else *val =cpu_get_host_ticks (); +#endif return 0 ; }#if defined(TARGET_RISCV32 )static int read_timeh (CPURISCVState *env,int csrno,target_ulong *val) { +#if !defined(CONFIG_USER_ONLY ) + *val =muldiv64 (qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ), + env->time_freq,NANOSECONDS_PER_SECOND ) >>32 ; +#else *val =cpu_get_host_ticks () >>32 ; +#endif return 0 ; }#endif -#else /* CONFIG_USER_ONLY */ +#if !defined(CONFIG_USER_ONLY )/* Machine constants */
@@ -856,14 +865,10 @@ 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 ) [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 */
Signature · 1 line
2.20.1
Quoted 334 lines · On Tue, 28 May 2019 11:30:20 PDT (-0700), [email protected] wrote:
Currently mcounteren.TM acts as though it is hardwired to zero, even though QEMU allows it to be set. This change resolves the issue by allowing reads to the time and timeh control registers when running in a privileged mode where such accesses are allowed.
The frequency of the time register is stored in the
time_freqfield of each hart so that it is accessible during CSR reads, but must be the same across all harts. Each board can initialize it to a custom value, although all currently use a 10 MHz frequency.Signed-off-by Jonathan Behrens <[email protected]>--- hw/riscv/riscv_hart.c | 4 ++++ hw/riscv/sifive_clint.c | 30 ++++++++++++++++++++++-------- hw/riscv/sifive_e.c | 2 ++ hw/riscv/sifive_u.c | 4 +++- hw/riscv/spike.c | 6 +++++- hw/riscv/virt.c | 4 +++- include/hw/riscv/riscv_hart.h | 1 + include/hw/riscv/sifive_clint.h | 4 ---- include/hw/riscv/sifive_e.h | 4 ++++ include/hw/riscv/sifive_u.h | 1 + include/hw/riscv/spike.h | 1 + include/hw/riscv/virt.h | 1 + target/riscv/cpu.h | 2 ++ target/riscv/csr.c | 17 +++++++++++------ 14 files changed, 60 insertions(+), 21 deletions(-)hw/riscv/riscv_hart.c +4
-0@@ -19,6 +19,7 @@*/ #include "qemu/osdep.h" +#include "qemu/timer.h" #include "qapi/error.h" #include "hw/sysbus.h" #include "target/riscv/cpu.h" @@ -27,6 +28,8 @@static Property riscv_harts_props[] = {DEFINE_PROP_UINT32 ("num-harts" , RISCVHartArrayState, num_harts,1 ),DEFINE_PROP_STRING ("cpu-type" , RISCVHartArrayState, cpu_type), +DEFINE_PROP_UINT64 ("timebase-frequency" , RISCVHartArrayState, time_freq, +NANOSECONDS_PER_SECOND ),DEFINE_PROP_END_OF_LIST (), };@@ -49,6 +52,7 @@ static void riscv_harts_realize(DeviceState *dev, Error **errp)sizeof (RISCVCPU ), s->cpu_type, &error_abort, NULL); s->harts[n].env.mhartid = n; + s->harts[n].env.time_freq = s->time_freq;qemu_register_reset (riscv_harts_cpu_reset, &s->harts[n]);object_property_set_bool (OBJECT (&s->harts[n]), true,"realized" , &err);hw/riscv/sifive_clint.c +22
-8@@ -26,10 +26,10 @@#include "hw/riscv/sifive_clint.h" #include "qemu/timer.h" -static uint64_t cpu_riscv_read_rtc (void ) +static uint64_t cpu_riscv_read_rtc (CPURISCVState *env) {return muldiv64 (qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ), -SIFIVE_CLINT_TIMEBASE_FREQ ,NANOSECONDS_PER_SECOND ); + env->time_freq,NANOSECONDS_PER_SECOND ); }/* @@ -41,7 +41,7 @@ static void sifive_clint_write_timecmp(RISCVCPU *cpu, uint64_t value)uint64_t next;uint64_t diff; -uint64_t rtc_r =cpu_riscv_read_rtc (); +uint64_t rtc_r =cpu_riscv_read_rtc (&cpu->env); cpu->env.timecmp = value;if (cpu->env.timecmp <= rtc_r) {@@ -56,7 +56,7 @@ static void sifive_clint_write_timecmp(RISCVCPU *cpu, uint64_t value) diff = cpu->env.timecmp - rtc_r;/* back to ns (note args switched in muldiv64) */ next =qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) + -muldiv64 (diff,NANOSECONDS_PER_SECOND ,SIFIVE_CLINT_TIMEBASE_FREQ ); +muldiv64 (diff,NANOSECONDS_PER_SECOND , cpu->env.time_freq);timer_mod (cpu->env.timer, next); }@@ -107,11 +107,25 @@ static uint64_t sifive_clint_read(void *opaque, hwaddr addr, unsigned size)return 0 ; } }else if (addr == clint->time_base) { -/* time_lo */ -return cpu_riscv_read_rtc () &0xFFFFFFFF ; +/* All harts must have the same time frequency, so just use hart 0 */ +CPUState *cpu =qemu_get_cpu (0 ); +CPURISCVState *env = cpu ? cpu->env_ptr :NULL ; +if (!env) { +error_report ("clint: hart 0 not valid?!" ); + }else { +/* time_lo */ +return cpu_riscv_read_rtc (env) &0xFFFFFFFF ; + } }else if (addr == clint->time_base +4 ) { -/* time_hi */ -return (cpu_riscv_read_rtc () >>32 ) &0xFFFFFFFF ; +/* All harts must have the same time frequency, so just use hart 0 */ +CPUState *cpu =qemu_get_cpu (0 ); +CPURISCVState *env = cpu ? cpu->env_ptr :NULL ; +if (!env) { +error_report ("clint: hart 0 not valid?!" ); + }else { +/* time_hi */ +return (cpu_riscv_read_rtc (env) >>32 ) &0xFFFFFFFF ; + } }error_report ("clint: invalid read: %08x" , (uint32_t )addr);hw/riscv/sifive_e.c +2
-0@@ -146,6 +146,8 @@ static void riscv_sifive_e_soc_init(Object *obj) &error_abort);object_property_set_int (OBJECT (&s->cpus), smp_cpus,"num-harts" , &error_abort); +object_property_set_int (OBJECT (&s->cpus),SIFIVE_E_TIMEBASE_FREQ , +"timebase-frequency" , &error_abort);sysbus_init_child_obj (obj,"riscv.sifive.e.gpio0" , &s->gpio,sizeof (s->gpio),TYPE_SIFIVE_GPIO );hw/riscv/sifive_u.c +3
-1@@ -116,7 +116,7 @@ static void create_fdt(SiFiveUState *s, const struct MemmapEntry *memmap,qemu_fdt_add_subnode (fdt,"/cpus" );qemu_fdt_setprop_cell (fdt,"/cpus" ,"timebase-frequency" , -SIFIVE_CLINT_TIMEBASE_FREQ ); +SIFIVE_U_TIMEBASE_FREQ );qemu_fdt_setprop_cell (fdt,"/cpus" ,"#size-cells" ,0x0 );qemu_fdt_setprop_cell (fdt,"/cpus" ,"#address-cells" ,0x1 );@@ -329,6 +329,8 @@ static void riscv_sifive_u_soc_init(Object *obj) &error_abort);object_property_set_int (OBJECT (&s->cpus), smp_cpus,"num-harts" , &error_abort); +object_property_set_int (OBJECT (&s->cpus),SIFIVE_U_TIMEBASE_FREQ , +"timebase-frequency" , &error_abort);sysbus_init_child_obj (obj,"gem" , &s->gem,sizeof (s->gem),TYPE_CADENCE_GEM );hw/riscv/spike.c +5
-1@@ -106,7 +106,7 @@ static void create_fdt(SpikeState *s, const struct MemmapEntry *memmap,qemu_fdt_add_subnode (fdt,"/cpus" );qemu_fdt_setprop_cell (fdt,"/cpus" ,"timebase-frequency" , -SIFIVE_CLINT_TIMEBASE_FREQ ); +SPIKE_TIMEBASE_FREQ );qemu_fdt_setprop_cell (fdt,"/cpus" ,"#size-cells" ,0x0 );qemu_fdt_setprop_cell (fdt,"/cpus" ,"#address-cells" ,0x1 );@@ -180,6 +180,8 @@ static void spike_board_init(MachineState *machine) &error_abort);object_property_set_int (OBJECT (&s->soc), smp_cpus,"num-harts" , &error_abort); +object_property_set_int (OBJECT (&s->soc),SPIKE_TIMEBASE_FREQ , +"timebase-frequency" , &error_abort);object_property_set_bool (OBJECT (&s->soc), true,"realized" , &error_abort);@@ -268,6 +270,8 @@ static void spike_v1_10_0_board_init(MachineState *machine) &error_abort);object_property_set_int (OBJECT (&s->soc), smp_cpus,"num-harts" , &error_abort); +object_property_set_int (OBJECT (&s->soc),SPIKE_TIMEBASE_FREQ , +"timebase-frequency" , &error_abort);object_property_set_bool (OBJECT (&s->soc), true,"realized" , &error_abort);hw/riscv/virt.c +3
-1@@ -185,7 +185,7 @@ static void *create_fdt(RISCVVirtState *s, const struct MemmapEntry *memmap,qemu_fdt_add_subnode (fdt,"/cpus" );qemu_fdt_setprop_cell (fdt,"/cpus" ,"timebase-frequency" , -SIFIVE_CLINT_TIMEBASE_FREQ ); +VIRT_TIMEBASE_FREQ );qemu_fdt_setprop_cell (fdt,"/cpus" ,"#size-cells" ,0x0 );qemu_fdt_setprop_cell (fdt,"/cpus" ,"#address-cells" ,0x1 );@@ -403,6 +403,8 @@ static void riscv_virt_board_init(MachineState *machine) &error_abort);object_property_set_int (OBJECT (&s->soc), smp_cpus,"num-harts" , &error_abort); +object_property_set_int (OBJECT (&s->soc),VIRT_TIMEBASE_FREQ , +"timebase-frequency" , &error_abort);object_property_set_bool (OBJECT (&s->soc), true,"realized" , &error_abort);include/hw/riscv/riscv_hart.h +1
-0@@ -33,6 +33,7 @@ typedef struct RISCVHartArrayState {/*< public >*/ uint32_t num_harts;char *cpu_type; +uint64_t time_freq;RISCVCPU *harts; } RISCVHartArrayState;include/hw/riscv/sifive_clint.h +0
-4@@ -47,8 +47,4 @@ enum {SIFIVE_TIME_BASE =0xBFF8 }; -enum { -SIFIVE_CLINT_TIMEBASE_FREQ =10000000 -}; -#endif include/hw/riscv/sifive_e.h +4
-0@@ -71,6 +71,10 @@ enum { SIFIVE_E_GPIO0_IRQ0 = 8 }; +enum { +SIFIVE_E_TIMEBASE_FREQ =10000000 , +}; +#define SIFIVE_E_PLIC_HART_CONFIG "M"#define SIFIVE_E_PLIC_NUM_SOURCES 127#define SIFIVE_E_PLIC_NUM_PRIORITIES 7include/hw/riscv/sifive_u.h +1
-0@@ -63,6 +63,7 @@ enum { };enum { +SIFIVE_U_TIMEBASE_FREQ =10000000 ,SIFIVE_U_CLOCK_FREQ =1000000000 ,SIFIVE_U_GEM_CLOCK_FREQ =125000000 };include/hw/riscv/spike.h +1
-0@@ -36,6 +36,7 @@ enum { };enum { +SPIKE_TIMEBASE_FREQ =10000000 ,SPIKE_CLOCK_FREQ =1000000000 };include/hw/riscv/virt.h +1
-0@@ -53,6 +53,7 @@ enum { };enum { +VIRT_TIMEBASE_FREQ =10000000 ,VIRT_CLOCK_FREQ =1000000000 };target/riscv/cpu.h +2
-0@@ -175,7 +175,9 @@ struct CPURISCVState {/* temporary htif regs */ uint64_t mfromhost;uint64_t mtohost; +uint64_t timecmp; +uint64_t time_freq;/* physical memory protection */ pmp_table_t pmp_state;target/riscv/csr.c +11
-2@@ -191,22 +191,31 @@ static int read_instreth(CPURISCVState *env, int csrno, target_ulong *val) }#endif /* TARGET_RISCV32 */ -#if defined(CONFIG_USER_ONLY )static int read_time (CPURISCVState *env,int csrno,target_ulong *val) { +#if !defined(CONFIG_USER_ONLY ) + *val =muldiv64 (qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ), + env->time_freq,NANOSECONDS_PER_SECOND ); +#else *val =cpu_get_host_ticks (); +#endif return 0 ; }#if defined(TARGET_RISCV32 )static int read_timeh (CPURISCVState *env,int csrno,target_ulong *val) { +#if !defined(CONFIG_USER_ONLY ) + *val =muldiv64 (qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ), + env->time_freq,NANOSECONDS_PER_SECOND ) >>32 ; +#else *val =cpu_get_host_ticks () >>32 ; +#endif return 0 ; }#endif -#else /* CONFIG_USER_ONLY */ +#if !defined(CONFIG_USER_ONLY )/* Machine constants */ @@ -856,14 +865,10 @@ static riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_INSTRETH] = { ctr, read_instreth },
target/riscv/csr.c static riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { · line 866
Quoted 10 lines
target/riscv/csr.c +0
-4#endif -/* User-level time CSRs are only available in linux-user -* In privileged mode, the monitor emulates these CSRs */ -#if defined(CONFIG_USER_ONLY) [CSR_TIME] = { ctr, read_time }, #if defined(TARGET_RISCV32) [CSR_TIMEH] = { ctr, read_timeh }, #endif -#endif
I would prefer to avoid making the timer CSRs availiable to S-mode and below, as that doesn't match what implementations actually do.
Quoted 2 lines
target/riscv/csr.c +0
-0#if !defined(CONFIG_USER_ONLY )/* Machine Timers and Counters */
A message that is not in the archive (sent off-list or not yet received): CANnJOVELaycXSxJaqMqhB8P78ikGYQLvT1KfYcKPpPNuJtRBAQ@mail.gmail.com
Apparently my previous message didn't make it out onto the list (sorry about all these email glitches!). I've included the message again below. Hopefully either a patch like this one or something simpler that just hard codes mcounteren.TM to zero (so QEMU is at least conformant) can be merged in time for 4.1.
Quoted 390 lines · Jonathan Behrens
I'm not sure that is accurate. Based on the discussion here
<https://forums.sifive.com/t/possible-bug-in-counteren-csrs/2123> the HiFive Unleashed actually does support reading the timer CSR from unprivileged modes (from that discussion it does so a little too well... but it should presumably be fixed in later iterations of the processor). And even if no real hardware supported this capability, it still might make sense to provide it in QEMU as an optimization.Quoted 379 lines · Palmer Dabbelt
Quoted 1 line · On Tue, 28 May 2019 11:30:20 PDT (-0700), [email protected] wrote:
Currently mcounteren.TM acts as though it is hardwired to zero, even
though QEMU allows it to be set. This change resolves the issue by allowing reads to the time and timeh control registers when running in a privileged mode where such accesses are allowed.
Quoted 2 lines
The frequency of the time register is stored in the
time_freqfield ofeach hart so that it is accessible during CSR reads, but must be the same across all harts. Each board can initialize it to a custom value, although all currently use a 10 MHz frequency.
Quoted 32 lines
Signed-off-by Jonathan Behrens <[email protected]>--- hw/riscv/riscv_hart.c | 4 ++++ hw/riscv/sifive_clint.c | 30 ++++++++++++++++++++++-------- hw/riscv/sifive_e.c | 2 ++ hw/riscv/sifive_u.c | 4 +++- hw/riscv/spike.c | 6 +++++- hw/riscv/virt.c | 4 +++- include/hw/riscv/riscv_hart.h | 1 + include/hw/riscv/sifive_clint.h | 4 ---- include/hw/riscv/sifive_e.h | 4 ++++ include/hw/riscv/sifive_u.h | 1 + include/hw/riscv/spike.h | 1 + include/hw/riscv/virt.h | 1 + target/riscv/cpu.h | 2 ++ target/riscv/csr.c | 17 +++++++++++------ 14 files changed, 60 insertions(+), 21 deletions(-)hw/riscv/riscv_hart.c +1
-0@@ -19,6 +19,7 @@*/ #include "qemu/osdep.h" +#include "qemu/timer.h" #include "qapi/error.h" #include "hw/sysbus.h" #include "target/riscv/cpu.h" hw/riscv/riscv_hart.c line 31
Quoted 4 lines
hw/riscv/riscv_hart.c +1
-0@@ -27,6 +28,8 @@static Property riscv_harts_props[] = {DEFINE_PROP_UINT32 ("num-harts" , RISCVHartArrayState, num_harts,1 ),DEFINE_PROP_STRING ("cpu-type" , RISCVHartArrayState, cpu_type), + DEFINE_PROP_UINT64("timebase-frequency", RISCVHartArrayState,
time_freq,hw/riscv/riscv_hart.c static void riscv_harts_realize(DeviceState *dev,
Quoted 5 lines
hw/riscv/riscv_hart.c +1
-0+ NANOSECONDS_PER_SECOND), DEFINE_PROP_END_OF_LIST(), };@@ -49,6 +52,7 @@ static void riscv_harts_realize(DeviceState *dev,Error **errp)
hw/riscv/sifive_clint.c static void sifive_clint_write_timecmp(RISCVCPU *cpu,
Quoted 25 lines
hw/riscv/riscv_hart.c +1
-0sizeof (RISCVCPU ), s->cpu_type, &error_abort, NULL); s->harts[n].env.mhartid = n; + s->harts[n].env.time_freq = s->time_freq;qemu_register_reset (riscv_harts_cpu_reset, &s->harts[n]);object_property_set_bool (OBJECT (&s->harts[n]), true,"realized" , &err);hw/riscv/sifive_clint.c +2
-2@@ -26,10 +26,10 @@#include "hw/riscv/sifive_clint.h" #include "qemu/timer.h" -static uint64_t cpu_riscv_read_rtc (void ) +static uint64_t cpu_riscv_read_rtc (CPURISCVState *env) {return muldiv64 (qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ), -SIFIVE_CLINT_TIMEBASE_FREQ ,NANOSECONDS_PER_SECOND ); + env->time_freq,NANOSECONDS_PER_SECOND ); }/* @@ -41,7 +41,7 @@ static void sifive_clint_write_timecmp(RISCVCPU *cpu,
uint64_tvalue)hw/riscv/sifive_clint.c static void sifive_clint_write_timecmp(RISCVCPU *cpu,
Quoted 9 lines
hw/riscv/sifive_clint.c +1
-1uint64_t next;uint64_t diff; -uint64_t rtc_r =cpu_riscv_read_rtc (); +uint64_t rtc_r =cpu_riscv_read_rtc (&cpu->env); cpu->env.timecmp = value;if (cpu->env.timecmp <= rtc_r) {@@ -56,7 +56,7 @@ static void sifive_clint_write_timecmp(RISCVCPU *cpu,
uint64_tvalue)hw/riscv/sifive_clint.c static void sifive_clint_write_timecmp(RISCVCPU *cpu, uint64_t value)
Quoted 4 lines
hw/riscv/sifive_clint.c +0
-1diff = cpu->env.timecmp - rtc_r;/* back to ns (note args switched in muldiv64) */ next = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + - muldiv64(diff, NANOSECONDS_PER_SECOND,
SIFIVE_CLINT_TIMEBASE_FREQ);hw/riscv/sifive_clint.c static uint64_t sifive_clint_read(void *opaque,
Quoted 5 lines
hw/riscv/sifive_clint.c +1
-0+muldiv64 (diff,NANOSECONDS_PER_SECOND , cpu->env.time_freq);timer_mod (cpu->env.timer, next); }@@ -107,11 +107,25 @@ static uint64_t sifive_clint_read(void *opaque,hwaddr addr, unsigned size)
hw/riscv/sifive_clint.c static uint64_t sifive_clint_read(void *opaque, hwaddr addr, unsigned size)
Quoted 6 lines
hw/riscv/sifive_clint.c +1
-2return 0 ; } }else if (addr == clint->time_base) { -/* time_lo */ -return cpu_riscv_read_rtc () &0xFFFFFFFF ; +/* All harts must have the same time frequency, so just use hart 0 */
hw/riscv/sifive_clint.c static uint64_t sifive_clint_read(void *opaque, hwaddr addr, unsigned size)
Quoted 12 lines
hw/riscv/sifive_clint.c +9
-2+CPUState *cpu =qemu_get_cpu (0 ); +CPURISCVState *env = cpu ? cpu->env_ptr :NULL ; +if (!env) { +error_report ("clint: hart 0 not valid?!" ); + }else { +/* time_lo */ +return cpu_riscv_read_rtc (env) &0xFFFFFFFF ; + } }else if (addr == clint->time_base +4 ) { -/* time_hi */ -return (cpu_riscv_read_rtc () >>32 ) &0xFFFFFFFF ; +/* All harts must have the same time frequency, so just use hart 0 */
hw/riscv/sifive_u.c static void create_fdt(SiFiveUState *s, const
Quoted 29 lines
hw/riscv/sifive_clint.c +8
-0+CPUState *cpu =qemu_get_cpu (0 ); +CPURISCVState *env = cpu ? cpu->env_ptr :NULL ; +if (!env) { +error_report ("clint: hart 0 not valid?!" ); + }else { +/* time_hi */ +return (cpu_riscv_read_rtc (env) >>32 ) &0xFFFFFFFF ; + } }error_report ("clint: invalid read: %08x" , (uint32_t )addr);hw/riscv/sifive_e.c +2
-0@@ -146,6 +146,8 @@ static void riscv_sifive_e_soc_init(Object *obj) &error_abort);object_property_set_int (OBJECT (&s->cpus), smp_cpus,"num-harts" , &error_abort); +object_property_set_int (OBJECT (&s->cpus),SIFIVE_E_TIMEBASE_FREQ , +"timebase-frequency" , &error_abort);sysbus_init_child_obj (obj,"riscv.sifive.e.gpio0" , &s->gpio,sizeof (s->gpio),TYPE_SIFIVE_GPIO );hw/riscv/sifive_u.c +0
-0@@ -116,7 +116,7 @@ static void create_fdt(SiFiveUState *s, conststruct MemmapEntry *memmap,
hw/riscv/spike.c static void create_fdt(SpikeState *s, const struct
Quoted 22 lines
hw/riscv/sifive_u.c +3
-1qemu_fdt_add_subnode (fdt,"/cpus" );qemu_fdt_setprop_cell (fdt,"/cpus" ,"timebase-frequency" , -SIFIVE_CLINT_TIMEBASE_FREQ ); +SIFIVE_U_TIMEBASE_FREQ );qemu_fdt_setprop_cell (fdt,"/cpus" ,"#size-cells" ,0x0 );qemu_fdt_setprop_cell (fdt,"/cpus" ,"#address-cells" ,0x1 );@@ -329,6 +329,8 @@ static void riscv_sifive_u_soc_init(Object *obj) &error_abort);object_property_set_int (OBJECT (&s->cpus), smp_cpus,"num-harts" , &error_abort); +object_property_set_int (OBJECT (&s->cpus),SIFIVE_U_TIMEBASE_FREQ , +"timebase-frequency" , &error_abort);sysbus_init_child_obj (obj,"gem" , &s->gem,sizeof (s->gem),TYPE_CADENCE_GEM );hw/riscv/spike.c +0
-0@@ -106,7 +106,7 @@ static void create_fdt(SpikeState *s, const structMemmapEntry *memmap,
hw/riscv/spike.c static void spike_v1_10_0_board_init(MachineState
Quoted 18 lines
hw/riscv/spike.c +3
-1qemu_fdt_add_subnode (fdt,"/cpus" );qemu_fdt_setprop_cell (fdt,"/cpus" ,"timebase-frequency" , -SIFIVE_CLINT_TIMEBASE_FREQ ); +SPIKE_TIMEBASE_FREQ );qemu_fdt_setprop_cell (fdt,"/cpus" ,"#size-cells" ,0x0 );qemu_fdt_setprop_cell (fdt,"/cpus" ,"#address-cells" ,0x1 );@@ -180,6 +180,8 @@ static void spike_board_init(MachineState *machine) &error_abort);object_property_set_int (OBJECT (&s->soc), smp_cpus,"num-harts" , &error_abort); +object_property_set_int (OBJECT (&s->soc),SPIKE_TIMEBASE_FREQ , +"timebase-frequency" , &error_abort);object_property_set_bool (OBJECT (&s->soc), true,"realized" , &error_abort);@@ -268,6 +270,8 @@ static void spike_v1_10_0_board_init(MachineState*machine)
hw/riscv/virt.c static void *create_fdt(RISCVVirtState *s, const
Quoted 13 lines
hw/riscv/spike.c +2
-0&error_abort);object_property_set_int (OBJECT (&s->soc), smp_cpus,"num-harts" , &error_abort); +object_property_set_int (OBJECT (&s->soc),SPIKE_TIMEBASE_FREQ , +"timebase-frequency" , &error_abort);object_property_set_bool (OBJECT (&s->soc), true,"realized" , &error_abort);hw/riscv/virt.c +0
-0@@ -185,7 +185,7 @@ static void *create_fdt(RISCVVirtState *s, conststruct MemmapEntry *memmap,
hw/riscv/virt.c static void riscv_virt_board_init(MachineState
Quoted 9 lines
hw/riscv/virt.c +1
-1qemu_fdt_add_subnode (fdt,"/cpus" );qemu_fdt_setprop_cell (fdt,"/cpus" ,"timebase-frequency" , -SIFIVE_CLINT_TIMEBASE_FREQ ); +VIRT_TIMEBASE_FREQ );qemu_fdt_setprop_cell (fdt,"/cpus" ,"#size-cells" ,0x0 );qemu_fdt_setprop_cell (fdt,"/cpus" ,"#address-cells" ,0x1 );@@ -403,6 +403,8 @@ static void riscv_virt_board_init(MachineState*machine)
Review
Quoted 9 lines
hw/riscv/virt.c +2
-0&error_abort);object_property_set_int (OBJECT (&s->soc), smp_cpus,"num-harts" , &error_abort); +object_property_set_int (OBJECT (&s->soc),VIRT_TIMEBASE_FREQ , +"timebase-frequency" , &error_abort);object_property_set_bool (OBJECT (&s->soc), true,"realized" , &error_abort);(quoted diff) +0
-0
b/include/hw/riscv/riscv_hart.hReview
Quoted 12 lines
index
0671d88a44..2e82e85b2b100644include/hw/riscv/riscv_hart.h +1
-0@@ -33,6 +33,7 @@ typedef struct RISCVHartArrayState {/*< public >*/ uint32_t num_harts;char *cpu_type; +uint64_t time_freq;RISCVCPU *harts; } RISCVHartArrayState;(quoted diff) +0
-0
b/include/hw/riscv/sifive_clint.htarget/riscv/csr.c static int read_instreth(CPURISCVState *env, int
Quoted 82 lines
index
e2865be1d1..aaa2a58c6e100644include/hw/riscv/sifive_clint.h +0
-4@@ -47,8 +47,4 @@ enum {SIFIVE_TIME_BASE =0xBFF8 }; -enum { -SIFIVE_CLINT_TIMEBASE_FREQ =10000000 -}; -#endif include/hw/riscv/sifive_e.h +4
-0@@ -71,6 +71,10 @@ enum { SIFIVE_E_GPIO0_IRQ0 = 8 }; +enum { +SIFIVE_E_TIMEBASE_FREQ =10000000 , +}; +#define SIFIVE_E_PLIC_HART_CONFIG "M"#define SIFIVE_E_PLIC_NUM_SOURCES 127#define SIFIVE_E_PLIC_NUM_PRIORITIES 7include/hw/riscv/sifive_u.h +1
-0@@ -63,6 +63,7 @@ enum { };enum { +SIFIVE_U_TIMEBASE_FREQ =10000000 ,SIFIVE_U_CLOCK_FREQ =1000000000 ,SIFIVE_U_GEM_CLOCK_FREQ =125000000 };include/hw/riscv/spike.h +1
-0@@ -36,6 +36,7 @@ enum { };enum { +SPIKE_TIMEBASE_FREQ =10000000 ,SPIKE_CLOCK_FREQ =1000000000 };include/hw/riscv/virt.h +1
-0@@ -53,6 +53,7 @@ enum { };enum { +VIRT_TIMEBASE_FREQ =10000000 ,VIRT_CLOCK_FREQ =1000000000 };target/riscv/cpu.h +2
-0@@ -175,7 +175,9 @@ struct CPURISCVState {/* temporary htif regs */ uint64_t mfromhost;uint64_t mtohost; +uint64_t timecmp; +uint64_t time_freq;/* physical memory protection */ pmp_table_t pmp_state;target/riscv/csr.c +0
-0@@ -191,22 +191,31 @@ static int read_instreth(CPURISCVState *env, intcsrno,
target_ulong*val)target/riscv/csr.c static riscv_csr_operations
Quoted 34 lines
target/riscv/csr.c +11
-2}#endif /* TARGET_RISCV32 */ -#if defined(CONFIG_USER_ONLY )static int read_time (CPURISCVState *env,int csrno,target_ulong *val) { +#if !defined(CONFIG_USER_ONLY ) + *val =muldiv64 (qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ), + env->time_freq,NANOSECONDS_PER_SECOND ); +#else *val =cpu_get_host_ticks (); +#endif return 0 ; }#if defined(TARGET_RISCV32 )static int read_timeh (CPURISCVState *env,int csrno,target_ulong *val) { +#if !defined(CONFIG_USER_ONLY ) + *val =muldiv64 (qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ), + env->time_freq,NANOSECONDS_PER_SECOND ) >>32 ; +#else *val =cpu_get_host_ticks () >>32 ; +#endif return 0 ; }#endif -#else /* CONFIG_USER_ONLY */ +#if !defined(CONFIG_USER_ONLY )/* Machine constants */ @@ -856,14 +865,10 @@ static riscv_csr_operationscsr_ops[CSR_TABLE_SIZE] = {
target/riscv/csr.c static riscv_csr_operations · line 865
Quoted 1 line
target/riscv/csr.c +0
-0[CSR_INSTRETH] = { ctr, read_instreth},target/riscv/csr.c static riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
Quoted 6 lines
target/riscv/csr.c +0
-3#endif -/* User-level time CSRs are only available in linux-user -* In privileged mode, the monitor emulates these CSRs */ -#if defined(CONFIG_USER_ONLY ) [CSR_TIME] = { ctr, read_time},target/riscv/csr.c static riscv_csr_operations · line 870
Quoted 2 lines
target/riscv/csr.c +0
-0#if defined(TARGET_RISCV32 ) [CSR_TIMEH] = { ctr, read_timeh},target/riscv/csr.c static riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { · line 866
Quoted 2 lines
target/riscv/csr.c +0
-1#endif -#endif I would prefer to avoid making the timer CSRs availiable to S-mode and below,
as that doesn't match what implementations actually do.Quoted 2 lines
target/riscv/csr.c +0
-0#if !defined(CONFIG_USER_ONLY )/* Machine Timers and Counters */
Quoted 6 lines · On Mon, 24 Jun 2019 16:03:20 PDT (-0700), [email protected] wrote:
Apparently my previous message didn't make it out onto the list (sorry about all these email glitches!). I've included the message again below. Hopefully either a patch like this one or something simpler that just hard codes mcounteren.TM to zero (so QEMU is at least conformant) can be merged in time for 4.1.
Quoted 10 lines
Quoted 7 lines · Jonathan Behrens
I'm not sure that is accurate. Based on the discussion here
<https://forums.sifive.com/t/possible-bug-in-counteren-csrs/2123> the HiFive Unleashed actually does support reading the timer CSR from unprivileged modes (from that discussion it does so a little too well... but it should presumably be fixed in later iterations of the processor). And even if no real hardware supported this capability, it still might make sense to provide it in QEMU as an optimization.
time and cycle are different registers: rdtime should trap, but rdcycle should work. The hardware traps rdtime into machine mode and emulates it via a memory mapped register. The bug in the FU540-C000 appears to be related to the counter enables, not the presence of the counters.
Quoted 383 lines
Quoted 383 lines · Palmer Dabbelt
Quoted 380 lines · On Tue, 28 May 2019 11:30:20 PDT (-0700), [email protected] wrote:
Quoted 1 line
Currently mcounteren.TM acts as though it is hardwired to zero, even
though QEMU allows it to be set. This change resolves the issue by allowing reads to the time and timeh control registers when running in a privileged mode where such accesses are allowed.
Quoted 2 lines
The frequency of the time register is stored in the
time_freqfield ofeach hart so that it is accessible during CSR reads, but must be the same across all harts. Each board can initialize it to a custom value, although all currently use a 10 MHz frequency.
Quoted 32 lines
Signed-off-by Jonathan Behrens <[email protected]>--- hw/riscv/riscv_hart.c | 4 ++++ hw/riscv/sifive_clint.c | 30 ++++++++++++++++++++++-------- hw/riscv/sifive_e.c | 2 ++ hw/riscv/sifive_u.c | 4 +++- hw/riscv/spike.c | 6 +++++- hw/riscv/virt.c | 4 +++- include/hw/riscv/riscv_hart.h | 1 + include/hw/riscv/sifive_clint.h | 4 ---- include/hw/riscv/sifive_e.h | 4 ++++ include/hw/riscv/sifive_u.h | 1 + include/hw/riscv/spike.h | 1 + include/hw/riscv/virt.h | 1 + target/riscv/cpu.h | 2 ++ target/riscv/csr.c | 17 +++++++++++------ 14 files changed, 60 insertions(+), 21 deletions(-)hw/riscv/riscv_hart.c +1
-0@@ -19,6 +19,7 @@*/ #include "qemu/osdep.h" +#include "qemu/timer.h" #include "qapi/error.h" #include "hw/sysbus.h" #include "target/riscv/cpu.h" hw/riscv/riscv_hart.c line 31
Quoted 4 lines
hw/riscv/riscv_hart.c +1
-0@@ -27,6 +28,8 @@static Property riscv_harts_props[] = {DEFINE_PROP_UINT32 ("num-harts" , RISCVHartArrayState, num_harts,1 ),DEFINE_PROP_STRING ("cpu-type" , RISCVHartArrayState, cpu_type), + DEFINE_PROP_UINT64("timebase-frequency", RISCVHartArrayState,
time_freq,hw/riscv/riscv_hart.c static void riscv_harts_realize(DeviceState *dev,
Quoted 5 lines
hw/riscv/riscv_hart.c +1
-0+ NANOSECONDS_PER_SECOND), DEFINE_PROP_END_OF_LIST(), };@@ -49,6 +52,7 @@ static void riscv_harts_realize(DeviceState *dev,Error **errp)
hw/riscv/sifive_clint.c static void sifive_clint_write_timecmp(RISCVCPU *cpu,
Quoted 25 lines
hw/riscv/riscv_hart.c +1
-0sizeof (RISCVCPU ), s->cpu_type, &error_abort, NULL); s->harts[n].env.mhartid = n; + s->harts[n].env.time_freq = s->time_freq;qemu_register_reset (riscv_harts_cpu_reset, &s->harts[n]);object_property_set_bool (OBJECT (&s->harts[n]), true,"realized" , &err);hw/riscv/sifive_clint.c +2
-2@@ -26,10 +26,10 @@#include "hw/riscv/sifive_clint.h" #include "qemu/timer.h" -static uint64_t cpu_riscv_read_rtc (void ) +static uint64_t cpu_riscv_read_rtc (CPURISCVState *env) {return muldiv64 (qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ), -SIFIVE_CLINT_TIMEBASE_FREQ ,NANOSECONDS_PER_SECOND ); + env->time_freq,NANOSECONDS_PER_SECOND ); }/* @@ -41,7 +41,7 @@ static void sifive_clint_write_timecmp(RISCVCPU *cpu,
uint64_tvalue)hw/riscv/sifive_clint.c static void sifive_clint_write_timecmp(RISCVCPU *cpu,
Quoted 9 lines
hw/riscv/sifive_clint.c +1
-1uint64_t next;uint64_t diff; -uint64_t rtc_r =cpu_riscv_read_rtc (); +uint64_t rtc_r =cpu_riscv_read_rtc (&cpu->env); cpu->env.timecmp = value;if (cpu->env.timecmp <= rtc_r) {@@ -56,7 +56,7 @@ static void sifive_clint_write_timecmp(RISCVCPU *cpu,
uint64_tvalue)hw/riscv/sifive_clint.c static void sifive_clint_write_timecmp(RISCVCPU *cpu, uint64_t value)
Quoted 4 lines
hw/riscv/sifive_clint.c +0
-1diff = cpu->env.timecmp - rtc_r;/* back to ns (note args switched in muldiv64) */ next = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + - muldiv64(diff, NANOSECONDS_PER_SECOND,
SIFIVE_CLINT_TIMEBASE_FREQ);hw/riscv/sifive_clint.c static uint64_t sifive_clint_read(void *opaque,
Quoted 5 lines
hw/riscv/sifive_clint.c +1
-0+muldiv64 (diff,NANOSECONDS_PER_SECOND , cpu->env.time_freq);timer_mod (cpu->env.timer, next); }@@ -107,11 +107,25 @@ static uint64_t sifive_clint_read(void *opaque,hwaddr addr, unsigned size)
hw/riscv/sifive_clint.c static uint64_t sifive_clint_read(void *opaque, hwaddr addr, unsigned size)
Quoted 6 lines
hw/riscv/sifive_clint.c +1
-2return 0 ; } }else if (addr == clint->time_base) { -/* time_lo */ -return cpu_riscv_read_rtc () &0xFFFFFFFF ; +/* All harts must have the same time frequency, so just use hart 0 */
hw/riscv/sifive_clint.c static uint64_t sifive_clint_read(void *opaque, hwaddr addr, unsigned size)
Quoted 12 lines
hw/riscv/sifive_clint.c +9
-2+CPUState *cpu =qemu_get_cpu (0 ); +CPURISCVState *env = cpu ? cpu->env_ptr :NULL ; +if (!env) { +error_report ("clint: hart 0 not valid?!" ); + }else { +/* time_lo */ +return cpu_riscv_read_rtc (env) &0xFFFFFFFF ; + } }else if (addr == clint->time_base +4 ) { -/* time_hi */ -return (cpu_riscv_read_rtc () >>32 ) &0xFFFFFFFF ; +/* All harts must have the same time frequency, so just use hart 0 */
hw/riscv/sifive_u.c static void create_fdt(SiFiveUState *s, const
Quoted 29 lines
hw/riscv/sifive_clint.c +8
-0+CPUState *cpu =qemu_get_cpu (0 ); +CPURISCVState *env = cpu ? cpu->env_ptr :NULL ; +if (!env) { +error_report ("clint: hart 0 not valid?!" ); + }else { +/* time_hi */ +return (cpu_riscv_read_rtc (env) >>32 ) &0xFFFFFFFF ; + } }error_report ("clint: invalid read: %08x" , (uint32_t )addr);hw/riscv/sifive_e.c +2
-0@@ -146,6 +146,8 @@ static void riscv_sifive_e_soc_init(Object *obj) &error_abort);object_property_set_int (OBJECT (&s->cpus), smp_cpus,"num-harts" , &error_abort); +object_property_set_int (OBJECT (&s->cpus),SIFIVE_E_TIMEBASE_FREQ , +"timebase-frequency" , &error_abort);sysbus_init_child_obj (obj,"riscv.sifive.e.gpio0" , &s->gpio,sizeof (s->gpio),TYPE_SIFIVE_GPIO );hw/riscv/sifive_u.c +0
-0@@ -116,7 +116,7 @@ static void create_fdt(SiFiveUState *s, conststruct MemmapEntry *memmap,
hw/riscv/spike.c static void create_fdt(SpikeState *s, const struct
Quoted 22 lines
hw/riscv/sifive_u.c +3
-1qemu_fdt_add_subnode (fdt,"/cpus" );qemu_fdt_setprop_cell (fdt,"/cpus" ,"timebase-frequency" , -SIFIVE_CLINT_TIMEBASE_FREQ ); +SIFIVE_U_TIMEBASE_FREQ );qemu_fdt_setprop_cell (fdt,"/cpus" ,"#size-cells" ,0x0 );qemu_fdt_setprop_cell (fdt,"/cpus" ,"#address-cells" ,0x1 );@@ -329,6 +329,8 @@ static void riscv_sifive_u_soc_init(Object *obj) &error_abort);object_property_set_int (OBJECT (&s->cpus), smp_cpus,"num-harts" , &error_abort); +object_property_set_int (OBJECT (&s->cpus),SIFIVE_U_TIMEBASE_FREQ , +"timebase-frequency" , &error_abort);sysbus_init_child_obj (obj,"gem" , &s->gem,sizeof (s->gem),TYPE_CADENCE_GEM );hw/riscv/spike.c +0
-0@@ -106,7 +106,7 @@ static void create_fdt(SpikeState *s, const structMemmapEntry *memmap,
hw/riscv/spike.c static void spike_v1_10_0_board_init(MachineState
Quoted 18 lines
hw/riscv/spike.c +3
-1qemu_fdt_add_subnode (fdt,"/cpus" );qemu_fdt_setprop_cell (fdt,"/cpus" ,"timebase-frequency" , -SIFIVE_CLINT_TIMEBASE_FREQ ); +SPIKE_TIMEBASE_FREQ );qemu_fdt_setprop_cell (fdt,"/cpus" ,"#size-cells" ,0x0 );qemu_fdt_setprop_cell (fdt,"/cpus" ,"#address-cells" ,0x1 );@@ -180,6 +180,8 @@ static void spike_board_init(MachineState *machine) &error_abort);object_property_set_int (OBJECT (&s->soc), smp_cpus,"num-harts" , &error_abort); +object_property_set_int (OBJECT (&s->soc),SPIKE_TIMEBASE_FREQ , +"timebase-frequency" , &error_abort);object_property_set_bool (OBJECT (&s->soc), true,"realized" , &error_abort);@@ -268,6 +270,8 @@ static void spike_v1_10_0_board_init(MachineState*machine)
hw/riscv/virt.c static void *create_fdt(RISCVVirtState *s, const
Quoted 13 lines
hw/riscv/spike.c +2
-0&error_abort);object_property_set_int (OBJECT (&s->soc), smp_cpus,"num-harts" , &error_abort); +object_property_set_int (OBJECT (&s->soc),SPIKE_TIMEBASE_FREQ , +"timebase-frequency" , &error_abort);object_property_set_bool (OBJECT (&s->soc), true,"realized" , &error_abort);hw/riscv/virt.c +0
-0@@ -185,7 +185,7 @@ static void *create_fdt(RISCVVirtState *s, conststruct MemmapEntry *memmap,
hw/riscv/virt.c static void riscv_virt_board_init(MachineState
Quoted 9 lines
hw/riscv/virt.c +1
-1qemu_fdt_add_subnode (fdt,"/cpus" );qemu_fdt_setprop_cell (fdt,"/cpus" ,"timebase-frequency" , -SIFIVE_CLINT_TIMEBASE_FREQ ); +VIRT_TIMEBASE_FREQ );qemu_fdt_setprop_cell (fdt,"/cpus" ,"#size-cells" ,0x0 );qemu_fdt_setprop_cell (fdt,"/cpus" ,"#address-cells" ,0x1 );@@ -403,6 +403,8 @@ static void riscv_virt_board_init(MachineState*machine)
Review
Quoted 9 lines
hw/riscv/virt.c +2
-0&error_abort);object_property_set_int (OBJECT (&s->soc), smp_cpus,"num-harts" , &error_abort); +object_property_set_int (OBJECT (&s->soc),VIRT_TIMEBASE_FREQ , +"timebase-frequency" , &error_abort);object_property_set_bool (OBJECT (&s->soc), true,"realized" , &error_abort);(quoted diff) +0
-0
b/include/hw/riscv/riscv_hart.hReview
Quoted 12 lines
index
0671d88a44..2e82e85b2b100644include/hw/riscv/riscv_hart.h +1
-0@@ -33,6 +33,7 @@ typedef struct RISCVHartArrayState {/*< public >*/ uint32_t num_harts;char *cpu_type; +uint64_t time_freq;RISCVCPU *harts; } RISCVHartArrayState;(quoted diff) +0
-0
b/include/hw/riscv/sifive_clint.htarget/riscv/csr.c static int read_instreth(CPURISCVState *env, int
Quoted 82 lines
index
e2865be1d1..aaa2a58c6e100644include/hw/riscv/sifive_clint.h +0
-4@@ -47,8 +47,4 @@ enum {SIFIVE_TIME_BASE =0xBFF8 }; -enum { -SIFIVE_CLINT_TIMEBASE_FREQ =10000000 -}; -#endif include/hw/riscv/sifive_e.h +4
-0@@ -71,6 +71,10 @@ enum { SIFIVE_E_GPIO0_IRQ0 = 8 }; +enum { +SIFIVE_E_TIMEBASE_FREQ =10000000 , +}; +#define SIFIVE_E_PLIC_HART_CONFIG "M"#define SIFIVE_E_PLIC_NUM_SOURCES 127#define SIFIVE_E_PLIC_NUM_PRIORITIES 7include/hw/riscv/sifive_u.h +1
-0@@ -63,6 +63,7 @@ enum { };enum { +SIFIVE_U_TIMEBASE_FREQ =10000000 ,SIFIVE_U_CLOCK_FREQ =1000000000 ,SIFIVE_U_GEM_CLOCK_FREQ =125000000 };include/hw/riscv/spike.h +1
-0@@ -36,6 +36,7 @@ enum { };enum { +SPIKE_TIMEBASE_FREQ =10000000 ,SPIKE_CLOCK_FREQ =1000000000 };include/hw/riscv/virt.h +1
-0@@ -53,6 +53,7 @@ enum { };enum { +VIRT_TIMEBASE_FREQ =10000000 ,VIRT_CLOCK_FREQ =1000000000 };target/riscv/cpu.h +2
-0@@ -175,7 +175,9 @@ struct CPURISCVState {/* temporary htif regs */ uint64_t mfromhost;uint64_t mtohost; +uint64_t timecmp; +uint64_t time_freq;/* physical memory protection */ pmp_table_t pmp_state;target/riscv/csr.c +0
-0@@ -191,22 +191,31 @@ static int read_instreth(CPURISCVState *env, intcsrno,
target_ulong*val)target/riscv/csr.c static riscv_csr_operations
Quoted 34 lines
target/riscv/csr.c +11
-2}#endif /* TARGET_RISCV32 */ -#if defined(CONFIG_USER_ONLY )static int read_time (CPURISCVState *env,int csrno,target_ulong *val) { +#if !defined(CONFIG_USER_ONLY ) + *val =muldiv64 (qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ), + env->time_freq,NANOSECONDS_PER_SECOND ); +#else *val =cpu_get_host_ticks (); +#endif return 0 ; }#if defined(TARGET_RISCV32 )static int read_timeh (CPURISCVState *env,int csrno,target_ulong *val) { +#if !defined(CONFIG_USER_ONLY ) + *val =muldiv64 (qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ), + env->time_freq,NANOSECONDS_PER_SECOND ) >>32 ; +#else *val =cpu_get_host_ticks () >>32 ; +#endif return 0 ; }#endif -#else /* CONFIG_USER_ONLY */ +#if !defined(CONFIG_USER_ONLY )/* Machine constants */ @@ -856,14 +865,10 @@ static riscv_csr_operationscsr_ops[CSR_TABLE_SIZE] = {
target/riscv/csr.c static riscv_csr_operations · line 865
Quoted 1 line
target/riscv/csr.c +0
-0[CSR_INSTRETH] = { ctr, read_instreth},target/riscv/csr.c static riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
Quoted 6 lines
target/riscv/csr.c +0
-3#endif -/* User-level time CSRs are only available in linux-user -* In privileged mode, the monitor emulates these CSRs */ -#if defined(CONFIG_USER_ONLY ) [CSR_TIME] = { ctr, read_time},target/riscv/csr.c static riscv_csr_operations · line 870
Quoted 2 lines
target/riscv/csr.c +0
-0#if defined(TARGET_RISCV32 ) [CSR_TIMEH] = { ctr, read_timeh},target/riscv/csr.c static riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { · line 866
Quoted 2 lines
target/riscv/csr.c +0
-1#endif -#endif I would prefer to avoid making the timer CSRs availiable to S-mode and below,
as that doesn't match what implementations actually do.Quoted 2 lines
target/riscv/csr.c +0
-0#if !defined(CONFIG_USER_ONLY )/* Machine Timers and Counters */
I just did some testing on a HiFive Unleashed board and can confirm what you are saying. The low 5 bits of both mcounteren and scounteren are writable (if you try to write 0xFFFFFFFF to them, they'll take on the value 0x1F) but even with the TM bit set in both mcounteren and scounteren the rdtime instruction always generates an illegal instruction exception.
Reading through the relevant chapter of the spec, I still think that having mcounteren.TM be writable but making rdtime unconditionally trap is non-conformant. If other people feel strongly that rdtime should always require trapping into firmware then the natural change would be to simply hardwire mcounteren.TM to zero (the value in scounteren wouldn't matter in that case so it could be left writable). My own (biased) personal feeling is that this full implementation makes sense at least for the virt machine type because it represents a clear case where deviating from current hardware enables a performance boost, and would not break compatibility with any current software: both OpenSBI and BBL try to enable hardware handling of rdtime when the platform claims to support it.
Quoted 430 lines · Palmer Dabbelt
Quoted 3 lines · On Mon, 24 Jun 2019 16:03:20 PDT (-0700), [email protected] wrote:
Apparently my previous message didn't make it out onto the list (sorry about all these email glitches!). I've included the message again below. Hopefully either a patch like this one or something simpler that just
hard
Quoted 1 line
codes mcounteren.TM to zero (so QEMU is at least conformant) can be
merged
Quoted 11 lines
in time for 4.1.
Quoted 6 lines · Jonathan Behrens
I'm not sure that is accurate. Based on the discussion here
<https://forums.sifive.com/t/possible-bug-in-counteren-csrs/2123> the HiFive Unleashed actually does support reading the timer CSR from unprivileged modes (from that discussion it does so a little too well... but it should presumably be fixed in later iterations of the processor). And even if no real hardware supported this capability, it still mightmake
Quoted 1 line
Quoted 1 line
sense to provide it in QEMU as an optimization.
time and cycle are different registers: rdtime should trap, but rdcycle should
work. The hardware traps rdtime into machine mode and emulates it via a memory
mapped register. The bug in the FU540-C000 appears to be related to the counter enables, not the presence of the counters.Quoted 2 lines
Quoted 2 lines
On Fri, Jun 14, 2019 at 7:52 AM Palmer Dabbelt <[email protected]>
Quoted 4 lines · wrote:
Quoted 4 lines
Quoted 4 lines · On Tue, 28 May 2019 11:30:20 PDT (-0700), [email protected] wrote:
Quoted 1 line
Currently mcounteren.TM acts as though it is hardwired to zero, even
though QEMU allows it to be set. This change resolves the issue by
allowing
Quoted 1 line
Quoted 1 line
Quoted 1 line
reads to the time and timeh control registers when running in a
privileged
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
mode where such accesses are allowed.
Quoted 2 lines
The frequency of the time register is stored in the
time_freqfieldof
Quoted 1 line
Quoted 1 line
Quoted 1 line
each hart so that it is accessible during CSR reads, but must be the
same
Quoted 1 line
Quoted 1 line
Quoted 1 line
across all harts. Each board can initialize it to a custom value,
although
Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
all currently use a 10 MHz frequency.
Quoted 26 lines
Signed-off-by Jonathan Behrens <[email protected]>--- hw/riscv/riscv_hart.c | 4 ++++ hw/riscv/sifive_clint.c | 30 ++++++++++++++++++++++-------- hw/riscv/sifive_e.c | 2 ++ hw/riscv/sifive_u.c | 4 +++- hw/riscv/spike.c | 6 +++++- hw/riscv/virt.c | 4 +++- include/hw/riscv/riscv_hart.h | 1 + include/hw/riscv/sifive_clint.h | 4 ---- include/hw/riscv/sifive_e.h | 4 ++++ include/hw/riscv/sifive_u.h | 1 + include/hw/riscv/spike.h | 1 + include/hw/riscv/virt.h | 1 + target/riscv/cpu.h | 2 ++ target/riscv/csr.c | 17 +++++++++++------ 14 files changed, 60 insertions(+), 21 deletions(-)hw/riscv/riscv_hart.c +0
-0@@ -19,6 +19,7 @@*/ Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
hw/riscv/riscv_hart.c line 29
Quoted 8 lines
hw/riscv/riscv_hart.c +1
-0#include "qemu/osdep.h" +#include "qemu/timer.h" #include "qapi/error.h" #include "hw/sysbus.h" #include "target/riscv/cpu.h" @@ -27,6 +28,8 @@static Property riscv_harts_props[] = { DEFINE_PROP_UINT32("num-harts", RISCVHartArrayState, num_harts,1),
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
hw/riscv/riscv_hart.c line 31
Quoted 2 lines
hw/riscv/riscv_hart.c +1
-0DEFINE_PROP_STRING ("cpu-type" , RISCVHartArrayState, cpu_type), + DEFINE_PROP_UINT64("timebase-frequency", RISCVHartArrayState,
time_freq,hw/riscv/riscv_hart.c static void riscv_harts_realize(DeviceState *dev,
Quoted 5 lines
hw/riscv/riscv_hart.c +1
-0+ NANOSECONDS_PER_SECOND), DEFINE_PROP_END_OF_LIST(), };@@ -49,6 +52,7 @@ static void riscv_harts_realize(DeviceState *dev,Error **errp)
Quoted 23 lines
hw/riscv/riscv_hart.c +1
-0sizeof (RISCVCPU ), s->cpu_type, &error_abort, NULL); s->harts[n].env.mhartid = n; + s->harts[n].env.time_freq = s->time_freq;qemu_register_reset (riscv_harts_cpu_reset, &s->harts[n]);object_property_set_bool (OBJECT (&s->harts[n]), true,"realized" , &err);hw/riscv/sifive_clint.c +2
-2@@ -26,10 +26,10 @@#include "hw/riscv/sifive_clint.h" #include "qemu/timer.h" -static uint64_t cpu_riscv_read_rtc (void ) +static uint64_t cpu_riscv_read_rtc (CPURISCVState *env) {return muldiv64 (qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ), -SIFIVE_CLINT_TIMEBASE_FREQ ,NANOSECONDS_PER_SECOND ); + env->time_freq,NANOSECONDS_PER_SECOND ); }Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
hw/riscv/sifive_clint.c static void sifive_clint_write_timecmp(RISCVCPU
Quoted 2 lines
hw/riscv/sifive_clint.c +0
-0/* @@ -41,7 +41,7 @@ static void sifive_clint_write_timecmp(RISCVCPU*cpu,
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
uint64_tvalue)hw/riscv/sifive_clint.c static void sifive_clint_write_timecmp(RISCVCPU
Quoted 9 lines
hw/riscv/sifive_clint.c +1
-1uint64_t next;uint64_t diff; -uint64_t rtc_r =cpu_riscv_read_rtc (); +uint64_t rtc_r =cpu_riscv_read_rtc (&cpu->env); cpu->env.timecmp = value;if (cpu->env.timecmp <= rtc_r) {@@ -56,7 +56,7 @@ static void sifive_clint_write_timecmp(RISCVCPU*cpu,
Quoted 42 lines
Quoted 42 lines
Quoted 42 lines
uint64_tvalue)hw/riscv/sifive_clint.c static void sifive_clint_write_timecmp(RISCVCPU *cpu, uint64_t value)
Quoted 4 lines
hw/riscv/sifive_clint.c +0
-1diff = cpu->env.timecmp - rtc_r;/* back to ns (note args switched in muldiv64) */ next = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + - muldiv64(diff, NANOSECONDS_PER_SECOND,
SIFIVE_CLINT_TIMEBASE_FREQ);hw/riscv/sifive_clint.c static uint64_t sifive_clint_read(void *opaque,
Quoted 5 lines
hw/riscv/sifive_clint.c +1
-0+muldiv64 (diff,NANOSECONDS_PER_SECOND , cpu->env.time_freq);timer_mod (cpu->env.timer, next); }@@ -107,11 +107,25 @@ static uint64_t sifive_clint_read(void *opaque,hwaddr addr, unsigned size)
hw/riscv/sifive_clint.c static uint64_t sifive_clint_read(void *opaque, hwaddr addr, unsigned size)
Quoted 6 lines
hw/riscv/sifive_clint.c +1
-2return 0 ; } }else if (addr == clint->time_base) { -/* time_lo */ -return cpu_riscv_read_rtc () &0xFFFFFFFF ; +/* All harts must have the same time frequency, so just use hart 0 */
hw/riscv/sifive_clint.c static uint64_t sifive_clint_read(void *opaque, hwaddr addr, unsigned size)
Quoted 12 lines
hw/riscv/sifive_clint.c +9
-2+CPUState *cpu =qemu_get_cpu (0 ); +CPURISCVState *env = cpu ? cpu->env_ptr :NULL ; +if (!env) { +error_report ("clint: hart 0 not valid?!" ); + }else { +/* time_lo */ +return cpu_riscv_read_rtc (env) &0xFFFFFFFF ; + } }else if (addr == clint->time_base +4 ) { -/* time_hi */ -return (cpu_riscv_read_rtc () >>32 ) &0xFFFFFFFF ; +/* All harts must have the same time frequency, so just use hart 0 */
Quoted 10 lines
hw/riscv/sifive_clint.c +8
-0+CPUState *cpu =qemu_get_cpu (0 ); +CPURISCVState *env = cpu ? cpu->env_ptr :NULL ; +if (!env) { +error_report ("clint: hart 0 not valid?!" ); + }else { +/* time_hi */ +return (cpu_riscv_read_rtc (env) >>32 ) &0xFFFFFFFF ; + } }Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
hw/riscv/sifive_e.c static void riscv_sifive_e_soc_init(Object *obj) · line 149
Quoted 10 lines
hw/riscv/sifive_clint.c +0
-0error_report ("clint: invalid read: %08x" , (uint32_t )addr);hw/riscv/sifive_e.c +1
-0@@ -146,6 +146,8 @@ static void riscv_sifive_e_soc_init(Object *obj) &error_abort);object_property_set_int (OBJECT (&s->cpus), smp_cpus,"num-harts" , &error_abort); + object_property_set_int(OBJECT(&s->cpus),
SIFIVE_E_TIMEBASE_FREQ,Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
hw/riscv/sifive_u.c static void create_fdt(SiFiveUState *s, const
Quoted 9 lines
hw/riscv/sifive_e.c +1
-0+ "timebase-frequency", &error_abort);sysbus_init_child_obj (obj,"riscv.sifive.e.gpio0" , &s->gpio,sizeof (s->gpio),TYPE_SIFIVE_GPIO );hw/riscv/sifive_u.c +0
-0@@ -116,7 +116,7 @@ static void create_fdt(SiFiveUState *s, conststruct MemmapEntry *memmap,
Quoted 8 lines
hw/riscv/sifive_u.c +1
-1qemu_fdt_add_subnode (fdt,"/cpus" );qemu_fdt_setprop_cell (fdt,"/cpus" ,"timebase-frequency" , -SIFIVE_CLINT_TIMEBASE_FREQ ); +SIFIVE_U_TIMEBASE_FREQ );qemu_fdt_setprop_cell (fdt,"/cpus" ,"#size-cells" ,0x0 );qemu_fdt_setprop_cell (fdt,"/cpus" ,"#address-cells" ,0x1 );Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
hw/riscv/sifive_u.c static void riscv_sifive_u_soc_init(Object *obj) · line 332
Quoted 5 lines
hw/riscv/sifive_u.c +1
-0@@ -329,6 +329,8 @@ static void riscv_sifive_u_soc_init(Object *obj) &error_abort);object_property_set_int (OBJECT (&s->cpus), smp_cpus,"num-harts" , &error_abort); + object_property_set_int(OBJECT(&s->cpus),
SIFIVE_U_TIMEBASE_FREQ,Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
hw/riscv/spike.c static void create_fdt(SpikeState *s, const
Quoted 9 lines
hw/riscv/sifive_u.c +1
-0+ "timebase-frequency", &error_abort);sysbus_init_child_obj (obj,"gem" , &s->gem,sizeof (s->gem),TYPE_CADENCE_GEM );hw/riscv/spike.c +0
-0@@ -106,7 +106,7 @@ static void create_fdt(SpikeState *s, conststruct
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
MemmapEntry *memmap,
hw/riscv/spike.c static void spike_board_init(MachineState
Quoted 9 lines
hw/riscv/spike.c +1
-1qemu_fdt_add_subnode (fdt,"/cpus" );qemu_fdt_setprop_cell (fdt,"/cpus" ,"timebase-frequency" , -SIFIVE_CLINT_TIMEBASE_FREQ ); +SPIKE_TIMEBASE_FREQ );qemu_fdt_setprop_cell (fdt,"/cpus" ,"#size-cells" ,0x0 );qemu_fdt_setprop_cell (fdt,"/cpus" ,"#address-cells" ,0x1 );@@ -180,6 +180,8 @@ static void spike_board_init(MachineState*machine)
Quoted 64 lines
Quoted 64 lines
Quoted 64 lines
hw/riscv/spike.c static void spike_v1_10_0_board_init(MachineState
Quoted 9 lines
hw/riscv/spike.c +2
-0&error_abort);object_property_set_int (OBJECT (&s->soc), smp_cpus,"num-harts" , &error_abort); +object_property_set_int (OBJECT (&s->soc),SPIKE_TIMEBASE_FREQ , +"timebase-frequency" , &error_abort);object_property_set_bool (OBJECT (&s->soc), true,"realized" , &error_abort);@@ -268,6 +270,8 @@ static void spike_v1_10_0_board_init(MachineState*machine)
hw/riscv/virt.c static void *create_fdt(RISCVVirtState *s, const
Quoted 13 lines
hw/riscv/spike.c +2
-0&error_abort);object_property_set_int (OBJECT (&s->soc), smp_cpus,"num-harts" , &error_abort); +object_property_set_int (OBJECT (&s->soc),SPIKE_TIMEBASE_FREQ , +"timebase-frequency" , &error_abort);object_property_set_bool (OBJECT (&s->soc), true,"realized" , &error_abort);hw/riscv/virt.c +0
-0@@ -185,7 +185,7 @@ static void *create_fdt(RISCVVirtState *s, conststruct MemmapEntry *memmap,
hw/riscv/virt.c static void riscv_virt_board_init(MachineState
Quoted 9 lines
hw/riscv/virt.c +1
-1qemu_fdt_add_subnode (fdt,"/cpus" );qemu_fdt_setprop_cell (fdt,"/cpus" ,"timebase-frequency" , -SIFIVE_CLINT_TIMEBASE_FREQ ); +VIRT_TIMEBASE_FREQ );qemu_fdt_setprop_cell (fdt,"/cpus" ,"#size-cells" ,0x0 );qemu_fdt_setprop_cell (fdt,"/cpus" ,"#address-cells" ,0x1 );@@ -403,6 +403,8 @@ static void riscv_virt_board_init(MachineState*machine)
Review
Quoted 9 lines
hw/riscv/virt.c +2
-0&error_abort);object_property_set_int (OBJECT (&s->soc), smp_cpus,"num-harts" , &error_abort); +object_property_set_int (OBJECT (&s->soc),VIRT_TIMEBASE_FREQ , +"timebase-frequency" , &error_abort);object_property_set_bool (OBJECT (&s->soc), true,"realized" , &error_abort);(quoted diff) +0
-0
b/include/hw/riscv/riscv_hart.hReview
Quoted 12 lines
index
0671d88a44..2e82e85b2b100644include/hw/riscv/riscv_hart.h +1
-0@@ -33,6 +33,7 @@ typedef struct RISCVHartArrayState {/*< public >*/ uint32_t num_harts;char *cpu_type; +uint64_t time_freq;RISCVCPU *harts; } RISCVHartArrayState;(quoted diff) +0
-0
b/include/hw/riscv/sifive_clint.hQuoted 7 lines
index
e2865be1d1..aaa2a58c6e100644include/hw/riscv/sifive_clint.h +0
-0@@ -47,8 +47,4 @@ enum { SIFIVE_TIME_BASE = 0xBFF8 };Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Review
Quoted 6 lines
include/hw/riscv/sifive_clint.h +0
-4-enum { -SIFIVE_CLINT_TIMEBASE_FREQ =10000000 -}; -#endif (quoted diff) +0
-0
b/include/hw/riscv/sifive_e.hQuoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
index
3b14eb7462..95bd33d872100644include/hw/riscv/sifive_e.h +0
-0@@ -71,6 +71,10 @@ enum { SIFIVE_E_GPIO0_IRQ0 = 8 };Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Review
Quoted 8 lines
include/hw/riscv/sifive_e.h +4
-0+enum { +SIFIVE_E_TIMEBASE_FREQ =10000000 , +}; +#define SIFIVE_E_PLIC_HART_CONFIG "M"#define SIFIVE_E_PLIC_NUM_SOURCES 127#define SIFIVE_E_PLIC_NUM_PRIORITIES 7(quoted diff) +0
-0
b/include/hw/riscv/sifive_u.hQuoted 47 lines
Quoted 47 lines
Quoted 47 lines
Quoted 47 lines
index
892f0eee21..85ee1eca78100644include/hw/riscv/sifive_u.h +1
-0@@ -63,6 +63,7 @@ enum { };enum { +SIFIVE_U_TIMEBASE_FREQ =10000000 ,SIFIVE_U_CLOCK_FREQ =1000000000 ,SIFIVE_U_GEM_CLOCK_FREQ =125000000 };include/hw/riscv/spike.h +1
-0@@ -36,6 +36,7 @@ enum { };enum { +SPIKE_TIMEBASE_FREQ =10000000 ,SPIKE_CLOCK_FREQ =1000000000 };include/hw/riscv/virt.h +1
-0@@ -53,6 +53,7 @@ enum { };enum { +VIRT_TIMEBASE_FREQ =10000000 ,VIRT_CLOCK_FREQ =1000000000 };target/riscv/cpu.h +2
-0@@ -175,7 +175,9 @@ struct CPURISCVState {/* temporary htif regs */ uint64_t mfromhost;uint64_t mtohost; +uint64_t timecmp; +uint64_t time_freq;Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
target/riscv/csr.c static int read_instreth(CPURISCVState *env,
Quoted 7 lines
target/riscv/cpu.h +0
-0/* physical memory protection */ pmp_table_t pmp_state;target/riscv/csr.c +0
-0@@ -191,22 +191,31 @@ static int read_instreth(CPURISCVState *env,int
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
csrno,
target_ulong*val)Quoted 5 lines
target/riscv/csr.c +0
-1}#endif /* TARGET_RISCV32 */ -#if defined(CONFIG_USER_ONLY ) static int read_time(CPURISCVState *env, int csrno, target_ulong*val)
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
target/riscv/csr.c +5
-0{ +#if !defined(CONFIG_USER_ONLY ) + *val =muldiv64 (qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ), + env->time_freq,NANOSECONDS_PER_SECOND ); +#else *val =cpu_get_host_ticks (); +#endif return 0 ; }Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
target/riscv/csr.c static int read_instreth(CPURISCVState *env, · line 206
Quoted 2 lines
target/riscv/csr.c +0
-0#if defined(TARGET_RISCV32 ) static int read_timeh(CPURISCVState *env, int csrno, target_ulong*val)
Quoted 41 lines
Quoted 41 lines
Quoted 40 lines
target/riscv/csr.c static riscv_csr_operations
Quoted 17 lines
target/riscv/csr.c +6
-1{ +#if !defined(CONFIG_USER_ONLY ) + *val =muldiv64 (qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ), + env->time_freq,NANOSECONDS_PER_SECOND ) >>32 ; +#else *val =cpu_get_host_ticks () >>32 ; +#endif return 0 ; }#endif -#else /* CONFIG_USER_ONLY */ +#if !defined(CONFIG_USER_ONLY )/* Machine constants */ @@ -856,14 +865,10 @@ static riscv_csr_operationscsr_ops[CSR_TABLE_SIZE] = {
target/riscv/csr.c static riscv_csr_operations · line 865
Quoted 1 line
target/riscv/csr.c +0
-0[CSR_INSTRETH] = { ctr, read_instreth},target/riscv/csr.c static riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = {
Quoted 6 lines
target/riscv/csr.c +0
-3#endif -/* User-level time CSRs are only available in linux-user -* In privileged mode, the monitor emulates these CSRs */ -#if defined(CONFIG_USER_ONLY ) [CSR_TIME] = { ctr, read_time},target/riscv/csr.c static riscv_csr_operations · line 870
Quoted 2 lines
target/riscv/csr.c +0
-0#if defined(TARGET_RISCV32 ) [CSR_TIMEH] = { ctr, read_timeh},target/riscv/csr.c static riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { · line 866
Quoted 2 lines
target/riscv/csr.c +0
-1#endif -#endif I would prefer to avoid making the timer CSRs availiable to S-mode and below,
as that doesn't match what implementations actually do.Quoted 2 lines
target/riscv/csr.c +0
-0#if !defined(CONFIG_USER_ONLY )/* Machine Timers and Counters */
Quoted 7 lines · Jonathan Behrens
I just did some testing on a HiFive Unleashed board and can confirm what you are saying. The low 5 bits of both mcounteren and scounteren are writable (if you try to write
0xFFFFFFFFto them, they'll take on the value0x1F) but even with the TM bit set in both mcounteren and scounteren the rdtime instruction always generates an illegal instruction exception.
Then I would think the FU540 is not spec complaint :)
Quoted 3 lines
Reading through the relevant chapter of the spec, I still think that having mcounteren.TM be writable but making rdtime unconditionally trap is non-conformant. If other people feel strongly that rdtime should always
Agree. To test hardware (FU540) compatibility in QEMU, maybe we can add a cpu property to allow hard-wiring mcounteren.TM to zero?
Quoted 9 lines
require trapping into firmware then the natural change would be to simply hardwire mcounteren.TM to zero (the value in scounteren wouldn't matter in that case so it could be left writable). My own (biased) personal feeling is that this full implementation makes sense at least for the
virtmachine type because it represents a clear case where deviating from current hardware enables a performance boost, and would not break compatibility with any current software: both OpenSBI and BBL try to enable hardware handling of rdtime when the platform claims to support it.
Regards,
Bin
Quoted 10 lines · On Tue, 25 Jun 2019 23:58:34 PDT (-0700), [email protected] wrote:
Quoted 7 lines · Jonathan Behrens
I just did some testing on a HiFive Unleashed board and can confirm what you are saying. The low 5 bits of both mcounteren and scounteren are writable (if you try to write
0xFFFFFFFFto them, they'll take on the value0x1F) but even with the TM bit set in both mcounteren and scounteren the rdtime instruction always generates an illegal instruction exception.Then I would think the FU540 is not spec complaint :)
Ya, it's an errata. There's a handful of them :)
Quoted 6 lines
Quoted 3 lines
Reading through the relevant chapter of the spec, I still think that having mcounteren.TM be writable but making rdtime unconditionally trap is non-conformant. If other people feel strongly that rdtime should always
Agree. To test hardware (FU540) compatibility in QEMU, maybe we can add a cpu property to allow hard-wiring mcounteren.TM to zero?
In theory we should have properties to control the behavior of all WARL fields, but it's a lot of work. I'd be happy to take a patch for any of them.
Quoted 12 lines
Quoted 9 lines
require trapping into firmware then the natural change would be to simply hardwire mcounteren.TM to zero (the value in scounteren wouldn't matter in that case so it could be left writable). My own (biased) personal feeling is that this full implementation makes sense at least for the
virtmachine type because it represents a clear case where deviating from current hardware enables a performance boost, and would not break compatibility with any current software: both OpenSBI and BBL try to enable hardware handling of rdtime when the platform claims to support it.Regards,
Bin
Quoted 19 lines · Palmer Dabbelt
Quoted 10 lines · On Tue, 25 Jun 2019 23:58:34 PDT (-0700), [email protected] wrote:
Quoted 7 lines · Jonathan Behrens
I just did some testing on a HiFive Unleashed board and can confirm what you are saying. The low 5 bits of both mcounteren and scounteren are writable (if you try to write
0xFFFFFFFFto them, they'll take on the value0x1F) but even with the TM bit set in both mcounteren and scounteren the rdtime instruction always generates an illegal instruction exception.Then I would think the FU540 is not spec complaint :)
Ya, it's an errata. There's a handful of them :)
Quoted 4 lines
Quoted 3 lines
Reading through the relevant chapter of the spec, I still think that having mcounteren.TM be writable but making rdtime unconditionally trap is non-conformant. If other people feel strongly that rdtime should always
Quoted 5 lines
Quoted 2 lines
Agree. To test hardware (FU540) compatibility in QEMU, maybe we can add a cpu property to allow hard-wiring mcounteren.TM to zero?
In theory we should have properties to control the behavior of all WARL fields, but it's a lot of work. I'd be happy to take a patch for any of them.
Hmmm... We should avoid taking patches that don't adhere to the spec just to match some hardware. In the case that core/popular software doesn't work it probably makes sense, but in general it's probably not the best move.
Alistair
Quoted 14 lines
Quoted 13 lines
Quoted 10 lines
require trapping into firmware then the natural change would be to simply hardwire mcounteren.TM to zero (the value in scounteren wouldn't matter in that case so it could be left writable). My own (biased) personal feeling is that this full implementation makes sense at least for the
virtmachine type because it represents a clear case where deviating from current hardware enables a performance boost, and would not break compatibility with any current software: both OpenSBI and BBL try to enable hardware handling of rdtime when the platform claims to support it.Regards,
Bin
Quoted 23 lines · On Thu, 27 Jun 2019 12:56:57 PDT (-0700), [email protected] wrote:
Quoted 22 lines · Palmer Dabbelt
Quoted 10 lines · On Tue, 25 Jun 2019 23:58:34 PDT (-0700), [email protected] wrote:
Quoted 7 lines · Jonathan Behrens
I just did some testing on a HiFive Unleashed board and can confirm what you are saying. The low 5 bits of both mcounteren and scounteren are writable (if you try to write
0xFFFFFFFFto them, they'll take on the value0x1F) but even with the TM bit set in both mcounteren and scounteren the rdtime instruction always generates an illegal instruction exception.Then I would think the FU540 is not spec complaint :)
Ya, it's an errata. There's a handful of them :)
Quoted 6 lines
Quoted 3 lines
Reading through the relevant chapter of the spec, I still think that having mcounteren.TM be writable but making rdtime unconditionally trap is non-conformant. If other people feel strongly that rdtime should always
Agree. To test hardware (FU540) compatibility in QEMU, maybe we can add a cpu property to allow hard-wiring mcounteren.TM to zero?
Quoted 7 lines
Quoted 2 lines
In theory we should have properties to control the behavior of all WARL fields, but it's a lot of work. I'd be happy to take a patch for any of them.
Hmmm... We should avoid taking patches that don't adhere to the spec just to match some hardware. In the case that core/popular software doesn't work it probably makes sense, but in general it's probably not the best move.
WARL is Write Any Read Legal. Essentially it means that the hardware gets to decide what sort of behavior that field has, and is the mechanism for optional features in the ISA. In this case it'd be entirely within spec, specifically:
Registers mcounteren and scounteren are WARL registers that must be implemented if U-mode and S-mode are implemented. Any of the bits may contain a hardwired value of zero, indicating reads to the corresponding counter will cause an illegal instruction exception when executing in a less-privileged mode.
Taking a patch that matches the out-of-spec FU540 behavior doesn't make any sense, I don't want to implement errata in QEMU :)
Quoted 17 lines
Alistair
Quoted 14 lines
Quoted 13 lines
Quoted 10 lines
require trapping into firmware then the natural change would be to simply hardwire mcounteren.TM to zero (the value in scounteren wouldn't matter in that case so it could be left writable). My own (biased) personal feeling is that this full implementation makes sense at least for the
virtmachine type because it represents a clear case where deviating from current hardware enables a performance boost, and would not break compatibility with any current software: both OpenSBI and BBL try to enable hardware handling of rdtime when the platform claims to support it.Regards,
Bin
Quoted 37 lines · Palmer Dabbelt
Quoted 23 lines · On Thu, 27 Jun 2019 12:56:57 PDT (-0700), [email protected] wrote:
Quoted 22 lines · Palmer Dabbelt
Quoted 10 lines · On Tue, 25 Jun 2019 23:58:34 PDT (-0700), [email protected] wrote:
Quoted 7 lines · Jonathan Behrens
I just did some testing on a HiFive Unleashed board and can confirm what you are saying. The low 5 bits of both mcounteren and scounteren are writable (if you try to write
0xFFFFFFFFto them, they'll take on the value0x1F) but even with the TM bit set in both mcounteren and scounteren the rdtime instruction always generates an illegal instruction exception.Then I would think the FU540 is not spec complaint :)
Ya, it's an errata. There's a handful of them :)
Quoted 6 lines
Quoted 3 lines
Reading through the relevant chapter of the spec, I still think that having mcounteren.TM be writable but making rdtime unconditionally trap is non-conformant. If other people feel strongly that rdtime should always
Agree. To test hardware (FU540) compatibility in QEMU, maybe we can add a cpu property to allow hard-wiring mcounteren.TM to zero?
Quoted 7 lines
Quoted 2 lines
In theory we should have properties to control the behavior of all WARL fields, but it's a lot of work. I'd be happy to take a patch for any of them.
Hmmm... We should avoid taking patches that don't adhere to the spec just to match some hardware. In the case that core/popular software doesn't work it probably makes sense, but in general it's probably not the best move.
WARL is Write Any Read Legal. Essentially it means that the hardware gets to decide what sort of behavior that field has, and is the mechanism for optional features in the ISA. In this case it'd be entirely within spec, specifically:
Quoted 8 lines
Registers mcounteren and scounteren are WARL registers that must be implemented if U-mode and S-mode are implemented. Any of the bits may contain a hardwired value of zero, indicating reads to the corresponding counter will cause an illegal instruction exception when executing in a less-privileged mode.
Taking a patch that matches the out-of-spec FU540 behavior doesn't make any sense, I don't want to implement errata in QEMU :)
Ah, I misread your email. That is fine then :)
Alistair
Quoted 18 lines
Quoted 18 lines
Alistair
Quoted 14 lines
Quoted 13 lines
Quoted 10 lines
require trapping into firmware then the natural change would be to simply hardwire mcounteren.TM to zero (the value in scounteren wouldn't matter in that case so it could be left writable). My own (biased) personal feeling is that this full implementation makes sense at least for the
virtmachine type because it represents a clear case where deviating from current hardware enables a performance boost, and would not break compatibility with any current software: both OpenSBI and BBL try to enable hardware handling of rdtime when the platform claims to support it.Regards,
Bin
Quoted 1 line
Taking a patch that matches the out-of-spec FU540 behavior doesn't make
any
Quoted 1 line
sense, I don't want to implement errata in QEMU :)
QEMU actually currently matches the out of spec behavior of the FU540, I just sent out a patch to fix this.
Jonathan
Quoted 92 lines · Alistair Francis
Quoted 3 lines · Palmer Dabbelt
Quoted 1 line · On Thu, 27 Jun 2019 12:56:57 PDT (-0700), [email protected] wrote:
On Wed, Jun 26, 2019 at 1:25 AM Palmer Dabbelt <[email protected]>
Quoted 3 lines · wrote:
Quoted 3 lines
Quoted 3 lines · On Tue, 25 Jun 2019 23:58:34 PDT (-0700), [email protected] wrote:
Quoted 1 line
On Wed, Jun 26, 2019 at 4:23 AM Jonathan Behrens <
Quoted 2 lines · [email protected]> wrote:
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
I just did some testing on a HiFive Unleashed board and can
confirm what
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
you are saying. The low 5 bits of both mcounteren and scounteren
are
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
writable (if you try to write
0xFFFFFFFFto them, they'll take onthe value
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
0x1F) but even with the TM bit set in both mcounteren andscounteren the
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
rdtime instruction always generates an illegal instruction
exception.
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Then I would think the FU540 is not spec complaint :)
Ya, it's an errata. There's a handful of them :)
Quoted 1 line
Quoted 1 line
Reading through the relevant chapter of the spec, I still think
that having
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
mcounteren.TM be writable but making rdtime unconditionally trap is non-conformant. If other people feel strongly that rdtime should
always
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
Agree. To test hardware (FU540) compatibility in QEMU, maybe we can add a cpu property to allow hard-wiring mcounteren.TM to zero?
In theory we should have properties to control the behavior of all
WARL fields,
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
but it's a lot of work. I'd be happy to take a patch for any of them.
Hmmm... We should avoid taking patches that don't adhere to the spec just to match some hardware. In the case that core/popular software doesn't work it probably makes sense, but in general it's probably not the best move.
WARL is Write Any Read Legal. Essentially it means that the hardware
gets to
Quoted 1 line
decide what sort of behavior that field has, and is the mechanism for
optional
Quoted 1 line
features in the ISA. In this case it'd be entirely within spec,
specifically:
Quoted 4 lines
Registers mcounteren and scounteren are WARL registers that must be implemented if U-mode and S-mode are implemented. Any of the bits may contain a hardwired value of zero, indicating reads to the
corresponding
Quoted 1 line
counter will cause an illegal instruction exception when executing
in a
Quoted 3 lines
less-privileged mode.
Taking a patch that matches the out-of-spec FU540 behavior doesn't make
any
Quoted 1 line
sense, I don't want to implement errata in QEMU :)
Ah, I misread your email. That is fine then :)
Alistair
Quoted 6 lines
Quoted 6 lines
Alistair
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
require trapping into firmware then the natural change would be to
simply
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
hardwire mcounteren.TM to zero (the value in scounteren wouldn't
matter in
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
that case so it could be left writable). My own (biased) personal
feeling
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
is that this full implementation makes sense at least for the
virtQuoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
machine type because it represents a clear case where deviating
from
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
current hardware enables a performance boost, and would not break compatibility with any current software: both OpenSBI and BBL try
to enable
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
hardware handling of rdtime when the platform claims to support it.
Regards,
Bin
Hi Palmer,
Quoted 19 lines · Palmer Dabbelt
Quoted 6 lines · On Mon, 24 Jun 2019 16:03:20 PDT (-0700), [email protected] wrote:
Apparently my previous message didn't make it out onto the list (sorry about all these email glitches!). I've included the message again below. Hopefully either a patch like this one or something simpler that just hard codes mcounteren.TM to zero (so QEMU is at least conformant) can be merged in time for 4.1.
Quoted 10 lines
Quoted 7 lines · Jonathan Behrens
I'm not sure that is accurate. Based on the discussion here
<https://forums.sifive.com/t/possible-bug-in-counteren-csrs/2123> the HiFive Unleashed actually does support reading the timer CSR from unprivileged modes (from that discussion it does so a little too well... but it should presumably be fixed in later iterations of the processor). And even if no real hardware supported this capability, it still might make sense to provide it in QEMU as an optimization.
Quoted 4 lines
time and cycle are different registers: rdtime should trap, but rdcycle should work. The hardware traps rdtime into machine mode and emulates it via a memory mapped register. The bug in the FU540-C000 appears to be related to the counter enables, not the presence of the counters.
I don't think rdtime is required to mandatorily trap.
Per privileged spec v1.10, chapter 3.1.17 Counter-Enable Registers ([m|h|s]counteren), it says:
"When the CY, TM, IR, or HPMn bit in the mcounteren register is clear, attempts to read the cycle, time, instret, or hpmcountern register while executing in S-mode or U-mode will cause an illegal instruction exception."
In the same chapter, it also says:
"Implementations can convert reads of the time CSR into loads to the memory-mapped mtime register, or hard-wire the TM bits in mxcounteren to 0 and emulate this functionality in M-mode software."
So per my understanding when mcounteren.TM is set, rdtime should NOT trap.
Regards,
Bin
Quoted 37 lines · On Tue, 25 Jun 2019 23:54:06 PDT (-0700), [email protected] wrote:
Hi Palmer,
Quoted 19 lines · Palmer Dabbelt
Quoted 6 lines · On Mon, 24 Jun 2019 16:03:20 PDT (-0700), [email protected] wrote:
Apparently my previous message didn't make it out onto the list (sorry about all these email glitches!). I've included the message again below. Hopefully either a patch like this one or something simpler that just hard codes mcounteren.TM to zero (so QEMU is at least conformant) can be merged in time for 4.1.
Quoted 10 lines
Quoted 7 lines · Jonathan Behrens
I'm not sure that is accurate. Based on the discussion here
<https://forums.sifive.com/t/possible-bug-in-counteren-csrs/2123> the HiFive Unleashed actually does support reading the timer CSR from unprivileged modes (from that discussion it does so a little too well... but it should presumably be fixed in later iterations of the processor). And even if no real hardware supported this capability, it still might make sense to provide it in QEMU as an optimization.Quoted 4 lines
time and cycle are different registers: rdtime should trap, but rdcycle should work. The hardware traps rdtime into machine mode and emulates it via a memory mapped register. The bug in the FU540-C000 appears to be related to the counter enables, not the presence of the counters.
I don't think rdtime is required to mandatorily trap.
Per privileged spec v1.10, chapter 3.1.17 Counter-Enable Registers ([m|h|s]counteren), it says:
"When the CY, TM, IR, or HPMn bit in the mcounteren register is clear, attempts to read the cycle, time, instret, or hpmcountern register while executing in S-mode or U-mode will cause an illegal instruction exception."
Quoted 7 lines
In the same chapter, it also says:
"Implementations can convert reads of the time CSR into loads to the memory-mapped mtime register, or hard-wire the TM bits in mxcounteren to 0 and emulate this functionality in M-mode software."
So per my understanding when mcounteren.TM is set, rdtime should NOT trap.
Implementations are allowed to implement the instruction, but all the exsting implementations trap to M-mode.