xref: /openbmc/qemu/util/memfd.c (revision d84be02d)
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 #if defined CONFIG_LINUX && !defined CONFIG_MEMFD
35 #include <sys/syscall.h>
36 #include <asm/unistd.h>
37 
38 static int memfd_create(const char *name, unsigned int flags)
39 {
40 #ifdef __NR_memfd_create
41     return syscall(__NR_memfd_create, name, flags);
42 #else
43     return -1;
44 #endif
45 }
46 #endif
47 
48 #ifndef MFD_CLOEXEC
49 #define MFD_CLOEXEC 0x0001U
50 #endif
51 
52 #ifndef MFD_ALLOW_SEALING
53 #define MFD_ALLOW_SEALING 0x0002U
54 #endif
55 
56 /*
57  * This is a best-effort helper for shared memory allocation, with
58  * optional sealing. The helper will do his best to allocate using
59  * memfd with sealing, but may fallback on other methods without
60  * sealing.
61  */
62 void *qemu_memfd_alloc(const char *name, size_t size, unsigned int seals,
63                        int *fd)
64 {
65     void *ptr;
66     int mfd = -1;
67 
68     *fd = -1;
69 
70 #ifdef CONFIG_LINUX
71     if (seals) {
72         mfd = memfd_create(name, MFD_ALLOW_SEALING | MFD_CLOEXEC);
73     }
74 
75     if (mfd == -1) {
76         /* some systems have memfd without sealing */
77         mfd = memfd_create(name, MFD_CLOEXEC);
78         seals = 0;
79     }
80 #endif
81 
82     if (mfd != -1) {
83         if (ftruncate(mfd, size) == -1) {
84             perror("ftruncate");
85             close(mfd);
86             return NULL;
87         }
88 
89         if (seals && fcntl(mfd, F_ADD_SEALS, seals) == -1) {
90             perror("fcntl");
91             close(mfd);
92             return NULL;
93         }
94     } else {
95         const char *tmpdir = g_get_tmp_dir();
96         gchar *fname;
97 
98         fname = g_strdup_printf("%s/memfd-XXXXXX", tmpdir);
99         mfd = mkstemp(fname);
100         unlink(fname);
101         g_free(fname);
102 
103         if (mfd == -1) {
104             perror("mkstemp");
105             return NULL;
106         }
107 
108         if (ftruncate(mfd, size) == -1) {
109             perror("ftruncate");
110             close(mfd);
111             return NULL;
112         }
113     }
114 
115     ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0);
116     if (ptr == MAP_FAILED) {
117         perror("mmap");
118         close(mfd);
119         return NULL;
120     }
121 
122     *fd = mfd;
123     return ptr;
124 }
125 
126 void qemu_memfd_free(void *ptr, size_t size, int fd)
127 {
128     if (ptr) {
129         munmap(ptr, size);
130     }
131 
132     if (fd != -1) {
133         close(fd);
134     }
135 }
136 
137 enum {
138     MEMFD_KO,
139     MEMFD_OK,
140     MEMFD_TODO
141 };
142 
143 bool qemu_memfd_check(void)
144 {
145     static int memfd_check = MEMFD_TODO;
146 
147     if (memfd_check == MEMFD_TODO) {
148         int fd;
149         void *ptr;
150 
151         ptr = qemu_memfd_alloc("test", 4096, 0, &fd);
152         memfd_check = ptr ? MEMFD_OK : MEMFD_KO;
153         qemu_memfd_free(ptr, 4096, fd);
154     }
155 
156     return memfd_check == MEMFD_OK;
157 }
158