From d1c4bc511592de1acedf943a0d35676a0a92fb00 Mon Sep 17 00:00:00 2001 From: Norman Feske Date: Wed, 15 Jan 2025 16:36:09 +0100 Subject: [PATCH] vm_session: use Callable for with_state Issue #5420 --- repos/base-foc/src/lib/base/x86/vm.cc | 10 ++++---- repos/base-hw/src/lib/base/vm.cc | 13 +++++----- repos/base-hw/src/lib/base/x86_64/vm.cc | 12 ++++++---- repos/base-nova/src/lib/base/vm.cc | 12 ++++++---- repos/base-sel4/src/lib/base/x86/vm.cc | 11 +++++---- repos/base/include/vm_session/connection.h | 28 ++++------------------ repos/base/lib/symbols/ld | 2 +- repos/base/src/lib/base/vm.cc | 5 ++-- 8 files changed, 42 insertions(+), 51 deletions(-) diff --git a/repos/base-foc/src/lib/base/x86/vm.cc b/repos/base-foc/src/lib/base/x86/vm.cc index 72a4af33bb..9d9685a94d 100644 --- a/repos/base-foc/src/lib/base/x86/vm.cc +++ b/repos/base-foc/src/lib/base/x86/vm.cc @@ -42,7 +42,6 @@ namespace Foc { using namespace Genode; using Exit_config = Vm_connection::Exit_config; -using Call_with_state = Vm_connection::Call_with_state; enum Virt { VMX, SVM, UNKNOWN }; @@ -1341,7 +1340,7 @@ struct Foc_vcpu : Thread, Noncopyable _wake_up.up(); } - void with_state(Call_with_state &cw) + void with_state(auto const &fn) { if (!_dispatching) { if (Thread::myself() != _ep_handler) { @@ -1374,7 +1373,7 @@ struct Foc_vcpu : Thread, Noncopyable _state_ready.down(); } - if (cw.call_with_state(_vcpu_state) + if (fn(_vcpu_state) || _extra_dispatch_up) resume(); @@ -1416,7 +1415,10 @@ static enum Virt virt_type(Env &env) ** vCPU API ** **************/ -void Vm_connection::Vcpu::_with_state(Call_with_state &cw) { static_cast(_native_vcpu).vcpu.with_state(cw); } +void Vm_connection::Vcpu::_with_state(With_state::Ft const &fn) +{ + static_cast(_native_vcpu).vcpu.with_state(fn); +} Vm_connection::Vcpu::Vcpu(Vm_connection &vm, Allocator &alloc, diff --git a/repos/base-hw/src/lib/base/vm.cc b/repos/base-hw/src/lib/base/vm.cc index 0218bd558a..0938448c92 100644 --- a/repos/base-hw/src/lib/base/vm.cc +++ b/repos/base-hw/src/lib/base/vm.cc @@ -27,7 +27,6 @@ using namespace Genode; using Exit_config = Vm_connection::Exit_config; -using Call_with_state = Vm_connection::Call_with_state; /**************************** @@ -56,8 +55,7 @@ struct Hw_vcpu : Rpc_client, Noncopyable Hw_vcpu(Env &, Vm_connection &, Vcpu_handler_base &); - - void with_state(Call_with_state &); + void with_state(auto const &); }; @@ -72,7 +70,7 @@ Hw_vcpu::Hw_vcpu(Env &env, Vm_connection &vm, Vcpu_handler_base &handler) } -void Hw_vcpu::with_state(Call_with_state &cw) +void Hw_vcpu::with_state(auto const &fn) { if (Thread::myself() != _ep_handler) { error("vCPU state requested outside of vcpu_handler EP"); @@ -80,7 +78,7 @@ void Hw_vcpu::with_state(Call_with_state &cw) } Kernel::pause_vm(Capability_space::capid(_kernel_vcpu)); - if (cw.call_with_state(_local_state())) + if (fn(_local_state())) Kernel::run_vm(Capability_space::capid(_kernel_vcpu)); } @@ -98,7 +96,10 @@ Capability Hw_vcpu::_create_vcpu(Vm_connection &vm, ** vCPU API ** **************/ -void Vm_connection::Vcpu::_with_state(Call_with_state &cw) { static_cast(_native_vcpu).with_state(cw); } +void Vm_connection::Vcpu::_with_state(With_state::Ft const &fn) +{ + static_cast(_native_vcpu).with_state(fn); +} Vm_connection::Vcpu::Vcpu(Vm_connection &vm, Allocator &alloc, diff --git a/repos/base-hw/src/lib/base/x86_64/vm.cc b/repos/base-hw/src/lib/base/x86_64/vm.cc index f08b48d373..41f5004f96 100644 --- a/repos/base-hw/src/lib/base/x86_64/vm.cc +++ b/repos/base-hw/src/lib/base/x86_64/vm.cc @@ -29,7 +29,6 @@ using namespace Genode; using Exit_config = Vm_connection::Exit_config; -using Call_with_state = Vm_connection::Call_with_state; /**************************** @@ -58,7 +57,7 @@ struct Hw_vcpu : Rpc_client, Noncopyable Hw_vcpu(Env &, Vm_connection &, Vcpu_handler_base &); - void with_state(Call_with_state &); + void with_state(auto const &fn); }; @@ -84,7 +83,7 @@ void Hw_vcpu::_run() } -void Hw_vcpu::with_state(Call_with_state &cw) +void Hw_vcpu::with_state(auto const &fn) { if (Thread::myself() != _ep_handler) { error("vCPU state requested outside of vcpu_handler EP"); @@ -92,7 +91,7 @@ void Hw_vcpu::with_state(Call_with_state &cw) } Kernel::pause_vm(Capability_space::capid(_kernel_vcpu)); - if(cw.call_with_state(_local_state())) + if (fn(_local_state())) _run(); } @@ -110,7 +109,10 @@ Capability Hw_vcpu::_create_vcpu(Vm_connection &vm, ** vCPU API ** **************/ -void Vm_connection::Vcpu::_with_state(Call_with_state &cw) { static_cast(_native_vcpu).with_state(cw); } +void Vm_connection::Vcpu::_with_state(With_state::Ft const &fn) +{ + static_cast(_native_vcpu).with_state(fn); +} Vm_connection::Vcpu::Vcpu(Vm_connection &vm, Allocator &alloc, diff --git a/repos/base-nova/src/lib/base/vm.cc b/repos/base-nova/src/lib/base/vm.cc index a4fb5b79e1..6f57fba4ea 100644 --- a/repos/base-nova/src/lib/base/vm.cc +++ b/repos/base-nova/src/lib/base/vm.cc @@ -35,7 +35,6 @@ using namespace Genode; using Exit_config = Vm_connection::Exit_config; -using Call_with_state = Vm_connection::Call_with_state; /****************************** @@ -163,7 +162,7 @@ struct Nova_vcpu : Rpc_client, Noncopyable call(); } - void with_state(Call_with_state &cw); + void with_state(auto const &fn); }; @@ -601,7 +600,7 @@ void Nova_vcpu::_handle_exit(Nova::Utcb &utcb) } -void Nova_vcpu::with_state(Call_with_state &cw) +void Nova_vcpu::with_state(auto const &fn) { Thread *myself = Thread::myself(); bool remote = (_dispatching != myself); @@ -626,7 +625,7 @@ void Nova_vcpu::with_state(Call_with_state &cw) _read_nova_state(utcb); } - _resume = cw.call_with_state(_vcpu_state); + _resume = fn(_vcpu_state); if (remote) { _write_nova_state(utcb); @@ -762,7 +761,10 @@ Nova_vcpu::Nova_vcpu(Env &env, Vm_connection &vm, Allocator &alloc, ** vCPU API ** **************/ -void Vm_connection::Vcpu::_with_state(Call_with_state &cw) { static_cast(_native_vcpu).with_state(cw); } +void Vm_connection::Vcpu::_with_state(With_state::Ft const &fn) +{ + static_cast(_native_vcpu).with_state(fn); +} Vm_connection::Vcpu::Vcpu(Vm_connection &vm, Allocator &alloc, diff --git a/repos/base-sel4/src/lib/base/x86/vm.cc b/repos/base-sel4/src/lib/base/x86/vm.cc index 0bf434d45e..a0119d8db5 100644 --- a/repos/base-sel4/src/lib/base/x86/vm.cc +++ b/repos/base-sel4/src/lib/base/x86/vm.cc @@ -38,7 +38,6 @@ using namespace Genode; using Exit_config = Vm_connection::Exit_config; -using Call_with_state = Vm_connection::Call_with_state; struct Sel4_vcpu; @@ -810,7 +809,7 @@ struct Sel4_vcpu : Genode::Thread, Noncopyable _wake_up.up(); } - void with_state(Call_with_state &cw) + void with_state(auto const &fn) { if (!_dispatching) { if (Thread::myself() != _ep_handler) { @@ -839,7 +838,7 @@ struct Sel4_vcpu : Genode::Thread, Noncopyable _state_ready.down(); } - if (cw.call_with_state(_state) + if (fn(_state) || _extra_dispatch_up) resume(); @@ -859,11 +858,15 @@ struct Sel4_vcpu : Genode::Thread, Noncopyable Sel4_native_rpc * rpc() { return &*_rpc; } }; + /************** ** vCPU API ** **************/ -void Vm_connection::Vcpu::_with_state(Call_with_state &cw) { static_cast(_native_vcpu).vcpu.with_state(cw); } +void Vm_connection::Vcpu::_with_state(With_state::Ft const &fn) +{ + static_cast(_native_vcpu).vcpu.with_state(fn); +} Vm_connection::Vcpu::Vcpu(Vm_connection &vm, Allocator &alloc, diff --git a/repos/base/include/vm_session/connection.h b/repos/base/include/vm_session/connection.h index 34bb4e47ef..d3a3357bfd 100644 --- a/repos/base/include/vm_session/connection.h +++ b/repos/base/include/vm_session/connection.h @@ -19,6 +19,7 @@ #ifndef _INCLUDE__VM_SESSION__CONNECTION_H_ #define _INCLUDE__VM_SESSION__CONNECTION_H_ +#include #include #include #include @@ -49,11 +50,7 @@ struct Genode::Vm_connection : Connection, Rpc_client /* for example OMIT_FPU_ON_IRQ */ }; - - struct Call_with_state : Genode::Interface - { - virtual bool call_with_state(Vcpu_state &) = 0; - }; + using With_state = Callable; /** * Virtual CPU @@ -63,28 +60,13 @@ struct Genode::Vm_connection : Connection, Rpc_client */ struct Vcpu : Genode::Noncopyable { - void _with_state(Call_with_state &); + void _with_state(With_state::Ft const &); + + void with_state(auto const &fn) { _with_state(With_state::Fn { fn }); } Native_vcpu &_native_vcpu; Vcpu(Vm_connection &, Allocator &, Vcpu_handler_base &, Exit_config const &); - - template - void with_state(FN const &fn) - { - struct Untyped_fn : Call_with_state - { - FN const &_fn; - Untyped_fn(FN const &fn) : _fn(fn) {} - - bool call_with_state(Vcpu_state &state) override - { - return _fn(state); - } - } untyped_fn(fn); - - _with_state(untyped_fn); - } }; friend class Vcpu; diff --git a/repos/base/lib/symbols/ld b/repos/base/lib/symbols/ld index 5c705f5af1..2d07886846 100644 --- a/repos/base/lib/symbols/ld +++ b/repos/base/lib/symbols/ld @@ -92,7 +92,7 @@ _ZN6Genode13Shared_objectC1ERNS_3EnvERNS_9AllocatorEPKcNS0_4BindENS0_4KeepE T _ZN6Genode13Shared_objectC2ERNS_3EnvERNS_9AllocatorEPKcNS0_4BindENS0_4KeepE T _ZN6Genode13Shared_objectD1Ev T _ZN6Genode13Shared_objectD2Ev T -_ZN6Genode13Vm_connection4Vcpu11_with_stateERNS0_15Call_with_stateE T +_ZN6Genode13Vm_connection4Vcpu11_with_stateERKNS_8CallableIbJRNS_10Vcpu_stateEEE2FtE T _ZN6Genode13Vm_connection4VcpuC1ERS0_RNS_9AllocatorERNS_17Vcpu_handler_baseERKNS0_11Exit_configE T _ZN6Genode13Vm_connection4VcpuC2ERS0_RNS_9AllocatorERNS_17Vcpu_handler_baseERKNS0_11Exit_configE T _ZN6Genode13Xml_generator4NodeC1ERS0_PKcRKNS1_3_FnE T diff --git a/repos/base/src/lib/base/vm.cc b/repos/base/src/lib/base/vm.cc index fa2c76f49d..3c66b9736d 100644 --- a/repos/base/src/lib/base/vm.cc +++ b/repos/base/src/lib/base/vm.cc @@ -24,11 +24,10 @@ static Vm_session::Native_vcpu dummy; struct Genode::Vcpu_state { }; -void Vm_connection::Vcpu::_with_state(Call_with_state &) {}; +void Vm_connection::Vcpu::_with_state(With_state::Ft const &) { }; Vm_connection::Vcpu::Vcpu(Vm_connection &, Allocator &, Vcpu_handler_base &, Exit_config const &) : _native_vcpu(dummy) -{ -} +{ }