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