From patchwork Fri Dec 1 10:08:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Yujie X-Patchwork-Id: 81101 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 581AD385C6F7 for ; Fri, 1 Dec 2023 10:09:12 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from eggs.gnu.org (eggs.gnu.org [IPv6:2001:470:142:3::10]) by sourceware.org (Postfix) with ESMTPS id 5A47C385C338 for ; Fri, 1 Dec 2023 10:08:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5A47C385C338 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=loongson.cn ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 5A47C385C338 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2001:470:142:3::10 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701425333; cv=none; b=Hz2qR/J2P7Ps1B4J3VMwVRUbr9K5WU46egUW2IicEz9hnS5x7042MptKkzDrX3kLg3oZyGVJkizzuJ8m5Odwnx1tJYSDjhQj3x2lGKTiCYGItTQwgywY0kICeI7wwWxhT8SrvhOW+K/G2yh5tqKZyEIo0xKEQ0+/78HKe+nwD0U= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701425333; c=relaxed/simple; bh=+pBpcKjglEtFhfkqaURqjRVOTWf7+YayzV0apnZ/CMA=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=mvcsagGDyRMfIbFT0nLdr+00qA4hmsgcs8vTvpwq3/Ix7NuJIaFexJyxwRN6NjPVApELQ/KxZY51RbB1rdwCDH36QG1zmE3ruJJbqgkVVbMNAP98w/BsB/BEO/HcV2o7MsHZ6OcQnUYP0RMBBHccvOzjvFLGiQ0Nw+8XI9vRA8Q= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from mail.loongson.cn ([114.242.206.163]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r90Ry-00083S-Ou for gcc-patches@gcc.gnu.org; Fri, 01 Dec 2023 05:08:51 -0500 Received: from loongson.cn (unknown [114.243.104.185]) by gateway (Coremail) with SMTP id _____8Dxg_CpsGlljCs+AA--.58556S3; Fri, 01 Dec 2023 18:08:41 +0800 (CST) Received: from localhost.localdomain (unknown [114.243.104.185]) by localhost.localdomain (Coremail) with SMTP id AQAAf8CxLC+lsGllaYRRAA--.8937S5; Fri, 01 Dec 2023 18:08:41 +0800 (CST) From: Yang Yujie To: gcc-patches@gcc.gnu.org Cc: ibuclaw@gdcproject.org, chenglulu@loongson.cn, xuchenghua@loongson.cn, Yang Yujie Subject: [PATCH v3 3/3] libruntime: Add fiber context switch code for LoongArch. Date: Fri, 1 Dec 2023 18:08:27 +0800 Message-ID: <20231201100827.227376-4-yangyujie@loongson.cn> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231201100827.227376-1-yangyujie@loongson.cn> References: <20231201100827.227376-1-yangyujie@loongson.cn> MIME-Version: 1.0 X-CM-TRANSID: AQAAf8CxLC+lsGllaYRRAA--.8937S5 X-CM-SenderInfo: 51dqw5pxmlvqxorr0wxvrqhubq/1tbiAQAMAGVpQqsJJgAAsH X-Coremail-Antispam: 1Uk129KBj93XoWxWF4kXr18Wr1ftF17WF4UAwc_yoWrCr48pr 48Ar4DWr40yrZxJwn7t3yaqr15Gwn3Z34kAr98KFWjv34rAF1SyrnakFyak39Yq3WfG3WY v34Y9w4DAFnrGFXCm3ZEXasCq-sJn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUUk2b4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Gr0_Xr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVCY1x0267AK xVW8Jr0_Cr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l57IF6xkI12xvs2x26I8E6xACxx 1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r126r1DMcIj6I8E87Iv 67AKxVW8JVWxJwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41l42xK82IYc2 Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s02 6x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r126r1DMIIYrxkI7VAKI48JMIIF0x vE2Ix0cI8IcVAFwI0_JFI_Gr1lIxAIcVC0I7IYx2IY6xkF7I0E14v26r1j6r4UMIIF0xvE 42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVW8JVWxJwCI42IY6I8E87Iv6x kF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUcDDGUUUUU Received-SPF: pass client-ip=114.242.206.163; envelope-from=yangyujie@loongson.cn; helo=mail.loongson.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_BARRACUDACENTRAL, SPF_FAIL, SPF_HELO_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org libphobos/ChangeLog: * libdruntime/config/loongarch/switchcontext.S: New file. --- .../config/loongarch/switchcontext.S | 133 ++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 libphobos/libdruntime/config/loongarch/switchcontext.S diff --git a/libphobos/libdruntime/config/loongarch/switchcontext.S b/libphobos/libdruntime/config/loongarch/switchcontext.S new file mode 100644 index 00000000000..edfb9b67e8f --- /dev/null +++ b/libphobos/libdruntime/config/loongarch/switchcontext.S @@ -0,0 +1,133 @@ +/* LoongArch support code for fibers and multithreading. + Copyright (C) 2023 Free Software Foundation, Inc. + +This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 3, or (at your option) any later +version. + +GCC is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +Under Section 7 of GPL version 3, you are granted additional +permissions described in the GCC Runtime Library Exception, version +3.1, as published by the Free Software Foundation. + +You should have received a copy of the GNU General Public License and +a copy of the GCC Runtime Library Exception along with this program; +see the files COPYING3 and COPYING.RUNTIME respectively. If not, see +. */ + +#include "../common/threadasm.S" + +/** + * Performs a context switch. + * + * $a0 - void** - ptr to old stack pointer + * $a1 - void* - new stack pointer + * + */ + +#if defined(__loongarch_lp64) +# define GPR_L ld.d +# define GPR_S st.d +# define SZ_GPR 8 +# define ADDSP(si) addi.d $sp, $sp, si +#elif defined(__loongarch64_ilp32) +# define GPR_L ld.w +# define GPR_S st.w +# define SZ_GPR 4 +# define ADDSP(si) addi.w $sp, $sp, si +#else +# error Unsupported GPR size (must be 64-bit or 32-bit). +#endif + +#if defined(__loongarch_double_float) +# define FPR_L fld.d +# define FPR_S fst.d +# define SZ_FPR 8 +#elif defined(__loongarch_single_float) +# define FPR_L fld.s +# define FPR_S fst.s +# define SZ_FPR 4 +#else +# define SZ_FPR 0 +#endif + + .text + .align 2 + .global fiber_switchContext + .type fiber_switchContext, @function +fiber_switchContext: + .cfi_startproc + ADDSP(-11 * SZ_GPR) + + // fp regs and return address are stored below the stack + // because we don't want the GC to scan them. + + // return address (r1) + GPR_S $r1, $sp, -SZ_GPR + +#if SZ_FPR != 0 + // callee-saved scratch FPRs (f24-f31) + FPR_S $f24, $sp, -SZ_GPR-1*SZ_FPR + FPR_S $f25, $sp, -SZ_GPR-2*SZ_FPR + FPR_S $f26, $sp, -SZ_GPR-3*SZ_FPR + FPR_S $f27, $sp, -SZ_GPR-4*SZ_FPR + FPR_S $f28, $sp, -SZ_GPR-5*SZ_FPR + FPR_S $f29, $sp, -SZ_GPR-6*SZ_FPR + FPR_S $f30, $sp, -SZ_GPR-7*SZ_FPR + FPR_S $f31, $sp, -SZ_GPR-8*SZ_FPR +#endif + + // callee-saved GPRs (r21, fp (r22), r23-r31) + GPR_S $r21, $sp, 0*SZ_GPR + GPR_S $fp, $sp, 1*SZ_GPR + GPR_S $s0, $sp, 2*SZ_GPR + GPR_S $s1, $sp, 3*SZ_GPR + GPR_S $s2, $sp, 4*SZ_GPR + GPR_S $s3, $sp, 5*SZ_GPR + GPR_S $s4, $sp, 6*SZ_GPR + GPR_S $s5, $sp, 7*SZ_GPR + GPR_S $s6, $sp, 8*SZ_GPR + GPR_S $s7, $sp, 9*SZ_GPR + GPR_S $s8, $sp, 10*SZ_GPR + + // swap stack pointer + GPR_S $sp, $a0, 0 + move $sp, $a1 + + GPR_L $r1, $sp, -SZ_GPR + +#if SZ_FPR != 0 + FPR_L $f24, $sp, -SZ_GPR-1*SZ_FPR + FPR_L $f25, $sp, -SZ_GPR-2*SZ_FPR + FPR_L $f26, $sp, -SZ_GPR-3*SZ_FPR + FPR_L $f27, $sp, -SZ_GPR-4*SZ_FPR + FPR_L $f28, $sp, -SZ_GPR-5*SZ_FPR + FPR_L $f29, $sp, -SZ_GPR-6*SZ_FPR + FPR_L $f30, $sp, -SZ_GPR-7*SZ_FPR + FPR_L $f31, $sp, -SZ_GPR-8*SZ_FPR +#endif + + GPR_L $r21, $sp, 0*SZ_GPR + GPR_L $fp, $sp, 1*SZ_GPR + GPR_L $s0, $sp, 2*SZ_GPR + GPR_L $s1, $sp, 3*SZ_GPR + GPR_L $s2, $sp, 4*SZ_GPR + GPR_L $s3, $sp, 5*SZ_GPR + GPR_L $s4, $sp, 6*SZ_GPR + GPR_L $s5, $sp, 7*SZ_GPR + GPR_L $s6, $sp, 8*SZ_GPR + GPR_L $s7, $sp, 9*SZ_GPR + GPR_L $s8, $sp, 10*SZ_GPR + + ADDSP(11 * SZ_GPR) + + jr $r1 // return + .cfi_endproc + .size fiber_switchContext,.-fiber_switchContext