Subversion Repositories HelenOS

Rev

Rev 2959 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2959 Rev 3535
1
/*
1
/*
2
 * Copyright (c) 2006 Ondrej Palkovsky
2
 * Copyright (c) 2006 Ondrej Palkovsky
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup libc
29
/** @addtogroup libc
30
 * @{
30
 * @{
31
 */
31
 */
32
/** @file
32
/** @file
33
 */
33
 */
34
 
34
 
35
#include <sys/time.h>
35
#include <sys/time.h>
36
#include <unistd.h>
36
#include <unistd.h>
37
#include <ipc/ipc.h>
37
#include <ipc/ipc.h>
38
#include <stdio.h>
38
#include <stdio.h>
39
#include <arch/barrier.h>
39
#include <arch/barrier.h>
40
#include <unistd.h>
40
#include <unistd.h>
41
#include <atomic.h>
41
#include <atomic.h>
42
#include <futex.h>
42
#include <futex.h>
43
#include <sysinfo.h>
43
#include <sysinfo.h>
44
#include <ipc/services.h>
44
#include <ipc/services.h>
45
 
45
 
46
#include <sysinfo.h>
46
#include <sysinfo.h>
47
#include <as.h>
47
#include <as.h>
48
#include <ddi.h>
48
#include <ddi.h>
49
 
49
 
-
 
50
#include <time.h>
-
 
51
 
50
/* Pointers to public variables with time */
52
/* Pointers to public variables with time */
51
struct {
53
struct {
52
    volatile sysarg_t seconds1;
54
    volatile sysarg_t seconds1;
53
    volatile sysarg_t useconds;
55
    volatile sysarg_t useconds;
54
    volatile sysarg_t seconds2;
56
    volatile sysarg_t seconds2;
55
} *ktime = NULL;
57
} *ktime = NULL;
56
 
58
 
57
/** Add microseconds to given timeval.
59
/** Add microseconds to given timeval.
58
 *
60
 *
59
 * @param tv        Destination timeval.
61
 * @param tv        Destination timeval.
60
 * @param usecs     Number of microseconds to add.
62
 * @param usecs     Number of microseconds to add.
61
 */
63
 */
62
void tv_add(struct timeval *tv, suseconds_t usecs)
64
void tv_add(struct timeval *tv, suseconds_t usecs)
63
{
65
{
64
    tv->tv_sec += usecs / 1000000;
66
    tv->tv_sec += usecs / 1000000;
65
    tv->tv_usec += usecs % 1000000;
67
    tv->tv_usec += usecs % 1000000;
66
    if (tv->tv_usec > 1000000) {
68
    if (tv->tv_usec > 1000000) {
67
        tv->tv_sec++;
69
        tv->tv_sec++;
68
        tv->tv_usec -= 1000000;
70
        tv->tv_usec -= 1000000;
69
    }
71
    }
70
}
72
}
71
 
73
 
72
/** Subtract two timevals.
74
/** Subtract two timevals.
73
 *
75
 *
74
 * @param tv1       First timeval.
76
 * @param tv1       First timeval.
75
 * @param tv2       Second timeval.
77
 * @param tv2       Second timeval.
76
 *
78
 *
77
 * @return      Return difference between tv1 and tv2 (tv1 - tv2) in
79
 * @return      Return difference between tv1 and tv2 (tv1 - tv2) in
78
 *          microseconds.
80
 *          microseconds.
79
 */
81
 */
80
suseconds_t tv_sub(struct timeval *tv1, struct timeval *tv2)
82
suseconds_t tv_sub(struct timeval *tv1, struct timeval *tv2)
81
{
83
{
82
    suseconds_t result;
84
    suseconds_t result;
83
 
85
 
84
    result = tv1->tv_usec - tv2->tv_usec;
86
    result = tv1->tv_usec - tv2->tv_usec;
85
    result += (tv1->tv_sec - tv2->tv_sec) * 1000000;
87
    result += (tv1->tv_sec - tv2->tv_sec) * 1000000;
86
 
88
 
87
    return result;
89
    return result;
88
}
90
}
89
 
91
 
90
/** Decide if one timeval is greater than the other.
92
/** Decide if one timeval is greater than the other.
91
 *
93
 *
92
 * @param t1        First timeval.
94
 * @param t1        First timeval.
93
 * @param t2        Second timeval.
95
 * @param t2        Second timeval.
94
 *
96
 *
95
 * @return      Return true tv1 is greater than tv2. Otherwise return
97
 * @return      Return true tv1 is greater than tv2. Otherwise return
96
 *          false.
98
 *          false.
97
 */
