[0b99e40] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Ondrej Palkovsky
|
---|
[0b99e40] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 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
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 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
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 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
|
---|
| 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
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
[b2951e2] | 27 | */
|
---|
| 28 |
|
---|
[a46da63] | 29 | /** @addtogroup libc
|
---|
[b2951e2] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
[0b99e40] | 33 | */
|
---|
| 34 |
|
---|
[f25b73d6] | 35 | #include <sys/time.h>
|
---|
[0b99e40] | 36 | #include <unistd.h>
|
---|
| 37 | #include <ipc/ipc.h>
|
---|
| 38 | #include <stdio.h>
|
---|
| 39 | #include <arch/barrier.h>
|
---|
[44c6d88d] | 40 | #include <unistd.h>
|
---|
| 41 | #include <atomic.h>
|
---|
[f8ddd17] | 42 | #include <sysinfo.h>
|
---|
[51dbadf3] | 43 | #include <ipc/services.h>
|
---|
[d9ece1cb] | 44 | #include <libc.h>
|
---|
[0b99e40] | 45 |
|
---|
| 46 | #include <sysinfo.h>
|
---|
| 47 | #include <as.h>
|
---|
| 48 | #include <ddi.h>
|
---|
| 49 |
|
---|
[c61d34b] | 50 | #include <time.h>
|
---|
| 51 |
|
---|
[0b99e40] | 52 | /* Pointers to public variables with time */
|
---|
| 53 | struct {
|
---|
[2d1fde3b] | 54 | volatile sysarg_t seconds1;
|
---|
[0b99e40] | 55 | volatile sysarg_t useconds;
|
---|
[2d1fde3b] | 56 | volatile sysarg_t seconds2;
|
---|
[0b99e40] | 57 | } *ktime = NULL;
|
---|
| 58 |
|
---|
[daa90e8] | 59 | /** Add microseconds to given timeval.
|
---|
| 60 | *
|
---|
| 61 | * @param tv Destination timeval.
|
---|
| 62 | * @param usecs Number of microseconds to add.
|
---|
| 63 | */
|
---|
| 64 | void tv_add(struct timeval *tv, suseconds_t usecs)
|
---|
| 65 | {
|
---|
| 66 | tv->tv_sec += usecs / 1000000;
|
---|
| 67 | tv->tv_usec += usecs % 1000000;
|
---|
| 68 | if (tv->tv_usec > 1000000) {
|
---|
| 69 | tv->tv_sec++;
|
---|
| 70 | tv->tv_usec -= 1000000;
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | /** Subtract two timevals.
|
---|
| 75 | *
|
---|
| 76 | * @param tv1 First timeval.
|
---|
| 77 | * @param tv2 Second timeval.
|
---|
| 78 | *
|
---|
| 79 | * @return Return difference between tv1 and tv2 (tv1 - tv2) in
|
---|
| 80 | * microseconds.
|
---|
| 81 | */
|
---|
| 82 | suseconds_t tv_sub(struct timeval *tv1, struct timeval *tv2)
|
---|
| 83 | {
|
---|
| 84 | suseconds_t result;
|
---|
| 85 |
|
---|
| 86 | result = tv1->tv_usec - tv2->tv_usec;
|
---|
| 87 | result += (tv1->tv_sec - tv2->tv_sec) * 1000000;
|
---|
| 88 |
|
---|
| 89 | return result;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | /** Decide if one timeval is greater than the other.
|
---|
| 93 | *
|
---|
| 94 | * @param t1 First timeval.
|
---|
| 95 | * @param t2 Second timeval.
|
---|
| 96 | *
|
---|
| 97 | * @return Return true tv1 is greater than tv2. Otherwise return
|
---|
| 98 | * false.
|
---|
| 99 | */
|
---|
| 100 | int tv_gt(struct timeval *tv1, struct timeval *tv2)
|
---|
| 101 | {
|
---|
| 102 | if (tv1->tv_sec > tv2->tv_sec)
|
---|
| 103 | return 1;
|
---|
| 104 | if (tv1->tv_sec == tv2->tv_sec && tv1->tv_usec > tv2->tv_usec)
|
---|
| 105 | return 1;
|
---|
| 106 | return 0;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | /** Decide if one timeval is greater than or equal to the other.
|
---|
| 110 | *
|
---|
| 111 | * @param tv1 First timeval.
|
---|
| 112 | * @param tv2 Second timeval.
|
---|
| 113 | *
|
---|
| 114 | * @return Return true if tv1 is greater than or equal to tv2.
|
---|
| 115 | * Otherwise return false.
|
---|
| 116 | */
|
---|
| 117 | int tv_gteq(struct timeval *tv1, struct timeval *tv2)
|
---|
| 118 | {
|
---|
| 119 | if (tv1->tv_sec > tv2->tv_sec)
|
---|
| 120 | return 1;
|
---|
| 121 | if (tv1->tv_sec == tv2->tv_sec && tv1->tv_usec >= tv2->tv_usec)
|
---|
| 122 | return 1;
|
---|
| 123 | return 0;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[0b99e40] | 126 |
|
---|
| 127 | /** POSIX gettimeofday
|
---|
| 128 | *
|
---|
| 129 | * The time variables are memory mapped(RO) from kernel, which updates
|
---|
| 130 | * them periodically. As it is impossible to read 2 values atomically, we
|
---|
| 131 | * use a trick: First read a seconds, then read microseconds, then
|
---|
[0384428] | 132 | * read seconds again. If a second elapsed in the meantime, set it to zero.
|
---|
| 133 | * This provides assurance, that at least the
|
---|
[0b99e40] | 134 | * sequence of subsequent gettimeofday calls is ordered.
|
---|
| 135 | */
|
---|
| 136 | int gettimeofday(struct timeval *tv, struct timezone *tz)
|
---|
| 137 | {
|
---|
| 138 | void *mapping;
|
---|
[2d1fde3b] | 139 | sysarg_t s1, s2;
|
---|
[27d293a] | 140 | int rights;
|
---|
[0b99e40] | 141 | int res;
|
---|
| 142 |
|
---|
| 143 | if (!ktime) {
|
---|
[2057572] | 144 | mapping = as_get_mappable_page(PAGE_SIZE);
|
---|
[0b99e40] | 145 | /* Get the mapping of kernel clock */
|
---|
[215e375] | 146 | res = ipc_share_in_start_1_1(PHONE_NS, mapping, PAGE_SIZE,
|
---|
[27d293a] | 147 | SERVICE_MEM_REALTIME, &rights);
|
---|
[0b99e40] | 148 | if (res) {
|
---|
| 149 | printf("Failed to initialize timeofday memarea\n");
|
---|
| 150 | _exit(1);
|
---|
| 151 | }
|
---|
[813a703] | 152 | if (!(rights & AS_AREA_READ)) {
|
---|
[3f695aad] | 153 | printf("Received bad rights on time area: %X\n",
|
---|
[813a703] | 154 | rights);
|
---|
[bf9afa07] | 155 | as_area_destroy(mapping);
|
---|
[3f695aad] | 156 | _exit(1);
|
---|
| 157 | }
|
---|
[bf9afa07] | 158 | ktime = mapping;
|
---|
[0b99e40] | 159 | }
|
---|
[c042bdd] | 160 | if (tz) {
|
---|
| 161 | tz->tz_minuteswest = 0;
|
---|
| 162 | tz->tz_dsttime = DST_NONE;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
[5bd03eb] | 165 | s2 = ktime->seconds2;
|
---|
| 166 | read_barrier();
|
---|
[0b99e40] | 167 | tv->tv_usec = ktime->useconds;
|
---|
| 168 | read_barrier();
|
---|
[5bd03eb] | 169 | s1 = ktime->seconds1;
|
---|
[2d1fde3b] | 170 | if (s1 != s2) {
|
---|
| 171 | tv->tv_usec = 0;
|
---|
| 172 | tv->tv_sec = s1 > s2 ? s1 : s2;
|
---|
| 173 | } else
|
---|
| 174 | tv->tv_sec = s1;
|
---|
[0b99e40] | 175 |
|
---|
| 176 | return 0;
|
---|
| 177 | }
|
---|
[44c6d88d] | 178 |
|
---|
[813a703] | 179 | time_t time(time_t *tloc)
|
---|
| 180 | {
|
---|
| 181 | struct timeval tv;
|
---|
| 182 |
|
---|
| 183 | if (gettimeofday(&tv, NULL))
|
---|
| 184 | return (time_t) -1;
|
---|
| 185 | if (tloc)
|
---|
| 186 | *tloc = tv.tv_sec;
|
---|
| 187 | return tv.tv_sec;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
[dd655970] | 190 | /** Wait unconditionally for specified number of microseconds */
|
---|
[0c09f2b] | 191 | int usleep(unsigned long usec)
|
---|
[44c6d88d] | 192 | {
|
---|
[d9ece1cb] | 193 | (void) __SYSCALL1(SYS_THREAD_USLEEP, usec);
|
---|
[0c09f2b] | 194 | return 0;
|
---|
[44c6d88d] | 195 | }
|
---|
[b2951e2] | 196 |
|
---|
[dd655970] | 197 | /** Wait unconditionally for specified number of seconds */
|
---|
| 198 | unsigned int sleep(unsigned int seconds)
|
---|
| 199 | {
|
---|
| 200 | /* Sleep in 1000 second steps to support
|
---|
| 201 | full argument range */
|
---|
| 202 | while (seconds > 0) {
|
---|
| 203 | unsigned int period = (seconds > 1000) ? 1000 : seconds;
|
---|
| 204 |
|
---|
[d9ece1cb] | 205 | usleep(period * 1000000);
|
---|
[dd655970] | 206 | seconds -= period;
|
---|
| 207 | }
|
---|
[0c09f2b] | 208 | return 0;
|
---|
[dd655970] | 209 | }
|
---|
| 210 |
|
---|
[a46da63] | 211 | /** @}
|
---|
[b2951e2] | 212 | */
|
---|