Nuclei-Software / nuclei-sdk

Nuclei RISC-V Software Development Kit
https://doc.nucleisys.com/nuclei_sdk
Apache License 2.0
117 stars 50 forks source link

Error in __set_exc_entry #31

Closed DagAgren closed 2 years ago

DagAgren commented 2 years ago

The comments for __set_exc_entry says that address should be aligned to 4 bytes, but the code itself forces alignment to 64 bytes:

https://github.com/Nuclei-Software/nuclei-sdk/blob/master/NMSIS/Core/Include/core_feature_eclic.h#L757

This looks like a mistake copied from the earlier n200_func.c file, which also incorrectly aligned the address to 64 bytes. The address should be masked by ~3, not ~0x3f.

This code probably works by accident since a lot of startup code seems to place the interrupt table at the start of Flash memory, so it is accidentally aligned.

EDIT: It seems that the address written to mtvec does have to be aligned to 64 bytes, or else the hardware refuses to work. So the actual bug here is that this requirement is not correctly documented for this function.

https://github.com/Nuclei-Software/nuclei-sdk/blob/master/NMSIS/Core/Include/core_feature_eclic.h#L751

This comment says the address has to be aligned to 4 bytes, but it should be aligned to 64 bytes.

DagAgren commented 2 years ago

This bug is also present at:

https://github.com/Nuclei-Software/nuclei-sdk/blob/master/NMSIS/Core/Include/core_feature_eclic.h#L794

fanghuaqi commented 2 years ago

Thanks for your reporting, I will take it into bug fix list.

fanghuaqi commented 2 years ago

Fixed the comment