gpu_neon: revive the old tests
[pcsx_rearmed.git] / libpcsxcore / lightrec / mem.c
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3  * Copyright (C) 2022 Paul Cercueil <paul@crapouillou.net>
4  */
5
6 #ifndef _GNU_SOURCE
7 #define _GNU_SOURCE
8 #endif
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <inttypes.h>
12 #include <stdbool.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <sys/mman.h>
16 #include <sys/shm.h>
17 #include <sys/stat.h>
18 #include <sys/syscall.h>
19 #include <unistd.h>
20
21 #include "../psxhw.h"
22 #include "../psxmem.h"
23 #include "../r3000a.h"
24
25 #include "mem.h"
26
27 #define ARRAY_SIZE(a) (sizeof(a) ? (sizeof(a) / sizeof((a)[0])) : 0)
28
29 #ifndef MAP_FIXED_NOREPLACE
30 #define MAP_FIXED_NOREPLACE 0x100000
31 #endif
32
33 #ifndef MFD_HUGETLB
34 #define MFD_HUGETLB 0x0004
35 #endif
36
37 static const uintptr_t supported_io_bases[] = {
38         0x0,
39         0x10000000,
40         0x40000000,
41         0x80000000,
42 };
43
44 static void * mmap_huge(void *addr, size_t length, int prot, int flags,
45                         int fd, off_t offset)
46 {
47         void *map = MAP_FAILED;
48
49         if (length >= 0x200000) {
50                 map = mmap(addr, length, prot,
51                            flags | MAP_HUGETLB | (21 << MAP_HUGE_SHIFT),
52                            fd, offset);
53                 if (map != MAP_FAILED)
54                         printf("Hugetlb mmap to address 0x%" PRIxPTR " succeeded\n",
55                                (uintptr_t) addr);
56         }
57
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",
62                                (uintptr_t) addr);
63 #ifdef MADV_HUGEPAGE
64                         madvise(map, length, MADV_HUGEPAGE);
65 #endif
66                 }
67         }
68
69         return map;
70 }
71
72 static int lightrec_mmap_ram(bool hugetlb)
73 {
74         unsigned int i, j;
75         int err, memfd, flags = 0;
76         uintptr_t base;
77         void *map;
78
79         if (hugetlb)
80                 flags |= MFD_HUGETLB;
81
82         memfd = syscall(SYS_memfd_create, "/lightrec_memfd",
83                         flags);
84         if (memfd < 0) {
85                 err = -errno;
86                 fprintf(stderr, "Failed to create memfd: %d\n", err);
87                 return err;
88         }
89
90         err = ftruncate(memfd, 0x200000);
91         if (err < 0) {
92                 err = -errno;
93                 fprintf(stderr, "Could not trim memfd: %d\n", err);
94                 goto err_close_memfd;
95         }
96
97         for (i = 0; i < ARRAY_SIZE(supported_io_bases); i++) {
98                 base = supported_io_bases[i];
99
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)
105                                 break;
106                         // some systems ignore MAP_FIXED_NOREPLACE
107                         if (map != base_ptr) {
108                                 munmap(map, 0x200000);
109                                 break;
110                         }
111                 }
112
113                 /* Impossible to map using this base */
114                 if (j == 0)
115                         continue;
116
117                 /* All mirrors mapped - we got a match! */
118                 if (j == 4)
119                         break;
120
121                 /* Only some mirrors mapped - clean the mess and try again */
122                 for (; j > 0; j--)
123                         munmap((void *)(base + (j - 1) * 0x200000), 0x200000);
124         }
125
126         if (i == ARRAY_SIZE(supported_io_bases)) {
127                 err = -EINVAL;
128                 goto err_close_memfd;
129         }
130
131         err = 0;
132         psxM = (s8 *)base;
133
134 err_close_memfd:
135         close(memfd);
136         return err;
137 }
138
139 int lightrec_init_mmap(void)
140 {
141         unsigned int i;
142         uintptr_t base;
143         void *map;
144         int err = lightrec_mmap_ram(true);
145         if (err) {
146                 err = lightrec_mmap_ram(false);
147                 if (err) {
148                         fprintf(stderr, "Unable to mmap RAM and mirrors\n");
149                         return err;
150                 }
151         }
152
153         base = (uintptr_t) psxM;
154
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) {
159                 err = -EINVAL;
160                 fprintf(stderr, "Unable to mmap parallel port\n");
161                 goto err_unmap;
162         }
163
164         psxP = (s8 *)map;
165
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) {
170                 err = -EINVAL;
171                 fprintf(stderr, "Unable to mmap BIOS\n");
172                 goto err_unmap_parallel;
173         }
174
175         psxR = (s8 *)map;
176
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) {
181                 err = -EINVAL;
182                 fprintf(stderr, "Unable to mmap scratchpad\n");
183                 goto err_unmap_bios;
184         }
185
186         psxH = (s8 *)map;
187
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,
191                         -1, 0);
192         if (map == MAP_FAILED) {
193                 err = -EINVAL;
194                 fprintf(stderr, "Unable to mmap code buffer\n");
195                 goto err_unmap_scratch;
196         }
197
198         code_buffer = map;
199
200         return 0;
201
202 err_unmap_scratch:
203         munmap(psxH, 0x10000);
204 err_unmap_bios:
205         munmap(psxR, 0x200000);
206 err_unmap_parallel:
207         munmap(psxP, 0x10000);
208 err_unmap:
209         for (i = 0; i < 4; i++)
210                 munmap((void *)((uintptr_t)psxM + i * 0x200000), 0x200000);
211         return err;
212 }
213
214 void lightrec_free_mmap(void)
215 {
216         unsigned int i;
217
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);
224 }