L4Re - L4 Runtime Environment
examples/sys/utcb-ipc/main.c

This example shows how to send IPC using the UTCB to store payload.

/*
* (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
* Alexander Warg <warg@os.inf.tu-dresden.de>,
* Björn Döbel <doebel@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/ipc.h>
#include <l4/sys/thread.h>
#include <l4/sys/factory.h>
#include <l4/sys/utcb.h>
#include <l4/re/env.h>
#include <l4/util/thread.h>
#include <stdio.h>
#include <string.h>
static unsigned char stack2[8 << 10];
static l4_cap_idx_t thread1_cap, thread2_cap;
static void thread1(void)
{
int i, j;
printf("Thread1 up (%p)\n", l4_utcb());
for (i = 0; i < 10; i++)
{
for (j = 0; j < L4_UTCB_GENERIC_DATA_SIZE; j++)
mr->mr[j] = 'A' + (i + j) % ('~' - 'A' + 1);
tag = l4_msgtag(0, L4_UTCB_GENERIC_DATA_SIZE, 0, 0);
printf("IPC-send error\n");
}
}
L4UTIL_THREAD_STATIC_FUNC(thread2)
{
unsigned i;
printf("Thread2 up (%p)\n", l4_utcb());
while (1)
{
printf("IPC receive error\n");
memcpy(&mr, l4_utcb_mr(), sizeof(mr));
printf("Thread2 receive (%d): ", l4_msgtag_words(tag));
for (i = 0; i < l4_msgtag_words(tag); i++)
printf("%c", (char)mr.mr[i]);
printf("\n");
}
__builtin_trap();
}
int main(void)
{
thread1_cap = l4re_env()->main_thread;
thread2_cap = l4re_util_cap_alloc();
if (l4_is_invalid_cap(thread2_cap))
return 1;
tag = l4_factory_create_thread(l4re_env()->factory, thread2_cap);
if (l4_error(tag))
return 1;
l4_thread_control_bind((l4_utcb_t *)l4re_env()->first_free_utcb,
L4RE_THIS_TASK_CAP);
tag = l4_thread_control_commit(thread2_cap);
if (l4_error(tag))
return 2;
tag = l4_thread_ex_regs(thread2_cap,
(l4_umword_t)thread2,
(l4_umword_t)(stack2 + sizeof(stack2)), 0);
if (l4_error(tag))
return 3;
tag = l4_scheduler_run_thread(l4re_env()->scheduler, thread2_cap, &sp);
if (l4_error(tag))
return 4;
thread1();
return 0;
}