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/basic_ostream>
12 #include <l4/sys/vcon>
13 #include <l4/util/atomic.h>
14 #include <stddef.h>
15 
operator new(size_t,void * p)16 inline void *operator new (size_t, void *p) { return p; }
17 
18 namespace L4 {
19 
20   class LogIOBackend : public IOBackend
21   {
22   public:
23     LogIOBackend();
24   protected:
25     void write(char const *str, unsigned len);
26   };
27 
LogIOBackend()28   LogIOBackend::LogIOBackend()
29   {}
30 
write(char const * str,unsigned len)31   void LogIOBackend::write(char const *str, unsigned len)
32   {
33     l4_msg_regs_t store;
34     l4_msg_regs_t *mr = l4_utcb_mr();
35     unsigned l = len;
36     L4::Cap<L4::Vcon> log(L4_BASE_LOG_CAP);
37 
38     __builtin_memcpy(&store, mr, sizeof(store));
39 
40     while (len)
41       {
42         l = len;
43         if (l > L4_VCON_WRITE_SIZE)
44           l = L4_VCON_WRITE_SIZE;
45         log->write(str, l);
46         len -= l;
47         str += l;
48       }
49 
50     __builtin_memcpy(mr, &store, sizeof(store));
51   }
52 
53   typedef char Fake_iobackend[sizeof(LogIOBackend)]
54     __attribute__((aligned(__alignof__(LogIOBackend))));
55   typedef char Fake_ostream[sizeof(BasicOStream)]
56     __attribute__((aligned(__alignof__(BasicOStream))));
57 
58   Fake_ostream cout;
59   Fake_ostream cerr;
60 
61   static Fake_iobackend _iob;
62 
63   void iostream_init();
64 
iostream_init()65   void iostream_init()
66   {
67     static l4_umword_t _initialized;
68     if (l4util_xchg(&_initialized, 1) == 0)
69       {
70         LogIOBackend *iob = new (&_iob) LogIOBackend();
71         new (&cerr) BasicOStream(iob);
72         new (&cout) BasicOStream(iob);
73       }
74   }
75 };
76