xref: /llvm-project/libc/src/sys/mman/linux/mprotect.cpp (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
147b72404SMichael Jones //===---------- Linux implementation of the POSIX mprotect function -------===//
247b72404SMichael Jones //
347b72404SMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
447b72404SMichael Jones // See https://llvm.org/LICENSE.txt for license information.
547b72404SMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
647b72404SMichael Jones //
747b72404SMichael Jones //===----------------------------------------------------------------------===//
847b72404SMichael Jones 
947b72404SMichael Jones #include "src/sys/mman/mprotect.h"
1047b72404SMichael Jones 
1147b72404SMichael Jones #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
1247b72404SMichael Jones #include "src/__support/common.h"
1347b72404SMichael Jones 
14*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
15204587a3SSiva Chandra Reddy #include "src/errno/libc_errno.h"
1647b72404SMichael Jones #include <sys/syscall.h> // For syscall numbers.
1747b72404SMichael Jones 
18*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
1947b72404SMichael Jones 
2047b72404SMichael Jones // This function is currently linux only. It has to be refactored suitably if
2147b72404SMichael Jones // mprotect is to be supported on non-linux operating systems also.
2247b72404SMichael Jones LLVM_LIBC_FUNCTION(int, mprotect, (void *addr, size_t size, int prot)) {
23b6bc9d72SGuillaume Chatelet   int ret = LIBC_NAMESPACE::syscall_impl<int>(
241801c356SMichael Jones       SYS_mprotect, reinterpret_cast<long>(addr), size, prot);
2547b72404SMichael Jones 
2647b72404SMichael Jones   // A negative return value indicates an error with the magnitude of the
2747b72404SMichael Jones   // value being the error code.
28f0a3954eSMichael Jones   if (ret < 0) {
29f0a3954eSMichael Jones     libc_errno = -ret;
3047b72404SMichael Jones     return -1;
3147b72404SMichael Jones   }
3247b72404SMichael Jones 
3347b72404SMichael Jones   return 0;
3447b72404SMichael Jones }
3547b72404SMichael Jones 
36*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
37