1 // SPDX-License-Identifier: LGPL-2.1-or-later
3 * Copyright (C) 2022 Paul Cercueil <paul@crapouillou.net>
15 #include <sys/syscall.h>
19 #include "../psxmem.h"
20 #include "../r3000a.h"
24 #define ARRAY_SIZE(a) (sizeof(a) ? (sizeof(a) / sizeof((a)[0])) : 0)
26 #ifndef MAP_FIXED_NOREPLACE
27 #define MAP_FIXED_NOREPLACE 0x100000
31 #define MFD_HUGETLB 0x0004
34 static const uintptr_t supported_io_bases[] = {
41 static void * mmap_huge(void *addr, size_t length, int prot, int flags,
44 void *map = MAP_FAILED;
46 if (length >= 0x200000) {
47 map = mmap(addr, length, prot,
48 flags | MAP_HUGETLB | (21 << MAP_HUGE_SHIFT),
50 if (map != MAP_FAILED)
51 printf("Hugetlb mmap to address 0x%lx succeeded\n", (uintptr_t) addr);
54 if (map == MAP_FAILED) {
55 map = mmap(addr, length, prot, flags, fd, offset);
56 if (map != MAP_FAILED)
57 printf("Regular mmap to address 0x%lx succeeded\n", (uintptr_t) addr);
63 static int lightrec_mmap_ram(bool hugetlb)
66 int err, memfd, flags = 0;
73 memfd = syscall(SYS_memfd_create, "/lightrec_memfd",
77 fprintf(stderr, "Failed to create memfd: %d\n", err);
81 err = ftruncate(memfd, 0x200000);
84 fprintf(stderr, "Could not trim memfd: %d\n", err);
88 for (i = 0; i < ARRAY_SIZE(supported_io_bases); i++) {
89 base = supported_io_bases[i];
91 for (j = 0; j < 4; j++) {
92 map = mmap_huge((void *)(base + j * 0x200000),
93 0x200000, PROT_READ | PROT_WRITE,
94 MAP_SHARED | MAP_FIXED, memfd, 0);
95 if (map == MAP_FAILED)
99 /* Impossible to map using this base */
103 /* All mirrors mapped - we got a match! */
107 /* Only some mirrors mapped - clean the mess and try again */
109 munmap((void *)(base + (j - 1) * 0x200000), 0x200000);
112 if (i == ARRAY_SIZE(supported_io_bases)) {
114 goto err_close_memfd;
125 int lightrec_init_mmap(void)
130 int err = lightrec_mmap_ram(true);
132 err = lightrec_mmap_ram(false);
134 fprintf(stderr, "Unable to mmap RAM and mirrors\n");
139 base = (uintptr_t) psxM;
141 map = mmap((void *)(base + 0x1f000000), 0x10000,
142 PROT_READ | PROT_WRITE,
143 MAP_PRIVATE | MAP_FIXED_NOREPLACE | MAP_ANONYMOUS, 0, 0);
144 if (map == MAP_FAILED) {
146 fprintf(stderr, "Unable to mmap parallel port\n");
152 map = mmap_huge((void *)(base + 0x1fc00000), 0x200000,
153 PROT_READ | PROT_WRITE,
154 MAP_PRIVATE | MAP_FIXED_NOREPLACE | MAP_ANONYMOUS, 0, 0);
155 if (map == MAP_FAILED) {
157 fprintf(stderr, "Unable to mmap BIOS\n");
158 goto err_unmap_parallel;
163 map = mmap((void *)(base + 0x1f800000), 0x10000,
164 PROT_READ | PROT_WRITE,
165 MAP_PRIVATE | MAP_FIXED_NOREPLACE | MAP_ANONYMOUS, 0, 0);
166 if (map == MAP_FAILED) {
168 fprintf(stderr, "Unable to mmap scratchpad\n");
174 map = mmap_huge((void *)(base + 0x800000), CODE_BUFFER_SIZE,
175 PROT_EXEC | PROT_READ | PROT_WRITE,
176 MAP_PRIVATE | MAP_FIXED_NOREPLACE | MAP_ANONYMOUS,
178 if (map == MAP_FAILED) {
180 fprintf(stderr, "Unable to mmap code buffer\n");
181 goto err_unmap_scratch;
189 munmap(psxH, 0x10000);
191 munmap(psxR, 0x200000);
193 munmap(psxP, 0x10000);
195 for (i = 0; i < 4; i++)
196 munmap((void *)((uintptr_t)psxM + i * 0x200000), 0x200000);
200 void lightrec_free_mmap(void)
204 munmap(code_buffer, CODE_BUFFER_SIZE);
205 munmap(psxH, 0x10000);
206 munmap(psxR, 0x200000);
207 munmap(psxP, 0x10000);
208 for (i = 0; i < 4; i++)
209 munmap((void *)((uintptr_t)psxM + i * 0x200000), 0x200000);