99
 */
98
int tv_gt(struct timeval *tv1, struct timeval *tv2)
100
int tv_gt(struct timeval *tv1, struct timeval *tv2)
99
{
101
{
100
    if (tv1->tv_sec > tv2->tv_sec)
102
    if (tv1->tv_sec > tv2->tv_sec)
101
        return 1;
103
        return 1;
102
    if (tv1->tv_sec == tv2->tv_sec && tv1->tv_usec > tv2->tv_usec)
104
    if (tv1->tv_sec == tv2->tv_sec && tv1->tv_usec > tv2->tv_usec)
103
        return 1;
105
        return 1;
104
    return 0;
106
    return 0;
105
}
107
}
106
 
108
 
107
/** Decide if one timeval is greater than or equal to the other.
109
/** Decide if one timeval is greater than or equal to the other.
108
 *
110
 *
109
 * @param tv1       First timeval.
111
 * @param tv1       First timeval.
110
 * @param tv2       Second timeval.
112
 * @param tv2       Second timeval.
111
 *
113
 *
112
 * @return      Return true if tv1 is greater than or equal to tv2.
114
 * @return      Return true if tv1 is greater than or equal to tv2.
113
 *          Otherwise return false.
115
 *          Otherwise return false.
114
 */
116
 */
115
int tv_gteq(struct timeval *tv1, struct timeval *tv2)
117
int tv_gteq(struct timeval *tv1, struct timeval *tv2)
116
{
118
{
117
    if (tv1->tv_sec > tv2->tv_sec)
119
    if (tv1->tv_sec > tv2->tv_sec)
118
        return 1;
120
        return 1;
119
    if (tv1->tv_sec == tv2->tv_sec && tv1->tv_usec >= tv2->tv_usec)
121
    if (tv1->tv_sec == tv2->tv_sec && tv1->tv_usec >= tv2->tv_usec)
120
        return 1;
122
        return 1;
121
    return 0;
123
    return 0;
122
}
124
}
123
 
125
 
124
 
126
 
125
/** POSIX gettimeofday
127
/** POSIX gettimeofday
126
 *
128
 *
127
 * The time variables are memory mapped(RO) from kernel, which updates
129
 * The time variables are memory mapped(RO) from kernel, which updates
128
 * them periodically. As it is impossible to read 2 values atomically, we
130
 * them periodically. As it is impossible to read 2 values atomically, we
129
 * use a trick: First read a seconds, then read microseconds, then
131
 * use a trick: First read a seconds, then read microseconds, then
130
 * read seconds again. If a second elapsed in the meantime, set it to zero.
132
 * read seconds again. If a second elapsed in the meantime, set it to zero.
131
 * This provides assurance, that at least the
133
 * This provides assurance, that at least the
132
 * sequence of subsequent gettimeofday calls is ordered.
134
 * sequence of subsequent gettimeofday calls is ordered.
133
 */
135
 */
