[PATCH 1/1] lib: sbi: illegal CSR 0x306 access in hpm_allowed()

Heinrich Schuchardt xypron.glpk at gmx.de
Fri Mar 19 09:22:29 GMT 2021


On 19.03.21 09:29, Xiang W wrote:
> 在 2021-03-19五的 07:19 +0000,Heinrich Schuchardt写道:
>> The trap handler sbi_emulate_csr_read() invokes hpm_allowed() which
>> reads
>> CSR 0x306. The K210 does not support CSR 0x306. While trying to
>> handle
>> a trap occurring in S-mode code this creates an additional trap in M-
>> mode.
>> This results in failure to redirect to S-mode and the system hanging
>> in
>> sbi_hart_hang().
>>
>> In hart_detect_features() we have already determined if CSR 0x306 is
>> available and stored that information in the scratch area. We can use
>> this
>> information to decide if CSR 0x306 shall be accessed in hpm_allowed()
>> and
>> thus avoid the M-mode trap.
>>
>> Signed-off-by: Heinrich Schuchardt <xypron.glpk at gmx.de>
>> ---
>>  lib/sbi/sbi_emulate_csr.c | 4 ++++
>>  1 file changed, 4 insertions(+)
>>
>> diff --git a/lib/sbi/sbi_emulate_csr.c b/lib/sbi/sbi_emulate_csr.c
>> index bee7761..14c8875 100644
>> --- a/lib/sbi/sbi_emulate_csr.c
>> +++ b/lib/sbi/sbi_emulate_csr.c
>> @@ -21,6 +21,10 @@
>>  static bool hpm_allowed(int hpm_num, ulong prev_mode, bool virt)
>>  {
>>  	ulong cen = -1UL;
>> +	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
>> +
>> +	if (!sbi_hart_has_feature(scratch, SBI_HART_HAS_MCOUNTEREN))
>> +		return FALSE;
> It may be inappropriate to return FALSE directly. K210 does not have
> cycle, time, instret registers?

CSR_MCOUNTEREN controls access to cycle, time or instreth in S-mode. The
SBI specification does not require that the registers are enabled.
Assuming that the registers are not enabled is the only sane assumption.

Best regards

Heinrich

>>
>>  	if (prev_mode <= PRV_S) {
>>  		cen &= csr_read(CSR_MCOUNTEREN);
>> --
>> 2.30.2
>>
>>
>




More information about the opensbi mailing list