From 27e94964d345999a01021d346833a98ed2a19572 Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Wed, 1 Jan 2025 09:16:39 -0500 Subject: [PATCH 01/10] Emulating D language fiber-specific exceptions, step 0 Reserve extra space on the fiber stack for saving/restoring the three 64-bit pointers at GS:[0]; initialize the new slots in each new fiber stack; but do not yet attempt to save or restore the special GS:[0] block. --- src/asm/jump_x86_64_ms_pe_masm.asm | 27 ++++++++++++------- src/asm/make_x86_64_ms_pe_masm.asm | 41 ++++++++++++++++++++--------- src/asm/ontop_x86_64_ms_pe_masm.asm | 27 ++++++++++++------- 3 files changed, 62 insertions(+), 33 deletions(-) diff --git a/src/asm/jump_x86_64_ms_pe_masm.asm b/src/asm/jump_x86_64_ms_pe_masm.asm index c8a28a55..d6fb539d 100644 --- a/src/asm/jump_x86_64_ms_pe_masm.asm +++ b/src/asm/jump_x86_64_ms_pe_masm.asm @@ -33,53 +33,60 @@ ; | 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| | 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 | ; ---------------------------------------------------------------------------------- ; ---------------------------------------------------------------------------------- -; | 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 | ; ---------------------------------------------------------------------------------- ; ---------------------------------------------------------------------------------- -; | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | +; | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | ; ---------------------------------------------------------------------------------- ; | 0x140 | 0x144 | 0x148 | 0x14c | 0x150 | 0x154 | 0x158 | 0x15c | ; ---------------------------------------------------------------------------------- -; | FCTX | DATA | | +; | FCTX | DATA | GS:[0] | GS:[8] | +; ---------------------------------------------------------------------------------- +; ---------------------------------------------------------------------------------- +; | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | +; ---------------------------------------------------------------------------------- +; | 0x160 | 0x164 | 0x168 | 0x16c | 0x170 | 0x174 | 0x178 | 0x17c | +; ---------------------------------------------------------------------------------- +; | GS:[16] | | ; ---------------------------------------------------------------------------------- .code @@ -88,7 +95,7 @@ jump_fcontext PROC BOOST_CONTEXT_EXPORT FRAME .endprolog ; prepare stack - lea rsp, [rsp-0118h] + lea rsp, [rsp-0130h] IFNDEF BOOST_USE_TSX ; save XMM storage @@ -185,7 +192,7 @@ ENDIF mov rax, [rsp+0110h] ; restore hidden address of transport_t ; prepare stack - lea rsp, [rsp+0118h] + lea rsp, [rsp+0130h] ; load return-address pop r10 diff --git a/src/asm/make_x86_64_ms_pe_masm.asm b/src/asm/make_x86_64_ms_pe_masm.asm index 8f6c959a..394205a2 100644 --- a/src/asm/make_x86_64_ms_pe_masm.asm +++ b/src/asm/make_x86_64_ms_pe_masm.asm @@ -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 @@ -33,53 +32,60 @@ ; | 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| | 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 | ; ---------------------------------------------------------------------------------- ; ---------------------------------------------------------------------------------- -; | 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 | ; ---------------------------------------------------------------------------------- ; ---------------------------------------------------------------------------------- -; | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | +; | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | ; ---------------------------------------------------------------------------------- ; | 0x140 | 0x144 | 0x148 | 0x14c | 0x150 | 0x154 | 0x158 | 0x15c | ; ---------------------------------------------------------------------------------- -; | FCTX | DATA | | +; | FCTX | DATA | GS:[0] | GS:[8] | +; ---------------------------------------------------------------------------------- +; ---------------------------------------------------------------------------------- +; | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | +; ---------------------------------------------------------------------------------- +; | 0x160 | 0x164 | 0x168 | 0x16c | 0x170 | 0x174 | 0x178 | 0x17c | +; ---------------------------------------------------------------------------------- +; | GS:[16] | | ; ---------------------------------------------------------------------------------- ; standard C library function @@ -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, 0168h ; third arg of make_fcontext() == address of context-function ; stored in RBX @@ -109,6 +115,8 @@ 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+0158h], rcx ; second arg of make_fcontext() == size of context-stack ; negate stack size for LEA instruction (== substraction) neg rdx @@ -116,13 +124,20 @@ make_fcontext PROC BOOST_CONTEXT_EXPORT FRAME lea rcx, [rcx+rdx] ; save bottom address of context stack as 'limit' mov [rax+0c0h], rcx + ; also save low address in GS:[16] + mov [rax+0160h], rcx ; save address of context stack limit as 'dealloction 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+0150h], rcx + + ; save MMX control- and status-word stmxcsr [rax+0a0h] ; save x87 control-word fnstcw [rax+0a4h] diff --git a/src/asm/ontop_x86_64_ms_pe_masm.asm b/src/asm/ontop_x86_64_ms_pe_masm.asm index b57dd158..1bdaebb5 100644 --- a/src/asm/ontop_x86_64_ms_pe_masm.asm +++ b/src/asm/ontop_x86_64_ms_pe_masm.asm @@ -33,53 +33,60 @@ ; | 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| | 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 | ; ---------------------------------------------------------------------------------- ; ---------------------------------------------------------------------------------- -; | 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 | ; ---------------------------------------------------------------------------------- ; ---------------------------------------------------------------------------------- -; | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | +; | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | ; ---------------------------------------------------------------------------------- ; | 0x140 | 0x144 | 0x148 | 0x14c | 0x150 | 0x154 | 0x158 | 0x15c | ; ---------------------------------------------------------------------------------- -; | FCTX | DATA | | +; | FCTX | DATA | GS:[0] | GS:[8] | +; ---------------------------------------------------------------------------------- +; ---------------------------------------------------------------------------------- +; | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | +; ---------------------------------------------------------------------------------- +; | 0x160 | 0x164 | 0x168 | 0x16c | 0x170 | 0x174 | 0x178 | 0x17c | +; ---------------------------------------------------------------------------------- +; | GS:[16] | | ; ---------------------------------------------------------------------------------- .code @@ -88,7 +95,7 @@ ontop_fcontext PROC BOOST_CONTEXT_EXPORT FRAME .endprolog ; prepare stack - lea rsp, [rsp-0118h] + lea rsp, [rsp-0130h] IFNDEF BOOST_USE_TSX ; save XMM storage @@ -185,7 +192,7 @@ ENDIF mov rax, [rsp+0110h] ; restore hidden address of transport_t ; prepare stack - lea rsp, [rsp+0118h] + lea rsp, [rsp+0130h] ; keep return-address on stack From b5a1a035f9c06564654164852236605c2e4d9d01 Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Wed, 1 Jan 2025 11:07:17 -0500 Subject: [PATCH 02/10] Restore code to 1bde50e, with the only changes being comments. --- src/asm/jump_x86_64_ms_pe_masm.asm | 6 ++++-- src/asm/make_x86_64_ms_pe_masm.asm | 19 ++++++++++--------- src/asm/ontop_x86_64_ms_pe_masm.asm | 6 ++++-- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/asm/jump_x86_64_ms_pe_masm.asm b/src/asm/jump_x86_64_ms_pe_masm.asm index d6fb539d..294b9249 100644 --- a/src/asm/jump_x86_64_ms_pe_masm.asm +++ b/src/asm/jump_x86_64_ms_pe_masm.asm @@ -95,7 +95,8 @@ jump_fcontext PROC BOOST_CONTEXT_EXPORT FRAME .endprolog ; prepare stack - lea rsp, [rsp-0130h] + lea rsp, [rsp-0118h] +;; lea rsp, [rsp-0130h] IFNDEF BOOST_USE_TSX ; save XMM storage @@ -192,7 +193,8 @@ ENDIF mov rax, [rsp+0110h] ; restore hidden address of transport_t ; prepare stack - lea rsp, [rsp+0130h] + lea rsp, [rsp+0118h] +;; lea rsp, [rsp+0130h] ; load return-address pop r10 diff --git a/src/asm/make_x86_64_ms_pe_masm.asm b/src/asm/make_x86_64_ms_pe_masm.asm index 394205a2..75f528c6 100644 --- a/src/asm/make_x86_64_ms_pe_masm.asm +++ b/src/asm/make_x86_64_ms_pe_masm.asm @@ -106,7 +106,8 @@ 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, 0168h + sub rax, 0150h +;; sub rax, 0168h ; third arg of make_fcontext() == address of context-function ; stored in RBX @@ -115,8 +116,8 @@ 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+0158h], rcx +;; ; also save high address in GS:[8] +;; mov [rax+0158h], rcx ; second arg of make_fcontext() == size of context-stack ; negate stack size for LEA instruction (== substraction) neg rdx @@ -124,18 +125,18 @@ make_fcontext PROC BOOST_CONTEXT_EXPORT FRAME lea rcx, [rcx+rdx] ; save bottom address of context stack as 'limit' mov [rax+0c0h], rcx - ; also save low address in GS:[16] - mov [rax+0160h], rcx +;; ; also save low address in GS:[16] +;; mov [rax+0160h], rcx ; save address of context stack limit as 'dealloction stack' mov [rax+0b8h], rcx ; set fiber-storage to zero xor rcx, rcx mov [rax+0b0h], rcx - ; set GS:[0] to end-pointer marker (ffff'ffff'ffff'ffffh) for exception - ; handler chain - not rcx - mov [rax+0150h], rcx +;; ; set GS:[0] to end-pointer marker (ffff'ffff'ffff'ffffh) for exception +;; ; handler chain +;; not rcx +;; mov [rax+0150h], rcx ; save MMX control- and status-word stmxcsr [rax+0a0h] diff --git a/src/asm/ontop_x86_64_ms_pe_masm.asm b/src/asm/ontop_x86_64_ms_pe_masm.asm index 1bdaebb5..ad832d94 100644 --- a/src/asm/ontop_x86_64_ms_pe_masm.asm +++ b/src/asm/ontop_x86_64_ms_pe_masm.asm @@ -95,7 +95,8 @@ ontop_fcontext PROC BOOST_CONTEXT_EXPORT FRAME .endprolog ; prepare stack - lea rsp, [rsp-0130h] + lea rsp, [rsp-0118h] +;; lea rsp, [rsp-0130h] IFNDEF BOOST_USE_TSX ; save XMM storage @@ -192,7 +193,8 @@ ENDIF mov rax, [rsp+0110h] ; restore hidden address of transport_t ; prepare stack - lea rsp, [rsp+0130h] + lea rsp, [rsp+0118h] +;; lea rsp, [rsp+0130h] ; keep return-address on stack From 9cc89ec0867a233654d51149c78caffa5478eb93 Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Wed, 1 Jan 2025 11:11:50 -0500 Subject: [PATCH 03/10] Add comment blocks for saving and restoring 3 ptrs at GS:[0]. --- src/asm/jump_x86_64_ms_pe_masm.asm | 14 ++++++++++++++ src/asm/ontop_x86_64_ms_pe_masm.asm | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/asm/jump_x86_64_ms_pe_masm.asm b/src/asm/jump_x86_64_ms_pe_masm.asm index 294b9249..83620a1d 100644 --- a/src/asm/jump_x86_64_ms_pe_masm.asm +++ b/src/asm/jump_x86_64_ms_pe_masm.asm @@ -142,6 +142,13 @@ ENDIF mov [rsp+0110h], rcx ; save hidden address of transport_t +;; mov rax, gs:[0h] ; save exception block at gs:[0] +;; mov [rsp+0150h], rax +;; mov rax, gs:[8h] +;; mov [rsp+0158h], rax +;; mov rax, gs:[10h] +;; mov [rsp+0160h], rax + ; preserve RSP (pointing to context-data) in R9 mov r9, rsp @@ -190,6 +197,13 @@ ENDIF mov rbx, [rsp+0100h] ; restore RBX mov rbp, [rsp+0108h] ; restore RBP +;; mov rax, [rsp+0150h] ; restore exception block at gs:[0] +;; mov gs:[0h], rax +;; mov rax, [rsp+0158h] +;; mov gs:[8h], rax +;; mov rax, [rsp+0160h] +;; mov gs:[10h], rax + mov rax, [rsp+0110h] ; restore hidden address of transport_t ; prepare stack diff --git a/src/asm/ontop_x86_64_ms_pe_masm.asm b/src/asm/ontop_x86_64_ms_pe_masm.asm index ad832d94..abe35139 100644 --- a/src/asm/ontop_x86_64_ms_pe_masm.asm +++ b/src/asm/ontop_x86_64_ms_pe_masm.asm @@ -142,6 +142,13 @@ ENDIF mov [rsp+0110h], rcx ; save hidden address of transport_t +;; mov rax, gs:[0h] ; save exception block at gs:[0] +;; mov [rsp+0150h], rax +;; mov rax, gs:[8h] +;; mov [rsp+0158h], rax +;; mov rax, gs:[10h] +;; mov [rsp+0160h], rax + ; preserve RSP (pointing to context-data) in RCX mov rcx, rsp @@ -190,6 +197,13 @@ ENDIF mov rbx, [rsp+0100h] ; restore RBX mov rbp, [rsp+0108h] ; restore RBP +;; mov rax, [rsp+0150h] ; restore exception block at gs:[0] +;; mov gs:[0h], rax +;; mov rax, [rsp+0158h] +;; mov gs:[8h], rax +;; mov rax, [rsp+0160h] +;; mov gs:[10h], rax + mov rax, [rsp+0110h] ; restore hidden address of transport_t ; prepare stack From 07d0ff83472efa7da52976d6fbae3e05a53c4a8a Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Wed, 1 Jan 2025 11:23:49 -0500 Subject: [PATCH 04/10] Reserve more space on the stack to save state for context switch. --- src/asm/jump_x86_64_ms_pe_masm.asm | 27 +++++++++++++++++---------- src/asm/make_x86_64_ms_pe_masm.asm | 25 ++++++++++++++++--------- src/asm/ontop_x86_64_ms_pe_masm.asm | 27 +++++++++++++++++---------- 3 files changed, 50 insertions(+), 29 deletions(-) diff --git a/src/asm/jump_x86_64_ms_pe_masm.asm b/src/asm/jump_x86_64_ms_pe_masm.asm index c8a28a55..d6fb539d 100644 --- a/src/asm/jump_x86_64_ms_pe_masm.asm +++ b/src/asm/jump_x86_64_ms_pe_masm.asm @@ -33,53 +33,60 @@ ; | 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| | 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 | ; ---------------------------------------------------------------------------------- ; ---------------------------------------------------------------------------------- -; | 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 | ; ---------------------------------------------------------------------------------- ; ---------------------------------------------------------------------------------- -; | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | +; | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | ; ---------------------------------------------------------------------------------- ; | 0x140 | 0x144 | 0x148 | 0x14c | 0x150 | 0x154 | 0x158 | 0x15c | ; ---------------------------------------------------------------------------------- -; | FCTX | DATA | | +; | FCTX | DATA | GS:[0] | GS:[8] | +; ---------------------------------------------------------------------------------- +; ---------------------------------------------------------------------------------- +; | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | +; ---------------------------------------------------------------------------------- +; | 0x160 | 0x164 | 0x168 | 0x16c | 0x170 | 0x174 | 0x178 | 0x17c | +; ---------------------------------------------------------------------------------- +; | GS:[16] | | ; ---------------------------------------------------------------------------------- .code @@ -88,7 +95,7 @@ jump_fcontext PROC BOOST_CONTEXT_EXPORT FRAME .endprolog ; prepare stack - lea rsp, [rsp-0118h] + lea rsp, [rsp-0130h] IFNDEF BOOST_USE_TSX ; save XMM storage @@ -185,7 +192,7 @@ ENDIF mov rax, [rsp+0110h] ; restore hidden address of transport_t ; prepare stack - lea rsp, [rsp+0118h] + lea rsp, [rsp+0130h] ; load return-address pop r10 diff --git a/src/asm/make_x86_64_ms_pe_masm.asm b/src/asm/make_x86_64_ms_pe_masm.asm index 8f6c959a..728018c3 100644 --- a/src/asm/make_x86_64_ms_pe_masm.asm +++ b/src/asm/make_x86_64_ms_pe_masm.asm @@ -33,53 +33,60 @@ ; | 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| | 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 | ; ---------------------------------------------------------------------------------- ; ---------------------------------------------------------------------------------- -; | 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 | ; ---------------------------------------------------------------------------------- ; ---------------------------------------------------------------------------------- -; | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | +; | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | ; ---------------------------------------------------------------------------------- ; | 0x140 | 0x144 | 0x148 | 0x14c | 0x150 | 0x154 | 0x158 | 0x15c | ; ---------------------------------------------------------------------------------- -; | FCTX | DATA | | +; | FCTX | DATA | GS:[0] | GS:[8] | +; ---------------------------------------------------------------------------------- +; ---------------------------------------------------------------------------------- +; | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | +; ---------------------------------------------------------------------------------- +; | 0x160 | 0x164 | 0x168 | 0x16c | 0x170 | 0x174 | 0x178 | 0x17c | +; ---------------------------------------------------------------------------------- +; | GS:[16] | | ; ---------------------------------------------------------------------------------- ; standard C library function @@ -100,7 +107,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, 0168h ; third arg of make_fcontext() == address of context-function ; stored in RBX diff --git a/src/asm/ontop_x86_64_ms_pe_masm.asm b/src/asm/ontop_x86_64_ms_pe_masm.asm index b57dd158..1bdaebb5 100644 --- a/src/asm/ontop_x86_64_ms_pe_masm.asm +++ b/src/asm/ontop_x86_64_ms_pe_masm.asm @@ -33,53 +33,60 @@ ; | 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| | 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 | ; ---------------------------------------------------------------------------------- ; ---------------------------------------------------------------------------------- -; | 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 | ; ---------------------------------------------------------------------------------- ; ---------------------------------------------------------------------------------- -; | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | +; | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | ; ---------------------------------------------------------------------------------- ; | 0x140 | 0x144 | 0x148 | 0x14c | 0x150 | 0x154 | 0x158 | 0x15c | ; ---------------------------------------------------------------------------------- -; | FCTX | DATA | | +; | FCTX | DATA | GS:[0] | GS:[8] | +; ---------------------------------------------------------------------------------- +; ---------------------------------------------------------------------------------- +; | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | +; ---------------------------------------------------------------------------------- +; | 0x160 | 0x164 | 0x168 | 0x16c | 0x170 | 0x174 | 0x178 | 0x17c | +; ---------------------------------------------------------------------------------- +; | GS:[16] | | ; ---------------------------------------------------------------------------------- .code @@ -88,7 +95,7 @@ ontop_fcontext PROC BOOST_CONTEXT_EXPORT FRAME .endprolog ; prepare stack - lea rsp, [rsp-0118h] + lea rsp, [rsp-0130h] IFNDEF BOOST_USE_TSX ; save XMM storage @@ -185,7 +192,7 @@ ENDIF mov rax, [rsp+0110h] ; restore hidden address of transport_t ; prepare stack - lea rsp, [rsp+0118h] + lea rsp, [rsp+0130h] ; keep return-address on stack From 7647c2aab7c219c88e2279360fb8dd83dc3dda5e Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Wed, 1 Jan 2025 12:02:48 -0500 Subject: [PATCH 05/10] Try adjusting fiber context state data by 32 bytes, not 24. Unfortunately this also produces immediate segfaults. --- src/asm/jump_x86_64_ms_pe_masm.asm | 4 ++-- src/asm/make_x86_64_ms_pe_masm.asm | 2 +- src/asm/ontop_x86_64_ms_pe_masm.asm | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/asm/jump_x86_64_ms_pe_masm.asm b/src/asm/jump_x86_64_ms_pe_masm.asm index d6fb539d..817fd3aa 100644 --- a/src/asm/jump_x86_64_ms_pe_masm.asm +++ b/src/asm/jump_x86_64_ms_pe_masm.asm @@ -95,7 +95,7 @@ jump_fcontext PROC BOOST_CONTEXT_EXPORT FRAME .endprolog ; prepare stack - lea rsp, [rsp-0130h] + lea rsp, [rsp-0138h] IFNDEF BOOST_USE_TSX ; save XMM storage @@ -192,7 +192,7 @@ ENDIF mov rax, [rsp+0110h] ; restore hidden address of transport_t ; prepare stack - lea rsp, [rsp+0130h] + lea rsp, [rsp+0138h] ; load return-address pop r10 diff --git a/src/asm/make_x86_64_ms_pe_masm.asm b/src/asm/make_x86_64_ms_pe_masm.asm index 728018c3..7c42379e 100644 --- a/src/asm/make_x86_64_ms_pe_masm.asm +++ b/src/asm/make_x86_64_ms_pe_masm.asm @@ -107,7 +107,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, 0168h + sub rax, 0170h ; third arg of make_fcontext() == address of context-function ; stored in RBX diff --git a/src/asm/ontop_x86_64_ms_pe_masm.asm b/src/asm/ontop_x86_64_ms_pe_masm.asm index 1bdaebb5..31f535b6 100644 --- a/src/asm/ontop_x86_64_ms_pe_masm.asm +++ b/src/asm/ontop_x86_64_ms_pe_masm.asm @@ -95,7 +95,7 @@ ontop_fcontext PROC BOOST_CONTEXT_EXPORT FRAME .endprolog ; prepare stack - lea rsp, [rsp-0130h] + lea rsp, [rsp-0138h] IFNDEF BOOST_USE_TSX ; save XMM storage @@ -192,7 +192,7 @@ ENDIF mov rax, [rsp+0110h] ; restore hidden address of transport_t ; prepare stack - lea rsp, [rsp+0130h] + lea rsp, [rsp+0138h] ; keep return-address on stack From 14467189e238b2638e5fcc8a1af5d9c659acbb18 Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Wed, 1 Jan 2025 15:13:23 -0500 Subject: [PATCH 06/10] Revert "Try adjusting fiber context state data by 32 bytes, not 24." This reverts commit 7647c2aab7c219c88e2279360fb8dd83dc3dda5e. --- src/asm/jump_x86_64_ms_pe_masm.asm | 4 ++-- src/asm/make_x86_64_ms_pe_masm.asm | 2 +- src/asm/ontop_x86_64_ms_pe_masm.asm | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/asm/jump_x86_64_ms_pe_masm.asm b/src/asm/jump_x86_64_ms_pe_masm.asm index 817fd3aa..d6fb539d 100644 --- a/src/asm/jump_x86_64_ms_pe_masm.asm +++ b/src/asm/jump_x86_64_ms_pe_masm.asm @@ -95,7 +95,7 @@ jump_fcontext PROC BOOST_CONTEXT_EXPORT FRAME .endprolog ; prepare stack - lea rsp, [rsp-0138h] + lea rsp, [rsp-0130h] IFNDEF BOOST_USE_TSX ; save XMM storage @@ -192,7 +192,7 @@ ENDIF mov rax, [rsp+0110h] ; restore hidden address of transport_t ; prepare stack - lea rsp, [rsp+0138h] + lea rsp, [rsp+0130h] ; load return-address pop r10 diff --git a/src/asm/make_x86_64_ms_pe_masm.asm b/src/asm/make_x86_64_ms_pe_masm.asm index 7c42379e..728018c3 100644 --- a/src/asm/make_x86_64_ms_pe_masm.asm +++ b/src/asm/make_x86_64_ms_pe_masm.asm @@ -107,7 +107,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, 0170h + sub rax, 0168h ; third arg of make_fcontext() == address of context-function ; stored in RBX diff --git a/src/asm/ontop_x86_64_ms_pe_masm.asm b/src/asm/ontop_x86_64_ms_pe_masm.asm index 31f535b6..1bdaebb5 100644 --- a/src/asm/ontop_x86_64_ms_pe_masm.asm +++ b/src/asm/ontop_x86_64_ms_pe_masm.asm @@ -95,7 +95,7 @@ ontop_fcontext PROC BOOST_CONTEXT_EXPORT FRAME .endprolog ; prepare stack - lea rsp, [rsp-0138h] + lea rsp, [rsp-0130h] IFNDEF BOOST_USE_TSX ; save XMM storage @@ -192,7 +192,7 @@ ENDIF mov rax, [rsp+0110h] ; restore hidden address of transport_t ; prepare stack - lea rsp, [rsp+0138h] + lea rsp, [rsp+0130h] ; keep return-address on stack From 9fff6e2d0aba689b8a6c22a8fe265afde11f360f Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Wed, 1 Jan 2025 17:00:29 -0500 Subject: [PATCH 07/10] This runs like unmodified Boost.Context, with extra space reserved. --- src/asm/jump_x86_64_ms_pe_masm.asm | 12 ++++++------ src/asm/make_x86_64_ms_pe_masm.asm | 16 ++++++++-------- src/asm/ontop_x86_64_ms_pe_masm.asm | 12 ++++++------ 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/asm/jump_x86_64_ms_pe_masm.asm b/src/asm/jump_x86_64_ms_pe_masm.asm index d6fb539d..db4dff87 100644 --- a/src/asm/jump_x86_64_ms_pe_masm.asm +++ b/src/asm/jump_x86_64_ms_pe_masm.asm @@ -65,28 +65,28 @@ ; ---------------------------------------------------------------------------------- ; | 0x100 | 0x104 | 0x108 | 0x10c | 0x110 | 0x114 | 0x118 | 0x11c | ; ---------------------------------------------------------------------------------- -; | RBX | RBP | hidden | RIP | +; | RBX | RBP | hidden | padding | ; ---------------------------------------------------------------------------------- ; ---------------------------------------------------------------------------------- ; | 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 | ; ---------------------------------------------------------------------------------- ; ---------------------------------------------------------------------------------- ; | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | ; ---------------------------------------------------------------------------------- ; | 0x140 | 0x144 | 0x148 | 0x14c | 0x150 | 0x154 | 0x158 | 0x15c | ; ---------------------------------------------------------------------------------- -; | FCTX | DATA | GS:[0] | GS:[8] | +; | parameter area | ; ---------------------------------------------------------------------------------- ; ---------------------------------------------------------------------------------- ; | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | ; ---------------------------------------------------------------------------------- ; | 0x160 | 0x164 | 0x168 | 0x16c | 0x170 | 0x174 | 0x178 | 0x17c | ; ---------------------------------------------------------------------------------- -; | GS:[16] | | +; | FCTX | DATA | | ; ---------------------------------------------------------------------------------- .code @@ -95,7 +95,7 @@ jump_fcontext PROC BOOST_CONTEXT_EXPORT FRAME .endprolog ; prepare stack - lea rsp, [rsp-0130h] + lea rsp, [rsp-0138h] IFNDEF BOOST_USE_TSX ; save XMM storage @@ -192,7 +192,7 @@ ENDIF mov rax, [rsp+0110h] ; restore hidden address of transport_t ; prepare stack - lea rsp, [rsp+0130h] + lea rsp, [rsp+0138h] ; load return-address pop r10 diff --git a/src/asm/make_x86_64_ms_pe_masm.asm b/src/asm/make_x86_64_ms_pe_masm.asm index 728018c3..d3bf29f8 100644 --- a/src/asm/make_x86_64_ms_pe_masm.asm +++ b/src/asm/make_x86_64_ms_pe_masm.asm @@ -65,28 +65,28 @@ ; ---------------------------------------------------------------------------------- ; | 0x100 | 0x104 | 0x108 | 0x10c | 0x110 | 0x114 | 0x118 | 0x11c | ; ---------------------------------------------------------------------------------- -; | RBX | RBP | hidden | RIP | +; | RBX | RBP | hidden | padding | ; ---------------------------------------------------------------------------------- ; ---------------------------------------------------------------------------------- ; | 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 | ; ---------------------------------------------------------------------------------- ; ---------------------------------------------------------------------------------- ; | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | ; ---------------------------------------------------------------------------------- ; | 0x140 | 0x144 | 0x148 | 0x14c | 0x150 | 0x154 | 0x158 | 0x15c | ; ---------------------------------------------------------------------------------- -; | FCTX | DATA | GS:[0] | GS:[8] | +; | parameter area | ; ---------------------------------------------------------------------------------- ; ---------------------------------------------------------------------------------- ; | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | ; ---------------------------------------------------------------------------------- ; | 0x160 | 0x164 | 0x168 | 0x16c | 0x170 | 0x174 | 0x178 | 0x17c | ; ---------------------------------------------------------------------------------- -; | GS:[16] | | +; | FCTX | DATA | | ; ---------------------------------------------------------------------------------- ; standard C library function @@ -107,7 +107,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, 0168h + sub rax, 0170h ; third arg of make_fcontext() == address of context-function ; stored in RBX @@ -123,7 +123,7 @@ make_fcontext PROC BOOST_CONTEXT_EXPORT FRAME 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' + ; save address of context stack limit as 'deallocation stack' mov [rax+0b8h], rcx ; set fiber-storage to zero xor rcx, rcx @@ -135,7 +135,7 @@ make_fcontext PROC BOOST_CONTEXT_EXPORT FRAME 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 @@ -143,7 +143,7 @@ make_fcontext PROC BOOST_CONTEXT_EXPORT FRAME 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 diff --git a/src/asm/ontop_x86_64_ms_pe_masm.asm b/src/asm/ontop_x86_64_ms_pe_masm.asm index 1bdaebb5..866d471c 100644 --- a/src/asm/ontop_x86_64_ms_pe_masm.asm +++ b/src/asm/ontop_x86_64_ms_pe_masm.asm @@ -65,28 +65,28 @@ ; ---------------------------------------------------------------------------------- ; | 0x100 | 0x104 | 0x108 | 0x10c | 0x110 | 0x114 | 0x118 | 0x11c | ; ---------------------------------------------------------------------------------- -; | RBX | RBP | hidden | RIP | +; | RBX | RBP | hidden | padding | ; ---------------------------------------------------------------------------------- ; ---------------------------------------------------------------------------------- ; | 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 | ; ---------------------------------------------------------------------------------- ; ---------------------------------------------------------------------------------- ; | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | ; ---------------------------------------------------------------------------------- ; | 0x140 | 0x144 | 0x148 | 0x14c | 0x150 | 0x154 | 0x158 | 0x15c | ; ---------------------------------------------------------------------------------- -; | FCTX | DATA | GS:[0] | GS:[8] | +; | parameter area | ; ---------------------------------------------------------------------------------- ; ---------------------------------------------------------------------------------- ; | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | ; ---------------------------------------------------------------------------------- ; | 0x160 | 0x164 | 0x168 | 0x16c | 0x170 | 0x174 | 0x178 | 0x17c | ; ---------------------------------------------------------------------------------- -; | GS:[16] | | +; | FCTX | DATA | | ; ---------------------------------------------------------------------------------- .code @@ -95,7 +95,7 @@ ontop_fcontext PROC BOOST_CONTEXT_EXPORT FRAME .endprolog ; prepare stack - lea rsp, [rsp-0130h] + lea rsp, [rsp-0138h] IFNDEF BOOST_USE_TSX ; save XMM storage @@ -192,7 +192,7 @@ ENDIF mov rax, [rsp+0110h] ; restore hidden address of transport_t ; prepare stack - lea rsp, [rsp+0130h] + lea rsp, [rsp+0138h] ; keep return-address on stack From a51e8793114be9682db80e808e354cda80128b1b Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Wed, 1 Jan 2025 19:44:45 -0500 Subject: [PATCH 08/10] Initialize the exception chain end block. --- src/asm/make_x86_64_ms_pe_masm.asm | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/asm/make_x86_64_ms_pe_masm.asm b/src/asm/make_x86_64_ms_pe_masm.asm index acfd7bb6..bdde3527 100644 --- a/src/asm/make_x86_64_ms_pe_masm.asm +++ b/src/asm/make_x86_64_ms_pe_masm.asm @@ -115,8 +115,8 @@ 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+0158h], 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 @@ -124,18 +124,18 @@ make_fcontext PROC BOOST_CONTEXT_EXPORT FRAME lea rcx, [rcx+rdx] ; save bottom address of context stack as 'limit' mov [rax+0c0h], rcx -;; ; also save low address in GS:[16] -;; mov [rax+0160h], rcx + ; 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 mov [rax+0b0h], rcx -;; ; set GS:[0] to end-pointer marker (ffff'ffff'ffff'ffffh) for exception -;; ; handler chain -;; not rcx -;; mov [rax+0150h], rcx + ; 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] From a91d3a19467aec1e7e772d077a5b4f67adbe2dbf Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Wed, 1 Jan 2025 19:52:20 -0500 Subject: [PATCH 09/10] Save exception block at GS:[0] in context-switch state block. This commit doesn't yet restore it. --- src/asm/jump_x86_64_ms_pe_masm.asm | 18 +++++++++--------- src/asm/ontop_x86_64_ms_pe_masm.asm | 18 +++++++++--------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/asm/jump_x86_64_ms_pe_masm.asm b/src/asm/jump_x86_64_ms_pe_masm.asm index d12bb856..04855307 100644 --- a/src/asm/jump_x86_64_ms_pe_masm.asm +++ b/src/asm/jump_x86_64_ms_pe_masm.asm @@ -141,12 +141,12 @@ ENDIF mov [rsp+0110h], rcx ; save hidden address of transport_t -;; mov rax, gs:[0h] ; save exception block at gs:[0] -;; mov [rsp+0150h], rax -;; mov rax, gs:[8h] -;; mov [rsp+0158h], rax -;; mov rax, gs:[10h] -;; mov [rsp+0160h], rax + 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 @@ -196,11 +196,11 @@ ENDIF mov rbx, [rsp+0100h] ; restore RBX mov rbp, [rsp+0108h] ; restore RBP -;; mov rax, [rsp+0150h] ; restore exception block at gs:[0] +;; mov rax, [rsp+0120h] ; restore exception block at gs:[0] ;; mov gs:[0h], rax -;; mov rax, [rsp+0158h] +;; mov rax, [rsp+0128h] ;; mov gs:[8h], rax -;; mov rax, [rsp+0160h] +;; mov rax, [rsp+0130h] ;; mov gs:[10h], rax mov rax, [rsp+0110h] ; restore hidden address of transport_t diff --git a/src/asm/ontop_x86_64_ms_pe_masm.asm b/src/asm/ontop_x86_64_ms_pe_masm.asm index da73de9e..231716ca 100644 --- a/src/asm/ontop_x86_64_ms_pe_masm.asm +++ b/src/asm/ontop_x86_64_ms_pe_masm.asm @@ -141,12 +141,12 @@ ENDIF mov [rsp+0110h], rcx ; save hidden address of transport_t -;; mov rax, gs:[0h] ; save exception block at gs:[0] -;; mov [rsp+0150h], rax -;; mov rax, gs:[8h] -;; mov [rsp+0158h], rax -;; mov rax, gs:[10h] -;; mov [rsp+0160h], rax + 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 RCX mov rcx, rsp @@ -196,11 +196,11 @@ ENDIF mov rbx, [rsp+0100h] ; restore RBX mov rbp, [rsp+0108h] ; restore RBP -;; mov rax, [rsp+0150h] ; restore exception block at gs:[0] +;; mov rax, [rsp+0120h] ; restore exception block at gs:[0] ;; mov gs:[0h], rax -;; mov rax, [rsp+0158h] +;; mov rax, [rsp+0128h] ;; mov gs:[8h], rax -;; mov rax, [rsp+0160h] +;; mov rax, [rsp+0130h] ;; mov gs:[10h], rax mov rax, [rsp+0110h] ; restore hidden address of transport_t From 8324696e9e5f5e132d6c9ac7e92b89a755feca30 Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Wed, 1 Jan 2025 19:57:39 -0500 Subject: [PATCH 10/10] Restore exception block at GS:[0] on every fiber context switch. --- src/asm/jump_x86_64_ms_pe_masm.asm | 12 ++++++------ src/asm/ontop_x86_64_ms_pe_masm.asm | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/asm/jump_x86_64_ms_pe_masm.asm b/src/asm/jump_x86_64_ms_pe_masm.asm index 04855307..607c9f9e 100644 --- a/src/asm/jump_x86_64_ms_pe_masm.asm +++ b/src/asm/jump_x86_64_ms_pe_masm.asm @@ -196,12 +196,12 @@ 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+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 diff --git a/src/asm/ontop_x86_64_ms_pe_masm.asm b/src/asm/ontop_x86_64_ms_pe_masm.asm index 231716ca..1a0c9ab4 100644 --- a/src/asm/ontop_x86_64_ms_pe_masm.asm +++ b/src/asm/ontop_x86_64_ms_pe_masm.asm @@ -196,12 +196,12 @@ 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+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