]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/bootstrap_custom/server/src/libc_support+.cc
Some minor fixes.
[l4.git] / l4 / pkg / bootstrap_custom / server / src / libc_support+.cc
1 /**
2  * \file        bootstrap/server/src/libc_support.c
3  * \brief       Support for C library
4  *
5  * \date        2004-2008
6  * \author      Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
7  *              Frank Mehnert <fm3@os.inf.tu-dresden.de> */
8
9 /*
10  * (c) 2005-2009 Author(s)
11  *     economic rights: Technische Universität Dresden (Germany)
12  *
13  * This file is part of TUD:OS and distributed under the terms of the
14  * GNU General Public License 2.
15  * Please see the COPYING-GPL-2 file for details.
16  */
17
18 #include <unistd.h>
19 #include <string.h>
20 #include <strings.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <errno.h>
25 #include <sys/types.h>
26 #include "panic.h"
27
28 #include <l4/cxx/basic_ostream>
29
30 #include "support.h"
31
32 Platform_base *Platform_base::platform;
33
34 static L4::Uart *stdio_uart;
35
36 L4::Uart *uart()
37 { return stdio_uart; }
38
39 void set_stdio_uart(L4::Uart *uart)
40 { stdio_uart = uart; }
41
42
43 inline void *operator new (size_t, void *p) { return p; }
44 // IO Stream backend
45 namespace {
46
47   class BootstrapIOBackend : public L4::IOBackend
48   {
49   protected:
50     void write(char const *str, unsigned len);
51   };
52
53   void BootstrapIOBackend::write(char const *str, unsigned len)
54   {
55     ::write(STDOUT_FILENO, str, len);
56   }
57
58 };
59
60 namespace L4 {
61   typedef char Fake_iobackend[sizeof(BootstrapIOBackend)]
62     __attribute__((aligned(__alignof__(BootstrapIOBackend))));
63   typedef char Fake_ostream[sizeof(BasicOStream)]
64     __attribute__((aligned(__alignof__(BasicOStream))));
65
66   Fake_ostream cout;
67   Fake_ostream cerr;
68
69   static Fake_iobackend _iob;
70
71   void iostream_init();
72   void iostream_init()
73   {
74     static int _initialized;
75     if (!_initialized)
76       {
77         _initialized = 1;
78         BootstrapIOBackend *iob = new (&_iob) BootstrapIOBackend();
79         new (&cerr) BasicOStream(iob);
80         new (&cout) BasicOStream(iob);
81       }
82   }
83 };
84
85 typedef void Ctor();
86
87 static void call_ctors(Ctor **start, Ctor **end)
88 {
89   for (; start < end; ++start)
90     if (*start)
91       (*start)();
92 }
93
94 void
95 ctor_init()
96 {
97   extern Ctor *__CTORS_BEGIN[];
98   extern Ctor *__CTORS_END[];
99   extern Ctor *__init_array_start[];
100   extern Ctor *__init_array_end[];
101   extern Ctor *__preinit_array_start[];
102   extern Ctor *__preinit_array_end[];
103
104   call_ctors(__preinit_array_start, __preinit_array_end);
105   call_ctors(__CTORS_BEGIN, __CTORS_END);
106   call_ctors(__init_array_start, __init_array_end);
107 }
108
109
110 void exit(int c) throw()
111 {
112   _exit(c);
113 }
114
115 void (*__exit_cleanup) (int) = 0;
116
117 extern "C" void __attribute__((noreturn))
118 __assert(const char *, const char *, int, register const char *);
119
120 extern "C" void __attribute__((noreturn))
121 __assert(const char *assertion, const char * filename,
122          int linenumber, register const char * function)
123 {
124   printf("%s:%d: %s: Assertion `%s' failed.\n",
125                                 filename,
126                                 linenumber,
127                                 ((function == NULL) ? "?function?" : function),
128                                 assertion
129                                 );
130   panic("Assertion");
131   while(1)
132     ;
133 }
134
135 ssize_t
136 write(int fd, const void *buf, size_t count)
137 {
138   if (!uart())
139     return count;
140
141   if (fd == STDOUT_FILENO || fd == STDERR_FILENO)
142     {
143       char *b = (char *)buf;
144       int i = count;
145       while (i--)
146         {
147           char c = *b++;
148           if (c == '\n')
149             uart()->write("\r", 1);
150           uart()->write(&c, 1);
151         }
152
153       return count;
154     }
155
156   errno = EBADF;
157   return -1;
158 }
159
160 #undef getchar
161 int
162 getchar(void)
163 {
164   int c;
165   if (!uart())
166     return -1;
167
168   do
169     c = uart()->get_char(0);
170   while (c == -1);
171   return c;
172 }
173
174 off_t lseek(int /*fd*/, off_t /*offset*/, int /*whence*/)
175 {
176   return 0;
177 }
178
179 void *__dso_handle = &__dso_handle;
180
181 extern "C" void reboot(void) __attribute__((noreturn));
182 void reboot(void)
183 {
184   void reboot_arch() __attribute__((noreturn));
185   reboot_arch();
186 }
187
188 extern "C" void __attribute__((noreturn))
189 _exit(int /*rc*/)
190 {
191   printf("\n\033[1mKey press reboots...\033[m\n");
192   getchar();
193   printf("Rebooting.\n\n");
194   reboot();
195 }
196
197 /** for assert */
198 void
199 abort(void) throw()
200 {
201   _exit(1);
202 }
203
204 void
205 panic(const char *fmt, ...)
206 {
207   va_list v;
208   va_start (v, fmt);
209   vprintf(fmt, v);
210   va_end(v);
211   putchar('\n');
212   exit(1);
213 }