Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to emulate D language exception state save/restore on W64 context switch. #279

Draft
wants to merge 11 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 32 additions & 11 deletions src/asm/jump_x86_64_ms_pe_masm.asm
Original file line number Diff line number Diff line change
Expand Up @@ -33,52 +33,59 @@
; | SEE registers (XMM6-XMM15) |
; ----------------------------------------------------------------------------------
; ----------------------------------------------------------------------------------
; | 32 | 32 | 33 | 34 | 35 | 36 | 37 | 38 |
; | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
; ----------------------------------------------------------------------------------
; | 0x80 | 0x84 | 0x88 | 0x8c | 0x90 | 0x94 | 0x98 | 0x9c |
; ----------------------------------------------------------------------------------
; | SEE registers (XMM6-XMM15) |
; ----------------------------------------------------------------------------------
; ----------------------------------------------------------------------------------
; | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 |
; | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 |
; ----------------------------------------------------------------------------------
; | 0xa0 | 0xa4 | 0xa8 | 0xac | 0xb0 | 0xb4 | 0xb8 | 0xbc |
; ----------------------------------------------------------------------------------
; | fc_mxcsr|fc_x87_cw| <alignment> | fbr_strg | fc_dealloc |
; ----------------------------------------------------------------------------------
; ----------------------------------------------------------------------------------
; | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 |
; | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 |
; ----------------------------------------------------------------------------------
; | 0xc0 | 0xc4 | 0xc8 | 0xcc | 0xd0 | 0xd4 | 0xd8 | 0xdc |
; ----------------------------------------------------------------------------------
; | limit | base | R12 | R13 |
; ----------------------------------------------------------------------------------
; ----------------------------------------------------------------------------------
; | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 |
; | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 |
; ----------------------------------------------------------------------------------
; | 0xe0 | 0xe4 | 0xe8 | 0xec | 0xf0 | 0xf4 | 0xf8 | 0xfc |
; ----------------------------------------------------------------------------------
; | R14 | R15 | RDI | RSI |
; ----------------------------------------------------------------------------------
; ----------------------------------------------------------------------------------
; | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 |
; | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 |
; ----------------------------------------------------------------------------------
; | 0x100 | 0x104 | 0x108 | 0x10c | 0x110 | 0x114 | 0x118 | 0x11c |
; ----------------------------------------------------------------------------------
; | RBX | RBP | hidden | RIP |
; | RBX | RBP | hidden | padding |
; ----------------------------------------------------------------------------------
; ----------------------------------------------------------------------------------
; | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 |
; | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 |
; ----------------------------------------------------------------------------------
; | 0x120 | 0x124 | 0x128 | 0x12c | 0x130 | 0x134 | 0x138 | 0x13c |
; ----------------------------------------------------------------------------------
; | parameter area |
; | GS:[0] | GS:[8] | GS:[16] | RIP |
; ----------------------------------------------------------------------------------
; ----------------------------------------------------------------------------------
; | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 |
; | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 |
; ----------------------------------------------------------------------------------
; | 0x140 | 0x144 | 0x148 | 0x14c | 0x150 | 0x154 | 0x158 | 0x15c |
; ----------------------------------------------------------------------------------
; | parameter area |
; ----------------------------------------------------------------------------------
; ----------------------------------------------------------------------------------
; | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 |
; ----------------------------------------------------------------------------------
; | 0x160 | 0x164 | 0x168 | 0x16c | 0x170 | 0x174 | 0x178 | 0x17c |
; ----------------------------------------------------------------------------------
; | FCTX | DATA | |
; ----------------------------------------------------------------------------------

Expand All @@ -88,7 +95,7 @@ jump_fcontext PROC BOOST_CONTEXT_EXPORT FRAME
.endprolog

; prepare stack
lea rsp, [rsp-0118h]
lea rsp, [rsp-0138h]

IFNDEF BOOST_USE_TSX
; save XMM storage
Expand Down Expand Up @@ -134,6 +141,13 @@ ENDIF

mov [rsp+0110h], rcx ; save hidden address of transport_t

mov rax, gs:[0h] ; save exception block at gs:[0]
mov [rsp+0120h], rax
mov rax, gs:[8h]
mov [rsp+0128h], rax
mov rax, gs:[10h]
mov [rsp+0130h], rax

; preserve RSP (pointing to context-data) in R9
mov r9, rsp

Expand Down Expand Up @@ -182,10 +196,17 @@ ENDIF
mov rbx, [rsp+0100h] ; restore RBX
mov rbp, [rsp+0108h] ; restore RBP

mov rax, [rsp+0120h] ; restore exception block at gs:[0]
mov gs:[0h], rax
mov rax, [rsp+0128h]
mov gs:[8h], rax
mov rax, [rsp+0130h]
mov gs:[10h], rax

mov rax, [rsp+0110h] ; restore hidden address of transport_t

; prepare stack
lea rsp, [rsp+0118h]
lea rsp, [rsp+0138h]

; load return-address
pop r10
Expand Down
49 changes: 32 additions & 17 deletions src/asm/make_x86_64_ms_pe_masm.asm
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