134
int gettimeofday(struct timeval *tv, struct timezone *tz)
136
int gettimeofday(struct timeval *tv, struct timezone *tz)
135
{
137
{
136
    void *mapping;
138
    void *mapping;
137
    sysarg_t s1, s2;
139
    sysarg_t s1, s2;
138
    int rights;
140
    int rights;
139
    int res;
141
    int res;
140
 
142
 
141
    if (!ktime) {
143
    if (!ktime) {
142
        mapping = as_get_mappable_page(PAGE_SIZE);
144
        mapping = as_get_mappable_page(PAGE_SIZE);
143
        /* Get the mapping of kernel clock */
145
        /* Get the mapping of kernel clock */
144
        res = ipc_share_in_start_1_1(PHONE_NS, mapping, PAGE_SIZE,
146
        res = ipc_share_in_start_1_1(PHONE_NS, mapping, PAGE_SIZE,
145
            SERVICE_MEM_REALTIME, &rights);
147
            SERVICE_MEM_REALTIME, &rights);
146
        if (res) {
148
        if (res) {
147
            printf("Failed to initialize timeofday memarea\n");
149
            printf("Failed to initialize timeofday memarea\n");
148
            printf("Address was 0x%x\n", (unsigned)mapping);
150
            printf("Address was 0x%x\n", (unsigned)mapping);
149
            _exit(1);
151
            _exit(1);
150
        }
152
        }
151
        if (!(rights & AS_AREA_READ)) {
153
        if (!(rights & AS_AREA_READ)) {
152
            printf("Received bad rights on time area: %X\n",
154
            printf("Received bad rights on time area: %X\n",
153
                rights);
155
                rights);
154
            as_area_destroy(mapping);
156
            as_area_destroy(mapping);
155
            _exit(1);
157
            _exit(1);
156
        }
158
        }
157
        ktime = mapping;
159
        ktime = mapping;
158
    }
160
    }
159
    if (tz) {
161
    if (tz) {
160
        tz->tz_minuteswest = 0;
162
        tz->tz_minuteswest = 0;
161
        tz->tz_dsttime = DST_NONE;
163
        tz->tz_dsttime = DST_NONE;
162
    }
164
    }
163
 
165
 
164
    s2 = ktime->seconds2;
166
    s2 = ktime->seconds2;
165
    read_barrier();
167
    read_barrier();
166
    tv->tv_usec = ktime->useconds;
168
    tv->tv_usec = ktime->useconds;
167
    read_barrier();
169
    read_barrier();
168
    s1 = ktime->seconds1;
170
    s1 = ktime->seconds1;
169
    if (s1 != s2) {
171
    if (s1 != s2) {
170
        tv->tv_usec = 0;
172
        tv->tv_usec = 0;
171
        tv->tv_sec = s1 > s2 ? s1 : s2;
173
        tv->tv_sec = s1 > s2 ? s1 : s2;
172
    } else
174
    } else
173
        tv->tv_sec = s1;
175
        tv->tv_sec = s1;
174
 
176
 
175
    return 0;
177
    return 0;
176
}
178
}
177
 
179
 
178
time_t time(time_t *tloc)
180
time_t time(time_t *tloc)
179
{
181
{
180
    struct timeval tv;
182
    struct timeval tv;
181
 
183
 
182
    if (gettimeofday(&tv, NULL))
184
    if (gettimeofday(&tv, NULL))
183
        return (time_t) -1;
185
        return (time_t) -1;
184
    if (tloc)
186
    if (tloc)
185
        *tloc = tv.tv_sec;
187
        *tloc = tv.tv_sec;
186
    return tv.tv_sec;
188
    return tv.tv_sec;
187
}
189
}
188
 
190
 
189
/** Wait unconditionally for specified number of microseconds */
191
/** Wait unconditionally for specified number of microseconds */
190
int usleep(unsigned long usec)
192
int usleep(unsigned long usec)
191
{
193
{
192
    atomic_t futex = FUTEX_INITIALIZER;
194
    atomic_t futex = FUTEX_INITIALIZER;
193
 
195
 
194
    futex_initialize(&futex, 0);
196
    futex_initialize(&futex, 0);
195
    futex_down_timeout(&futex, usec, 0);
197
    futex_down_timeout(&futex, usec, 0);
196
    return 0;
198
    return 0;
197
}
199
}
198
 
200
 
199
/** Wait unconditionally for specified number of seconds */
201
/** Wait unconditionally for specified number of seconds */
200
unsigned int sleep(unsigned int seconds)
202
unsigned int sleep(unsigned int seconds)
201
{
203
{
202
    atomic_t futex = FUTEX_INITIALIZER;
204
    atomic_t futex = FUTEX_INITIALIZER;
203
 
205
 
204
    futex_initialize(&futex, 0);
206
    futex_initialize(&futex, 0);
205
   
207
   
206
    /* Sleep in 1000 second steps to support
208
    /* Sleep in 1000 second steps to support
207
       full argument range */
209
       full argument range */
208
    while (seconds > 0) {
210
    while (seconds > 0) {
209
        unsigned int period = (seconds > 1000) ? 1000 : seconds;
211
        unsigned int period = (seconds > 1000) ? 1000 : seconds;
210
   
212
   
211
        futex_down_timeout(&futex, period * 1000000, 0);
213
        futex_down_timeout(&futex, period * 1000000, 0);
212
        seconds -= period;
214
        seconds -= period;
213
    }
215
    }
214
    return 0;
216
    return 0;
215
}
217
}
216
 
218
 
217
/** @}
219
/** @}
218
 */
220
 */
219
 
221