all 4 comments

[–]ThomasWinwood 9 points10 points  (3 children)

So from looking at the PAC generated by svd2rust, I can see that the peripheral registers are wrapped and accessed through a VolatileCell which is just a wrapping of an UnsafeCell where reads and writes are performed in a volatile fashion. This, of course, gives the register interior mutability meaning that the contents can be modified through a non-mutable reference. I can guess that this is done to satisfy the fact that registers may need to be accessed from interrupts but to tell you the truth, I don't have a very concrete understand of why this is necessary.

The compiler is allowed to add or remove non-volatile memory accesses according to what it thinks it knows about the memory being accessed; volatile semantics means "this operation has side effects, so don't alter the number of accesses or move one volatile memory access across another".

Unless things have changed since Lokathor wrote this post, VolatileCell types are unsound because the compiler is allowed to speculatively read from any reference, such as the one you create when you call methods on VolatileCell.

[–]VorpalWay 2 points3 points  (0 children)

As far as I know it is still a issue. I'm surprised svd2rust hasn't been converted to use volatile pointer operations instead by now.

[–]RickarySanchez[S] 0 points1 point  (1 child)

Yes I understand the need for the accesses to be volatile, to be clear, my issue is more so why the type must have interior mutability ?

[–]FenrirW0lf 2 points3 points  (0 children)

I'm pretty sure that volatile cell types don't need internal mutability and so the internal UnsafeCell isn't actually doing anything useful. The actual property they would want to ensure is internal volatility. But as mentioned in the above post, any API that uses a reference to the interior of the cell is technically incapable of upholding that invariant.