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