1 /**
2  * Utilities, generic file
3  */
4 /*
5  * (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
6  *               Alexander Warg <warg@os.inf.tu-dresden.de>,
7  *               Frank Mehnert <fm3@os.inf.tu-dresden.de>
8  *     economic rights: Technische Universität Dresden (Germany)
9  * This file is part of TUD:OS and distributed under the terms of the
10  * GNU Lesser General Public License 2.1.
11  * Please see the COPYING-LGPL-2.1 file for details.
12  */
13 #ifndef __L4UTIL__UTIL_H__
14 #define __L4UTIL__UTIL_H__
15 
16 #include <l4/sys/types.h>
17 #include <l4/sys/compiler.h>
18 #include <l4/sys/ipc.h>
19 
20 /**
21  * \defgroup l4util_api Utility Functions
22  */
23 
24 EXTERN_C_BEGIN
25 
26 /**
27  * Calculate l4 timeouts
28  * \ingroup l4util_api
29  * \param mus   time in microseconds. Special cases:
30  *              - 0 - > timeout 0
31  *              - ~0U -> timeout NEVER
32  * \return the corresponding l4_timeout value
33  */
34 L4_CV l4_timeout_s l4util_micros2l4to(unsigned int mus) L4_NOTHROW;
35 
36 /**
37  * Suspend thread for a period of \a ms milliseconds
38  * \param ms Time in milliseconds
39  * \ingroup l4util_api
40  */
41 L4_CV void l4_sleep(int ms) L4_NOTHROW;
42 
43 /**
44  * Suspend thread for a period of \a us microseconds.
45  * \param us Time in microseconds
46  * \ingroup l4util_api
47  *
48  * WARNING: This function is mostly bogus since the timer resolution of
49  *          current L4 implementations is about 1ms!
50  */
51 L4_CV void l4_usleep(int us) L4_NOTHROW;
52 
53 /**
54  * Go sleep and never wake up.
55  * \ingroup l4util_api
56  *
57  */
58 L4_INLINE void l4_sleep_forever(void) L4_NOTHROW L4_NORETURN;
59 
60 /**
61  * Touch data area to force mapping (read-only)
62  * \ingroup l4util_api
63  *
64  * \param addr Start of memory area to touch.
65  * \param size Size of area to touch.
66  */
67 L4_INLINE void
68 l4_touch_ro(const void *addr, unsigned size) L4_NOTHROW;
69 
70 /**
71  * Touch data areas to force mapping (read-write)
72  * \ingroup l4util_api
73  *
74  * \param addr Start of memory area to touch.
75  * \param size Size of area to touch.
76  */
77 L4_INLINE void
78 l4_touch_rw(const void *addr, unsigned size) L4_NOTHROW;
79 
80 
81 
82 /*
83  * Implementations
84  */
85 
86 L4_INLINE void
l4_sleep_forever(void)87 l4_sleep_forever(void) L4_NOTHROW
88 {
89   for (;;)
90     l4_ipc_sleep(L4_IPC_NEVER);
91 }
92 
93 L4_INLINE void
l4_touch_ro(const void * addr,unsigned size)94 l4_touch_ro(const void *addr, unsigned size) L4_NOTHROW
95 {
96   l4_addr_t b, e;
97 
98   b = l4_trunc_page((l4_addr_t)addr);
99   e = l4_trunc_page((l4_addr_t)addr + size - 1);
100 
101   for (; b <= e; b += L4_PAGESIZE)
102     (void)(*(volatile char *)b);
103 }
104 
105 
106 L4_INLINE void
l4_touch_rw(const void * addr,unsigned size)107 l4_touch_rw(const void *addr, unsigned size) L4_NOTHROW
108 {
109   l4_addr_t b, e;
110 
111   b = l4_trunc_page((l4_addr_t)addr);
112   e = l4_trunc_page((l4_addr_t)addr + size - 1);
113 
114   for (; b <= e; b += L4_PAGESIZE)
115     {
116       char x = *(volatile char *)b;
117       *(volatile char *)b = x;
118     }
119 }
120 
121 EXTERN_C_END
122 
123 #endif /* __L4UTIL__UTIL_H__ */
124