xref: /openbmc/qemu/util/memfd.c (revision b70ce101)
1 /*
2  * memfd.c
3  *
4  * Copyright (c) 2015 Red Hat, Inc.
5  *
6  * QEMU library functions on POSIX which are shared between QEMU and
7  * the QEMU tools.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to deal
11  * in the Software without restriction, including without limitation the rights
12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25  * THE SOFTWARE.
26  */
27 
28 #include "qemu/osdep.h"
29 
30 #include <glib/gprintf.h>
31 
32 #include "qemu/memfd.h"
33 
34 #ifdef CONFIG_MEMFD
35 #include <sys/memfd.h>
36 #elif defined CONFIG_LINUX
37 #include <sys/syscall.h>
38 #include <asm/unistd.h>
39 
40 static int memfd_create(const char *name, unsigned int flags)
41 {
42 #ifdef __NR_memfd_create
43     return syscall(__NR_memfd_create, name, flags);
44 #else
45     return -1;
46 #endif
47 }
48 #endif
49 
50 #ifndef MFD_CLOEXEC
51 #define MFD_CLOEXEC 0x0001U
52 #endif
53 
54 #ifndef MFD_ALLOW_SEALING
55 #define MFD_ALLOW_SEALING 0x0002U
56 #endif
57 
58 /*
59  * This is a best-effort helper for shared memory allocation, with
60  * optional sealing. The helper will do his best to allocate using
61  * memfd with sealing, but may fallback on other methods without
62  * sealing.
63  */
64 void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals,
65                        int *fd)
66 {
67     void *ptr;
68     int mfd = -1;
69 
70     *fd = -1;
71 
72 #ifdef CONFIG_LINUX
73     if (seals) {
74         mfd = memfd_create(name, MFD_ALLOW_SEALING | MFD_CLOEXEC);
75     }
76 
77     if (mfd == -1) {
78         /* some systems have memfd without sealing */
79         mfd = memfd_create(name, MFD_CLOEXEC);
80         seals = 0;
81     }
82 #endif
83 
84     if (mfd != -1) {
85         if (ftruncate(mfd, size) == -1) {
86             perror("ftruncate");
87             close(mfd);
88             return NULL;
89         }
90 
91         if (seals && fcntl(mfd, F_ADD_SEALS, seals) == -1) {
92             perror("fcntl");
93             close(mfd);
94             return NULL;
95         }
96     } else {
97         const char *tmpdir = g_get_tmp_dir();
98         gchar *fname;
99 
100         fname = g_strdup_printf("%s/memfd-XXXXXX", tmpdir);
101         mfd = mkstemp(fname);
102         unlink(fname);
103         g_free(fname);
104 
105         if (mfd == -1) {
106             perror("mkstemp");
107             return NULL;
108         }
109 
110         if (ftruncate(mfd, size) == -1) {
111             perror("ftruncate");
112             close(mfd);
113             return NULL;
114         }
115     }
116 
117     ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0);
118     if (ptr == MAP_FAILED) {
119         perror("mmap");
120         close(mfd);
121         return NULL;
122     }
123 
124     *fd = mfd;
125     return ptr;
126 }
127 
128 void qemu_memfd_free(void *ptr, size_t size, int fd)
129 {
130     if (ptr) {
131         munmap(ptr, size);
132     }
133 
134     if (fd != -1) {
135         close(fd);
136     }
137 }
138 
139 enum {
140     MEMFD_KO,
141     MEMFD_OK,
142     MEMFD_TODO
143 };
144 
145 bool qemu_memfd_check(void)
146 {
147     static int memfd_check = MEMFD_TODO;
148 
149     if (memfd_check == MEMFD_TODO) {
150         int fd;
151         void *ptr;
152 
153         ptr = qemu_memfd_alloc("test", 4096, 0, &fd);
154         memfd_check = ptr ? MEMFD_OK : MEMFD_KO;
155         qemu_memfd_free(ptr, 4096, fd);
156     }
157 
158     return memfd_check == MEMFD_OK;
159 }
160