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 @@ -17,6 +16,7 @@ #include "../../user_io.h" #include "../../spi.h" #include "../../cfg.h" +#include "../../shmem.h" #include "miminig_fs_messages.h" #define SHMEM_ADDR 0x27FF4000 @@ -236,7 +236,7 @@ static void fill_date(time_t time, int date[3]) time_t ticks = secs * 50; days -= 2922; // Days between 1970 - 01 - 01 and 1978 - 01 - 01 if (days < 0) days = 0; - + date[0] = SWAP_INT(days); date[1] = SWAP_INT(mins); date[2] = SWAP_INT(ticks); @@ -509,10 +509,10 @@ static int process_request(void *reqres_buffer) ExamineFhRequest *req = (ExamineFhRequest*)reqres_buffer; ExamineFhResponse *res = (ExamineFhResponse*)reqres_buffer; sz_res = sizeof(ExamineFhResponse); - + uint32_t key = SWAP_INT(req->arg1); dbg_print(" key: %d\n", key); - + if (open_file_handles.find(key) == open_file_handles.end()) { ret = ERROR_OBJECT_NOT_FOUND; @@ -543,11 +543,11 @@ static int process_request(void *reqres_buffer) ret = ERROR_OBJECT_NOT_FOUND; break; } - + dbg_print(" fn: %s\n", fn); dbg_print(" size: %lld\n", open_file_handles[key].size); dbg_print(" type: %d\n", type); - + res->disk_key = SWAP_INT(disk_key); res->entry_type = SWAP_INT(type); res->size = SWAP_INT(size); @@ -767,7 +767,7 @@ static int process_request(void *reqres_buffer) ret = ERROR_OBJECT_NOT_FOUND; break; } - + strcpy(buf, getFullPath(buf)); const char *fp2 = getFullPath(cp2); @@ -862,13 +862,13 @@ static int process_request(void *reqres_buffer) uint32_t key1 = SWAP_INT(req->key1); uint32_t key2 = SWAP_INT(req->key2); - + if ((locks.find(key1) == locks.end()) || (locks.find(key2) == locks.end())) { ret = LOCK_DIFFERENT; break; } - + if (locks[key1].path == locks[key2].path) { ret = LOCK_SAME; @@ -891,23 +891,12 @@ static int process_request(void *reqres_buffer) return sz_res; } -static void share_init() -{ - int fd; - if ((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) return; - - shmem = (uint8_t*)mmap(0, SHMEM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, SHMEM_ADDR); - close(fd); - - if (shmem == (uint8_t *)-1) printf("minimig_setup_shmem: Unable to mmap(/dev/mem)\n"); - return; -} - void minimig_share_poll() { if (!shmem) { - share_init(); + shmem = (uint8_t *)shmem_map(SHMEM_ADDR, SHMEM_SIZE); + if (!shmem) shmem = (uint8_t *)-1; } else if(shmem != (uint8_t *)-1) { diff --git a/support/neogeo/neogeo_loader.cpp b/support/neogeo/neogeo_loader.cpp index c193193..5d66058 100644 --- a/support/neogeo/neogeo_loader.cpp +++ b/support/neogeo/neogeo_loader.cpp @@ -5,13 +5,13 @@ #include #include #include // clock_gettime, CLOCK_REALTIME -#include #include "neogeo_loader.h" #include "../../sxmlc.h" #include "../../user_io.h" #include "../../fpga_io.h" #include "../../osd.h" #include "../../menu.h" +#include "../../shmem.h" struct NeoFile { @@ -204,14 +204,6 @@ static uint32_t load_crom_to_mem(const char* path, const char* name, uint8_t ind return 0; } - int memfd = open("/dev/mem", O_RDWR | O_SYNC); - if (memfd == -1) - { - printf("Unable to open /dev/mem!\n"); - FileClose(&f); - return 0; - } - size *= 2; FileSeek(&f, offset, SEEK_SET); @@ -229,11 +221,9 @@ static uint32_t load_crom_to_mem(const char* path, const char* name, uint8_t ind if (partsz > LOADBUF_SZ) partsz = LOADBUF_SZ; //printf("partsz=%d, map_addr=0x%X\n", partsz, map_addr); - void *base = mmap(0, partsz, PROT_READ | PROT_WRITE, MAP_SHARED, memfd, map_addr); - if (base == (void *)-1) + void *base = shmem_map(map_addr, partsz); + if (!base) { - printf("Unable to mmap (0x%X, %d)!\n", map_addr, partsz); - close(memfd); FileClose(&f); return 0; } @@ -243,12 +233,11 @@ static uint32_t load_crom_to_mem(const char* path, const char* name, uint8_t ind ProgressMessage("Loading", name, size - (remain - partsz), size); - munmap(base, partsz); + shmem_unmap(base, partsz); remain -= partsz; map_addr += partsz; } - close(memfd); FileClose(&f); ProgressMessage(); @@ -274,14 +263,6 @@ static uint32_t load_rom_to_mem(const char* path, const char* name, uint8_t neo_ return 0; } - int memfd = open("/dev/mem", O_RDWR | O_SYNC); - if (memfd == -1) - { - printf("Unable to open /dev/mem!\n"); - FileClose(&f); - return 0; - } - FileSeek(&f, offset, SEEK_SET); printf("ROM %s (offset %u, size %u, exp %u, type %u, addr %u) with index %u\n", name, offset, size, expand, neo_file_type, addr, index); @@ -302,11 +283,9 @@ static uint32_t load_rom_to_mem(const char* path, const char* name, uint8_t neo_ if (partszf > LOADBUF_SZ) partszf = LOADBUF_SZ; //printf("partsz=%d, map_addr=0x%X\n", partsz, map_addr); - void *base = mmap(0, partsz, PROT_READ | PROT_WRITE, MAP_SHARED, memfd, map_addr); - if (base == (void *)-1) + void *base = shmem_map(map_addr, partsz); + if (!base) { - printf("Unable to mmap (0x%X, %d)!\n", map_addr, partsz); - close(memfd); FileClose(&f); return 0; } @@ -332,12 +311,11 @@ static uint32_t load_rom_to_mem(const char* path, const char* name, uint8_t neo_ ProgressMessage("Loading", name, size - (remain - partsz), size); - munmap(base, partsz); + shmem_unmap(base, partsz); remain -= partsz; map_addr += partsz; } - close(memfd); FileClose(&f); ProgressMessage(); diff --git a/support/x86/x86.cpp b/support/x86/x86.cpp index 768af3a..818398f 100644 --- a/support/x86/x86.cpp +++ b/support/x86/x86.cpp @@ -30,13 +30,13 @@ #include #include #include -#include #include #include "../../spi.h" #include "../../user_io.h" #include "../../file_io.h" #include "../../fpga_io.h" +#include "../../shmem.h" #include "x86_share.h" #include "x86_ide.h" #include "x86_cdrom.h" @@ -170,33 +170,16 @@ void x86_dma_recvbuf(uint32_t address, uint32_t length, uint32_t *data) DisableIO(); } -static void* shmem_init(int offset, int size) -{ - int fd; - if ((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) return 0; - - void *shmem = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, SHMEM_ADDR + offset); - close(fd); - - if (shmem == (void *)-1) - { - printf("ao486 share_init: Unable to mmap(/dev/mem)\n"); - return 0; - } - - return shmem; -} - static int load_bios(const char* name, uint8_t index) { printf("BIOS: %s\n", name); - void *buf = shmem_init(index ? 0xC0000 : 0xF0000, BIOS_SIZE); + void *buf = shmem_map(SHMEM_ADDR + (index ? 0xC0000 : 0xF0000), BIOS_SIZE); if (!buf) return 0; memset(buf, 0, BIOS_SIZE); FileLoad(name, buf, BIOS_SIZE); - munmap(buf, BIOS_SIZE); + shmem_unmap(buf, BIOS_SIZE); return 1; } diff --git a/support/x86/x86_cdrom.cpp b/support/x86/x86_cdrom.cpp index 98145ab..684c6c2 100644 --- a/support/x86/x86_cdrom.cpp +++ b/support/x86/x86_cdrom.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #include #include @@ -23,7 +22,7 @@ #include "x86_ide.h" #include "x86_cdrom.h" -#if 0 +#if 0 #define dbg_printf printf #define dbg_print_regs ide_print_regs #define dbg_hexdump hexdump @@ -249,11 +248,11 @@ static const char* load_chd_file(drive_t *drv, const char *chdfile) //Borrow the cd.h "toc_t" and mister_chd* parse function. Then translate the toc_t to drive_t+track_t. //TODO: abstract all the bin/cue+chd+iso parsing and reading into a shared class // - + const char *ext = chdfile+strlen(chdfile)-4; uint32_t total_sector_size = 0; - + if (strncasecmp(".chd", ext, 4)) { //Not a CHD @@ -281,7 +280,7 @@ static const char* load_chd_file(drive_t *drv, const char *chdfile) for(int i = 0; i <= tmpTOC.last; i++) { cd_track_t *chd_track = &tmpTOC.tracks[i]; - track_t *trk = &drv->track[i]; + track_t *trk = &drv->track[i]; trk->number = i+1; trk->sectorSize = chd_track->sector_size; if (chd_track->type) @@ -302,9 +301,9 @@ static const char* load_chd_file(drive_t *drv, const char *chdfile) } //Add the lead-out track - + track_t *lead_out = &drv->track[drv->track_cnt]; - lead_out->number = drv->track_cnt+1; + lead_out->number = drv->track_cnt+1; lead_out->attr = 0; lead_out->start = tmpTOC.tracks[tmpTOC.last].end; lead_out->length = 0; @@ -981,7 +980,7 @@ void cdrom_read(ide_config *ide) dbg_printf("** partial CD read\n"); } - if (ide->state == IDE_STATE_INIT_RW && !drive->chd_f) + if (ide->state == IDE_STATE_INIT_RW && !drive->chd_f) { uint32_t pos = ide->regs.pkt_lba * ide->drive[ide->regs.drv].track[ide->drive[ide->regs.drv].data_num].sectorSize; @@ -998,13 +997,13 @@ void cdrom_read(ide_config *ide) hdr = 0; } uint32_t d_offset = 0; - + if (ide->state == IDE_STATE_INIT_RW) { - drive->chd_last_partial_lba = ide->regs.pkt_lba; + drive->chd_last_partial_lba = ide->regs.pkt_lba; } - for(uint32_t i = 0; i < cnt; i++) + for(uint32_t i = 0; i < cnt; i++) { if (mister_chd_read_sector(drive->chd_f, drive->chd_last_partial_lba + drive->track[drive->data_num].chd_offset, d_offset, hdr, 2048, ide_buf, drive->chd_hunkbuf, &drive->chd_hunknum) != CHDERR_NONE) diff --git a/support/x86/x86_ide.cpp b/support/x86/x86_ide.cpp index a0120a1..a591672 100644 --- a/support/x86/x86_ide.cpp +++ b/support/x86/x86_ide.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #include #include diff --git a/support/x86/x86_share.cpp b/support/x86/x86_share.cpp index c31b8af..373416b 100644 --- a/support/x86/x86_share.cpp +++ b/support/x86/x86_share.cpp @@ -31,7 +31,6 @@ #include #include #include -#include #include #include @@ -42,6 +41,7 @@ #include "../../user_io.h" #include "../../file_io.h" #include "../../cfg.h" +#include "../../shmem.h" #define SHMEM_ADDR 0x300CE000 #define SHMEM_SIZE 0x2000 @@ -1117,23 +1117,12 @@ static int process_request(void *reqres_buffer) return reslen; } -static void share_init() -{ - int fd; - if ((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) return; - - shmem = (uint8_t*)mmap(0, SHMEM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, SHMEM_ADDR); - close(fd); - - if (shmem == (uint8_t *)-1) printf("x86_setup_shmem: Unable to mmap(/dev/mem)\n"); - return; -} - void x86_share_poll() { if (!shmem) { - share_init(); + shmem = (uint8_t *)shmem_map(SHMEM_ADDR, SHMEM_SIZE); + if (!shmem) shmem = (uint8_t *)-1; } else if (shmem != (uint8_t *)-1) { diff --git a/user_io.cpp b/user_io.cpp index 34d7e47..dcf190c 100644 --- a/user_io.cpp +++ b/user_io.cpp @@ -9,7 +9,6 @@ #include #include #include -#include #include "lib/lodepng/lodepng.h" @@ -33,6 +32,7 @@ #include "cheats.h" #include "video.h" #include "audio.h" +#include "shmem.h" #include "support.h" @@ -904,16 +904,8 @@ uint16_t sdram_sz(int sz) { int res = 0; - int fd; - if ((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) return 0; - - 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 0; - } + void* buf = shmem_map(0x1FFFF000, 0x1000); + if (!buf) return 0; volatile uint8_t* par = (volatile uint8_t*)buf; par += 0xF00; @@ -938,8 +930,7 @@ uint16_t sdram_sz(int sz) } } - munmap(buf, 0x1000); - close(fd); + shmem_unmap(buf, 0x1000); return res; } @@ -947,16 +938,8 @@ uint16_t altcfg(int alt) { int res = 0; - int fd; - if ((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) return 0; - - 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 0; - } + void* buf = shmem_map(0x1FFFF000, 0x1000); + if (!buf) return 0; volatile uint8_t* par = (volatile uint8_t*)buf; par += 0xF04; @@ -981,8 +964,7 @@ uint16_t altcfg(int alt) } } - munmap(buf, 0x1000); - close(fd); + shmem_unmap(buf, 0x1000); return res; } @@ -1952,7 +1934,6 @@ static int process_ss(const char *rom_name) { static char ss_name[1024] = {}; static char *ss_sufx = 0; - static int memfd = -1; static uint32_t ss_cnt[4] = {}; static void *base[4] = {}; @@ -1960,24 +1941,14 @@ static int process_ss(const char *rom_name) if (rom_name) { - if (memfd < 0) - { - memfd = open("/dev/mem", O_RDWR | O_SYNC); - if (memfd == -1) - { - printf("Unable to open /dev/mem!\n"); - return 0; - } - } - uint32_t len = ss_size; uint32_t map_addr = ss_base; fileTYPE f = {}; for (int i = 0; i < 4; i++) { - if (!base[i]) base[i] = mmap(0, len, PROT_READ | PROT_WRITE, MAP_SHARED, memfd, map_addr); - if (base[i] == (void *)-1) + if (!base[i]) base[i] = shmem_map(map_addr, len); + if (!base[i]) { printf("Unable to mmap (0x%X, %d)!\n", map_addr, len); } @@ -2026,36 +1997,33 @@ static int process_ss(const char *rom_name) if (ss_timer && !CheckTimer(ss_timer)) return 0; ss_timer = GetTimer(1000); - if (memfd >= 0) + fileTYPE f = {}; + for (int i = 0; i < 4; i++) { - fileTYPE f = {}; - for (int i = 0; i < 4; i++) + if (base[i]) { - if (base[i] && (base[i] != (void *)-1)) + uint32_t curcnt = ((uint32_t*)(base[i]))[0]; + uint32_t size = ((uint32_t*)(base[i]))[1]; + + if (curcnt != ss_cnt[i]) { - uint32_t curcnt = ((uint32_t*)(base[i]))[0]; - uint32_t size = ((uint32_t*)(base[i]))[1]; - - if (curcnt != ss_cnt[i]) + ss_cnt[i] = curcnt; + if (size) size = (size + 2) * 4; + if (size > 0 && size <= ss_size) { - ss_cnt[i] = curcnt; - if (size) size = (size + 2) * 4; - if (size > 0 && size <= ss_size) - { - MenuHide(); - Info("Saving the state", 500); + MenuHide(); + Info("Saving the state", 500); - *ss_sufx = i + '1'; - if (FileOpenEx(&f, ss_name, O_CREAT | O_TRUNC | O_RDWR | O_SYNC)) - { - int ret = FileWriteAdv(&f, base[i], size); - FileClose(&f); - printf("Wrote %d bytes to file: %s\n", ret, ss_name); - } - else - { - printf("Unable to create file: %s\n", ss_name); - } + *ss_sufx = i + '1'; + if (FileOpenEx(&f, ss_name, O_CREAT | O_TRUNC | O_RDWR | O_SYNC)) + { + int ret = FileWriteAdv(&f, base[i], size); + FileClose(&f); + printf("Wrote %d bytes to file: %s\n", ret, ss_name); + } + else + { + printf("Unable to create file: %s\n", ss_name); } } } diff --git a/video.cpp b/video.cpp index 11cd470..1af13da 100644 --- a/video.cpp +++ b/video.cpp @@ -2,7 +2,6 @@ #include #include #include -#include #include #include #include @@ -20,6 +19,7 @@ #include "menu.h" #include "video.h" #include "input.h" +#include "shmem.h" #include "support.h" #include "lib/imlib2/Imlib2.h" @@ -576,16 +576,11 @@ static void fb_init() { if (!fb_base) { - int fd = open("/dev/mem", O_RDWR | O_SYNC); - if (fd == -1) return; - - fb_base = (volatile uint32_t*)mmap(0, FB_SIZE * 4 * 3, PROT_READ | PROT_WRITE, MAP_SHARED, fd, FB_ADDR); - if (fb_base == (void *)-1) + fb_base = (volatile uint32_t*)shmem_map(FB_ADDR, FB_SIZE * 4 * 3); + if (!fb_base) { printf("Unable to mmap FB!\n"); - fb_base = 0; } - close(fd); } spi_uio_cmd16(UIO_SET_FBUF, 0); } @@ -1068,7 +1063,7 @@ static uint64_t getus() static void vs_wait() { - int fb = open("/dev/fb0", O_RDWR); + int fb = open("/dev/fb0", O_RDWR | O_CLOEXEC); int zero = 0; uint64_t t1, t2; if (ioctl(fb, FBIO_WAITFORVSYNC, &zero) == -1) @@ -1139,7 +1134,7 @@ static Imlib_Image load_bg() if (PathIsDir(bgdir)) { - int rndfd = open("/dev/urandom", O_RDONLY); + int rndfd = open("/dev/urandom", O_RDONLY | O_CLOEXEC); if (rndfd >= 0) { uint32_t rnd; @@ -1383,7 +1378,7 @@ int video_chvt(int num) { cur_vt = num; int fd; - if ((fd = open("/dev/tty0", O_RDONLY)) >= 0) + if ((fd = open("/dev/tty0", O_RDONLY | O_CLOEXEC)) >= 0) { if (ioctl(fd, VT_ACTIVATE, cur_vt)) printf("ioctl VT_ACTIVATE fails\n"); if (ioctl(fd, VT_WAITACTIVE, cur_vt)) printf("ioctl VT_WAITACTIVE fails\n");