Skip to content

Commit aa30f2c

Browse files
author
Gregory Price
committed
Allocation: DAX
1 parent ec559d1 commit aa30f2c

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

doc/allocation/dax.rst

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,58 @@
22
33
DAX Devices
44
###########
5-
todo
5+
CXL capacity exposed as a DAX device can be accessed directly via mmap.
6+
Users may wish to use this interface mechanism to write their own userland
7+
CXL allocator, or to managed shared or persistent memory regions across multiple
8+
hosts.
9+
10+
If the capacity is shared across hosts or persistent, appropriate flushing
11+
mechanisms must be employed unless the region supports Snoop Back-Invalidate.
12+
13+
Note that mappings must be aligned (size and base) to the dax device's base
14+
alignment, which is typically 2MB - but maybe be configured larger.
15+
16+
::
17+
18+
#include <stdio.h>
19+
#include <stdlib.h>
20+
#include <stdint.h>
21+
#include <sys/mman.h>
22+
#include <fcntl.h>
23+
#include <unistd.h>
24+
25+
#define DEVICE_PATH "/dev/dax0.0" // Replace DAX device path
26+
#define DEVICE_SIZE (4ULL * 1024 * 1024 * 1024) // 4GB
27+
28+
int main() {
29+
int fd;
30+
void* mapped_addr;
31+
32+
/* Open the DAX device */
33+
fd = open(DEVICE_PATH, O_RDWR);
34+
if (fd < 0) {
35+
perror("open");
36+
return -1;
37+
}
38+
39+
/* Map the device into memory */
40+
mapped_addr = mmap(NULL, DEVICE_SIZE, PROT_READ | PROT_WRITE,
41+
MAP_SHARED, fd, 0);
42+
if (mapped_addr == MAP_FAILED) {
43+
perror("mmap");
44+
close(fd);
45+
return -1;
46+
}
47+
48+
printf("Mapped address: %p\n", mapped_addr);
49+
50+
/* You can now access the device through the mapped address */
51+
uint64_t* ptr = (uint64_t*)mapped_addr;
52+
*ptr = 0x1234567890abcdef; // Write a value to the device
53+
printf("Value at address %p: 0x%016llx\n", ptr, *ptr);
54+
55+
/* Clean up */
56+
munmap(mapped_addr, DEVICE_SIZE);
57+
close(fd);
58+
return 0;
59+
}

0 commit comments

Comments
 (0)