Index: /trunk/kStuff/kProfiler2/Makefile.kmk
===================================================================
--- /trunk/kStuff/kProfiler2/Makefile.kmk	(revision 3585)
+++ /trunk/kStuff/kProfiler2/Makefile.kmk	(revision 3586)
@@ -56,4 +56,5 @@
 TEMPLATE_kPrf2_CXXFLAGS.os2     = -g
 TEMPLATE_kPrf2_CXXFLAGS.win     = -Zi -Zl -MD -W3 -GF -GR-
+TEMPLATE_kPrf2_CXXFLAGS.win.amd64 = -GS- #-FAcs
 ifneq ($(BUILD_TYPE),debug)
 TEMPLATE_kPrf2_CXXFLAGS.freebsd+= -O3
@@ -97,4 +98,5 @@
 kPrf2_DEFS.x86      = KPRF_BITS=32
 kPrf2_DEFS.amd64    = KPRF_BITS=64
+kPrf2_LDFLAGS.win.amd64 = -Entry:DllMain
 
 kPrf2_SOURCES       = \
@@ -110,4 +112,7 @@
 	KPRF_ENTER=_KPrfEnter \
 	KPRF_LEAVE=_KPrfLeave
+prfamd64msc.asm_DEFS.win.amd64 = \
+	KPRF_ENTER=KPrfEnter \
+	KPRF_LEAVE=KPrfLeave
 
 #
Index: /trunk/kStuff/kProfiler2/prfamd64msc.asm
===================================================================
--- /trunk/kStuff/kProfiler2/prfamd64msc.asm	(revision 3585)
+++ /trunk/kStuff/kProfiler2/prfamd64msc.asm	(revision 3586)
@@ -74,20 +74,23 @@
 _penter:
         ; save volatile register and get the time stamp.
-        push    eax
-        push    edx
+        push    rax
+        push    rdx
         rdtsc
-        pushfd
-        push    ecx
-
-        ; setting up the enter call frame (cdecl).
-        sub     esp, 4 + 4 + 8
-        mov     [esp + 0ch], edx        ; Param 3 - the timestamp
-        mov     [esp + 08h], eax
-        lea     edx, [esp + 24h]        ; Param 2 - frame pointer (pointer to the return address of the function calling us)
-        mov     [esp + 04h], edx
-        mov     eax, [esp + 20h]        ; Param 1 - The function address
-        sub     eax, 5                  ; call instruction
-        mov     [esp], eax
-
+        pushfq
+        push    rcx
+        push    r8
+        push    r9
+        push    r10
+        push    r11
+        sub     rsp, 30h                ; rsp is aligned at this point (7 pushes).
+                                        ; reserve 20h for spill, and 8 bytes for ts.
+
+        ; 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)
+        mov     rcx, [rdx]              ; Param 1 - The function address
         call    KPRF_ENTER
         jmp     common_return_path
@@ -115,20 +118,23 @@
 _pexit:
         ; save volatile register and get the time stamp.
-        push    eax
-        push    edx
+        push    rax
+        push    rdx
         rdtsc
-        pushfd
-        push    ecx
-
-        ; setting up the leave call frame (cdecl).
-        sub     esp, 4 + 4 + 8
-        mov     [esp + 0ch], edx        ; Param 3 - the timestamp
-        mov     [esp + 08h], eax
-        lea     edx, [esp + 24h]        ; Param 2 - frame pointer (pointer to the return address of the function calling us)
-        mov     [esp + 04h], edx
-        mov     eax, [esp + 20h]        ; Param 1 - Some address in the function.
-        sub     eax, 5                  ; call instruction
-        mov     [esp], eax
-
+        pushfq
+        push    rcx
+        push    r8
+        push    r9
+        push    r10
+        push    r11
+        sub     rsp, 30h                ; rsp is aligned at this point (7 pushes).
+                                        ; reserve 20h for spill, and 8 bytes for ts.
+
+        ; setting up the leave 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)
+        mov     rcx, [rdx]              ; Param 1 - The function address
         call    KPRF_LEAVE
         jmp common_return_path
@@ -144,30 +150,33 @@
         test    eax, eax
         jz      common_no_overhead
-        cmp     byte [g_fCalibrated], 0
+        cmp     byte [g_fCalibrated wrt rip], 0
         jnz     common_overhead
         call    calibrate
 common_overhead:
-        mov     ecx, eax                ; ecx <- pointer to overhead counter.
-        mov     eax, [g_OverheadAdj]    ; apply the adjustment before reading tsc
-        sub     [esp + 08h], eax
-        sbb     dword [esp + 0ch], 0
+        mov     rcx, rax                ; rcx <- pointer to overhead counter.
+        mov     eax, [g_OverheadAdj wrt rip]; apply the adjustment before reading tsc
+        sub     [rsp + 20h], rax
 
         rdtsc
