Index: /trunk/kStuff/kProfiler2/prfamd64msc.asm
===================================================================
--- /trunk/kStuff/kProfiler2/prfamd64msc.asm	(revision 3591)
+++ /trunk/kStuff/kProfiler2/prfamd64msc.asm	(revision 3592)
@@ -83,5 +83,5 @@
         push    r10
         push    r11
-        sub     rsp, 30h                ; rsp is aligned at this point (7 pushes).
+        sub     rsp, 28h                ; rsp is unaligned at this point (8 pushes).
                                         ; reserve 20h for spill, and 8 bytes for ts.
 
@@ -91,5 +91,5 @@
         or      r8, rax                 ; param 3 - the timestamp
         mov     [rsp + 20h], r8         ; save the tsc for later use.
-        lea     rdx, [rsp + 7*8 + 30h]  ; Param 2 - frame pointer (pointer to the return address of the function calling us)
+        lea     rdx, [rsp + 8*8 + 28h]  ; Param 2 - frame pointer (pointer to the return address of the function calling us)
         mov     rcx, [rdx]              ; Param 1 - The function address
         call    KPRF_ENTER
@@ -127,13 +127,13 @@
         push    r10
         push    r11
-        sub     rsp, 30h                ; rsp is aligned at this point (7 pushes).
+        sub     rsp, 28h                ; rsp is unaligned at this point (8 pushes).
                                         ; reserve 20h for spill, and 8 bytes for ts.
 
-        ; setting up the leave call frame.
+        ; setting up the enter call frame
         mov     r8d, edx
         shl     r8, 32
         or      r8, rax                 ; param 3 - the timestamp
         mov     [rsp + 20h], r8         ; save the tsc for later use.
-        lea     rdx, [rsp + 7*8 + 30h]  ; Param 2 - frame pointer (pointer to the return address of the function calling us)
+        lea     rdx, [rsp + 8*8 + 28h]  ; Param 2 - frame pointer (pointer to the return address of the function calling us)
         mov     rcx, [rdx]              ; Param 1 - The function address
         call    KPRF_LEAVE
@@ -148,5 +148,5 @@
 common_return_path:
         ; Update overhead
-        test    eax, eax
+        test    rax, rax
         jz      common_no_overhead
         cmp     byte [g_fCalibrated wrt rip], 0
@@ -166,5 +166,5 @@
 
         ; restore volatile registers.
-        add     rsp, 30h
+        add     rsp, 28h
         pop     r11
         pop     r10
@@ -180,12 +180,8 @@
 ; Data rsi points to while we're calibrating.
 struc CALIBDATA
-    .OverheadLo resd 1
-    .OverheadHi resd 1
-    .ProfiledLo resd 1
-    .ProfiledHi resd 1
-    .EnterTSLo  resd 1
-    .EnterTSHi  resd 1
-    .MinLo      resd 1
-    .MinHi      resd 1
+    .Overhead   resq 1
+    .Profiled   resq 1
+    .EnterTS    resq 1
+    .Min        resq 1
 endstruc
 
@@ -219,5 +215,5 @@
         mov     rsi, rsp                ; rsi points to the CALIBDATA
 
-        and     rsp, -15
+        and     rsp, -16
 
         ;
@@ -238,37 +234,39 @@
         ;
         mov     ecx, 200h
-        mov     dword [rsi + CALIBDATA.MinLo], 0ffffffffh
-        mov     dword [rsi + CALIBDATA.MinHi], 07fffffffh
+        mov     dword [rsi + CALIBDATA.Min], 0ffffffffh
+        mov     dword [rsi + CALIBDATA.Min + 4], 07fffffffh
 calib_inner_loop:
 
         ; zero the overhead and profiled times.
         xor     eax, eax
-        mov     [rsi + CALIBDATA.OverheadLo], rax
-        mov     [rsi + CALIBDATA.ProfiledLo], rax
+        mov     [rsi + CALIBDATA.Overhead], rax
+        mov     [rsi + CALIBDATA.Profiled], rax
         call    calib_nullproc
 
         ; subtract the overhead
