[igt-dev] [PATCH i-g-t v2 2/2] build: provide stub implementation for memfd_create

Lucas De Marchi lucas.demarchi at intel.com
Tue Jul 24 22:20:24 UTC 2018


When libc misses memfd_create(), provide a stub implementation to go
through the syscall() route. Syscall numbers are provided for platforms
currently supported by i-g-t only.

v2: add support to autotools

Signed-off-by: Lucas De Marchi <lucas.demarchi at intel.com>
---
 configure.ac                  |  3 +++
 lib/stubs/syscalls/sys/mman.h | 37 +++++++++++++++++++++++++++++++++++
 meson.build                   |  3 +++
 3 files changed, 43 insertions(+)
 create mode 100644 lib/stubs/syscalls/sys/mman.h

diff --git a/configure.ac b/configure.ac
index 0a5b0425..416a3240 100644
--- a/configure.ac
+++ b/configure.ac
@@ -81,6 +81,9 @@ AC_CHECK_FUNCS(timer_create, [], [
 ])
 AC_SUBST(TIMER_LIBS)
 
+dnl Check for memfd_create
+AC_CHECK_FUNCS(memfd_create)
+
 dnl Check for CPUID
 cpuid="yes"
 AC_TRY_LINK([
diff --git a/lib/stubs/syscalls/sys/mman.h b/lib/stubs/syscalls/sys/mman.h
new file mode 100644
index 00000000..2ac2da6a
--- /dev/null
+++ b/lib/stubs/syscalls/sys/mman.h
@@ -0,0 +1,37 @@
+/* SPDX-License-Identifier: MIT */
+
+#pragma once
+
+#include_next <sys/mman.h>
+
+#if !defined(HAVE_MEMFD_CREATE) || !HAVE_MEMFD_CREATE
+#include <errno.h>
+#include <sys/syscall.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#ifndef __NR_memfd_create
+#if defined __x86_64__
+#define __NR_memfd_create 319
+#elif defined __i386__
+#define __NR_memfd_create 356
+#elif defined __arm__
+#define __NR_memfd_create 385
+#else
+#warning "__NR_memfd_create unknown for your architecture"
+#endif
+#endif
+
+static inline int missing_memfd_create(const char *name, unsigned int flags)
+{
+#ifdef __NR_memfd_create
+	return syscall(__NR_memfd_create, name, flags);
+#else
+	errno = ENOSYS;
+	return -1;
+#endif
+}
+
+#define memfd_create missing_memfd_create
+
+#endif
diff --git a/meson.build b/meson.build
index 22118828..63ac6292 100644
--- a/meson.build
+++ b/meson.build
@@ -196,6 +196,9 @@ if cc.has_member('struct sysinfo', 'totalram',
 	config.set('HAVE_STRUCT_SYSINFO_TOTALRAM', 1)
 endif
 
+have = cc.has_function('memfd_create', prefix : '''#include <sys/mman.h>''', args : '-D_GNU_SOURCE')
+config.set10('HAVE_MEMFD_CREATE', have)
+
 add_project_arguments('-D_GNU_SOURCE', language : 'c')
 add_project_arguments('-include', 'config.h', language : 'c')
 
-- 
2.17.1



More information about the igt-dev mailing list