1 // SPDX-License-Identifier: LGPL-2.1-or-later
3 * Copyright (C) 2022 Paul Cercueil <paul@crapouillou.net>
18 #include <sys/syscall.h>
22 #include "../psxmem.h"
23 #include "../r3000a.h"
27 #define ARRAY_SIZE(a) (sizeof(a) ? (sizeof(a) / sizeof((a)[0])) : 0)
29 #ifndef MAP_FIXED_NOREPLACE
30 #define MAP_FIXED_NOREPLACE 0x100000
34 #define MFD_HUGETLB 0x0004
37 static const uintptr_t supported_io_bases[] = {
44 static void * mmap_huge(void *addr, size_t length, int prot, int flags,
47 void *map = MAP_FAILED;
49 if (length >= 0x200000) {
50 map = mmap(addr, length, prot,
51 flags | MAP_HUGETLB | (21 << MAP_HUGE_SHIFT),
53 if (map != MAP_FAILED)
54 printf("Hugetlb mmap to address 0x%" PRIxPTR " succeeded\n",
58 if (map == MAP_FAILED) {
59 map = mmap(addr, length, prot, flags, fd, offset);
60 if (map != MAP_FAILED) {
61 printf("Regular mmap to address 0x%" PRIxPTR " succeeded\n",
64 madvise(map, length, MADV_HUGEPAGE);
72 static int lightrec_mmap_ram(bool hugetlb)
75 int err, memfd, flags = 0;
82 memfd = syscall(SYS_memfd_create, "/lightrec_memfd",
86 fprintf(stderr, "Failed to create memfd: %d\n", err);
90 err = ftruncate(memfd, 0x200000);
93 fprintf(stderr, "Could not trim memfd: %d\n", err);
97 for (i = 0; i < ARRAY_SIZE(supported_io_bases); i++) {
98 base = supported_io_bases[i];
100 for (j = 0; j < 4; j++) {
101 void *base_ptr = (void *)(base + j * 0x200000);
102 map = mmap_huge(base_ptr, 0x200000, PROT_READ | PROT_WRITE,
103 MAP_SHARED | MAP_FIXED_NOREPLACE, memfd, 0);
104 if (map == MAP_FAILED)
106 // some systems ignore MAP_FIXED_NOREPLACE
107 if (map != base_ptr) {
108 munmap(map, 0x200000);
113 /* Impossible to map using this base */
117 /* All mirrors mapped - we got a match! */
121 /* Only some mirrors mapped - clean the mess and try again */
123 munmap((void *)(base + (j - 1) * 0x200000), 0x200000);
126 if (i == ARRAY_SIZE(supported_io_bases)) {
128 goto err_close_memfd;
139 int lightrec_init_mmap(void)
144 int err = lightrec_mmap_ram(true);
146 err = lightrec_mmap_ram(false);
148 fprintf(stderr, "Unable to mmap RAM and mirrors\n");
153 base = (uintptr_t) psxM;
155 map = mmap((void *)(base + 0x1f000000), 0x10000,
156 PROT_READ | PROT_WRITE,
157 MAP_PRIVATE | MAP_FIXED_NOREPLACE | MAP_ANONYMOUS, -1, 0);
158 if (map == MAP_FAILED) {
160 fprintf(stderr, "Unable to mmap parallel port\n");
166 map = mmap_huge((void *)(base + 0x1fc00000), 0x200000,
167 PROT_READ | PROT_WRITE,
168 MAP_PRIVATE | MAP_FIXED_NOREPLACE | MAP_ANONYMOUS, -1, 0);
169 if (map == MAP_FAILED) {
171 fprintf(stderr, "Unable to mmap BIOS\n");
172 goto err_unmap_parallel;
177 map = mmap((void *)(base + 0x1f800000), 0x10000,
178 PROT_READ | PROT_WRITE,
179 MAP_PRIVATE | MAP_FIXED_NOREPLACE | MAP_ANONYMOUS, 0, 0);
180 if (map == MAP_FAILED) {
182 fprintf(stderr, "Unable to mmap scratchpad\n");
188 map = mmap_huge((void *)(base + 0x800000), CODE_BUFFER_SIZE,
189 PROT_EXEC | PROT_READ | PROT_WRITE,
190 MAP_PRIVATE | MAP_FIXED_NOREPLACE | MAP_ANONYMOUS,
192 if (map == MAP_FAILED) {
194 fprintf(stderr, "Unable to mmap code buffer\n");
195 goto err_unmap_scratch;
203 munmap(psxH, 0x10000);
205 munmap(psxR, 0x200000);
207 munmap(psxP, 0x10000);
209 for (i = 0; i < 4; i++)
210 munmap((void *)((uintptr_t)psxM + i * 0x200000), 0x200000);
214 void lightrec_free_mmap(void)
218 munmap(code_buffer, CODE_BUFFER_SIZE);
219 munmap(psxH, 0x10000);
220 munmap(psxR, 0x200000);
221 munmap(psxP, 0x10000);
222 for (i = 0; i < 4; i++)
223 munmap((void *)((uintptr_t)psxM + i * 0x200000), 0x200000);