]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/commpage/commpage_gettimeofday.s
xnu-1504.15.3.tar.gz
[apple/xnu.git] / osfmk / i386 / commpage / commpage_gettimeofday.s
1 /*
2 * Copyright (c) 2003-2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <sys/appleapiopts.h>
30 #include <machine/cpu_capabilities.h>
31 #include <machine/commpage.h>
32
33 #define NSEC_PER_SEC 1000*1000*1000
34 #define NSEC_PER_USEC 1000
35
36 COMMPAGE_FUNCTION_START(gettimeofday, 32, 4)
37 push %ebp
38 mov %esp,%ebp
39 push %esi
40 push %ebx
41
42 0:
43 movl _COMM_PAGE_GTOD_GENERATION,%esi /* get generation (0 if disabled) */
44 testl %esi,%esi /* disabled? */
45 jz 4f
46
47 mov $ _COMM_PAGE_NANOTIME,%eax
48 call *%eax /* get ns in %edx:%eax */
49
50
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
55
56 cmpl _COMM_PAGE_GTOD_GENERATION,%esi /* has time data changed out from under us? */
57 jne 0b
58
59 div %ecx
60 add %eax,%ebx
61
62 mov $ NSEC_PER_USEC,%ecx
63 mov %edx,%eax
64 xor %edx,%edx
65 div %ecx
66
67 mov 8(%ebp),%ecx
68 mov %ebx,(%ecx)
69 mov %eax,4(%ecx)
70 xor %eax,%eax
71
72 3:
73 pop %ebx
74 pop %esi
75 pop %ebp
76 ret
77 4: /* fail */
78 movl $1,%eax
79 jmp 3b
80 COMMPAGE_DESCRIPTOR(gettimeofday,_COMM_PAGE_GETTIMEOFDAY,0,0)
81
82
83 COMMPAGE_FUNCTION_START(gettimeofday_64, 64, 4)
84 // %rdi = ptr to timeval
85 pushq %rbp // set up a frame for backtraces
86 movq %rsp,%rbp
87 movq %rdi,%r9 // save ptr to timeval
88 movq $_COMM_PAGE_32_TO_64(_COMM_PAGE_TIME_DATA_START),%r10
89 0:
90 movl _GTOD_GENERATION(%r10),%r11d // get generation (0 if disabled)
91 testl %r11d,%r11d // disabled?
92 jz 4f
93
94 movq $_COMM_PAGE_32_TO_64(_COMM_PAGE_NANOTIME),%rax
95 call *%rax // get %rax <- nanotime(), preserving %r9, %r10 and %r11
96
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?
100 jne 0b
101
102 movl $ NSEC_PER_SEC,%ecx
103 movq %rax,%rdx
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
107
108 movl $ NSEC_PER_USEC,%ecx
109 movl %edx,%eax
110 xorl %edx,%edx
111 divl %ecx // divide residual ns by 1000 to get residual us in %eax
112
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
116 3:
117 popq %rbp
118 ret
119 4: // fail
120 movl $1,%eax
121 jmp 3b
122 COMMPAGE_DESCRIPTOR(gettimeofday_64,_COMM_PAGE_GETTIMEOFDAY,0,0)