]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/examples/sys/aliens/main.c
update
[l4.git] / l4 / pkg / examples / sys / aliens / main.c
1 /*
2  * (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
3  *               Alexander Warg <warg@os.inf.tu-dresden.de>,
4  *               Björn Döbel <doebel@os.inf.tu-dresden.de>
5  *     economic rights: Technische Universität Dresden (Germany)
6  *
7  * This file is part of TUD:OS and distributed under the terms of the
8  * GNU General Public License 2.
9  * Please see the COPYING-GPL-2 file for details.
10  */
11 /*
12  * Example to show syscall tracing.
13  */
14 //#define MEASURE
15
16 #include <l4/sys/ipc.h>
17 #include <l4/sys/thread.h>
18 #include <l4/sys/factory.h>
19 #include <l4/sys/utcb.h>
20 #include <l4/sys/kdebug.h>
21 #include <l4/util/util.h>
22 #include <l4/util/rdtsc.h>
23 #include <l4/re/env.h>
24 #include <l4/re/c/util/cap_alloc.h>
25 #include <l4/sys/debugger.h>
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30
31
32 static char alien_thread_stack[8 << 10];
33 static l4_cap_idx_t alien;
34
35 static void alien_thread(void)
36 {
37   volatile l4_msgtag_t x;
38   while (1) {
39     x = l4_ipc_call(0x1234 << L4_CAP_SHIFT, l4_utcb(), l4_msgtag(0, 0, 0, 0), L4_IPC_NEVER);
40 #ifdef MEASURE
41     l4_sleep(0);
42 #else
43     l4_sleep(1000);
44     outstring("An int3 -- you should see this\n");
45     outnstring("345", 3);
46 #endif
47   }
48
49 }
50
51 int main(void)
52 {
53   l4_msgtag_t tag;
54 #ifdef MEASURE
55   l4_cpu_time_t s, e;
56 #endif
57   l4_utcb_t *u = l4_utcb();
58   l4_exc_regs_t exc;
59   l4_umword_t mr0, mr1;
60
61   printf("Alien feature testing\n");
62
63   l4_debugger_set_object_name(l4re_env()->main_thread, "alientest");
64
65   /* Start alien thread */
66   if (l4_is_invalid_cap(alien = l4re_util_cap_alloc()))
67     return 1;
68
69   l4_touch_rw(alien_thread_stack, sizeof(alien_thread_stack));
70
71   tag = l4_factory_create_thread(l4re_env()->factory, alien);
72   if (l4_error(tag))
73     return 1;
74
75   l4_debugger_set_object_name(alien, "alienth");
76
77   l4_thread_control_start();
78   l4_thread_control_pager(l4re_env()->main_thread);
79   l4_thread_control_exc_handler(l4re_env()->main_thread);
80   l4_thread_control_bind((l4_utcb_t *)l4re_env()->first_free_utcb, L4RE_THIS_TASK_CAP);
81   l4_thread_control_alien(1);
82   tag = l4_thread_control_commit(alien);
83   if (l4_error(tag))
84     return 2;
85
86   tag = l4_thread_ex_regs(alien,
87                           (l4_umword_t)alien_thread,
88                           (l4_umword_t)alien_thread_stack + sizeof(alien_thread_stack),
89                           0);
90   if (l4_error(tag))
91     return 3;
92
93   l4_sched_param_t sp = l4_sched_param(1, 0);
94   tag = l4_scheduler_run_thread(l4re_env()->scheduler, alien, &sp);
95   if (l4_error(tag))
96     return 4;
97
98 #ifdef MEASURE
99   l4_calibrate_tsc(l4re_kip());
100 #endif
101
102   /* Pager/Exception loop */
103   if (l4_msgtag_has_error(tag = l4_ipc_receive(alien, u, L4_IPC_NEVER)))
104     {
105       printf("l4_ipc_receive failed");
106       return 1;
107     }
108
109   memcpy(&exc, l4_utcb_exc(), sizeof(exc));
110   mr0 = l4_utcb_mr()->mr[0];
111   mr1 = l4_utcb_mr()->mr[1];
112
113   for (;;)
114     {
115 #ifdef MEASURE
116       s = l4_rdtsc();
117 #endif
118
119       if (l4_msgtag_is_exception(tag))
120         {
121 #ifndef MEASURE
122           printf("PC=%08lx SP=%08lx Err=%08lx Trap=%lx, %s syscall, SC-Nr: %lx\n",
123                  l4_utcb_exc_pc(&exc), exc.sp, exc.err,
124                  exc.trapno, (exc.err & 4) ? " after" : "before",
125                  exc.err >> 3);
126 #endif
127           tag = l4_msgtag((exc.err & 4) ? 0 : L4_PROTO_ALLOW_SYSCALL,
128                           L4_UTCB_EXCEPTION_REGS_SIZE, 0, 0);
129         }
130       else
131         printf("Umm, non-handled request (like PF): %lx %lx\n", mr0, mr1);
132
133       memcpy(l4_utcb_exc(), &exc, sizeof(exc));
134
135       /* Reply and wait */
136       if (l4_msgtag_has_error(tag = l4_ipc_call(alien, u, tag, L4_IPC_NEVER)))
137         {
138           printf("l4_ipc_call failed\n");
139           return 1;
140         }
141       memcpy(&exc, l4_utcb_exc(), sizeof(exc));
142       mr0 = l4_utcb_mr()->mr[0];
143       mr1 = l4_utcb_mr()->mr[1];
144 #ifdef MEASURE
145       e = l4_rdtsc();
146       printf("time %lld\n", l4_tsc_to_ns(e - s));
147 #endif
148     }
149
150   return 0;
151 }