]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/commpage/commpage_gettimeofday.s
xnu-792.22.5.tar.gz
[apple/xnu.git] / osfmk / i386 / commpage / commpage_gettimeofday.s
1 /*
2 * Copyright (c) 2003-2006 Apple Computer, 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 .text
37 .align 2, 0x90
38
39 Lgettimeofday:
40 push %ebp
41 mov %esp,%ebp
42 push %esi
43 push %edi
44 push %ebx
45
46 0:
47 cmp $0,_COMM_PAGE_TIMEENABLE
48 je 4f
49 mov _COMM_PAGE_TIMEBASE,%esi
50 mov _COMM_PAGE_TIMEBASE+4,%edi
51 mov _COMM_PAGE_TIMESTAMP,%ebx
52
53 mov $ _COMM_PAGE_NANOTIME,%eax
54 call *%eax /* get ns in %edx:%eax */
55
56 cmp _COMM_PAGE_TIMEBASE,%esi
57 jne 0b
58 cmp _COMM_PAGE_TIMEBASE+4,%edi
59 jne 0b
60 cmp $0,_COMM_PAGE_TIMEENABLE
61 je 4f
62
63 mov $ NSEC_PER_SEC,%ecx
64 sub %esi,%eax
65 sbb %edi,%edx
66 div %ecx
67 add %eax,%ebx
68
69 mov $ NSEC_PER_USEC,%ecx
70 mov %edx,%eax
71 xor %edx,%edx
72 div %ecx
73
74 mov 8(%ebp),%ecx
75 mov %ebx,(%ecx)
76 mov %eax,4(%ecx)
77 xor %eax,%eax
78
79 3:
80 pop %ebx
81 pop %edi
82 pop %esi
83 pop %ebp
84 ret
85 4: /* fail */
86 movl $1,%eax
87 jmp 3b
88
89 COMMPAGE_DESCRIPTOR(gettimeofday,_COMM_PAGE_GETTIMEOFDAY,0,0)
90
91
92 .code64
93 .text
94 .align 2, 0x90
95
96 Lgettimeofday_64: // %rdi = ptr to timeval
97 pushq %rbp // set up a frame for backtraces
98 movq %rsp,%rbp
99 movq %rdi,%r9 // save ptr to timeval
100 movq $_COMM_PAGE_32_TO_64(_COMM_PAGE_TIMEBASE),%r10
101 0:
102 cmpl $0,_TIMEENABLE(%r10) // is data valid? (test _COMM_PAGE_TIMEENABLE)
103 jz 4f // no
104 movq _TIMEBASE(%r10),%r11 // get _COMM_PAGE_TIMEBASE
105 movq $_COMM_PAGE_32_TO_64(_COMM_PAGE_NANOTIME),%rax
106 call *%rax // get %rax <- nanotime(), preserving %r9, %r10 and %r11
107 movl _TIMESTAMP(%r10),%r8d // get _COMM_PAGE_TIMESTAMP
108 cmpq _TIMEBASE(%r10),%r11 // has _COMM_PAGE_TIMEBASE changed?
109 jne 0b // loop until we have consistent data
110 cmpl $0,_TIMEENABLE(%r10) // is data valid? (test _COMM_PAGE_TIMEENABLE)
111 jz 4f // no
112
113 movl $ NSEC_PER_SEC,%ecx
114 subq %r11,%rax // generate nanoseconds since timestamp
115 movq %rax,%rdx
116 shrq $32,%rdx // get high half of delta in %edx
117 divl %ecx // %eax <- seconds since timestamp, %edx <- nanoseconds
118 addl %eax,%r8d // add seconds elapsed to timestamp seconds
119
120 movl $ NSEC_PER_USEC,%ecx
121 movl %edx,%eax
122 xorl %edx,%edx
123 divl %ecx // divide residual ns by 1000 to get residual us in %eax
124
125 movq %r8,(%r9) // store 64-bit seconds into timeval
126 movl %eax,8(%r9) // store 32-bit useconds into timeval
127 xorl %eax,%eax // return 0 for success
128 3:
129 popq %rbp
130 ret
131 4: // fail
132 movl $1,%eax
133 jmp 3b
134
135 COMMPAGE_DESCRIPTOR(gettimeofday_64,_COMM_PAGE_GETTIMEOFDAY,0,0)