Rev 1453 | Rev 1501 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1453 | Rev 1462 | ||
---|---|---|---|
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 <sys/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 | #include <unistd.h> |
34 | #include <unistd.h> |
35 | #include <atomic.h> |
35 | #include <atomic.h> |
36 | #include <futex.h> |
36 | #include <futex.h> |
37 | 37 | ||
38 | #include <sysinfo.h> |
38 | #include <sysinfo.h> |
39 | #include <as.h> |
39 | #include <as.h> |
40 | #include <ddi.h> |
40 | #include <ddi.h> |
41 | 41 | ||
42 | /* Pointers to public variables with time */ |
42 | /* Pointers to public variables with time */ |
43 | struct { |
43 | struct { |
44 | volatile sysarg_t seconds1; |
44 | volatile sysarg_t seconds1; |
45 | volatile sysarg_t useconds; |
45 | volatile sysarg_t useconds; |
46 | volatile sysarg_t seconds2; |
46 | volatile sysarg_t seconds2; |
47 | } *ktime = NULL; |
47 | } *ktime = NULL; |
48 | 48 | ||
49 | 49 | ||
50 | /** POSIX gettimeofday |
50 | /** POSIX gettimeofday |
51 | * |
51 | * |
52 | * The time variables are memory mapped(RO) from kernel, which updates |
52 | * The time variables are memory mapped(RO) from kernel, which updates |
53 | * them periodically. As it is impossible to read 2 values atomically, we |
53 | * them periodically. As it is impossible to read 2 values atomically, we |
54 | * use a trick: First read a seconds, then read microseconds, then |
54 | * use a trick: First read a seconds, then read microseconds, then |
55 | * read seconds again. If a second elapsed in the meantime, read |
55 | * read seconds again. If a second elapsed in the meantime, read |
56 | * useconds again. This provides assurance, that at least the |
56 | * useconds again. This provides assurance, that at least the |
57 | * sequence of subsequent gettimeofday calls is ordered. |
57 | * sequence of subsequent gettimeofday calls is ordered. |
58 | */ |
58 | */ |
59 | #define TMAREA (100*1024*1024) |
59 | #define TMAREA (100*1024*1024) |
60 | int gettimeofday(struct timeval *tv, struct timezone *tz) |
60 | int gettimeofday(struct timeval *tv, struct timezone *tz) |
61 | { |
61 | { |
62 | void *mapping; |
62 | void *mapping; |
63 | sysarg_t s1, s2; |
63 | sysarg_t s1, s2; |
64 | sysarg_t t1; |
64 | sysarg_t rights; |
65 | int res; |
65 | int res; |
66 | 66 | ||
67 | if (!ktime) { |
67 | if (!ktime) { |
68 | /* TODO: specify better, where to map the area */ |
68 | /* TODO: specify better, where to map the area */ |
69 | /* Get the mapping of kernel clock */ |
69 | /* Get the mapping of kernel clock */ |
70 | res = ipc_call_sync_3(PHONE_NS, IPC_M_AS_AREA_RECV, |
70 | res = ipc_call_sync_3(PHONE_NS, IPC_M_AS_AREA_RECV, |
71 | TMAREA, |
71 | TMAREA, |
72 | PAGE_SIZE, |
72 | PAGE_SIZE, |
73 | AS_AREA_READ | AS_AREA_CACHEABLE, |
73 | 0, |
74 | &t1,&t1,&t1); |
74 | NULL,&rights,NULL); |
75 | if (res) { |
75 | if (res) { |
76 | printf("Failed to initialize timeofday memarea\n"); |
76 | printf("Failed to initialize timeofday memarea\n"); |
77 | _exit(1); |
77 | _exit(1); |
- | 78 | } |
|
- | 79 | if (rights != (AS_AREA_READ | AS_AREA_CACHEABLE)) { |
|
- | 80 | printf("Received bad rights on time area: %X\n", |
|
- | 81 | rights); |
|
- | 82 | as_area_destroy(TMAREA); |
|
- | 83 | _exit(1); |
|
78 | } |
84 | } |
79 | ktime = (void *) (TMAREA); |
85 | ktime = (void *) (TMAREA); |
80 | } |
86 | } |
81 | if (tz) { |
87 | if (tz) { |
82 | tz->tz_minuteswest = 0; |
88 | tz->tz_minuteswest = 0; |
83 | tz->tz_dsttime = DST_NONE; |
89 | tz->tz_dsttime = DST_NONE; |
84 | } |
90 | } |
85 | 91 | ||
86 | s2 = ktime->seconds2; |
92 | s2 = ktime->seconds2; |
87 | read_barrier(); |
93 | read_barrier(); |
88 | tv->tv_usec = ktime->useconds; |
94 | tv->tv_usec = ktime->useconds; |
89 | read_barrier(); |
95 | read_barrier(); |
90 | s1 = ktime->seconds1; |
96 | s1 = ktime->seconds1; |
91 | if (s1 != s2) { |
97 | if (s1 != s2) { |
92 | tv->tv_usec = 0; |
98 | tv->tv_usec = 0; |
93 | tv->tv_sec = s1 > s2 ? s1 : s2; |
99 | tv->tv_sec = s1 > s2 ? s1 : s2; |
94 | } else |
100 | } else |
95 | tv->tv_sec = s1; |
101 | tv->tv_sec = s1; |
96 | 102 | ||
97 | return 0; |
103 | return 0; |
98 | } |
104 | } |
99 | 105 | ||
100 | /** Wait unconditionally for specified microseconds */ |
106 | /** Wait unconditionally for specified microseconds */ |
101 | void usleep(unsigned long usec) |
107 | void usleep(unsigned long usec) |
102 | { |
108 | { |
103 | atomic_t futex = FUTEX_INITIALIZER; |
109 | atomic_t futex = FUTEX_INITIALIZER; |
104 | 110 | ||
105 | futex_initialize(&futex,0); |
111 | futex_initialize(&futex,0); |
106 | futex_down_timeout(&futex, usec, 0); |
112 | futex_down_timeout(&futex, usec, 0); |
107 | } |
113 | } |
108 | 114 |