-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implement mmap/munmap functions
copy & modified from mucl https://github.com/DaveeFTW/musl/blob/vita/src/internal/vita/mmap.c
- Loading branch information
1 parent
53e6192
commit abff0b3
Showing
4 changed files
with
164 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |