2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 #include <sys/appleapiopts.h>
24 #include <machine/cpu_capabilities.h>
25 #include <machine/commpage.h>
38 COMMPAGE_DESCRIPTOR(mach_absolute_time,_COMM_PAGE_ABSOLUTE_TIME,1,0)
47 movl $(_COMM_PAGE_NANOTIME_INFO), %esi
50 * The nanotime info consists of:
51 * - base_tsc 64-bit timestamp register value
52 * - base_ns 64-bit corresponding nanosecond uptime value
53 * - scale 32-bit current scale multiplier
54 * - shift 32-bit current shift divider
55 * - check_tsc 64-bit timestamp check value
57 * This enables an timestamp register's value, tsc, to be converted
58 * into a nanosecond nanotime value, ns:
60 * ns = base_ns + ((tsc - base_tsc) * scale >> shift)
62 * The kernel updates this every tick or whenever a performance
63 * speed-step changes the scaling. To avoid locking, a duplicated
64 * sequence counting scheme is used. The base_tsc value is updated
65 * whenever the info starts to be changed, and check_tsc is updated
66 * to the same value at the end of the update. The regularity of
67 * update ensures that (tsc - base_tsc) is a 32-bit quantity.
68 * When a conversion is performed, we read base_tsc before we start
69 * and check_tsc at the end -- if there's a mis-match we repeat.
70 * It's sufficient to compare only the low-order 32-bits.
75 // Read nanotime info and stash in registers.
77 movl NANOTIME_BASE_TSC(%esi), %ebx // ebx := lo(base_tsc)
78 movl NANOTIME_BASE_NS(%esi), %ebp
79 movl NANOTIME_BASE_NS+4(%esi), %edi // edi:ebp := base_ns
80 movl NANOTIME_SHIFT(%esi), %ecx // ecx := shift
82 // Read timestamp register (tsc) and calculate delta.
84 rdtsc // edx:eax := tsc
85 subl %ebx, %eax // eax := (tsc - base_tsc)
86 movl NANOTIME_SCALE(%esi), %edx // edx := shift
88 // Check for consistency and re-read if necessary.
90 cmpl NANOTIME_CHECK_TSC(%esi), %ebx
94 // edx:eax := ((tsc - base_tsc) * scale)
99 // eax := ((tsc - base_tsc) * scale >> shift)
101 shrdl %cl, %edx, %eax
103 cmovnel %edx, %eax // %eax := %edx if shift == 32
108 // edx:eax = (base_ns + ((tsc - base_tsc) * scale >> shift))
119 COMMPAGE_DESCRIPTOR(nanotime,_COMM_PAGE_NANOTIME,1,0)