anchor.diff:    ;; Full state was saved.
     (sys.lap-x86:gs)
     (sys.lap-x86:mov64 (:object nil #.+thread-full-save-p+) t)
  +  ;; Jump to common function.
  +  (sys.lap-x86:mov64 :r9 :r10) ; next-thread
  +  (sys.lap-x86:mov64 :r13 (:function %%switch-to-thread-common))
  +  (sys.lap-x86:jmp (:r13 #.(+ (- sys.int::+tag-object+) 8 (* sys.int::+fref-entry-point+ 8)))))
  +
  +;; (current-thread new-thread)
  +(sys.int::define-lap-function %%switch-to-thread-common ()
  +  ;; Old thread's state has been saved, restore the new-thread's state.
     ;; Switch threads.
     (sys.lap-x86:mov32 :ecx #.sys.int::+msr-ia32-gs-base+)
  -  (sys.lap-x86:mov64 :rax :r10)
  -  (sys.lap-x86:mov64 :rdx :r10)
  +  (sys.lap-x86:mov64 :rax :r9)
  +  (sys.lap-x86:mov64 :rdx :r9)
     (sys.lap-x86:shr64 :rdx 32)
     (sys.lap-x86:wrmsr)
     ;; Restore fpu state.
     (sys.lap-x86:gs)
     (sys.lap-x86:fxrstor (:object nil #.+thread-fx-save-area+))
     ;; Drop the locks on both threads. Must be done before touching the thread stack.
  -  (sys.lap-x86:mov64 :r11 (:constant :unlocked))
  -  (sys.lap-x86:mov64 (:object :r10 #.+thread-lock+) :r11)
  -  (sys.lap-x86:mov64 (:object :r8 #.+thread-lock+) :r11)
  +  (sys.lap-x86:mov64 :r10 (:constant :unlocked))
  +  (sys.lap-x86:mov64 (:object :r9 #.+thread-lock+) :r10)
  +  (sys.lap-x86:mov64 (:object :r8 #.+thread-lock+) :r10)
     ;; Check if the thread is in the interrupt save area.
     (sys.lap-x86:gs)
     (sys.lap-x86:cmp64 (:object nil #.+thread-full-save-p+) nil)
     (sys.lap-x86:jne FULL-RESTORE)
     ;; Restore stack pointer.
     (sys.lap-x86:gs)
  -  (sys.lap-x86:mov64 :rsp (:object nil #.+thread-stack-pointer+))
  +  (sys.lap-x86:mov64 :rsp (:object nil #.+thread-state-rsp+))
     ;; Restore frame pointer.
     (sys.lap-x86:gs)
  -  (sys.lap-x86:mov64 :rbp (:object nil #.+thread-frame-pointer+))
  +  (sys.lap-x86:mov64 :rbp (:object nil #.+thread-state-rbp+))
     ;; Reenable interrupts. Must be done before touching the thread stack.
     (sys.lap-x86:sti)
  +  (:gc :no-frame)
     ;; No value return.
     (sys.lap-x86:xor32 :ecx :ecx)
     (sys.lap-x86:mov64 :r8 nil)
  
positive.code:   (sys.lap-x86:xor32 :ecx :ecx)
    (sys.lap-x86:mov64 :r8 nil)
    (sys.lap-x86:sti)
    (sys.lap-x86:ret))
  
  (sys.int::define-lap-function %%switch-to-thread-via-wired-stack ()
    (:gc :no-frame)
    ;; Save frame pointer.
    (sys.lap-x86:gs)
    (sys.lap-x86:mov64 (:object nil #.+thread-frame-pointer+) :r10)
    ;; Save fpu state.
    (sys.lap-x86:gs)
    (sys.lap-x86:fxsave (:object nil #.+thread-fx-save-area+))
    ;; Save stack pointer.
    (sys.lap-x86:gs)
    (sys.lap-x86:mov64 (:object nil #.+thread-stack-pointer+) :r9)
    ;; Only partial state was saved.
    (sys.lap-x86:gs)
    (sys.lap-x86:mov64 (:object nil #.+thread-full-save-p+) nil)
    ;; Switch threads.
    (sys.lap-x86:mov32 :ecx #.sys.int::+msr-ia32-gs-base+)
    (sys.lap-x86:mov64 :rax :r11)
    (sys.lap-x86:mov64 :rdx :r11)
    (sys.lap-x86:shr64 :rdx 32)
    (sys.lap-x86:wrmsr)
    ;; Restore fpu state.
    (sys.lap-x86:gs)
    (sys.lap-x86:fxrstor (:object nil #.+thread-fx-save-area+))
    ;; Drop the locks on both threads. Must be done before touching the thread stack.
    (sys.lap-x86:mov64 :r12 (:constant :unlocked))
    (sys.lap-x86:mov64 (:object :r11 #.+thread-lock+) :r12)
    (sys.lap-x86:mov64 (:object :r8 #.+thread-lock+) :r12)
    ;; Check if the thread is in the interrupt save area.
    (sys.lap-x86:gs)
    (sys.lap-x86:cmp64 (:object nil #.+thread-full-save-p+) nil)
    (sys.lap-x86:jne FULL-RESTORE)
    ;; Restore stack pointer.
    (sys.lap-x86:gs)
    (sys.lap-x86:mov64 :rsp (:object nil #.+thread-stack-pointer+))
    ;; Restore frame pointer.
    (sys.lap-x86:gs)
    (sys.lap-x86:mov64 :rbp (:object nil #.+thread-frame-pointer+))
    ;; Reenable interrupts. Must be done before touching the thread stack.
    (sys.lap-x86:sti)
    ;; No value return.
    (sys.lap-x86:xor32 :ecx :ecx)
    (sys.lap-x86:mov64 :r8 nil)
    ;; Return, restoring RIP.
    (sys.lap-x86:ret)
    ;; Returning to an interrupted thread. Restore saved registers and stuff.
    FULL-RESTORE
    (sys.lap-x86:lea64 :rsp (:object :r11 #.+thread-interrupt-save-area+))
    (sys.lap-x86:pop :r15)
    (sys.lap-x86:pop :r14)
    (sys.lap-x86:pop :r13)
    (sys.lap-x86:pop :r12)
    (sys.lap-x86:pop :r11)
    (sys.lap-x86:pop :r10)
    (sys.lap-x86:pop :r9)
    (sys.lap-x86:pop :r8)
    (sys.lap-x86:pop :rdi)
    (sys.lap-x86:pop :rsi)
    (sys.lap-x86:pop :rbx)
    (sys.lap-x86:pop :rdx)
    (sys.lap-x86:pop :rcx)
    (sys.lap-x86:pop :rax)
    (sys.lap-x86:pop :rbp)
    (sys.lap-x86:iret))
  
  (defun %reschedule ()
    ;; Interrupts must be off and the current thread's lock must be held.
  
negative.code:   (sys.lap-x86:ret)
    ;; Returning to an interrupted thread. Restore saved registers and stuff.
    FULL-RESTORE
    (sys.lap-x86:lea64 :rsp (:object :r9 #.+thread-interrupt-save-area+))
    (sys.lap-x86:pop :r15)
    (sys.lap-x86:pop :r14)
    (sys.lap-x86:pop :r13)
  