2012-10-02 09:01:15 +00:00
|
|
|
/*
|
2020-12-18 13:08:06 +00:00
|
|
|
* \brief Connection to a VM service
|
2012-10-02 09:01:15 +00:00
|
|
|
* \author Stefan Kalkowski
|
2020-12-18 13:08:06 +00:00
|
|
|
* \author Christian Helmuth
|
2023-06-06 13:57:38 +00:00
|
|
|
* \author Benjamin Lamowski
|
2012-10-02 09:01:15 +00:00
|
|
|
* \date 2012-10-02
|
2020-12-18 13:08:06 +00:00
|
|
|
*
|
|
|
|
* The VM connection is the API for VM and vCPU handling and implemented
|
|
|
|
* for each platform specifically.
|
2012-10-02 09:01:15 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2023-03-03 11:07:28 +00:00
|
|
|
* Copyright (C) 2012-2023 Genode Labs GmbH
|
2012-10-02 09:01:15 +00:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 12:23:52 +00:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2012-10-02 09:01:15 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _INCLUDE__VM_SESSION__CONNECTION_H_
|
|
|
|
#define _INCLUDE__VM_SESSION__CONNECTION_H_
|
|
|
|
|
|
|
|
#include <base/connection.h>
|
2020-12-18 13:08:06 +00:00
|
|
|
#include <base/rpc_client.h>
|
|
|
|
#include <vm_session/vm_session.h>
|
|
|
|
#include <cpu_session/cpu_session.h>
|
2023-06-06 13:57:38 +00:00
|
|
|
#include <util/interface.h>
|
2020-12-18 13:08:06 +00:00
|
|
|
#include <util/retry.h>
|
|
|
|
#include <util/noncopyable.h>
|
2012-10-02 09:01:15 +00:00
|
|
|
|
2020-12-18 13:08:06 +00:00
|
|
|
namespace Genode {
|
|
|
|
struct Vm_connection;
|
|
|
|
struct Vcpu_handler_base;
|
|
|
|
struct Vcpu_state;
|
|
|
|
struct Allocator;
|
|
|
|
}
|
2016-05-10 15:24:51 +00:00
|
|
|
|
|
|
|
|
2020-12-18 13:08:06 +00:00
|
|
|
struct Genode::Vm_connection : Connection<Vm_session>, Rpc_client<Vm_session>
|
2012-10-23 15:12:09 +00:00
|
|
|
{
|
2020-12-18 13:08:06 +00:00
|
|
|
/*
|
|
|
|
* VM-Exit state-transfer configuration
|
|
|
|
*
|
|
|
|
* Per default all Vcpu_state is transfered on each exit reason. The exit
|
|
|
|
* config enables omission of some state transfers on specific exit
|
|
|
|
* reasons.
|
|
|
|
*/
|
|
|
|
struct Exit_config
|
|
|
|
{
|
|
|
|
/* for example OMIT_FPU_ON_IRQ */
|
|
|
|
};
|
|
|
|
|
2023-06-06 13:57:38 +00:00
|
|
|
|
|
|
|
struct Call_with_state : Genode::Interface
|
|
|
|
{
|
|
|
|
virtual bool call_with_state(Vcpu_state &) = 0;
|
|
|
|
};
|
|
|
|
|
2020-12-18 13:08:06 +00:00
|
|
|
/**
|
|
|
|
* Virtual CPU
|
|
|
|
*
|
|
|
|
* A vCPU can be created only from a Vm_connection and is, thus, bounded to
|
|
|
|
* a VM until destroyed.
|
|
|
|
*/
|
|
|
|
struct Vcpu : Genode::Noncopyable
|
|
|
|
{
|
2023-06-06 13:57:38 +00:00
|
|
|
void _with_state(Call_with_state &);
|
|
|
|
|
2020-12-18 13:08:06 +00:00
|
|
|
Native_vcpu &_native_vcpu;
|
|
|
|
|
|
|
|
Vcpu(Vm_connection &, Allocator &, Vcpu_handler_base &, Exit_config const &);
|
|
|
|
|
2023-06-06 13:57:38 +00:00
|
|
|
template <typename FN>
|
|
|
|
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);
|
|
|
|
}
|
2020-12-18 13:08:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
friend class Vcpu;
|
|
|
|
|
2012-10-23 15:12:09 +00:00
|
|
|
/**
|
2016-05-10 15:24:51 +00:00
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* \param priority designated priority of the VM
|
|
|
|
* \param affinity which physical CPU the VM should run on top of
|
2012-10-23 15:12:09 +00:00
|
|
|
*/
|
2023-03-03 11:07:28 +00:00
|
|
|
Vm_connection(Env &env, Label const &label = Label(),
|
2016-05-10 15:24:51 +00:00
|
|
|
long priority = Cpu_session::DEFAULT_PRIORITY,
|
|
|
|
unsigned long affinity = 0)
|
|
|
|
:
|
2023-03-03 11:07:28 +00:00
|
|
|
Connection<Vm_session>(env, label, Ram_quota { 16*1024 }, Affinity(),
|
|
|
|
Args("priority=", Hex(priority), ", "
|
|
|
|
"affinity=", Hex(affinity))),
|
2020-12-18 13:08:06 +00:00
|
|
|
Rpc_client<Vm_session>(cap())
|
2016-05-10 15:24:51 +00:00
|
|
|
{ }
|
|
|
|
|
2024-05-22 14:35:38 +00:00
|
|
|
auto with_upgrade(auto const &fn) -> decltype(fn())
|
2018-09-26 13:53:18 +00:00
|
|
|
{
|
|
|
|
return Genode::retry<Genode::Out_of_ram>(
|
2024-06-28 14:35:57 +00:00
|
|
|
[&] {
|
2018-09-26 13:53:18 +00:00
|
|
|
return Genode::retry<Genode::Out_of_caps>(
|
2024-06-28 14:35:57 +00:00
|
|
|
[&] { return fn(); },
|
|
|
|
[&] { this->upgrade_caps(2); });
|
2018-09-26 13:53:18 +00:00
|
|
|
},
|
2024-06-28 14:35:57 +00:00
|
|
|
[&] { this->upgrade_ram(4096); }
|
2018-09-26 13:53:18 +00:00
|
|
|
);
|
|
|
|
}
|
2020-12-18 13:08:06 +00:00
|
|
|
|
2023-03-03 11:07:28 +00:00
|
|
|
|
2020-12-18 13:08:06 +00:00
|
|
|
/**************************
|
|
|
|
** Vm_session interface **
|
|
|
|
**************************/
|
|
|
|
|
|
|
|
void attach(Dataspace_capability ds, addr_t vm_addr, Attach_attr attr) override
|
|
|
|
{
|
2024-06-28 14:35:57 +00:00
|
|
|
with_upgrade([&] {
|
2020-12-18 13:08:06 +00:00
|
|
|
call<Rpc_attach>(ds, vm_addr, attr); });
|
|
|
|
}
|
|
|
|
|
|
|
|
void detach(addr_t vm_addr, size_t size) override {
|
|
|
|
call<Rpc_detach>(vm_addr, size); }
|
|
|
|
|
|
|
|
void attach_pic(addr_t vm_addr) override {
|
|
|
|
call<Rpc_attach_pic>(vm_addr); }
|
2016-05-10 15:24:51 +00:00
|
|
|
};
|
2012-10-02 09:01:15 +00:00
|
|
|
|
|
|
|
#endif /* _INCLUDE__VM_SESSION__CONNECTION_H_ */
|