]> git.saurik.com Git - apple/libc.git/blob - arm/sys/arm_commpage_gettimeofday.c
Libc-498.1.7.tar.gz
[apple/libc.git] / arm / sys / arm_commpage_gettimeofday.c
1 #include <time.h>
2 #include <tzfile.h>
3 #include <sys/time.h>
4 #include <errno.h>
5 #include <sys/syscall.h>
6 #include <unistd.h>
7 #include <mach/clock_types.h>
8 #include <mach/mach.h>
9 #include <mach/mach_time.h>
10 #include <machine/cpu_capabilities.h>
11
12 int __commpage_gettimeofday(struct timeval *);
13
14
15 #define TIME_ADD(rsecs, secs, rfrac, frac, unit) \
16 { \
17 (rfrac) += (frac); \
18 while ((rfrac) >= (unit)) { \
19 (rfrac) -= (unit); \
20 (rsecs) += 1; \
21 } \
22 (rsecs) += (secs); \
23 }
24
25
26 int __commpage_gettimeofday(struct timeval *tp) {
27 commpage_timeofday_data_t *commpage_timeofday_datap;
28 uint64_t tbr;
29 uint64_t t64;
30 uint64_t TimeBase;
31 uint32_t TimeStamp_usec;
32 uint32_t TimeStamp_sec;
33 uint32_t TimeBaseTicks_per_sec;
34 uint64_t TimeBase_magic;
35 uint32_t TimeBase_add;
36 uint32_t TimeBase_shift;
37 uint32_t x, q;
38
39
40 commpage_timeofday_datap = (commpage_timeofday_data_t *)_COMM_PAGE_TIMEOFDAY_DATA;
41
42 do {
43 TimeBase = commpage_timeofday_datap->TimeBase;
44 TimeStamp_sec = commpage_timeofday_datap->TimeStamp_sec;
45 TimeStamp_usec = commpage_timeofday_datap->TimeStamp_usec;
46 TimeBaseTicks_per_sec = commpage_timeofday_datap->TimeBaseTicks_per_sec;
47 TimeBase_magic = commpage_timeofday_datap->TimeBase_magic;
48 TimeBase_add = commpage_timeofday_datap->TimeBase_add;
49 TimeBase_shift = commpage_timeofday_datap->TimeBase_shift;
50 } while (TimeBase != commpage_timeofday_datap->TimeBase);
51
52 if (TimeBase == 0)
53 return(1);
54
55 tbr = mach_absolute_time();
56
57 t64 = tbr - TimeBase;
58
59 if (t64 >= (uint64_t)TimeBaseTicks_per_sec)
60 return(1);
61
62 x = (uint32_t)t64;
63 q = ((uint64_t)x * (uint32_t)(TimeBase_magic)) >> 32;
64 tp->tv_usec = TimeBase_add ? (((x - q) >> 1) + q) >> (TimeBase_shift - 1) : q >> TimeBase_shift;
65 tp->tv_sec = 0;
66
67 TIME_ADD(tp->tv_sec, TimeStamp_sec, tp->tv_usec, TimeStamp_usec, USEC_PER_SEC);
68
69 return(0);
70 }