Skip to content

Commit c6168cd

Browse files
committed
posix: implement mprotect()
Provide a stub for mprotect() to satisfy the requirement for the base definitions / system interfaces and subsequently PSE51, PSE52, PSE52, etc. Currently, Zephyr's virtual memory-management API does not seem to support modifying memory protection bits after pages have already been mapped. Signed-off-by: Chris Friedt <[email protected]>
1 parent f4f364c commit c6168cd

File tree

5 files changed

+37
-2
lines changed

5 files changed

+37
-2
lines changed

include/zephyr/posix/posix_features.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@
7777
#define _POSIX_MEMLOCK_RANGE _POSIX_VERSION
7878
#endif
7979

80-
/* #define _POSIX_MEMORY_PROTECTION (-1L) */
80+
#ifdef CONFIG_POSIX_MEMORY_PROTECTION
81+
#define _POSIX_MEMORY_PROTECTION _POSIX_VERSION
82+
#endif
8183

8284
#ifdef CONFIG_POSIX_MESSAGE_PASSING
8385
#define _POSIX_MESSAGE_PASSING _POSIX_VERSION

include/zephyr/posix/sys/sysconf.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ enum {
159159
COND_CODE_1(CONFIG_POSIX_MEMLOCK, (_POSIX_MEMLOCK), (-1L))
160160
#define __z_posix_sysconf_SC_MEMLOCK_RANGE \
161161
COND_CODE_1(CONFIG_POSIX_MEMLOCK_RANGE, (_POSIX_MEMLOCK_RANGE), (-1L))
162-
#define __z_posix_sysconf_SC_MEMORY_PROTECTION (-1L)
162+
#define __z_posix_sysconf_SC_MEMORY_PROTECTION \
163+
COND_CODE_1(CONFIG_POSIX_MEMORY_PROTECTION, (_POSIX_MEMORY_PROTECTION), (-1L))
163164
#define __z_posix_sysconf_SC_MESSAGE_PASSING \
164165
COND_CODE_1(CONFIG_POSIX_MESSAGE_PASSING, (_POSIX_MESSAGE_PASSING), (-1L))
165166
#define __z_posix_sysconf_SC_MONOTONIC_CLOCK \

lib/posix/options/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ zephyr_library_sources_ifdef(CONFIG_POSIX_FILE_SYSTEM fs.c)
5353
zephyr_library_sources_ifdef(CONFIG_POSIX_FSYNC fsync.c)
5454
zephyr_library_sources_ifdef(CONFIG_POSIX_MEMLOCK mlockall.c)
5555
zephyr_library_sources_ifdef(CONFIG_POSIX_MEMLOCK_RANGE mlock.c)
56+
zephyr_library_sources_ifdef(CONFIG_POSIX_MEMORY_PROTECTION mprotect.c)
5657
zephyr_library_sources_ifdef(CONFIG_POSIX_MAPPED_FILES mmap.c)
5758
zephyr_library_sources_ifdef(CONFIG_POSIX_MESSAGE_PASSING mqueue.c)
5859
zephyr_library_sources_ifdef(CONFIG_POSIX_MULTI_PROCESS

lib/posix/options/Kconfig.mem

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,14 @@ config POSIX_MEMLOCK_RANGE
6363

6464
endif
6565

66+
config POSIX_MEMORY_PROTECTION
67+
bool "POSIX memory protection [EXPERIMENTAL]"
68+
select EXPERIMENTAL
69+
help
70+
Select 'y' here and Zephyr will provide support for mprotect().
71+
72+
For more information, please see
73+
https://pubs.opengroup.org/onlinepubs/9699919799.orig/functions/V2_chap02.html
74+
https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_port.html#tag_24_03_04
75+
6676
endmenu

lib/posix/options/mprotect.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) 2024, Tenstorrent AI ULC
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <errno.h>
8+
#include <stddef.h>
9+
#include <sys/types.h>
10+
11+
#include <zephyr/posix/sys/mman.h>
12+
13+
int mprotect(void *addr, size_t len, int prot)
14+
{
15+
ARG_UNUSED(addr);
16+
ARG_UNUSED(len);
17+
ARG_UNUSED(prot);
18+
19+
errno = ENOSYS;
20+
return -1;
21+
}

0 commit comments

Comments
 (0)