Reputation: 53
I am creating a custom operating system and is there any way to store data (almost like a filesystem) in assembly so that if the computer shuts off and turns back on the data will still be there?
Upvotes: 4
Views: 639
Reputation: 365576
You can write device drivers for SATA hard drives, USB mass storage, floppy disks, NVMe flash, or whatever else in asm. You might also be able to use BIOS functions to access them (especially if you're on x86). But then you have to manage writes in chunks of 512B or 4096B, because those kinds of storage are block-based.
A more literal interpretation of the question has an interesting answer: can a store instruction like mov [mem], eax
put data into persistent storage where a load instruction can get it later (after a power cycle)?
Yes, if your hardware has some memory-mapped non-volatile RAM. (Physically memory-mapped NVRAM like an NVDIMM, not like mmap()
to logically map a file into the virtual memory address space of a process). See this answer on Superuser about Intel Optane DC Persistent Memory
x86 for example has recently gotten more instructions to support NVRAM, like clwb
to write-back a cache line (all the way to memory) without necessarily evicting it. Early implementations of clwb
may just run it like clflushopt
, though: @Ana reports that Skylake-X does evict.
Also, clflushopt
is a more efficient way to force more cache lines to memory. Use a memory barrier like sfence
after a weakly-ordered flush like clflushopt
to make sure data is in non-volatile RAM before further writes appear.
For a while Intel was going to require pcommit
as part of making sure data had hit non-volatile storage, but decided against it. With that in mind, see Why Intel added the CLWB and PCOMMIT instructions for more details about using persistent RAM.
IDK what the situation is on architectures other than x86, but presumably NV RAM is / will be usable with ARM and other CPUs, too.
Upvotes: 5