The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/osfmk/i386/machine_routines_asm.s

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * Copyright (c) 2000-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 <i386/asm.h>
   30 #include <i386/proc_reg.h>
   31 #include <i386/eflags.h>
   32        
   33 #include <i386/postcode.h>
   34 #include <i386/apic.h>
   35 #include <assym.s>
   36 
   37 /*
   38 **      ml_get_timebase()
   39 **
   40 **      Entry   - %esp contains pointer to 64 bit structure.
   41 **
   42 **      Exit    - 64 bit structure filled in.
   43 **
   44 */
   45 ENTRY(ml_get_timebase)
   46 
   47                         movl    S_ARG0, %ecx
   48                         
   49                         rdtsc
   50                         
   51                         movl    %edx, 0(%ecx)
   52                         movl    %eax, 4(%ecx)
   53                         
   54                         ret
   55 
   56 /*
   57  *      Convert between various timer units 
   58  *
   59  *              uint64_t tmrCvt(uint64_t time, uint64_t *conversion)
   60  *
   61  *              This code converts 64-bit time units to other units.
   62  *              For example, the TSC is converted to HPET units.
   63  *
   64  *              Time is a 64-bit integer that is some number of ticks.
   65  *              Conversion is 64-bit fixed point number which is composed
   66  *              of a 32 bit integer and a 32 bit fraction. 
   67  *
   68  *              The time ticks are multiplied by the conversion factor.  The
   69  *              calculations are done as a 128-bit value but both the high
   70  *              and low words are dropped.  The high word is overflow and the
   71  *              low word is the fraction part of the result.
   72  *
   73  *              We return a 64-bit value.
   74  *
   75  *              Note that we can use this function to multiply 2 conversion factors.
   76  *              We do this in order to calculate the multiplier used to convert
   77  *              directly between any two units.
   78  *
   79  */
   80 
   81                         .globl  EXT(tmrCvt)
   82                         .align FALIGN
   83 
   84 LEXT(tmrCvt)
   85 
   86                         pushl   %ebp                                    // Save a volatile
   87                         movl    %esp,%ebp                               // Get the parameters - 8
   88                         pushl   %ebx                                    // Save a volatile
   89                         pushl   %esi                                    // Save a volatile
   90                         pushl   %edi                                    // Save a volatile
   91 
   92 //                      %ebp + 8        - low-order ts
   93 //                      %ebp + 12       - high-order ts
   94 //                      %ebp + 16       - low-order cvt
   95 //                      %ebp + 20       - high-order cvt
   96 
   97                         movl    8(%ebp),%eax                    // Get low-order ts
   98                         mull    16(%ebp)                                // Multiply by low-order conversion
   99                         movl    %edx,%edi                               // Need to save only the high order part
  100                         
  101                         movl    12(%ebp),%eax                   // Get the high-order ts
  102                         mull    16(%ebp)                                // Multiply by low-order conversion
  103                         addl    %eax,%edi                               // Add in the overflow from the low x low calculation
  104                         adcl    $0,%edx                                 // Add in any overflow to high high part
  105                         movl    %edx,%esi                               // Save high high part
  106                         
  107 //                      We now have the upper 64 bits of the 96 bit multiply of ts and the low half of cvt
  108 //                      in %esi:%edi
  109 
  110                         movl    8(%ebp),%eax                    // Get low-order ts
  111                         mull    20(%ebp)                                // Multiply by high-order conversion
  112                         movl    %eax,%ebx                               // Need to save the low order part
  113                         movl    %edx,%ecx                               // Need to save the high order part
  114                         
  115                         movl    12(%ebp),%eax                   // Get the high-order ts
  116                         mull    20(%ebp)                                // Multiply by high-order conversion
  117                         
  118 //                      Now have %ecx:%ebx as low part of high low and %edx:%eax as high part of high high
  119 //                      We don't care about the highest word since it is overflow
  120                         
  121                         addl    %edi,%ebx                               // Add the low words
  122                         adcl    %ecx,%esi                               // Add in the high plus carry from low
  123                         addl    %eax,%esi                               // Add in the rest of the high
  124                         
  125                         movl    %ebx,%eax                               // Pass back low word
  126                         movl    %esi,%edx                               // and the high word
  127                         
  128                         popl    %edi                                    // Restore a volatile
  129                         popl    %esi                                    // Restore a volatile
  130                         popl    %ebx                                    // Restore a volatile
  131                         popl    %ebp                                    // Restore a volatile
  132 
  133                         ret                                             // Leave...
  134 
  135                         .globl  EXT(_rtc_nanotime_store)
  136                         .align  FALIGN
  137 
  138 LEXT(_rtc_nanotime_store)
  139                 push            %ebp
  140                 movl            %esp,%ebp
  141                 push            %esi
  142 
  143                 mov             32(%ebp),%edx                           /* get ptr to rtc_nanotime_info */
  144                 
  145                 movl            RNT_GENERATION(%edx),%esi               /* get current generation */
  146                 movl            $0,RNT_GENERATION(%edx)                 /* flag data as being updated */
  147 
  148                 mov             8(%ebp),%eax
  149                 mov             %eax,RNT_TSC_BASE(%edx)
  150                 mov             12(%ebp),%eax
  151                 mov             %eax,RNT_TSC_BASE+4(%edx)
  152 
  153                 mov             24(%ebp),%eax
  154                 mov             %eax,RNT_SCALE(%edx)
  155 
  156                 mov             28(%ebp),%eax
  157                 mov             %eax,RNT_SHIFT(%edx)
  158 
  159                 mov             16(%ebp),%eax
  160                 mov             %eax,RNT_NS_BASE(%edx)
  161                 mov             20(%ebp),%eax
  162                 mov             %eax,RNT_NS_BASE+4(%edx)
  163                 
  164                 incl            %esi                                    /* next generation */
  165                 jnz             1f
  166                 incl            %esi                                    /* skip 0, which is a flag */
  167 1:              movl            %esi,RNT_GENERATION(%edx)               /* update generation and make usable */
  168 
  169                 pop             %esi
  170                 pop             %ebp
  171                 ret
  172 
  173 
  174 /* unint64_t _rtc_nanotime_read( rtc_nanotime_t *rntp, int slow );
  175  *
  176  * This is the same as the commpage nanotime routine, except that it uses the
  177  * kernel internal "rtc_nanotime_info" data instead of the commpage data.  The two copies
  178  * of data (one in the kernel and one in user space) are kept in sync by rtc_nanotime_update().
  179  *
  180  * There are actually two versions of the algorithm, one each for "slow" and "fast"
  181  * processors.  The more common "fast" algorithm is:
  182  *
  183  *      nanoseconds = (((rdtsc - rnt_tsc_base) * rnt_tsc_scale) / 2**32) - rnt_ns_base;
  184  *
  185  * Of course, the divide by 2**32 is a nop.  rnt_tsc_scale is a constant computed during initialization:
  186  *
  187  *      rnt_tsc_scale = (10e9 * 2**32) / tscFreq;
  188  *
  189  * The "slow" algorithm uses long division:
  190  *
  191  *      nanoseconds = (((rdtsc - rnt_tsc_base) * 10e9) / tscFreq) - rnt_ns_base;
  192  *
  193  * Since this routine is not synchronized and can be called in any context, 
  194  * we use a generation count to guard against seeing partially updated data.  In addition,
  195  * the _rtc_nanotime_store() routine -- just above -- zeroes the generation before
  196  * updating the data, and stores the nonzero generation only after all other data has been
  197  * stored.  Because IA32 guarantees that stores by one processor must be seen in order
  198  * by another, we can avoid using a lock.  We spin while the generation is zero.
  199  *
  200  * In accordance with the ABI, we return the 64-bit nanotime in %edx:%eax.
  201  */
  202  
  203                 .globl  EXT(_rtc_nanotime_read)
  204                 .align  FALIGN
  205 LEXT(_rtc_nanotime_read)
  206                 pushl           %ebp
  207                 movl            %esp,%ebp
  208                 pushl           %esi
  209                 pushl           %edi
  210                 pushl           %ebx
  211                 movl            8(%ebp),%edi                            /* get ptr to rtc_nanotime_info */
  212                 movl            12(%ebp),%eax                           /* get "slow" flag */
  213                 testl           %eax,%eax
  214                 jnz             Lslow
  215                 
  216                 /* Processor whose TSC frequency is faster than SLOW_TSC_THRESHOLD */
  217 0:
  218                 movl            RNT_GENERATION(%edi),%esi               /* get generation (0 if being changed) */
  219                 testl           %esi,%esi                               /* if being changed, loop until stable */
  220                 jz              0b
  221 
  222                 rdtsc                                                   /* get TSC in %edx:%eax */
  223                 subl            RNT_TSC_BASE(%edi),%eax
  224                 sbbl            RNT_TSC_BASE+4(%edi),%edx
  225 
  226                 movl            RNT_SCALE(%edi),%ecx
  227 
  228                 movl            %edx,%ebx
  229                 mull            %ecx
  230                 movl            %ebx,%eax
  231                 movl            %edx,%ebx
  232                 mull            %ecx
  233                 addl            %ebx,%eax
  234                 adcl            $0,%edx
  235 
  236                 addl            RNT_NS_BASE(%edi),%eax
  237                 adcl            RNT_NS_BASE+4(%edi),%edx
  238 
  239                 cmpl            RNT_GENERATION(%edi),%esi               /* have the parameters changed? */
  240                 jne             0b                                      /* yes, loop until stable */
  241 
  242                 popl            %ebx
  243                 popl            %edi
  244                 popl            %esi
  245                 popl            %ebp
  246                 ret
  247 
  248                 /* Processor whose TSC frequency is slower than or equal to SLOW_TSC_THRESHOLD */
  249 Lslow:
  250                 movl            RNT_GENERATION(%edi),%esi               /* get generation (0 if being changed) */
  251                 testl           %esi,%esi                               /* if being changed, loop until stable */
  252                 jz              Lslow
  253                 pushl           %esi                                    /* save generation */
  254                 pushl           RNT_SHIFT(%edi)                         /* save low 32 bits of tscFreq */
  255 
  256                 rdtsc                                                   /* get TSC in %edx:%eax */
  257                 subl            RNT_TSC_BASE(%edi),%eax
  258                 sbbl            RNT_TSC_BASE+4(%edi),%edx
  259 
  260                 /*
  261                 * Do the math to convert tsc ticks to nanoseconds.  We first
  262                 * do long multiply of 1 billion times the tsc.  Then we do
  263                 * long division by the tsc frequency
  264                 */
  265                 mov             $1000000000, %ecx                       /* number of nanoseconds in a second */
  266                 mov             %edx, %ebx
  267                 mul             %ecx
  268                 mov             %edx, %edi
  269                 mov             %eax, %esi
  270                 mov             %ebx, %eax
  271                 mul             %ecx
  272                 add             %edi, %eax
  273                 adc             $0, %edx                                /* result in edx:eax:esi */
  274                 mov             %eax, %edi
  275                 popl            %ecx                                    /* get low 32 tscFreq */
  276                 xor             %eax, %eax
  277                 xchg            %edx, %eax
  278                 div             %ecx
  279                 xor             %eax, %eax
  280                 mov             %edi, %eax
  281                 div             %ecx
  282                 mov             %eax, %ebx
  283                 mov             %esi, %eax
  284                 div             %ecx
  285                 mov             %ebx, %edx                              /* result in edx:eax */
  286                 
  287                 movl            8(%ebp),%edi                            /* recover ptr to rtc_nanotime_info */
  288                 popl            %esi                                    /* recover generation */
  289 
  290                 addl            RNT_NS_BASE(%edi),%eax
  291                 adcl            RNT_NS_BASE+4(%edi),%edx
  292 
  293                 cmpl            RNT_GENERATION(%edi),%esi               /* have the parameters changed? */
  294                 jne             Lslow                                   /* yes, loop until stable */
  295 
  296                 pop             %ebx
  297                 pop             %edi
  298                 pop             %esi
  299                 pop             %ebp
  300                 ret                                                     /* result in edx:eax */
  301 

Cache object: 1efcf929c07f942f12106c9480303bfe


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.