[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>
|
---|
| 42 | #include <futex.h>
|
---|
[f8ddd17] | 43 | #include <sysinfo.h>
|
---|
[51dbadf3] | 44 | #include <ipc/services.h>
|
---|
[0b99e40] | 45 |
|
---|
| 46 | #include <sysinfo.h>
|
---|
| 47 | #include <as.h>
|
---|
| 48 | #include <ddi.h>
|
---|
| 49 |
|
---|
| 50 | /* Pointers to public variables with time */
|
---|
| 51 | struct {
|
---|
[2d1fde3b] | 52 | volatile sysarg_t seconds1;
|
---|
[0b99e40] | 53 | volatile sysarg_t useconds;
|
---|
[2d1fde3b] | 54 | volatile sysarg_t seconds2;
|
---|
[0b99e40] | 55 | } *ktime = NULL;
|
---|
| 56 |
|
---|
| 57 |
|
---|
| 58 | /** POSIX gettimeofday
|
---|
| 59 | *
|
---|
| 60 | * The time variables are memory mapped(RO) from kernel, which updates
|
---|
| 61 | * them periodically. As it is impossible to read 2 values atomically, we
|
---|
| 62 | * use a trick: First read a seconds, then read microseconds, then
|
---|
[0384428] | 63 | * read seconds again. If a second elapsed in the meantime, set it to zero.
|
---|
| 64 | * This provides assurance, that at least the
|
---|
[0b99e40] | 65 | * sequence of subsequent gettimeofday calls is ordered.
|
---|
| 66 | */
|
---|
| 67 | int gettimeofday(struct timeval *tv, struct timezone *tz)
|
---|
| 68 | {
|
---|
| 69 | void *mapping;
|
---|
[2d1fde3b] | 70 | sysarg_t s1, s2;
|
---|
[3f695aad] | 71 | sysarg_t rights;
|
---|
[0b99e40] | 72 | int res;
|
---|
| 73 |
|
---|
| 74 | if (!ktime) {
|
---|
[2057572] | 75 | mapping = as_get_mappable_page(PAGE_SIZE);
|
---|
[0b99e40] | 76 | /* Get the mapping of kernel clock */
|
---|
[2057572] | 77 | res = ipc_call_sync_3(PHONE_NS, IPC_M_AS_AREA_RECV,
|
---|
| 78 | (sysarg_t) mapping, PAGE_SIZE, SERVICE_MEM_REALTIME, NULL,
|
---|
| 79 | &rights, NULL);
|
---|
[0b99e40] | 80 | if (res) {
|
---|
| 81 | printf("Failed to initialize timeofday memarea\n");
|
---|
| 82 | _exit(1);
|
---|
| 83 | }
|
---|
[90f5d64] | 84 | if (! (rights & AS_AREA_READ)) {
|
---|
[3f695aad] | 85 | printf("Received bad rights on time area: %X\n",
|
---|
| 86 | rights);
|
---|
[bf9afa07] | 87 | as_area_destroy(mapping);
|
---|
[3f695aad] | 88 | _exit(1);
|
---|
| 89 | }
|
---|
[bf9afa07] | 90 | ktime = mapping;
|
---|
[0b99e40] | 91 | }
|
---|
[c042bdd] | 92 | if (tz) {
|
---|
| 93 | tz->tz_minuteswest = 0;
|
---|
| 94 | tz->tz_dsttime = DST_NONE;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[5bd03eb] | 97 | s2 = ktime->seconds2;
|
---|
| 98 | read_barrier();
|
---|
[0b99e40] | 99 | tv->tv_usec = ktime->useconds;
|
---|
| 100 | read_barrier();
|
---|
[5bd03eb] | 101 | s1 = ktime->seconds1;
|
---|
[2d1fde3b] | 102 | if (s1 != s2) {
|
---|
| 103 | tv->tv_usec = 0;
|
---|
| 104 | tv->tv_sec = s1 > s2 ? s1 : s2;
|
---|
| 105 | } else
|
---|
| 106 | tv->tv_sec = s1;
|
---|
[0b99e40] | 107 |
|
---|
| 108 | return 0;
|
---|
| 109 | }
|
---|
[44c6d88d] | 110 |
|
---|
[dd655970] | 111 | /** Wait unconditionally for specified number of microseconds */
|
---|
[44c6d88d] | 112 | void usleep(unsigned long usec)
|
---|
| 113 | {
|
---|
| 114 | atomic_t futex = FUTEX_INITIALIZER;
|
---|
| 115 |
|
---|
[dd655970] | 116 | futex_initialize(&futex, 0);
|
---|
[44c6d88d] | 117 | futex_down_timeout(&futex, usec, 0);
|
---|
| 118 | }
|
---|
[b2951e2] | 119 |
|
---|
[dd655970] | 120 | /** Wait unconditionally for specified number of seconds */
|
---|
| 121 | unsigned int sleep(unsigned int seconds)
|
---|
| 122 | {
|
---|
| 123 | atomic_t futex = FUTEX_INITIALIZER;
|
---|
| 124 |
|
---|
| 125 | futex_initialize(&futex, 0);
|
---|
| 126 |
|
---|
| 127 | /* Sleep in 1000 second steps to support
|
---|
| 128 | full argument range */
|
---|
| 129 | while (seconds > 0) {
|
---|
| 130 | unsigned int period = (seconds > 1000) ? 1000 : seconds;
|
---|
| 131 |
|
---|
| 132 | futex_down_timeout(&futex, period * 1000000, 0);
|
---|
| 133 | seconds -= period;
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[a46da63] | 137 | /** @}
|
---|
[b2951e2] | 138 | */
|
---|