mmap SIGBUS error and initializing the file
Asked Answered
B

2

7

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]);
Benedikta answered 14/6, 2017 at 20:11 Comment(8)
what is the value of ram_bytes?Titi
You didn't check if mmap succeeded!Straddle
@DavidSchwartz it doesn't matter, sigbus will be generated when the mapping is successful.Roar
@AnttiHaapala Sure, but that would be a completely different reason than if, for example, he's on a platform that whose shared memory support has been disabled.Straddle
@Titi ram_bytes is 1048576 (1 MiB)Benedikta
Sidenote: If you want to model a CPU, why not simply allocating the RAM? Why the complicated way mmapping a file?Cantrell
Also int16_t ramD; is a very strange variable type for a file descriptor. Why not just use an int?Clapperclaw
@Olaf mmap allows me to sync the file to disk using msync after a series of processes that act on the RAM memory (file)Benedikta
F
11

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.

Frye answered 14/6, 2017 at 20:24 Comment(0)
L
1

Consider using posix_fallocate which ensures disk space is allocated unlike ftruncate. https://man7.org/linux/man-pages/man3/posix_fallocate.3.html

Lodgment answered 27/11, 2023 at 17:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.