News for hackers. Stuff that matters.
Re:Rust in QEMU Roadmap by bonzini on Wednesday November 27, 2024 @04:11AM (#42254353)
Thanks for posting this to HN! Author here, happy to answer any questions.

(By the way, I did not originally start the project, though I've worked quite a bit on the safe abstractions that are mentioned in the roadmap).

Re:Rust in QEMU Roadmap by ndesaulniers on Tuesday December 03, 2024 @12:44AM (#42303318)
Bullish or bearish on rust in the kernel?
Re:Rust in QEMU Roadmap by bonzini on Tuesday December 03, 2024 @12:49AM (#42303332)
Hello! Small world. :)

For drivers, it's already happening, especially for graphics but not limited to that. 6.13 has some very important changes. A lot of Linux is drivers so that's already a reason to be bullish.

Answering for QEMU instead: it depends on the community being willing to share the burden of writing the FFI code. Despite Rust being low level, there is still a substantial amount of work to do. Replies to the roadmap pointed out tracepoints as an area where I know nothing and therefore I would like someone else to do the work (I am working mostly on the object and threading models, which is also where a lot of the impedance mismatch between Rust and C lies).

Re:Rust in QEMU Roadmap by aapoalas on Tuesday December 03, 2024 @02:44AM (#42303847)
Hello. I assume tracepoints mean kprobes/uprobes or something along those lines? I've just this weekend worked on implementing/adapting a crate for DTrace USDTs aka DTrace probes to also work on Linux and generate SystemTap SDTs (aka USDTs aka dtrace probes).

This is probably a little different from tracepoints in the kernel space but I'm somewhat interested in going deeper and into the kernel side of things. Let me know if you have any pointers as to where I might be of concrete assistance to you!

Re:Rust in QEMU Roadmap by bonzini on Tuesday December 03, 2024 @02:53AM (#42303900)
QEMU has several tracepoint providers, the main ones are (a slightly fancy version of) printf and USDT. There is a Python program that generates the C code for the chosen backend(s), so the thing to do would be to adjust the script to produce either an FFI bridge or the equivalent Rust code.
Re:Rust in QEMU Roadmap by aapoalas on Tuesday December 03, 2024 @04:52AM (#42304537)
Can you link the script? This sounds vaguely like something that would be no skin off my back, so I'd be quite happy to help with this.
Re:Rust in QEMU Roadmap by bonzini on Tuesday December 03, 2024 @05:17AM (#42304690)
Re:Rust in QEMU Roadmap by aapoalas on Wednesday December 04, 2024 @09:45AM (#42317913)
Here's my WIP addition to Oxide Computer's usdt crate: https://github.com/oxidecomputer/usdt/pull/340

This crate gives you a nifty macro that translates Rust probe definitions into the correct ELF sections and other related assembly. Other ways to declare probes are also supported, though.

Their support is currently only for illumos and OS X but I managed to get Linux SystemTap SDTs working as well, and a PR for FreeBSD exists as well but has been languishing in review purgatory for two years: I imagine QEMU might enjoy this wide support (if they land).

Re:Rust in QEMU Roadmap by JoshTriplett on Tuesday December 03, 2024 @03:06AM (#42303956)
What does your wishlist for Rust look like? (Besides "simpler C/Rust interoperability", of course.) Has QEMU run into things that Rust-for-Linux hasn't, that feel missing from the Rust language?
Re:Rust in QEMU Roadmap by bonzini on Tuesday December 03, 2024 @03:15AM (#42304008)
Right now the only language-level thing I would like is const operator overloading. Even supporting MSRV as old as 1.63 was not a big deal, the worst thing was dependencies using let...else which we will vendor and patch.

Pin is what it is, but it is mostly okay since I haven't needed projection so far. Initialization using Linux's "impl PinInit<Self>" approach seems to be working very well in my early experiments, I contributed changes to use the crate without unstable features.

In the FFI area: Bindgen support for toml configuration (https://github.com/rust-lang/rust-bindgen/pull/2917 but it could also be response files on the command line), and easier passing of closures from Rust to C (though I found a very nice way to do it for ZSTs that implement Fn, which is by far the common case).

The "data structure interoperability" part of the roadmap is something I should present to someone in the Rust community for impressions. Some kind of standardization of core FFI traits would be nice.

Outside the Rust core proper, Meson support needs to mature a bit for ease of use, but it is getting there. Linux obviously doesn't need that.

BTW, saw your comment in the dead thread, you're too nice. I have been curious about Rust for some time and with Linux maturing, and Linaro doing the first contribution of build system integration + sample device code, it was time to give it a try.

Re:Rust in QEMU Roadmap by JoshTriplett on Tuesday December 03, 2024 @10:00AM (#42306792)
> Right now the only language-level thing I would like is const operator overloading.

As far as I know, const traits are still on track.

> easier passing of closures from Rust to C

As in, turning a Rust closure into a C function-pointer-plus-context-parameter?

> The "data structure interoperability" part of the roadmap is something I should present to someone in the Rust community for impressions. Some kind of standardization of core FFI traits would be nice.

Would be happy to work with you to get something on the calendar.

Re:Rust in QEMU Roadmap by bonzini on Tuesday December 03, 2024 @11:51AM (#42308259)
> As far as I know, const traits are still on track.

Yes they are. My use case is something like the bitflags crate, there are lots of bit flags in emulated devices of course. In the meanwhile I guess it would be possible to use macros to turn something like "bit_const!(Type:A|B)" to "Type(A.0|B.0)" or something like that.

> As in, turning a Rust closure into a C function-pointer-plus-context-parameter?

Yes, more in general everything related to callbacks is doable but very verbose. We might do (procedural?) macro magic later on to avoid the verbosity but for now I prefer to stick to pure Rust until there's an idea of which patterns recur.

Let me know by email about any occasions to present what I have.

Re:Rust in QEMU Roadmap by 0x457 on Tuesday December 03, 2024 @12:34PM (#42308764)
I made this handy function to pass callbacks to C: https://github.com/andoriyu/uclicious/blob/master/src/traits...
Re:Rust in QEMU Roadmap by o11c on Tuesday December 03, 2024 @10:34AM (#42307230)
> Related to this, the IsA trait allows typesafe compile-time checked casts. Unlike in C code, casting to a superclass can be written in such a way that the compiler will complain if the destination type is not a superclass, and with zero runtime cost.

This is unfair to C. With a little work when defining all classes (or non-leaf classes if you're willing for a hairier implementation), you can do this there too.

There are probably other ways, but the way that seems most "obvious" to me is to define the base via union of all bases in the chain, rather than only the immediate base class. So:

  /*
  class Foo {int f;};
  class Bar extends Foo {int b;};
  class Qux extends Bar {int q;};
  class Leaf extends Qux {int l;};
  */
  struct Foo { int f; };
  struct Bar { union { struct Foo base, base_Foo}; int b; };
  struct Qux { union { struct Bar base, base_Bar; struct Foo base_Foo; }; int q; };
  struct Leaf { struct Qux base; int l; }
Then your "compile-time-safe-cast to base class" macro just checks 3 things in order:

  1. check if we're already the right class.
  2. check if `base` is the right class.
  3. unconditionally try to use the appropriately-named `base_Foo` member as calculated from the type.
(runtime-safe-checked downcasts just have to check that the reverse is possible)
Re:Rust in QEMU Roadmap by bonzini on Tuesday December 03, 2024 @11:14AM (#42307765)
"Your scientists were so preoccupied with whether or not they could, they didn't stop to think if they should." :D

Though I admit that Rust also has to include the full list of base classes (https://github.com/rust-lang/rfcs/pull/1268 would fix it).

Re:Rust in QEMU Roadmap by o11c on Tuesday December 03, 2024 @03:15PM (#42310786)
The main reason the C version must hard-code is because C macros can't be recursive. What's Rust's excuse?

(that said, the C approach is also nice for manually accessing fields of distant base classes)

Re:Rust in QEMU Roadmap by bonzini on Wednesday December 04, 2024 @12:56AM (#42314808)
In Rust's case it is that when you say "you have IsA<Device> for all types that implement IsA<PCIDevice>" you are actually saying "you have IsA<Device> for all types, but only if they implement IsA<PCIDevice>".

When you later say "you have IsA<Device> for all types, but only if they implement IsA<I2CDevice>" the two conditions conflict.

It might be possible to avoid this issue with a different implementation but this is the simplest one that works and QEMU's hierarchy is generally shallow. If a better implementation came along, it would be a matter of search and repeat.

Re:Rust in QEMU Roadmap by jefbyokyie on Tuesday December 03, 2024 @04:40PM (#42311849)
> define the base via union of all bases in the chain [...] just checks 3 things in order

Can you please show a concrete example? Thanks.

Re:Rust in QEMU Roadmap by david-gpu on Tuesday December 03, 2024 @07:53PM (#42313443)
As a recovering language lawyer, I need to point out that what you are describing is not portable behavior because it is writing to one member of a union and reading from a different member, if I understood you correctly.

Will it work on most tool chains? Yes. But when it doesn't, it is going to be fun.

Re:Rust in QEMU Roadmap by bonzini on Wednesday December 04, 2024 @12:50AM (#42314789)
No, you only need to assert that the field with that name exists.
Re:Rust in QEMU Roadmap by flohofwoe on Wednesday December 04, 2024 @04:12AM (#42315776)
AFAIK this is legal in C but not in C++ (one of many subtle differences between actual C and the C subset of C++).
Re:Rust in QEMU Roadmap by david-gpu on Wednesday December 04, 2024 @07:03AM (#42316656)
Looking at the ISO 9899:2018 spec, a few sections appear to be relevant.

This one I wasn't familiar with, and I believe it absolves the comment I initially replied to in this thread.

> 6.5.2.3.6 states:

> One special guarantee is made in order to simplify the use of unions: if a union contains several structures that share a common initial sequence (see below), and if the union object currently contains one of these structures, it is permitted to inspect the common initial part of any of them anywhere that a declaration of the completed type of the union is visible

Outside of that special case, the general semantics of unions are specified as follows. I think that means you are right?

> 6.2.6.1.7 states

> When a value is stored in a member of an object of union type, the bytes of the object representation that do not correspond to that member but do correspond to other members take unspecified values

Re:Rust in QEMU Roadmap by flohofwoe on Wednesday December 04, 2024 @08:43AM (#42317344)
There is also a footnote:

99) If the member used to read the contents of a union object is not the same as the member last used to store a value in the object, the appropriate part of the object representation of the value is reinterpreted as an object representation in the new type as described in 6.2.6 (a process sometimes called "type punning"). This might be a trap representation.

...it all makes sense until that last sentence, maybe that's for really weird bit casts like trying to reinterpret an integer as float. FWIW the only place I used union type punning was in an Z80 emulator for the 16-bit register pairs, but that's no longer necessary, since modern compilers will optimize two consequitive byte reads into a single word read:

https://www.godbolt.org/z/or7Woc4TP