genode/repos/base/src/test/rm_fault/main.cc

229 lines
5.9 KiB
C++
Raw Normal View History

2011-12-22 16:19:25 +01:00
/*
* \brief Test program for raising and handling region-manager faults
* \author Norman Feske
* \date 2008-09-24
*
* This program starts itself as child. When started, it first determines
* wheather it is parent or child by requesting its own file from the ROM
* service. Because the program blocks all session-creation calls for the
* ROM service, each program instance can determine its parent or child
* role by the checking the result of the session creation.
*/
/*
2013-01-10 21:44:47 +01:00
* Copyright (C) 2008-2013 Genode Labs GmbH
2011-12-22 16:19:25 +01:00
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU General Public License version 2.
*/
#include <base/log.h>
2011-12-22 16:19:25 +01:00
#include <base/env.h>
#include <base/sleep.h>
#include <base/child.h>
#include <pd_session/connection.h>
#include <rm_session/connection.h>
2011-12-22 16:19:25 +01:00
#include <ram_session/connection.h>
#include <rom_session/connection.h>
#include <cpu_session/connection.h>
#include <cap_session/connection.h>
#include <rm_session/client.h>
using namespace Genode;
2011-12-22 16:19:25 +01:00
/***********
** Child **
***********/
enum { MANAGED_ADDR = 0x10000000 };
void read_at(addr_t addr)
{
log("perform read operation at ", Hex(addr));
2011-12-22 16:19:25 +01:00
int value = *(int *)addr;
log("read value ", Hex(value));
2011-12-22 16:19:25 +01:00
}
void modify(addr_t addr)
{
log("modify memory at ", Hex(addr), " to ", Hex(++(*(int *)addr)));
2011-12-22 16:19:25 +01:00
}
void main_child()
{
log("child role started");
2011-12-22 16:19:25 +01:00
/* perform illegal access */
read_at(MANAGED_ADDR);
while (true)
modify(MANAGED_ADDR);
log("--- child role of region-manager fault test finished ---");
2011-12-22 16:19:25 +01:00
}
/************
** Parent **
************/
class Test_child : public Child_policy
{
private:
enum { STACK_SIZE = 8*1024 };
/*
* Entry point used for serving the parent interface
*/
Rpc_entrypoint _entrypoint;
Region_map_client _address_space;
Pd_session_client _pd;
Ram_session_client _ram;
Cpu_session_client _cpu;
Child::Initial_thread _initial_thread;
2011-12-22 16:19:25 +01:00
Child _child;
Parent_service _log_service;
public:
/**
* Constructor
*/
Test_child(Genode::Dataspace_capability elf_ds,
Genode::Pd_connection &pd,
2011-12-22 16:19:25 +01:00
Genode::Ram_session_capability ram,
Genode::Cpu_session_capability cpu,
Genode::Cap_session *cap)
:
_entrypoint(cap, STACK_SIZE, "child", false),
_address_space(pd.address_space()), _pd(pd), _ram(ram), _cpu(cpu),
_initial_thread(_cpu, _pd, "child"),
_child(elf_ds, Dataspace_capability(), _pd, _pd, _ram, _ram,
_cpu, _initial_thread, *env()->rm_session(), _address_space,
_entrypoint, *this),
_log_service("LOG")
2011-12-22 16:19:25 +01:00
{
/* start execution of the new child */
_entrypoint.activate();
}
/****************************
** Child-policy interface **
****************************/
const char *name() const { return "rmchild"; }
Service *resolve_session_request(const char *service, const char *)
{
/* forward white-listed session requests to our parent */
return !strcmp(service, "LOG") ? &_log_service : 0;
2011-12-22 16:19:25 +01:00
}
void filter_session_args(const char *service,
char *args, size_t args_len)
{
/* define session label for sessions forwarded to our parent */
Arg_string::set_arg_string(args, args_len, "label", "child");
2011-12-22 16:19:25 +01:00
}
};
void main_parent(Dataspace_capability elf_ds)
{
log("parent role started");
2011-12-22 16:19:25 +01:00
/* create environment for new child */
static Pd_connection pd;
2011-12-22 16:19:25 +01:00
static Ram_connection ram;
static Cpu_connection cpu;
static Cap_connection cap;
/* transfer some of our own ram quota to the new child */
enum { CHILD_QUOTA = 1*1024*1024 };
ram.ref_account(env()->ram_session_cap());
env()->ram_session()->transfer_quota(ram.cap(), CHILD_QUOTA);
static Signal_receiver fault_handler;
/* register fault handler at the child's address space */
2011-12-22 16:19:25 +01:00
static Signal_context signal_context;
Region_map_client address_space(pd.address_space());
address_space.fault_handler(fault_handler.manage(&signal_context));
2011-12-22 16:19:25 +01:00
/* create child */
static Test_child child(elf_ds, pd, ram.cap(), cpu.cap(), &cap);
2011-12-22 16:19:25 +01:00
/* allocate dataspace used for creating shared memory between parent and child */
Dataspace_capability ds = env()->ram_session()->alloc(4096);
volatile int *local_addr = env()->rm_session()->attach(ds);
for (int i = 0; i < 4; i++) {
log("wait for region-manager fault");
2011-12-22 16:19:25 +01:00
fault_handler.wait_for_signal();
log("received region-manager fault signal, request fault state");
2011-12-22 16:19:25 +01:00
Region_map::State state = address_space.state();
2011-12-22 16:19:25 +01:00
char const *state_name =
state.type == Region_map::State::READ_FAULT ? "READ_FAULT" :
state.type == Region_map::State::WRITE_FAULT ? "WRITE_FAULT" :
state.type == Region_map::State::EXEC_FAULT ? "EXEC_FAULT" : "READY";
log("rm session state is ", state_name, ", pf_addr=", Hex(state.addr));
2011-12-22 16:19:25 +01:00
/* ignore spuriuous fault signal */
if (state.type == Region_map::State::READY) {
log("ignoring spurious fault signal");
2011-12-22 16:19:25 +01:00
continue;
}
addr_t child_virt_addr = state.addr & ~(4096 - 1);
/* allocate dataspace to resolve the fault */
log("attach dataspace to the child at ", Hex(child_virt_addr));
2011-12-22 16:19:25 +01:00
*local_addr = 0x1234;
address_space.attach_at(ds, child_virt_addr);
2011-12-22 16:19:25 +01:00
/* wait until our child modifies the dataspace content */
while (*local_addr == 0x1234);
log("child modified dataspace content, new value is ", Hex(*local_addr));
2011-12-22 16:19:25 +01:00
log("revoke dataspace from child");
address_space.detach((void *)child_virt_addr);
2011-12-22 16:19:25 +01:00
}
fault_handler.dissolve(&signal_context);
log("--- parent role of region-manager fault test finished ---");
2011-12-22 16:19:25 +01:00
}
/*************************
** Common main program **
*************************/
int main(int argc, char **argv)
{
log("--- region-manager fault test ---");
2011-12-22 16:19:25 +01:00
/* obtain own elf file from rom service */
try {
static Rom_connection rom("test-rm_fault");
2011-12-22 16:19:25 +01:00
main_parent(rom.dataspace());
} catch (Genode::Rom_connection::Rom_connection_failed) {
main_child();
}
return 0;
}