<div dir="ltr"><div dir="ltr">Hi,<div><br></div><div>I wasn't really sure how I should implement everything. Specifically, in print_vhost_memory_region there's a flags_padding field, but currently, there are no flags, so it's always zero. I still made it so that if the flags are not zero they are printed as hex, just in case flags would be added to the kernel before they are implemented in strace.</div><div><br></div><div>Thanks :)</div><div><br></div></div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Oct 22, 2023 at 3:44 AM Daniel Cohen Hillel <<a href="mailto:danielcohenhillel@gmail.com">danielcohenhillel@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">* vhost.c: New file.<br>
* Makefile.am (strace_SOURCES): Add it.<br>
* <a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a> (AC_CHECK_HEADERS): Add linux/vhost.h.<br>
* defs.h (kvm_ioctl): New prototype.<br>
* ioctl.c (ioctl_decode) HAVE_LINUX_VHOST_H]: Use vhost_ioctl.<br>
---<br>
<a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a> | 1 +<br>
src/Makefile.am | 1 +<br>
src/defs.h | 1 +<br>
src/ioctl.c | 4 ++++<br>
src/vhost.c | 25 +++++++++++++++++++++++++<br>
5 files changed, 32 insertions(+)<br>
create mode 100644 src/vhost.c<br>
<br>
diff --git a/<a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a> b/<a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a><br>
index 125f37208..0649c55f0 100644<br>
--- a/<a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a><br>
+++ b/<a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a><br>
@@ -467,6 +467,7 @@ AC_CHECK_HEADERS(m4_normalize([<br>
AC_CHECK_HEADERS(m4_normalize([<br>
linux/ipc.h<br>
linux/kvm.h<br>
+ linux/vhost.h<br>
]),,, [AC_INCLUDES_DEFAULT<br>
#include <linux/types.h><br>
])<br>
diff --git a/src/Makefile.am b/src/Makefile.am<br>
index 6bd12eb60..098479f22 100644<br>
--- a/src/Makefile.am<br>
+++ b/src/Makefile.am<br>
@@ -395,6 +395,7 @@ libstrace_a_SOURCES = \<br>
utime.c \<br>
utimes.c \<br>
v4l2.c \<br>
+ vhost.c \<br>
wait.c \<br>
wait.h \<br>
watchdog_ioctl.c \<br>
diff --git a/src/defs.h b/src/defs.h<br>
index 811d2d850..27d2105d3 100644<br>
--- a/src/defs.h<br>
+++ b/src/defs.h<br>
@@ -1447,6 +1447,7 @@ DECL_IOCTL(tee);<br>
DECL_IOCTL(term);<br>
DECL_IOCTL(ubi);<br>
DECL_IOCTL(uffdio);<br>
+DECL_IOCTL(vhost);<br>
DECL_IOCTL(watchdog);<br>
# undef DECL_IOCTL<br>
<br>
diff --git a/src/ioctl.c b/src/ioctl.c<br>
index fc6cef933..58635df6d 100644<br>
--- a/src/ioctl.c<br>
+++ b/src/ioctl.c<br>
@@ -425,6 +425,10 @@ ioctl_decode(struct tcb *tcp, const struct finfo *finfo)<br>
#ifdef HAVE_LINUX_KVM_H<br>
case 0xae:<br>
return kvm_ioctl(tcp, code, arg);<br>
+#endif<br>
+#ifdef HAVE_LINUX_VHOST_H<br>
+ case 0xaf:<br>
+ return vhost_ioctl(tcp, code, arg);<br>
#endif<br>
case 0xb4:<br>
return gpio_ioctl(tcp, code, arg);<br>
diff --git a/src/vhost.c b/src/vhost.c<br>
new file mode 100644<br>
index 000000000..5df77e039<br>
--- /dev/null<br>
+++ b/src/vhost.c<br>
@@ -0,0 +1,25 @@<br>
+/*<br>
+ * Support for decoding of VHOST_* ioctl commands.<br>
+ *<br>
+ * Copyright (c) 2023 Daniel Cohen Hillel <<a href="mailto:danielcohenhillel@gmail.com" target="_blank">danielcohenhillel@gmail.com</a>><br>
+ * Copyright (c) 2023 The strace developers.<br>
+ * All rights reserved.<br>
+ *<br>
+ * SPDX-License-Identifier: LGPL-2.1-or-later<br>
+ */<br>
+<br>
+#include "defs.h"<br>
+<br>
+#ifdef HAVE_LINUX_VHOST_H<br>
+# include <linux/vhost.h><br>
+<br>
+int<br>
+vhost_ioctl(struct tcb *const tcp, const unsigned int code, const kernel_ulong_t arg)<br>
+{<br>
+ switch (code) {<br>
+ default:<br>
+ return RVAL_DECODED;<br>
+ }<br>
+}<br>
+<br>
+#endif /* HAVE_LINUX_VHOST_H */<br>
\ No newline at end of file<br>
-- <br>
2.34.1<br>
<br>
<br>
>From 2e85282a9547269fdf8df98609fbe83ef3084c03 Mon Sep 17 00:00:00 2001<br>
From: Daniel Cohen Hillel <<a href="mailto:danielcohenhillel@gmail.com" target="_blank">danielcohenhillel@gmail.com</a>><br>
Date: Sun, 22 Oct 2023 03:30:10 +0300<br>
Subject: [PATCH 2/2] vhost: decode arguments of various VHOST_* ioctls<br>
<br>
* <a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a> (AC_CHECK_TYPES): Add structs under `linux/vhost_types.h`:<br>
struct vhost_vring_file,<br>
struct vhost_vring_state,<br>
struct vhost_vring_addr,<br>
struct vhost_memory,<br>
struct vhost_memory_region<br>
* xlat/<a href="http://vhost_vring_flags.in" rel="noreferrer" target="_blank">vhost_vring_flags.in</a>: New file.<br>
* vhost.c:<br>
[HAVE_STRUCT_VHOST_VRING_FILE]: vhost_ioctl_decode_vring_file: New function.<br>
[HAVE_STRUCT_VHOST_VRING_STATE]: vhost_ioctl_decode_vring_state: New function.<br>
[HAVE_STRUCT_VHOST_VRING_ADDR]:<br>
include "xlat/vhost_vring_flags.h"<br>
and vhost_ioctl_decode_vring_set_addr: New function.<br>
[HAVE_STRUCT_VHOST_MEMORY, HAVE_STRUCT_VHOST_MEMORY_REGION]:<br>
print_vhost_memory_region, vhost_ioctl_decode_set_mem_table: New functions<br>
(vhost_ioctl): Added new functions to the switch case (under correct ifdefs)<br>
* List of new supported ioctls:<br>
- VHOST_SET_VRING_KICK<br>
- VHOST_SET_VRING_CALL<br>
- VHOST_SET_VRING_ERR<br>
- VHOST_NET_SET_BACKEND<br>
- VHOST_SET_VRING_NUM<br>
- VHOST_SET_VRING_BASE<br>
- VHOST_GET_VRING_BASE<br>
- VHOST_SET_VRING_ENDIAN<br>
- VHOST_GET_VRING_ENDIAN<br>
- VHOST_SET_VRING_BUSYLOOP_TIMEOUT<br>
- VHOST_GET_VRING_BUSYLOOP_TIMEOUT<br>
- VHOST_VDPA_SET_VRING_ENABLE<br>
- VHOST_SET_VRING_ADDR<br>
- VHOST_SET_MEM_TABLE<br>
---<br>
<a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a> | 8 ++<br>
src/vhost.c | 180 ++++++++++++++++++++++++++++++++++<br>
src/xlat/<a href="http://vhost_vring_flags.in" rel="noreferrer" target="_blank">vhost_vring_flags.in</a> | 1 +<br>
3 files changed, 189 insertions(+)<br>
create mode 100644 src/xlat/<a href="http://vhost_vring_flags.in" rel="noreferrer" target="_blank">vhost_vring_flags.in</a><br>
<br>
diff --git a/<a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a> b/<a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a><br>
index 0649c55f0..73da08ae2 100644<br>
--- a/<a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a><br>
+++ b/<a href="http://configure.ac" rel="noreferrer" target="_blank">configure.ac</a><br>
@@ -546,6 +546,14 @@ AC_CHECK_TYPES(m4_normalize([<br>
struct kvm_ioeventfd<br>
]),,, [#include <linux/kvm.h>])<br>
<br>
+AC_CHECK_TYPES(m4_normalize([<br>
+ struct vhost_vring_file,<br>
+ struct vhost_vring_state,<br>
+ struct vhost_vring_addr,<br>
+ struct vhost_memory,<br>
+ struct vhost_memory_region<br>
+]),,, [#include <linux/vhost_types.h>])<br>
+<br>
AC_CHECK_TYPES(m4_normalize([<br>
struct sockaddr_alg_new<br>
]),,, [#include <netinet/in.h><br>
diff --git a/src/vhost.c b/src/vhost.c<br>
index 5df77e039..42f1aa1ec 100644<br>
--- a/src/vhost.c<br>
+++ b/src/vhost.c<br>
@@ -13,10 +13,190 @@<br>
#ifdef HAVE_LINUX_VHOST_H<br>
# include <linux/vhost.h><br>
<br>
+# ifdef HAVE_STRUCT_VHOST_VRING_FILE<br>
+static int<br>
+vhost_ioctl_decode_vring_file(struct tcb *const tcp, const unsigned int code,<br>
+ const kernel_ulong_t arg)<br>
+{<br>
+ struct vhost_vring_file f;<br>
+<br>
+ if (entering(tcp) && (<br>
+ code == VHOST_GET_VRING_BASE<br>
+ || code == VHOST_GET_VRING_ENDIAN<br>
+ || code == VHOST_GET_VRING_BUSYLOOP_TIMEOUT)) {<br>
+ return 0;<br>
+ }<br>
+<br>
+ tprint_arg_next();<br>
+<br>
+ if (umove_or_printaddr(tcp, arg, &f))<br>
+ return RVAL_IOCTL_DECODED;<br>
+<br>
+ tprint_struct_begin();<br>
+<br>
+ PRINT_FIELD_U(f, index);<br>
+ tprint_struct_next();<br>
+ PRINT_FIELD_FD(f, fd, tcp);<br>
+<br>
+ tprint_struct_end();<br>
+<br>
+ return RVAL_IOCTL_DECODED;<br>
+}<br>
+# endif /* HAVE_STRUCT_VHOST_VRING_FILE */<br>
+<br>
+# ifdef HAVE_STRUCT_VHOST_VRING_STATE<br>
+static int<br>
+vhost_ioctl_decode_vring_state(struct tcb *const tcp, const unsigned int code,<br>
+ const kernel_ulong_t arg)<br>
+{<br>
+ struct vhost_vring_state s;<br>
+<br>
+ tprint_arg_next();<br>
+<br>
+ if (umove_or_printaddr(tcp, arg, &s))<br>
+ return RVAL_IOCTL_DECODED;<br>
+<br>
+ tprint_struct_begin();<br>
+<br>
+ PRINT_FIELD_U(s, index);<br>
+ tprint_struct_next();<br>
+ PRINT_FIELD_U(s, num);<br>
+<br>
+ tprint_struct_end();<br>
+<br>
+ return RVAL_IOCTL_DECODED;<br>
+}<br>
+# endif /* HAVE_STRUCT_VHOST_VRING_STATE */<br>
+<br>
+# ifdef HAVE_STRUCT_VHOST_VRING_ADDR<br>
+# include "xlat/vhost_vring_flags.h"<br>
+static int<br>
+vhost_ioctl_decode_vring_set_addr(struct tcb *const tcp, const unsigned int code,<br>
+ const kernel_ulong_t arg)<br>
+{<br>
+ struct vhost_vring_addr addr;<br>
+<br>
+ tprint_arg_next();<br>
+<br>
+ if (umove_or_printaddr(tcp, arg, &addr))<br>
+ return RVAL_IOCTL_DECODED;<br>
+<br>
+ tprint_struct_begin();<br>
+<br>
+ PRINT_FIELD_U(addr, index);<br>
+ tprint_struct_next();<br>
+ PRINT_FIELD_FLAGS(addr, flags, vhost_vring_flags, "VHOST_VRING_F_???");<br>
+ tprint_struct_next();<br>
+ PRINT_FIELD_X(addr, desc_user_addr);<br>
+ tprint_struct_next();<br>
+ PRINT_FIELD_X(addr, used_user_addr);<br>
+ tprint_struct_next();<br>
+ PRINT_FIELD_X(addr, avail_user_addr);<br>
+ if (addr.flags & VHOST_VRING_F_LOG) {<br>
+ tprint_struct_next();<br>
+ PRINT_FIELD_X(addr, log_guest_addr);<br>
+ }<br>
+<br>
+ tprint_struct_end();<br>
+<br>
+ return RVAL_IOCTL_DECODED;<br>
+}<br>
+# endif /* HAVE_STRUCT_VHOST_VRING_ADDR */<br>
+<br>
+# ifdef HAVE_STRUCT_VHOST_MEMORY<br>
+# ifdef HAVE_STRUCT_VHOST_MEMORY_REGION<br>
+static bool<br>
+print_vhost_memory_region(struct tcb *const tcp,<br>
+ void* elem_buf, size_t elem_size, void* data)<br>
+{<br>
+ struct vhost_memory_region region = *((struct vhost_memory_region *)elem_buf);<br>
+<br>
+ tprint_struct_begin();<br>
+<br>
+ PRINT_FIELD_X(region, guest_phys_addr);<br>
+ tprint_struct_next();<br>
+ PRINT_FIELD_U(region, memory_size);<br>
+ tprint_struct_next();<br>
+ PRINT_FIELD_X(region, userspace_addr);<br>
+<br>
+ // No flags currently specified<br>
+ if (region.flags_padding != 0) {<br>
+ tprint_struct_next();<br>
+ PRINT_FIELD_X(region, flags_padding);<br>
+ }<br>
+<br>
+ tprint_struct_end();<br>
+<br>
+ return true;<br>
+}<br>
+<br>
+static int<br>
+vhost_ioctl_decode_set_mem_table(struct tcb *const tcp, const unsigned int code,<br>
+ const kernel_ulong_t arg)<br>
+{<br>
+ struct vhost_memory vhost_memory_hdr;<br>
+<br>
+ tprint_arg_next();<br>
+<br>
+ if (umove_or_printaddr(tcp, arg, &vhost_memory_hdr))<br>
+ return RVAL_IOCTL_DECODED;<br>
+<br>
+ tprint_struct_begin();<br>
+<br>
+ PRINT_FIELD_U(vhost_memory_hdr, nregions);<br>
+ tprint_struct_next();<br>
+<br>
+ if (abbrev(tcp)) {<br>
+ tprint_array_begin();<br>
+ if (vhost_memory_hdr.nregions)<br>
+ tprint_more_data_follows();<br>
+ tprint_array_end();<br>
+ } else {<br>
+ struct vhost_memory_region region;<br>
+ print_array(tcp, arg + sizeof(vhost_memory_hdr), vhost_memory_hdr.nregions,<br>
+ ®ion, sizeof(region), tfetch_mem,<br>
+ print_vhost_memory_region, NULL);<br>
+ }<br>
+<br>
+ tprint_struct_end();<br>
+<br>
+ return RVAL_IOCTL_DECODED;<br>
+}<br>
+# endif /* HAVE_STRUCT_VHOST_MEMORY */<br>
+# endif /* HAVE_STRUCT_VHOST_MEMORY_REGION */<br>
+<br>
int<br>
vhost_ioctl(struct tcb *const tcp, const unsigned int code, const kernel_ulong_t arg)<br>
{<br>
switch (code) {<br>
+ # ifdef HAVE_STRUCT_VHOST_VRING_FILE<br>
+ case VHOST_SET_VRING_KICK:<br>
+ case VHOST_SET_VRING_CALL:<br>
+ case VHOST_SET_VRING_ERR:<br>
+ case VHOST_NET_SET_BACKEND:<br>
+ return vhost_ioctl_decode_vring_file(tcp, code, arg);<br>
+ # endif<br>
+ # ifdef HAVE_STRUCT_VHOST_VRING_STATE<br>
+ case VHOST_SET_VRING_NUM:<br>
+ case VHOST_SET_VRING_BASE:<br>
+ case VHOST_GET_VRING_BASE:<br>
+ case VHOST_SET_VRING_ENDIAN:<br>
+ case VHOST_GET_VRING_ENDIAN:<br>
+ case VHOST_SET_VRING_BUSYLOOP_TIMEOUT:<br>
+ case VHOST_GET_VRING_BUSYLOOP_TIMEOUT:<br>
+ case VHOST_VDPA_SET_VRING_ENABLE:<br>
+ return vhost_ioctl_decode_vring_state(tcp, code, arg);<br>
+ # endif<br>
+ # ifdef HAVE_STRUCT_VHOST_VRING_STATE<br>
+ case VHOST_SET_VRING_ADDR:<br>
+ return vhost_ioctl_decode_vring_set_addr(tcp, code, arg);<br>
+ # endif<br>
+ # ifdef HAVE_STRUCT_VHOST_MEMORY<br>
+ # ifdef HAVE_STRUCT_VHOST_MEMORY_REGION<br>
+ case VHOST_SET_MEM_TABLE:<br>
+ return vhost_ioctl_decode_set_mem_table(tcp, code, arg);<br>
+ # endif<br>
+ # endif<br>
default:<br>
return RVAL_DECODED;<br>
}<br>
diff --git a/src/xlat/<a href="http://vhost_vring_flags.in" rel="noreferrer" target="_blank">vhost_vring_flags.in</a> b/src/xlat/<a href="http://vhost_vring_flags.in" rel="noreferrer" target="_blank">vhost_vring_flags.in</a><br>
new file mode 100644<br>
index 000000000..2be344666<br>
--- /dev/null<br>
+++ b/src/xlat/<a href="http://vhost_vring_flags.in" rel="noreferrer" target="_blank">vhost_vring_flags.in</a><br>
@@ -0,0 +1 @@<br>
+VHOST_VRING_F_LOG (1<<0)<br>
\ No newline at end of file<br>
-- <br>
2.34.1<br>
<br>
</blockquote></div><br clear="all"><div><br></div><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><span style="color:rgb(111,168,220);font-family:"courier new",monospace">Daniel Cohen Hillel דניאל כהן הלל</span><br></div><div><font face="courier new, monospace" color="#6fa8dc">ID: 212553804 :ת"ז</font></div></div></div></div>