summary refs log tree commit diff
path: root/pkgs/development/libraries/wlroots/0002-util-support-virtio_wl-shm-allocation.patch
blob: cd25aedf35adefdc43da4d0403e16e3c27073713 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
From 8dc7c4e472f6c23e20aee6a7041d260344ffd918 Mon Sep 17 00:00:00 2001
From: Alyssa Ross <hi@alyssa.is>
Date: Sat, 23 May 2020 03:42:33 +0000
Subject: [PATCH 2/2] util: support virtio_wl shm allocation

---
 include/util/virtio_wl.h     | 14 ++++++
 include/util/virtio_wl_shm.h |  8 +++
 util/meson.build             |  2 +
 util/shm.c                   | 12 +++++
 util/virtio_wl.c             | 96 ++++++++++++++++++++++++++++++++++++
 util/virtio_wl_shm.c         | 65 ++++++++++++++++++++++++
 6 files changed, 197 insertions(+)
 create mode 100644 include/util/virtio_wl.h
 create mode 100644 include/util/virtio_wl_shm.h
 create mode 100644 util/virtio_wl.c
 create mode 100644 util/virtio_wl_shm.c

diff --git a/include/util/virtio_wl.h b/include/util/virtio_wl.h
new file mode 100644
index 00000000..ae5c19b5
--- /dev/null
+++ b/include/util/virtio_wl.h
@@ -0,0 +1,14 @@
+#ifndef UTIL_VIRTIO_WL_H
+#define UTIL_VIRTIO_WL_H
+
+struct virtwl_ioctl_txn;
+
+int virtio_wl_connect(const char *name, uint32_t flags);
+
+int32_t virtio_wl_sendmsg(int sockfd, struct virtwl_ioctl_txn *ioctl_txn);
+int32_t virtio_wl_send(int sockfd, const void *buf, uint32_t len);
+
+int32_t virtio_wl_recvmsg(int sockfd, struct virtwl_ioctl_txn *ioctl_txn);
+int32_t virtio_wl_recv(int sockfd, void *buf, uint32_t len);
+
+#endif
diff --git a/include/util/virtio_wl_shm.h b/include/util/virtio_wl_shm.h
new file mode 100644
index 00000000..d9f9f045
--- /dev/null
+++ b/include/util/virtio_wl_shm.h
@@ -0,0 +1,8 @@
+#ifndef UTIL_VIRTIO_WL_SHM_H
+#define UTIL_VIRTIO_WL_SHM_H
+
+#include <stddef.h>
+
+int allocate_virtio_wl_shm_file(size_t size);
+
+#endif
diff --git a/util/meson.build b/util/meson.build
index 5e31cbbe..a39cc9bd 100644
--- a/util/meson.build
+++ b/util/meson.build
@@ -7,5 +7,7 @@ wlr_files += files(
 	'signal.c',
 	'time.c',
 	'token.c',
+	'virtio_wl.c',
+	'virtio_wl_shm.c',
 )
 
diff --git a/util/shm.c b/util/shm.c
index f7c7303e..d8110904 100644
--- a/util/shm.c
+++ b/util/shm.c
@@ -2,11 +2,14 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <string.h>
+#include <stdlib.h>
 #include <sys/mman.h>
 #include <time.h>
 #include <unistd.h>
 #include <wlr/config.h>
+#include <wlr/util/log.h>
 #include "util/shm.h"
