Lucio Andrés Illanes Albornoz (arab, vxp) 92254a
diff --git a/src/init/psx_init_cwd.c b/src/init/psx_init_cwd.c
Lucio Andrés Illanes Albornoz (arab, vxp) 92254a
index 924b1b8..02846ba 100644
Lucio Andrés Illanes Albornoz (arab, vxp) 92254a
--- a/src/init/psx_init_cwd.c
Lucio Andrés Illanes Albornoz (arab, vxp) 92254a
+++ b/src/init/psx_init_cwd.c
Lucio Andrés Illanes Albornoz (arab, vxp) 92254a
@@ -17,7 +17,7 @@
Lucio Andrés Illanes Albornoz (arab, vxp) 92254a
 #ifndef __PSX_DEFAULT_ROOT_DIRECTORY
Lucio Andrés Illanes Albornoz (arab, vxp) 92254a
 #define __PSX_DEFAULT_ROOT_DIRECTORY { \
Lucio Andrés Illanes Albornoz (arab, vxp) 92254a
 				'\\','?','?','\\', \
Lucio Andrés Illanes Albornoz (arab, vxp) 92254a
-				'C',':','\\','m','i','d','i','p','i','x'}
Lucio Andrés Illanes Albornoz (arab, vxp) 92254a
+				'Z',':','\\'}
Lucio Andrés Illanes Albornoz (arab, vxp) 92254a
 #endif
Lucio Andrés Illanes Albornoz (arab, vxp) 92254a
 
