Skip to content

Commit

Permalink
Initial implement mmap/munmap functions
Browse files Browse the repository at this point in the history
  • Loading branch information
d3m3vilurr committed Oct 6, 2019
1 parent 53e6192 commit abff0b3
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 4 deletions.
2 changes: 1 addition & 1 deletion newlib/libc/sys/vita/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ NET_OBJS = gethostbyname.o getaddrinfo.o freeaddrinfo.o getservbyname.o inet_nto
DIRENT_OBJS = closedir.o opendir.o readdir.o readdir_r.o rewinddir.o seekdir.o telldir.o

NET_SOURCES = net/gethostbyname.c net/getaddrinfo.c net/freeaddrinfo.c net/getservbyname.c net/inet_ntop.c net/inet_ntoa.c net/inet_netof.c net/inet_pton.c net/inet_lnaof.c net/inet_addr.c net/inet_network.c net/inet_net_ntop.c net/inet_aton.c net/inet_net_pton.c net/inet_makeaddr.c
lib_a_SOURCES = syscalls.c sbrk.c threading.c mlock.c io.c socket.c dup.c select.c error.c dirent.c lcltime_r.c ${NET_SOURCES}
lib_a_SOURCES = syscalls.c sbrk.c threading.c mmap.c mlock.c io.c socket.c dup.c select.c error.c dirent.c lcltime_r.c ${NET_SOURCES}
lib_a_LIBADD = ${SOCKET_OBJS} ${NET_OBJS} ${DIRENT_OBJS}
lib_a_CCASFLAGS = $(AM_CCASFLAGS)
lib_a_CFLAGS = $(AM_CFLAGS)
Expand Down
13 changes: 10 additions & 3 deletions newlib/libc/sys/vita/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ am__objects_1 = lib_a-gethostbyname.$(OBJEXT) \
lib_a-inet_net_ntop.$(OBJEXT) lib_a-inet_aton.$(OBJEXT) \
lib_a-inet_net_pton.$(OBJEXT) lib_a-inet_makeaddr.$(OBJEXT)
am_lib_a_OBJECTS = lib_a-syscalls.$(OBJEXT) lib_a-sbrk.$(OBJEXT) \
lib_a-threading.$(OBJEXT) lib_a-mlock.$(OBJEXT) \
lib_a-io.$(OBJEXT) lib_a-socket.$(OBJEXT) lib_a-dup.$(OBJEXT) \
lib_a-threading.$(OBJEXT) lib_a-mmap.$(OBJEXT) \
lib_a-mlock.$(OBJEXT) lib_a-io.$(OBJEXT) \
lib_a-socket.$(OBJEXT) lib_a-dup.$(OBJEXT) \
lib_a-select.$(OBJEXT) lib_a-error.$(OBJEXT) \
lib_a-dirent.$(OBJEXT) lib_a-lcltime_r.$(OBJEXT) \
$(am__objects_1)
Expand Down Expand Up @@ -210,7 +211,7 @@ SOCKET_OBJS = accept.o bind.o connect.o getpeername.o getsockname.o getsockopt.o
NET_OBJS = gethostbyname.o getaddrinfo.o freeaddrinfo.o getservbyname.o inet_ntop.o inet_ntoa.o inet_netof.o inet_pton.o inet_lnaof.o inet_addr.o inet_network.o inet_net_ntop.o inet_aton.o inet_net_pton.o inet_makeaddr.o
DIRENT_OBJS = closedir.o opendir.o readdir.o readdir_r.o rewinddir.o seekdir.o telldir.o
NET_SOURCES = net/gethostbyname.c net/getaddrinfo.c net/freeaddrinfo.c net/getservbyname.c net/inet_ntop.c net/inet_ntoa.c net/inet_netof.c net/inet_pton.c net/inet_lnaof.c net/inet_addr.c net/inet_network.c net/inet_net_ntop.c net/inet_aton.c net/inet_net_pton.c net/inet_makeaddr.c
lib_a_SOURCES = syscalls.c sbrk.c threading.c mlock.c io.c socket.c dup.c select.c error.c dirent.c lcltime_r.c ${NET_SOURCES}
lib_a_SOURCES = syscalls.c sbrk.c threading.c mmap.c mlock.c io.c socket.c dup.c select.c error.c dirent.c lcltime_r.c ${NET_SOURCES}
lib_a_LIBADD = ${SOCKET_OBJS} ${NET_OBJS} ${DIRENT_OBJS}
lib_a_CCASFLAGS = $(AM_CCASFLAGS)
lib_a_CFLAGS = $(AM_CFLAGS)
Expand Down Expand Up @@ -292,6 +293,12 @@ lib_a-threading.o: threading.c
lib_a-threading.obj: threading.c
$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-threading.obj `if test -f 'threading.c'; then $(CYGPATH_W) 'threading.c'; else $(CYGPATH_W) '$(srcdir)/threading.c'; fi`