+#include "util/virtio_wl_shm.h"
 
 static void randname(char *buf) {
 	struct timespec ts;
@@ -19,6 +22,11 @@ static void randname(char *buf) {
 }
 
 int create_shm_file(void) {
+	if (getenv("WLR_VIRTIO_WL")) {
+		wlr_log(WLR_ERROR, "cannot use create_shm_file with virtio_wl");
+		return -1;
+	}
+
 	int retries = 100;
 	do {
 		char name[] = "/wlroots-XXXXXX";
@@ -37,6 +45,10 @@ int create_shm_file(void) {
 }
 
 int allocate_shm_file(size_t size) {
+	if (getenv("WLR_VIRTIO_WL")) {
+		return allocate_virtio_wl_shm_file(size);
+	}
+
 	int fd = create_shm_file();
 	if (fd < 0) {
 		return -1;
diff --git a/util/virtio_wl.c b/util/virtio_wl.c
new file mode 100644
index 00000000..e7ee58ac
--- /dev/null
+++ b/util/virtio_wl.c
@@ -0,0 +1,96 @@
+#define _POSIX_C_SOURCE 200809L
+
+#include <fcntl.h>
+#include <linux/virtwl.h>
+#include <stdint.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include "util/virtio_wl.h"
+
+// This is essentially vendored reusable library code, so I consider
+// it exempt from the wlroots style guide. :)
+
+int virtio_wl_connect(const char *name, uint32_t flags)
+{
+	static int wl_fd = -1;
+	if (wl_fd < 0)
+		wl_fd = open("/dev/wl0", O_RDWR | O_CLOEXEC);
+	if (wl_fd < 0)
+		return wl_fd;
+
+	struct virtwl_ioctl_new new_ctx = {
+		.type = name ? VIRTWL_IOCTL_NEW_CTX_NAMED : VIRTWL_IOCTL_NEW_CTX,
+		.fd = -1,
+		.flags = flags,
+	};
+	// Device assumes name 32 bytes long if not null terminated.
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wstringop-truncation"
+	if (name)
+		strncpy(new_ctx.name, name, sizeof(new_ctx.name));
+#pragma GCC diagnostic pop
+
+	if (ioctl(wl_fd, VIRTWL_IOCTL_NEW, &new_ctx))
+		return -1;
+
+	return new_ctx.fd;
+}
+
+int32_t virtio_wl_sendmsg(int sockfd, struct virtwl_ioctl_txn *ioctl_txn)
+{
+	int r = ioctl(sockfd, VIRTWL_IOCTL_SEND, ioctl_txn);
+	if (!r)
+		r = ioctl_txn->len > INT32_MAX ? INT32_MAX : ioctl_txn->len;
+	return r;
+}
+
+int32_t virtio_wl_send(int sockfd, const void *buf, uint32_t len)
+{
+	struct virtwl_ioctl_txn *ioctl_txn = malloc(sizeof(*ioctl_txn) + len);
+	if (!ioctl_txn)
+		return -1;
+
+	for (size_t i = 0; i < VIRTWL_SEND_MAX_ALLOCS; i++)
+		ioctl_txn->fds[i] = -1;
+
+	ioctl_txn->len = len;
+	memcpy((uint8_t *)ioctl_txn + sizeof(*ioctl_txn), buf, len);
+
+	int r = virtio_wl_sendmsg(sockfd, ioctl_txn);
+
+	free(ioctl_txn);
+	return r;
+}
+
+int32_t virtio_wl_recvmsg(int sockfd, struct virtwl_ioctl_txn *ioctl_txn)
+{
+	if (ioctl(sockfd, VIRTWL_IOCTL_RECV, ioctl_txn))
+		return -1;
+
+	return ioctl_txn->len > INT32_MAX ? INT32_MAX : ioctl_txn->len;
+}
+
+int32_t virtio_wl_recv(int sockfd, void *buf, uint32_t len)
+{
+	struct virtwl_ioctl_txn *ioctl_txn = malloc(sizeof(*ioctl_txn) + len);
+	if (!ioctl_txn)
+		return -1;
+
+	ioctl_txn->len = len;
+
+	int rv = virtio_wl_recvmsg(sockfd, ioctl_txn);
+	if (rv < 0)
+		goto cleanup;
+
+	memcpy(buf, (uint8_t *)ioctl_txn + sizeof(*ioctl_txn), ioctl_txn->len);
+
+	for (size_t i = 0; i < VIRTWL_SEND_MAX_ALLOCS; i++)
+		if (ioctl_txn->fds[i] >= 0)
+			close(ioctl_txn->fds[i]);
+
+ cleanup:
+	free(ioctl_txn);
+	return rv;
+}
diff --git a/util/virtio_wl_shm.c b/util/virtio_wl_shm.c
new file mode 100644
index 00000000..b2109310
--- /dev/null
+++ b/util/virtio_wl_shm.c
@@ -0,0 +1,65 @@
+#include <assert.h>
+#include <errno.h>
+#include <linux/virtwl.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include "util/virtio_wl.h"
+#include "util/virtio_wl_shm.h"
+
+// This is essentially vendored reusable library code, so I consider
+// it exempt from the wlroots style guide. :)
+
+int allocate_virtio_wl_shm_file(size_t size)
+{
+	static const size_t NAME_SIZE = 224;
+	static const char *NAME = "wlroots";
+
+	int r;
+	uint8_t *message = NULL;
+	struct virtwl_ioctl_txn *ioctl_txn = NULL;
+
+	int conn = virtio_wl_connect("__crosvm_memfd", 0);
+	if (conn < 0)
+		return conn;
+
+	message = calloc(NAME_SIZE + 8, 1);
+	if (!message) {
+		r = -1;
+		goto cleanup;
+	}
+	strcpy((char *)message, NAME);
+
+	// Encode size as 64-bit little-endian unsigned integer.
+	for (uint8_t i = 0; i < 8; i++)
+		message[NAME_SIZE + i] = (uint8_t)((uint64_t)size >> (8 * i));
+
+	if ((r = virtio_wl_send(conn, message, NAME_SIZE + 8)) < 0)
+		 goto cleanup;
+
+	int32_t len = 1;
+	if (!(ioctl_txn = malloc(sizeof(*ioctl_txn) + len))) {
+		r = -1;
+		goto cleanup;
+	}
+	ioctl_txn->len = len;
+
+	if ((r = virtio_wl_recvmsg(conn, ioctl_txn)) < 0)
+		goto cleanup;
+
+	if (((uint8_t *)ioctl_txn + sizeof(*ioctl_txn))[0]) {
+		// We don't actually know why we didn't get the
+		// memory, but out of memory is a reasonable guess.
+		errno = ENOMEM;
+		r = -1;
+		goto cleanup;
+	}
+
+	r = ioctl_txn->fds[0];
+	assert(r >= 0);
+
+ cleanup:
+	free(message);
+	free(ioctl_txn);
+	return r;
+}
-- 
2.31.1