2 * Copyright (c) 2003-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <sys/appleapiopts.h>
30 #include <machine/cpu_capabilities.h>
31 #include <machine/commpage.h>
33 #define NSEC_PER_SEC 1000*1000*1000
34 #define NSEC_PER_USEC 1000
36 COMMPAGE_FUNCTION_START(gettimeofday, 32, 4)
43 movl _COMM_PAGE_GTOD_GENERATION,%esi /* get generation (0 if disabled) */
44 testl %esi,%esi /* disabled? */
47 mov $ _COMM_PAGE_NANOTIME,%eax
48 call *%eax /* get ns in %edx:%eax */
51 sub _COMM_PAGE_GTOD_NS_BASE,%eax
52 sbb _COMM_PAGE_GTOD_NS_BASE+4,%edx
53 mov _COMM_PAGE_GTOD_SEC_BASE,%ebx /* load all the data before checking generation */
54 mov $ NSEC_PER_SEC,%ecx
56 cmpl _COMM_PAGE_GTOD_GENERATION,%esi /* has time data changed out from under us? */
62 mov $ NSEC_PER_USEC,%ecx
80 COMMPAGE_DESCRIPTOR(gettimeofday,_COMM_PAGE_GETTIMEOFDAY,0,0)
83 COMMPAGE_FUNCTION_START(gettimeofday_64, 64, 4)
84 // %rdi = ptr to timeval
85 pushq %rbp // set up a frame for backtraces
87 movq %rdi,%r9 // save ptr to timeval
88 movq $_COMM_PAGE_32_TO_64(_COMM_PAGE_TIME_DATA_START),%r10
90 movl _GTOD_GENERATION(%r10),%r11d // get generation (0 if disabled)
91 testl %r11d,%r11d // disabled?
94 movq $_COMM_PAGE_32_TO_64(_COMM_PAGE_NANOTIME),%rax
95 call *%rax // get %rax <- nanotime(), preserving %r9, %r10 and %r11
97 movl _GTOD_SEC_BASE(%r10),%r8d // get _COMM_PAGE_TIMESTAMP
98 subq _GTOD_NS_BASE(%r10),%rax // generate nanoseconds since timestamp
99 cmpl _GTOD_GENERATION(%r10),%r11d // has data changed out from under us?
102 movl $ NSEC_PER_SEC,%ecx
104 shrq $32,%rdx // get high half of delta in %edx
105 divl %ecx // %eax <- seconds since timestamp, %edx <- nanoseconds
106 addl %eax,%r8d // add seconds elapsed to timestamp seconds
108 movl $ NSEC_PER_USEC,%ecx
111 divl %ecx // divide residual ns by 1000 to get residual us in %eax
113 movq %r8,(%r9) // store 64-bit seconds into timeval
114 movl %eax,8(%r9) // store 32-bit useconds into timeval
115 xorl %eax,%eax // return 0 for success
122 COMMPAGE_DESCRIPTOR(gettimeofday_64,_COMM_PAGE_GETTIMEOFDAY,0,0)