Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to have a value that may be changed by processor unexpectedly?

Tags:

rust

For example, suppose I have a PageTable value and register its physical address with CR3 register.

#![no_std]

use {
    spinning_top::{const_spinlock, Spinlock},
    x86_64::structures::paging::PageTable,
};

// The physical address of `PML4` is registered with CR3 register.
static PML4: Spinlock<PageTable> = const_spinlock(PageTable::new());

The processor changes the Accessed bit of an entry of PML4 if a page is accessed. I'm afraid that the optimized code may use a cached value stored in one of the CPU registers or the stack instead of the actual value on memory, causing the use of the old value of the bit.

In this case, the impact should be small, and I don't care the Accessed bit. But, in general, is it safe to have a value that the processor may change unexpectedly (like MMIO and DMA buffer) or a reference to such a value? Or should I do a read-modify-write cycle every time through a raw pointer with read_volatile and write_volatile?

like image 946
toku-sa-n Avatar asked Aug 17 '21 15:08

toku-sa-n


People also ask

Is changing number of processors safe?

yes change it, i did it too. it only forces your pc to use all available cpu cores. normally they are limited. so if your cpu has 8 cores you select 8 cores, nothing will happen its a normal setting no harm or danger.

Will changing CPU affect anything?

All of your data is stored on your hard drive, so you won't lose anything. Just make sure to unplug your power cord, after turning off your computer. Don't forget to apply thermal paste to the new CPU. The i5 3550S probably won't bottleneck your system.

What happens if processor is faulty?

A computer with a bad CPU won't go through the usual "boot-up" process when you turn the power on. You may hear the fans and disk drive running, but the screen may remain completely blank. No amount of key pressing or mouse clicking will get a response from the PC.

Is processor worth changing?

So, if you're using any applications that are severely limited by how fast your single-core speed is, a CPU upgrade does still make perfect sense. Newer processor generations generally bring much better single-core performance to the table.


1 Answers

Since you specifically asked about "a reference to such a value": If you have a &T, and T mutates for any reason while the reference is alive, the program will show undefined behavior.

As far as I know, the documentation in the Reference is not yet final on this point, yet the principle is clear. From the docs of UnsafeCell:

If you have a reference &T, then normally in Rust the compiler performs optimizations based on the knowledge that &T points to immutable data. Mutating that data, for example through an alias or by transmuting an &T into an &mut T, is considered undefined behavior.

And (edited for length):

If you create a safe reference with lifetime 'a (either a &T or &mut T reference) that is accessible by safe code, then you must not access the data in any way that contradicts that reference for the remainder of 'a. For example, this means that if you take the *mut T from an UnsafeCell and cast it to an &T, then the data in T must remain immutable until that reference’s lifetime expires.

In effect, if there is a &PML4 or &mut PML4, the "volatile" property of PML4 can be considered a mutation through a "hidden" alias.

Depending on your needs, you either need to channel access to PML4 via a read_volatile (which is safe, because read_volatile always does a copy of underlying memory into what is then immutable memory); you are then free to give out references to that copy. Or you wrap the thing into an UnsafeCell<PML4>, in which case access still needs to be done by reading the value from the inner pointer (volatile or not), but at least you can pass around a &UnsafeCell<PML4>.

like image 161
user2722968 Avatar answered Sep 21 '22 13:09

user2722968