; Copyright Oliver Kowalke 2009.
; Distributed under the Boost Software License, Version 1.0.
; (See accompanying file LICENSE_1_0.txt or copy at
Expand Down Expand Up @@ -33,52 +32,59 @@
; | SEE registers (XMM6-XMM15) |
; ----------------------------------------------------------------------------------
; ----------------------------------------------------------------------------------
; | 32 | 32 | 33 | 34 | 35 | 36 | 37 | 38 |
; | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
; ----------------------------------------------------------------------------------
; | 0x80 | 0x84 | 0x88 | 0x8c | 0x90 | 0x94 | 0x98 | 0x9c |
; ----------------------------------------------------------------------------------
; | SEE registers (XMM6-XMM15) |
; ----------------------------------------------------------------------------------
; ----------------------------------------------------------------------------------
; | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 |
; | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 |
; ----------------------------------------------------------------------------------
; | 0xa0 | 0xa4 | 0xa8 | 0xac | 0xb0 | 0xb4 | 0xb8 | 0xbc |
; ----------------------------------------------------------------------------------
; | fc_mxcsr|fc_x87_cw| <alignment> | fbr_strg | fc_dealloc |
; ----------------------------------------------------------------------------------
; ----------------------------------------------------------------------------------
; | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 |
; | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 |
; ----------------------------------------------------------------------------------
; | 0xc0 | 0xc4 | 0xc8 | 0xcc | 0xd0 | 0xd4 | 0xd8 | 0xdc |
; ----------------------------------------------------------------------------------
; | limit | base | R12 | R13 |
; ----------------------------------------------------------------------------------
; ----------------------------------------------------------------------------------
; | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 |
; | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 |
; ----------------------------------------------------------------------------------
; | 0xe0 | 0xe4 | 0xe8 | 0xec | 0xf0 | 0xf4 | 0xf8 | 0xfc |
; ----------------------------------------------------------------------------------
; | R14 | R15 | RDI | RSI |
; ----------------------------------------------------------------------------------
; ----------------------------------------------------------------------------------
; | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 |
; | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 |
; ----------------------------------------------------------------------------------
; | 0x100 | 0x104 | 0x108 | 0x10c | 0x110 | 0x114 | 0x118 | 0x11c |
; ----------------------------------------------------------------------------------
; | RBX | RBP | hidden | RIP |
; | RBX | RBP | hidden | padding |
; ----------------------------------------------------------------------------------
; ----------------------------------------------------------------------------------
; | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 |
; | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 |
; ----------------------------------------------------------------------------------
; | 0x120 | 0x124 | 0x128 | 0x12c | 0x130 | 0x134 | 0x138 | 0x13c |
; ----------------------------------------------------------------------------------
; | parameter area |
; | GS:[0] | GS:[8] | GS:[16] | RIP |
; ----------------------------------------------------------------------------------
; ----------------------------------------------------------------------------------
; | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 |
; | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 |
; ----------------------------------------------------------------------------------
; | 0x140 | 0x144 | 0x148 | 0x14c | 0x150 | 0x154 | 0x158 | 0x15c |
; ----------------------------------------------------------------------------------
; | parameter area |
; ----------------------------------------------------------------------------------
; ----------------------------------------------------------------------------------
; | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 |
; ----------------------------------------------------------------------------------
; | 0x160 | 0x164 | 0x168 | 0x16c | 0x170 | 0x174 | 0x178 | 0x17c |
; ----------------------------------------------------------------------------------
; | FCTX | DATA | |
; ----------------------------------------------------------------------------------

Expand All @@ -100,7 +106,7 @@ make_fcontext PROC BOOST_CONTEXT_EXPORT FRAME

; reserve space for context-data on context-stack
; on context-function entry: (RSP -0x8) % 16 == 0
sub rax, 0150h
sub rax, 0170h

; third arg of make_fcontext() == address of context-function
; stored in RBX
Expand All @@ -109,34 +115,43 @@ make_fcontext PROC BOOST_CONTEXT_EXPORT FRAME
; first arg of make_fcontext() == top of context-stack
; save top address of context stack as 'base'
mov [rax+0c8h], rcx
; also save high address in GS:[8]
mov [rax+0128h], rcx
; second arg of make_fcontext() == size of context-stack
; negate stack size for LEA instruction (== substraction)
neg rdx
; compute bottom address of context stack (limit)
lea rcx, [rcx+rdx]
; save bottom address of context stack as 'limit'
mov [rax+0c0h], rcx
; save address of context stack limit as 'dealloction stack'
; also save low address in GS:[16]
mov [rax+0130h], rcx
; save address of context stack limit as 'deallocation stack'
mov [rax+0b8h], rcx
; set fiber-storage to zero
xor rcx, rcx
; set fiber-storage to zero
xor rcx, rcx
mov [rax+0b0h], rcx

; save MMX control- and status-word
; set GS:[0] to end-pointer marker (ffff'ffff'ffff'ffffh) for exception
; handler chain
not rcx
mov [rax+0120h], rcx

; save MMX control- and status-word
stmxcsr [rax+0a0h]
; save x87 control-word
fnstcw [rax+0a4h]

; compute address of transport_t
lea rcx, [rax+0140h]
lea rcx, [rax+0160h]
; store address of transport_t in hidden field
mov [rax+0110h], rcx

; compute abs address of label trampoline
lea rcx, trampoline
; save address of trampoline as return-address for context-function
; will be entered after calling jump_fcontext() first time
mov [rax+0118h], rcx
mov [rax+0138h], rcx

; compute abs address of label finish
lea rcx, finish
Expand Down
Loading