Hi,
On Fri, Feb 10, 2017 at 07:07:21AM +0000, Alex Bradbury wrote:
> Hi Allen, I think we're talking along similar lines. The existing ISA
> spec makes it clear misaligned accesses can be handled with a software
> routine. As you say, an M-mode profile could indicate whether this is
> the case (and so you can expect it to be slow). I'm concerned about
> the idea of an M-mode profile that makes misaligned accesses illegal
> (i.e. where software can't expect such a routine to exist), as this is
> either introducing a backwards-incompatible change to the spec or else
> a new base ISA - pretty huge steps that should be strongly motivated
> IMHO.
IMO the current spec only says that misaligned accesses is illegal in user
mode, since it describes the "User-Level ISA". Really nothing is said in
the 2.1 spec Vol. 1 about other modes than U mode. Thus, making misaligned
accesses illegal for e.g. M-mode does not introduce a backwards-incompatible
change.
But by the same argument it would be possible do disable the addi
instruction for M-mode without introducing a backwards-incompatible
change. Obviously that would be a very bad idea.
So I think it is a mistake that the spec does not mention other modes and I
think several paragraphs should be added to the spec:
- Somewhere in the introduction it should mention that other modes than
user mode exists, and that all parts of the spec apply to all modes
unless mentioned otherwise.
- In the "Timers and Counters" section it should be mentioned that this
are U-mode CSRs that should only be used in code running in U-mode,
that there are other timer/counter CSRs for other modes, and M-mode only
machines don't implement this U-mode CSRs.
- In "Load and Store Instructions" it should be clarified if misaligned
memory access is something that is supported in other modes than U-mode.
- A comment should be added to "Calling Convention" clarifying that even
though the hard-float calling convention requires RV32G/RV64G, that
does not imply that the hardware must support this ISA natively. Thus
there is for example utility in building a binary for RV32IMF when this
is what your CPU actually can do, even when the OS running on the core
provides a full RV32G user land.
I disagree with Alex and do *not* think the ISA spec should require support
for misaligned memory access in other modes than U-mode. Here is why:
The spec gives two reasons why misaligned memory access is supported by the
user-land ISA: porting legacy code, and packed-SIMD performance.
Let me address packed-SIMD performance first: Right now there is no
packed-SIMD extension. When there is one, it would be really stupid to
generate packed-SIMD code with misaligned memory access and then the
misaligned memory accesses are handled by ISRs. The code would very likely
be significantly slower than without packed-SIMD. Therefore it would
probably be a good idea to make fast misaligned memory access a requirement
for the packed-SIMD extension. In this case it would be reasonable to also
require it to work in all modes, not just U-mode.
Regarding porting legacy code: Is this really such a big issue for code
that is neither going to run in U-mode nor within a large OS kernel? (An OS
kernel is of course free to implement a trap handler for misaligned mem
access and support it as an OS-specific extensions for its drivers.
However, I would suggest that most OS developers would prefer performence
over convenience in this regard, i.e. opt to not add a kernel-mode trap
handler to implement misaligned mem access from within the kernel code.)
Clearly, the legacy code the spec is talking about is programs. Practically
all existing cross-platform kernel code (drivers, network stacks, etc.)
probably already avoids misaligned memory access because that is something
that's not available cross-platform.
And what about M-mode only microcontrollers programmed on the bare-metal
level without any OS? I think it would be ridiculous to have a situation
where the system I'm writing code for is a RISC-V if and only if I
implement a trap handler for misaligned memory access within my own code.
That's why I think misaligned memory access should only be required to work
in U-mode. It is something that the OS code provides for userland, but not
something that necessarily the OS has to provide for itself in order to
make the environment it runs in RISC-V compliant. And when there is no OS
(such in the case of M-mode only systems), then there is no guarantee that
misaligned memory access is supported.
regards,
- clifford