Subversion Repositories HelenOS-historic

Rev

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

Rev 1442 Rev 1449
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
#include <time.h>
29
#include <sys/time.h>
30
#include <unistd.h>
30
#include <unistd.h>
31
#include <ipc/ipc.h>
31
#include <ipc/ipc.h>
32
#include <stdio.h>
32
#include <stdio.h>
33
#include <arch/barrier.h>
33
#include <arch/barrier.h>
34
 
34
 
35
#include <sysinfo.h>
35
#include <sysinfo.h>
36
#include <as.h>
36
#include <as.h>
37
#include <ddi.h>
37
#include <ddi.h>
38
 
38
 
39
/* Pointers to public variables with time */
39
/* Pointers to public variables with time */
40
struct {
40
struct {
41
    volatile sysarg_t seconds1;
41
    volatile sysarg_t seconds1;
42
    volatile sysarg_t useconds;
42
    volatile sysarg_t useconds;
43
    volatile sysarg_t seconds2;
43
    volatile sysarg_t seconds2;
44
} *ktime = NULL;
44
} *ktime = NULL;
45
 
45
 
46
 
46
 
47
/** POSIX gettimeofday
47
/** POSIX gettimeofday
48
 *
48
 *
49
 * The time variables are memory mapped(RO) from kernel, which updates
49
 * The time variables are memory mapped(RO) from kernel, which updates
50
 * them periodically. As it is impossible to read 2 values atomically, we
50
 * them periodically. As it is impossible to read 2 values atomically, we
51
 * use a trick: First read a seconds, then read microseconds, then
51
 * use a trick: First read a seconds, then read microseconds, then
52
 * read seconds again. If a second elapsed in the meantime, read
52
 * read seconds again. If a second elapsed in the meantime, read
53
 * useconds again. This provides assurance, that at least the
53
 * useconds again. This provides assurance, that at least the
54
 * sequence of subsequent gettimeofday calls is ordered.
54
 * sequence of subsequent gettimeofday calls is ordered.
55
 */
55
 */
56
#define TMAREA (100*1024*1024)
56
#define TMAREA (100*1024*1024)
57
int gettimeofday(struct timeval *tv, struct timezone *tz)
57
int gettimeofday(struct timeval *tv, struct timezone *tz)
58
{
58
{
59
    void *mapping;
59
    void *mapping;
60
    sysarg_t s1, s2;
60
    sysarg_t s1, s2;
61
    sysarg_t t1;
61
    sysarg_t t1;
62
    int res;
62
    int res;
63
 
63
 
64
    if (!ktime) {
64
    if (!ktime) {
65
        /* TODO: specify better, where to map the area */
65
        /* TODO: specify better, where to map the area */
66
        /* Get the mapping of kernel clock */
66
        /* Get the mapping of kernel clock */
67
        res = ipc_call_sync_3(PHONE_NS, IPC_M_AS_AREA_RECV,
67
        res = ipc_call_sync_3(PHONE_NS, IPC_M_AS_AREA_RECV,
68
                      TMAREA,
68
                      TMAREA,
69
                      PAGE_SIZE,
69
                      PAGE_SIZE,
70
                      AS_AREA_READ | AS_AREA_CACHEABLE,
70
                      AS_AREA_READ | AS_AREA_CACHEABLE,
71
                      &t1,&t1,&t1);
71
                      &t1,&t1,&t1);
72
        if (res) {
72
        if (res) {
73
            printf("Failed to initialize timeofday memarea\n");
73
            printf("Failed to initialize timeofday memarea\n");
74
            _exit(1);
74
            _exit(1);
75
        }
75
        }
76
        ktime = (void *) (TMAREA);
76
        ktime = (void *) (TMAREA);
77
    }
77
    }
78
    if (tz) {
78
    if (tz) {
79
        tz->tz_minuteswest = 0;
79
        tz->tz_minuteswest = 0;
80
        tz->tz_dsttime = DST_NONE;
80
        tz->tz_dsttime = DST_NONE;
81
    }
81
    }
82
 
82
 
83
    s2 = ktime->seconds2;
83
    s2 = ktime->seconds2;
84
    read_barrier();
84
    read_barrier();
85
    tv->tv_usec = ktime->useconds;
85
    tv->tv_usec = ktime->useconds;
86
    read_barrier();
86
    read_barrier();
87
    s1 = ktime->seconds1;
87
    s1 = ktime->seconds1;
88
    if (s1 != s2) {
88
    if (s1 != s2) {
89
        tv->tv_usec = 0;
89
        tv->tv_usec = 0;
90
        tv->tv_sec = s1 > s2 ? s1 : s2;
90
        tv->tv_sec = s1 > s2 ? s1 : s2;
91
    } else
91
    } else
92
        tv->tv_sec = s1;
92
        tv->tv_sec = s1;
93
 
93
 
94
    return 0;
94
    return 0;
95
}
95
}
96
 
96