]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/sigma0/server/src/io_backend.cc
update
[l4.git] / l4 / pkg / sigma0 / server / src / io_backend.cc
1 /*
2  * (c) 2004-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
3  *               Alexander Warg <warg@os.inf.tu-dresden.de>
4  *     economic rights: Technische Universität Dresden (Germany)
5  *
6  * This file is part of TUD:OS and distributed under the terms of the
7  * GNU General Public License 2.
8  * Please see the COPYING-GPL-2 file for details.
9  */
10
11 #include <l4/cxx/ipc_stream>
12 #include <l4/cxx/basic_ostream>
13 #include <l4/sys/vcon>
14 #include <l4/util/atomic.h>
15 #include <stddef.h>
16
17 inline void *operator new (size_t, void *p) { return p; }
18
19 namespace L4 {
20
21   class LogIOBackend : public IOBackend
22   {
23   public:
24     LogIOBackend();
25   protected:
26     void write(char const *str, unsigned len);
27   };
28
29   LogIOBackend::LogIOBackend()
30   {}
31
32   void LogIOBackend::write(char const *str, unsigned len)
33   {
34     l4_msg_regs_t store;
35     l4_msg_regs_t *mr = l4_utcb_mr();
36     unsigned l = len;
37     L4::Cap<L4::Vcon> log(L4_BASE_LOG_CAP);
38
39     __builtin_memcpy(&store, mr, sizeof(store));
40
41     while (len)
42       {
43         l = len;
44         if (l > L4_VCON_WRITE_SIZE)
45           l = L4_VCON_WRITE_SIZE;
46         log->write(str, l);
47         len -= l;
48         str += l;
49       }
50
51     __builtin_memcpy(mr, &store, sizeof(store));
52   }
53
54   typedef char Fake_iobackend[sizeof(LogIOBackend)]
55     __attribute__((aligned(__alignof__(LogIOBackend))));
56   typedef char Fake_ostream[sizeof(BasicOStream)]
57     __attribute__((aligned(__alignof__(BasicOStream))));
58
59   Fake_ostream cout;
60   Fake_ostream cerr;
61
62   static Fake_iobackend _iob;
63
64   void iostream_init();
65
66   void iostream_init()
67   {
68     static l4_umword_t _initialized;
69     if (l4util_xchg(&_initialized, 1) == 0)
70       {
71         LogIOBackend *iob = new (&_iob) LogIOBackend();
72         new (&cerr) BasicOStream(iob);
73         new (&cout) BasicOStream(iob);
74       }
75   }
76 };