lib_a-mmap.o: mmap.c
$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-mmap.o `test -f 'mmap.c' || echo '$(srcdir)/'`mmap.c

lib_a-mmap.obj: mmap.c
$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-mmap.obj `if test -f 'mmap.c'; then $(CYGPATH_W) 'mmap.c'; else $(CYGPATH_W) '$(srcdir)/mmap.c'; fi`

lib_a-mlock.o: mlock.c
$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-mlock.o `test -f 'mlock.c' || echo '$(srcdir)/'`mlock.c

Expand Down
83 changes: 83 additions & 0 deletions newlib/libc/sys/vita/mmap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Copyright (C) 2017, David "Davee" Morgan
Copyright (C) 2019, Sunguk Lee
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <sys/mman.h>
#include <string.h>
#include <sys/errno.h>
#include <sys/types.h>
#include <psp2/kernel/sysmem.h>

void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) {
// can't use file
if (fd != -1) {
errno = ENOSYS;
return MAP_FAILED;
}
// can't use WX perm
if ((prot & (PROT_EXEC | PROT_WRITE)) == (PROT_EXEC | PROT_WRITE)) {
errno = EPERM;
return MAP_FAILED;
}

if ((prot & (PROT_WRITE | PROT_READ)) != (PROT_WRITE | PROT_READ)) {
errno = EPERM;
return MAP_FAILED;
}
if ((flags & MAP_PRIVATE) != MAP_PRIVATE) {
errno = ENOSYS;
return MAP_FAILED;
}

// can't use shared mem
if (flags & MAP_SHARED) {
errno = ENOSYS;
return MAP_FAILED;
}

SceUID blkid = sceKernelAllocMemBlock("mmap", SCE_KERNEL_MEMBLOCK_TYPE_USER_RW, length, 0);
if (blkid < 0) {
errno = ENOMEM;
return MAP_FAILED;
}

void *block_addr = NULL;
sceKernelGetMemBlockBase(blkid, &block_addr);

if (flags & (MAP_ANON | MAP_ANONYMOUS))
memset(block_addr, 0, length);

return block_addr;
}

int munmap(void *addr, size_t length) {
SceUID block = sceKernelFindMemBlockByAddr(addr, length);

if (block < 0) {
errno = EINVAL;
return -1;
}

sceKernelFreeMemBlock(block);
return 0;
}
70 changes: 70 additions & 0 deletions newlib/libc/sys/vita/sys/mman.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 vitasdk
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _SYS_MMAN_H_
#define _SYS_MMAN_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <sys/types.h>

#define PROT_NONE 0
#define PROT_READ 1
#define PROT_WRITE 2
#define PROT_EXEC 4

#define MAP_FAILED ((void *) -1)
#define MAP_SHARED 0x01
#define MAP_PRIVATE 0x02
#define MAP_FIXED 0x10
#define MAP_ANON 0x20
#define MAP_ANONYMOUS MAP_ANON

#define MS_ASYNC 1
#define MS_INVALIDATE 2
#define MS_SYNC 4

#define MCL_CURRENT 1
#define MCL_FUTURE 2

#define POSIX_MADV_NORMAL 0
#define POSIX_MADV_RANDOM 1
#define POSIX_MADV_SEQUENTIAL 2
#define POSIX_MADV_WILLNEED 3
#define POSIX_MADV_DONTNEED 4

void *mmap (void *, size_t, int, int, int, off_t);
int munmap (void *, size_t);

#ifdef __cplusplus
}
#endif

#endif

0 comments on commit abff0b3

Please sign in to comment.