^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * hugepage-mmap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Example of using huge page memory in a user application using the mmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * system call. Before running this application, make sure that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * administrator has mounted the hugetlbfs filesystem (on some directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * like /mnt) using the command mount -t hugetlbfs nodev /mnt. In this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * example, the app is requesting memory of size 256MB that is backed by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * huge pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * For the ia64 architecture, the Linux kernel reserves Region number 4 for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * huge pages. That means that if one requires a fixed address, a huge page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * aligned address starting with 0x800000... will be required. If a fixed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * address is not required, the kernel will select an address in the proper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * Other architectures, such as ppc64, i386 or x86_64 are not so constrained.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <sys/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define FILE_NAME "huge/hugepagefile"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define LENGTH (256UL*1024*1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define PROTECTION (PROT_READ | PROT_WRITE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) /* Only ia64 requires this */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #ifdef __ia64__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define ADDR (void *)(0x8000000000000000UL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define FLAGS (MAP_SHARED | MAP_FIXED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define ADDR (void *)(0x0UL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #define FLAGS (MAP_SHARED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static void check_bytes(char *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) printf("First hex is %x\n", *((unsigned int *)addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static void write_bytes(char *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) unsigned long i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) for (i = 0; i < LENGTH; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) *(addr + i) = (char)i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static int read_bytes(char *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) unsigned long i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) check_bytes(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) for (i = 0; i < LENGTH; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (*(addr + i) != (char)i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) printf("Mismatch at %lu\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) int main(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) void *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) int fd, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) fd = open(FILE_NAME, O_CREAT | O_RDWR, 0755);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) perror("Open failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) addr = mmap(ADDR, LENGTH, PROTECTION, FLAGS, fd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (addr == MAP_FAILED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) perror("mmap");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) unlink(FILE_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) printf("Returned address is %p\n", addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) check_bytes(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) write_bytes(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) ret = read_bytes(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) munmap(addr, LENGTH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) unlink(FILE_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }