Loading...
Searching...
No Matches
examples/libs/l4re/streammap/client.cc

Client/Server example showing how to map a page to another task – Client implementation.

Client/Server example showing how to map a page to another task – Client implementation. Note that there's also a shared memory library that supplies this functionality in more convenient way.

/*
* (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
* Alexander Warg <warg@os.inf.tu-dresden.de>
* economic rights: Technische Universität Dresden (Germany)
*
* This file is part of TUD:OS and distributed under the terms of the
* GNU General Public License 2.
* Please see the COPYING-GPL-2 file for details.
*/
#include <l4/sys/err.h>
#include <l4/sys/types.h>
#include <l4/re/env>
#include <l4/re/util/cap_alloc>
#include <l4/cxx/ipc_stream>
#include <stdio.h>
#include "shared.h"
static int
func_smap_call(L4::Cap<void> const &server)
{
L4::Ipc::Iostream s(l4_utcb());
l4_addr_t addr = 0;
int err;
if ((err = L4Re::Env::env()->rm()->reserve_area(&addr, L4_PAGESIZE,
L4Re::Rm::F::Search_addr)))
{
printf("The reservation of one page within our virtual memory failed with %d\n", err);
return 1;
}
s << L4::Opcode(Mapper::Do_map)
<< (l4_addr_t)addr;
s << L4::Ipc::Rcv_fpage::mem((l4_addr_t)addr, L4_PAGESHIFT, 0);
int r = l4_error(s.call(server.cap(), Mapper::Protocol));
if (r)
return r; // failure
printf("String sent by server: %s\n", (char *)addr);
return 0; // ok
}
int
main()
{
L4::Cap<void> server = L4Re::Env::env()->get_cap<void>("smap");
if (!server.is_valid())
{
printf("Could not get capability slot!\n");
return 1;
}
printf("Asking for page from server\n");
if (func_smap_call(server))
{
printf("Error talking to server\n");
return 1;
}
printf("It worked!\n");
L4Re::Util::cap_alloc.free(server, L4Re::This_task);
return 0;
}
L4Re::Env::env
static Env const * env() noexcept
Returns the initial environment for the current task.
Definition env:96
L4Re::Env::get_cap
L4::Cap< T > get_cap(char const *name, unsigned l) const noexcept
Get the capability selector for the object named name.
Definition env:197
L4::Cap
C++ interface for capabilities.
Definition capability.h:224
L4::Ipc::Iostream
Input/Output stream for IPC [un]marshalling.
Definition ipc_stream:790
L4::Ipc::Iostream::call
l4_msgtag_t call(l4_cap_idx_t dst, l4_timeout_t timeout, long proto=0)
Do an IPC call using the message in the output stream and receive the reply in the input stream.
Definition ipc_stream:966
L4::Ipc::Rcv_fpage::mem
static Rcv_fpage mem(l4_addr_t base, int order, l4_addr_t snd_base=0, L4::Cap< void > rcv_task=L4::Cap< void >::Invalid) noexcept
Construct a receive item for the memory space.
Definition ipc_types:594
env
Environment interface.
err.h
Error codes.
l4_addr_t
unsigned long l4_addr_t
Address type.
Definition l4int.h:34
l4_error
long l4_error(l4_msgtag_t tag) L4_NOTHROW
Get IPC error code if any or message tag label otherwise for an IPC call.
Definition ipc.h:646
L4_PAGESIZE
#define L4_PAGESIZE
Minimal page size (in bytes).
Definition consts.h:391
L4_PAGESHIFT
#define L4_PAGESHIFT
Size of a page, log2-based.
Definition consts.h:26
l4_utcb
l4_utcb_t * l4_utcb(void) L4_NOTHROW L4_PURE
Get the UTCB address.
Definition utcb.h:346
L4Re::Util::cap_alloc
_Cap_alloc & cap_alloc
Capability allocator.
ipc_stream
IPC stream.
types.h
Common L4 ABI Data Types.
L4::Opcode
int Opcode
Data type for RPC opcodes.
Definition __typeinfo.h:36
L4Re::Rm::F::Search_addr
@ Search_addr
Search for a suitable address range.
Definition rm:114
cap_alloc
Capability allocator.