]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/commpage/commpage_gettimeofday.s
xnu-792.13.8.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_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30
31 #include <sys/appleapiopts.h>
32 #include <machine/cpu_capabilities.h>
33 #include <machine/commpage.h>
34
35 #define NSEC_PER_SEC 1000*1000*1000
36 #define NSEC_PER_USEC 1000
37
38 .text
39 .align 2, 0x90
40
41 Lgettimeofday:
42 push %ebp
43 mov %esp,%ebp
44 push %esi
45 push %edi
46 push %ebx
47
48 0:
49 cmp $0,_COMM_PAGE_TIMEENABLE
50 je 4f
51 mov _COMM_PAGE_TIMEBASE,%esi
52 mov _COMM_PAGE_TIMEBASE+4,%edi
53 mov _COMM_PAGE_TIMESTAMP,%ebx
54
55 mov $ _COMM_PAGE_NANOTIME,%eax
56 call *%eax /* get ns in %edx:%eax */
57
58 cmp _COMM_PAGE_TIMEBASE,%esi
59 jne 0b
60 cmp _COMM_PAGE_TIMEBASE+4,%edi
61 jne 0b
62 cmp $0,_COMM_PAGE_TIMEENABLE
63 je 4f
64
65 mov $ NSEC_PER_SEC,%ecx
66 sub %esi,%eax
67 sbb %edi,%edx
68 div %ecx
69 add %eax,%ebx
70
71 mov $ NSEC_PER_USEC,%ecx
72 mov %edx,%eax
73 xor %edx,%edx
74 div %ecx
75
76 mov 8(%ebp),%ecx
77 mov %ebx,(%ecx)
78 mov %eax,4(%ecx)
79 xor %eax,%eax
80
81 3:
82 pop %ebx
83 pop %edi
84 pop %esi
85 pop %ebp
86 ret
87 4: /* fail */
88 movl $1,%eax
89 jmp 3b
90
91 COMMPAGE_DESCRIPTOR(gettimeofday,_COMM_PAGE_GETTIMEOFDAY,0,0)
92
93
94 .code64
95 .text
96 .align 2, 0x90
97
98 Lgettimeofday_64: // %rdi = ptr to timeval
99 pushq %rbp // set up a frame for backtraces
100 movq %rsp,%rbp
101 movq %rdi,%r9 // save ptr to timeval
102 movq $_COMM_PAGE_32_TO_64(_COMM_PAGE_TIMEBASE),%r10
103 0:
104 cmpl $0,_TIMEENABLE(%r10) // is data valid? (test _COMM_PAGE_TIMEENABLE)
105 jz 4f // no
106 movq _TIMEBASE(%r10),%r11 // get _COMM_PAGE_TIMEBASE
107 movq $_COMM_PAGE_32_TO_64(_COMM_PAGE_NANOTIME),%rax
108 call *%rax // get %rax <- nanotime(), preserving %r9, %r10 and %r11
109 movl _TIMESTAMP(%r10),%r8d // get _COMM_PAGE_TIMESTAMP
110 cmpq _TIMEBASE(%r10),%r11 // has _COMM_PAGE_TIMEBASE changed?
111 jne 0b // loop until we have consistent data
112 cmpl $0,_TIMEENABLE(%r10) // is data valid? (test _COMM_PAGE_TIMEENABLE)
113 jz 4f // no
114
115 movl $ NSEC_PER_SEC,%ecx
116 subq %r11,%rax // generate nanoseconds since timestamp
117 movq %rax,%rdx
118 shrq $32,%rdx // get high half of delta in %edx
119 divl %ecx // %eax <- seconds since timestamp, %edx <- nanoseconds
120 addl %eax,%r8d // add seconds elapsed to timestamp seconds
121
122 movl $ NSEC_PER_USEC,%ecx
123 movl %edx,%eax
124 xorl %edx,%edx
125 divl %ecx // divide residual ns by 1000 to get residual us in %eax
126
127 movq %r8,(%r9) // store 64-bit seconds into timeval
128 movl %eax,8(%r9) // store 32-bit useconds into timeval
129 xorl %eax,%eax // return 0 for success
130 3:
131 popq %rbp
132 ret
133 4: // fail
134 movl $1,%eax
135 jmp 3b
136
137 COMMPAGE_DESCRIPTOR(gettimeofday_64,_COMM_PAGE_GETTIMEOFDAY,0,0)