phil-opp / blog_os

Writing an OS in Rust
http://os.phil-opp.com
Apache License 2.0
15.71k stars 1.08k forks source link

VGA MMIO: does it really not need volatile writes? #1344

Open meithecatte opened 1 month ago

meithecatte commented 1 month ago

Currently, the code used to access VGA does plain memory pokes:

    for (i, &byte) in HELLO.iter().enumerate() {
        unsafe {
            *vga_buffer.offset(i as isize * 2) = byte;
            *vga_buffer.offset(i as isize * 2 + 1) = 0xb;
        }
    }

This surprised me — I expected to see something like ptr::write_volatile here. Are you sure this code isn't working by accident?

I'm not too familiar with the exact semantics, but my guess would be that without volatile, these memory accesses are not considered side-effects, and therefore the compiler could, for example:

Is this something you've given some thought, or merely an oversight?

xuanplus commented 1 month ago

You will find it in next post: VGA Text Mode