public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Iain Buclaw <ibuclaw@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc(refs/users/ibuclaw/heads/darwin)] D, Darwin, PPC : Implement thread interfaces.
Date: Sat, 10 Apr 2021 17:02:08 +0000 (GMT)	[thread overview]
Message-ID: <20210410170208.34C903A0702F@sourceware.org> (raw)

https://gcc.gnu.org/g:75f0bfd6821869b155937b353d5ed2bf0023d25c

commit 75f0bfd6821869b155937b353d5ed2bf0023d25c
Author: Iain Sandoe <iain@sandoe.co.uk>
Date:   Fri Dec 11 00:55:38 2020 +0000

    D, Darwin, PPC : Implement thread interfaces.
    
    This provides the implementation for the threads and fibres
    interfaces for PowerPC Darwin (32 and 64 bit).

Diff:
---
 libphobos/libdruntime/core/thread/fiber.d    | 45 +++++++++++++-
 libphobos/libdruntime/core/thread/osthread.d | 92 ++++++++++++++++++++++++++++
 2 files changed, 134 insertions(+), 3 deletions(-)

diff --git a/libphobos/libdruntime/core/thread/fiber.d b/libphobos/libdruntime/core/thread/fiber.d
index 1161914a8a2..ffd3968ef2c 100644
--- a/libphobos/libdruntime/core/thread/fiber.d
+++ b/libphobos/libdruntime/core/thread/fiber.d
@@ -69,7 +69,8 @@ private
     }
     else version (X86)
     {
-        version = AlignFiberStackTo16Byte;
+        version (OSX)
+            version = AlignFiberStackTo16Byte;
 
         version (CET)
         {
@@ -121,7 +122,12 @@ private
     }
     else version (PPC)
     {
-        version (Posix)
+        version (OSX)
+        {
+            version = AlignFiberStackTo16Byte;
+            version = AsmPPC_External_Darwin;
+        }
+        else version (Posix)
         {
             version = AsmPPC_Posix;
             version = AsmExternal;
@@ -129,8 +135,14 @@ private
     }
     else version (PPC64)
     {
-        version (Posix)
+        version (OSX)
+        {
+            version = AlignFiberStackTo16Byte;
+            version = AsmPPC_External_Darwin;
+        }
+        else version (Posix)
         {
+            version = AsmPPC64_Posix;
             version = AlignFiberStackTo16Byte;
         }
     }
@@ -175,6 +187,7 @@ private
         version (AsmX86_Posix)      {} else
         version (AsmX86_64_Windows) {} else
         version (AsmX86_64_Posix)   {} else
+        version (AsmPPC_External_Darwin) {} else
         version (AsmExternal)       {} else
         {
             // NOTE: The ucontext implementation requires architecture specific
@@ -231,6 +244,10 @@ private
       version (AArch64)
           extern (C) void fiber_trampoline() nothrow;
   }
+  else version (AsmPPC_External_Darwin)
+  {
+      extern (C) void fiber_switchContext( void** oldp, void* newp ) nothrow @nogc;
+  }
   else
     extern (C) void fiber_switchContext( void** oldp, void* newp ) nothrow @nogc
     {
@@ -1384,6 +1401,28 @@ private:
 
             assert( (cast(size_t) pstack & 0x0f) == 0 );
         }
+        else version (AsmPPC_External_Darwin)
+        {
+            version (StackGrowsDown) {}
+            else static assert(false, "PowerPC Darwin only supports decrementing stacks");
+
+            uint wsize = size_t.sizeof;
+
+            // linkage + regs + FPRs + VRs
+            uint space = 8 * wsize + 20 * wsize + 18 * 8 + 12 * 16;
+            (cast(ubyte*)pstack - space)[0 .. space] = 0;
+
+            pstack -= wsize * 6;
+            *cast(size_t*)pstack = cast(size_t) &fiber_entryPoint; // LR
+            pstack -= wsize * 22;
+
+            // On Darwin PPC64 pthread self is in R13 (which is reserved).
+            // At present, it is not safe to migrate fibres between threads, but if that
+            // changes, then updating the value of R13 will also need to be handled.
+            version (PPC64)
+              *cast(size_t*)(pstack + wsize) = cast(size_t) Thread.getThis().m_addr;
+            assert( (cast(size_t) pstack & 0x0f) == 0 );
+        }
         else version (AsmMIPS_O32_Posix)
         {
             version (StackGrowsDown) {}
diff --git a/libphobos/libdruntime/core/thread/osthread.d b/libphobos/libdruntime/core/thread/osthread.d
index 282e7b8ac90..de5df33ac7a 100644
--- a/libphobos/libdruntime/core/thread/osthread.d
+++ b/libphobos/libdruntime/core/thread/osthread.d
@@ -37,6 +37,11 @@ else version (TVOS)
     version = Darwin;
 else version (WatchOS)
     version = Darwin;
+version (Darwin)
+{
+    version (PPC) version = Darwin_AnyPPC;
+    version (PPC64) version = Darwin_AnyPPC;
+}
 
 version (Shared)
     version (GNU)
@@ -420,6 +425,17 @@ class Thread : ThreadBase
         {
             uint[16]        m_reg; // r0-r15
         }
+        else version (PPC)
+        {
+            // Make the assumption that we only care about non-fp and non-vr regs.
+            // ??? : it seems plausible that a valid address can be copied into a VR.
+            uint[32]        m_reg; //  r0-31
+        }
+        else version (PPC64)
+        {
+            // As above.
+            ulong[32]       m_reg; // r0-31
+        }
         else
         {
             static assert(false, "Architecture not supported." );
@@ -1429,6 +1445,60 @@ in (fn)
             }
             sp = cast(void*)&regs[0];
         }
+        else version (PPC)
+        {
+            void*[19] regs = void;
+            asm pure nothrow @nogc
+            {
+                "stw r13, %0" : "=m" (regs[ 0]);
+                "stw r14, %0" : "=m" (regs[ 1]);
+                "stw r15, %0" : "=m" (regs[ 2]);
+                "stw r16, %0" : "=m" (regs[ 3]);
+                "stw r17, %0" : "=m" (regs[ 4]);
+                "stw r18, %0" : "=m" (regs[ 5]);
+                "stw r19, %0" : "=m" (regs[ 6]);
+                "stw r20, %0" : "=m" (regs[ 7]);
+                "stw r21, %0" : "=m" (regs[ 9]);
+                "stw r22, %0" : "=m" (regs[ 9]);
+                "stw r23, %0" : "=m" (regs[10]);
+                "stw r24, %0" : "=m" (regs[11]);
+                "stw r25, %0" : "=m" (regs[12]);
+                "stw r26, %0" : "=m" (regs[13]);
+                "stw r27, %0" : "=m" (regs[14]);
+                "stw r28, %0" : "=m" (regs[15]);
+                "stw r29, %0" : "=m" (regs[16]);
+                "stw r30, %0" : "=m" (regs[17]);
+                "stw r31, %0" : "=m" (regs[18]);
+            }
+            sp = cast(void*)&regs[0];
+        }
+        else version (PPC64)
+        {
+            void*[19] regs = void;
+            asm pure nothrow @nogc
+            {
+                "std r13, %0" : "=m" (regs[ 0]);
+                "std r14, %0" : "=m" (regs[ 1]);
+                "std r15, %0" : "=m" (regs[ 2]);
+                "std r16, %0" : "=m" (regs[ 3]);
+                "std r17, %0" : "=m" (regs[ 4]);
+                "std r18, %0" : "=m" (regs[ 5]);
+                "std r19, %0" : "=m" (regs[ 6]);
+                "std r20, %0" : "=m" (regs[ 7]);
+                "std r21, %0" : "=m" (regs[ 8]);
+                "std r22, %0" : "=m" (regs[ 9]);
+                "std r23, %0" : "=m" (regs[10]);
+                "std r24, %0" : "=m" (regs[11]);
+                "std r25, %0" : "=m" (regs[12]);
+                "std r26, %0" : "=m" (regs[13]);
+                "std r27, %0" : "=m" (regs[14]);
+                "std r28, %0" : "=m" (regs[15]);
+                "std r29, %0" : "=m" (regs[16]);
+                "std r30, %0" : "=m" (regs[17]);
+                "std r31, %0" : "=m" (regs[18]);
+            }
+            sp = cast(void*)&regs[0];
+        }
         else
         {
             __builtin_unwind_init();
@@ -1826,6 +1896,28 @@ private extern (D) bool suspend( Thread t ) nothrow @nogc
             t.m_reg[14] = state.lr;
             t.m_reg[15] = state.pc;
         }
+        else version (PPC)
+        {
+            ppc_thread_state_t    state = void;
+            mach_msg_type_number_t  count = PPC_THREAD_STATE_COUNT;
+
+            if ( thread_get_state( t.m_tmach, PPC_THREAD_STATE, &state, &count ) != KERN_SUCCESS )
+                onThreadError( "Unable to load thread state" );
+            if ( !t.m_lock )
+                t.m_curr.tstack = cast(void*) state.r[1];
+            t.m_reg[] = state.r[];
+        }
+        else version (PPC64)
+        {
+            ppc_thread_state64_t    state = void;
+            mach_msg_type_number_t  count = PPC_THREAD_STATE64_COUNT;
+
+            if ( thread_get_state( t.m_tmach, PPC_THREAD_STATE64, &state, &count ) != KERN_SUCCESS )
+                onThreadError( "Unable to load thread state" );
+            if ( !t.m_lock )
+                t.m_curr.tstack = cast(void*) state.r[1];
+            t.m_reg[] = state.r[];
+        }
         else
         {
             static assert(false, "Architecture not supported." );


             reply	other threads:[~2021-04-10 17:02 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-10 17:02 Iain Buclaw [this message]
  -- strict thread matches above, loose matches on Subject: below --
2021-09-17 14:34 Iain Buclaw
2021-04-19 18:06 Iain Buclaw
2021-04-10 15:05 Iain Buclaw
2021-03-14 22:01 Iain Buclaw
2021-03-07 17:02 Iain Buclaw
2021-01-30 19:10 Iain Buclaw
2021-01-28 17:33 Iain Buclaw

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210410170208.34C903A0702F@sourceware.org \
    --to=ibuclaw@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).