Skip to content

Commit

Permalink
Basic x64 prep
Browse files Browse the repository at this point in the history
  • Loading branch information
zero318 committed Jun 3, 2024
1 parent ef6c456 commit c9413dd
Show file tree
Hide file tree
Showing 31 changed files with 873 additions and 173 deletions.
6 changes: 5 additions & 1 deletion thcrap/src/binhack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ static TH_NOINLINE double constpool_float_value(const char *const expr, patch_va
}
// Fallback to the expression parser
(void)eval_expr(expr, end_char, &val->z, NULL, rel_source, hMod);
return val->z;
return (double)val->z;
}

// Data to be rendered into a constpool.
Expand Down Expand Up @@ -1467,7 +1467,11 @@ bool codecave_from_json(const char *name, json_t *in, codecave_t *out) {
return false;
case JEVAL_SUCCESS:
// Round the alignment to the next power of 2 (including 1)
#if TH_X86
if (unsigned long bit; _BitScanReverse(&bit, align_val - 1)) {
#else
if (unsigned long bit; _BitScanReverse64(&bit, align_val - 1)) {
#endif
align_val = 1u << (bit + 1);
} else {
align_val = 1u;
Expand Down
1 change: 0 additions & 1 deletion thcrap/src/binhack.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ size_t binhacks_apply(const binhack_t *binhacks, size_t binhacks_count, HMODULE
// }
// "codecaves": {
// "test_cave": "somecode"
// }
// }
size_t codecaves_apply(codecave_t *codecaves, size_t codecaves_count, HMODULE hMod, HackpointMemoryPage page_array[5]);

Expand Down
2 changes: 1 addition & 1 deletion thcrap/src/bp_entry.asm
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Breakpoint entry point. Written for i686-w64-mingw32-as.
*/

.intel_syntax
.intel_syntax noprefix

.global _bp_entry0, _bp_entry0_jsonptr, _bp_entry0_funcptr, _bp_entry0_caveptr, _bp_entry0_end
.global _bp_entry0s, _bp_entry0s_jsonptr, _bp_entry0s_funcptr, _bp_entry0s_caveptr, _bp_entry0s_end, _bp_entry0s_retpop
Expand Down
343 changes: 343 additions & 0 deletions thcrap/src/bp_entry_64.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,343 @@
/**
* Touhou Community Reliant Automatic Patcher
* Main DLL
*
* ----
*
* Breakpoint entry point. Written for i686-w64-mingw32-as.
*/

.intel_syntax noprefix

.global bp_entry0, bp_entry0_jsonptr, bp_entry0_funcptr, bp_entry0_caveptr, bp_entry0_end
.global bp_entry0s, bp_entry0s_jsonptr, bp_entry0s_funcptr, bp_entry0s_caveptr, bp_entry0s_end, bp_entry0s_retpop
.global bp_entry1, bp_entry1_jsonptr, bp_entry1_funcptr, bp_entry1_caveptr, bp_entry1_end
.global bp_entry1s, bp_entry1s_jsonptr, bp_entry1s_funcptr, bp_entry1s_caveptr, bp_entry1s_end, bp_entry1s_retpop
.global bp_entry2, bp_entry2_jsonptr, bp_entry2_funcptr, bp_entry2_caveptr, bp_entry2_end
.global bp_entry2s, bp_entry2s_jsonptr, bp_entry2s_funcptr, bp_entry2s_caveptr, bp_entry2s_end, bp_entry2s_retpop
.global bp_entry3, bp_entry3_jsonptr, bp_entry3_funcptr, bp_entry3_caveptr, bp_entry3_end
.global bp_entry3s, bp_entry3s_jsonptr, bp_entry3s_funcptr, bp_entry3s_caveptr, bp_entry3s_end, bp_entry3s_retpop

.macro movRAXQ value
.byte 0x48
.byte 0xB8
.quad \value
.endm

.macro movRCXQ value
.byte 0x48
.byte 0xB9
.quad \value
.endm

.macro movRDXQ value
.byte 0x48
.byte 0xBA
.quad \value
.endm

.macro pusha
push rax
push rcx
push rdx
push rbx
lea rbx, [rsp+0x28] /* fixed rsp offset */
push rbx
push rbp
push rsi
push rdi
push r8
push r9
push r10
push r11
push r12
push r13
push r14
push r15
.endm

.macro popa_except_rax
pop r15
pop r14
pop r13
pop r12
pop r11
pop r10
pop r9
pop r8
pop rdi
pop rsi
pop rbp
pop rcx
pop rbx
pop rdx
pop rcx
.endm

.macro popa
popa_except_rax
pop rax
.endm

.macro pushf_fast
lahf
seto al
push rax
.endm

.macro popf_fast
pop rax
cmp al, 0x81
sahf
.endm

bp_entry0:
pusha
pushf_fast
cld
mov rbp, rsp
and rsp, -16
sub rsp, 0x20
mov rcx, rbp
bp_entry0_jsonptr:
movRDXQ 0x0000000000000000
bp_entry0_funcptr:
movRAXQ 0x0000000000000000
call rax
mov rsp, rbp
test eax, eax
jz skip_ret_set0
bp_entry0_caveptr:
movRCXQ 0x0000000000000000
mov qword ptr [rbx-8], rcx
skip_ret_set0:
popf_fast
popa
ret
.balign 16, 0xCC
bp_entry0_end:

bp_entry0s:
pusha
pushf_fast
cld
mov rbp, rsp
and rsp, -16
sub rsp, 0x20
mov rcx, rbp
bp_entry0s_jsonptr:
movRDXQ 0x0000000000000000
bp_entry0s_funcptr:
movRAXQ 0x0000000000000000
call rax
mov rsp, rbp
test eax, eax
pop rax
popa_except_rax
jnz ret_set0s
cmp al, 0x81
sahf
pop rax
bp_entry0s_retpop:
ret 0
int3
ret_set0s:
cmp al, 0x81
sahf
bp_entry0s_caveptr:
movRAXQ 0x0000000000000000
mov qword ptr [rsp+8], rax
pop rax
ret
.balign 16, 0xCC
bp_entry0s_end:

bp_entry1:
pusha
pushf
cld
mov rbp, rsp
and rsp, -16
sub rsp, 0x20
mov rcx, rbp
bp_entry1_jsonptr:
movRDXQ 0x0000000000000000
bp_entry1_funcptr:
movRAXQ 0x0000000000000000
call rax
mov rsp, rbp
test eax, eax
jz skip_ret_set1
bp_entry1_caveptr:
movRCXQ 0x0000000000000000
mov qword ptr [rbx-8], rcx
skip_ret_set1:
popf
popa
ret
.balign 16, 0xCC
bp_entry1_end:

bp_entry1s:
pusha
pushf
cld
mov rbp, rsp
and rsp, -16
sub rsp, 0x20
mov rcx, rbp
bp_entry1s_jsonptr:
movRDXQ 0x0000000000000000
bp_entry1s_funcptr:
movRAXQ 0x0000000000000000
call rax
mov rsp, rbp
test eax, eax
pop rax
popa_except_rax
push rax
jnz ret_set1s
popf
pop rax
bp_entry1s_retpop:
ret 0
int3
ret_set1s:
popf
bp_entry1s_caveptr:
movRAXQ 0x0000000000000000
mov qword ptr [rsp+8], rax
pop rax
ret
.balign 16, 0xCC
bp_entry1s_end:

bp_entry2:
pusha
pushf_fast
cld
mov rbp, rsp
and rsp, -16
sub rsp, 512 + 0x20
mov rcx, rbp
fxsave64 [rsp-0x20]
bp_entry2_jsonptr:
movRDXQ 0x0000000000000000
bp_entry2_funcptr:
movRAXQ 0x0000000000000000
call rax
fxrstor64 [rsp+0x20]
mov rsp, rbp
test eax, eax
jz skip_ret_set2
bp_entry2_caveptr:
movRCXQ 0x0000000000000000
mov qword ptr [ebp+0x24], rcx
skip_ret_set2:
popf_fast
popa
ret
.balign 16, 0xCC
bp_entry2_end:

bp_entry2s:
pusha
pushf_fast
cld
mov rbp, rsp
and rsp, -16
sub rsp, 512 + 0x20
mov rcx, rbp
fxsave64 [rsp-0x20]
bp_entry2s_jsonptr:
movRDXQ 0x0000000000000000
bp_entry2s_funcptr:
movRAXQ 0x0000000000000000
call rax
fxrstor64 [rsp+0x20]
mov rsp, rbp
test eax, eax
pop rax
popa_except_rax
jnz ret_set2s
cmp al, 0x81
sahf
pop rax
bp_entry2s_retpop:
ret 0
int3
ret_set2s:
cmp al, 0x81
sahf
pop rax
bp_entry2s_caveptr:
movRAXQ 0x0000000000000000
mov qword ptr [rsp+8], rax
pop rax
ret
.balign 16, 0xCC
bp_entry2s_end:

bp_entry3:
pusha
pushf
cld
mov rbp, rsp
and rsp, -16
sub rsp, 512 + 0x20
mov rcx, rbp
fxsave64 [rsp-0x20]
bp_entry3_jsonptr:
movRDXQ 0x0000000000000000
bp_entry3_funcptr:
movRAXQ 0x0000000000000000
call rax
fxrstor64 [rsp+0x20]
mov rsp, rbp
test eax, eax
jz skip_ret_set3
bp_entry3_caveptr:
movRCXQ 0x0000000000000000
mov qword ptr [rbx-8], rcx
skip_ret_set3:
popf
popa
ret
.balign 16, 0xCC
bp_entry3_end:

bp_entry3s:
pusha
pushf
cld
mov rbp, rsp
and rsp, -16
sub rsp, 512 + 0x20
mov rcx, rbp
fxsave64 [rsp-0x20]
bp_entry3s_jsonptr:
movRDXQ 0x0000000000000000
bp_entry3s_funcptr:
movRAXQ 0x0000000000000000
call rax
fxrstor64 [rsp+0x20]
mov rsp, rbp
test eax, eax
pop rax
popa_except_rax
push rax
jnz ret_set3s
popf
pop rax
bp_entry3s_retpop:
ret 0
int3
ret_set3s:
popf
bp_entry3s_caveptr:
movRAXQ 0x0000000000000000
mov qword ptr [rsp+8], rax
pop rax
ret
.balign 16, 0xCC
bp_entry3s_end:
Loading

0 comments on commit c9413dd

Please sign in to comment.