L4Re – L4 Runtime Environment
examples/sys/singlestep/main.c

This example shows how a thread can be single stepped on the x86 architecture.

/*
* (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.
*/
/*
* Single stepping example for the x86-32 architecture.
*/
#include <l4/sys/ipc.h>
#include <l4/sys/factory.h>
#include <l4/sys/thread.h>
#include <l4/sys/utcb.h>
#include <l4/sys/kdebug.h>
#include <l4/util/util.h>
#include <l4/re/env.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
static char thread_stack[8 << 10];
static void thread_func(void)
{
while (1)
{
unsigned long d = 0;
/* Enable single stepping */
asm volatile("pushf; pop %0; or $256,%0; push %0; popf\n"
: "=r" (d) : "r" (d));
/* Some instructions */
asm volatile("nop");
asm volatile("nop");
asm volatile("nop");
asm volatile("mov $0x12345000, %%edx" : : : "edx"); // a non-existent cap
asm volatile("int $0x30\n");
asm volatile("nop");
asm volatile("nop");
asm volatile("nop");
/* Disabled single stepping */
asm volatile("pushf; pop %0; and $~256,%0; push %0; popf\n"
: "=r" (d) : "r" (d));
/* You won't see those */
asm volatile("nop");
asm volatile("nop");
asm volatile("nop");
}
}
int main(void)
{
int ipc_stat = 0;
l4_umword_t mr0, mr1;
printf("Singlestep testing\n");
return 1;
l4_touch_rw(thread_stack, sizeof(thread_stack));
l4_touch_ro(thread_func, 1);
tag = l4_factory_create_thread(l4re_env()->factory, th);
if (l4_error(tag))
return 1;
l4_thread_control_bind((l4_utcb_t *)l4re_env()->first_free_utcb,
L4RE_THIS_TASK_CAP);
if (l4_error(tag))
return 2;
tag = l4_thread_ex_regs(th, (l4_umword_t)thread_func,
(l4_umword_t)thread_stack + sizeof(thread_stack),
0);
if (l4_error(tag))
return 3;
tag = l4_scheduler_run_thread(l4re_env()->scheduler, th, &sp);
if (l4_error(tag))
return 4;
/* Pager/Exception loop */
if (l4_msgtag_has_error(tag = l4_ipc_receive(th, u, L4_IPC_NEVER)))
{
printf("l4_ipc_receive failed");
return 5;
}
memcpy(&exc, l4_utcb_exc(), sizeof(exc));
mr0 = l4_utcb_mr()->mr[0];
mr1 = l4_utcb_mr()->mr[1];
for (;;)
{
{
printf("PC = %08lx Trap = %08lx Err = %08lx, SP = %08lx SC-Nr: %lx\n",
l4_utcb_exc_pc(&exc), exc.trapno, exc.err,
exc.sp, exc.err >> 3);
if (exc.err >> 3)
{
if (!(exc.err & 4))
{
if (ipc_stat)
enter_kdebug("Should not be 1");
}
else
{
if (!ipc_stat)
enter_kdebug("Should not be 0");
}
ipc_stat = !ipc_stat;
}
l4_sleep(100);
}
else
printf("Umm, non-handled request: %ld, %08lx %08lx\n",
l4_msgtag_label(tag), mr0, mr1);
memcpy(l4_utcb_exc(), &exc, sizeof(exc));
/* Reply and wait */
if (l4_msgtag_has_error(tag = l4_ipc_call(th, u, tag, L4_IPC_NEVER)))
{
printf("l4_ipc_call failed\n");
return 5;
}
memcpy(&exc, l4_utcb_exc(), sizeof(exc));
mr0 = l4_utcb_mr()->mr[0];
mr1 = l4_utcb_mr()->mr[1];
}
return 0;
}
void l4_sleep(int ms) L4_NOTHROW
Suspend thread for a period of ms milliseconds.
Capability allocator C interface.
Environment interface.
Common factory related definitions.
l4_cap_idx_t l4re_util_cap_alloc(void) L4_NOTHROW
Get free capability index at capability allocator.
l4re_env_t * l4re_env(void) L4_NOTHROW
Get L4Re initial environment.
Definition: env.h:185
unsigned long l4_umword_t
Unsigned machine word.
Definition: l4int.h:51
unsigned long l4_cap_idx_t
L4 Capability selector Type.
Definition: types.h:342
unsigned l4_is_invalid_cap(l4_cap_idx_t c) L4_NOTHROW
Test if a capability selector is the invalid capability.
Definition: types.h:392
l4_msgtag_t l4_factory_create_thread(l4_cap_idx_t factory, l4_cap_idx_t target_cap) L4_NOTHROW
Create a new thread.
Definition: factory.h:366
l4_msgtag_t l4_ipc_receive(l4_cap_idx_t object, l4_utcb_t *utcb, l4_timeout_t timeout) L4_NOTHROW
Wait for a message from a specific source.
Definition: ipc.h:505
l4_msgtag_t l4_ipc_call(l4_cap_idx_t object, l4_utcb_t *utcb, l4_msgtag_t tag, l4_timeout_t timeout) L4_NOTHROW
Object call (usual invocation).
Definition: ipc.h:463
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:535
unsigned l4_msgtag_is_exception(l4_msgtag_t t) L4_NOTHROW
Test for exception protocol.
Definition: types.h:451
unsigned l4_msgtag_has_error(l4_msgtag_t t) L4_NOTHROW
Test for error indicator flag.
Definition: types.h:437
l4_msgtag_t l4_msgtag(long label, unsigned words, unsigned items, unsigned flags) L4_NOTHROW
Create a message tag from the specified values.
Definition: types.h:408
long l4_msgtag_label(l4_msgtag_t t) L4_NOTHROW
Get the protocol of tag.
Definition: types.h:420
@ L4_PROTO_NONE
Default protocol tag to reply to kernel.
Definition: types.h:51
@ L4_PROTO_ALLOW_SYSCALL
Allow an alien the system call.
Definition: types.h:52
l4_sched_param_t l4_sched_param(unsigned prio, l4_cpu_time_t quantum=0) L4_NOTHROW
Construct scheduler parameter.
Definition: scheduler.h:221
l4_msgtag_t l4_scheduler_run_thread(l4_cap_idx_t scheduler, l4_cap_idx_t thread, l4_sched_param_t const *sp) L4_NOTHROW
Run a thread on a Scheduler.
Definition: scheduler.h:316
l4_msgtag_t l4_thread_ex_regs(l4_cap_idx_t thread, l4_addr_t ip, l4_addr_t sp, l4_umword_t flags) L4_NOTHROW
Exchange basic thread registers.
Definition: thread.h:838
void l4_thread_control_bind(l4_utcb_t *thread_utcb, l4_cap_idx_t task) L4_NOTHROW
Bind the thread to a task.
Definition: thread.h:871
l4_msgtag_t l4_thread_control_commit(l4_cap_idx_t thread) L4_NOTHROW
Commit the thread control parameters.
Definition: thread.h:889
void l4_thread_control_alien(int on) L4_NOTHROW
Enable alien mode.
Definition: thread.h:877
void l4_thread_control_exc_handler(l4_cap_idx_t exc_handler) L4_NOTHROW
Set the exception handler.
Definition: thread.h:864
void l4_thread_control_start(void) L4_NOTHROW
Start a thread control API sequence.
Definition: thread.h:852
void l4_thread_control_pager(l4_cap_idx_t pager) L4_NOTHROW
Set the pager.
Definition: thread.h:858
@ L4_UTCB_EXCEPTION_REGS_SIZE
Number if message registers used for exception IPC.
Definition: utcb.h:44
l4_msg_regs_t * l4_utcb_mr(void) L4_NOTHROW L4_PURE
Get the message-register block of a UTCB.
Definition: utcb.h:352
struct l4_utcb_t l4_utcb_t
Opaque type for the UTCB.
Definition: utcb.h:67
l4_utcb_t * l4_utcb(void) L4_NOTHROW L4_PURE
Get the UTCB address.
Definition: utcb.h:340
l4_exc_regs_t * l4_utcb_exc(void) L4_NOTHROW L4_PURE
Get the message-register block of a UTCB (for an exception IPC).
Definition: utcb.h:361
l4_umword_t l4_utcb_exc_pc(l4_exc_regs_t const *u) L4_NOTHROW L4_PURE
Access function to get the program counter of the exception state.
Definition: utcb.h:90
void l4_touch_rw(const void *addr, unsigned size) L4_NOTHROW
Touch data areas to force mapping (read-write)
Definition: util.h:107
void l4_touch_ro(const void *addr, unsigned size) L4_NOTHROW
Touch data area to force mapping (read-only)
Definition: util.h:94
String.
UTCB structure for exceptions.
Definition: utcb.h:39
l4_umword_t trapno
trap number
Definition: utcb.h:76
l4_umword_t sp
stack pointer
Definition: utcb.h:44
l4_umword_t err
error code
Definition: utcb.h:41
Message tag data structure.
Definition: types.h:160
Scheduler parameter set.
Definition: scheduler.h:121
l4_umword_t mr[L4_UTCB_GENERIC_DATA_SIZE]
Message registers.
Definition: utcb.h:80