linux-user: add syscall-filter and fork-cpu in plugin interfaces
RFC PATCH 1/1 linux-user: add syscall-filter and fork-cpu in plugin interfaces
This commit introduces a syscall filtering mechanism for user-mode emulation, allowing plugins to intercept and handle system calls.
Key features:
- New API functions: qemu_plugin_set_syscall_filter() and qemu_plugin_fork_cpu_loop()
- A new enum qemu_plugin_syscall_filter_ret to represent filter results
- Integration into the cpu loop for i386 and x86_64
- A new header file include/user/syscall-filter.h
The filtering mechanism works by allowing a plugin to register a callback that is invoked before each system call. The callback can decide whether to pass, skip, or exit the current cpu loop. When skipping, the plugin can provide a value to be returned to the guest.
Additionally, a new function qemu_plugin_fork_cpu_loop() is provided to allow plugins to fork a new cpu loop, which is useful for temporarily resuming guest emulation (e.g., for executing callbacks) during the first system call.
The implementation inserts the filter checks in the cpu loop for both the int $0x80 (i386) and syscall (x86_64) paths.
At KVM Forum 2025, we presented Lorelei, a system designed to enable guest programs executed by the QEMU user-mode emulator to accelerate performance by invoking natively compiled host libraries via syscalls. We have successfully supported libraries such as SDL, OpenGL, and Vulkan, allowing QEMU user-mode to emulate GPU-dependent games.
Following an invitation from Mr. Bouvier to upstream Lorelei to QEMU, we refined its architecture to load the Lorelei module as a TCG plugin.
Since host libraries may execute guest function pointers (callbacks), Lorelei implements a novel nested execution model:
- During syscall filtering, Lorelei triggers a temporary cpu_loop when a host library begins executing a guest callback
- This temporarily reverts to full emulation mode to execute the guest callback
- The guest code then issues a magic syscall to exit the temporary cpu_loop
- Control returns to the host library's original execution context
Slides: https://gitlab.com/qemu-project/kvm-forum/-/raw/main/_attachments/2025/Slides_DQPMaZE.pdf
Video: https://www.youtube.com/watch?v=_jioQFm7wyU&list=PLW3ep1uCIRfxwmllXTOA2txfDWN6vUOHp&index=22
--- include/qemu/plugin.h | 28 +++++++++++++++++++ include/qemu/qemu-plugin.h | 44 +++++++++++++++++++++++++++++ include/user/syscall-filter.h | 36 ++++++++++++++++++++++++ linux-user/i386/cpu_loop.c | 52 +++++++++++++++++++++++++++++++++-- linux-user/main.c | 1 + linux-user/user-internals.h | 1 + plugins/api.c | 14 ++++++++++ plugins/core.c | 31 +++++++++++++++++++++ plugins/plugin.h | 8 ++++++ 9 files changed, 213 insertions(+), 2 deletions(-) create mode 100644 include/user/syscall-filter.h
include/qemu/plugin.h +28 -0
@@ -207,6 +207,22 @@ void qemu_plugin_user_prefork_lock(void);*/ void qemu_plugin_user_postfork (bool is_child); +/** +* qemu_plugin_filter_syscall(): filter a system call +* @cpu: CPUState pointer +* @num: syscall number +* @a1-a8: syscall arguments +* @sysret: pointer to the sysret value, not used if passed +* @return: syscall filter return value +*/ +int qemu_plugin_filter_syscall (CPUState *cpu, +int num,uint64_t a1,uint64_t a2, +uint64_t a3,uint64_t a4,uint64_t a5, +uint64_t a6,uint64_t a7,uint64_t a8, +uint64_t *sysret); + +void qemu_plugin_set_fork_cpu_loop_entry (void (*entry)(uint64_t )); +#else /* !CONFIG_PLUGIN */ static inline void qemu_plugin_add_opts (void )
@@ -277,6 +293,18 @@ static inline void qemu_plugin_user_prefork_lock(void)static inline void qemu_plugin_user_postfork (bool is_child) { } +static inline +int qemu_plugin_filter_syscall (CPUState *cpu, +int num,uint64_t a1,uint64_t a2, +uint64_t a3,uint64_t a4,uint64_t a5, +uint64_t a6,uint64_t a7,uint64_t a8, +uint64_t *sysret) +{ } + +static inline +void qemu_plugin_set_fork_cpu_loop_entry (void (*entry)(uint64_t )) +{ } +#endif /* !CONFIG_PLUGIN */ #endif /* QEMU_PLUGIN_H */
include/qemu/qemu-plugin.h +44 -0
48 lines not on this page · raw message
include/user/syscall-filter.h new file +36 -0
@@ -0,0 +1,36 @@ +/* +* System Call Filter Wrappers for *-user +* +* Copyright (c) 2019 Linaro +* Written by Ziyang Zhang <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef SYSCALL_FILTER_H +#define SYSCALL_FILTER_H + +#ifndef CONFIG_USER_ONLY +#error Cannot include this header from system emulation +#endif + +#include "user/abitypes.h" +#include "qemu/plugin.h" + +static inline int filter_syscall (CPUState *cpu,int num, +abi_long arg1,abi_long arg2, +abi_long arg3,abi_long arg4, +abi_long arg5,abi_long arg6, +abi_long arg7,abi_long arg8,abi_ulong *sysret) +{ +uint64_t sysret64 =0 ; +int ret =qemu_plugin_filter_syscall (cpu, num, + arg1, arg2, arg3, arg4, + arg5, arg6, arg7, arg8, &sysret64); +if (ret !=QEMU_PLUGIN_SYSCALL_FILTER_PASS ) { + *sysret = sysret64; + } +return ret; +} + +#endif // SYSCALL_FILTER_H
linux-user/i386/cpu_loop.c +50 -2
76 lines not on this page · raw message
linux-user/main.c +1 -0
@@ -738,6 +738,7 @@ int main(int argc, char **argv, char **envp) }trace_init_file ();qemu_plugin_load_list (&plugins, &error_fatal); +qemu_plugin_set_fork_cpu_loop_entry (fork_cpu_loop);/* Zero out regs */ memset (regs,0 ,sizeof (struct target_pt_regs ));
linux-user/user-internals.h +1 -0
@@ -66,6 +66,7 @@ abi_long do_syscall(CPUArchState *cpu_env, int num, abi_long arg1, abi_long arg8);extern __threadCPUState *thread_cpu; G_NORETURN void cpu_loop(CPUArchState *env); +void fork_cpu_loop (uint64_t sysret);abi_long get_errno (abi_long ret);const char *target_strerror (int err);int get_osversion (void );
plugins/api.c +14 -0
@@ -628,3 +628,17 @@ void qemu_plugin_update_ns(const void *handle, int64_t new_time) }#endif } + +bool qemu_plugin_set_syscall_filter (qemu_plugin_id_t id, +qemu_plugin_syscall_filter_cb_t cb) +{ +#ifdef CONFIG_USER_ONLY +return plugin_set_syscall_filter (id, cb); +#else +return false; +#endif +} + +void qemu_plugin_fork_cpu_loop (uint64_t sysret) { +plugin_fork_cpu_loop (sysret); +}
plugins/core.c +31 -0
@@ -692,6 +692,21 @@ void qemu_plugin_user_postfork(bool is_child) } } +int qemu_plugin_filter_syscall (CPUState *cpu, +int num,uint64_t a1,uint64_t a2, +uint64_t a3,uint64_t a4,uint64_t a5, +uint64_t a6,uint64_t a7,uint64_t a8, +uint64_t *sysret) { +if (plugin.syscall_filter_cb) { +return plugin.syscall_filter_cb (num, a1, a2, a3, a4, a5, a6, a7, a8, sysret); + } +return QEMU_PLUGIN_SYSCALL_FILTER_PASS ; +} + +void qemu_plugin_set_fork_cpu_loop_entry (void (*entry)(uint64_t )) { + plugin.fork_cpu_loop_entry = entry; +} +static bool plugin_dyn_cb_arr_cmp (const void *ap,const void *bp) {return ap == bp;
@@ -743,3 +758,19 @@ void plugin_scoreboard_free(struct qemu_plugin_scoreboard *score)g_array_free (score->data, TRUE);g_free (score); } + +bool plugin_set_syscall_filter (qemu_plugin_id_t id, +qemu_plugin_syscall_filter_cb_t cb) +{ +if (plugin.syscall_filter_cb) { +return false; + } + plugin.syscall_filter_cb = cb; +return true; +} + +void plugin_fork_cpu_loop (uint64_t sysret) { +if (plugin.fork_cpu_loop_entry) { + plugin.fork_cpu_loop_entry (sysret); + } +}
plugins/plugin.h +8 -0
@@ -48,6 +48,9 @@ struct qemu_plugin_state {struct qht dyn_cb_arr_ht;/* How many vcpus were started */ int num_vcpus; + +qemu_plugin_syscall_filter_cb_t syscall_filter_cb; +void (*fork_cpu_loop_entry)(uint64_t ); };
@@ -118,4 +121,9 @@ struct qemu_plugin_scoreboard *plugin_scoreboard_new(size_t element_size);void plugin_scoreboard_free (struct qemu_plugin_scoreboard *score); +bool plugin_set_syscall_filter (qemu_plugin_id_t id, +qemu_plugin_syscall_filter_cb_t cb); + +void plugin_fork_cpu_loop (uint64_t sysret); +#endif /* PLUGIN_H */
Signature · 1 line
2.25.1
Hy Ziyang,
Quoted 395 lines · Ziyang Zhang
This commit introduces a syscall filtering mechanism for user-mode emulation, allowing plugins to intercept and handle system calls.
Key features:
- New API functions:qemu_plugin_set_syscall_filter()andqemu_plugin_fork_cpu_loop()
- A new enumqemu_plugin_syscall_filter_retto represent filter results
- Integration into the cpu loop for i386 andx86_64
- A new header fileinclude/user/syscall-filter.hThe filtering mechanism works by allowing a plugin to register a callback that is invoked before each system call. The callback can decide whether to pass, skip, or exit the current cpu loop. When skipping, the plugin can provide a value to be returned to the guest.
Additionally, a new function
qemu_plugin_fork_cpu_loop()is provided to allow plugins to fork a new cpu loop, which is useful for temporarily resuming guest emulation (e.g., for executing callbacks) during the first system call.The implementation inserts the filter checks in the cpu loop for both the int $0x80 (i386) and syscall (
x86_64) paths.At KVM Forum 2025, we presented Lorelei, a system designed to enable guest programs executed by the QEMU user-mode emulator to accelerate performance by invoking natively compiled host libraries via syscalls. We have successfully supported libraries such as SDL, OpenGL, and Vulkan, allowing QEMU user-mode to emulate GPU-dependent games.
Following an invitation from Mr. Bouvier to upstream Lorelei to QEMU, we refined its architecture to load the Lorelei module as a TCG plugin.
Since host libraries may execute guest function pointers (callbacks), Lorelei implements a novel nested execution model:
- During syscall filtering, Lorelei triggers a temporary
cpu_loopwhen a host library begins executing a guest callback
- This temporarily reverts to full emulation mode to execute the guest callback
- The guest code then issues a magic syscall to exit the temporarycpu_loop
- Control returns to the host library's original execution contextSlides: https://gitlab.com/qemu-project/kvm-forum/-/raw/main/_attachments/2025/Slides_DQPMaZE.pdf
Video: https://www.youtube.com/watch?v=_jioQFm7wyU&list=PLW3ep1uCIRfxwmllXTOA2txfDWN6vUOHp&index=22Signed-off-by Ziyang Zhang <[email protected]>--- include/qemu/plugin.h | 28 +++++++++++++++++++ include/qemu/qemu-plugin.h | 44 +++++++++++++++++++++++++++++ include/user/syscall-filter.h | 36 ++++++++++++++++++++++++ linux-user/i386/cpu_loop.c | 52 +++++++++++++++++++++++++++++++++-- linux-user/main.c | 1 + linux-user/user-internals.h | 1 + plugins/api.c | 14 ++++++++++ plugins/core.c | 31 +++++++++++++++++++++ plugins/plugin.h | 8 ++++++ 9 files changed, 213 insertions(+), 2 deletions(-)create mode 100644
include/user/syscall-filter.hinclude/qemu/plugin.h +28
-0@@ -207,6 +207,22 @@ void qemu_plugin_user_prefork_lock(void);*/ void qemu_plugin_user_postfork (bool is_child); +/** +* qemu_plugin_filter_syscall(): filter a system call +* @cpu: CPUState pointer +* @num: syscall number +* @a1-a8: syscall arguments +* @sysret: pointer to the sysret value, not used if passed +* @return: syscall filter return value +*/ +int qemu_plugin_filter_syscall (CPUState *cpu, +int num,uint64_t a1,uint64_t a2, +uint64_t a3,uint64_t a4,uint64_t a5, +uint64_t a6,uint64_t a7,uint64_t a8, +uint64_t *sysret); + +void qemu_plugin_set_fork_cpu_loop_entry (void (*entry)(uint64_t )); +#else /* !CONFIG_PLUGIN */ static inline void qemu_plugin_add_opts (void )@@ -277,6 +293,18 @@ static inline void qemu_plugin_user_prefork_lock(void)static inline void qemu_plugin_user_postfork (bool is_child) { } +static inline +int qemu_plugin_filter_syscall (CPUState *cpu, +int num,uint64_t a1,uint64_t a2, +uint64_t a3,uint64_t a4,uint64_t a5, +uint64_t a6,uint64_t a7,uint64_t a8, +uint64_t *sysret) +{ } + +static inline +void qemu_plugin_set_fork_cpu_loop_entry (void (*entry)(uint64_t )) +{ } +#endif /* !CONFIG_PLUGIN */ #endif /* QEMU_PLUGIN_H */ include/qemu/qemu-plugin.h +44
-0@@ -939,4 +939,48 @@ void qemu_plugin_u64_set(qemu_plugin_u64 entry, unsigned int vcpu_index,QEMU_PLUGIN_API uint64_t qemu_plugin_u64_sum(qemu_plugin_u64 entry); +/** +* enum qemu_plugin_syscall_filter_ret - syscall filter return values +* +* @QEMU_PLUGIN_SYSCALL_FILTER_PASS: control should continue as usual +* @QEMU_PLUGIN_SYSCALL_FILTER_SKIP: syscall is filtered and skipped +* @QEMU_PLUGIN_SYSCALL_FILTER_EXIT: current cpu loop should exit +* +*/ +enum qemu_plugin_syscall_filter_ret { +QEMU_PLUGIN_SYSCALL_FILTER_PASS =0 , +QEMU_PLUGIN_SYSCALL_FILTER_SKIP , +QEMU_PLUGIN_SYSCALL_FILTER_EXIT , +}; + +/** +* typedef qemu_plugin_syscall_filter_cb_t - syscall filter callback +* @num: syscall number +* @a1-a8: syscall arguments +* @sysret: pointer to the sysret value, only used when returning SKIP +* @return: syscall filter return value +*/ +typedef int +(*qemu_plugin_syscall_filter_cb_t )(uint64_t num,uint64_t a1,uint64_t a2, +uint64_t a3,uint64_t a4,uint64_t a5, +uint64_t a6,uint64_t a7,uint64_t a8, +uint64_t *sysret); + +/** +* qemu_plugin_set_syscall_filter() - set the unique syscall filter +* @id: plugin ID +* @cb: callback function to filter the syscall +* +* Returns true if the filter was set, false if the syscall number is already +* registered or if the callback is NULL. +*/ +bool qemu_plugin_set_syscall_filter (qemu_plugin_id_t id, +qemu_plugin_syscall_filter_cb_t cb); + +/** +* qemu_plugin_fork_cpu_loop() - fork a new cpu loop +* @sysret: value to return to the child cpu loop +*/ +void qemu_plugin_fork_cpu_loop (uint64_t sysret); +#endif /* QEMU_QEMU_PLUGIN_H */ include/user/syscall-filter.h new file +36
-0@@ -0,0 +1,36 @@ +/* +* System Call Filter Wrappers for *-user +* +* Copyright (c) 2019 Linaro +* Written by Ziyang Zhang <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef SYSCALL_FILTER_H +#define SYSCALL_FILTER_H + +#ifndef CONFIG_USER_ONLY +#error Cannot include this header from system emulation +#endif + +#include "user/abitypes.h" +#include "qemu/plugin.h" + +static inline int filter_syscall (CPUState *cpu,int num, +abi_long arg1,abi_long arg2, +abi_long arg3,abi_long arg4, +abi_long arg5,abi_long arg6, +abi_long arg7,abi_long arg8,abi_ulong *sysret) +{ +uint64_t sysret64 =0 ; +int ret =qemu_plugin_filter_syscall (cpu, num, + arg1, arg2, arg3, arg4, + arg5, arg6, arg7, arg8, &sysret64); +if (ret !=QEMU_PLUGIN_SYSCALL_FILTER_PASS ) { + *sysret = sysret64; + } +return ret; +} + +#endif // SYSCALL_FILTER_H linux-user/i386/cpu_loop.c +50
-2@@ -24,6 +24,7 @@#include "cpu_loop-common.h" #include "signal-common.h" #include "user-mmap.h" +#include "user/syscall-filter.h" /***********************************************************/ /* CPUX86 core interface */ @@ -203,8 +204,21 @@ static bool maybe_handle_vm86_trap(CPUX86State *env, int trapnr)return false; } -void cpu_loop (CPUX86State *env) -{ +static void cpu_loop_impl (CPUX86State *env); + +void cpu_loop (CPUX86State *env) { +cpu_loop_impl (env); +__builtin_unreachable (); +} + +void fork_cpu_loop (uint64_t sysret) { +CPUX86State *env =cpu_env (thread_cpu); + env->regs[R_EAX ] = sysret; +process_pending_signals (env); +cpu_loop_impl (env); +} + +static void cpu_loop_impl (CPUX86State *env) {CPUState *cs =env_cpu (env);int trapnr;abi_ulong ret;@@ -220,6 +234,23 @@ void cpu_loop(CPUX86State *env)#ifndef TARGET_X86_64 case EXCP_SYSCALL :#endif +/* syscall filter */ + { +int syscall_filter_ret =filter_syscall (cs, + env->regs[R_EAX ], + env->regs[R_EBX ], + env->regs[R_ECX ], + env->regs[R_EDX ], + env->regs[R_ESI ], + env->regs[R_EDI ], + env->regs[R_EBP ], +0 ,0 , &ret); +if (syscall_filter_ret ==QEMU_PLUGIN_SYSCALL_FILTER_EXIT ) +return ; +else if (syscall_filter_ret ==QEMU_PLUGIN_SYSCALL_FILTER_SKIP ) +break ; + } +/* linux syscall from int $0x80 */ ret = do_syscall(env, env->regs[R_EAX],@@ -238,6 +269,23 @@ void cpu_loop(CPUX86State *env)break ;#ifdef TARGET_X86_64 case EXCP_SYSCALL : +/* syscall filter */ + { +int syscall_filter_ret =filter_syscall (cs, + env->regs[R_EAX ], + env->regs[R_EDI ], + env->regs[R_ESI ], + env->regs[R_EDX ], + env->regs[10 ], + env->regs[8 ], + env->regs[9 ], +0 ,0 , &ret); +if (syscall_filter_ret ==QEMU_PLUGIN_SYSCALL_FILTER_EXIT ) +return ; +else if (syscall_filter_ret ==QEMU_PLUGIN_SYSCALL_FILTER_SKIP ) +break ; + } +/* linux syscall from syscall instruction. */ ret = do_syscall(env, env->regs[R_EAX],linux-user/main.c +1
-0@@ -738,6 +738,7 @@ int main(int argc, char **argv, char **envp) }trace_init_file ();qemu_plugin_load_list (&plugins, &error_fatal); +qemu_plugin_set_fork_cpu_loop_entry (fork_cpu_loop);/* Zero out regs */ memset (regs,0 ,sizeof (struct target_pt_regs ));linux-user/user-internals.h +1
-0@@ -66,6 +66,7 @@ abi_long do_syscall(CPUArchState *cpu_env, int num, abi_long arg1, abi_long arg8);extern __threadCPUState *thread_cpu; G_NORETURN void cpu_loop(CPUArchState *env); +void fork_cpu_loop (uint64_t sysret);abi_long get_errno (abi_long ret);const char *target_strerror (int err);int get_osversion (void );plugins/api.c +14
-0@@ -628,3 +628,17 @@ void qemu_plugin_update_ns(const void *handle, int64_t new_time) }#endif } + +bool qemu_plugin_set_syscall_filter (qemu_plugin_id_t id, +qemu_plugin_syscall_filter_cb_t cb) +{ +#ifdef CONFIG_USER_ONLY +return plugin_set_syscall_filter (id, cb); +#else +return false; +#endif +} + +void qemu_plugin_fork_cpu_loop (uint64_t sysret) { +plugin_fork_cpu_loop (sysret); +}plugins/core.c +31
-0@@ -692,6 +692,21 @@ void qemu_plugin_user_postfork(bool is_child) } } +int qemu_plugin_filter_syscall (CPUState *cpu, +int num,uint64_t a1,uint64_t a2, +uint64_t a3,uint64_t a4,uint64_t a5, +uint64_t a6,uint64_t a7,uint64_t a8, +uint64_t *sysret) { +if (plugin.syscall_filter_cb) { +return plugin.syscall_filter_cb (num, a1, a2, a3, a4, a5, a6, a7, a8, sysret); + } +return QEMU_PLUGIN_SYSCALL_FILTER_PASS ; +} + +void qemu_plugin_set_fork_cpu_loop_entry (void (*entry)(uint64_t )) { + plugin.fork_cpu_loop_entry = entry; +} +static bool plugin_dyn_cb_arr_cmp (const void *ap,const void *bp) {return ap == bp;@@ -743,3 +758,19 @@ void plugin_scoreboard_free(struct qemu_plugin_scoreboard *score)g_array_free (score->data, TRUE);g_free (score); } + +bool plugin_set_syscall_filter (qemu_plugin_id_t id, +qemu_plugin_syscall_filter_cb_t cb) +{ +if (plugin.syscall_filter_cb) { +return false; + } + plugin.syscall_filter_cb = cb; +return true; +} + +void plugin_fork_cpu_loop (uint64_t sysret) { +if (plugin.fork_cpu_loop_entry) { + plugin.fork_cpu_loop_entry (sysret); + } +}plugins/plugin.h +3
-0@@ -48,6 +48,9 @@ struct qemu_plugin_state {struct qht dyn_cb_arr_ht;/* How many vcpus were started */ int num_vcpus; + +qemu_plugin_syscall_filter_cb_t syscall_filter_cb; +void (*fork_cpu_loop_entry)(uint64_t ); };
plugins/plugin.h struct qemu_plugin_scoreboard *plugin_scoreboard_new(size_t element_size); · line 129
Quoted 9 lines
plugins/plugin.h +5
-0@@ -118,4 +121,9 @@ struct qemu_plugin_scoreboard *plugin_scoreboard_new(size_t element_size);void plugin_scoreboard_free (struct qemu_plugin_scoreboard *score); +bool plugin_set_syscall_filter (qemu_plugin_id_t id, +qemu_plugin_syscall_filter_cb_t cb); + +void plugin_fork_cpu_loop (uint64_t sysret); +#endif /* PLUGIN_H */
thanks for posting this!
Just for sake of centralizing the conversation, let's ignore this thread, and answer of the other one, where you added someone else in copy.
I'll answer on other thread.
Pierrick