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 "standard-headers/linux/vhost_types.h"
17 #ifdef CONFIG_VHOST_KERNEL
18 #include <linux/vhost.h>
19 #include <sys/ioctl.h>
21 static int vhost_kernel_call(struct vhost_dev
*dev
, unsigned long int request
,
24 int fd
= (uintptr_t) dev
->opaque
;
26 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_KERNEL
);
28 return ioctl(fd
, request
, arg
);
31 static int vhost_kernel_init(struct vhost_dev
*dev
, void *opaque
)
33 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_KERNEL
);
40 static int vhost_kernel_cleanup(struct vhost_dev
*dev
)
42 int fd
= (uintptr_t) dev
->opaque
;
44 assert(dev
->vhost_ops
->backend_type
== VHOST_BACKEND_TYPE_KERNEL
);
49 static int vhost_kernel_memslots_limit(struct vhost_dev
*dev
)
54 if (g_file_get_contents("/sys/module/vhost/parameters/max_mem_regions",
56 uint64_t val
= g_ascii_strtoull(s
, NULL
, 10);
57 if (!((val
== G_MAXUINT64
|| !val
) && errno
)) {
61 error_report("ignoring invalid max_mem_regions value in vhost module:"
68 static int vhost_kernel_net_set_backend(struct vhost_dev
*dev
,
69 struct vhost_vring_file
*file
)
71 return vhost_kernel_call(dev
, VHOST_NET_SET_BACKEND
, file
);
74 static int vhost_kernel_scsi_set_endpoint(struct vhost_dev
*dev
,
75 struct vhost_scsi_target
*target
)
77 return vhost_kernel_call(dev
, VHOST_SCSI_SET_ENDPOINT
, target
);
80 static int vhost_kernel_scsi_clear_endpoint(struct vhost_dev
*dev
,
81 struct vhost_scsi_target
*target
)
83 return vhost_kernel_call(dev
, VHOST_SCSI_CLEAR_ENDPOINT
, target
);
86 static int vhost_kernel_scsi_get_abi_version(struct vhost_dev
*dev
, int *version
)
88 return vhost_kernel_call(dev
, VHOST_SCSI_GET_ABI_VERSION
, version
);
91 static int vhost_kernel_set_log_base(struct vhost_dev
*dev
, uint64_t base
,
92 struct vhost_log
*log
)
94 return vhost_kernel_call(dev
, VHOST_SET_LOG_BASE
, &base
);
97 static int vhost_kernel_set_mem_table(struct vhost_dev
*dev
,
98 struct vhost_memory
*mem
)
100 return vhost_kernel_call(dev
, VHOST_SET_MEM_TABLE
, mem
);
103 static int vhost_kernel_set_vring_addr(struct vhost_dev
*dev
,
104 struct vhost_vring_addr
*addr
)
106 return vhost_kernel_call(dev
, VHOST_SET_VRING_ADDR
, addr
);
109 static int vhost_kernel_set_vring_endian(struct vhost_dev
*dev
,
110 struct vhost_vring_state
*ring
)
112 return vhost_kernel_call(dev
, VHOST_SET_VRING_ENDIAN
, ring
);
115 static int vhost_kernel_set_vring_num(struct vhost_dev
*dev
,
116 struct vhost_vring_state
*ring
)
118 return vhost_kernel_call(dev
, VHOST_SET_VRING_NUM
, ring
);
121 static int vhost_kernel_set_vring_base(struct vhost_dev
*dev
,
122 struct vhost_vring_state
*ring
)
124 return vhost_kernel_call(dev
, VHOST_SET_VRING_BASE
, ring
);
127 static int vhost_kernel_get_vring_base(struct vhost_dev
*dev
,
128 struct vhost_vring_state
*ring
)
130 return vhost_kernel_call(dev
, VHOST_GET_VRING_BASE
, ring
);
133 static int vhost_kernel_set_vring_kick(struct vhost_dev
*dev
,
134 struct vhost_vring_file
*file
)
136 return vhost_kernel_call(dev
, VHOST_SET_VRING_KICK
, file
);
139 static int vhost_kernel_set_vring_call(struct vhost_dev
*dev
,
140 struct vhost_vring_file
*file
)
142 return vhost_kernel_call(dev
, VHOST_SET_VRING_CALL
, file
);
145 static int vhost_kernel_set_vring_busyloop_timeout(struct vhost_dev
*dev
,
146 struct vhost_vring_state
*s
)
148 return vhost_kernel_call(dev
, VHOST_SET_VRING_BUSYLOOP_TIMEOUT
, s
);
151 static int vhost_kernel_set_features(struct vhost_dev
*dev
,
154 return vhost_kernel_call(dev
, VHOST_SET_FEATURES
, &features
);
157 static int vhost_kernel_get_features(struct vhost_dev
*dev
,
160 return vhost_kernel_call(dev
, VHOST_GET_FEATURES
, features
);
163 static int vhost_kernel_set_owner(struct vhost_dev
*dev
)
165 return vhost_kernel_call(dev
, VHOST_SET_OWNER
, NULL
);
168 static int vhost_kernel_reset_device(struct vhost_dev
*dev
)
170 return vhost_kernel_call(dev
, VHOST_RESET_OWNER
, NULL
);
173 static int vhost_kernel_get_vq_index(struct vhost_dev
*dev
, int idx
)
175 assert(idx
>= dev
->vq_index
&& idx
< dev
->vq_index
+ dev
->nvqs
);
177 return idx
- dev
->vq_index
;
180 #ifdef CONFIG_VHOST_VSOCK
181 static int vhost_kernel_vsock_set_guest_cid(struct vhost_dev
*dev
,
184 return vhost_kernel_call(dev
, VHOST_VSOCK_SET_GUEST_CID
, &guest_cid
);
187 static int vhost_kernel_vsock_set_running(struct vhost_dev
*dev
, int start
)
189 return vhost_kernel_call(dev
, VHOST_VSOCK_SET_RUNNING
, &start
);
191 #endif /* CONFIG_VHOST_VSOCK */
193 static void vhost_kernel_iotlb_read(void *opaque
)
195 struct vhost_dev
*dev
= opaque
;
196 struct vhost_msg msg
;
199 while ((len
= read((uintptr_t)dev
->opaque
, &msg
, sizeof msg
)) > 0) {
200 if (len
< sizeof msg
) {
201 error_report("Wrong vhost message len: %d", (int)len
);
204 if (msg
.type
!= VHOST_IOTLB_MSG
) {
205 error_report("Unknown vhost iotlb message type");
209 vhost_backend_handle_iotlb_msg(dev
, &msg
.iotlb
);
213 static int vhost_kernel_send_device_iotlb_msg(struct vhost_dev
*dev
,
214 struct vhost_iotlb_msg
*imsg
)
216 struct vhost_msg msg
;
218 msg
.type
= VHOST_IOTLB_MSG
;
221 if (write((uintptr_t)dev
->opaque
, &msg
, sizeof msg
) != sizeof msg
) {
222 error_report("Fail to update device iotlb");
229 static void vhost_kernel_set_iotlb_callback(struct vhost_dev
*dev
,
233 qemu_set_fd_handler((uintptr_t)dev
->opaque
,
234 vhost_kernel_iotlb_read
, NULL
, dev
);
236 qemu_set_fd_handler((uintptr_t)dev
->opaque
, NULL
, NULL
, NULL
);
239 static const VhostOps kernel_ops
= {
240 .backend_type
= VHOST_BACKEND_TYPE_KERNEL
,
241 .vhost_backend_init
= vhost_kernel_init
,
242 .vhost_backend_cleanup
= vhost_kernel_cleanup
,
243 .vhost_backend_memslots_limit
= vhost_kernel_memslots_limit
,
244 .vhost_net_set_backend
= vhost_kernel_net_set_backend
,
245 .vhost_scsi_set_endpoint
= vhost_kernel_scsi_set_endpoint
,
246 .vhost_scsi_clear_endpoint
= vhost_kernel_scsi_clear_endpoint
,
247 .vhost_scsi_get_abi_version
= vhost_kernel_scsi_get_abi_version
,
248 .vhost_set_log_base
= vhost_kernel_set_log_base
,
249 .vhost_set_mem_table
= vhost_kernel_set_mem_table
,
250 .vhost_set_vring_addr
= vhost_kernel_set_vring_addr
,
251 .vhost_set_vring_endian
= vhost_kernel_set_vring_endian
,
252 .vhost_set_vring_num
= vhost_kernel_set_vring_num
,
253 .vhost_set_vring_base
= vhost_kernel_set_vring_base
,
254 .vhost_get_vring_base
= vhost_kernel_get_vring_base
,
255 .vhost_set_vring_kick
= vhost_kernel_set_vring_kick
,
256 .vhost_set_vring_call
= vhost_kernel_set_vring_call
,
257 .vhost_set_vring_busyloop_timeout
=
258 vhost_kernel_set_vring_busyloop_timeout
,
259 .vhost_set_features
= vhost_kernel_set_features
,
260 .vhost_get_features
= vhost_kernel_get_features
,
261 .vhost_set_owner
= vhost_kernel_set_owner
,
262 .vhost_reset_device
= vhost_kernel_reset_device
,
263 .vhost_get_vq_index
= vhost_kernel_get_vq_index
,
264 #ifdef CONFIG_VHOST_VSOCK
265 .vhost_vsock_set_guest_cid
= vhost_kernel_vsock_set_guest_cid
,
266 .vhost_vsock_set_running
= vhost_kernel_vsock_set_running
,
267 #endif /* CONFIG_VHOST_VSOCK */
268 .vhost_set_iotlb_callback
= vhost_kernel_set_iotlb_callback
,
269 .vhost_send_device_iotlb_msg
= vhost_kernel_send_device_iotlb_msg
,
273 int vhost_set_backend_type(struct vhost_dev
*dev
, VhostBackendType backend_type
)
277 switch (backend_type
) {
278 #ifdef CONFIG_VHOST_KERNEL
279 case VHOST_BACKEND_TYPE_KERNEL
:
280 dev
->vhost_ops
= &kernel_ops
;
283 #ifdef CONFIG_VHOST_USER
284 case VHOST_BACKEND_TYPE_USER
:
285 dev
->vhost_ops
= &user_ops
;
289 error_report("Unknown vhost backend type");
296 int vhost_backend_update_device_iotlb(struct vhost_dev
*dev
,
297 uint64_t iova
, uint64_t uaddr
,
299 IOMMUAccessFlags perm
)
301 struct vhost_iotlb_msg imsg
;
306 imsg
.type
= VHOST_IOTLB_UPDATE
;
310 imsg
.perm
= VHOST_ACCESS_RO
;
313 imsg
.perm
= VHOST_ACCESS_WO
;
316 imsg
.perm
= VHOST_ACCESS_RW
;
322 if (dev
->vhost_ops
&& dev
->vhost_ops
->vhost_send_device_iotlb_msg
)
323 return dev
->vhost_ops
->vhost_send_device_iotlb_msg(dev
, &imsg
);
328 int vhost_backend_invalidate_device_iotlb(struct vhost_dev
*dev
,
329 uint64_t iova
, uint64_t len
)
331 struct vhost_iotlb_msg imsg
;
335 imsg
.type
= VHOST_IOTLB_INVALIDATE
;
337 if (dev
->vhost_ops
&& dev
->vhost_ops
->vhost_send_device_iotlb_msg
)
338 return dev
->vhost_ops
->vhost_send_device_iotlb_msg(dev
, &imsg
);
343 int vhost_backend_handle_iotlb_msg(struct vhost_dev
*dev
,
344 struct vhost_iotlb_msg
*imsg
)
348 switch (imsg
->type
) {
349 case VHOST_IOTLB_MISS
:
350 ret
= vhost_device_iotlb_miss(dev
, imsg
->iova
,
351 imsg
->perm
!= VHOST_ACCESS_RO
);
353 case VHOST_IOTLB_ACCESS_FAIL
:
354 /* FIXME: report device iotlb error */
355 error_report("Access failure IOTLB message type not supported");
358 case VHOST_IOTLB_UPDATE
:
359 case VHOST_IOTLB_INVALIDATE
:
361 error_report("Unexpected IOTLB message type");