Subversion Repositories HelenOS-historic

Rev

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

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