]> git.saurik.com Git - apple/libc.git/blob - x86_64/sys/i386_gettimeofday_asm.s
28a3bd2f6a10d225b4037ac769147c12ef130fca
[apple/libc.git] / x86_64 / sys / i386_gettimeofday_asm.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 "platfunc.h"
32
33 #define NSEC_PER_SEC 1000*1000*1000
34 #define NSEC_PER_USEC 1000
35
36 .private_extern ___commpage_gettimeofday
37 .align 4, 0x90
38 ___commpage_gettimeofday:
39 // %rdi = ptr to timeval
40 pushq %rbp // set up a frame for backtraces
41 pushq %r12 // push callee-saved registers we want to use
42 pushq %r13
43 pushq %r14
44 subq $8, %rsp
45 movq %rsp,%rbp
46 movq %rdi,%r12 // save ptr to timeval
47 movq $(_COMM_PAGE_TIME_DATA_START),%r13
48 0:
49 movl _GTOD_GENERATION(%r13),%r14d // get generation (0 if disabled)
50 testl %r14d,%r14d // disabled?
51 jz 4f
52
53 call _mach_absolute_time // get %rax <- nanotime()
54
55 movl _GTOD_SEC_BASE(%r13),%r8d // get _COMM_PAGE_TIMESTAMP
56 subq _GTOD_NS_BASE(%r13),%rax // generate nanoseconds since timestamp
57 cmpl _GTOD_GENERATION(%r13),%r14d // has data changed out from under us?
58 jne 0b
59
60 movl $ NSEC_PER_SEC,%ecx
61 movq %rax,%rdx
62 shrq $32,%rdx // get high half of delta in %edx
63 divl %ecx // %eax <- seconds since timestamp, %edx <- nanoseconds
64 addl %eax,%r8d // add seconds elapsed to timestamp seconds
65
66 movl $ NSEC_PER_USEC,%ecx
67 movl %edx,%eax
68 xorl %edx,%edx
69 divl %ecx // divide residual ns by 1000 to get residual us in %eax
70
71 movq %r8,(%r12) // store 64-bit seconds into timeval
72 movl %eax,8(%r12) // store 32-bit useconds into timeval
73 xorl %eax,%eax // return 0 for success
74 3:
75 addq $8, %rsp
76 popq %r14
77 popq %r13
78 popq %r12
79 popq %rbp
80 ret
81 4: // fail
82 movl $1,%eax
83 jmp 3b