Merge pull request #586 from notaz/libretro_overwrite_drc
[pcsx_rearmed.git] / frontend / switch / sys / mman.h
1 #ifndef MMAN_H
2 #define MMAN_H
3
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <stdint.h>
11 #include <malloc.h>
12 #include <switch.h>
13 #include <stdlib.h>
14
15 //#include "3ds_utils.h"
16
17 #define PROT_READ       0b001
18 #define PROT_WRITE      0b010
19 #define PROT_EXEC       0b100
20 #define MAP_PRIVATE     2
21 #define MAP_FIXED       0x10
22 #define MAP_ANONYMOUS   0x20
23
24 #define MAP_FAILED      ((void *)-1)
25
26 static void* dynarec_cache = NULL;
27 static void* dynarec_cache_mapping = NULL;
28
29 static inline void* mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset)
30 {
31    (void)fd;
32    (void)offset;
33
34    //void* addr_out;
35     Result rc = svcMapPhysicalMemory(addr, len);
36     if (R_FAILED(rc))
37     {
38         printf("mmap failed\n");
39         return malloc(len);
40     }
41
42     return addr;
43
44 //   if((prot == (PROT_READ | PROT_WRITE | PROT_EXEC)) &&
45 //      (flags == (MAP_PRIVATE | MAP_ANONYMOUS)))
46 //   {
47 //      if(true)// __ctr_svchax)
48 //     {
49 //         /* this hack works only for pcsx_rearmed */
50 //         uint32_t currentHandle;
51 //
52 //         if(!dynarec_cache)
53 //            dynarec_cache = memalign(0x1000, len);
54 //
55 //         //svcDuplicateHandle(&currentHandle, 0xFFFF8001);
56 //         //svcControlProcessMemory(currentHandle, addr, dynarec_cache,
57 //         //                        len, MEMOP_MAP, prot);
58 //         svcCloseHandle(currentHandle);
59 //         dynarec_cache_mapping = addr;
60 //         return addr;
61 //      }
62 //      else
63 //      {
64 //         printf("tried to mmap RWX pages without svcControlProcessMemory access !\n");
65 //         return MAP_FAILED;
66 //      }
67 //
68 //   }
69
70 //   addr_out = memalign(0x1000, len);
71 //   if(!addr_out)
72 //      return MAP_FAILED;
73 //
74 //   return addr_out;
75 }
76
77 static inline int mprotect(void *addr, size_t len, int prot)
78 {
79     return 0;
80    //if(true) // __ctr_svchax)
81    //{
82    //   uint32_t currentHandle;
83    //   //svcDuplicateHandle(&currentHandle, 0xFFFF8001);
84    //   //svcControlProcessMemory(currentHandle, addr, NULL,
85    //   //                        len, MEMOP_PROT, prot);
86    //   svcCloseHandle(currentHandle);
87    //   return 0;
88    //}
89
90    //printf("mprotect called without svcControlProcessMemory access !\n");
91    //return -1;
92 }
93
94 static inline int munmap(void *addr, size_t len)
95 {
96     Result rc = svcUnmapPhysicalMemory(addr, len);
97     if (R_FAILED(rc))
98     {
99         printf("munmap failed\n");
100         free(addr);
101     }
102     return 0;
103 //   if((addr == dynarec_cache_mapping) && true)//__ctr_svchax)
104 //   {
105 //      uint32_t currentHandle;
106 //      //svcDuplicateHandle(&currentHandle, 0xFFFF8001);
107 //      //svcControlProcessMemory(currentHandle,
108 //      //                        dynarec_cache, dynarec_cache_mapping,
109 //      //                        len, MEMOP_UNMAP, 0b111);
110 //      svcCloseHandle(currentHandle);
111 //      dynarec_cache_mapping = NULL;
112 //
113 //   }
114 //   else
115       free(addr);
116
117    return 0;
118 }
119
120 #ifdef __cplusplus
121 };
122 #endif
123
124 #endif // MMAN_H
125