Skip to content

Commit 430785c

Browse files
thiru-mcwBlackhex
authored andcommitted
Add inline assembly pthread wrapper (#28)
This change introduces AArch64-specific inline assembly implementation that prepares the environment for starting a new thread.
1 parent 799a68a commit 430785c

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

winsup/cygwin/create_posix_thread.cc

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,22 @@ pthread_wrapper (PVOID arg)
100100
: : [WRAPPER_ARG] "o" (wrapper_arg),
101101
[CYGTLS] "i" (__CYGTLS_PADSIZE__));
102102
#elif defined(__aarch64__)
103-
// TODO
103+
/* Sets up a new thread stack, frees the original OS stack,
104+
* and calls the thread function with its arg using AArch64 ABI. */
105+
__asm__ __volatile__ ("\n\
106+
mov x19, %[WRAPPER_ARG] // x19 = &wrapper_arg \n\
107+
ldr x10, [x19, #24] // x10 = wrapper_arg.stackbase \n\
108+
sub sp, x10, %[CYGTLS] // sp = stackbase - (CYGTLS + 32)\n\
109+
mov fp, xzr // clear frame pointer (x29) \n\
110+
mov x0, sp // x0 = new stack pointer \n\
111+
mov x1, xzr // x1 = 0 (dwSize) \n\
112+
mov x2, #0x8000 // x2 = MEM_RELEASE \n\
113+
bl VirtualFree // free original stack \n\
114+
ldp x19, x0, [x19] // x19 = func, x0 = arg \n\
115+
blr x19 // call thread function \n"
116+
: : [WRAPPER_ARG] "r" (&wrapper_arg),
117+
[CYGTLS] "r" (__CYGTLS_PADSIZE__ + 32) // add 32 bytes shadow space
118+
: "x0", "x1", "x2", "x10", "x19", "x29", "memory");
104119
#else
105120
#error unimplemented for this target
106121
#endif

0 commit comments

Comments
 (0)