Subversion Repositories HelenOS

Rev

Rev 2015 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2015 Rev 2071
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
/* Pointers to public variables with time */
50
/* Pointers to public variables with time */
51
struct {
51
struct {
52
    volatile sysarg_t seconds1;
52
    volatile sysarg_t seconds1;
53
    volatile sysarg_t useconds;
53
    volatile sysarg_t useconds;
54
    volatile sysarg_t seconds2;
54
    volatile sysarg_t seconds2;
55
} *ktime = NULL;
55
} *ktime = NULL;
56
 
56
 
57
 
57
 
58
/** POSIX gettimeofday
58
/** POSIX gettimeofday
59
 *
59
 *
60
 * The time variables are memory mapped(RO) from kernel, which updates
60
 * The time variables are memory mapped(RO) from kernel, which updates
61
 * them periodically. As it is impossible to read 2 values atomically, we
61
 * them periodically. As it is impossible to read 2 values atomically, we
62
 * use a trick: First read a seconds, then read microseconds, then
62
 * use a trick: First read a seconds, then read microseconds, then
63
 * read seconds again. If a second elapsed in the meantime, set it to zero.
63
 * read seconds again. If a second elapsed in the meantime, set it to zero.
64
 * This provides assurance, that at least the
64
 * This provides assurance, that at least the
65
 * sequence of subsequent gettimeofday calls is ordered.
65
 * sequence of subsequent gettimeofday calls is ordered.
66
 */
66
 */
67
int gettimeofday(struct timeval *tv, struct timezone *tz)
67
int gettimeofday(struct timeval *tv, struct timezone *tz)
68
{
68
{
69
    void *mapping;
69
    void *mapping;
70
    sysarg_t s1, s2;
70
    sysarg_t s1, s2;
71
    sysarg_t rights;
71
    sysarg_t rights;
72
    int res;
72
    int res;
73
 
73
 
74
    if (!ktime) {
74
    if (!ktime) {
75
        mapping = as_get_mappable_page(PAGE_SIZE, (int)
75
        mapping = as_get_mappable_page(PAGE_SIZE, (int)
76
            sysinfo_value("clock.fcolor"));
76
            sysinfo_value("clock.fcolor"));
77
        /* Get the mapping of kernel clock */
77
        /* Get the mapping of kernel clock */
78
        res = ipc_call_sync_3(PHONE_NS, IPC_M_AS_AREA_RECV, (sysarg_t)
78
        res = ipc_call_sync_3(PHONE_NS, IPC_M_AS_AREA_RECV, (sysarg_t)
79
            mapping, PAGE_SIZE, SERVICE_MEM_REALTIME, NULL, &rights,
79
            mapping, PAGE_SIZE, SERVICE_MEM_REALTIME, NULL, &rights,
80
            NULL);
80
            NULL);
81
        if (res) {
81
        if (res) {
82
            printf("Failed to initialize timeofday memarea\n");
82
            printf("Failed to initialize timeofday memarea\n");
83
            _exit(1);
83
            _exit(1);
84
        }
84
        }
85
        if (! (rights & AS_AREA_READ)) {
85
        if (! (rights & AS_AREA_READ)) {
86
            printf("Received bad rights on time area: %X\n",
86
            printf("Received bad rights on time area: %X\n",
87
                   rights);
87
                   rights);
88
            as_area_destroy(mapping);
88
            as_area_destroy(mapping);
89
            _exit(1);
89
            _exit(1);
90
        }
90
        }
91
        ktime = mapping;
91
        ktime = mapping;
92
    }
92
    }
93
    if (tz) {
93
    if (tz) {
94
        tz->tz_minuteswest = 0;
94
        tz->tz_minuteswest = 0;
95
        tz->tz_dsttime = DST_NONE;
95
        tz->tz_dsttime = DST_NONE;
96
    }
96
    }
97
 
97
 
98
    s2 = ktime->seconds2;
98
    s2 = ktime->seconds2;
99
    read_barrier();
99
    read_barrier();
100
    tv->tv_usec = ktime->useconds;
100
    tv->tv_usec = ktime->useconds;
101
    read_barrier();
101
    read_barrier();
102
    s1 = ktime->seconds1;
102
    s1 = ktime->seconds1;
103
    if (s1 != s2) {
103
    if (s1 != s2) {
104
        tv->tv_usec = 0;
104
        tv->tv_usec = 0;
105
        tv->tv_sec = s1 > s2 ? s1 : s2;
105
        tv->tv_sec = s1 > s2 ? s1 : s2;
106
    } else
106
    } else
107
        tv->tv_sec = s1;
107
        tv->tv_sec = s1;
108
 
108
 
109
    return 0;
109
    return 0;
110
}
110
}
111
 
111
 
112
/** Wait unconditionally for specified microseconds */
112
/** Wait unconditionally for specified microseconds */
113
void usleep(unsigned long usec)
113
void usleep(unsigned long usec)
114
{
114
{
115
    atomic_t futex = FUTEX_INITIALIZER;
115
    atomic_t futex = FUTEX_INITIALIZER;
116
 
116
 
117
    futex_initialize(&futex,0);
117
    futex_initialize(&futex,0);
118
    futex_down_timeout(&futex, usec, 0);
118
    futex_down_timeout(&futex, usec, 0);
119
}
119
}
120
 
120
 
121
/** @}
121
/** @}
122
 */
122
 */
123
 
123