4 * Copyright (c) 2013 Virtual Open Systems Sarl.
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
11 #include "qemu/osdep.h"
12 #include "hw/virtio/vhost.h"
13 #include "hw/virtio/vhost-backend.h"
14 #include "qemu/error-report.h"
15 #include "qemu/main-loop.h"
16 #include "standard-headers/linux/vhost_types.h"
18 #ifdef CONFIG_VHOST_KERNEL
19 #include <linux/vhost.h>
20 #include <sys/ioctl.h>
22 static int vhost_kernel_call(struct vhost_dev
*dev
, unsigned long int request
,
25 int fd
= (uintptr_t) dev
->opaque
;
27 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_KERNEL
);
29 return ioctl(fd
, request
, arg
);
32 static int vhost_kernel_init(struct vhost_dev
*dev
, void *opaque
)
34 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_KERNEL
);
41 static int vhost_kernel_cleanup(struct vhost_dev
*dev
)
43 int fd
= (uintptr_t) dev
->opaque
;
45 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_KERNEL
);
50 static int vhost_kernel_memslots_limit(struct vhost_dev
*dev
)
55 if (g_file_get_contents("/sys/module/vhost/parameters/max_mem_regions",
57 uint64_t val
= g_ascii_strtoull(s
, NULL
, 10);
58 if (!((val
== G_MAXUINT64
|| !val
) && errno
)) {
62 error_report("ignoring invalid max_mem_regions value in vhost module:"
69 static int vhost_kernel_net_set_backend(struct vhost_dev
*dev
,
70 struct vhost_vring_file
*file
)
72 return vhost_kernel_call(dev
, VHOST_NET_SET_BACKEND
, file
);
75 static int vhost_kernel_scsi_set_endpoint(struct vhost_dev
*dev
,
76 struct vhost_scsi_target
*target
)
78 return vhost_kernel_call(dev
, VHOST_SCSI_SET_ENDPOINT
, target
);
81 static int vhost_kernel_scsi_clear_endpoint(struct vhost_dev
*dev
,
82 struct vhost_scsi_target
*target
)
84 return vhost_kernel_call(dev
, VHOST_SCSI_CLEAR_ENDPOINT
, target
);
87 static int vhost_kernel_scsi_get_abi_version(struct vhost_dev
*dev
, int *version
)
89 return vhost_kernel_call(dev
, VHOST_SCSI_GET_ABI_VERSION
, version
);
92 static int vhost_kernel_set_log_base(struct vhost_dev
*dev
, uint64_t base
,
93 struct vhost_log
*log
)
95 return vhost_kernel_call(dev
, VHOST_SET_LOG_BASE
, &base
);
98 static int vhost_kernel_set_mem_table(struct vhost_dev
*dev
,
99 struct vhost_memory
*mem
)
101 return vhost_kernel_call(dev
, VHOST_SET_MEM_TABLE
, mem
);
104 static int vhost_kernel_set_vring_addr(struct vhost_dev
*dev
,
105 struct vhost_vring_addr
*addr
)
107 return vhost_kernel_call(dev
, VHOST_SET_VRING_ADDR
, addr
);
110 static int vhost_kernel_set_vring_endian(struct vhost_dev
*dev
,
111 struct vhost_vring_state
*ring
)
113 return vhost_kernel_call(dev
, VHOST_SET_VRING_ENDIAN
, ring
);
116 static int vhost_kernel_set_vring_num(struct vhost_dev
*dev
,
117 struct vhost_vring_state
*ring
)
119 return vhost_kernel_call(dev
, VHOST_SET_VRING_NUM
, ring
);
122 static int vhost_kernel_set_vring_base(struct vhost_dev
*dev
,
123 struct vhost_vring_state
*ring
)
125 return vhost_kernel_call(dev
, VHOST_SET_VRING_BASE
, ring
);
128 static int vhost_kernel_get_vring_base(struct vhost_dev
*dev
,
129 struct vhost_vring_state
*ring
)
131 return vhost_kernel_call(dev
, VHOST_GET_VRING_BASE
, ring
);
134 static int vhost_kernel_set_vring_kick(struct vhost_dev
*dev
,
135 struct vhost_vring_file
*file
)
137 return vhost_kernel_call(dev
, VHOST_SET_VRING_KICK
, file
);
140 static int vhost_kernel_set_vring_call(struct vhost_dev
*dev
,
141 struct vhost_vring_file
*file
)
143 return vhost_kernel_call(dev
, VHOST_SET_VRING_CALL
, file
);
146 static int vhost_kernel_set_vring_busyloop_timeout(struct vhost_dev
*dev
,
147 struct vhost_vring_state
*s
)
149 return vhost_kernel_call(dev
, VHOST_SET_VRING_BUSYLOOP_TIMEOUT
, s
);
152 static int vhost_kernel_set_features(struct vhost_dev
*dev
,
155 return vhost_kernel_call(dev
, VHOST_SET_FEATURES
, &features
);
158 static int vhost_kernel_get_features(struct vhost_dev
*dev
,
161 return vhost_kernel_call(dev
, VHOST_GET_FEATURES
, features
);
164 static int vhost_kernel_set_owner(struct vhost_dev
*dev
)
166 return vhost_kernel_call(dev
, VHOST_SET_OWNER
, NULL
);
169 static int vhost_kernel_reset_device(struct vhost_dev
*dev
)
171 return vhost_kernel_call(dev
, VHOST_RESET_OWNER
, NULL
);
174 static int vhost_kernel_get_vq_index(struct vhost_dev
*dev
, int idx
)
176 assert(idx
>= dev
->vq_index
&& idx
< dev
->vq_index
+ dev
->nvqs
);
178 return idx
- dev
->vq_index
;
181 #ifdef CONFIG_VHOST_VSOCK
182 static int vhost_kernel_vsock_set_guest_cid(struct vhost_dev
*dev
,
185 return vhost_kernel_call(dev
, VHOST_VSOCK_SET_GUEST_CID
, &guest_cid
);
188 static int vhost_kernel_vsock_set_running(struct vhost_dev
*dev
, int start
)
190 return vhost_kernel_call(dev
, VHOST_VSOCK_SET_RUNNING
, &start
);
192 #endif /* CONFIG_VHOST_VSOCK */
194 static void vhost_kernel_iotlb_read(void *opaque
)
196 struct vhost_dev
*dev
= opaque
;
197 struct vhost_msg msg
;
200 while ((len
= read((uintptr_t)dev
->opaque
, &msg
, sizeof msg
)) > 0) {
201 if (len
< sizeof msg
) {
202 error_report("Wrong vhost message len: %d", (int)len
);
205 if (msg
.type
!= VHOST_IOTLB_MSG
) {
206 error_report("Unknown vhost iotlb message type");
210 vhost_backend_handle_iotlb_msg(dev
, &msg
.iotlb
);
214 static int vhost_kernel_send_device_iotlb_msg(struct vhost_dev
*dev
,
215 struct vhost_iotlb_msg
*imsg
)
217 struct vhost_msg msg
;
219 msg
.type
= VHOST_IOTLB_MSG
;
222 if (write((uintptr_t)dev
->opaque
, &msg
, sizeof msg
) != sizeof msg
) {
223 error_report("Fail to update device iotlb");
230 static void vhost_kernel_set_iotlb_callback(struct vhost_dev
*dev
,
234 qemu_set_fd_handler((uintptr_t)dev
->opaque
,
235 vhost_kernel_iotlb_read
, NULL
, dev
);
237 qemu_set_fd_handler((uintptr_t)dev
->opaque
, NULL
, NULL
, NULL
);
240 static const VhostOps kernel_ops
= {
241 .backend_type
= VHOST_BACKEND_TYPE_KERNEL
,
242 .vhost_backend_init
= vhost_kernel_init
,
243 .vhost_backend_cleanup
= vhost_kernel_cleanup
,
244 .vhost_backend_memslots_limit
= vhost_kernel_memslots_limit
,
245 .vhost_net_set_backend
= vhost_kernel_net_set_backend
,
246 .vhost_scsi_set_endpoint
= vhost_kernel_scsi_set_endpoint
,
247 .vhost_scsi_clear_endpoint
= vhost_kernel_scsi_clear_endpoint
,
248 .vhost_scsi_get_abi_version
= vhost_kernel_scsi_get_abi_version
,
249 .vhost_set_log_base
= vhost_kernel_set_log_base
,
250 .vhost_set_mem_table
= vhost_kernel_set_mem_table
,
251 .vhost_set_vring_addr
= vhost_kernel_set_vring_addr
,
252 .vhost_set_vring_endian
= vhost_kernel_set_vring_endian
,
253 .vhost_set_vring_num
= vhost_kernel_set_vring_num
,
254 .vhost_set_vring_base
= vhost_kernel_set_vring_base
,
255 .vhost_get_vring_base
= vhost_kernel_get_vring_base
,
256 .vhost_set_vring_kick
= vhost_kernel_set_vring_kick
,
257 .vhost_set_vring_call
= vhost_kernel_set_vring_call
,
258 .vhost_set_vring_busyloop_timeout
=
259 vhost_kernel_set_vring_busyloop_timeout
,
260 .vhost_set_features
= vhost_kernel_set_features
,
261 .vhost_get_features
= vhost_kernel_get_features
,
262 .vhost_set_owner
= vhost_kernel_set_owner
,
263 .vhost_reset_device
= vhost_kernel_reset_device
,
264 .vhost_get_vq_index
= vhost_kernel_get_vq_index
,
265 #ifdef CONFIG_VHOST_VSOCK
266 .vhost_vsock_set_guest_cid
= vhost_kernel_vsock_set_guest_cid
,
267 .vhost_vsock_set_running
= vhost_kernel_vsock_set_running
,
268 #endif /* CONFIG_VHOST_VSOCK */
269 .vhost_set_iotlb_callback
= vhost_kernel_set_iotlb_callback
,
270 .vhost_send_device_iotlb_msg
= vhost_kernel_send_device_iotlb_msg
,
274 int vhost_set_backend_type(struct vhost_dev
*dev
, VhostBackendType backend_type
)
278 switch (backend_type
) {
279 #ifdef CONFIG_VHOST_KERNEL
280 case VHOST_BACKEND_TYPE_KERNEL
:
281 dev
->vhost_ops
= &kernel_ops
;
284 #ifdef CONFIG_VHOST_USER
285 case VHOST_BACKEND_TYPE_USER
:
286 dev
->vhost_ops
= &user_ops
;
290 error_report("Unknown vhost backend type");
297 int vhost_backend_update_device_iotlb(struct vhost_dev
*dev
,
298 uint64_t iova
, uint64_t uaddr
,
300 IOMMUAccessFlags perm
)
302 struct vhost_iotlb_msg imsg
;
307 imsg
.type
= VHOST_IOTLB_UPDATE
;
311 imsg
.perm
= VHOST_ACCESS_RO
;
314 imsg
.perm
= VHOST_ACCESS_WO
;
317 imsg
.perm
= VHOST_ACCESS_RW
;
323 if (dev
->vhost_ops
&& dev
->vhost_ops
->vhost_send_device_iotlb_msg
)
324 return dev
->vhost_ops
->vhost_send_device_iotlb_msg(dev
, &imsg
);
329 int vhost_backend_invalidate_device_iotlb(struct vhost_dev
*dev
,
330 uint64_t iova
, uint64_t len
)
332 struct vhost_iotlb_msg imsg
;
336 imsg
.type
= VHOST_IOTLB_INVALIDATE
;
338 if (dev
->vhost_ops
&& dev
->vhost_ops
->vhost_send_device_iotlb_msg
)
339 return dev
->vhost_ops
->vhost_send_device_iotlb_msg(dev
, &imsg
);
344 int vhost_backend_handle_iotlb_msg(struct vhost_dev
*dev
,
345 struct vhost_iotlb_msg
*imsg
)
349 switch (imsg
->type
) {
350 case VHOST_IOTLB_MISS
:
351 ret
= vhost_device_iotlb_miss(dev
, imsg
->iova
,
352 imsg
->perm
!= VHOST_ACCESS_RO
);
354 case VHOST_IOTLB_ACCESS_FAIL
:
355 /* FIXME: report device iotlb error */
356 error_report("Access failure IOTLB message type not supported");
359 case VHOST_IOTLB_UPDATE
:
360 case VHOST_IOTLB_INVALIDATE
:
362 error_report("Unexpected IOTLB message type");