[0b99e40] | 1 | /*
|
---|
| 2 | * Copyright (C) 2006 Ondrej Palkovsky
|
---|
| 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 |
|
---|
| 29 | /** @addtogroup libc
|
---|
| 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>
|
---|
[51dbadf3] | 43 | #include <ipc/services.h>
|
---|
[0b99e40] | 44 |
|
---|
| 45 | #include <sysinfo.h>
|
---|
| 46 | #include <as.h>
|
---|
| 47 | #include <ddi.h>
|
---|
| 48 |
|
---|
| 49 | /* Pointers to public variables with time */
|
---|
| 50 | struct {
|
---|
[2d1fde3b] | 51 | volatile sysarg_t seconds1;
|
---|
[0b99e40] | 52 | volatile sysarg_t useconds;
|
---|
[2d1fde3b] | 53 | volatile sysarg_t seconds2;
|
---|
[0b99e40] | 54 | } *ktime = NULL;
|
---|
| 55 |
|
---|
| 56 |
|
---|
| 57 | /** POSIX gettimeofday
|
---|
| 58 | *
|
---|
| 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
|
---|
| 61 | * use a trick: First read a seconds, then read microseconds, then
|
---|
| 62 | * read seconds again. If a second elapsed in the meantime, read
|
---|
| 63 | * useconds again. This provides assurance, that at least the
|
---|
| 64 | * sequence of subsequent gettimeofday calls is ordered.
|
---|
| 65 | */
|
---|
| 66 | int gettimeofday(struct timeval *tv, struct timezone *tz)
|
---|
| 67 | {
|
---|
| 68 | void *mapping;
|
---|
[2d1fde3b] | 69 | sysarg_t s1, s2;
|
---|
[3f695aad] | 70 | sysarg_t rights;
|
---|
[0b99e40] | 71 | int res;
|
---|
| 72 |
|
---|
| 73 | if (!ktime) {
|
---|
[bf9afa07] | 74 | mapping = as_get_mappable_page(PAGE_SIZE);
|
---|
[0b99e40] | 75 | /* Get the mapping of kernel clock */
|
---|
| 76 | res = ipc_call_sync_3(PHONE_NS, IPC_M_AS_AREA_RECV,
|
---|
[51dbadf3] | 77 | mapping, PAGE_SIZE, SERVICE_MEM_REALTIME,
|
---|
[3f695aad] | 78 | NULL,&rights,NULL);
|
---|
[0b99e40] | 79 | if (res) {
|
---|
| 80 | printf("Failed to initialize timeofday memarea\n");
|
---|
| 81 | _exit(1);
|
---|
| 82 | }
|
---|
[90f5d64] | 83 | if (! (rights & AS_AREA_READ)) {
|
---|
[3f695aad] | 84 | printf("Received bad rights on time area: %X\n",
|
---|
| 85 | rights);
|
---|
[bf9afa07] | 86 | as_area_destroy(mapping);
|
---|
[3f695aad] | 87 | _exit(1);
|
---|
| 88 | }
|
---|
[bf9afa07] | 89 | ktime = mapping;
|
---|
[0b99e40] | 90 | }
|
---|
[c042bdd] | 91 | if (tz) {
|
---|
| 92 | tz->tz_minuteswest = 0;
|
---|
| 93 | tz->tz_dsttime = DST_NONE;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[5bd03eb] | 96 | s2 = ktime->seconds2;
|
---|
| 97 | read_barrier();
|
---|
[0b99e40] | 98 | tv->tv_usec = ktime->useconds;
|
---|
| 99 | read_barrier();
|
---|
[5bd03eb] | 100 | s1 = ktime->seconds1;
|
---|
[2d1fde3b] | 101 | if (s1 != s2) {
|
---|
| 102 | tv->tv_usec = 0;
|
---|
| 103 | tv->tv_sec = s1 > s2 ? s1 : s2;
|
---|
| 104 | } else
|
---|
| 105 | tv->tv_sec = s1;
|
---|
[0b99e40] | 106 |
|
---|
| 107 | return 0;
|
---|
| 108 | }
|
---|
[44c6d88d] | 109 |
|
---|
[eaf34f7] | 110 | /** Wait unconditionally for specified microseconds */
|
---|
[44c6d88d] | 111 | void usleep(unsigned long usec)
|
---|
| 112 | {
|
---|
| 113 | atomic_t futex = FUTEX_INITIALIZER;
|
---|
| 114 |
|
---|
| 115 | futex_initialize(&futex,0);
|
---|
| 116 | futex_down_timeout(&futex, usec, 0);
|
---|
| 117 | }
|
---|
[b2951e2] | 118 |
|
---|
| 119 |
|
---|
| 120 | /** @}
|
---|
| 121 | */
|
---|
| 122 |
|
---|
| 123 |
|
---|