]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/rtc/server/src/main.cc
22785749c1975809a42d458ec1841c10e5978eda
[l4.git] / l4 / pkg / rtc / server / src / main.cc
1 /**
2  * \file    rtc/server/src/main.cc
3  * \brief   Initialization and main server loop
4  *
5  * \date    09/23/2003
6  * \author  Frank Mehnert <fm3@os.inf.tu-dresden.de>
7  * \author  Adam Lackorzynski <adam@os.inf.tu-dresden.de> */
8
9 /*
10  * (c) 2003-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 <l4/rtc/rtc.h>
19 #if defined ARCH_x86 || defined ARCH_amd64
20 #include <l4/util/rdtsc.h>
21 #endif
22 #include <l4/re/env>
23 #include <l4/re/namespace>
24 #include <l4/cxx/exceptions>
25 #include <l4/re/error_helper>
26 #include <l4/re/util/cap_alloc>
27 #include <l4/sys/factory>
28 #include <l4/cxx/ipc_server>
29 #include <l4/cxx/iostream>
30 #include <l4/cxx/l4iostream>
31
32 #include <l4/sys/ipc_gate>
33
34 #include <cstdlib>
35 #include <cstdio>
36
37 #include "rtc.h"
38
39 l4_uint32_t system_time_offs_rel_1970;
40
41 int
42 l4rtc_if_get_offset_component(l4_uint32_t *offset)
43 {
44 #if defined ARCH_x86 || defined ARCH_amd64
45   *offset = system_time_offs_rel_1970;
46   return 0;
47 #endif
48   *offset = 0;
49   return 1;
50 }
51
52 int
53 l4rtc_if_get_linux_tsc_scaler_component(l4_uint32_t *scaler)
54 {
55 #if defined ARCH_x86 || defined ARCH_amd64
56   *scaler = l4_scaler_tsc_linux;
57   return 0;
58 #endif
59   *scaler = 0;
60   return 1;
61 }
62
63 class Rtc_dispatcher
64 {
65 public:
66   int dispatch(l4_umword_t obj, L4::Ipc_iostream &ios);
67 };
68
69 int
70 Rtc_dispatcher::dispatch(l4_umword_t, L4::Ipc_iostream &ios)
71 {
72   l4_msgtag_t t;
73   ios >> t;
74
75   if (t.label() != 0)
76     return -L4_EBADPROTO;
77
78   l4_umword_t opcode;
79   ios >> opcode;
80
81   switch (opcode)
82     {
83     case L4RTC_OPCODE_get_offset:
84       l4_uint32_t offset;
85       l4rtc_if_get_offset_component(&offset);
86       ios << offset;
87       return L4_EOK;
88     case L4RTC_OPCODE_get_linux_tsc_scaler:
89       l4_uint32_t scaler;
90       l4rtc_if_get_linux_tsc_scaler_component(&scaler);
91       ios << scaler;
92       return L4_EOK;
93     default:
94       return -L4_ENOSYS;
95     }
96 }
97
98 static L4::Server<> server(l4_utcb());
99
100 int
101 main()
102 {
103   get_base_time_func_t get_base_time;
104
105 #if defined ARCH_x86 || defined ARCH_amd64
106 #ifdef ARCH_x86
107   if (!(get_base_time = init_ux()))
108 #endif
109     if (!(get_base_time = init_x86()))
110 #endif
111       {
112         printf("Initialization failed, exiting\n");
113         exit(1);
114       }
115
116   try
117     {
118 #if defined ARCH_x86 || defined ARCH_amd64
119       l4_calibrate_tsc(l4re_kip());
120 #endif
121       if (get_base_time())
122         return 1;
123
124
125       L4::Cap<L4::Ipc_gate> cap = L4Re::Env::env()->get_cap<L4::Ipc_gate>("rtc");
126       L4Re::chksys(cap->bind_thread(L4Re::Env::env()->main_thread(), 0x1230),
127                    "bind to rtc server gate");
128     }
129   catch (L4::Runtime_error const &e)
130     {
131       L4::cerr << e << "TERMINATED\n";
132       abort();
133     }
134
135   server.loop(Rtc_dispatcher());
136 }