-        sub     eax, [esp + 08h]
-        sbb     edx, [esp + 0ch]
-        add     [ecx], eax
-        adc     [ecx + 4], edx
+        shl     rdx, 32
+        or      rdx, rax                ; rdx = 64-bit timestamp
+        sub     rdx, [rsp + 20h]        ; rdx = elapsed
+        lock add [rcx], rdx             ; update counter.
 common_no_overhead:
-        add     esp, 4 + 4 + 8
 
         ; restore volatile registers.
-        pop     ecx
-        popfd
-        pop     edx
-        pop     eax
+        add     rsp, 30h
+        pop     r11
+        pop     r10
+        pop     r9
+        pop     r8
+        pop     rcx
+        popfq
+        pop     rdx
+        pop     rax
         ret
 
 ;;
-; Data esi points to while we're calibrating.
+; Data rsi points to while we're calibrating.
 struc CALIBDATA
     .OverheadLo resd 1
@@ -188,11 +197,27 @@
 ;
 calibrate:
-        ; prolog
-        push    ebp
-        mov     ebp, esp
-        pushfd
-        pushad
-        sub     esp, CALIBDATA_size
-        mov     esi, esp                ; esi points to the CALIBDATA
+        ; prolog - save everything
+        push    rbp
+        pushfq
+        push    rax                     ; pushaq
+        push    rbx
+        push    rcx
+        push    rdx
+        push    rdi
+        push    rsi
+        push    r8
+        push    r9
+        push    r10
+        push    r11
+        push    r12
+        push    r13
+        push    r14
+        push    r15
+        mov     rbp, rsp
+
+        sub     rsp, CALIBDATA_size
+        mov     rsi, rsp                ; rsi points to the CALIBDATA
+
+        and     rsp, -15
 
         ;
@@ -200,5 +225,5 @@
         ;
         mov     eax, 1
-        xchg    dword [g_fCalibrated], eax
+        xchg    dword [g_fCalibrated wrt rip], eax
 
         ;
@@ -213,52 +238,44 @@
         ;
         mov     ecx, 200h
-        mov     dword [esi + CALIBDATA.MinLo], 0ffffffffh
-        mov     dword [esi + CALIBDATA.MinHi], 07fffffffh
+        mov     dword [rsi + CALIBDATA.MinLo], 0ffffffffh
+        mov     dword [rsi + CALIBDATA.MinHi], 07fffffffh
 calib_inner_loop:
 
         ; zero the overhead and profiled times.
         xor     eax, eax
-        mov     [esi + CALIBDATA.OverheadLo], eax
-        mov     [esi + CALIBDATA.OverheadHi], eax
-        mov     [esi + CALIBDATA.ProfiledLo], eax
-        mov     [esi + CALIBDATA.ProfiledHi], eax
+        mov     [rsi + CALIBDATA.OverheadLo], rax
+        mov     [rsi + CALIBDATA.ProfiledLo], rax
         call    calib_nullproc
 
         ; subtract the overhead
-        mov     eax, [esi + CALIBDATA.ProfiledLo]
-        mov     edx, [esi + CALIBDATA.ProfiledHi]
-        sub     eax, [esi + CALIBDATA.OverheadLo]
-        sbb     edx, [esi + CALIBDATA.OverheadHi]
+        mov     rax, [rsi + CALIBDATA.ProfiledLo]
+        sub     rax, [rsi + CALIBDATA.OverheadLo]
 
         ; update the minimum value.
-        test    edx, 080000000h
-        jnz near calib_outer_dec        ; if negative, just simplify and shortcut
-        cmp     edx, [esi + CALIBDATA.MinHi]
-        jg      calib_inner_next
-        jl      calib_inner_update_minimum
-        cmp     eax, [esi + CALIBDATA.MinLo]
+        bt      rax, 63
+        jc near calib_outer_dec        ; if negative, just simplify and shortcut
+        cmp     rax, [rsi + CALIBDATA.MinHi]
         jge     calib_inner_next
 calib_inner_update_minimum:
-        mov     [esi + CALIBDATA.MinLo], eax
-        mov     [esi + CALIBDATA.MinHi], edx
+        mov     [rsi + CALIBDATA.MinLo], rax
 calib_inner_next:
         loop    calib_inner_loop
 
         ; Is the minimum value acceptable?
-        test    dword [esi + CALIBDATA.MinHi], 80000000h
+        test    dword [rsi + CALIBDATA.MinLo + 4], 80000000h
         jnz     calib_outer_dec         ; simplify if negative.
