diff --git a/DiskImage.cpp b/DiskImage.cpp
index aca982a..e699f98 100644
--- a/DiskImage.cpp
+++ b/DiskImage.cpp
@@ -1156,7 +1156,7 @@ void TDiskImage::Open(const char *filename, bool ROnly)
}
FType = typ;
- int hfile = open(filename, O_RDONLY);
+ int hfile = open(filename, O_RDONLY | O_CLOEXEC);
if (hfile < 0)
{
diff --git a/MiSTer.vcxproj b/MiSTer.vcxproj
index e7857cf..088b138 100644
--- a/MiSTer.vcxproj
+++ b/MiSTer.vcxproj
@@ -75,6 +75,7 @@
+
@@ -139,6 +140,7 @@
+
diff --git a/MiSTer.vcxproj.filters b/MiSTer.vcxproj.filters
index b17a54d..e2cdd2b 100644
--- a/MiSTer.vcxproj.filters
+++ b/MiSTer.vcxproj.filters
@@ -208,6 +208,9 @@
Source Files\support
+
+ Source Files
+
@@ -396,5 +399,8 @@
Header Files\support
+
+ Header Files
+
\ No newline at end of file
diff --git a/audio.cpp b/audio.cpp
index 9ace888..d1e7575 100644
--- a/audio.cpp
+++ b/audio.cpp
@@ -2,8 +2,6 @@
#include
#include
#include
-#include
-#include
#include
#include
#include
diff --git a/battery.cpp b/battery.cpp
index 9393858..3a4d7ef 100644
--- a/battery.cpp
+++ b/battery.cpp
@@ -115,7 +115,7 @@ static int smbus_open(int dev_address)
int fd;
sprintf(str, "/dev/i2c-%d", bus);
- if ((fd = open(str, O_RDWR)) < 0)
+ if ((fd = open(str, O_RDWR | O_CLOEXEC)) < 0)
{
printf("Unable to open I2C bus %s: %s\n", str, strerror(errno));
continue;
diff --git a/brightness.cpp b/brightness.cpp
index 925d196..7fe0d3d 100644
--- a/brightness.cpp
+++ b/brightness.cpp
@@ -71,7 +71,7 @@ static int spi_open(int speed, int mode)
int fd ;
mode &= 3; // Mode is 0, 1, 2 or 3
- if ((fd = open ("/dev/spidev1.0", O_RDWR)) < 0)
+ if ((fd = open("/dev/spidev1.0", O_RDWR | O_CLOEXEC)) < 0)
{
printf("Unable to open SPI device: %s\n", strerror (errno));
return -1;
diff --git a/file_io.cpp b/file_io.cpp
index edfcdc2..3d92a55 100644
--- a/file_io.cpp
+++ b/file_io.cpp
@@ -394,7 +394,7 @@ int FileOpenEx(fileTYPE *file, const char *name, int mode, char mute, int use_zi
}
else
{
- int fd = (mode == -1) ? shm_open("/vdsk", O_CREAT | O_RDWR | O_TRUNC, 0777) : open(full_path, mode, 0777);
+ int fd = (mode == -1) ? shm_open("/vdsk", O_CREAT | O_RDWR | O_TRUNC | O_CLOEXEC, 0777) : open(full_path, mode | O_CLOEXEC, 0777);
if (fd <= 0)
{
if(!mute) printf("FileOpenEx(open) File:%s, error: %s.\n", full_path, strerror(errno));
diff --git a/fpga_io.cpp b/fpga_io.cpp
index 70fc114..25e92c2 100644
--- a/fpga_io.cpp
+++ b/fpga_io.cpp
@@ -7,7 +7,6 @@
#include
#include
#include
-#include
#include
#include "fpga_io.h"
@@ -15,6 +14,7 @@
#include "input.h"
#include "osd.h"
#include "menu.h"
+#include "shmem.h"
#include "fpga_base_addr_ac5.h"
#include "fpga_manager.h"
@@ -36,7 +36,6 @@ static struct socfpga_system_manager *sysmgr_regs = (socfpga_system_manager *)S
static struct nic301_registers *nic301_regs = (nic301_registers *)SOCFPGA_L3REGS_ADDRESS;
static uint32_t *map_base;
-static int fd;
#define writel(val, reg) *MAP_ADDR(reg) = val
#define readl(reg) *MAP_ADDR(reg)
@@ -392,15 +391,8 @@ static void do_bridge(uint32_t enable)
static int make_env(const char *name, const char *cfg)
{
- if ((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) return -1;
-
- void* buf = mmap(0, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0x1FFFF000);
- if (buf == (void *)-1)
- {
- printf("Unable to mmap(/dev/mem)\n");
- close(fd);
- return -1;
- }
+ void* buf = shmem_map(0x1FFFF000, 0x1000);
+ if (!buf) return -1;
volatile char* str = (volatile char*)buf;
memset((void*)str, 0, 0xF00);
@@ -424,7 +416,7 @@ static int make_env(const char *name, const char *cfg)
*str++ = '"';
*str++ = '\n';
FileLoad(cfg, (void*)str, 0);
- munmap(buf, 0x1000);
+ shmem_unmap(buf, 0x1000);
return 0;
}
@@ -536,15 +528,8 @@ uint32_t fpga_core_read(uint32_t offset)
int fpga_io_init()
{
- if ((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) return -1;
-
- map_base = (uint32_t*)mmap(0, FPGA_REG_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, FPGA_REG_BASE);
- if (map_base == (void *)-1)
- {
- printf("Unable to mmap(/dev/mem)\n");
- close(fd);
- return -1;
- }
+ map_base = (uint32_t*)shmem_map(FPGA_REG_BASE, FPGA_REG_SIZE);
+ if (!map_base) return -1;
fpga_gpo_write(0);
return 0;
diff --git a/input.cpp b/input.cpp
index 46b3739..dbd8c66 100644
--- a/input.cpp
+++ b/input.cpp
@@ -916,7 +916,7 @@ int mwd = -1;
static int set_watch()
{
mwd = -1;
- mfd = inotify_init();
+ mfd = inotify_init1(IN_CLOEXEC);
if (mfd < 0)
{
printf("ERR: inotify_init");
@@ -1304,7 +1304,7 @@ static int input_uinp_setup()
{
struct uinput_user_dev uinp;
- if (!(uinp_fd = open("/dev/uinput", O_WRONLY | O_NDELAY)))
+ if (!(uinp_fd = open("/dev/uinput", O_WRONLY | O_NDELAY | O_CLOEXEC)))
{
printf("Unable to open /dev/uinput\n");
uinp_fd = -1;
@@ -3064,10 +3064,10 @@ int input_test(int getchar)
unlink(CMD_FIFO);
mkfifo(CMD_FIFO, 0666);
- pool[NUMDEV+1].fd = open(CMD_FIFO, O_RDWR | O_NONBLOCK);
+ pool[NUMDEV+1].fd = open(CMD_FIFO, O_RDWR | O_NONBLOCK | O_CLOEXEC);
pool[NUMDEV+1].events = POLLIN;
- pool[NUMDEV + 2].fd = open(LED_MONITOR, O_RDONLY);
+ pool[NUMDEV + 2].fd = open(LED_MONITOR, O_RDONLY | O_CLOEXEC);
pool[NUMDEV + 2].events = POLLPRI;
state++;
@@ -3095,7 +3095,7 @@ int input_test(int getchar)
{
memset(&input[n], 0, sizeof(input[n]));
sprintf(input[n].devname, "/dev/input/%s", de->d_name);
- int fd = open(input[n].devname, O_RDWR);
+ int fd = open(input[n].devname, O_RDWR | O_CLOEXEC);
//printf("open(%s): %d\n", input[n].devname, fd);
if (fd > 0)
diff --git a/scaler.cpp b/scaler.cpp
index 3ece5e6..ca80e19 100644
--- a/scaler.cpp
+++ b/scaler.cpp
@@ -14,10 +14,10 @@ with help from the MiSTer contributors including Grabulosaure
#include
#include
-#include
#include
#include "scaler.h"
+#include "shmem.h"
mister_scaler * mister_scaler_init()
@@ -32,11 +32,9 @@ mister_scaler * mister_scaler_init()
//printf("map_start = %d map_off=%d offset=%d\n",map_start,ms->map_off,offset);
unsigned char *buffer;
- ms->fd=open("/dev/mem", O_RDONLY, S_IRUSR | S_IWUSR);
- ms->map=(char *)mmap(NULL, ms->num_bytes+ms->map_off,PROT_READ, MAP_SHARED, ms->fd, map_start);
- if (ms->map==MAP_FAILED)
+ ms->map=(char *)shmem_map(map_start, ms->num_bytes+ms->map_off);
+ if (!ms->map)
{
- printf("problem MAP_FAILED\n");
mister_scaler_free(ms);
return NULL;
}
@@ -68,8 +66,7 @@ mister_scaler * mister_scaler_init()
void mister_scaler_free(mister_scaler *ms)
{
- munmap(ms->map,ms->num_bytes+ms->map_off);
- close(ms->fd);
+ shmem_unmap(ms->map,ms->num_bytes+ms->map_off);
free(ms);
}
diff --git a/scaler.h b/scaler.h
index 00b67aa..c69a0bf 100644
--- a/scaler.h
+++ b/scaler.h
@@ -15,7 +15,6 @@ typedef struct {
char *map;
int num_bytes;
int map_off;
- int fd;
} mister_scaler;
#define MISTER_SCALER_BASEADDR 0x20000000
diff --git a/shmem.cpp b/shmem.cpp
new file mode 100644
index 0000000..ec6acaa
--- /dev/null
+++ b/shmem.cpp
@@ -0,0 +1,73 @@
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+#include "shmem.h"
+
+static int memfd = -1;
+
+void *shmem_map(uint32_t address, uint32_t size)
+{
+ if (memfd < 0)
+ {
+ memfd = open("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC);
+ if (memfd == -1)
+ {
+ printf("Error: Unable to open /dev/mem!\n");
+ return 0;
+ }
+ }
+
+ void *res = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, memfd, address);
+ if (res == (void *)-1)
+ {
+ printf("Error: Unable to mmap (0x%X, %d)!\n", address, size);
+ return 0;
+ }
+
+ return res;
+}
+
+int shmem_unmap(void* map, uint32_t size)
+{
+ if (munmap(map, size) < 0)
+ {
+ printf("Error: Unable to unmap(0x%X, %d)!\n", (uint32_t)map, size);
+ return 0;
+ }
+
+ return 1;
+}
+
+int shmem_put(uint32_t address, uint32_t size, void *buf)
+{
+ void *shmem = shmem_map(address, size);
+ if (shmem)
+ {
+ memcpy(shmem, buf, size);
+ shmem_unmap(shmem, size);
+ }
+
+ return shmem != 0;
+}
+
+int shmem_get(uint32_t address, uint32_t size, void *buf)
+{
+ void *shmem = shmem_map(address, size);
+ if (shmem)
+ {
+ memcpy(buf, shmem, size);
+ shmem_unmap(shmem, size);
+ }
+
+ return shmem != 0;
+}
diff --git a/shmem.h b/shmem.h
new file mode 100644
index 0000000..865bc1b
--- /dev/null
+++ b/shmem.h
@@ -0,0 +1,13 @@
+
+#include
+
+#ifndef SHMEM_H
+#define SHMEM_H
+
+void *shmem_map(uint32_t address, uint32_t size);
+int shmem_unmap(void* map, uint32_t size);
+int shmem_put(uint32_t address, uint32_t size, void *buf);
+int shmem_get(uint32_t address, uint32_t size, void *buf);
+
+#define fpga_mem(x) (0x20000000 | ((x) & 0x1FFFFFFF))
+#endif
diff --git a/support/arcade/mra_loader.cpp b/support/arcade/mra_loader.cpp
index fb049c0..7831657 100644
--- a/support/arcade/mra_loader.cpp
+++ b/support/arcade/mra_loader.cpp
@@ -4,7 +4,6 @@
#include
#include
#include
-#include
#include "../../sxmlc.h"
#include "../../user_io.h"
@@ -13,6 +12,7 @@
#include "../../menu.h"
#include "../../fpga_io.h"
#include "../../lib/md5/md5.h"
+#include "../../shmem.h"
#include "buffer.h"
#include "mra_loader.h"
@@ -292,33 +292,6 @@ static int rom_patch(const uint8_t *buf, int offset, uint16_t len, int dataop)
return 1;
}
-static void send_to_ddr(uint32_t address, void* buf, uint32_t len)
-{
- int memfd = open("/dev/mem", O_RDWR | O_SYNC);
- if (memfd == -1)
- {
- printf("Unable to open /dev/mem!\n");
- return;
- }
-
- //make sure it's in FPGA address space
- uint32_t map_addr = 0x20000000 | address;
-
- void *base = mmap(0, len, PROT_READ | PROT_WRITE, MAP_SHARED, memfd, map_addr);
- if (base == (void *)-1)
- {
- printf("Unable to mmap (0x%X, %d)!\n", map_addr, len);
- close(memfd);
- return;
- }
-
- memcpy(base, buf, len);
- munmap(base, len);
-
- close(memfd);
- return;
-}
-
static void rom_finish(int send, uint32_t address, int index)
{
if (romlen[0] && romdata)
@@ -336,7 +309,7 @@ static void rom_finish(int send, uint32_t address, int index)
if (address)
{
- send_to_ddr(address, data, len);
+ shmem_put(fpga_mem(address), len, data);
}
else
{
diff --git a/support/minimig/minimig_share.cpp b/support/minimig/minimig_share.cpp
index 14898ea..1961c02 100644
--- a/support/minimig/minimig_share.cpp
+++ b/support/minimig/minimig_share.cpp
@@ -5,7 +5,6 @@
#include
#include
#include
-#include
#include
#include