-        mov     rax, [rsi + CALIBDATA.ProfiledLo]
-        sub     rax, [rsi + CALIBDATA.OverheadLo]
+        mov     rax, [rsi + CALIBDATA.Profiled]
+        sub     rax, [rsi + CALIBDATA.Overhead]
 
         ; update the minimum value.
         bt      rax, 63
         jc near calib_outer_dec        ; if negative, just simplify and shortcut
-        cmp     rax, [rsi + CALIBDATA.MinHi]
+        cmp     rax, [rsi + CALIBDATA.Min]
         jge     calib_inner_next
 calib_inner_update_minimum:
-        mov     [rsi + CALIBDATA.MinLo], rax
+        mov     [rsi + CALIBDATA.Min], rax
 calib_inner_next:
         loop    calib_inner_loop
 
         ; Is the minimum value acceptable?
-        test    dword [rsi + CALIBDATA.MinLo + 4], 80000000h
+        test    dword [rsi + CALIBDATA.Min + 4], 80000000h
         jnz     calib_outer_dec         ; simplify if negative.
-        cmp     dword [rsi + CALIBDATA.MinHi + 4], 0
+        cmp     dword [rsi + CALIBDATA.Min + 4], 0
         jnz     calib_outer_inc         ; this shouldn't be possible
-        cmp     dword [rsi + CALIBDATA.MinLo], 1fh
+        cmp     dword [rsi + CALIBDATA.Min], 1fh
         jbe     calib_outer_dec         ; too low - 2 ticks per pair is the minimum!
-        cmp     dword [rsi + CALIBDATA.MinLo], 30h
-        jbe     calib_done              ; this is fine!
+        ;cmp     dword [rsi + CALIBDATA.Min], 30h
+        ;jbe     calib_done              ; this is fine!
+        cmp     dword [rsi + CALIBDATA.Min], 70h ; - a bit weird...
+        jbe     calib_outer_next         ; do the full 200h*200h iteration
 calib_outer_inc:
         inc     dword [g_OverheadAdj wrt rip]
@@ -284,5 +282,5 @@
 
         ; epilog - restore it all.
-        leave
+        mov     rsp, rbp
         pop     r15
         pop     r14
@@ -300,4 +298,5 @@
         pop     rax
         popfq
+        pop     rbp
         ret
 
@@ -319,5 +318,5 @@
         push    r10
         push    r11
-        sub     rsp, 30h                ; rsp is aligned at this point (7 pushes).
+        sub     rsp, 28h                ; rsp is unaligned at this point (8 pushes).
                                         ; reserve 20h for spill, and 8 bytes for ts.
 
@@ -328,7 +327,7 @@
         mov     [rsp + 20h], r8
 
-        mov     [rsi + CALIBDATA.EnterTSLo], r8
-
-        lea     rax, [rsi + CALIBDATA.OverheadLo]
+        mov     [rsi + CALIBDATA.EnterTS], r8
+
+        lea     rax, [rsi + CALIBDATA.Overhead]
         jmp     common_overhead
 
@@ -348,5 +347,5 @@
         push    r10
         push    r11
-        sub     rsp, 30h                ; rsp is aligned at this point (7 pushes).
+        sub     rsp, 28h                ; rsp is unaligned at this point (8 pushes).
                                         ; reserve 20h for spill, and 8 bytes for ts.
 
@@ -357,8 +356,8 @@
         mov     [rsp + 20h], r8
 
-        sub     r8, [rsi + CALIBDATA.EnterTSLo]
-        add     [rsi + CALIBDATA.ProfiledLo], r8
-
-        lea     rax, [rsi + CALIBDATA.EnterTSLo]
+        sub     r8, [rsi + CALIBDATA.EnterTS]
+        add     [rsi + CALIBDATA.Profiled], r8
+
+        lea     rax, [rsi + CALIBDATA.EnterTS]
         jmp     common_overhead
 
