News for hackers. Stuff that matters.
Re:RVVM – The RISC-V Virtual Machine by ognyankulev on Friday March 03, 2023 @08:59AM (#35009712)
Instruction interpreter is nicely human-readable: https://github.com/LekKit/RVVM/blob/staging/src/cpu/riscv_i....
Re:RVVM – The RISC-V Virtual Machine by bippingchip on Friday March 03, 2023 @09:04AM (#35009766)
The RISC-V isa is specifically designed to be nice and regular and easy to decode, and work with, which definitely shows here (also in the RTL code if you look at some of the well designed RISC-V cores)

Of course, bot x86 and Arm started like that as well - but after 20+ years of evolution, they have to drag along a lot of history. (and one never really takes things away from an ISA, you only add new features, and at best deprecate old ones).

That said, with the extensibility of the ISA in mind as a design principle, I do have good hopes, RISC-V will stand the test of time reasonable well in that sense...

Re:RVVM – The RISC-V Virtual Machine by klelatti on Friday March 03, 2023 @10:47AM (#35011112)
The ARM64 ISA does not have 20+ years of evolution as it was announced in 2011. It’s essentially a brand new ISA and has cast off almost all of the legacy of previous Arm ISAs.
Re:RVVM – The RISC-V Virtual Machine by firstlink on Saturday March 04, 2023 @06:03PM (#35025398)
This seems right at first; however, about half of the armv8 spec is taken up by a copy of the armv7 spec and info about interop between the two ISAs. So armv8 isa is considerably constrained not only by the interop requirements, whereby an armv8 OS/hypervisor must be able to control the environment of an armv7 process/OS, but also by the need for implementing both ISAs without excessive duplication of silicon. For example, an actual v8+v7 implementation must surely have a single pipeline supporting both ISAs.
Re:RVVM – The RISC-V Virtual Machine by klelatti on Saturday March 04, 2023 @06:35PM (#35025586)
I think the key test is whether significant features that were seen as problematic in earlier versions have indeed survived. AFAIK the large majority were fixed.

Patterson and Henessy comment as to how different (better) ARM64 is when compared to previous versions!

Re:RVVM – The RISC-V Virtual Machine by brucehoult on Sunday March 05, 2023 @05:30PM (#35034854)
It seems notable to me that Aarch64 is the only general purpose "clean sheet" ISA designed after 1990 (after POWER) that has condition codes. This seems like a prime example of something constrained by 32 bit ARM compatibility -- both porting software, and in the shared pipeline in CPUs that implement both.

On the gripping hand, there are a number of cores that implement ONLY the 64 bit ISA, starting from ThunderX, to Apple's M1/M2, to the latest ARM cores found in for example the Snapdragon 8 Gen 1/2 phone SoCs -- from memory only one of the three core types in those SoCs can run 32 bit code. ARM has said all their future ARMv9 cores will not have 32 bit compatibility.

Re:RVVM – The RISC-V Virtual Machine by KMag on Friday March 03, 2023 @11:12AM (#35011445)
A while back, I read a Sun patent on not implementing some instructions and emulating them in the kernel's illegal operation trap handler. The whole patent seemed obvious to me, but I'm glad that it was patented and now expired.

For MIT's 6.004 "Beta" processor loosely based on the DEC Alpha AXP, our test cases ran with a minimal kernel that would trap and emulate multiply, divide, etc. instructions using shifts and adds/subtracts.

In any case, particularly in the world of hypervisors, it doesn't seem too hard to deprecate an instruction and stop implementing it in hardware, and push that complexity into firmware. As long as the CPU covers the Popek and Goldberg virtualization requirements, hypervisors could be nested, and the firmware could implement a lowest level hypervisor that handles unimplemented instructions.

More generally, I wish ARM64, RISC-V, and other modern ISAs had taken DEC Alpha AXP's idea of restricting all of the privileged instructions to the firmware (PALCode in the Alpha's case) and basically implementing a single-tenant hypervisor in the firmware, with the OS kernel using an upcall instruction to the hypervisor/firmware. (UNSW's L4/Alpha microkernel was actually implemented in PALCode, so in that case, the L4 microkernel was the firmware-implemented hypervisor and the L4 syscalls were upcalls to the firmware.) As it stands, hypervisors need to both implement upcalls for efficiency and also implement trap-and-emulate functionality for OS kernels that aren't hypervisor-aware. The trap-and-emulate portions of the code are both lower performance and more complicated than the upcall handlers. Both hypervisors and OS kernels would be simpler if the platform guaranteed a hypervisor is always present.

Re:RVVM – The RISC-V Virtual Machine by nerpderp82 on Friday March 03, 2023 @12:00PM (#35012173)
> Both hypervisors and OS kernels would be simpler if the platform guaranteed a hypervisor is always present.

This would make an excellent RISC-V proposal!

The nice thing is that RISC-V already has the concept of the HART, you could have a supervisor hart that manages many virtual harts.

Re:RVVM – The RISC-V Virtual Machine by snvzz on Friday March 03, 2023 @12:54PM (#35012893)
>A while back, I read a Sun patent on not implementing some instructions and emulating them in the kernel's illegal operation trap handler. The whole patent seemed obvious to me, but I'm glad that it was patented and now expired, providing obvious prior art in any attempts to patent it today.

I understand opensbi runs in M mode, taking on that role among others.

Re:RVVM – The RISC-V Virtual Machine by LekKit on Saturday March 04, 2023 @05:57AM (#35019975)
> I wish ARM64, RISC-V, and other modern ISAs had taken DEC Alpha AXP's idea of restricting all of the privileged instructions to the firmware

This is already possible on RISC-V to some extend, by trapping privileged instructions into upper privileged modes. Everything in the ISA is made so it may be achieved cleanly. It also does not allow to detect current privileged mode, so the kernel running in U-mode and trapped on each privileged instruction would never know it's actually not in S-mode.

There is even a software-based hypervisor extension emulator based on that, that brings KVM to non-hypervisor-capable HW: https://github.com/dramforever/opensbi-h

Re:RVVM – The RISC-V Virtual Machine by KMag on Saturday March 04, 2023 @06:16PM (#35025494)
Right, but it's cleaner and better performant to use higher-level upcalls to the hypervisor rather than trapping and emulating every privileged instruction.

As it stands, the hypervisor needs to implement both trap-and-emulate and upcall handlers, and OSes need to implement both running on bare metal and (if they want to perform well on hypervisors) hypervisor upcalls.

If you want your hypervisor to support nested hypervisors, then I guess you'd still need to implement trap-and-emulate in the hypervisor to allow running a hypervisor on top. However, you at least remove the dual paths in the OS kernel if you just disallow the bare-metal case. This also allows a bit more flexibility in hardware implementation as you can change the hardware implementation and the instruction sequence in the hypervisor without needing to modify any legacy OS kernels.

Re:RVVM – The RISC-V Virtual Machine by snvzz on Friday March 03, 2023 @12:52PM (#35012859)
>Of course, bot x86 and Arm started like that as well - but after 20+ years of evolution, they have to drag along a lot of history. (and one never really takes things away from an ISA, you only add new features, and at best deprecate old ones).

Feature-wise, RISC-V is already about on par and has managed not to become a mess.

Furthermore, unlike x86, ARM has broken binary compatibility several times in the past, and yet failed to use the chance to do anything else than minor adjustments.

RISC-V is in a really good position.

Re:RVVM – The RISC-V Virtual Machine by chungy on Friday March 03, 2023 @01:42PM (#35013443)
Pretty sure ARM has never broken binary compatibility, even across ISA expansions (16→32 bit, 32→64 bit).
Re:RVVM – The RISC-V Virtual Machine by sweetjuly on Friday March 03, 2023 @09:02PM (#35017401)
ARMv8 aarch32 is backwards compatible with v5-v7 but aarch64 mode cannot run aarch32 code of any kind. ARMv8 makes supporting aarch32 mode optional too and so it really is a hard break.
Re:RVVM – The RISC-V Virtual Machine by brucehoult on Sunday March 05, 2023 @10:50PM (#35037335)
ARM has broken binary compatibility many many times.

The first was probably when they took the condition codes out of the hi bits of the PC into their own register, to allow 32 bit addressing instead of 26 bit addressing, thus nuking all the software that thought (as it was encouraged to!) that saving and restoring the PC also saved and restored condition codes.

After Thumb mode was introduced they've waffled over whether you must use only BX to change modes, or whether any instruction that writes to PC (mov, add, pop ...) is ok.

Thumb only CPUs such as Cortex M3/M4/M7 can't run code from e.g. ARM7TDMI. The Thumb mode stuff will work, but it's not a complete ISA. On ARM7TDMI you have to switch to ARM mode for many things, while on ARMv7 you need additional instructions that don't exist on ARM7TDMI. CM0 adds only the essential operations to Thumb1, while M3/M4/M7 add basically a complete re-encoding of ARM mode -- minus conditional execution on every instruction.

In Thumb mode on ARM7DTMI (and successors) a 4 byte `BL` instruction is actually two 2-byte instructions run one after the other. You can separate them with arbitrary other instructions between them, as long as LR is not touched. In ARMv7 `BL` looks the same, but you can no longer split the two 2-byte halves -- it is now actually a 4-byte instruction, and other 4-byte instructions exist using the same initial 2 bytes.

And of course Aarch64 is utterly incompatible with any of the 32 bit ISAs. Early 64 bit cores from ARM also ran 32 bit code. At first they could boot 32 bit OSes, then only 64 bit OSes but they could run 32 bit user code. The latest ARM cores run only 64 bit code, and ARM has said no future 64 bit cores will run 32 bit code at all. Other 64 bit only cores also exist, including the ThunderX (9 years ago!) and Apple's M1 and M2 as well as all iPhones starting from the iPhone 8.

Re:RVVM – The RISC-V Virtual Machine by astrange on Friday March 03, 2023 @09:38PM (#35017578)
ARMv8 AArch64 is pretty different from earlier ARMs and is probably more performant than the overly academic RISC-V.
Re:RVVM – The RISC-V Virtual Machine by GoblinSlayer on Friday March 03, 2023 @04:01PM (#35014932)
That's quite copious way to implement it.
Re:RVVM – The RISC-V Virtual Machine by actionfromafar on Friday March 03, 2023 @06:13PM (#35016289)
So is this, even more so in my taste:

https://github.com/cnlohr/mini-rv32ima/blob/master/mini-rv32...

A RISC-V emulator in one include file.

[dead]
Re:RVVM – The RISC-V Virtual Machine by actionfromafar on Sunday March 05, 2023 @05:24AM (#35028354)
Well, the next best thing is to show it now running Doom! Or Quake.