Lucio Andrés Illanes Albornoz (arab, vxp) 92254a
 int32_t __psx_init_cwd(void)
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
diff --git a/project/common.mk b/project/common.mk
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
index 4390578..fa6df57 100644
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
--- a/project/common.mk
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+++ b/project/common.mk
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
@@ -102,6 +102,7 @@ COMMON_SRCS = \
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 	src/kernel/_systime.c \
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 	src/kernel/_uname.c \
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 	src/mman/_mmap.c \
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	src/mman/_mprotect.c \
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 	src/mman/_mremap.c \
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 	src/mman/_munmap.c \
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 	src/mount/_mount.c \
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
diff --git a/src/init/psx_dev_wip.c b/src/init/psx_dev_wip.c
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
index fe319bc..3915590 100644
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
--- a/src/init/psx_dev_wip.c
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+++ b/src/init/psx_dev_wip.c
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
@@ -89,6 +89,7 @@ static void __populate_syscall_vtbl(void)
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 	/* mman */
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 	set_syscall_pointer(mmap);
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	set_syscall_pointer(mprotect);
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 	set_syscall_pointer(mremap);
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 	set_syscall_pointer(munmap);
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
@@ -263,6 +264,7 @@ static void __populate_strace_vtbl(void)
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 	/* mman */
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 	set_strace_pointer(mmap);
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	set_strace_pointer(mprotect);
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 	set_strace_pointer(mremap);
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 	set_strace_pointer(munmap);
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
diff --git a/src/internal/psx.h b/src/internal/psx.h
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
index 1791bda..21efa46 100644
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
--- a/src/internal/psx.h
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+++ b/src/internal/psx.h
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
@@ -97,6 +97,7 @@ typedef intptr_t	__sys_routine(uname)(struct __utsname *);
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 /* mman */
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 typedef void *		__sys_routine(mmap)(void * addr, size_t length, int prot, int flags, int,off_t offset);
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+typedef intptr_t	__sys_routine(mprotect)(void * addr, size_t length, int prot);
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 typedef void *		__sys_routine(mremap)(void * mapaddr, size_t mapsize, size_t newsize, int flags);
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 typedef intptr_t	__sys_routine(munmap)(void * addr, size_t length);
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
@@ -270,6 +271,7 @@ __sys_interface(uname);
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 /* mman */
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 __sys_interface(mmap);
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+__sys_interface(mprotect);
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 __sys_interface(mremap);
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 __sys_interface(munmap);
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
diff --git a/src/internal/psx_strace.c b/src/internal/psx_strace.c
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
index 6d39093..45e7ecd 100644
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
--- a/src/internal/psx_strace.c
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+++ b/src/internal/psx_strace.c
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
@@ -859,6 +859,22 @@ void * __strace_mmap(void * addr,size_t length,int prot,int flags,int fd,off_t o
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 }
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+intptr_t __strace_mprotect(void * addr,size_t length,int prot)
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+{
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	struct __strace_params params = {
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+		__STRACE_MPROTECT,3,
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+		{__STRACE_SINTEGER,0},
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+		{{__STRACE_POINTER,(intptr_t)addr},
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+		 {__STRACE_SINTEGER,length},
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+		 {__STRACE_SINTEGER,prot}}};
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	if (!addr) params.params[0].type = __STRACE_SINTEGER;
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	params.ret.value = (intptr_t)__sys_mprotect(addr,length,prot);
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	__strace(&params);
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	return (void *)params.ret.value;
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+}
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 void * __strace_mremap(void * mapaddr, size_t mapsize, size_t newsize, int flags)
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 {
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 	struct __strace_params params = {
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
diff --git a/src/internal/psx_strace.h b/src/internal/psx_strace.h
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
index 67eecce..293cfdf 100644
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
--- a/src/internal/psx_strace.h
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+++ b/src/internal/psx_strace.h
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
@@ -60,6 +60,7 @@ enum __strace_sysids {
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 	__STRACE_SYSINFO,
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 	__STRACE_UNAME,
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 	__STRACE_MMAP,
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	__STRACE_MPROTECT,
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 	__STRACE_MREMAP,
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 	__STRACE_MUNMAP,
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 	__STRACE_MOUNT,
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
@@ -197,6 +198,7 @@ enum __strace_sysids {
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 	"sysinfo",			\
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 	"uname",			\
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 	"mmap",				\
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	"mprotect",			\
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 	"mremap",			\
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 	"munmap",			\
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 	"mount",			\
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
@@ -346,6 +348,7 @@ __strace_interface(uname);
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 /* mman */
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 __strace_interface(mmap);
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+__strace_interface(mprotect);
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 __strace_interface(mremap);
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 __strace_interface(munmap);
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
diff --git a/src/mman/_mmap.c b/src/mman/_mmap.c
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
index 13762e3..27ea942 100644
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
--- a/src/mman/_mmap.c
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+++ b/src/mman/_mmap.c
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
@@ -95,7 +95,9 @@ void * __sys_mmap(
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 		return (void *)__psx_sig_epilog(m.tlca,-EINVAL,EPSXONLY);
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 	/* protection */
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
-	if (__psx_convert_flags_to_native(
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	if (!prot)
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+		m.cprot = NT_PAGE_NOACCESS;
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	else if (__psx_convert_flags_to_native(
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 			__mmap_section_prot,
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 			prot,&m.cprot,0))
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
 		return (void *)__psx_sig_epilog(m.tlca,-EINVAL,EPSXONLY);
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
diff --git a/src/mman/_mprotect.c b/src/mman/_mprotect.c
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
new file mode 100644
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
index 0000000..7ac46d3
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
--- /dev/null
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+++ b/src/mman/_mprotect.c
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
@@ -0,0 +1,61 @@
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+/********************************************************/
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+/*  psxscl: a thread-safe system call layer library     */
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+/*  Copyright (C) 2013--2016  Z. Gilboa                 */
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+/*  Released under GPLv2 and GPLv3; see COPYING.PSXSCL. */
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+/********************************************************/
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+#include <ntapi/ntapi.h>
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+#include "psx_systypes.h"
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+#include "psx_tlca.h"
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+#include "psx_errno.h"
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+#include "psx_flags.h"
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+#include "psx_mman.h"
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+#include "psx_signal.h"
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+#include "psx_ofd.h"
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+#include "psx_acl.h"
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+#include "psx.h"
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+static const struct __flag_set __mmap_section_prot[] = {
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	{PROT_READ,	NT_PAGE_READONLY},
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	{PROT_WRITE,	NT_PAGE_READWRITE},
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	{PROT_EXEC,	NT_PAGE_EXECUTE},
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	{PROT_NONE,	0},
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	{0,		0}};
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+__psx_api
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+intptr_t __sys_mprotect(
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	void *	addr,
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	size_t	length,
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	int	prot)
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+{
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	int32_t			status;
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	struct __psx_tlca *	tlca;
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	struct __psx_ctx *	ctx;
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	uint32_t		cprot, protect_type_old;
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	/* prolog */
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	tlca = __tlca_self();
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	if (!(ctx = __tlca_shared_ctx(tlca))) return 0;
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	__psx_sig_prolog(tlca);
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	/* protection */
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	if (!prot)
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+		cprot = NT_PAGE_NOACCESS;
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	else if (__psx_convert_flags_to_native(
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+			__mmap_section_prot,
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+			prot,&cprot,0))
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+		return __psx_sig_epilog(tlca,-EINVAL,EPSXONLY);
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	if (cprot & NT_PAGE_READWRITE)
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+		cprot = NT_PAGE_READWRITE;
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	if ((status = __ntapi->zw_protect_virtual_memory(
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+			NT_CURRENT_PROCESS_HANDLE,
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+			&addr, &length,
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+			cprot, &protect_type_old)))
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+		return __psx_sig_epilog(tlca,-ENOMEM,status);
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+	return __psx_sig_epilog(tlca,0,status);
Lucio Andrés Illanes Albornoz (arab, vxp) 3aebd9
+}