-        cmp     dword [esi + CALIBDATA.MinHi], 0
+        cmp     dword [rsi + CALIBDATA.MinHi + 4], 0
         jnz     calib_outer_inc         ; this shouldn't be possible
-        cmp     dword [esi + CALIBDATA.MinLo], 1fh
+        cmp     dword [rsi + CALIBDATA.MinLo], 1fh
         jbe     calib_outer_dec         ; too low - 2 ticks per pair is the minimum!
-        cmp     dword [esi + CALIBDATA.MinLo], 30h
+        cmp     dword [rsi + CALIBDATA.MinLo], 30h
         jbe     calib_done              ; this is fine!
 calib_outer_inc:
-        inc     dword [g_OverheadAdj]
+        inc     dword [g_OverheadAdj wrt rip]
         jmp     calib_outer_next
 calib_outer_dec:
-        cmp     dword [g_OverheadAdj], 1
+        cmp     dword [g_OverheadAdj wrt rip], 1
         je      calib_done
-        dec     dword [g_OverheadAdj]
+        dec     dword [g_OverheadAdj wrt rip]
 calib_outer_next:
         dec     ebx
@@ -266,9 +283,21 @@
 calib_done:
 
-        ; epilog
-        add     esp, CALIBDATA_size
-        popad
-        popfd
+        ; epilog - restore it all.
         leave
+        pop     r15
+        pop     r14
+        pop     r13
+        pop     r12
+        pop     r11
+        pop     r10
+        pop     r9
+        pop     r8
+        pop     rsi
+        pop     rdi
+        pop     rdx
+        pop     rcx
+        pop     rbx
+        pop     rax
+        popfq
         ret
 
@@ -280,22 +309,26 @@
 align 16
 calib_penter:
-        ; This part must be identical
-        push    eax
-        push    edx
+        ; This part must be identical past the rdtsc.
+        push    rax
+        push    rdx
         rdtsc
-        pushfd
-        push    ecx
-
-        ; store the entry
-        mov     [esi + CALIBDATA.EnterTSLo], eax
-        mov     [esi + CALIBDATA.EnterTSHi], edx
-
-        ; create the call frame
-        push    edx
-        push    eax
-        push    0
-        push    0
-
-        lea     eax, [esi + CALIBDATA.OverheadLo]
+        pushfq
+        push    rcx
+        push    r8
+        push    r9
+        push    r10
+        push    r11
+        sub     rsp, 30h                ; rsp is aligned at this point (7 pushes).
+                                        ; reserve 20h for spill, and 8 bytes for ts.
+
+        ; store the entry / stack frame.
+        mov     r8d, edx
+        shl     r8, 32
+        or      r8, rax
+        mov     [rsp + 20h], r8
+
+        mov     [rsi + CALIBDATA.EnterTSLo], r8
+
+        lea     rax, [rsi + CALIBDATA.OverheadLo]
         jmp     common_overhead
 
@@ -305,28 +338,27 @@
 align 16
 calib_pexit:
-        ; This part must be identical
-        push    eax
-        push    edx
+        ; This part must be identical past the rdtsc.
+        push    rax
+        push    rdx
         rdtsc
-        pushfd
-        push    ecx
-
-        ; update the time
-        push    eax
-        push    edx
-        sub     eax, [esi + CALIBDATA.EnterTSLo]
-        sbb     edx, [esi + CALIBDATA.EnterTSHi]
-        add     [esi + CALIBDATA.ProfiledLo], eax
-        adc     [esi + CALIBDATA.ProfiledHi], edx
-        pop     edx
-        pop     eax
-
-        ; create the call frame
-        push    edx
-        push    eax
-        push    0
-        push    0
-
-        lea     eax, [esi + CALIBDATA.EnterTSLo]
+        pushfq
+        push    rcx
+        push    r8
+        push    r9
+        push    r10
+        push    r11
+        sub     rsp, 30h                ; rsp is aligned at this point (7 pushes).
+                                        ; reserve 20h for spill, and 8 bytes for ts.
+
+        ; store the entry / stack frame.
+        mov     r8d, edx
+        shl     r8, 32
+        or      r8, rax
+        mov     [rsp + 20h], r8
+
+        sub     r8, [rsi + CALIBDATA.EnterTSLo]
+        add     [rsi + CALIBDATA.ProfiledLo], r8
+
+        lea     rax, [rsi + CALIBDATA.EnterTSLo]
         jmp     common_overhead
 
@@ -388,2 +420,9 @@
         ret
 
+
+;
+; Dummy stack check function.
+;
+global __chkstk
+__chkstk:
+    ret
