I'm trying to model a basic CPU by mmapping a 1 MiB file, corresponding to the RAM size. I want to read/write this file. Currently I'm getting a SIGBUS error with ram[2] = 1
which I gather is from trying to mmap outside the file range. I've read that perhaps I need to fill the file with zeroes as placeholders, but I'm a bit confused as to why I have to do this, since I thought mmap would automatically set aside a memory chunk for me that would be allocated when I first touch it (as I am trying to do below with my test). What am I missing?
int16_t ramD;
if ( (ramD = open("ramMap.txt", O_RDWR | O_CREAT, 0666)) == -1)
{
errx(EX_OSERR, "RAM could not be initialized");
}
uint8_t* ram = mmap(0, ram_bytes, PROT_READ | PROT_WRITE, MAP_SHARED, ramD, 0);
ram[2] = 1;
printf("%i", ram[2]);
Return Value Upon successful completion, the mmap() function returns the address at which the mapping was placed; otherwise, it returns a value of MAP_FAILED, which has a value of 0, and sets errno to indicate the error.
In computing, mmap(2) is a POSIX-compliant Unix system call that maps files or devices into memory. It is a method of memory-mapped file I/O. It implements demand paging because file contents are not immediately read from disk and initially use no physical RAM at all.
The mmap() function can be used to map a region of memory that is larger than the current size of the object. Memory access within the mapping but beyond the current end of the underlying objects may result in SIGBUS signals being sent to the process.
mmap works by manipulating your process's page table, a data structure your CPU uses to map address spaces. The CPU will translate "virtual" addresses to "physical" ones, and does so according to the page table set up by your kernel. When you access the mapped memory for the first time, your CPU generates a page fault.
The SIGBUS
means that you're writing outside the file. From Linux man pages mmap(2)
:
SIGBUS
Attempted access to a portion of the buffer that does not correspond to the file (for example, beyond the end of the file, including the case where another process has truncated the file).
As you create a new file, it is initially empty, i.e. has size of 0 bytes. You need to resize it using ftruncate
to be at least big enough to contain the address written to (possibly rounded up to the page size). As you wanted to have a ram disk of size ram_bytes
, then:
ftruncate(ramD, ram_bytes);
See this answer for a longer explanation about the same mechanism, using POSIX shared memory objects.
PS. open
returns an int
; you should use an int
, not int16_t
, to store the file descriptor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With