L4Re – L4 Runtime Environment

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 <stdio.h>
#include "shared.h"
static int
func_smap_call(L4::Cap<void> const &server)
l4_addr_t addr = 0;
int err;
if ((err = L4Re::Env::env()->rm()->reserve_area(&addr, L4_PAGESIZE,
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
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;
#define L4_PAGESHIFT
Size of a page, log2-based.
Definition: consts.h:37
static Env const * env() noexcept
Returns the initial environment for the current task.
Definition: env:103
L4::Cap< T > get_cap(char const *name, unsigned l) const noexcept
Get the capability selector for the object named name.
Definition: env:204
bool free(L4::Cap< void > cap, l4_cap_idx_t task=L4_INVALID_CAP, unsigned unmap_flags=L4_FP_ALL_SPACES) noexcept
Free the capability.
l4_cap_idx_t cap() const noexcept
Return capability selector.
Definition: capability.h:52
bool is_valid() const noexcept
Test whether the capability is a valid capability index (i.e., not L4_INVALID_CAP).
Definition: capability.h:60
C++ interface for capabilities.
Definition: capability.h:219
Input/Output stream for IPC [un]marshalling.
Definition: ipc_stream:803
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:973
Environment interface.
Error codes.
unsigned long l4_addr_t
Address type.
Definition: l4int.h:45
long l4_error(l4_msgtag_t tag) L4_NOTHROW
Return error code of a system call return message tag or the tag label.
Definition: ipc.h:520
#define L4_PAGESIZE
Minimal page size (in bytes).
Definition: consts.h:307
l4_utcb_t * l4_utcb(void) L4_NOTHROW L4_PURE
Get the UTCB address.
Definition: utcb.h:340
IPC stream.
Common L4 ABI Data Types.
_Cap_alloc & cap_alloc
Capability allocator.
int Opcode
Data type for RPC opcodes.
Definition: __typeinfo.h:47
@ Search_addr
Search for a suitable address range.
Definition: rm:105
Capability allocator.