1 /**
2  * \file
3  * Kernel Info Page access functions.
4  * \ingroup l4_api
5  */
6 /*
7  * (c) 2008-2013 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
8  *               Alexander Warg <warg@os.inf.tu-dresden.de>
9  *     economic rights: Technische Universität Dresden (Germany)
10  *
11  * This file is part of TUD:OS and distributed under the terms of the
12  * GNU General Public License 2.
13  * Please see the COPYING-GPL-2 file for details.
14  *
15  * As a special exception, you may use this file as part of a free software
16  * library without restriction.  Specifically, if other files instantiate
17  * templates or use macros or inline functions from this file, or you compile
18  * this file and link it with other files to produce an executable, this
19  * file does not by itself cause the resulting executable to be covered by
20  * the GNU General Public License.  This exception does not however
21  * invalidate any other reasons why the executable file might be covered by
22  * the GNU General Public License.
23  */
24 #pragma once
25 
26 #include <l4/sys/compiler.h>
27 #include <l4/sys/l4int.h>
28 
29 #include <l4/sys/__kip-arch.h>
30 
31 /**
32  * \internal
33  */
34 struct l4_kip_platform_info
35 {
36   char                             name[16];
37   l4_uint32_t                      is_mp;
38   struct l4_kip_platform_info_arch arch;
39 };
40 
41 #if L4_MWORD_BITS == 32
42 #  include <l4/sys/__kip-32bit.h>
43 #else
44 #  include <l4/sys/__kip-64bit.h>
45 #endif
46 
47 /**
48  * \addtogroup l4_kip_api
49  *
50  * C interface for the Kernel Interface Page:<br>
51  * \includefile{l4/sys/kip.h}
52  */
53 /*@{*/
54 
55 /**
56  * \internal
57  */
58 enum l4_kernel_info_consts_t
59 {
60   L4_KIP_VERSION_FIASCO      = 0x87004444,
61   L4_KIP_VERSION_FIASCO_MASK = 0xff00ffff,
62 };
63 
64 enum
65 {
66   /**
67    * Offset of KIP code (provided by the kernel) for reading the KIP clock in
68    * microseconds. If the kernel is configured for a fine-grained KIP clock
69    * (CONFIG_SYNC_TSC enabled for IA32, ARM_SYNC_CLOCK for ARM), this code
70    * provides the KIP clock with microseconds granularity and accuracy by
71    * reading the hardware clock used by the kernel and transforming this value
72    * into microseconds. Otherwise this code just reads the KIP clock value.
73    */
74   L4_KIP_OFFS_READ_US        = 0x900,
75 
76   /**
77    * Offset of KIP code (provided by the kernel) for reading the time stamp
78    * counter and transforming this value into nanoseconds. If the kernel is
79    * configured for fine-grained KIP clock (CONFIG_SYNC enabled for IA32,
80    * ARM_SYNC_CLOCK for ARM), this code provides the KIP clock with nanoseconds
81    * granularity and accuracy by reading the hardware clock used by the kernel
82    * and transforming this value into nanoseconds. Otherwise this code just
83    * reads the KIP clock value and multiplies it by 1000.
84    */
85   L4_KIP_OFFS_READ_NS        = 0x980,
86 };
87 
88 /**
89  * Kernel Info Page identifier ("L4µK").
90  */
91 #define L4_KERNEL_INFO_MAGIC (0x4BE6344CL) /* "L4µK" */
92 
93 
94 /**
95  *  Get the kernel version.
96  *
97  * \param kip  Kernel Info Page.
98  *
99  * \return Kernel version string. 0 if KIP could not be mapped.
100  */
101 L4_INLINE l4_umword_t l4_kip_version(l4_kernel_info_t *kip) L4_NOTHROW;
102 
103 /**
104  *  Get the kernel version string.
105  *
106  * \param kip  Kernel Info Page.
107  *
108  * \return Kernel version string.
109  */
110 L4_INLINE const char *l4_kip_version_string(l4_kernel_info_t *kip) L4_NOTHROW;
111 
112 /**
113  * Return offset in bytes of version_strings relative to the KIP base.
114  *
115  * \param kip  Pointer to the kernel info page (KIP).
116  *
117  * \return offset of version_strings relative to the KIP base address, in
118  *         bytes.
119  */
120 L4_INLINE int
121 l4_kernel_info_version_offset(l4_kernel_info_t *kip) L4_NOTHROW;
122 
123 /**
124  * Return clock value from the KIP.
125  *
126  * \param kip  Pointer to the kernel info page (KIP).
127  *
128  * \return Value of the clock field in the KIP.
129  *
130  * The KIP clock always contains the current (relative) time in micro seconds
131  * independently of the CPU frequency. The clock is only guaranteed to be
132  * accurate within the scheduling granularity announced in the KIP.
133  *
134  * This function basically calls the KIP code for reading the KIP clock with
135  * microseconds resolution. The accuracy depends on the platform and the kernel
136  * configuration.
137  *
138  * \see L4_KIP_OFFS_READ_US.
139  */
140 L4_INLINE l4_cpu_time_t
141 l4_kip_clock(l4_kernel_info_t *kip) L4_NOTHROW;
142 
143 /**
144  * Return least significant machine word of clock value from the KIP.
145  *
146  * \param kip  Pointer to the kernel info page (KIP).
147  *
148  * \return Lower machine word of clock value from the KIP.
149  *
150  * This function will always provide the least significant machine word of the
151  * clock value from the KIP, regardless of the kernel configuration.
152  */
153 L4_INLINE l4_umword_t
154 l4_kip_clock_lw(l4_kernel_info_t *kip) L4_NOTHROW;
155 
156 /**
157  * Return current clock using the KIP in nanoseconds.
158  *
159  * \param kip  Pointer to the kernel info page (KIP).
160  *
161  * \return Value of the current clock in nanoseconds.
162  *
163  * This function basically calls the KIP code for reading the KIP clock with
164  * nanoseconds resolution. The accuracy depends on the platform and the kernel
165  * configuration.
166  *
167  * \see L4_KIP_OFFS_READ_NS.
168  */
169 L4_INLINE l4_uint64_t
170 l4_kip_clock_ns(l4_kernel_info_t *kip) L4_NOTHROW;
171 
172 /*@}*/
173 
174 /*************************************************************************
175  * Implementations
176  *************************************************************************/
177 
178 L4_INLINE l4_umword_t
l4_kip_version(l4_kernel_info_t * kip)179 l4_kip_version(l4_kernel_info_t *kip) L4_NOTHROW
180 { return kip->version & L4_KIP_VERSION_FIASCO_MASK; }
181 
182 L4_INLINE const char*
l4_kip_version_string(l4_kernel_info_t * k)183 l4_kip_version_string(l4_kernel_info_t *k) L4_NOTHROW
184 { return (const char *)k + l4_kernel_info_version_offset(k); }
185 
186 L4_INLINE int
l4_kernel_info_version_offset(l4_kernel_info_t * kip)187 l4_kernel_info_version_offset(l4_kernel_info_t *kip) L4_NOTHROW
188 { return kip->offset_version_strings << 4; }
189 
190 L4_INLINE l4_cpu_time_t
l4_kip_clock(l4_kernel_info_t * kip)191 l4_kip_clock(l4_kernel_info_t *kip) L4_NOTHROW
192 {
193   // Use kernel-provided code to determine the current clock.
194   typedef l4_uint64_t (*kip_time_fn_read_us)(void);
195   kip_time_fn_read_us read_us =
196     (kip_time_fn_read_us)((l4_uint8_t*)kip + L4_KIP_OFFS_READ_US);
197   return read_us();
198 }
199 
200 L4_INLINE l4_cpu_time_t
l4_kip_clock_ns(l4_kernel_info_t * kip)201 l4_kip_clock_ns(l4_kernel_info_t *kip) L4_NOTHROW
202 {
203   typedef l4_uint64_t (*kip_time_fn_read_ns)(void);
204   kip_time_fn_read_ns read_ns =
205     (kip_time_fn_read_ns)((l4_uint8_t*)kip + L4_KIP_OFFS_READ_NS);
206   return read_ns();
207 }
208 
209 L4_INLINE l4_umword_t
l4_kip_clock_lw(l4_kernel_info_t * kip)210 l4_kip_clock_lw(l4_kernel_info_t *kip) L4_NOTHROW
211 {
212   /* We do the casting because the clock field is volatile */
213   unsigned long *c = (unsigned long *)&kip->_clock_val;
214   l4_mb();
215   return c[0];
216 }
217