From patchwork Mon Mar 31 12:30:55 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Forissier X-Patchwork-Id: 877283 Delivered-To: patch@linaro.org Received: by 2002:a5d:6dae:0:b0:38f:210b:807b with SMTP id u14csp1725923wrs; Mon, 31 Mar 2025 05:31:47 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVoT+mvrGAJd8U7bI4B6VxyiwdWriTHbAPVeR/qXRUW13xKOkdck4XSXJ1BLx3die04eTj7kA==@linaro.org X-Google-Smtp-Source: AGHT+IEfGXs0vmeRA1EXJk1Q3jiBJDvIHoMxjVbhb9hL/Q4b+KWj9XN1djzufb8yrOwsy0M4y/n1 X-Received: by 2002:a05:6102:151e:b0:4c1:a2b4:aa0c with SMTP id ada2fe7eead31-4c6d3889552mr4940333137.10.1743424306842; Mon, 31 Mar 2025 05:31:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1743424306; cv=none; d=google.com; s=arc-20240605; b=dx+wXgjWm+wp4WzHsbcdQmQeMSyf6sPYQ/BVN5aOB/EutIABOwxrY9znQ64GzxFkK7 vycnaJ4BaQk3If7tTGV0L1CbZWH0/d11TBe70Gr03YWe9qSjNoO5sCE+VfMAASPZV3R4 p3akLKzxrkxou/5zUdL/zgXUmh9QgQ3ckWs5mk4WBkZzt1DmXKGwgiySwTn9BsjjqAm2 xIs/bsTp7hU5t8tL8Q5JyOtQuiEHvqKrsIxfqafM7PmnKg55Bk/vN4DFou5a2+KGWJyc j3BhPgKios2ClNHcbWzVFNC/03sc9iklC23Ft7mhgdcBa1naz/OoGwAZYJYtI81Oje/K ru0g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=eUVHl6Za6D6pDyge7r7bRk6nRvr2lGPQEthV8hlLudM=; fh=eVnf/63glE1rfclMfDN/+xiyW4eXlOfvYwdF/CMAaQg=; b=BAjo2GvkpywTq5QX06NKRFO2UFcEzi+V0AV4BwIUueEYMFg1Zl3C4CMmb5I+eX9aUM lgdGMzZJC8zrRK84xTc7ZrGiZd92CDCGmrqPYTlcRT482gXUBUYOdpLx8x5huPx6RuwU 9vAz6LzjFumL0pgknCFBnKv60atH09kk0Qzg/injJKoJ31Ue0HW/fTmLs7vEpqAxEtMf WcnzcR45p5mIyr+mirfd5/ETyYWCj1QVS6NNmlq37XskBvn03ueoMVM/FW/K0LiQpScL q4o1TD/qN5yb/rkUEp5efd/Q86RhplzGmf2CGnaxnaVz9Hdp3fs3Qj09Gj0muf55jahM VAKw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="wO7VNb/2"; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id ada2fe7eead31-4c6bfe12016si1696326137.236.2025.03.31.05.31.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:31:46 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="wO7VNb/2"; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id C5B9A81EC0; Mon, 31 Mar 2025 14:31:38 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="wO7VNb/2"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 598FA8171B; Mon, 31 Mar 2025 14:31:36 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 3E1BE819D2 for ; Mon, 31 Mar 2025 14:31:34 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=jerome.forissier@linaro.org Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-4393dc02b78so27519535e9.3 for ; Mon, 31 Mar 2025 05:31:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1743424293; x=1744029093; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=eUVHl6Za6D6pDyge7r7bRk6nRvr2lGPQEthV8hlLudM=; b=wO7VNb/2rAv+poPD/hrvAuxXF4VX5v/7GFc125PrMHqb5URn75YisX7BdFsjEchF0x uuTXomFvo4ajwO8T6EJf8/ZW4lnYxnxEFmCaeMPiPrlaiDcKjUmFMHJ1jjfJMUsBgAik f8baD9d9SMAffXCb1w0gnGCvBlKpxZAy2he8YG54Y/94NAsEGeG7Nls5YUNV2r7bJInO 3506yHG5E8qrJrVM1Ji1lsOhDOL6mhpPziGmnQoxNkzeMofAzM8C1Z/hI5e5WNaMPVjj mECeewL3fPNRLCs/AoCp8xmx6DTYTLxdmMjJXbNAJF59l1olH8Rf9eU3rpK89sl+sU9u exKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743424293; x=1744029093; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eUVHl6Za6D6pDyge7r7bRk6nRvr2lGPQEthV8hlLudM=; b=srlovk7QqHT4sEhVMnQBkOBkdlmN0zQ4IanZW0UlVhS7u7kJT0KeJH5Etrb80c08n0 nRDBF36/TEspIyzmD5wj/zWZbBHFTMb4OnW7C94MDxuTXHJcRYuvMeEFskRtW+rJl0xo mwMpg/wJBxIGF7l42FY9ZByl0fZ18TMwMr74mLYVc9T78mXe780eyo87iKatN+ZF2IHk b831l+d9Q5xd0eSe1Cfjv3uDAMHpv4qg+8sGvxXWhOBETTdmyrYL/pozeuON5dPuIP+v l178AhgOkDRGq2DE/zdHyZT+YaOKgD+PGb/CaBfLGNUipPx/wEZlB267H1CXB7d4IqCY Bp+Q== X-Gm-Message-State: AOJu0Yx6prvUrpNi9W4BbGiqJTB45qfpPxvyPFywCK/v1/YYkeygw+Nr Kt6JPzGfBMFPtcx8bOeF9KTzuM7p+16ev1QGxou3wKJt1u2X40VW6c93bGmUIogHXXpyuU+hTIU +DmA+8g== X-Gm-Gg: ASbGncvyikLUpHWIIXvrZRvkGy5eZ+okf2F71qnvIlV/aCO4u09w3o7CwzJXJoM5dRu oQ5ZUswBaScJil2QMP8RNLtFDSLTCTDbU1Y5y81SmqVeGO/mJqHKVr3jD+JWgKwRrzTW7UNlzuM tPT/3J4suTs7mxU2H6PwjjEE6AlRgnyGf9LY6uG/DDAG0zv7vPLyWBu9hyKW+uSJeopgP0kOQvy oaULYIVrzbUSwRUYvfNuZLJ1iI77p1I5pyY7uttnVEInUzN88iLFWgP9ZNLf/AGcAN5w2KROJpI JpcY8/+HFMESMNU/Xqn95OvsvnDbYjgr0owo+1HUUS0XXBx044Cf X-Received: by 2002:a5d:598d:0:b0:391:6fd:bb65 with SMTP id ffacd0b85a97d-39c120ca6c1mr7269090f8f.9.1743424293443; Mon, 31 Mar 2025 05:31:33 -0700 (PDT) Received: from builder.. ([2a01:e0a:3cb:7bb0:f23c:25c9:dc74:be1]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-39c0b6589e4sm11337029f8f.10.2025.03.31.05.31.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:31:33 -0700 (PDT) From: Jerome Forissier To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Jerome Forissier , Tom Rini , Heinrich Schuchardt , Simon Glass , Andrew Goodbody , Jiaxun Yang , Yu-Chien Peter Lin , Christian Marangi , Vasileios Amoiridis Subject: [PATCH v5 01/16] arch: introduce initjmp() and Kconfig symbol HAVE_INITJMP Date: Mon, 31 Mar 2025 14:30:55 +0200 Message-ID: <20250331123120.2025062-2-jerome.forissier@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250331123120.2025062-1-jerome.forissier@linaro.org> References: <20250331123120.2025062-1-jerome.forissier@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Add the HAVE_INIJMP symbol to be set by architectures that support initjmp(), a non-standard extension to setjmp()/longjmp() allowing to initialize a jump buffer with a function pointer and a stack pointer. This will be useful to later introduce threads. With this new function it becomes possible to longjmp() to a particular function pointer (rather than to a point previously reached during program execution as is the case with setjmp()), and with a custom stack. Both things are needed to spin off a new thread. Then the usual setjmp()/longjmp() pair is enough to save and restore a context, i.e., switch thread. Add the initjmp() prototype to since it is common to all architectures. Add an entry to the API documentation: doc/api/setjmp.rst. Signed-off-by: Jerome Forissier Reviewed-by: Ilias Apalodimas --- arch/Kconfig | 7 +++++++ doc/api/index.rst | 1 + doc/api/setjmp.rst | 10 ++++++++++ include/setjmp.h | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+) create mode 100644 doc/api/setjmp.rst diff --git a/arch/Kconfig b/arch/Kconfig index 35b19f9bfdc..14111ca14fb 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -13,6 +13,13 @@ config HAVE_SETJMP help The architecture supports setjmp() and longjmp(). +config HAVE_INITJMP + bool + depends on HAVE_SETJMP + help + The architecture supports initjmp(), a non-standard companion to + setjmp() and longjmp(). + config SUPPORT_BIG_ENDIAN bool diff --git a/doc/api/index.rst b/doc/api/index.rst index a108718ea99..0dc9ad45d41 100644 --- a/doc/api/index.rst +++ b/doc/api/index.rst @@ -25,6 +25,7 @@ U-Boot API documentation rng sandbox serial + setjmp sysreset timer unicode diff --git a/doc/api/setjmp.rst b/doc/api/setjmp.rst new file mode 100644 index 00000000000..7718047085c --- /dev/null +++ b/doc/api/setjmp.rst @@ -0,0 +1,10 @@ +.. SPDX-License-Identifier: GPL-2.0-or-later + +Long jump API +============= + +.. kernel-doc:: include/setjmp.h + :doc: Overview + +.. kernel-doc:: include/setjmp.h + :internal: diff --git a/include/setjmp.h b/include/setjmp.h index 37d3a8af85d..32dd48803e9 100644 --- a/include/setjmp.h +++ b/include/setjmp.h @@ -3,12 +3,27 @@ #ifndef _SETJMP_H_ #define _SETJMP_H_ 1 +/** + * DOC: Overview + * + * The long jump API allows to perform nonlocal gotos, that is jump from one + * function to another typically further down in the stack, while properly + * restoring the stack's state (unwinding). The two functions needed to do this + * are setjmp() and longjmp(). + * + * In addition to these two standard POSIX.1-2001/C89 functions, a third one is + * present in U-Boot: initjmp(). It is an extension which allows to implement + * user-mode threads. + */ + #ifdef CONFIG_HAVE_SETJMP #include #else struct jmp_buf_data { }; #endif +#include +#include /** * typedef jmp_buf - information needed to restore a calling environment @@ -37,4 +52,21 @@ int setjmp(jmp_buf env); */ void longjmp(jmp_buf env, int val); +/** + * initjmp() - prepare for a long jump to a given function with a given stack + * + * This function sets up a jump buffer for later use with longjmp(). It allows + * to branch to a specific function with a specific stack. Please note that + * @func MUST NOT return. It shall typically restore the main stack and resume + * execution by doing a long jump to a jump buffer initialized by setjmp() + * before the long jump. initjmp() allows to implement multithreading. + * + * @env: jump buffer + * @func: function to be called on longjmp(), MUST NOT RETURN + * @stack_base: the stack to be used by @func (lower address) + * @stack_sz: the stack size in bytes + */ +int initjmp(jmp_buf env, void __noreturn (*func)(void), void *stack_base, + size_t stack_sz); + #endif /* _SETJMP_H_ */ From patchwork Mon Mar 31 12:30:56 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Forissier X-Patchwork-Id: 877284 Delivered-To: patch@linaro.org Received: by 2002:a5d:6dae:0:b0:38f:210b:807b with SMTP id u14csp1725993wrs; Mon, 31 Mar 2025 05:31:56 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVwLaaPTUdjpOrqDA4NtqzSZc4EwmrXi74G5JVrOplPCZwJOH+60nPzu1DMHcB8Uhf1BRJE7g==@linaro.org X-Google-Smtp-Source: AGHT+IE6YkuLdFm+MVLaoIBrNpMsQjAoheNZRJ/MC02S0G6dlYvzp8xqHaUvLJFkGF0BaRv175bt X-Received: by 2002:a05:6102:38c6:b0:4c1:7be4:eb61 with SMTP id ada2fe7eead31-4c6d3a15861mr4361490137.25.1743424316121; Mon, 31 Mar 2025 05:31:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1743424316; cv=none; d=google.com; s=arc-20240605; b=gp/fTJbuPJgBMSTzTGCGszIDr4iL6cAl6CaAUrio43Oafj3OoCWyA9J/ACJaslVf0/ 6IsMLpqmR+8ssrxRwcqQPb5hGa93G2OKnZBVJVAd/+dh95Jy0B+BwfXQBL8mVrqucpDA dlSDYFcowGGSxGbeX/HmogHzuPAQhadSA9oiL7NXuq1b04rm/R2szulaadjx5d1En7lm Bbm6Y/mLOvoDLbBp3QTQXnbHHMZSdMILuIfb7QxJGkQvCKh4zB8f8J4IF/KunbVJ3M25 wEYxU5iVO0kEmVmlXBiFYfLUgXLc+fVdwDDEhhhfUxZ8DK8quq1NImjUvYVLRyZdZ9zu Q/yg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=G4izsw6c41KId99iFvUt/6FPYmmeKcRDakIJ4Dk9JX8=; fh=I3NPwp3B+xRnH869KIfNIEcv5gtHwU29eQzLJ14T5oI=; b=MskHC5pMN19hmP52i0EQgnxM7Xet1iC5bbWD7etXix5PNu0FFXvvKu/VPdokA1BwG7 xDpI4JmA2ezw9SfFgqSyPvnEQ9EmBCUyupkA1mtHa4SarFqzsjA/eHdLznq1sMCo+O4A i03EmmuiuWmUZ0g2rivH47graTN4UqBv0H9cMWT6bwbJmv+F8Gb2lufLZEQZVE8YJMzy xYR+1OPuZn65+C6cqFgnreri7tsH9xI+sj9EDhk48YaDYtn+/sA/+V08fkywoMfEgAer wDO6fRPKzFY/RdeG8PRwF5QSYGNaIWrMAn9OnzjVF2b2HJ2VTdCyi6iPpCif0SeNpOe2 jmUQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Qp73UQcD; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id ada2fe7eead31-4c6bfe7ffdfsi1743785137.523.2025.03.31.05.31.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:31:56 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Qp73UQcD; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 174FE81F44; Mon, 31 Mar 2025 14:31:39 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="Qp73UQcD"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 6360981EDD; Mon, 31 Mar 2025 14:31:37 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 520D281E47 for ; Mon, 31 Mar 2025 14:31:35 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=jerome.forissier@linaro.org Received: by mail-wr1-x42f.google.com with SMTP id ffacd0b85a97d-39c1ef4acf2so260403f8f.0 for ; Mon, 31 Mar 2025 05:31:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1743424294; x=1744029094; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=G4izsw6c41KId99iFvUt/6FPYmmeKcRDakIJ4Dk9JX8=; b=Qp73UQcDZaAAjvIuc0za9+a7iukE6nDSEKMV9xACJNnbsSXqwC1H4c5pQj4tMze95x JyrWd95IjhcCUpHcNoke5q2gNyIO9RvLLevOBZGDJuh62j3X6VLM8M8901wszO9fpZWZ 3j4ahgimUQwRy4HK75MG/ESfJmUHADWik7BpL/IKQ8LH9MpErePCulrShUov71c8Vus0 NFW1vi1k4YzZAYvFA11PPor6tWWgjn5pkA8wUTHr+AkEmg6BhIDw/dB4HvMLqpY3padj eG5+ccU2S01YAxbJvAeIhqAWWry9jNzNs0Ndgh8dqKnfHMineUoAnIqTN/vu3xohu9wS 7ftA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743424294; x=1744029094; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=G4izsw6c41KId99iFvUt/6FPYmmeKcRDakIJ4Dk9JX8=; b=jp1myEIImFTyLYgexZ5fBJ+ZHDhg9LZ7+lp01hZ/JqG02CePL+yzYO2znCDUoaI+YH JwLOAHGW5mckuEZ6EySb07qTnBW1vzvKUD679ut5aKcehKG0E5fhfyw0pYmNSPqeiLpr MHODHixEyUsaKAVXDP/xFy7ozcM7gUoZ1cbl7RsJMYJ2dHkxiDzMOIlv/RZ/ytTJV4yQ dejmx7Z3Pr/GHHdVNXXQO7krIm+L+Qn9BSo3Lg0LjcJJNyFNMYpJBYFOgvE27aDwjQZS q1Ko8ZTBzI4YkhgeC8GObQ4LA+sADt2NdPS6ah3HfNuubM7dHbs9MAsOWkMt6zNaG0CH 2rVQ== X-Gm-Message-State: AOJu0YyQxsFB4Iig5wQJlZ5mVdakfhIFoi0coNTJE+zu1PiaOWpqmFBY LEh+A31PV/vzvdXTyzRCn6OfPWTwTcCW92V6Y6NA3bvgxXt21Ikx9KEDnV9ykqKatrFEUsZkkTT QEbM7iQ== X-Gm-Gg: ASbGncv/pn/46rsL1RmerGqGCWfTPLKtzqqep7ezRWZqNe+ASidhHQCbWGdiMmmkarz yWS0Ndg3yb9sNuOgF+Ln5oPVBhFXZYhqaNNSIKjfNbO3F5GB25IOSuyrcnNzP7XD29YsM2oxEVn 8XffLL2RlPN0K4BFPHqjDcJIv+HktTArme2PwQLDyHtPyhj/2Z11n8J/e0+msq4D2wbt19St+nj fdq2Eie6fN3m6liqCl2w8RMMnjSPuuacZauIJsDvG7StWIJmomOi8LXmp13NbE1uc998Uw+sWdV G4eDeP4X1xoT/rBNusfcFxYTH9LpUkXa6xppvvX/TjpxlcBiSTUA X-Received: by 2002:a05:6000:42c6:b0:391:2e97:5788 with SMTP id ffacd0b85a97d-39c1211ca8amr5871856f8f.55.1743424294612; Mon, 31 Mar 2025 05:31:34 -0700 (PDT) Received: from builder.. ([2a01:e0a:3cb:7bb0:f23c:25c9:dc74:be1]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-39c0b6589e4sm11337029f8f.10.2025.03.31.05.31.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:31:34 -0700 (PDT) From: Jerome Forissier To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Jerome Forissier , Tom Rini , Heinrich Schuchardt , Simon Glass , Dan Carpenter , Leo Yu-Chi Liang , Jiaxun Yang , Andrew Goodbody , Yu-Chien Peter Lin Subject: [PATCH v5 02/16] arm: add initjmp() Date: Mon, 31 Mar 2025 14:30:56 +0200 Message-ID: <20250331123120.2025062-3-jerome.forissier@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250331123120.2025062-1-jerome.forissier@linaro.org> References: <20250331123120.2025062-1-jerome.forissier@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Implement initjmp() for Arm. a non-standard extension to setjmp()/ longjmp() allowing to initialize a jump buffer with a function pointer and a stack pointer. This will be useful to later introduce threads. With this new function it becomes possible to longjmp() to a particular function pointer (rather than to a point previously reached during program execution as is the case with setjmp()), and with a custom stack. Both things are needed to spin off a new thread. Then the usual setjmp()/longjmp() pair is enough to save and restore a context, i.e., switch thread. Signed-off-by: Jerome Forissier --- arch/Kconfig | 1 + arch/arm/lib/setjmp.S | 12 ++++++++++++ arch/arm/lib/setjmp_aarch64.S | 10 ++++++++++ 3 files changed, 23 insertions(+) diff --git a/arch/Kconfig b/arch/Kconfig index 14111ca14fb..7a3141e92b3 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -95,6 +95,7 @@ config ARC config ARM bool "ARM architecture" select HAVE_SETJMP + select HAVE_INITJMP select ARCH_SUPPORTS_LTO select CREATE_ARCH_SYMLINK select HAVE_PRIVATE_LIBGCC if !ARM64 diff --git a/arch/arm/lib/setjmp.S b/arch/arm/lib/setjmp.S index 2f041aeef01..81bef578719 100644 --- a/arch/arm/lib/setjmp.S +++ b/arch/arm/lib/setjmp.S @@ -34,3 +34,15 @@ ENTRY(longjmp) ret lr ENDPROC(longjmp) .popsection + +.pushsection .text.initjmp, "ax" +ENTRY(initjmp) + stm a1, {v1-v8} + /* a2: entry point address, a3: stack base, a4: stack size */ + add a3, a3, a4 + str a3, [a1, #32] /* where setjmp would save sp */ + str a2, [a1, #36] /* where setjmp would save lr */ + mov a1, #0 + ret lr +ENDPROC(initjmp) +.popsection diff --git a/arch/arm/lib/setjmp_aarch64.S b/arch/arm/lib/setjmp_aarch64.S index 1b8d000eb48..01193ccc426 100644 --- a/arch/arm/lib/setjmp_aarch64.S +++ b/arch/arm/lib/setjmp_aarch64.S @@ -39,3 +39,13 @@ ENTRY(longjmp) ret ENDPROC(longjmp) .popsection + +.pushsection .text.initjmp, "ax" +ENTRY(initjmp) + /* x1: entry point address, x2: stack base, x3: stack size */ + add x2, x2, x3 + stp x1, x2, [x0,#88] + mov x0, #0 + ret +ENDPROC(initjmp) +.popsection From patchwork Mon Mar 31 12:30:57 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Forissier X-Patchwork-Id: 877285 Delivered-To: patch@linaro.org Received: by 2002:a5d:6dae:0:b0:38f:210b:807b with SMTP id u14csp1726075wrs; Mon, 31 Mar 2025 05:32:06 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWydXWPXQF2itV0wetAFy3o34RDVqHu7LMhrDSwBowygdC4e61SxHUBWw8gNdFlV9JNuVrKPw==@linaro.org X-Google-Smtp-Source: AGHT+IHdyHOFJx3pgfDf9H/9WsAxfStxqdCQqYGG+0LUXQD+MMmsbLSKQsRmoeDugrtz7vXG/KGT X-Received: by 2002:a05:6102:3e8a:b0:4c1:90ee:ab2 with SMTP id ada2fe7eead31-4c6d38cb31emr4610296137.14.1743424325847; Mon, 31 Mar 2025 05:32:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1743424325; cv=none; d=google.com; s=arc-20240605; b=AB2RQMJy6U/hiZADl2nRyLmUmBNxKdYOy0kLCTxhLdYI6Qxo+Xj/jp7oFB7qTq17qr shMR98lIEczxBemPWMcztPdi3wMEBPFAjQyuf2wYatprVm+cpY+sv1OHY0x2ma1fNvzl sirkbeUf4G4NSWa8pfKjEcPIeI418pEGQ+n1op0wrPxARs3vRmELNEvTXfIE8exxUm5R dmrzVHsj0AenH1Bep7Zm258m2b3TklKD/qYxEa+7d/ex8XntsIWL9bDVnLi+towza/j+ ksS6HlC4iSMiMD/bn2rzm+WTTWwePQ6ZrmUpkVzlBg8KsJMb9aqaPYYfFvPywkAeWtkP j3mQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=uwIiwSIVCNOG7AvHRdtrv6FKroYqnkWvT/sitGZCSNY=; fh=IocgoaOXRz58DyN4egcnVrwQQi33ckQfWMi+mIprZ6U=; b=CqX7mSjBVv3ox9OoNY5LY6BAZDuSNJ5p8ayLk5ov0b4h9zcaIDaTEi9rjqEOjjLj7D hA3V6+42vtxrP6DhA63aEirXcQE+P23BgjsXWVQ1RHPgiYac6Q46zxIatKfSKV2N0h0V aqWOuT1Xv7UCPGcwDOtlYZ6dhon0II0entI1fCuVN7UgRdJ8t/1/ZpETjoqa92F36Z/i Wxit0M/i8ipeaMxW2mBICCt/3v0MrjgUFYLQC/QhWiXL7ep3bZKZU0X4r2+WuqFfpcNT YeaaGesjGqv+CwHvrVbLfCGqQizzVJ0520OnHE/KnS/Di4prn+pavemyv7EfTZoEovU1 L0IQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=c+MuyaHk; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id ada2fe7eead31-4c6bfe00c6fsi1675361137.324.2025.03.31.05.32.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:32:05 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=c+MuyaHk; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 627FB81E47; Mon, 31 Mar 2025 14:31:41 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="c+MuyaHk"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 96D2D81F0D; Mon, 31 Mar 2025 14:31:38 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 902E181E6C for ; Mon, 31 Mar 2025 14:31:36 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=jerome.forissier@linaro.org Received: by mail-wm1-x334.google.com with SMTP id 5b1f17b1804b1-43cfb6e9031so40554955e9.0 for ; Mon, 31 Mar 2025 05:31:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1743424296; x=1744029096; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=uwIiwSIVCNOG7AvHRdtrv6FKroYqnkWvT/sitGZCSNY=; b=c+MuyaHk/VyIPlBRVaxD7zsd6jTL4pC4lJ8pE32h4dyY48rtKR9A/DqFYTGO4WidGl CjurU+SlbMxz8g3fRBLklod3u3goIBiBYnnEB9f7Mh+cZrMJHgPQu71zibxbfrBKWj1b s2bRxcl27sHk+QTT5S93sRtuNc0bz9W1E2OpH0CvEr4lJT7ZYSQIdZPw+PXnpGMJjIxP MxCr6QH2MrmgrpAMx5EhaT4w2rz5uEJPXEZiXgSuroMLsH7kegEmjLnNTXF+ZufVqoQS 1pNd+fqHswVOzMRA+XHZJcm542gj1KYfR+8IQIOWTDj/mSKYgA46c6sI4NNubO6SE3Rl RWkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743424296; x=1744029096; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=uwIiwSIVCNOG7AvHRdtrv6FKroYqnkWvT/sitGZCSNY=; b=t+4DOQnjQASx51mCku1fvoILVRnPI9cryaxZhhYz3uE9BTgcwf719UbRnTiDz9cmMl PptPybSYqKcjl37VCHyLrE0v6g2VJnBPDZIzOIHNlCnhg34IE1KjvX1ZkUZqZ+9YgMHL qvBStaxXJ2A14UjssWENjQoh9seTEq6Cvu/xeZJbYquEGIG9I2pyx9YGI+jLsKKyDyXH pqEsQv0X34Wshc8/ymU0HwwqNjga4/2Mju/63gGNqj9n5PSOtM339glO4AWDD4kny5Hw l4yQRp9OPoJlXEe5M568dzljHHYwYpcPxoWDoNN/Cd9o8IB8UqSxPJ99s2f5cPCmAxhu quog== X-Gm-Message-State: AOJu0YwOSW1pVQq30IqH6k7ErWpUEkN48D/MvHx7G1OlJrY9wTiL7DYV m4oyX7mbjEm0NY/+UPGHGbCWL1bh0ahT+oq/cgQTAKayZPbW6BGV+KKK5HapF36BbYQCCRU0eW9 aNPbZhg== X-Gm-Gg: ASbGncsxBLOLQhl0+pLBK8rETz0/T96z5OKQNipO7oHuaPDzHD9OKE/rq5CjZpHveKy puP/ybaTKwqrbUuVn29whfDstSrafjVeFLnskJ30GyEXX2kX7FM7jCUietFoh86Y9H8locu49/d U0NbLZJ+0c/jjFSOJZF1BHvW6kQQTzQ5ZtqTOhVBjoMfGkivsKwcT947EgYEw0CpL5/YnpOpH57 3DbQfwzgEKnfh0DlbuFxpDlBxRXeTb5wV6RGwM3PwBX7v0QaEL0jOobhiSpBDw5+XChay/dgV8d wbS1IejkKrnEROOoEBZt5nazfFx0jLB+vJ1O31Ghogz23hIRBve+ X-Received: by 2002:a05:600c:a03:b0:43d:94:cff0 with SMTP id 5b1f17b1804b1-43db62bc167mr70884545e9.19.1743424295767; Mon, 31 Mar 2025 05:31:35 -0700 (PDT) Received: from builder.. ([2a01:e0a:3cb:7bb0:f23c:25c9:dc74:be1]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-39c0b6589e4sm11337029f8f.10.2025.03.31.05.31.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:31:35 -0700 (PDT) From: Jerome Forissier To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Jerome Forissier , Tom Rini , Rick Chen , Leo , Simon Glass , Heinrich Schuchardt , Jiaxun Yang , Yu-Chien Peter Lin , Andrew Goodbody Subject: [PATCH v5 03/16] riscv: add initjmp() Date: Mon, 31 Mar 2025 14:30:57 +0200 Message-ID: <20250331123120.2025062-4-jerome.forissier@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250331123120.2025062-1-jerome.forissier@linaro.org> References: <20250331123120.2025062-1-jerome.forissier@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Implement initjmp() for RISC-V, a non-standard extension to setjmp()/ longjmp() allowing to initialize a jump buffer with a function pointer and a stack pointer. This will be useful to later introduce threads. With this new function it becomes possible to longjmp() to a particular function pointer (rather than to a point previously reached during program execution as is the case with setjmp()), and with a custom stack. Both things are needed to spin off a new thread. Then the usual setjmp()/longjmp() pair is enough to save and restore a context, i.e., switch thread. Signed-off-by: Jerome Forissier --- arch/Kconfig | 1 + arch/riscv/lib/setjmp.S | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/arch/Kconfig b/arch/Kconfig index 7a3141e92b3..aa60c5ff03c 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -153,6 +153,7 @@ config RISCV bool "RISC-V architecture" select CREATE_ARCH_SYMLINK select HAVE_SETJMP + select HAVE_INITJMP select SUPPORT_ACPI select SUPPORT_LITTLE_ENDIAN select SUPPORT_OF_CONTROL diff --git a/arch/riscv/lib/setjmp.S b/arch/riscv/lib/setjmp.S index 99d6195827e..9e1f3d5749b 100644 --- a/arch/riscv/lib/setjmp.S +++ b/arch/riscv/lib/setjmp.S @@ -59,3 +59,14 @@ ENTRY(longjmp) ret ENDPROC(longjmp) .popsection + +.pushsection .text.initjmp, "ax" +ENTRY(initjmp) + /* a1: entry point address, a2: stack base, a3: stack size */ + add a2, a2, a3 + STORE_IDX(a1, 12) + STORE_IDX(a2, 13) + li a0, 0 + ret +ENDPROC(initjmp) +.popsection From patchwork Mon Mar 31 12:30:58 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Forissier X-Patchwork-Id: 877286 Delivered-To: patch@linaro.org Received: by 2002:a5d:6dae:0:b0:38f:210b:807b with SMTP id u14csp1726128wrs; Mon, 31 Mar 2025 05:32:14 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXeGAiSjHoRmCLilKIx/FK6M8AApLpe0xWhwdlFQqM3lUsEPNdATlvtMdSBbhqqB2gn2BwGlA==@linaro.org X-Google-Smtp-Source: AGHT+IFWX/p4Fx31ufelxV1panWPRRl8FEuJS0m+RcsP3M4utHRPEoPUUj5rTpG8mtIksHyWn0zY X-Received: by 2002:a05:6102:160f:b0:4bb:de88:d027 with SMTP id ada2fe7eead31-4c6d38819c0mr5023719137.7.1743424334369; Mon, 31 Mar 2025 05:32:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1743424334; cv=none; d=google.com; s=arc-20240605; b=RyFQLZrlGyUCgvsAC24sbAgK/AxwXl5rp+fglMhDLF6PLdaH9BZAnt7xGLNZHhZXa7 mf+dL0aTmnLdeQBkcETTfRzIfHhCTMsNbARfF+lqcBuXTIxJrI0fXkMyrO+w7Srh/A3c ZGhxkTUBQ/mjPJPdEV5xthuxZHLHgSTP0xjC8dMpDqwuOYfbeV3+o80vPRTgP98LOCWL g7Fw0zWhe9IGlj52Hl2PWM2xMZ/9QuudCC8fh6L8VZXYgsV/Rr/IR25cT5EC+6hsmMFR v0Ukipr4sF8OuBp/k6SRw0owgpfJp5+bommOdjW4dd7LWe38NCYbyoqrgKTFEpNBMzuA ZvsA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=GERz7uJbPWQNt9gxwTX7plSa3IWAbbNTwGxUI2CBlZg=; fh=HaKHCOHL46OAAPpM3W6HDLkDmJxXWc+qggc/xnkS9u8=; b=NHLoSbPaEt4mBus1Q1KROFCtxoJpaGbS4hAVgvIRCkJV3H0TXTUbCXq9uTVXuR3FCA IHJj1uiMepuTnTnBdnJOw9+8aDiic3Jgl1XagTxqfWob5tn1QnJPeZ1JO4dR7pOyW+uS cLQ66O7VWH7hWteD+iI3ZrKyY0NfSbqOseAckou8BgMUZHANkeiv376iiXUe/gF9tNRx /+S40kW34uwaQmY9Lk7holZcM4jH5j80Z0OAL6VezGuNIwh/3JME7+qym5fgWBmJgc3/ IAB/d42kHmuKu50BFtZ+L1iZosXmm/FPe1ZK1GqRRj/ZOY4RnXRswrXiz9ZpFAgk+Ma0 PmSw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=J5UPVyiA; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id ada2fe7eead31-4c6bfea3a0esi1807619137.638.2025.03.31.05.32.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:32:14 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=J5UPVyiA; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id B814381F66; Mon, 31 Mar 2025 14:31:41 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="J5UPVyiA"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id A89E781703; Mon, 31 Mar 2025 14:31:40 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wr1-x444.google.com (mail-wr1-x444.google.com [IPv6:2a00:1450:4864:20::444]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 1112881E47 for ; Mon, 31 Mar 2025 14:31:38 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=jerome.forissier@linaro.org Received: by mail-wr1-x444.google.com with SMTP id ffacd0b85a97d-3913d129c1aso3238047f8f.0 for ; Mon, 31 Mar 2025 05:31:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1743424297; x=1744029097; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=GERz7uJbPWQNt9gxwTX7plSa3IWAbbNTwGxUI2CBlZg=; b=J5UPVyiAMhXJSMsEPpmiXXaCXEwpehvdE22epeJZQQa3W9tFoUrmWA6M1hl0fYTP6z bBZzospeCAKHv+xJ5aqwp1eEeIOHrYt6aRCHJYAfjoLpN9Z4TVC5JMlf0g48wMl0GON8 QpETHeAF+TdGb3boUCmH9BECyylkIEkIgbF0ZZuV/GShM8K5JmWqtphsESNRHm+c0I85 9F/jmC+uCUayDJCXGdInptH1mHxf5TaHvFkXseeAp2nbcoXqNFMDY0aLKqOSxE3syCQ+ 5lWUXjcKBb7OmUXPevrlxek7ZSXmDS5HVYHSXJEq8BhtXY7h9Ntm4prUil6V2juIspAq kGUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743424297; x=1744029097; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GERz7uJbPWQNt9gxwTX7plSa3IWAbbNTwGxUI2CBlZg=; b=Os83RJFsZev1mwI1USIHGS3SFtWBv8INcSD9vjhgJjmkq7dTTg347II/ihmxGXdFg0 TmHjgYBS1/szKJ2kkJyN2CLgzIhDQh72aewftndUL04zELsgY50HFIaUqo0g9c+A/geO pCT7nDPFQdMDWm7j6+DkEcOwrkcY+VeWL4MNWQiVlKXS5bSozRwy/o0LKKio8W7Cnsyl /rlp9Y8QbIeMm7jreJTC+Omnu6zH1jJmA+rjHfPrY77gO3nL0LVeEKhc0LwhhfpZ4B0n bclP4RFoiQOD8sGhS0jBLu/JbS/g2yC7VCmo8VFg+5Br/6ejYL6Ml/rpgvfof2H0B8gZ TYmA== X-Gm-Message-State: AOJu0YxJ2zNLVaRbs0h1mTa4gm34DwtQchqMNA1yi9plOJFSTZfAUVfl DNSBrKn47Nzl61a1NOaMyDtDczQLi7AreIQEDgQvzqNd/9Hh/f1EO70jfzK97bSU38zJdz72f2O KEvFZ9n2s X-Gm-Gg: ASbGncsvFO8b42G1OsAQtayy+/ieU3PIwMgA1CQHWuMRaa3297KaY90zbx2xxUhHjHF hc/2dZs+tjqUvEZkOq3KrKweDApwEa92E6bb2get6N1j+Vr8HBTks8fr+RkZIYMccpf7fnWlGky AOdrVxwrLTSirWWnJtuNorknCxyX7xla8xoltDeaDwSZG4A9b0q18GhY7aiyuLySarQG9n+abha jqgRKGuqbrygNi1diw9uPi32s+z60SkfANXXHiE9eFD8LKBo0FzFTq15AlvSFKwiDzr/IdHIjs4 n3EsXHca7hJaNC4H3zsjhF1rnpmiAV5iF+Cs86ycVnhfgmMyA9il X-Received: by 2002:a05:6000:1acc:b0:38f:4ffd:c757 with SMTP id ffacd0b85a97d-39c11b76660mr7348859f8f.2.1743424297212; Mon, 31 Mar 2025 05:31:37 -0700 (PDT) Received: from builder.. ([2a01:e0a:3cb:7bb0:f23c:25c9:dc74:be1]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-39c0b6589e4sm11337029f8f.10.2025.03.31.05.31.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:31:36 -0700 (PDT) From: Jerome Forissier To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Jerome Forissier , Simon Glass , Tom Rini Subject: [PATCH v5 04/16] sandbox: add initjmp() Date: Mon, 31 Mar 2025 14:30:58 +0200 Message-ID: <20250331123120.2025062-5-jerome.forissier@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250331123120.2025062-1-jerome.forissier@linaro.org> References: <20250331123120.2025062-1-jerome.forissier@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Add initjm[() to sandbox, a non-standard extension to setjmp()/ longjmp() allowing to initialize a jump buffer with a function pointer and a stack pointer. This will be useful to later introduce threads. With this new function it becomes possible to longjmp() to a particular function pointer (rather than to a point previously reached during program execution as is the case with setjmp()), and with a custom stack. Both things are needed to spin off a new thread. Then the usual setjmp()/longjmp() pair is enough to save and restore a context, i.e., switch thread. The implementation is taken verbatim from barebox [1] with the exception of the additional stack_sz argument. It is quite complex because contrary to U-Boot platform code we don't know how the system's C library implements the jump buffer, so we can't just write the function and stack pointers into it. [1] https://github.com/barebox/barebox/blob/b2a15c383ddc/arch/sandbox/os/setjmp.c Signed-off-by: Jerome Forissier --- arch/sandbox/cpu/Makefile | 11 ++- arch/sandbox/cpu/initjmp.c | 175 +++++++++++++++++++++++++++++++++++++ 2 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 arch/sandbox/cpu/initjmp.c diff --git a/arch/sandbox/cpu/Makefile b/arch/sandbox/cpu/Makefile index bfcdc335d32..038ad78accc 100644 --- a/arch/sandbox/cpu/Makefile +++ b/arch/sandbox/cpu/Makefile @@ -5,7 +5,7 @@ # (C) Copyright 2000-2003 # Wolfgang Denk, DENX Software Engineering, wd@denx.de. -obj-y := cache.o cpu.o state.o +obj-y := cache.o cpu.o state.o initjmp.o extra-y := start.o os.o extra-$(CONFIG_SANDBOX_SDL) += sdl.o obj-$(CONFIG_XPL_BUILD) += spl.o @@ -29,6 +29,15 @@ cmd_cc_eth-raw-os.o = $(CC) $(filter-out -nostdinc, \ $(obj)/eth-raw-os.o: $(src)/eth-raw-os.c FORCE $(call if_changed_dep,cc_eth-raw-os.o) +# initjmp.c is build in the system environment, so needs standard includes +# CFLAGS_REMOVE_initjmp.o cannot be used to drop header include path +quiet_cmd_cc_initjmp.o = CC $(quiet_modtag) $@ +cmd_cc_initjmp.o = $(CC) $(filter-out -nostdinc, \ + $(patsubst -I%,-idirafter%,$(c_flags))) -c -o $@ $< + +$(obj)/initjmp.o: $(src)/initjmp.c FORCE + $(call if_changed_dep,cc_initjmp.o) + # sdl.c fails to build with -fshort-wchar using musl cmd_cc_sdl.o = $(CC) $(filter-out -nostdinc -fshort-wchar, \ $(patsubst -I%,-idirafter%,$(c_flags))) -fno-lto -c -o $@ $< diff --git a/arch/sandbox/cpu/initjmp.c b/arch/sandbox/cpu/initjmp.c new file mode 100644 index 00000000000..6e72d32cb4b --- /dev/null +++ b/arch/sandbox/cpu/initjmp.c @@ -0,0 +1,175 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later +/* + * An implementation of initjmp() in C, that plays well with the system's + * setjmp() and longjmp() functions. + * Taken verbatim from arch/sandbox/os/setjmp.c in the barebox project. + * Modified so that initjmp() accepts a stack_size argument. + * + * Copyright (C) 2006 Anthony Liguori + * Copyright (C) 2011 Kevin Wolf + * Copyright (C) 2012 Alex Barcelo + * Copyright (C) 2021 Ahmad Fatoum, Pengutronix + * Copyright (C) 2025 Linaro Ltd. + * This file is partly based on pth_mctx.c, from the GNU Portable Threads + * Copyright (c) 1999-2006 Ralf S. Engelschall + */ + +/* XXX Is there a nicer way to disable glibc's stack check for longjmp? */ +#ifdef _FORTIFY_SOURCE +#undef _FORTIFY_SOURCE +#endif + +#include +#include +#include +#include +#include + +typedef sigjmp_buf _jmp_buf __attribute__((aligned((16)))); +_Static_assert(sizeof(_jmp_buf) <= 512, "sigjmp_buf size exceeds expectation"); + +/* + * Information for the signal handler (trampoline) + */ +static struct { + _jmp_buf *reenter; + void (*entry)(void); + volatile sig_atomic_t called; +} tr_state; + +/* + * "boot" function + * This is what starts the coroutine, is called from the trampoline + * (from the signal handler when it is not signal handling, read ahead + * for more information). + */ +static void __attribute__((noinline, noreturn)) +coroutine_bootstrap(void (*entry)(void)) +{ + for (;;) + entry(); +} + +/* + * This is used as the signal handler. This is called with the brand new stack + * (thanks to sigaltstack). We have to return, given that this is a signal + * handler and the sigmask and some other things are changed. + */ +static void coroutine_trampoline(int signal) +{ + /* Get the thread specific information */ + tr_state.called = 1; + + /* + * Here we have to do a bit of a ping pong between the caller, given that + * this is a signal handler and we have to do a return "soon". Then the + * caller can reestablish everything and do a siglongjmp here again. + */ + if (!sigsetjmp(*tr_state.reenter, 0)) { + return; + } + + /* + * Ok, the caller has siglongjmp'ed back to us, so now prepare + * us for the real machine state switching. We have to jump + * into another function here to get a new stack context for + * the auto variables (which have to be auto-variables + * because the start of the thread happens later). Else with + * PIC (i.e. Position Independent Code which is used when PTH + * is built as a shared library) most platforms would + * horrible core dump as experience showed. + */ + coroutine_bootstrap(tr_state.entry); +} + +int __attribute__((weak)) initjmp(_jmp_buf jmp, void (*func)(void), + void *stack_base, size_t stack_size) +{ + struct sigaction sa; + struct sigaction osa; + stack_t ss; + stack_t oss; + sigset_t sigs; + sigset_t osigs; + + /* The way to manipulate stack is with the sigaltstack function. We + * prepare a stack, with it delivering a signal to ourselves and then + * put sigsetjmp/siglongjmp where needed. + * This has been done keeping coroutine-ucontext (from the QEMU project) + * as a model and with the pth ideas (GNU Portable Threads). + * See coroutine-ucontext for the basics of the coroutines and see + * pth_mctx.c (from the pth project) for the + * sigaltstack way of manipulating stacks. + */ + + tr_state.entry = func; + tr_state.reenter = (void *)jmp; + + /* + * Preserve the SIGUSR2 signal state, block SIGUSR2, + * and establish our signal handler. The signal will + * later transfer control onto the signal stack. + */ + sigemptyset(&sigs); + sigaddset(&sigs, SIGUSR2); + pthread_sigmask(SIG_BLOCK, &sigs, &osigs); + sa.sa_handler = coroutine_trampoline; + sigfillset(&sa.sa_mask); + sa.sa_flags = SA_ONSTACK; + if (sigaction(SIGUSR2, &sa, &osa) != 0) { + return -1; + } + + /* + * Set the new stack. + */ + ss.ss_sp = stack_base; + ss.ss_size = stack_size; + ss.ss_flags = 0; + if (sigaltstack(&ss, &oss) < 0) { + return -1; + } + + /* + * Now transfer control onto the signal stack and set it up. + * It will return immediately via "return" after the sigsetjmp() + * was performed. Be careful here with race conditions. The + * signal can be delivered the first time sigsuspend() is + * called. + */ + tr_state.called = 0; + pthread_kill(pthread_self(), SIGUSR2); + sigfillset(&sigs); + sigdelset(&sigs, SIGUSR2); + while (!tr_state.called) { + sigsuspend(&sigs); + } + + /* + * Inform the system that we are back off the signal stack by + * removing the alternative signal stack. Be careful here: It + * first has to be disabled, before it can be removed. + */ + sigaltstack(NULL, &ss); + ss.ss_flags = SS_DISABLE; + if (sigaltstack(&ss, NULL) < 0) { + return -1; + } + sigaltstack(NULL, &ss); + if (!(oss.ss_flags & SS_DISABLE)) { + sigaltstack(&oss, NULL); + } + + /* + * Restore the old SIGUSR2 signal handler and mask + */ + sigaction(SIGUSR2, &osa, NULL); + pthread_sigmask(SIG_SETMASK, &osigs, NULL); + + /* + * jmp can now be used to enter the trampoline again, but not as a + * signal handler. Instead it's longjmp'd to directly. + */ + return 0; +} + From patchwork Mon Mar 31 12:30:59 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Forissier X-Patchwork-Id: 877287 Delivered-To: patch@linaro.org Received: by 2002:a5d:6dae:0:b0:38f:210b:807b with SMTP id u14csp1726215wrs; Mon, 31 Mar 2025 05:32:24 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXTxP5LXdfKR/66StvV2/Yxkb18CxVW/5DLJzLKPfqsVeFBd6jlDW+iwePuYwpErCUMsykfWw==@linaro.org X-Google-Smtp-Source: AGHT+IEVSQ3gBVugB1WItVDMMwWLOngqVGQ0r7Ud54wk4Ili1+kGDpINp2/oXQmL1PDEBSV2XAwR X-Received: by 2002:a05:6102:3e8a:b0:4c3:6ba1:3aac with SMTP id ada2fe7eead31-4c6d3837bcdmr5381852137.3.1743424343820; Mon, 31 Mar 2025 05:32:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1743424343; cv=none; d=google.com; s=arc-20240605; b=chQl//zXh34GEtuMZYGk4M4aR9xMMH0QxCUT7DhuwZhE83iA+1NCpbTx6B5ANb/9In 1Yw51d59sJalx7/rjKbU9PnnYau13My0e4mY0DQ67Alx2VmbBp8P16iYASIvAEguUp23 Imd9SNiz0/cMakf9gBO9aTM4Hi0FHszsnGXCMnRhV2e0desjfPYM7y7uacavfoEclKwQ p78f/NyCF2jWWGx/Tsg9WlI4gG3MleoLy50Mt2HuOL1G7Lp1AF1rWxxyOfLFaH519kNb JdCObbZhtMpRhazUJusS9u4r+c8v0U5IvDtxQP1DAOWSkjDngPCJJQOkOkW3WWeX2CyE gkPA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=9Wr0962nKSa8gzKuTD03RUSdUdB+lmJXD5dXPdk4D/w=; fh=5u8mhcT/cvD7c0jneSWBm/018GsimgfQmc5aiiIZNZM=; b=AinjeZZIrNUAN6gZHl+oweXGsQalGmpIbJC8KkbnIsm+lYH2oCBSRxg6iS9XjNqMhK 5aNpwo17IlPLC68OK5rDBb1qifkIhLOk08xP4EYVhJYW6fpiNdToE9qpMjwW0GWXvWj+ ZipL+M1ypfWVN5B6P6uEL5rM3ySDCl6gVPZ5VBaXAKrX19KVtMoNPUDsNI2uXHjQbKsS 9adG5Bn5F61QF25rf1/D3G3sUGZ/2qM/H0Z1AsCGAzV0Ln45GkwszehPSCMYsq5DaIPb bcUrjwmqRHXw21jNYSsiHB8m1Qx0xIA8wHmtAGNRIx5IJbi0sBUgpvHD/N+3/k/WbxYt JJSQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=bEM4QlRk; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id ada2fe7eead31-4c6bfc8a877si1608960137.78.2025.03.31.05.32.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:32:23 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=bEM4QlRk; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 17477819D2; Mon, 31 Mar 2025 14:31:44 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="bEM4QlRk"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id B566081F59; Mon, 31 Mar 2025 14:31:41 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id A736181F59 for ; Mon, 31 Mar 2025 14:31:39 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=jerome.forissier@linaro.org Received: by mail-wm1-x333.google.com with SMTP id 5b1f17b1804b1-43cfdc2c8c9so24755555e9.2 for ; Mon, 31 Mar 2025 05:31:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1743424299; x=1744029099; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=9Wr0962nKSa8gzKuTD03RUSdUdB+lmJXD5dXPdk4D/w=; b=bEM4QlRkef45rRMmtLNxmfYKsiqvE2KpI1tIqtseuJ5ArD8bT0hdBXultNk3PVjRPz uUg2BgpDPj0YHAcqWuhBwAHUuqZMVA2pxh2GNA8xjnsxg7ISRG3MatE4ymiD2uOAYDJ3 pOiFSowmZhQYm8pzmPbO+NIhMH3pfmArEQXeaaxg+wqlqL8XFAt8vE43P8nkW6yUke1Z hpwKIO/p3dBFBUAE00vjOGJvDWto+FfnfkoOaz+kE8Tm95elbCEnAFmEEF+RYzUDLMbm euxrdLecxC3V6HevHB/zAl5MF9lR+Iyrn11gxzwVDmFXE9wBfM04j5pFG4uEPSgg1Hfg nIYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743424299; x=1744029099; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9Wr0962nKSa8gzKuTD03RUSdUdB+lmJXD5dXPdk4D/w=; b=DVLdNsXOBWpdjbvd8aPOe+y1EzNc5rDUDCJo6uT6CeeuUbPxPwKQgDsAXm0XxLdejL MQP6WcwYY1wkc42Qwee+5SJsdW6kvIbjIP7LbfqV5UkCCSlN5QDPWQfCItLqLDoHFMTM CPW12rB650ERnyT/otJtCXMR561mOWHZbn4PpS+j4CP/uLdWKhy41eJPkzARfq7H+/2M OcQc8ITROwEII4evKqlWtJrKr5Mh6iK+dXTPWbasdmX5vlJeZJcq6zjkJJSBwmES1cgU ElxfiYRFW5uIEfyObRmBiU5y6Smi/W3Nqjc0iUY7XdzmEBxe0Vlf7TxGtX3akEpsRCro luJQ== X-Gm-Message-State: AOJu0YzLPNffRtoNmaKcD1Elv1EoWsPFwnOY4jvhitmAYB18op9NK9in J8kMDkKtHeUGs/cO5qLawPFO07PvRpqR7SVQ80BjWlaZWsCupKkAF3CRRp633nJ5CTwWPIrS892 XvnStcg== X-Gm-Gg: ASbGnctS9c6wuwhqkNQeJcvTXTzUyKW40cD2BiBXnU3c7lirUYiRKivTpOe8d6j2hd9 7/rgp58Ho0EM9jLJVThEfsgbRrsu57ADgZXArD6VXFoCmqDJu5U7yuyWeMlWhLQ/BLa0ZDF9shO 98OpZ8LTjW794KMX/Iu7tY/Ons866y8JzXD7KR7+1sCO7nMTnTtlRJ39aRyXPgj39NwE6yoHFEs QL0ru9LHuRLPrGxYplSG/Eh/R76QB4D/blJr+dJnajXlGVQA0q8Uuc0pLSk74jWLPrbvwtpQvGL 6QGJ+jB5OfkhsaqS716buHj/r4ce3JMc/mronJkiPui8GockYliM X-Received: by 2002:a05:600c:c16:b0:43d:24d:bbe2 with SMTP id 5b1f17b1804b1-43db8524b1bmr62436275e9.28.1743424298927; Mon, 31 Mar 2025 05:31:38 -0700 (PDT) Received: from builder.. ([2a01:e0a:3cb:7bb0:f23c:25c9:dc74:be1]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-39c0b6589e4sm11337029f8f.10.2025.03.31.05.31.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:31:38 -0700 (PDT) From: Jerome Forissier To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Jerome Forissier , Tom Rini , Simon Glass , Heinrich Schuchardt , Philippe Reynes , Raymond Mao Subject: [PATCH v5 05/16] test: lib: add initjmp() test Date: Mon, 31 Mar 2025 14:30:59 +0200 Message-ID: <20250331123120.2025062-6-jerome.forissier@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250331123120.2025062-1-jerome.forissier@linaro.org> References: <20250331123120.2025062-1-jerome.forissier@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Test the initjmp() function when HAVE_INITJMP is set. Use the test as an example in the API documentation. Signed-off-by: Jerome Forissier Reviewed-by: Ilias Apalodimas --- doc/api/setjmp.rst | 10 +++++++ test/lib/Makefile | 1 + test/lib/initjmp.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 test/lib/initjmp.c diff --git a/doc/api/setjmp.rst b/doc/api/setjmp.rst index 7718047085c..c48b2e3e096 100644 --- a/doc/api/setjmp.rst +++ b/doc/api/setjmp.rst @@ -8,3 +8,13 @@ Long jump API .. kernel-doc:: include/setjmp.h :internal: + +Example +------- + +Here is an example showing how to use the a long jump functions and +initjmp() in particular: + +.. literalinclude:: ../../test/lib/initjmp.c + :language: c + :linenos: diff --git a/test/lib/Makefile b/test/lib/Makefile index 0e4cb8e3dfd..bf04685dae1 100644 --- a/test/lib/Makefile +++ b/test/lib/Makefile @@ -14,6 +14,7 @@ obj-y += hexdump.o obj-$(CONFIG_SANDBOX) += kconfig.o obj-y += lmb.o obj-$(CONFIG_HAVE_SETJMP) += longjmp.o +obj-$(CONFIG_HAVE_INITJMP) += initjmp.o obj-$(CONFIG_CONSOLE_RECORD) += test_print.o obj-$(CONFIG_SSCANF) += sscanf.o obj-$(CONFIG_$(PHASE_)STRTO) += str.o diff --git a/test/lib/initjmp.c b/test/lib/initjmp.c new file mode 100644 index 00000000000..5b4b50b3f0f --- /dev/null +++ b/test/lib/initjmp.c @@ -0,0 +1,73 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright 2025 Linaro Limited + * + * Unit test for initjmp() + */ + +#include +#include +#include +#include +#include +#include + +static bool ep_entered; +static jmp_buf return_buf; + +static void __noreturn entrypoint(void) +{ + ep_entered = true; + + /* Jump back to the main routine */ + longjmp(return_buf, 1); + + /* Not reached */ + panic("longjmp failed\n"); +} + +static int lib_initjmp(struct unit_test_state *uts) +{ + int ret; + void *stack; + jmp_buf buf; + /* Arbitrary but smaller values (< page size?) fail on SANDBOX */ + size_t stack_sz = 8192; + + (void)entrypoint; + + ep_entered = false; + + stack = malloc(stack_sz); + ut_assertnonnull(stack); + + /* + * Prepare return_buf so that entrypoint may jump back just after the + * if() + */ + if (!setjmp(return_buf)) { + /* return_buf initialized, entrypoint not yet called */ + + /* + * Prepare another jump buffer to jump into entrypoint with the + * given stack + */ + ret = initjmp(buf, entrypoint, stack, stack_sz); + ut_assertok(ret); + + /* Jump into entrypoint */ + longjmp(buf, 1); + /* + * Not reached since entrypoint is expected to branch after + * the if() + */ + ut_assert(false); + } + + ut_assert(ep_entered); + + free(stack); + + return 0; +} +LIB_TEST(lib_initjmp, 0); From patchwork Mon Mar 31 12:31:00 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Forissier X-Patchwork-Id: 877289 Delivered-To: patch@linaro.org Received: by 2002:a5d:6dae:0:b0:38f:210b:807b with SMTP id u14csp1726332wrs; Mon, 31 Mar 2025 05:32:42 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUCva+GUY6f2dDb1bFRVB2eRAXt1+/YzuM6Tgh53bO6fQI4ntDk5A8R0qiMKqtQaFo0Z5jPbw==@linaro.org X-Google-Smtp-Source: AGHT+IFpnCMy4n2ySqK8jy9kfTnb80ljwylcDgRDagQ7Zch0RDtIyG/0CWz2WP52VPWGD+YAwn8K X-Received: by 2002:a05:6102:2c8d:b0:4c3:346:29ef with SMTP id ada2fe7eead31-4c6d396f046mr4653612137.11.1743424361755; Mon, 31 Mar 2025 05:32:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1743424361; cv=none; d=google.com; s=arc-20240605; b=I6u6lw6p7UjWNSGpBg6Er6YMTyP0xuTq27LH0CAih3ukvE87Qqq1ektd5CGe8jNagr g10K9N5esYfKjTQvd2y9BDvcuA19VezTvTAsd7vMp4UulSbxuIrFpPccJaFVQ3zpSr7i c+FYB/wyK/qzVLXXgPSO3Sxvc29Bs9eAwyGR4uBcmUh+yDgoUWuxohY3oZyN5YXfX2Fm OMQ3LNOTH2urFtEkStlGfdDLqGLLc+Y5JEPnUVZyW9OzEXzWELd1CwZ/zxcU6GpQfYRB XPiJ/zN8I6cuQ/HMkpcBkCS4+QfzKRAG3IqxF7Gh2Yz6l72H1lbPqLw/QYL+JKfBWBcm gsAA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=+tmM38K8tIdiO7TPc0HndOjbZ2fOYyIuvmRuc6vMa/c=; fh=xrCYZkcx4cympqMWrXj7dIw2+5iF+RbFZOPwYjVYz7o=; b=MREKeDKIx99W+x8qFU7mzbxqexLMP0ic7nDB0WNExg1o38xWlcyfvnvpkAQkmA04zr D9CyyBwTA+PN4sw4KaYIrY2Ja5SvGHbxEU7JX1X94bX2m/eikU2VMjqyfSOEcJ94OQge LJY3zyNHhB/VvhUaCwP+74vPsgW8a4ef0IFzQYmmIbHsm9QNlBkPSsshmMWN+8dmLjrs Iwq3QU4Lfct+s3Yt1qvHuw6G1NLit6GIIgm7Z7AlbQtfo699uIjRw40FUSfXJAngAP/F Pc9wsunpjz78YqlzhwlQUCNLh8LrVS56P5pjL4qCU+knjfc79ilWJPsOMoV1/0ogMURV tehw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=JhS4Wv7T; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id ada2fe7eead31-4c6bfe3fd6fsi1722744137.355.2025.03.31.05.32.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:32:41 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=JhS4Wv7T; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id A7AC781FC8; Mon, 31 Mar 2025 14:31:47 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="JhS4Wv7T"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 847E081E6C; Mon, 31 Mar 2025 14:31:46 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wm1-x344.google.com (mail-wm1-x344.google.com [IPv6:2a00:1450:4864:20::344]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 6B7E082008 for ; Mon, 31 Mar 2025 14:31:42 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=jerome.forissier@linaro.org Received: by mail-wm1-x344.google.com with SMTP id 5b1f17b1804b1-43cfb6e9031so40555875e9.0 for ; Mon, 31 Mar 2025 05:31:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1743424302; x=1744029102; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+tmM38K8tIdiO7TPc0HndOjbZ2fOYyIuvmRuc6vMa/c=; b=JhS4Wv7TWG+tO8OQXmz26wxytB5j38dxOf5nO+QnanP/W7BQRHmQNLtyjXomtnPRKW asW/kLEMG2gY/INW7D6kQywQL0mQhawQhkb8HVGtEXjGc2nrXNhsaFrFebq5jGuLs+HE VVh7BJe8roPvUZm/OU8CxQ4aGAex0I7YJegD1cGEU/GQMc9Z0qgukwmusmjuPo4l7aMW jmSDZ/aQQE+GEhIVXcU+ucl93EW4GViTWzTkiT5em876hrEiK0ZStHcjxcBdb341YLml kIISdjofJR9GBOaIGTQ1jpAw8i2iYdXOA8KcezG/tNIEETtyJcPZ83toqwzDRtvA0XkA aAQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743424302; x=1744029102; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+tmM38K8tIdiO7TPc0HndOjbZ2fOYyIuvmRuc6vMa/c=; b=AljdEDW/X2b1mtJ7wsQhc4MgHmCk+H3MgGo+JQ1MV/14Sr93TXoV14hp7P7UW/pL1G elO/5L7GoN13b110ONo35VM5u4CVVY1TyOHsenQyj8Kk2O5s7vpeCxdSMGHyfL4uZFo/ g6mZtS729qcUaOmdtlUNtUwDSed2m7N1jS/MQ17cTM5cAdRaIGvl08AFMq/yAGBQV3EP J2I4ptCQLefHFILkvgbtg3/OUjLpZQgrkCbc7RuFMvarvPhHSUNPvEBPF/sjbrW33zgH 4hRv9Hdi2gzKD6coQky7ek7+Megfrl9hLnlu3Dxdr5loXqilT1BkVSn6xxWa3KxGodmh M7CA== X-Gm-Message-State: AOJu0Yxf8MWU41MF3feoFGUEayJNNFcaCBT1gjpGsVyNFbsZlaftfW7y YyjZhhevtRY9qiHwoC3f4Jm4c+WXQzfjlPxN8yvaOWyqO/CZ0dRfo/lKWjgdonMUvJ1MEdbJ5OB xBstlwYxz X-Gm-Gg: ASbGncsdznMHnnVzE9FC77SvAMIl7DL6++A0QTUMFt+DsrumXwXTn/uAgfMUNyzljRz gZsxNs5AQmCOm/AL7HcivmBhKM+EwhT7Ll6m9KvrMaj4Teuyitex05sCqxjWIvTyznlFETmBsOs TUp8uo9B+FiT1SJlPPSUFGjoICOoOQ16vAQrUoLj7n3u1g8ogVV3VJX0BVpqU1jA+xI8VQbv2ZB r+5TzZo8X7SBQUAqfZxtk4ywlUvyRfkxHoXf0WkJ5CzkT5EfDP9BnorqUXA4OldtGgapVr8x0wp k4suc5aWY4mi2Rv/7CukjTxmyb9ce18DkAAlTGDRs7pFcliTkkc3 X-Received: by 2002:a05:600c:4f4f:b0:43c:fcbc:968c with SMTP id 5b1f17b1804b1-43db6228823mr76309965e9.7.1743424301533; Mon, 31 Mar 2025 05:31:41 -0700 (PDT) Received: from builder.. ([2a01:e0a:3cb:7bb0:f23c:25c9:dc74:be1]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-39c0b6589e4sm11337029f8f.10.2025.03.31.05.31.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:31:41 -0700 (PDT) From: Jerome Forissier To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Jerome Forissier , Tom Rini , Christian Marangi , Heinrich Schuchardt , Vasileios Amoiridis , Simon Glass , Sughosh Ganu , Raymond Mao , Michal Simek , Patrick Rudolph , Mattijs Korpershoek Subject: [PATCH v5 06/16] uthread: add cooperative multi-tasking interface Date: Mon, 31 Mar 2025 14:31:00 +0200 Message-ID: <20250331123120.2025062-7-jerome.forissier@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250331123120.2025062-1-jerome.forissier@linaro.org> References: <20250331123120.2025062-1-jerome.forissier@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Add a new internal API called uthread (Kconfig symbol: UTHREAD) which provides cooperative multi-tasking. The goal is to be able to improve the performance of some parts of U-Boot by overlapping lengthy operations, and also implement background jobs in the U-Boot shell. Each uthread has its own stack allocated on the heap. The default stack size is defined by the UTHREAD_STACK_SIZE symbol and is used when uthread_create() receives zero for the stack_sz argument. The implementation is based on context-switching via initjmp()/setjmp()/ longjmp() and is inspired from barebox threads [1]. A notion of thread group helps with dependencies, such as when a thread needs to block until a number of other threads have returned. The name "uthread" comes from "user-space threads" because the scheduling happens with no help from a higher privileged mode, contrary to more complex models where kernel threads are defined. But the 'u' may as well stand for 'U-Boot' since the bootloader may actually be running at any privilege level and the notion of user vs. kernel may not make much sense in this context. [1] https://github.com/barebox/barebox/blob/master/common/bthread.c Signed-off-by: Jerome Forissier Reviewed-by: Ilias Apalodimas --- doc/api/index.rst | 1 + doc/api/uthread.rst | 7 +++ include/uthread.h | 123 +++++++++++++++++++++++++++++++++++++++ lib/Kconfig | 21 +++++++ lib/Makefile | 2 + lib/uthread.c | 139 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 293 insertions(+) create mode 100644 doc/api/uthread.rst create mode 100644 include/uthread.h create mode 100644 lib/uthread.c diff --git a/doc/api/index.rst b/doc/api/index.rst index 0dc9ad45d41..506843ed74a 100644 --- a/doc/api/index.rst +++ b/doc/api/index.rst @@ -29,3 +29,4 @@ U-Boot API documentation sysreset timer unicode + uthread diff --git a/doc/api/uthread.rst b/doc/api/uthread.rst new file mode 100644 index 00000000000..21233ff6b22 --- /dev/null +++ b/doc/api/uthread.rst @@ -0,0 +1,7 @@ +.. SPDX-License-Identifier: GPL-2.0-or-later + +Uthread API +=========== + +.. kernel-doc:: include/uthread.h + :internal: diff --git a/include/uthread.h b/include/uthread.h new file mode 100644 index 00000000000..ec3034c097a --- /dev/null +++ b/include/uthread.h @@ -0,0 +1,123 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* + * Copyright 2025 Linaro Limited + */ + +#include +#include +#include + +#ifndef _UTHREAD_H_ +#define _UTHREAD_H_ + +/** + * DOC: Overview + * + * The uthread framework is a basic task scheduler that allows to run functions + * "in parallel" on a single CPU core. The scheduling is cooperative, not + * preemptive -- meaning that context switches from one task to another task is + * voluntary, via a call to uthread_schedule(). This characteristic makes thread + * synchronization much easier, because a thread cannot be interrupted in the + * middle of a critical section (reading from or writing to shared state, for + * instance). + * + * CONFIG_UTHREAD in lib/Kconfig enables the uthread framework. When disabled, + * the uthread_create() and uthread_schedule() functions may still be used so + * that code differences between uthreads enabled and disabled can be reduced to + * a minimum. + */ + +/** + * struct uthread - a thread object + * + * @fn: thread entry point + * @arg: argument passed to the entry point when the thread is started + * @ctx: context to resume execution of this thread (via longjmp()) + * @stack: initial stack pointer for the thread + * @done: true once @fn has returned, false otherwise + * @grp_id: user-supplied identifier for this thread and possibly others. A + * thread can belong to zero or one group (not more), and a group may contain + * any number of threads. + * @list: link in the global scheduler list + */ +struct uthread { + void (*fn)(void *); + void *arg; + jmp_buf ctx; + void *stack; + bool done; + unsigned int grp_id; + struct list_head list; +}; + +#ifdef CONFIG_UTHREAD + +/** + * uthread_create() - Create a uthread object and make it ready for execution + * + * Threads are automatically deleted when they return from their entry point. + * + * @uthr: a pointer to a user-allocated uthread structure to store information + * about the new thread, or NULL to let the framework allocate and manage its + * own structure. + * @fn: the thread's entry point + * @arg: argument passed to the thread's entry point + * @stack_sz: stack size for the new thread (in bytes). The stack is allocated + * on the heap. + * @grp_id: an optional thread group ID that the new thread should belong to + * (zero for no group) + */ +int uthread_create(struct uthread *uthr, void (*fn)(void *), void *arg, + size_t stack_sz, unsigned int grp_id); +/** + * uthread_schedule() - yield the CPU to the next runnable thread + * + * This function is called either by the main thread or any secondary thread + * (that is, any thread created via uthread_create()) to switch execution to + * the next runnable thread. + * + * Return: true if a thread was scheduled, false if no runnable thread was found + */ +bool uthread_schedule(void); +/** + * uthread_grp_new_id() - return a new ID for a thread group + * + * Return: the new thread group ID + */ +unsigned int uthread_grp_new_id(void); +/** + * uthread_grp_done() - test if all threads in a group are done + * + * @grp_id: the ID of the thread group that should be considered + * Return: false if the group contains at least one runnable thread (i.e., one + * thread which entry point has not returned yet), true otherwise + */ +bool uthread_grp_done(unsigned int grp_id); + +#else + +static inline int uthread_create(struct uthread *uthr, void (*fn)(void *), + void *arg, size_t stack_sz, + unsigned int grp_id) +{ + fn(arg); + return 0; +} + +static inline bool uthread_schedule(void) +{ + return false; +} + +static inline unsigned int uthread_grp_new_id(void) +{ + return 0; +} + +static inline bool uthread_grp_done(unsigned int grp_id) +{ + return true; +} + +#endif /* CONFIG_UTHREAD */ +#endif /* _UTHREAD_H_ */ diff --git a/lib/Kconfig b/lib/Kconfig index a21b3378fa7..db9debaaeeb 100644 --- a/lib/Kconfig +++ b/lib/Kconfig @@ -1255,6 +1255,27 @@ config PHANDLE_CHECK_SEQ enable this config option to distinguish them using phandles in fdtdec_get_alias_seq() function. +config UTHREAD + bool "Enable thread support" + depends on HAVE_INITJMP + help + Implement a simple form of cooperative multi-tasking based on + context-switching via initjmp(), setjmp() and longjmp(). The + uthread_ interface enables the main thread of execution to create + one or more secondary threads and schedule them until they all have + returned. At any point a thread may suspend its execution and + schedule another thread, which allows for the efficient multiplexing + of leghthy operations. + +config UTHREAD_STACK_SIZE + int "Default uthread stack size" + depends on UTHREAD + default 32768 + help + The default stack size for uthreads. Each uthread has its own stack. + When the stack_sz argument to uthread_create() is zero then this + value is used. + endmenu source "lib/fwu_updates/Kconfig" diff --git a/lib/Makefile b/lib/Makefile index a30ce1595d5..cd7d04528c6 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -160,6 +160,8 @@ obj-$(CONFIG_LIB_ELF) += elf.o obj-$(CONFIG_$(PHASE_)SEMIHOSTING) += semihosting.o +obj-$(CONFIG_UTHREAD) += uthread.o + # # Build a fast OID lookup registry from include/linux/oid_registry.h # diff --git a/lib/uthread.c b/lib/uthread.c new file mode 100644 index 00000000000..c3f6c84c234 --- /dev/null +++ b/lib/uthread.c @@ -0,0 +1,139 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2021 Ahmad Fatoum, Pengutronix + * Copyright (C) 2025 Linaro Limited + * + * An implementation of cooperative multi-tasking inspired from barebox threads + * https://github.com/barebox/barebox/blob/master/common/bthread.c + */ + +#include +#include +#include +#include +#include +#include +#include + +static struct uthread main_thread = { + .list = LIST_HEAD_INIT(main_thread.list), +}; + +static struct uthread *current = &main_thread; + +/** + * uthread_trampoline() - Call the current thread's entry point then resume the + * main thread. + * + * This is a helper function which is used as the @func argument to the + * initjmp() function, and ultimately invoked via setjmp(). It does not return + * but instead longjmp()'s back to the main thread. + */ +static void __noreturn uthread_trampoline(void) +{ + struct uthread *curr = current; + + curr->fn(curr->arg); + curr->done = true; + current = &main_thread; + longjmp(current->ctx, 1); + /* Not reached */ + while (true) + ; +} + +/** + * uthread_free() - Free memory used by a uthread object. + */ +static void uthread_free(struct uthread *uthread) +{ + if (!uthread) + return; + free(uthread->stack); + free(uthread); +} + +int uthread_create(struct uthread *uthr, void (*fn)(void *), void *arg, + size_t stack_sz, unsigned int grp_id) +{ + bool user_allocated = false; + + if (!stack_sz) + stack_sz = CONFIG_UTHREAD_STACK_SIZE; + + if (uthr) { + user_allocated = true; + } else { + uthr = calloc(1, sizeof(*uthr)); + if (!uthr) + return -1; + } + + uthr->stack = memalign(16, stack_sz); + if (!uthr->stack) + goto err; + + uthr->fn = fn; + uthr->arg = arg; + uthr->grp_id = grp_id; + + list_add_tail(&uthr->list, ¤t->list); + + initjmp(uthr->ctx, uthread_trampoline, uthr->stack, stack_sz); + + return 0; +err: + if (!user_allocated) + free(uthr); + return -1; +} + +/** + * uthread_resume() - switch execution to a given thread + * + * @uthread: the thread object that should be resumed + */ +static void uthread_resume(struct uthread *uthread) +{ + if (!setjmp(current->ctx)) { + current = uthread; + longjmp(uthread->ctx, 1); + } +} + +bool uthread_schedule(void) +{ + struct uthread *next; + struct uthread *tmp; + + list_for_each_entry_safe(next, tmp, ¤t->list, list) { + if (!next->done) { + uthread_resume(next); + return true; + } else { + /* Found a 'done' thread, free its resources */ + list_del(&next->list); + uthread_free(next); + } + } + return false; +} + +unsigned int uthread_grp_new_id(void) +{ + static unsigned int id = 0; + + return ++id; +} + +bool uthread_grp_done(unsigned int grp_id) +{ + struct uthread *next; + + list_for_each_entry(next, &main_thread.list, list) { + if (next->grp_id == grp_id && !next->done) + return false; + } + + return true; +} From patchwork Mon Mar 31 12:31:01 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Forissier X-Patchwork-Id: 877288 Delivered-To: patch@linaro.org Received: by 2002:a5d:6dae:0:b0:38f:210b:807b with SMTP id u14csp1726276wrs; Mon, 31 Mar 2025 05:32:33 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXkRUGK4mgWbb6vRWVbw1UUeqCxpshyHnHtprHu26ud+t+wpr7qEvuJbUhlj2fVzUVg5eXFgQ==@linaro.org X-Google-Smtp-Source: AGHT+IElmuKVRjMNvuaHuFkee5qxOkm2JJTYORZw7DAKl7YEf7gHBQcf4azDzoM/VmIYP0645IfB X-Received: by 2002:a05:6102:3fa3:b0:4bb:d394:46cd with SMTP id ada2fe7eead31-4c6d39bbb6bmr4757526137.18.1743424352727; Mon, 31 Mar 2025 05:32:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1743424352; cv=none; d=google.com; s=arc-20240605; b=Vv89CWGOrqxOlhI0TGjx0ZzcBCo2imcEvDLol82paH+42sCL1x8k8CsFJA1XrhNJVP 3mWXFGBUbi/ziFpABPQivDowkS4MsCQ2b9wxN00WTV/7oQOHv7RKakBehH/z4V8mi7u4 Ma44dPxzc1mJ7KPB1LhVVoa+LeWQHUhbJhxiW1v0z99egw3JHEOUNN+hKO0xH7wQliS/ 3dr5JW6giCIOvv17WS0at7oAwK+hMqUWjB+2A00RH02u7tinPwFvqJ4mLhjKwGU156Vm b7R9UK0a27ZeWObWSxYy1hXew2QKZhznMg/gz6ja7/JfX9EA0XOG1H9112btA7Mw3adS EYlg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=uHZKSG0xtRYRdde4r+bICyY03DcqDMhunPskeFUaEDU=; fh=pwzqd1tcaoJvPaaF+Zz6jA22igjjbWCnYSzWt80XhHg=; b=JTr41VZ/KhKCECTFKhoGZCtAjeDaMvJfFXVE/5QcfbphjfCt4Q8rfwctDRgx1Vktbv IHU6/X+DcO2s9HZvGGcrwKiJlRntzPq19jE2xRQ+EpvYYuRPYwLGi5mlzli1aoirQpb8 dWWkbMKZRztnmmDaYXRyCf/5tQDiLODnQhQhProoZXZMgFAuoE18Gx7YHIfMfkZIIzp/ ICLfhDO5vtmf3PJtR8b40SghdZRiv6WHcMmvHrSDrYZ/olHPqbZi9uOPalWnMVJvWduF 1X/BGqBHco4b1DdMpu7/fIil5ZIFUoPXqgYqIysFLa59hs8qxwKTnvkFlKViL3qx51lP PaSQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=i4UpjmjW; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id ada2fe7eead31-4c6bfe00827si1582619137.331.2025.03.31.05.32.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:32:32 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=i4UpjmjW; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 60B3981EDD; Mon, 31 Mar 2025 14:31:47 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="i4UpjmjW"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 4DB3381703; Mon, 31 Mar 2025 14:31:46 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 3440381FC8 for ; Mon, 31 Mar 2025 14:31:44 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=jerome.forissier@linaro.org Received: by mail-wr1-x435.google.com with SMTP id ffacd0b85a97d-3996af42857so3536511f8f.0 for ; Mon, 31 Mar 2025 05:31:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1743424303; x=1744029103; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=uHZKSG0xtRYRdde4r+bICyY03DcqDMhunPskeFUaEDU=; b=i4UpjmjWAmMX1QtRAi4qIpGqHsSCz8OphWW+Ywca609MKQdy9j5wLhB6/9s+zz6vVv EaLJJB2RefI3G4xUEnGPJvRg3t3GjfLbd89A/LnrFHxLlXFvyb8zG/WED3jo44bjcMRk NcG7GXdsQ/0dI5L0UglZoVVuLKDzXQ45Ta19P41VZjzEguGO2r9XbHRgVfkhVYT/ZvNE CGHs8veOx1nuC2GvAZrOvKgfX4KNaQKGy87I41JJzLTpfpdNh+1BPck7JefxIVbO/7bE 0i8UnlHV9Frmbq7K8LlaCFG2NevGi/+o36/iSPIs1XggkyfcnxNVArhIhEZftnRo6n6R FzFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743424303; x=1744029103; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=uHZKSG0xtRYRdde4r+bICyY03DcqDMhunPskeFUaEDU=; b=mi2+PlVYs2BrEMaLA2PKI6nEo2P7MKTTKJJRTCFCQcuCXu/pZVVedl0O6V1JXn9ian B2Y51OQAwIj4yhl8QlpSpicRMEaChN1CjwgrP5hdZrcPEA5W4Ej8/PtRSYW6hjuRllLj 5DtbWHgpQ7mSo/tQwJUowSHCdjTk5MQ6fXbFYBdevepDOLIT8IpTnJVwV034n6fS9rYL KCcaqtUg0+Uf3ZeYPqtU2049wNUdl2ODswQ9wr5Y5dbU070EPc2aDMt7FeZRwmG/VRuM qoNDLrC5H+/FQymgAKaZ8UvLgdhhohTtccLuK3zSSmp/7MFmv4993pMxkaOUFOxQ5Xw/ itmA== X-Gm-Message-State: AOJu0YzJ+yXVzL9VBDGYpdE8eyql9Raxx27rF5F3JDIybhgqezx7e2Tr o0wEwnpX03U8VhavDyxLavYLU8zVrrGprnNnGvz+Tv9iYAWlr+K9sGmAFXA15pMDhplN46QCnfU TWRJgtg== X-Gm-Gg: ASbGncsinjKxStaKNk8mkvYAO7me4VbZdMXbq73yVqkIHZ+CnLvYkP3mbD5Z9aH1Mp6 RVe+ZtFP0Rbs3EODroh3cmh0PdYt/JDL17ToOszHeWQ2+DUdexSl0oO5ktR3ddtPj3fkfpKjsUz eJQX1nYrzcmZyGBqDCd2P1+xIfr5+/v+PbnVIg19xDVtebLmKgfCUemsqZBMFmprlzliWAn47Ae tDC9wnvj0AQ3+3fhzCWmNenL8XCtT8vOz2zvCv7tc9+MlgRFStDR19+plHv6EWh/qLVCG5V0Vqy X085jMfhrcVLkmHdTHL1H2zxkBYCnKkbNxzGj+OT3RX1D9i0kzGX X-Received: by 2002:a5d:64c3:0:b0:399:6d8a:3f with SMTP id ffacd0b85a97d-39c11ba42eamr6731464f8f.27.1743424303464; Mon, 31 Mar 2025 05:31:43 -0700 (PDT) Received: from builder.. ([2a01:e0a:3cb:7bb0:f23c:25c9:dc74:be1]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-39c0b6589e4sm11337029f8f.10.2025.03.31.05.31.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:31:42 -0700 (PDT) From: Jerome Forissier To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Jerome Forissier , Tom Rini Subject: [PATCH v5 07/16] uthread: add uthread_mutex Date: Mon, 31 Mar 2025 14:31:01 +0200 Message-ID: <20250331123120.2025062-8-jerome.forissier@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250331123120.2025062-1-jerome.forissier@linaro.org> References: <20250331123120.2025062-1-jerome.forissier@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Add struct uthread_mutex and uthread_mutex_lock(), uthread_mutex_trylock(), uthread_mutex_unlock() to protect shared data structures from concurrent modifications. Signed-off-by: Jerome Forissier --- include/uthread.h | 60 +++++++++++++++++++++++++++++++++++++++++++++++ lib/uthread.c | 27 +++++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/include/uthread.h b/include/uthread.h index ec3034c097a..87292b604ca 100644 --- a/include/uthread.h +++ b/include/uthread.h @@ -50,6 +50,23 @@ struct uthread { struct list_head list; }; +/** + * Internal state of a struct uthread_mutex + */ +enum uthread_mutex_state { + UTHREAD_MUTEX_UNLOCKED = 0, + UTHREAD_MUTEX_LOCKED = 1 +}; + +/** + * Uthread mutex + */ +struct uthread_mutex { + enum uthread_mutex_state state; +}; + +#define UTHREAD_MUTEX_INITIALIZER { .state = UTHREAD_MUTEX_UNLOCKED } + #ifdef CONFIG_UTHREAD /** @@ -94,6 +111,44 @@ unsigned int uthread_grp_new_id(void); */ bool uthread_grp_done(unsigned int grp_id); +/** + * uthread_mutex_lock() - lock a mutex + * + * If the cwmutexlock is available (i.e., not owned by any other thread), then + * it is locked for use by the current thread. Otherwise the current thread + * blocks: it enters a wait loop by scheduling other threads until the mutex + * becomes unlocked. + * + * @mutex: pointer to the mutex to lock + * Return: 0 on success, in which case the lock is owned by the calling thread. + * != 0 otherwise (the lock is not owned by the calling thread). + */ +int uthread_mutex_lock(struct uthread_mutex *mutex); + +/** + * uthread_mutex_trylock() - lock a mutex if not currently locked + * + * Similar to uthread_mutex_lock() except return immediately if the mutex is + * locked already. + * + * @mutex: pointer to the mutex to lock + * Return: 0 on success, in which case the lock is owned by the calling thread. + * EBUSY if the mutex is already locked by another thread. Any other non-zero + * value on error. + */ +int uthread_mutex_trylock(struct uthread_mutex *mutex); + +/** + * uthread_mutex_unlock() - unlock a mutex + * + * The mutex is assumed to be owned by the calling thread on entry. On exit, it + * is unlocked. + * + * @mutex: pointer to the mutex to unlock + * Return: 0 on success, != 0 on error + */ +int uthread_mutex_unlock(struct uthread_mutex *mutex); + #else static inline int uthread_create(struct uthread *uthr, void (*fn)(void *), @@ -119,5 +174,10 @@ static inline bool uthread_grp_done(unsigned int grp_id) return true; } +/* These are macros for convenience on the caller side */ +#define uthread_mutex_lock(_mutex) ({ 0; }) +#define uthread_mutex_trylock(_mutex) ({ 0 }) +#define uthread_mutex_unlock(_mutex) ({ 0; }) + #endif /* CONFIG_UTHREAD */ #endif /* _UTHREAD_H_ */ diff --git a/lib/uthread.c b/lib/uthread.c index c3f6c84c234..dae3ec758fd 100644 --- a/lib/uthread.c +++ b/lib/uthread.c @@ -8,6 +8,7 @@ */ #include +#include #include #include #include @@ -137,3 +138,29 @@ bool uthread_grp_done(unsigned int grp_id) return true; } + +int uthread_mutex_lock(struct uthread_mutex *mutex) +{ + while (mutex->state == UTHREAD_MUTEX_LOCKED) + uthread_schedule(); + + mutex->state = UTHREAD_MUTEX_LOCKED; + return 0; +} + +int uthread_mutex_trylock(struct uthread_mutex *mutex) +{ + if (mutex->state == UTHREAD_MUTEX_UNLOCKED) { + mutex->state = UTHREAD_MUTEX_LOCKED; + return 0; + } + + return EBUSY; +} + +int uthread_mutex_unlock(struct uthread_mutex *mutex) +{ + mutex->state = UTHREAD_MUTEX_UNLOCKED; + + return 0; +} From patchwork Mon Mar 31 12:31:02 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Forissier X-Patchwork-Id: 877290 Delivered-To: patch@linaro.org Received: by 2002:a5d:6dae:0:b0:38f:210b:807b with SMTP id u14csp1726400wrs; Mon, 31 Mar 2025 05:32:51 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXGO3D+wNyGtGP8kWg09s6P5Cbi3xRR0XOAEewSJ858m7PmrBCz0SxLsJkNeaR+71/53Nu7og==@linaro.org X-Google-Smtp-Source: AGHT+IEKleIIUngPxFjToHwC2xchlacH+o6XIeMgugI6Vou5SOCNRYcQzmOEbNLgLbalgJKefmgk X-Received: by 2002:a05:6102:5687:b0:4b6:20a5:8a11 with SMTP id ada2fe7eead31-4c6d38248e6mr4483386137.1.1743424371236; Mon, 31 Mar 2025 05:32:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1743424371; cv=none; d=google.com; s=arc-20240605; b=HLDgKwjhaerMplzsVIk0bBWjebWG12Z4+A+rVXSRcGe8el1kiyWVC+PQJ5YYgycHIk FJoSLMZx2uj1OnX+ENgWXjg8Wdeqi9L/dH1hzkekKh35pB+Oc1lyQVdxaQqopHljq3/8 JuLoE7sIQNY05dByquj+Jxa0fRcxXws4lI4tCNrV/n0x5b3Kl/2cIdkfNkxhDkc1KKwO dL3kZiokehkzYBSxLY8EyJSouionRWELyGYdt4zCeM5D9eToVNH/7NkdLdTudzVr9mWz x8aGIOwOh2yJLWnjwHDR84TBxN7flGZRUQUk2JlZxlprCzeNPGmBgoUxB2QWolvjHPjq x5Qg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=y421ZCXdbIuk+CO6p9JQkLRemHxOxOFsYoJqtKMt0Rc=; fh=FcJrLuOZm3DOgvhNhuu9RcOXf23qDSxE2e4MWRi7Xy0=; b=XxHifO+9hD5hThGkmEnIxY8FpO2Na1Vu67f1RwL3trJJfYMV7YsSXVrxDewud54JRu SP5pQKAXWWNssKXfr12l8LT3a9sbRNfSbnaCqrA27HbAr3EjSngBeI6lKcxpm8O3sTne oAH6kAo+WrMOBAdjbI50nKXgX29FoEP0rTCwkEXrnnL22fDcESBChiBy59eQzo2D0EYC U0EpM134CiOz6LK67oFmmjrU+235DfA6ZCj1gMczMtowyBXPM7e4wEG3z4gtxZZ6FlXk 8yWggFZMwaHLv6HEYZBlTlcuXldEFS2armfMJuDt245VdEsu3QZ7Pezksb19REkGBsfp aj+w==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=raVjvbCy; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id ada2fe7eead31-4c6bfc8b7d2si1586991137.10.2025.03.31.05.32.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:32:51 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=raVjvbCy; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 017978205A; Mon, 31 Mar 2025 14:31:51 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="raVjvbCy"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 388FE81F7E; Mon, 31 Mar 2025 14:31:48 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 7A34B82027 for ; Mon, 31 Mar 2025 14:31:45 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=jerome.forissier@linaro.org Received: by mail-wr1-x42d.google.com with SMTP id ffacd0b85a97d-39ac56756f6so3766162f8f.2 for ; Mon, 31 Mar 2025 05:31:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1743424305; x=1744029105; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=y421ZCXdbIuk+CO6p9JQkLRemHxOxOFsYoJqtKMt0Rc=; b=raVjvbCyvLAg4zcNPZsTjCku/GYpKkG7Q13izobhq4L1AVmlbmGHFSL2skCHJ5jTXs 51yV6ahEduKdfQuoAKrIrhsX3Ronm5JivPYJxf/1XIHSE/y0jNQq1StOPP1UbAbu4jjs sfD4zGD6ZtksnqHld/iFjjvDTZTRmjJg4gU4Yh55iaZ4CANwMQFoMAora/Sjlx+Sctdm 7E91MxFW/KlMg+43T7Jhi4EyqWNfDi9FcBfsaqeJuP1d2xXnhasSwojh4ZQ8mTfnGRjE r+l6VH3DpxnNK809wsrcaJnAgsSeSZOuNxLyupupiG/82LivfCv0VE4IcXWRfmAG5wc3 BpJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743424305; x=1744029105; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=y421ZCXdbIuk+CO6p9JQkLRemHxOxOFsYoJqtKMt0Rc=; b=xTpkw0MyaJfJkMx79ZlstWJt5yG9WSdTPRYO91nyAdABAPGY5RIlBlgnvVkTOfiTXt w3f4wO3oWiq8B8oKJrZ4RLh5DJDIiwpvqe49Bht+z6+zNVtMO+1DIQ05H9zJMVCrLdvj 3CReZSEWYmpRtnQkw2H8rSPdgf94ceQ9269jP7uYYQrAVqXaMY6i1YfXMDP0kM8dYkkB Ww/uuXkkMFIe+5hCJJvCNJXUDrG/9nlYmTurmaocu3XXTVrW5m8HGKr6TnVIUW+z7SG/ hCe2Wxt4fDfRJ3CFcBNyp104TOwm36NlE4RtwjFmUxbnvsB1LvjfvCctaDri91qH+My+ 1EgQ== X-Gm-Message-State: AOJu0YzceroX6qSkWtfFt8xG3knfWAfNBTCcW/5+2qNFQnNVMwBFk2g0 AaTfx9v08e/+4exqiXidJ0BWF4VzwkCrRlOOU62a4XDls6dFhGcLBl2qvLIHQ/XSD33e/5pb6GF 2h0oBXg== X-Gm-Gg: ASbGnctObbcNjW3WX8VtpIXZbT574DRQ39jAAzOwxUrorCBkWVQM4VpY1Qqzzcqm58i 8mzkjPfTKLqmCoAsxBw7tKb/qRBacOrJFZHPzvr3ie4gVIg1H2On6ZDt7y30u75L/wvxZZ4a4nd UJ37+gjMWk0uFZkjjAtol8RTdKfF9H9rI316TPpuzFnMpBWZCDIrFnl9Xbg/nXykOXG5WCzvGq1 d9AfVw+M3naoaLfAMaz5WUGNOh42WM1iVXzuDYs2xQlsvREpBTkqCy+0W5qrm9rWE4GBqbsL2sC 1IIGNZWqk7215uBWl/timy2qiNc+OTwbYiG1H84fbz5r4ujQCKpD X-Received: by 2002:a5d:5f91:0:b0:390:e7c1:59c4 with SMTP id ffacd0b85a97d-39c120de736mr7436775f8f.13.1743424304767; Mon, 31 Mar 2025 05:31:44 -0700 (PDT) Received: from builder.. ([2a01:e0a:3cb:7bb0:f23c:25c9:dc74:be1]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-39c0b6589e4sm11337029f8f.10.2025.03.31.05.31.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:31:44 -0700 (PDT) From: Jerome Forissier To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Jerome Forissier , Stefan Roese , Tom Rini , Rasmus Villemoes , Simon Glass , Patrice Chotard Subject: [PATCH v5 08/16] cyclic: invoke uthread_schedule() from schedule() Date: Mon, 31 Mar 2025 14:31:02 +0200 Message-ID: <20250331123120.2025062-9-jerome.forissier@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250331123120.2025062-1-jerome.forissier@linaro.org> References: <20250331123120.2025062-1-jerome.forissier@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Make the schedule() call from the CYCLIC framework a uthread scheduling point too. This makes sense since schedule() is called from a lot of places where uthread_schedule() needs to be called. Signed-off-by: Jerome Forissier Reviewed-by: Ilias Apalodimas Reviewed-by: Stefan Roese --- common/cyclic.c | 3 +++ include/u-boot/schedule.h | 3 +++ 2 files changed, 6 insertions(+) diff --git a/common/cyclic.c b/common/cyclic.c index fad071a39c6..b695f092f52 100644 --- a/common/cyclic.c +++ b/common/cyclic.c @@ -16,6 +16,7 @@ #include #include #include +#include DECLARE_GLOBAL_DATA_PTR; @@ -100,6 +101,8 @@ void schedule(void) */ if (gd) cyclic_run(); + + uthread_schedule(); } int cyclic_unregister_all(void) diff --git a/include/u-boot/schedule.h b/include/u-boot/schedule.h index 4fd34c41229..4605971fdcb 100644 --- a/include/u-boot/schedule.h +++ b/include/u-boot/schedule.h @@ -3,6 +3,8 @@ #ifndef _U_BOOT_SCHEDULE_H #define _U_BOOT_SCHEDULE_H +#include + #if CONFIG_IS_ENABLED(CYCLIC) /** * schedule() - Schedule all potentially waiting tasks @@ -17,6 +19,7 @@ void schedule(void); static inline void schedule(void) { + uthread_schedule(); } #endif From patchwork Mon Mar 31 12:31:03 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Forissier X-Patchwork-Id: 877291 Delivered-To: patch@linaro.org Received: by 2002:a5d:6dae:0:b0:38f:210b:807b with SMTP id u14csp1726463wrs; Mon, 31 Mar 2025 05:33:00 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXsrcGhu9rSpO96g3QnQpU49Aod0H76tU1s1sPKTs+5QkZ0Lx0LeFBMEksPKJmoY2CJ5SSPzQ==@linaro.org X-Google-Smtp-Source: AGHT+IFLFj0LCrY/NWaTfEm9vrLxGbsx6JT1Miclw8WIdtuSv2wGHkJ/9Qb3JEFlY7i3UG/cJprB X-Received: by 2002:a05:6102:2ad5:b0:4c4:dead:59a3 with SMTP id ada2fe7eead31-4c6d3837dfdmr5137163137.2.1743424380625; Mon, 31 Mar 2025 05:33:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1743424380; cv=none; d=google.com; s=arc-20240605; b=aIOVFm6Bc1rwJt8ItMpKfKYR51S19xM4RjyGV5zO6qCw56DGa5g878XTp45OIeQ1Ly 6D+scUvKHhmMjSjjXO7irCqAXqxcsNt//8dAUeFO20cfPiBT/wRA46HIN221KnCC7Q/l XHnFQ4RtIrnJK0BFox+Z+TGXj1w6pspPPi8BF5StyoKwUPfxVSUxtUIPImXOIzEHZme0 Tgkm7IGwYQuKG7zaRhFbidGjLCRoJ0aXOdoFm8GNMD2QLHlYVUkg9nVQtK4ttBNeHrEV b1bmA72pQNc23kkMM1N278wals0XxsQWTwKHYBvossvjh+oQJfrxNLaXcHxvSP7q1G9Z 18tQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=N5+bxMo/9OI1mpOVDR5OrqZBPXNv4qSx9YKZscWS57E=; fh=WSXgq+dxaJSHadwMDVBKq11PIkESctA8ND/ZfWAV8a0=; b=Sju5afTwFUs4bkzXydxCK5rgIoftw/hfCZiWBwT4up1hZYUVL0/o9C5OvLSuTXt0r+ ZnGFyIunahj2dVUNXimjKhjAGq4f3Z9wmkYhq88FaebNJuS7S9gmvtaimCsq/oLsAVpk kQwRlokC4IKncVET7HJWe9X7eqJR7xKIZ1XKzu1t7+g1JeuAk9V/rCMC8jIGcQQVuTuG xKdsdf4pnOtdAjRThYqj4yYOVCJjB25TpUTRPkhtxJbHKSEieVmXTVn46IrBkVEiXU6k SycvDO/0LmNZkigk78m2xPc2OShPcV3SxHmPh42FmmaXdes2NU8E5cAFAL0OsirjgRpF ptfQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=AvWVfeJO; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id ada2fe7eead31-4c6bfe12ba3si1673909137.216.2025.03.31.05.33.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:33:00 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=AvWVfeJO; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 4681082072; Mon, 31 Mar 2025 14:31:51 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="AvWVfeJO"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 69DD481F7E; Mon, 31 Mar 2025 14:31:48 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 5B86981ECD for ; Mon, 31 Mar 2025 14:31:46 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=jerome.forissier@linaro.org Received: by mail-wr1-x42b.google.com with SMTP id ffacd0b85a97d-39ac8e7688aso3531839f8f.2 for ; Mon, 31 Mar 2025 05:31:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1743424305; x=1744029105; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=N5+bxMo/9OI1mpOVDR5OrqZBPXNv4qSx9YKZscWS57E=; b=AvWVfeJOYKXtVMjtrk2+XAIV2FWSztnEEG9wbisPoxGjcD3gmAv25lNyuN+e77fc8w cbjtBBsx0QPBY3BbYaGG82+NguhdnTaeozPHO2yJRfoAoPLKZh4NtSRjcWYDoTD+8npd 9RHJQpHb8KAezAB0TAjGTiiCa3pgvFCa4x/UFGqGZ1A5QyP5cBp+DWTMcs/hGYPQt90w dC8Y4d9xC9T4y/ALznc3Yeso7LilBvxu4b63DycPyMpRe+aJcGkTARay1OoCWzcJgzcl JT9hJHZBYyxj3tIRw9niFtQevNB47QwZKd8bCcBVp3Now48paSukOeIChXR+zfZQLof7 rSFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743424305; x=1744029105; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=N5+bxMo/9OI1mpOVDR5OrqZBPXNv4qSx9YKZscWS57E=; b=cXNEUlmElcTGvQXgE7WhOEZ7T/TNe5y7bErUMCmL4lYCcqxDClgtFA6KpNkieLMgUL /lPaUISrzayumTeBHqJs7+WgxjQgeZu1Imh2m4nVinFKxg8ZtrRDy5Vy6gWzwRoRHb6g ueWGP83crbXn3PWgapJ5uJSmgC0WujJD7fJUUIAkNcswhOQ/IziuPZgAsIUA3F6sIduU Q11B4XdBJPE56FdWZRTDG61TDJXTHDZW9yRsMZj+Wim5jRu5QxJJsea7k3mIeaZgzaXS VLbdKbR76TDVOp4tziVJPmyc9VwI3FdFZM9FLV3nsXFavlfbtkeb30f9ofQNzE2sDDXv OLrQ== X-Gm-Message-State: AOJu0Yx7s+rVU5pE4XxQVhq74GYw9LSQBT3RZ6Ud8cteh4MCke5r1cZl +ZLtpo1uNr48MawQHYTeJ4XAoK/5d16wLpW0tXIgVFAfVQpODZr5XeuJeJvJjf/ifdQOvKkorIs Lf1O70Q== X-Gm-Gg: ASbGncswkCKVN89arV2u+yHsEiKRaJO9Mwef1mPZ/DI+CO+qGHqd92O1lYatNTl8gYD 8s/Y9NemfpI2n1lOdfTlcWVJu9gE6wub+2XaUIjdrQw0a7ov5lKS3ujuHQGKx9e/5beeQJgrLbF RzXoRZPUIGbhywl6i2mVUermPKehh5jvnFe35fWZDIujDaVVMaZR2mRlhJONThKKFmR+RAfh7a0 mpWswuB3maHeOc4tZw3m+ZDQoKGjAaXww1dS6zKowH/MlD+Bi9dT3DEfu0CWXlGbkLBTDoxCRnV /MWJ8U13FxZdxUaWQ16u3HwPow04Z8rni1gGp/T7GIISUk1NhH9d X-Received: by 2002:a05:6000:184e:b0:39c:12ce:1054 with SMTP id ffacd0b85a97d-39c12ce1106mr6249694f8f.8.1743424305583; Mon, 31 Mar 2025 05:31:45 -0700 (PDT) Received: from builder.. ([2a01:e0a:3cb:7bb0:f23c:25c9:dc74:be1]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-39c0b6589e4sm11337029f8f.10.2025.03.31.05.31.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:31:45 -0700 (PDT) From: Jerome Forissier To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Jerome Forissier , Tom Rini , Simon Glass Subject: [PATCH v5 09/16] lib: time: hook uthread_schedule() into udelay() Date: Mon, 31 Mar 2025 14:31:03 +0200 Message-ID: <20250331123120.2025062-10-jerome.forissier@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250331123120.2025062-1-jerome.forissier@linaro.org> References: <20250331123120.2025062-1-jerome.forissier@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Introduce a uthread scheduling loop into udelay() when CONFIG_UTHREAD is enabled. This means that any uthread calling into udelay() may yield to uthread and be scheduled again later. There is no delay in the scheduling loop because tests have shown that such a delay can have a detrimental effect on the console (input drops characters). Signed-off-by: Jerome Forissier Reviewed-by: Ilias Apalodimas --- lib/time.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/time.c b/lib/time.c index d88edafb196..0e9b079f9cf 100644 --- a/lib/time.c +++ b/lib/time.c @@ -17,6 +17,7 @@ #include #include #include +#include #ifndef CFG_WD_PERIOD # define CFG_WD_PERIOD (10 * 1000 * 1000) /* 10 seconds default */ @@ -197,7 +198,13 @@ void udelay(unsigned long usec) do { schedule(); kv = usec > CFG_WD_PERIOD ? CFG_WD_PERIOD : usec; - __udelay(kv); + if (CONFIG_IS_ENABLED(UTHREAD)) { + ulong t0 = timer_get_us(); + while (timer_get_us() - t0 < kv) + uthread_schedule(); + } else { + __udelay(kv); + } usec -= kv; } while(usec); } From patchwork Mon Mar 31 12:31:04 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Forissier X-Patchwork-Id: 877292 Delivered-To: patch@linaro.org Received: by 2002:a5d:6dae:0:b0:38f:210b:807b with SMTP id u14csp1726543wrs; Mon, 31 Mar 2025 05:33:12 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCX204fyztD3fJ3ShyC66Jx9WxJGIu+7V3LG1zF4sc6KY7G32YbomsmceKwWgR6XzLAUXvUZoQ==@linaro.org X-Google-Smtp-Source: AGHT+IERj4Foq0Ft7C6cwLOoRPZ65SYwDj2nGbPW37U9rFCa5Rb/nvfZszD8GyUNTBWoEhwRIBPB X-Received: by 2002:a67:e713:0:b0:4c1:76a4:aee4 with SMTP id ada2fe7eead31-4c6d39516f8mr5162657137.19.1743424391790; Mon, 31 Mar 2025 05:33:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1743424391; cv=none; d=google.com; s=arc-20240605; b=C/GRwts2dFc+C/fF9EJebehxRgTZ9DCx+dZ3TsNhwNlM2Gmwjr5PZ3AA4xTw1smnRW Fu29+Mh1gO+QHxJQGbZ9WJc7OcMXTaBkRtnNjU7/yHMj2GJnNiNGCDGSgMl1gikrgto1 m8eJAD+D86dJwvsNy7GUsPDUH0YkRTK/YsEbY5RT9Md1Oog5fEHrcNZbih/fak4ufWgb NMpEMXOTur+ci4+2zZ1SxHwnEeAPlw9gU5wblXrUTEMe5ToFSH6nXe1Ny2cTwODfTAL5 gXGGFeEQEshNfxY6inCAm1q3M2HxtaAwVZrcgaTSnaNRJ9MX3M681sWlzVAisxRtPbfK cw+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=qkBJkl3WKwaUf5l3xYynOeT0PQynHCpYMM2dqIakoiU=; fh=pxkeymicbWSzax63Za9jHWp/6CzGxVjGkJodzxw4Txs=; b=einNj5gU5nD2hgdFrUxncfe6nlH2kQOOcBNa1QIQn2fYLniGgzcgwNL/fjVQ0eTFOP DDzH01JsLVoXe+9kRPirJN+Hj1fRFrg6FlHwHXtNBLwbuOIwsb0T0B3fLPaZsfA5XH5/ nvhufhPQaEWFtSdAT+FolmPx1N0SttEJjGmjUmE+3EzKh7qqGAdM1L59qJEpNlXxLVKz 7jXk2mVQiHDxLR8JxzPiUM2WmRFZoWBiDFJaQV/iHqeyaeFgdPWp7rKh3qotihN8Z77b DTysKdA8RtKbBJHKL/ug0SYDiiRyLC/rOQTEKCs6qWr7aSKE5Plj7XhWjAZkAHMCdFTh wntw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=zuf17zPN; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id ada2fe7eead31-4c6bfe83f3csi1595845137.568.2025.03.31.05.33.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:33:11 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=zuf17zPN; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 9E9D082027; Mon, 31 Mar 2025 14:31:53 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="zuf17zPN"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 5094782027; Mon, 31 Mar 2025 14:31:50 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 2DD5981F0D for ; Mon, 31 Mar 2025 14:31:48 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=jerome.forissier@linaro.org Received: by mail-wm1-x331.google.com with SMTP id 5b1f17b1804b1-43ea40a6e98so2975835e9.1 for ; Mon, 31 Mar 2025 05:31:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1743424307; x=1744029107; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=qkBJkl3WKwaUf5l3xYynOeT0PQynHCpYMM2dqIakoiU=; b=zuf17zPNMxy6NUYGMq5b8e81v/T/9zjIzasA3hfQRG6HS4KNyiuIrOOsvWfiHLgeId zoMPQBpNquuTLZFTLdhp3muggAPDyLt4RSMG9LNzNgN2ZuQvjCVrbX9X5IiC2Tp3BFdt 7UTlE4TaILvxxiwSWAaAjANyf9XuhpU5CFEWVS6ZhhdgwMYfs7HpalSkvVSPgjqU4bfD kw25RynHITm2kP38/nepDUUWLB9yckNT/Zg2CQpyytg7DlR8cW5Lbj9KKN9FJUusq4nH hhAXwMFiLHvJF0ljy42uKjbCM+IgCJ22/sTwLxK0uKu6WHtgcftQbu6g/m33cAP06d7L TgQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743424307; x=1744029107; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qkBJkl3WKwaUf5l3xYynOeT0PQynHCpYMM2dqIakoiU=; b=UP7DUzBHLsXWkCFZMobJR3j6EQM6xikQOPXLqisXqSVwkHMhooDan/Xl94DkFw8H2k tYBZSOYo+SDD4kVNx/dVZhqO8X2uVjEF7K2+9ovP0BmGAbmMeRCNdBO3mSSvxFQnwmko cDo+XoMTyYJv+nykJ/dYHuTzVO3dNr4otsByklj+Vc/I+Gbxpws5SUqieoNRDHEvVFuN 5lkk9JM6GxMmD+0cmK/BmAA/F1yyGgwgZ5/ZCK6GxRfebBsGwJL7X9Ao+wlT5yxxiEmh kdiMl1Qx42sttfMyrvT9RNb6fDD1AHkpKXrwI9eVDG4Zna2VrasaTHkTClzpjoh/hx/K iTaA== X-Gm-Message-State: AOJu0YzIhyMORYRnMcdpE8zs9/iwBk18r4KLKl/Lt0h1h7M8/JUoeMz7 lLYz/wsfGdsES86yFEliGmZgStQAY0Bny04OFopNW9sfQKhdJ0q4VMDpfUcKGAbLzq9io81iUeG 7R83Mwg== X-Gm-Gg: ASbGncsx+BRKGsYugLQYeUjwTnUpyvZbm2rQDs8/1T4L3SbA7ryAt0QkW5Jc739MYx0 jtdtF3pdj+7TCLPgDqlbQsJShJPBScVB6APQB+BW0UHUxYcqr0to81KMZIXAFPyoxG9LZ99DJQe PdPw6fF4r7od9h8cRg+p3VriZdPhEH3psE+xH/a6I6rIYhX2tl/QPEcx/uME28JVNE86Ul/5QSY Ytr62NMC25xkjMUYP6EJInVW6hbA9BnXbVvhm8QKMnPX523xNxk3iWcieoofrsDJXIfRAagsVSw TGVjmlKNuOpOHBQayr7PjjkIRvcuxwxgdjEOwDhsWNGYNb6Zq8Ah X-Received: by 2002:a05:6000:420a:b0:391:487f:2828 with SMTP id ffacd0b85a97d-39c120cd1d0mr7340258f8f.10.1743424307373; Mon, 31 Mar 2025 05:31:47 -0700 (PDT) Received: from builder.. ([2a01:e0a:3cb:7bb0:f23c:25c9:dc74:be1]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-39c0b6589e4sm11337029f8f.10.2025.03.31.05.31.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:31:46 -0700 (PDT) From: Jerome Forissier To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Jerome Forissier , Tom Rini , Heinrich Schuchardt , Simon Glass , Raymond Mao , Philippe Reynes Subject: [PATCH v5 10/16] test: lib: add uthread test Date: Mon, 31 Mar 2025 14:31:04 +0200 Message-ID: <20250331123120.2025062-11-jerome.forissier@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250331123120.2025062-1-jerome.forissier@linaro.org> References: <20250331123120.2025062-1-jerome.forissier@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Add a thread framework test to the lib tests. Update the API documentation to use the test as an example. Signed-off-by: Jerome Forissier Acked-by: Ilias Apalodimas --- doc/api/uthread.rst | 12 +++++++ test/lib/Makefile | 1 + test/lib/uthread.c | 80 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 test/lib/uthread.c diff --git a/doc/api/uthread.rst b/doc/api/uthread.rst index 21233ff6b22..8b25cc1ff80 100644 --- a/doc/api/uthread.rst +++ b/doc/api/uthread.rst @@ -3,5 +3,17 @@ Uthread API =========== +.. kernel-doc:: include/uthread.h + :doc: Overview + .. kernel-doc:: include/uthread.h :internal: + +Example +------- + +Here is an example of how to use this API: + +.. literalinclude:: ../../test/lib/uthread.c + :language: c + :linenos: diff --git a/test/lib/Makefile b/test/lib/Makefile index bf04685dae1..c991dff1c63 100644 --- a/test/lib/Makefile +++ b/test/lib/Makefile @@ -31,6 +31,7 @@ obj-$(CONFIG_CRC8) += test_crc8.o obj-$(CONFIG_UT_LIB_CRYPT) += test_crypt.o obj-$(CONFIG_UT_TIME) += time.o obj-$(CONFIG_$(XPL_)UT_UNICODE) += unicode.o +obj-$(CONFIG_UTHREAD) += uthread.o obj-$(CONFIG_LIB_UUID) += uuid.o else obj-$(CONFIG_SANDBOX) += kconfig_spl.o diff --git a/test/lib/uthread.c b/test/lib/uthread.c new file mode 100644 index 00000000000..0c0540585e7 --- /dev/null +++ b/test/lib/uthread.c @@ -0,0 +1,80 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright 2025 Linaro Limited + * + * Unit test for uthread + */ + +#include +#include +#include +#include + +static int count; + +/* A thread entry point */ +static void worker(void *arg) +{ + int loops = (int)(unsigned long)arg; + int i; + + for (i = 0; i < loops; i++) { + count++; + uthread_schedule(); + } +} + +/* + * lib_uthread() - testing the uthread API + * + * This function creates two threads with the same entry point. The first one + * receives 5 as an argument, the second one receives 10. The number indicates + * the number of time the worker thread should loop on uthread_schedule() + * before returning. The workers increment a global counter each time they loop. + * As a result the main thread knows how many times it should call + * uthread_schedule() to let the two threads proceed, and it also knows which + * value the counter should have at any moment. + */ +static int lib_uthread(struct unit_test_state *uts) +{ + int i; + int id1, id2; + + count = 0; + id1 = uthread_grp_new_id(); + ut_assert(id1 != 0); + id2 = uthread_grp_new_id(); + ut_assert(id2 != 0); + ut_assert(id1 != id2); + ut_assertok(uthread_create(NULL, worker, (void *)5, 0, id1)); + ut_assertok(uthread_create(NULL ,worker, (void *)10, 0, 0)); + /* + * The first call is expected to schedule the first worker, which will + * schedule the second one, which will schedule back to the main thread + * (here). Therefore count should be 2. + */ + ut_assert(uthread_schedule()); + ut_asserteq(2, count); + ut_assert(!uthread_grp_done(id1)); + /* Four more calls should bring the count to 10 */ + for (i = 0; i < 4; i++) { + ut_assert(!uthread_grp_done(id1)); + ut_assert(uthread_schedule()); + } + ut_asserteq(10, count); + /* This one allows the first worker to exit */ + ut_assert(uthread_schedule()); + /* At this point there should be no runnable thread in group 'id1' */ + ut_assert(uthread_grp_done(id1)); + /* Five more calls for the second worker to finish incrementing */ + for (i = 0; i < 5; i++) + ut_assert(uthread_schedule()); + ut_asserteq(15, count); + /* Plus one call to let the second worker return from its entry point */ + ut_assert(uthread_schedule()); + /* Now both tasks should be done, schedule should return false */ + ut_assert(!uthread_schedule()); + + return 0; +} +LIB_TEST(lib_uthread, 0); From patchwork Mon Mar 31 12:31:05 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Forissier X-Patchwork-Id: 877293 Delivered-To: patch@linaro.org Received: by 2002:a5d:6dae:0:b0:38f:210b:807b with SMTP id u14csp1726625wrs; Mon, 31 Mar 2025 05:33:20 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCX6nMq1EHTujij/uhEFzPeR4VJxPgwVX22H8u12iIeHpVg7iErvjrDu42HC2s/Xa++OhV+Zig==@linaro.org X-Google-Smtp-Source: AGHT+IGV9oR5QltwGO4Sm8KnLOyS54dWqwHsw7ezS+bxleOF11JVZ2KlZwBsqsIDeUYLHK68RFBZ X-Received: by 2002:a05:6102:3e8a:b0:4c3:6ba1:3aac with SMTP id ada2fe7eead31-4c6d3837bcdmr5385589137.3.1743424400050; Mon, 31 Mar 2025 05:33:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1743424400; cv=none; d=google.com; s=arc-20240605; b=lJq/k3F6OtpsjWKLG4HaolpmBO9zRB+n5hqksXsIHg7bU+cD41adIBA3ap9HWE9J3w 654vV6feVSHstuzBecSamk7FFTHnEdS8OI1zjna7MGZ7l06lbase3jHbjnslsHGM+rJo 6cFB89Q0P6jISBNOl2kCt6d470l2OQoWkb+4qWUHSAlefY5XnMj6JazWdWOy6o3F1do0 3vRVwBHGAeVCmfFNuNslBhEwG0knfCqjesN0pK6poQL12FhX1gzem/Gat35l248dzLvf ZIqLcwMHbjuvA4CIAYHsE5KrvmyifaZqOXk9s57aFJDECNlW/ulD2TS3Y+c2On0Ek5Ro BrXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=vRaJm7848vfOJLHvrW3owiUGagnHG2yjnxN7am7SWEk=; fh=pwzqd1tcaoJvPaaF+Zz6jA22igjjbWCnYSzWt80XhHg=; b=ky+4mHAZ6aczelzoO/QhiF39N4HqZk47KxYnMbYvhHdiaMKB4TRhVAWpz+93VryrPx iRymAJSYWPnPkCM56ia8jR7ma2PD9VriRY1VqolGGV7ytWWw8Dp38lwl9y+UfeCwPNgC FIPI2DXOxbCltGylFFRWldKGw+te1dJ0MIS1iV48JiYjsAyay+/5nOtv/xN9kthT3/V4 LLcajl36npluNN9H2iIJrirdwcKUOkmCAbLzkYATvj7G4bCMHLKR8DXcE8C78dqpaBsX U+bf8l4aHRZlpBOA9kB5BkeiFNsa1OPKgrN5EOZLLYIX8tWibHGaJWfG4gWmcbd+sSPS ELEw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=TImoIXsq; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id ada2fe7eead31-4c6bfea5805si1642204137.680.2025.03.31.05.33.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:33:20 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=TImoIXsq; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id EB34282033; Mon, 31 Mar 2025 14:31:53 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="TImoIXsq"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 6D8EE81703; Mon, 31 Mar 2025 14:31:51 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 6186A81703 for ; Mon, 31 Mar 2025 14:31:49 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=jerome.forissier@linaro.org Received: by mail-wr1-x433.google.com with SMTP id ffacd0b85a97d-38a25d4b9d4so2318604f8f.0 for ; Mon, 31 Mar 2025 05:31:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1743424308; x=1744029108; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=vRaJm7848vfOJLHvrW3owiUGagnHG2yjnxN7am7SWEk=; b=TImoIXsqeXXfOILouqXKqw5nU9DEAtJbvgTL/zrCs1djs6mgkOEWQglHNkCBwV+jM2 mZ5wC7pHEoG+VAAN293O5dvD9s8D8+Vk7k/iSB1dZPSKGL2lBXlLfkry5+5qG0Z82iY2 aYT4q6K5SVU3cmtrQvSr8clYn1+uMkJJ4cphpT8IA5aKHnGW9wQF13uIMzVc9nEpfVds cyRa/rXgDlACizi7z125/R1hw5+M3hGCFh122Gc9kxAKthoQGqqmvrNF8yeD1uhO4jCR 5tVH34ICXT/XStCTE+3SnVOS7m6yyCn0ax+uZuzGFqdupm/aCj5qSSNsbvHOtENlif9L meGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743424308; x=1744029108; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vRaJm7848vfOJLHvrW3owiUGagnHG2yjnxN7am7SWEk=; b=j+DjK7bjl2xYknhRMllOnliqhWPRf04h6KTZoZZro0kKefz4/1ZDfeiLIRrfb54Y5W 7f+cislwwUJ1oCfl3M9CD/fyjeIMlkheyqE7yRRFJhpQErPc3AoxXxfCUyGm9tfjJmC8 r0NyZOuJ+2z/CR+MLfd8SHHE/xSDqKsn5CVKE1/9s66f/lR2UCA4JrK4qfbvKcY431Hv gJpQnBDJsSaNen/8lDYtYUs/Y5WCXoHKdd0B2HVZtmDSlFWuVSy2XYDiRRFhpGHkXv85 fdW1yRtTarg3+0B8gFwvpwNH8n4K9HI/FoWwaFLT62/p49QiLYLhjKxZ5YEoNu1djELR SOiA== X-Gm-Message-State: AOJu0YxT/o/eFFW5e29jrWhURN0c7GdFQ1wbKUhk9YDQPE5GRwem4A9o Du9Ot20p1ybGEWR7QFWY8IKJ1h9jCXiusPIgfc8vVrxM4y5DmcuqM6pIMuCLxIQqcwfbe18i3yO y10TIbg== X-Gm-Gg: ASbGnctG1IzQXF34jbr+AQK+YDdZruKPuPr4+81ROuyk6pF/cNwztiAtK5KZaP/wf8z W8zwlJtEDn2681qJt8EvubpBxB/caTqtgqON66p/3RNsflLSwhjsi7MYHKk9ukuVV/sAygHeIVM fHfYRUrp4XlAC8Kw78IvuTJ3e957ZThKBDWYLh9HnEsRa/vOHEajRYkQw/HsrHhn6JEI9smrFwI 98aPSnUY3bSUMhMzQOUh+uA2LUcGKtjwwo/pNRytvDH3n1H6XS8gRcVNA1z2O8J/glat4wj/9Px 5orWhleD/DxVRzOQKUM5vgk/VFLBXFCvYmflgAz0nQ+3JkMEBn22 X-Received: by 2002:a05:6000:2910:b0:391:4889:503e with SMTP id ffacd0b85a97d-39c120f5923mr6319901f8f.33.1743424308596; Mon, 31 Mar 2025 05:31:48 -0700 (PDT) Received: from builder.. ([2a01:e0a:3cb:7bb0:f23c:25c9:dc74:be1]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-39c0b6589e4sm11337029f8f.10.2025.03.31.05.31.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:31:48 -0700 (PDT) From: Jerome Forissier To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Jerome Forissier , Tom Rini Subject: [PATCH v5 11/16] test: lib: add uthread_mutex test Date: Mon, 31 Mar 2025 14:31:05 +0200 Message-ID: <20250331123120.2025062-12-jerome.forissier@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250331123120.2025062-1-jerome.forissier@linaro.org> References: <20250331123120.2025062-1-jerome.forissier@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Add a test for uthread mutexes. Signed-off-by: Jerome Forissier --- test/lib/uthread.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/test/lib/uthread.c b/test/lib/uthread.c index 0c0540585e7..d33daa62f6c 100644 --- a/test/lib/uthread.c +++ b/test/lib/uthread.c @@ -78,3 +78,69 @@ static int lib_uthread(struct unit_test_state *uts) return 0; } LIB_TEST(lib_uthread, 0); + +struct mw_args { + struct unit_test_state *uts; + struct uthread_mutex *m; + int flag; +}; + +static int mutex_worker_ret; + +static int _mutex_worker(struct mw_args *args) +{ + struct unit_test_state *uts = args->uts; + + ut_asserteq(EBUSY, uthread_mutex_trylock(args->m)); + ut_assertok(uthread_mutex_lock(args->m)); + args->flag = 1; + ut_assertok(uthread_mutex_unlock(args->m)); + + return 0; +} + +static void mutex_worker(void *arg) +{ + mutex_worker_ret = _mutex_worker((struct mw_args *)arg); +} + +/* + * lib_uthread_mutex() - testing uthread mutex operations + * + */ +static int lib_uthread_mutex(struct unit_test_state *uts) +{ + struct uthread_mutex m = UTHREAD_MUTEX_INITIALIZER; + struct mw_args args = { .uts = uts, .m = &m, .flag = 0 }; + int id; + int i; + + id = uthread_grp_new_id(); + ut_assert(id != 0); + /* Take the mutex */ + ut_assertok(uthread_mutex_lock(&m)); + /* Start a thread */ + ut_assertok(uthread_create(NULL, mutex_worker, (void *)&args, 0, + id)); + /* Let the thread run for a bit */ + for (i = 0; i < 100; i++) + ut_assert(uthread_schedule()); + /* Thread should not have set the flag due to the mutex */ + ut_asserteq(0, args.flag); + /* Release the mutex */ + ut_assertok(uthread_mutex_unlock(&m)); + /* Schedule the thread until it is done */ + while (uthread_schedule()) + ; + /* Now the flag should be set */ + ut_asserteq(1, args.flag); + /* And the mutex should be available */ + ut_assertok(uthread_mutex_trylock(&m)); + ut_assertok(uthread_mutex_unlock(&m)); + + /* Of course no error are expected from the thread routine */ + ut_assertok(mutex_worker_ret); + + return 0; +} +LIB_TEST(lib_uthread_mutex, 0); From patchwork Mon Mar 31 12:31:06 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Forissier X-Patchwork-Id: 877294 Delivered-To: patch@linaro.org Received: by 2002:a5d:6dae:0:b0:38f:210b:807b with SMTP id u14csp1726692wrs; Mon, 31 Mar 2025 05:33:29 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWTCUTGQdpufLf73LShYtGGSobCpCXNlW/36ZQE4ImcS+CKCyffxCina7TFBylFdERNeoXaJQ==@linaro.org X-Google-Smtp-Source: AGHT+IEN0+xTNJki1OxiEwG4CphoW3Hoag60W7F8vwA07Xvcw+x0hvQy3FY7Ry+JzlRkIhrZ9gOK X-Received: by 2002:a05:6122:2105:b0:523:6eef:af62 with SMTP id 71dfb90a1353d-52610c95f95mr6326883e0c.4.1743424408949; Mon, 31 Mar 2025 05:33:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1743424408; cv=none; d=google.com; s=arc-20240605; b=M74cYRcNl8R17DGFQbE9w3iokv+f6NqG/xI1IESbTS7bwRSCqcsWflp9adwL3WpBkL +2WePikTwHiUPRKzjVlfvFHcHUaQZ26B5+Wf3AXDHRl8O2jIwGDpToY8uI0Xhaob3Tbd BIvMl54Q7y+Q34n6bxi/KhcXL5ODW+Fw9ehbRGoxF4gqDxTU/ODui9Zh2pdGIBYn0XMR kw0DATTmUxI43sV6TP3wnMLE2Ks+vVYGFRfIuH8YDigDrTBBaknERK64MBJB4reEOzi6 MR+hxfGVFCpKQAKrPc9YylPSpd/gXETvTRWjtO1soIt4Ea6qNNh+a6l9YP9CfUjqDJuC sjYg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Y1BmGfnz9Zf8enWa6MBJvo/gF3i4MJ+4iKJWgpAGdFA=; fh=keFItDNZKY6GK00X2JZFul47PtJZO32zs5RLfwodOTc=; b=UV3eX8+bJHCEA+IYSpiUVtp4i11NJz5THRLeu2BBGDEpdsDeZeje81T6ivyNvJ8Kuc 0msDnsw97X6WVKzMD7EKto33ozziinmg4ZIC3mVgmqk9GNXXD3XDjaFI/v8A76u9FHnj /nSReK0aB7UP9YfY805t7n/nJxzlyBzdCr7tC5GcI4WRGlYrqmSNI454MZCEtTQNtdXF QMMfKg74yIhqaVrkei7tIrlz/0CWNJc5M4OIcs2xQnrgLdK0nq7nEmRfnZY6wXMWhDTU 8sXNsuKCaG+gRSJCv0Dmah8NV9oIyBbjBd4JrOd4Gxsng0Q9/J+5rUN6CeR0PpPW4ZD/ 7nBg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="K/wHSKNB"; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id 71dfb90a1353d-5260eabc5b5si1769693e0c.162.2025.03.31.05.33.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:33:28 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="K/wHSKNB"; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 43EF28206E; Mon, 31 Mar 2025 14:31:54 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="K/wHSKNB"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 8420D81FE6; Mon, 31 Mar 2025 14:31:52 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 6F27282012 for ; Mon, 31 Mar 2025 14:31:50 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=jerome.forissier@linaro.org Received: by mail-wr1-x432.google.com with SMTP id ffacd0b85a97d-3912fdddf8fso3351035f8f.1 for ; Mon, 31 Mar 2025 05:31:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1743424310; x=1744029110; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Y1BmGfnz9Zf8enWa6MBJvo/gF3i4MJ+4iKJWgpAGdFA=; b=K/wHSKNBCjokhiNlz2uGuNFKgk8Kh17AVAw6cwznm3YQokFK9TjXeQJNuPsELiOMqI NA+Venlpd2TLxhdt5JMsI7FTC3fEu6O3Kq86NQ+XNDvs220qDAmE+08aXKMckX6HmA7c 1SJ0Dx+cUhsv+wrWXXoJ+b5xKrM+bboZPUPUU5kuV6q3R1RGLf7KrNdGVe0qpEhqDWqq Ru7esbgLsuivpmp89GBW5QhIDDr66xgE89wnj9ZcZ1HE30twy4SpxWKafWYQfwTSmbaf T2uPpj1OQZ7JyTv1LZtNHp+OEMDVMFgQ9x/Vcdo9fY2//dzYmnbqP5GFw7FRIwWoQLir W/cQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743424310; x=1744029110; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Y1BmGfnz9Zf8enWa6MBJvo/gF3i4MJ+4iKJWgpAGdFA=; b=XqA3zzgvAucC1gcbwSxnn+3MYJ6517E3MiXVuB6C6YFLRVQF5jnnVI9GmRTf+i4USm Ahw73DBlpOFsr9UUIF6wNXThh++Kcj/2jIFBFTbHLCec1zhP420nbFxT7pomskqLdHhd FUQjN46yRKGuRMWm0LzNdgO2su8sB1LagB3vOb+4OHPsp7+YKLlzdBCAOjvByI9agUYx FxCBr8O4pzqMicaX5pvFjKPSA0dsTtvM2xydGriLqPbGYJGdAk9LH4O4zMdrmMWrBWpP ZGcK8bPeWXOP1TqRfFIMCCGMyl1VtxHM9v8aYkUHZFH9GjbqLUV5HichB4myu+Ml8Jnh GDxw== X-Gm-Message-State: AOJu0YwcVKVMdoxugRWuO8ujTNZ+VZdpgwkk7xSdOxsDZP/Bt3D5A9lJ zlAex7+tSkuGNoWIN9BBgg89aV4fjiy7sF2/tkyVSfduMmZqScb3pu6EUrgjWtbKFpXLPZR+JuH tyhiLTg== X-Gm-Gg: ASbGncvDR4TJGcZAAgExBq11CFI9wxLz0iFp/gS1p1nxVBMg7/mC0SXLkmiPgvAYFt7 yFloRhh+UWXdXQwV0e+zZVr6qoOR2PA0drzlAftkqTXEl2j+K5UmIbZ+z7MO07E4Q0qzn2YnTOB OBlJ213WTOy8OkfpOe/CWLIYB2ogy4PwWvA2BjeFTGSzgP+/Nd3T/d1EkRcf1i3BSCFNKJSizbs Jy0E+PiH/YOzG4jKx6HScX34QIMWNYQ14nU0I5zQs4DDPZBVhJTjbRCeMORrHafCVbqpAVm3nWY +Qb/FzDPVlszYDtPkeZVTW3FSye6k0N3r4pmAh/vUXsy6gHuo+kU X-Received: by 2002:a05:6000:1a85:b0:39c:1f04:d151 with SMTP id ffacd0b85a97d-39c1f04d17emr1476163f8f.26.1743424309694; Mon, 31 Mar 2025 05:31:49 -0700 (PDT) Received: from builder.. ([2a01:e0a:3cb:7bb0:f23c:25c9:dc74:be1]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-39c0b6589e4sm11337029f8f.10.2025.03.31.05.31.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:31:49 -0700 (PDT) From: Jerome Forissier To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Jerome Forissier , Marek Vasut , Tom Rini , Mattijs Korpershoek , Heinrich Schuchardt , Dragan Simic Subject: [PATCH v5 12/16] dm: usb: move bus initialization into new static function usb_init_bus() Date: Mon, 31 Mar 2025 14:31:06 +0200 Message-ID: <20250331123120.2025062-13-jerome.forissier@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250331123120.2025062-1-jerome.forissier@linaro.org> References: <20250331123120.2025062-1-jerome.forissier@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean To prepare for the introduction of threads in the USB initialization sequence, move code out of usb_init() into a new helper function: usb_init_bus() and count the number of USB controllers initialized successfully by using the DM device_active() function. Signed-off-by: Jerome Forissier --- drivers/usb/host/usb-uclass.c | 83 +++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 39 deletions(-) diff --git a/drivers/usb/host/usb-uclass.c b/drivers/usb/host/usb-uclass.c index bfec303e7af..fa1af8f555a 100644 --- a/drivers/usb/host/usb-uclass.c +++ b/drivers/usb/host/usb-uclass.c @@ -287,6 +287,45 @@ static int usb_probe_companion(struct udevice *bus) return 0; } +static void usb_init_bus(struct udevice *bus) +{ + int ret; + + /* init low_level USB */ + printf("Bus %s: ", bus->name); + + /* + * For Sandbox, we need scan the device tree each time when we + * start the USB stack, in order to re-create the emulated USB + * devices and bind drivers for them before we actually do the + * driver probe. + * + * For USB onboard HUB, we need to do some non-trivial init + * like enabling a power regulator, before enumeration. + */ + if (IS_ENABLED(CONFIG_SANDBOX) || + IS_ENABLED(CONFIG_USB_ONBOARD_HUB)) { + ret = dm_scan_fdt_dev(bus); + if (ret) { + printf("USB device scan from fdt failed (%d)", ret); + return; + } + } + + ret = device_probe(bus); + if (ret == -ENODEV) { /* No such device. */ + puts("Port not available.\n"); + return; + } + + if (ret) { /* Other error. */ + printf("probe failed, error %d\n", ret); + return; + } + + usb_probe_companion(bus); +} + int usb_init(void) { int controllers_initialized = 0; @@ -305,47 +344,11 @@ int usb_init(void) uc_priv = uclass_get_priv(uc); uclass_foreach_dev(bus, uc) { - /* init low_level USB */ - printf("Bus %s: ", bus->name); - - /* - * For Sandbox, we need scan the device tree each time when we - * start the USB stack, in order to re-create the emulated USB - * devices and bind drivers for them before we actually do the - * driver probe. - * - * For USB onboard HUB, we need to do some non-trivial init - * like enabling a power regulator, before enumeration. - */ - if (IS_ENABLED(CONFIG_SANDBOX) || - IS_ENABLED(CONFIG_USB_ONBOARD_HUB)) { - ret = dm_scan_fdt_dev(bus); - if (ret) { - printf("USB device scan from fdt failed (%d)", ret); - continue; - } - } - - ret = device_probe(bus); - if (ret == -ENODEV) { /* No such device. */ - puts("Port not available.\n"); - controllers_initialized++; - continue; - } - - if (ret) { /* Other error. */ - printf("probe failed, error %d\n", ret); - continue; - } - - ret = usb_probe_companion(bus); - if (ret) - continue; - - controllers_initialized++; - usb_started = true; + usb_init_bus(bus); } + usb_started = true; + /* * lowlevel init done, now scan the bus for devices i.e. search HUBs * and configure them, first scan primary controllers. @@ -354,6 +357,8 @@ int usb_init(void) if (!device_active(bus)) continue; + controllers_initialized++; + priv = dev_get_uclass_priv(bus); if (!priv->companion) usb_scan_bus(bus, true); From patchwork Mon Mar 31 12:31:07 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Forissier X-Patchwork-Id: 877295 Delivered-To: patch@linaro.org Received: by 2002:a5d:6dae:0:b0:38f:210b:807b with SMTP id u14csp1726763wrs; Mon, 31 Mar 2025 05:33:38 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCU0m4f72RDHxc2fT77OUzQHLiIHWM4ThnNqljqtUP26kg4Vqg2y47FdICPAcDmq22xg1ZPcSQ==@linaro.org X-Google-Smtp-Source: AGHT+IG2tECMFr31KbZl9oiO7tQcEqf0B7tFXywl1mOTbr7f7qmgiMogkHefiSLoPmGov+1cBj9L X-Received: by 2002:a05:6102:fa4:b0:4c2:fccb:a647 with SMTP id ada2fe7eead31-4c6c2673a3bmr8656932137.5.1743424418063; Mon, 31 Mar 2025 05:33:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1743424418; cv=none; d=google.com; s=arc-20240605; b=EqX1VI36/4db0E7OsCJsBAKqOB3aGERwim+18MmzgIHb+RD2DLkVYIvWFdpMixxoD6 78E0579HEZs279yxsWK6rLTeBS6Liyc+G13/m2l+kCn/OpvVsEdIaKBwud0iSKBm+Ctz vg9zaevLoImAJYDEstqUNa4lL4tTr5KbL73rSA4SEQd1iptQndLKuvaUGyCK7awPG/UM LqAhHVbxPB38yxNu3AoUVjVX1eaFl413NCDgNfmcB4wOOVmPwgq6PLP80CgDe63AsgGz a0mH7DhpFtHtQufdfRsiz+P7ppNdNsTY9onPJks8UpWn7ddZjReCP97OxcXCcJ/GGEmg pbJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=OKeQ8vI7z2gDAzURuQkmVBQJ2YFTvSpxm63yrjDiD9s=; fh=5aXuylTp+1P39kujRcxnoNnjfMcEteq3Ecakvxei87k=; b=get0Rfc9/X78eoCXtHwlcx3fpra9HOmMI9manIzD9ZmThr8wwe/vavwkubtWiA/U+R zZCPdA6z7X3sAoCa7s+htaHZXztb4Slw+IOul8J8nHGwR6zjxuQqJp10VeeNN9bve5DP /OZoBrxnThslRRRSuQL7akH+DeTu6qwqAPsGLBWAp40pBVazphTRHcBI5LHhBzHS4jfs b1VdEo+wZ0ZLNO9abFnvhhbqvvAVsnMN4yFc3yYdBLqTt+BAWmZ35ZyGlhWLnUHdpxS/ iMAvsJyhqS6bIib1LSILI5K+eSVt4AoK/P5XbOvUffYMjm0NmbBOfRyS2P4lmv4QRsOe wWoQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Rjt+g+zf; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id ada2fe7eead31-4c6bfc8a1f0si1674637137.134.2025.03.31.05.33.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:33:38 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Rjt+g+zf; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 927DB81F95; Mon, 31 Mar 2025 14:31:56 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="Rjt+g+zf"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 15FFE820A1; Mon, 31 Mar 2025 14:31:55 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wm1-x344.google.com (mail-wm1-x344.google.com [IPv6:2a00:1450:4864:20::344]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 15C1C81703 for ; Mon, 31 Mar 2025 14:31:52 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=jerome.forissier@linaro.org Received: by mail-wm1-x344.google.com with SMTP id 5b1f17b1804b1-43ea40a6e98so2976675e9.1 for ; Mon, 31 Mar 2025 05:31:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1743424311; x=1744029111; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=OKeQ8vI7z2gDAzURuQkmVBQJ2YFTvSpxm63yrjDiD9s=; b=Rjt+g+zfjPptWis0bqdXQGWJOnTUZQdxe88zk7/FL9EVDgGzfDE4ITA7PCqjGO6hVr kutop6kWxXg8++sos+OvVWY8M63Gaeh69KSH5wplQqSq+WNAjbMUbX0vN+L+ZXgwg7ek sXbWnP7rFmIviMm/h7jlf3pyfmU3hY+h/Et/A5bOLOvEWJrEa6L9wndU+UP1dGm5b/wR VvuX8jKwYJ/52dc3qVr0gzmiel88lFJlz4MeNRZ/keP0u35aPrO1Z/KhFtCVWcL66TbM tTRDW3pQVPW3orG7XHbRbiLWihlV+3p2Djiavhbik+yMV4nZIFgb5f4/ynFrFVXF/HtY YXiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743424311; x=1744029111; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OKeQ8vI7z2gDAzURuQkmVBQJ2YFTvSpxm63yrjDiD9s=; b=sdSglirdbQxwt2RnmuNtuVSFdDFWAwU77KtILj12uiLXOWVET86rnp1jvPScuswOqO l6T77Akzm/tHX1Vm0YloSYEqkEmyUktRrgbA5IrJPr7zQJONuN0LVz6t3KqpRXWV16Lq OUvtabyX0cnsM4UhnnoCC9AlgPhSzlkNE8pLjcvPBI8PM6ecSXbcKzL6dX+ZBaMpArdF 1H6/wnvh8tCOk79I2hHFFeyVepWqwLL335J0Li7D/SSkpBqZ4ak/OcJ0WYINtV7uiE3Y iNdG87OTvXFEbEaJQpHIpq7XGa3OU6aIkpIhCX/IvVuU2icI+dGZfSXvDMGzRZxP1X26 e6Vg== X-Gm-Message-State: AOJu0YxoUar6uNZnqYdO0W58/Ufh7z+Aexnagwqp0BWm0qHY7mj+drBj 48drtDPHXu/SZhXPcf7/mPLLSK4oeXKFLPtTrE3O8OIUs9HwPAl5Ebj3qb0dvXSGyQzC+HSNqwY mNr537spS X-Gm-Gg: ASbGnctBFNZAAPBbTP2AwlHLMifgkJt8uLnSeiNYAgI/rHQ3gwDyLvA7+8QXfB4Vaj+ GX7P7UzRqCv/9EO2dGzzdgxXzzlxQYqrm2Td/zzs/lGR2dsqr7ReZMnr+3laNfAWt0+SkYBqagE yhjFLm2rt1ihbfCNldRLT5uG1VXEzUicAqfc1/EdsH6KzijZQ8tFZ4lGm5wZTMTGxG5hyLZzy9n 2zbeNo5Srtr0o1P169Y2AeiZ0uqppLtbtUE1C1AEKdOWy0vfaddEnAEzdl/kCD6kpPxIvQimXDp 1651CIq7BfQ1Uyqa9JTZbemjGg0e80AqVqG90fgVHEJ32pJR6Na+0VFx9SfS234= X-Received: by 2002:a05:6000:2805:b0:391:4052:a232 with SMTP id ffacd0b85a97d-39c1211fc2cmr5366124f8f.55.1743424311279; Mon, 31 Mar 2025 05:31:51 -0700 (PDT) Received: from builder.. ([2a01:e0a:3cb:7bb0:f23c:25c9:dc74:be1]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-39c0b6589e4sm11337029f8f.10.2025.03.31.05.31.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:31:50 -0700 (PDT) From: Jerome Forissier To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Jerome Forissier , Marek Vasut , Tom Rini , Simon Glass , Mattijs Korpershoek , Caleb Connolly , Dragan Simic , Heinrich Schuchardt , Julius Lehmann , Guillaume La Roque Subject: [PATCH v5 13/16] dm: usb: initialize and scan multiple buses simultaneously with uthread Date: Mon, 31 Mar 2025 14:31:07 +0200 Message-ID: <20250331123120.2025062-14-jerome.forissier@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250331123120.2025062-1-jerome.forissier@linaro.org> References: <20250331123120.2025062-1-jerome.forissier@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Use the uthread framework to initialize and scan USB buses in parallel for better performance. The console output is slightly modified with a final per-bus report of the number of devices found, common to UTHREAD and !UTHREAD. The USB tests are updated accordingly. Tested on two platforms: 1. arm64 QEMU on a somewhat contrived example (4 USB buses, each with one audio device, one keyboard, one mouse and one tablet) $ make qemu_arm64_defconfig $ make -j$(nproc) CROSS_COMPILE="ccache aarch64-linux-gnu-" $ qemu-system-aarch64 -M virt -nographic -cpu max -bios u-boot.bin \ $(for i in {1..4}; do echo -device qemu-xhci,id=xhci$i \ -device\ usb-{audio,kbd,mouse,tablet},bus=xhci$i.0; \ done) 2. i.MX93 EVK (imx93_11x11_evk_defconfig) with two USB hubs, each with one webcam and one ethernet adapter, resulting in the following device tree: USB device tree: 1 Hub (480 Mb/s, 0mA) | u-boot EHCI Host Controller | +-2 Hub (480 Mb/s, 100mA) | GenesysLogic USB2.1 Hub | +-3 Vendor specific (480 Mb/s, 350mA) | Realtek USB 10/100/1000 LAN 001000001 | +-4 (480 Mb/s, 500mA) HD Pro Webcam C920 8F7CD51F 1 Hub (480 Mb/s, 0mA) | u-boot EHCI Host Controller | +-2 Hub (480 Mb/s, 100mA) | USB 2.0 Hub | +-3 Vendor specific (480 Mb/s, 200mA) | Realtek USB 10/100/1000 LAN 000001 | +-4 (480 Mb/s, 500mA) Generic OnLan-CS30 201801010008 Note that i.MX was tested on top of the downstream repository [1] since USB doesn't work in the upstream master branch. [1] https://github.com/nxp-imx/uboot-imx/tree/lf-6.6.52-2.2.0 commit 6c4545203d12 ("LF-13928 update key for capsule") The time spent in usb_init() ("usb start" command) is reported on the console. Here are the results: | CONFIG_UTHREAD=n | CONFIG_UTHREAD=y --------+------------------+----------------- QEMU | 5628 ms | 2212 ms i.MX93 | 4591 ms | 2441 ms Signed-off-by: Jerome Forissier --- drivers/usb/host/usb-uclass.c | 119 ++++++++++++++++++++++++++++------ test/boot/bootdev.c | 14 ++-- test/boot/bootflow.c | 2 +- 3 files changed, 108 insertions(+), 27 deletions(-) diff --git a/drivers/usb/host/usb-uclass.c b/drivers/usb/host/usb-uclass.c index fa1af8f555a..35684be1207 100644 --- a/drivers/usb/host/usb-uclass.c +++ b/drivers/usb/host/usb-uclass.c @@ -9,6 +9,7 @@ #define LOG_CATEGORY UCLASS_USB #include +#include #include #include #include @@ -17,6 +18,7 @@ #include #include #include +#include static bool asynch_allowed; @@ -172,6 +174,10 @@ int usb_get_max_xfer_size(struct usb_device *udev, size_t *size) return ops->get_max_xfer_size(bus, size); } +#if CONFIG_IS_ENABLED(UTHREAD) +static struct uthread_mutex mutex = UTHREAD_MUTEX_INITIALIZER; +#endif + int usb_stop(void) { struct udevice *bus; @@ -180,10 +186,14 @@ int usb_stop(void) struct usb_uclass_priv *uc_priv; int err = 0, ret; + uthread_mutex_lock(&mutex); + /* De-activate any devices that have been activated */ ret = uclass_get(UCLASS_USB, &uc); - if (ret) + if (ret) { + uthread_mutex_unlock(&mutex); return ret; + } uc_priv = uclass_get_priv(uc); @@ -218,28 +228,23 @@ int usb_stop(void) uc_priv->companion_device_count = 0; usb_started = 0; + uthread_mutex_unlock(&mutex); + return err; } -static void usb_scan_bus(struct udevice *bus, bool recurse) +static void _usb_scan_bus(void *arg) { + struct udevice *bus = (struct udevice *)arg; struct usb_bus_priv *priv; struct udevice *dev; int ret; priv = dev_get_uclass_priv(bus); - assert(recurse); /* TODO: Support non-recusive */ - - printf("scanning bus %s for devices... ", bus->name); - debug("\n"); ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev); if (ret) - printf("failed, error %d\n", ret); - else if (priv->next_addr == 0) - printf("No USB Device found\n"); - else - printf("%d USB Device(s) found\n", priv->next_addr); + printf("Scanning bus %s failed, error %d\n", bus->name, ret); } static void remove_inactive_children(struct uclass *uc, struct udevice *bus) @@ -287,12 +292,12 @@ static int usb_probe_companion(struct udevice *bus) return 0; } -static void usb_init_bus(struct udevice *bus) +static void _usb_init_bus(void *arg) { + struct udevice *bus = (struct udevice *)arg; int ret; /* init low_level USB */ - printf("Bus %s: ", bus->name); /* * For Sandbox, we need scan the device tree each time when we @@ -307,39 +312,96 @@ static void usb_init_bus(struct udevice *bus) IS_ENABLED(CONFIG_USB_ONBOARD_HUB)) { ret = dm_scan_fdt_dev(bus); if (ret) { - printf("USB device scan from fdt failed (%d)", ret); + printf("Bus %s: USB device scan from fdt failed (%d)\n", + bus->name, ret); return; } } ret = device_probe(bus); if (ret == -ENODEV) { /* No such device. */ - puts("Port not available.\n"); + printf("Bus %s: Port not available.\n", bus->name); return; } if (ret) { /* Other error. */ - printf("probe failed, error %d\n", ret); + printf("Bus %s: probe failed, error %d\n", bus->name, ret); return; } usb_probe_companion(bus); } +static int nthr; +static int grp_id; + +static void usb_init_bus(struct udevice *bus) +{ + if (!grp_id) + grp_id = uthread_grp_new_id(); + if (!uthread_create(NULL, _usb_init_bus, (void *)bus, 0, grp_id)) + nthr++; +} + +static void usb_scan_bus(struct udevice *bus, bool recurse) +{ + if (!grp_id) + grp_id = uthread_grp_new_id(); + if (!uthread_create(NULL, _usb_scan_bus, (void *)bus, 0, grp_id)) + nthr++; +} + +static void usb_report_devices(struct uclass *uc) +{ + struct usb_bus_priv *priv; + struct udevice *bus; + + uclass_foreach_dev(bus, uc) { + if (!device_active(bus)) + continue; + priv = dev_get_uclass_priv(bus); + printf("Bus %s: ", bus->name); + if (priv->next_addr == 0) + printf("No USB Device found\n"); + else + printf("%d USB Device(s) found\n", priv->next_addr); + } +} + +static void run_threads(void) +{ +#if CONFIG_IS_ENABLED(UTHREAD) + if (!nthr) + return; + while (!uthread_grp_done(grp_id)) + uthread_schedule(); + nthr = 0; + grp_id = 0; +#endif +} + int usb_init(void) { int controllers_initialized = 0; + unsigned long t0 = timer_get_us(); struct usb_uclass_priv *uc_priv; struct usb_bus_priv *priv; struct udevice *bus; struct uclass *uc; int ret; + uthread_mutex_lock(&mutex); + + if (usb_started) { + ret = 0; + goto out; + } + asynch_allowed = 1; ret = uclass_get(UCLASS_USB, &uc); if (ret) - return ret; + goto out; uc_priv = uclass_get_priv(uc); @@ -347,6 +409,9 @@ int usb_init(void) usb_init_bus(bus); } + if (CONFIG_IS_ENABLED(UTHREAD)) + run_threads(); + usb_started = true; /* @@ -364,6 +429,9 @@ int usb_init(void) usb_scan_bus(bus, true); } + if (CONFIG_IS_ENABLED(UTHREAD)) + run_threads(); + /* * Now that the primary controllers have been scanned and have handed * over any devices they do not understand to their companions, scan @@ -380,21 +448,34 @@ int usb_init(void) } } - debug("scan end\n"); + if (CONFIG_IS_ENABLED(UTHREAD)) + run_threads(); + + usb_report_devices(uc); /* Remove any devices that were not found on this scan */ remove_inactive_children(uc, bus); ret = uclass_get(UCLASS_USB_HUB, &uc); if (ret) - return ret; + goto out; + remove_inactive_children(uc, bus); /* if we were not able to find at least one working bus, bail out */ if (controllers_initialized == 0) printf("No USB controllers found\n"); + debug("USB initialized in %ld ms\n", + (timer_get_us() - t0) / 1000); + + uthread_mutex_unlock(&mutex); + return usb_started ? 0 : -ENOENT; +out: + uthread_mutex_unlock(&mutex); + + return ret; } int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp) diff --git a/test/boot/bootdev.c b/test/boot/bootdev.c index 5f07430714e..70a1d868de8 100644 --- a/test/boot/bootdev.c +++ b/test/boot/bootdev.c @@ -392,8 +392,8 @@ static int bootdev_test_hunter(struct unit_test_state *uts) ut_assert_console_end(); ut_assertok(bootdev_hunt("usb1", false)); - ut_assert_nextline( - "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found"); + ut_assert_skip_to_line( + "Bus usb@1: 5 USB Device(s) found"); ut_assert_console_end(); /* USB is 7th in the list, so bit 8 */ @@ -448,8 +448,8 @@ static int bootdev_test_cmd_hunt(struct unit_test_state *uts) ut_assert_nextline("scanning bus for devices..."); ut_assert_skip_to_line("Hunting with: spi_flash"); ut_assert_nextline("Hunting with: usb"); - ut_assert_nextline( - "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found"); + ut_assert_skip_to_line( + "Bus usb@1: 5 USB Device(s) found"); ut_assert_nextline("Hunting with: virtio"); ut_assert_console_end(); @@ -550,8 +550,8 @@ static int bootdev_test_hunt_prio(struct unit_test_state *uts) ut_assertok(bootdev_hunt_prio(BOOTDEVP_5_SCAN_SLOW, true)); ut_assert_nextline("Hunting with: ide"); ut_assert_nextline("Hunting with: usb"); - ut_assert_nextline( - "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found"); + ut_assert_skip_to_line( + "Bus usb@1: 5 USB Device(s) found"); ut_assert_console_end(); return 0; @@ -603,7 +603,7 @@ static int bootdev_test_hunt_label(struct unit_test_state *uts) ut_assertnonnull(dev); ut_asserteq_str("usb_mass_storage.lun0.bootdev", dev->name); ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS, mflags); - ut_assert_nextlinen("Bus usb@1: scanning bus usb@1"); + ut_assert_nextline("Bus usb@1: 5 USB Device(s) found"); ut_assert_console_end(); return 0; diff --git a/test/boot/bootflow.c b/test/boot/bootflow.c index eb7f00af39a..699ba0edaaf 100644 --- a/test/boot/bootflow.c +++ b/test/boot/bootflow.c @@ -1289,7 +1289,7 @@ static int bootflow_efi(struct unit_test_state *uts) ut_assertok(run_command("bootflow scan", 0)); ut_assert_skip_to_line( - "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) found"); + "Bus usb@1: 5 USB Device(s) found"); ut_assertok(run_command("bootflow list", 0)); From patchwork Mon Mar 31 12:31:08 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Forissier X-Patchwork-Id: 877296 Delivered-To: patch@linaro.org Received: by 2002:a5d:6dae:0:b0:38f:210b:807b with SMTP id u14csp1726844wrs; Mon, 31 Mar 2025 05:33:49 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVy8dnT/aVLRh5J3M2/Uhz6sQOKItXmbdKapLcWayezmd1z8/DCamOSC/l67KCd1w6XGsRb4A==@linaro.org X-Google-Smtp-Source: AGHT+IHeGcUQ+u3oaZsVy+jhwUEdL2GnwGPrrzpBjGH93pnQTYg/1vDC3OALOhFdn3tDXVZ4qiBf X-Received: by 2002:a05:6102:14a8:b0:4ba:eb24:fb28 with SMTP id ada2fe7eead31-4c6d3834d61mr5089457137.3.1743424428993; Mon, 31 Mar 2025 05:33:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1743424428; cv=none; d=google.com; s=arc-20240605; b=IK7e3lKth801uZKdue+czumMlleuRTxL36JzAUJN0p1OGemP+YlANLCjKly3qJ0ZbI 576Dmtrx+CmESjfKbfM8OqwUqTUXgElSzFaWz3XoRTCiF826RO59ihxCQtDhpv8k3Jvd Qv2lEQhM35ovaaxoSj83kmhGB+0sC0JKgzTgkI8kS7yufQ+/kj/l6OV96ioxouJnoj5G sQgJeCDpa+i0gm9lvJv7AM2beqxlx4EEyM9AqzPhwPYQGV3YUKd0PFl2uh+5/rxAK4ek /KSyPgzCEHHStUygBo2WTjZG+M+T/AiWKcWp8tzoNSOC8NoguPOhpZpX69GKk83cnUM6 H+JA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=2aLrWfKtfqnU92onvG84yCpeMIndD96SJj3KPZDlJ8E=; fh=/imu51IA/0W+la1Y3yDjOV2q9WOQSQLY3q/FTVvjP+Y=; b=OUb509KYE24rdfKmsgGVBmHBpa36fhUBoBGfXfHxwBNpOQ/HBf7aajrXZjQLeCqrnZ Su0wbcbUnMUhhcTLIZwMEaKQM/jGyGLW4HQ6iixWgUm7jnLd3F0jNw6sLNZ+oqE2gEfh zOlC00FzO+/ekG5xrONVZyRZEUZZW/mXm+nSRSMgqTMv/LyyWzFXxaj0wrcBA1xPZkO+ UJIDJo0FH+RZDtZqw6PrPdW91V85M/eWFkp0Cl40BeirMZvwatFvvJq7ECr2ahcX8SjI ZsBmiagUDfLvYk7VbA6nQize3UtjqDNdDwo3ocCQwpnpcL445xweGE0daM75pnKKkotB IBgA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WxLbw9ts; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id ada2fe7eead31-4c6bfe4f08asi1585335137.404.2025.03.31.05.33.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:33:48 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WxLbw9ts; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id DF6EA820B0; Mon, 31 Mar 2025 14:31:59 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="WxLbw9ts"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 3C77581F0D; Mon, 31 Mar 2025 14:31:56 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 1678582045 for ; Mon, 31 Mar 2025 14:31:54 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=jerome.forissier@linaro.org Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-43cf257158fso31400065e9.2 for ; Mon, 31 Mar 2025 05:31:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1743424313; x=1744029113; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=2aLrWfKtfqnU92onvG84yCpeMIndD96SJj3KPZDlJ8E=; b=WxLbw9tsm5owP4pxK49oeMIA/Ls9c1/uZM7uXBuDNpXOlGv+kgHlkDyOlPziFyVd1f Oq/vc7BvcsUoVuOE/IyzV1d4uEb9Vkv5zI/3Hp1hlJyEB7Kpe/RJnkrNiM45UElZlBii 0X73H/nVDUgtlmwbAZ9dA4N35xM/LWkBnwNp9QTzGAbfKOR2LUa229sTfAp9RjmQSVFt SkrzPP1W8c/zjFNZAhuFuf26ihQ3Ny6YX42sIDqWIxQqFo9WFHnLSoaDiDIy7Q0R2VJs G3tRYP6jW0A1TpF48UmeTtmYbcqLCjn455HN671gny2VbsBsRG8hjxtt+qCsgbONWFJI aVkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743424313; x=1744029113; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=2aLrWfKtfqnU92onvG84yCpeMIndD96SJj3KPZDlJ8E=; b=tRGVfY6H9hOkTAzSYtxJFRDyKEDBktHPQziChcd3mCl8CK8seXfMnYyAVf8oowj7I2 Ob+oUXQK4vSPNFd1jjKE9sBtaYdxkUFGb/BZpBl8sHjt2C/Pj7a2P+q5/VRoiEYJm2Ze /dFSiAXDvJzYP6c+N2m5vsS8ObUGmuDzDHTlmkAuz6mUHuWLKfy22xC2LXb8OdeFo6Gn da6Z+s05F5GFpHcPEmt6u8F3iFLOtItfsAmatsrAeIpPu2g1km3T+0EzOcWoN9ljIJvQ vUheyi/CUz2RpqtQtFkenU0D8+znEYam09Q6hDwfgZ+dNj8dfoqP2F3haTuD5b6GsD9m S4eA== X-Gm-Message-State: AOJu0YyfQH35bHXSOw3ks4wDQD3Hup5pItuTTjwjkumnk146NIz61x5U DXRU5imlwgynFmWk0W1p5HL0gvaNnRFlLwfoUZQdv7tss2cg3cz3GZD4D/dhQMWnfg/rrFkj/ff Baf/+VQ== X-Gm-Gg: ASbGncurXZ9d8Tn/qb1X5c/upFHOXdn1xXjMwZsgG3oYLRPxkNMIYgQnfWvr/2oqs/b 9oom93kzcjnxLZqHF2ljKPEdf6/MktyNB8VAt/1W4iofH1UkrwVOg9prAmrTuoUWBBMDQSBTK/3 2aVUn0x6N2iGeUwfHTc3KghwHch0a6N0OWqA0qiCAHP4ZVvm7UXIW5FwmLYgNan4YhQr/A8KDh4 o+geV+dEzYeEA10z5nsDA8KraJIG+RG6am5KNn8SMsPIoyKMpwjKhKOT6uIH8AlFfMYp61VqB0O 9guEbagpK7HJMMFDUFI+FOBgGwKsnlqZel+fJVab1XP18qZSyBJj X-Received: by 2002:a05:600c:4eca:b0:43d:abd:ad0e with SMTP id 5b1f17b1804b1-43db6249689mr84364745e9.18.1743424313366; Mon, 31 Mar 2025 05:31:53 -0700 (PDT) Received: from builder.. ([2a01:e0a:3cb:7bb0:f23c:25c9:dc74:be1]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-39c0b6589e4sm11337029f8f.10.2025.03.31.05.31.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:31:52 -0700 (PDT) From: Jerome Forissier To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Jerome Forissier , Mattijs Korpershoek , Tom Rini , Simon Glass , Heinrich Schuchardt , Ibai Erkiaga , Michal Simek , Dmitry Rokosov , Venkatesh Yadav Abbarapu Subject: [PATCH v5 14/16] cmd: add spawn and wait commands Date: Mon, 31 Mar 2025 14:31:08 +0200 Message-ID: <20250331123120.2025062-15-jerome.forissier@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250331123120.2025062-1-jerome.forissier@linaro.org> References: <20250331123120.2025062-1-jerome.forissier@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Add a spawn command which runs another command in the background, as well as a wait command to suspend the shell until one or more background jobs have completed. The job_id environment variable is set by spawn and wait accepts optional job ids, so that one can selectively wait on any job. Example: => date; spawn sleep 5; spawn sleep 3; date; echo "waiting..."; wait; date Date: 2025-02-21 (Friday) Time: 17:04:52 Date: 2025-02-21 (Friday) Time: 17:04:52 waiting... Date: 2025-02-21 (Friday) Time: 17:04:57 => Another example showing how background jobs can make initlizations faster. The board is i.MX93 EVK, with one spinning HDD connected to USB1 via a hub, and a network cable plugged into ENET1. # From power up / reset u-boot=> setenv autoload 0 u-boot=> setenv ud "usb start; dhcp" u-boot=> time run ud [...] time: 8.058 seconds # From power up / reset u-boot=> setenv autoload 0 u-boot=> setenv ud "spawn usb start; spawn dhcp; wait" u-boot=> time run ud [...] time: 4.475 seconds Signed-off-by: Jerome Forissier Acked-by: Ilias Apalodimas Reviewed-by: Mattijs Korpershoek --- cmd/Kconfig | 17 +++++ cmd/Makefile | 2 + cmd/spawn.c | 188 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 207 insertions(+) create mode 100644 cmd/spawn.c diff --git a/cmd/Kconfig b/cmd/Kconfig index cd391d422ae..0cbb75edfbe 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -3079,4 +3079,21 @@ config CMD_MESON help Enable useful commands for the Meson Soc family developed by Amlogic Inc. +config CMD_SPAWN + bool "spawn and wait commands" + depends on UTHREAD + help + spawn runs a command in the background and sets the job_id environment + variable. wait is used to suspend the shell execution until one or more + jobs are complete. + +config CMD_SPAWN_NUM_JOBS + int "Maximum number of simultaneous jobs for spawn" + default 16 + help + Job identifiers are in the range 1..CMD_SPAWN_NUM_JOBS. In other words + there can be no more that CMD_SPAWN_NUM_JOBS running simultaneously. + When a jobs exits, its identifier is available to be re-used by the next + spawn command. + endif diff --git a/cmd/Makefile b/cmd/Makefile index c1275d466c8..b61f6586157 100644 --- a/cmd/Makefile +++ b/cmd/Makefile @@ -239,6 +239,8 @@ obj-$(CONFIG_CMD_SCP03) += scp03.o obj-$(CONFIG_HUSH_SELECTABLE) += cli.o +obj-$(CONFIG_CMD_SPAWN) += spawn.o + obj-$(CONFIG_ARM) += arm/ obj-$(CONFIG_RISCV) += riscv/ obj-$(CONFIG_SANDBOX) += sandbox/ diff --git a/cmd/spawn.c b/cmd/spawn.c new file mode 100644 index 00000000000..5879fff5bbd --- /dev/null +++ b/cmd/spawn.c @@ -0,0 +1,188 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright 2025, Linaro Ltd. + */ + +#include +#include +#include +#include +#include + +/* Spawn arguments and job index */ +struct spa { + int argc; + char **argv; + unsigned int job_idx; +}; + +/* + * uthread group identifiers for each running job + * 0: job slot available, != 0: uthread group id + * Note that job[0] is job_id 1, job[1] is job_id 2 etc. + */ +static unsigned int job[CONFIG_CMD_SPAWN_NUM_JOBS]; +/* + * Return values of the commands run as jobs */ +static enum command_ret_t job_ret[CONFIG_CMD_SPAWN_NUM_JOBS]; + +static void spa_free(struct spa *spa) +{ + int i; + + if (!spa) + return; + + for (i = 0; i < spa->argc; i++) + free(spa->argv[i]); + free(spa->argv); + free(spa); +} + +static struct spa *spa_create(int argc, char *const argv[]) +{ + struct spa *spa; + int i; + + spa = calloc(1, sizeof(*spa)); + if (!spa) + return NULL; + spa->argc = argc; + spa->argv = malloc(argc * sizeof(char *)); + if (!spa->argv) + goto err; + for (i = 0; i < argc; i++) { + spa->argv[i] = strdup(argv[i]); + if (!spa->argv[i]) + goto err; + } + return spa; +err: + spa_free(spa); + return NULL; +} + +static void spawn_thread(void *arg) +{ + struct spa *spa = (struct spa *)arg; + ulong cycles = 0; + int repeatable = 0; + + job_ret[spa->job_idx] = cmd_process(0, spa->argc, spa->argv, + &repeatable, &cycles); + spa_free(spa); +} + +static unsigned int next_job_id(void) +{ + int i; + + for (i = 0; i < CONFIG_CMD_SPAWN_NUM_JOBS; i++) + if (!job[i]) + return i + 1; + + /* No job available */ + return 0; +} + +static void refresh_jobs(void) +{ + int i; + + for (i = 0; i < CONFIG_CMD_SPAWN_NUM_JOBS; i++) + if (job[i] && uthread_grp_done(job[i])) + job[i] = 0; + +} + +static int do_spawn(struct cmd_tbl *cmdtp, int flag, int argc, + char *const argv[]) +{ + unsigned int id; + unsigned int idx; + struct spa *spa; + int ret; + + if (argc == 1) + return CMD_RET_USAGE; + + spa = spa_create(argc - 1, argv + 1); + if (!spa) + return CMD_RET_FAILURE; + + refresh_jobs(); + + id = next_job_id(); + if (!id) + return CMD_RET_FAILURE; + idx = id - 1; + + job[idx] = uthread_grp_new_id(); + + ret = uthread_create(NULL, spawn_thread, spa, 0, job[idx]); + if (ret) { + job[idx] = 0; + return CMD_RET_FAILURE; + } + + ret = env_set_ulong("job_id", id); + if (ret) + return CMD_RET_FAILURE; + + return CMD_RET_SUCCESS; +} + +U_BOOT_CMD(spawn, CONFIG_SYS_MAXARGS, 0, do_spawn, + "run commands and summarize execution time", + "command [args...]\n"); + +static enum command_ret_t wait_job(unsigned int idx) +{ + int prev = disable_ctrlc(false); + + while (!uthread_grp_done(job[idx])) { + if (ctrlc()) { + puts("\n"); + disable_ctrlc(prev); + return CMD_RET_FAILURE; + } + uthread_schedule(); + } + + job[idx] = 0; + disable_ctrlc(prev); + + return job_ret[idx]; +} + +static int do_wait(struct cmd_tbl *cmdtp, int flag, int argc, + char *const argv[]) +{ + enum command_ret_t ret = CMD_RET_SUCCESS; + unsigned long id; + unsigned int idx; + int i; + + if (argc == 1) { + for (i = 0; i < CONFIG_CMD_SPAWN_NUM_JOBS; i++) + if (job[idx]) + ret = wait_job(i); + } else { + for (i = 1; i < argc; i++) { + id = dectoul(argv[i], NULL); + if (id < 0 || id > CONFIG_CMD_SPAWN_NUM_JOBS) + return CMD_RET_USAGE; + idx = (int)id - 1; + ret = wait_job(idx); + } + } + + return ret; +} + +U_BOOT_CMD(wait, CONFIG_SYS_MAXARGS, 0, do_wait, + "wait for one or more jobs to complete", + "[job_id ...]\n" + " - Wait until all specified jobs have exited and return the\n" + " exit status of the last job waited for. When no job_id is\n" + " given, wait for all the background jobs.\n"); From patchwork Mon Mar 31 12:31:09 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Forissier X-Patchwork-Id: 877297 Delivered-To: patch@linaro.org Received: by 2002:a5d:6dae:0:b0:38f:210b:807b with SMTP id u14csp1726920wrs; Mon, 31 Mar 2025 05:33:59 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUDLbDSqS1l/yGWKQ8qi9H1jOZMwpJgB2egLB2w4YdNxkxcLFXyPp9Te6nrBgp7TE0tK08H9g==@linaro.org X-Google-Smtp-Source: AGHT+IENOtSMTddeqAIeAYwtV2OeA1zVx5yW/7zEOSGGvLGtC+/vY558W1clVFCKop9jRQQfa2bz X-Received: by 2002:a05:6102:3f88:b0:4bb:623:e1f7 with SMTP id ada2fe7eead31-4c6d39afc55mr5029705137.16.1743424438825; Mon, 31 Mar 2025 05:33:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1743424438; cv=none; d=google.com; s=arc-20240605; b=kocvc0zgJjFoJxjfmRgW+72tXWd+3IhKu+xIbshcBGasNyRnuVtauYtADfgMDURSZX kig2DIALAnjzOq10XSJwX8WAXuB2aEjx7cHHZ579IiWLMDof7rZ/3KZ3wQ0LQHdhGhPo kC7lUeTWil4mSIbyl4K16rBuOT6JDOKH7Yv9nCSONBgcxWJZXKhUIyl8vu0hzowyqiUi 2ldGBgIClDq2EtmHOw26iKt53eM68UUBErWi4dGueFDgFql7dPjYoKEVWrV/vVHeH36X TfzZjXfVF0nJONiJtEZwB6okAcywKyTuAAwfZw1APBXy+FUTIwn68kE68TFhuh/JH55W K6/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=oFltWPALYkQiMDNUmSg+en3vLnxEiJ0+n8F2dvwsl/U=; fh=UXi5gUnwqAkJNiukY9dZPo8O1bj7wfbxhZ7gyd0YbGk=; b=LrkViicMixw0kkoLln8trewQx0ivEVASzb1FQK/BIzcTgn8XFivLP9bh/C3v8byunj JRohN0DM3n25j5a2Ly4JOr9ihk//hlUKIT8yh1GwrIsIRI2YKFt1EWPcz57aVKT6bcu0 ju8sVCGgLt0yZgcHEyoMh3poPsBqi/7j/jhEyOfLqC1yxAqz2d+rPp6PAwCdwHsZvyUq +U0s49Ew1eRZCWgRdT6MNXSbLHdi6xJ4gVmwkNBWlM5XzR3QjAx5HXvbpvbVnPDc24cD jZxrVnSPe5iT9YS36J7HCwh7yger9FrG2/0hgxL05/ByMXPtCsmLt+fPPBCmnLmCo2Si +CWA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=zTeAqi7K; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id ada2fe7eead31-4c6bfe4f728si1577740137.429.2025.03.31.05.33.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:33:58 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=zTeAqi7K; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 39AD3820E1; Mon, 31 Mar 2025 14:32:00 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="zTeAqi7K"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id CE59C8203B; Mon, 31 Mar 2025 14:31:57 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id C274781703 for ; Mon, 31 Mar 2025 14:31:55 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=jerome.forissier@linaro.org Received: by mail-wm1-x32b.google.com with SMTP id 5b1f17b1804b1-43ce71582e9so30592575e9.1 for ; Mon, 31 Mar 2025 05:31:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1743424315; x=1744029115; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=oFltWPALYkQiMDNUmSg+en3vLnxEiJ0+n8F2dvwsl/U=; b=zTeAqi7Kpax7rmLx2gvAld06GqvAOI+Fjl2OS70DHYpSXVuXzlByFNTHqyhOKn4/vK Eg8eMkHmhxNGETe8wRNB397vJ7PsNJk6Ut5F8AKzpVtWOtW03O20U4Ot3fTQnYeGnk12 +pjmSUwJOwPcWFzILzgEZs1LoaTIoMolh1UCd2hEJONJeTAoc7YQ4UmsJ/Z4YWv/MmGc V4kthj86lFUA7im57fgxK+2+ohPqjEKADvU5Wp7YOXkhAfoHFb637ylVU6pEHqyKQ6Rt PBJy6g3qyQM37U/314InvqMVGjfW8Wr2lJ3uBiGeqDAGfJ0HN2OjIUmtagZioVYaCrA7 oQdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743424315; x=1744029115; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=oFltWPALYkQiMDNUmSg+en3vLnxEiJ0+n8F2dvwsl/U=; b=ipKuQ5XoA5ZC9E5uKvDRh5WcGZNrcp/q/ortiXsQpMc1bKjvx2rkPV0xk9dQ88SmYr blCzkb5FIM/eWwuWkb+EMgOQWYnzkgmSL1ydwM0KdXP83zMhI4FGBqjTxyms48i67X0G zvM60KzS5i7aVrnF1/q2qWT0MN47VrdcUQOVmBfOPW+iQ0DmU1EHkQFCKzRU9vyC9Dxl H758cMRLWe/O4HxXgnZ7b83gvm/WYz4givkdLSBiCPUbY8ybHVKS2krC66nXKjDT5N3s eoIhAcOviSNXoivYQXpBZsm/0cX2IBW5Xl0ik4p3dccKpJofoOQdk8b/O0DNFeJZ1vEk EVAg== X-Gm-Message-State: AOJu0YxiXREJti71VM+3U7LrrM0+uv1OVO3hR/fuNe7GUZ2mnUtK5X2k gXJMSytS9olqvufqW9YVlwLnr9U3T+rNuZMKBTWvCL4j/IzDpkWy/7vFcrShIXpTYtLGOBcqPi9 eZCgZZw== X-Gm-Gg: ASbGncvfxuqC8bHYMOUBo+uoIgeLtxe/vb5JVTMoSPoB8UJ8YDSfbKzoFhBQ3fLpZqP vprjgkwFhmgOi1+8EguPt5ggJ6PtqE2LZag/lvGC+KQ/kwO93oh1fBvkFlBlgPyyUU943mG1m9y 6VNgy7SpQQJsqxEhmwaKkns7dYZRqnSd64v+Ty39O6jLte7aY6cF/w/SGpHTJKtNk+DEnkiDUdM oA9PRqUfRhujz6tKsSDQNrdcuctYKYu0Ci1acnagy1wizRlGsEUe3ohcdjcAM/k1XcH3G7GuG2M cXnpUa/At7QOQFGkuJtRBcdkigz12yGfOp6kOq0YTIsAZahUgJqb X-Received: by 2002:a05:600c:4e12:b0:43d:db5:7af8 with SMTP id 5b1f17b1804b1-43db628c48bmr71536215e9.21.1743424315078; Mon, 31 Mar 2025 05:31:55 -0700 (PDT) Received: from builder.. ([2a01:e0a:3cb:7bb0:f23c:25c9:dc74:be1]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-39c0b6589e4sm11337029f8f.10.2025.03.31.05.31.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:31:54 -0700 (PDT) From: Jerome Forissier To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Jerome Forissier , Tom Rini , Simon Glass , Heinrich Schuchardt Subject: [PATCH v5 15/16] test: cmd: add test for spawn and wait commands Date: Mon, 31 Mar 2025 14:31:09 +0200 Message-ID: <20250331123120.2025062-16-jerome.forissier@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250331123120.2025062-1-jerome.forissier@linaro.org> References: <20250331123120.2025062-1-jerome.forissier@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Test the spawn and wait commands. Signed-off-by: Jerome Forissier Acked-by: Ilias Apalodimas --- test/cmd/Makefile | 1 + test/cmd/spawn.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 test/cmd/spawn.c diff --git a/test/cmd/Makefile b/test/cmd/Makefile index d8a5e77402d..cf47f04851c 100644 --- a/test/cmd/Makefile +++ b/test/cmd/Makefile @@ -39,3 +39,4 @@ obj-$(CONFIG_CMD_WGET) += wget.o endif obj-$(CONFIG_ARM_FFA_TRANSPORT) += armffa.o endif +obj-$(CONFIG_CMD_SPAWN) += spawn.o diff --git a/test/cmd/spawn.c b/test/cmd/spawn.c new file mode 100644 index 00000000000..8f48f5ee25c --- /dev/null +++ b/test/cmd/spawn.c @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Tests for spawn and wait commands + * + * Copyright 2025, Linaro Ltd. + */ + +#include +#include +#include +#include + +static int test_cmd_spawn(struct unit_test_state *uts) +{ + ut_assertok(run_command("wait; spawn sleep 2; setenv j ${job_id}; " + "spawn setenv spawned true; " + "setenv jj ${job_id}; wait; " + "echo ${j} ${jj} ${spawned}", 0)); + console_record_readline(uts->actual_str, sizeof(uts->actual_str)); + ut_asserteq_ptr(uts->actual_str, + strstr(uts->actual_str, "1 2 true")); + + ut_assertok(run_command("spawn true; wait; setenv t $?; spawn false; " + "wait; setenv f $?; wait; echo $t $f $?", 0)); + console_record_readline(uts->actual_str, sizeof(uts->actual_str)); + ut_asserteq_ptr(uts->actual_str, + strstr(uts->actual_str, "0 1 0")); + ut_assert_console_end(); + + return 0; +} +CMD_TEST(test_cmd_spawn, UTF_CONSOLE); From patchwork Mon Mar 31 12:31:10 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Forissier X-Patchwork-Id: 877298 Delivered-To: patch@linaro.org Received: by 2002:a5d:6dae:0:b0:38f:210b:807b with SMTP id u14csp1726999wrs; Mon, 31 Mar 2025 05:34:08 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXJq2VHoM12rPshkyPC/XxwrovoMs6g2ewDqDHimDy/ZFJAxF8RDJzjsstTJTJcv97Dj/Uk+w==@linaro.org X-Google-Smtp-Source: AGHT+IGXpJSLHCqdj/bKacNG6PlyP1X+Dl2kqmaKMZ28vzqOJUFQ1Zsrv5cSOuIbkkDkibsaE97L X-Received: by 2002:a05:6102:149a:b0:4c1:9e65:f90b with SMTP id ada2fe7eead31-4c6d39b0661mr5090923137.17.1743424448521; Mon, 31 Mar 2025 05:34:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1743424448; cv=none; d=google.com; s=arc-20240605; b=MYVt9gWsGSBp6+MUjUBei0o5OzzOkiAkjngqrCFdLF88eVy5pbDttP+AxPq9RuQbQw WOK116gpmz3q3XVAh6Xjr0ecbOTgwZtuZCwkqlb8eIfncHhgZIJAkXjvEuChitTrnsYv 9k6KqQZWUYA+iws+gMB8M+ZvuG0d226ZkwfdPkROsGekGEVIxFTk3baYSAAaYAqqkYK5 PgN3fRU0CbJJdlUI0q+ySlVrHP5GtUHL1yekA9GXxQecbrqlHtcccAln+e6olg2x/BYr eUFNQQIjsUi8VCFx9mkhjuliJYUMJglWH25hp874pd636a9MOXLpDykULpB4HCOjpg7I DvPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Iz+pWSRan5RcbHuOAmgsyvPmB2L2wa+fNWwBN2HgjWU=; fh=xNN7bnxUgrTrxlPgIs75/UrgX/l5yitpr2TwPzmP0Qk=; b=OrTXnWEJwjNXBz6avmJ2hUTPNhD2H2E78uimkbMxzLKPxFMW/IK7QhHUwMfsnZVDAi Hm3wiHq+gq/YDI+swj1erOEGr8onLHfG0vAuLl9qfmnYYrD48I1Zp5cpmp95HFH7mn7M bmU9z7+S+gTxF8wYhUU353R7C4hDkkjo334XXqW3uXbzOp7501XlKD0ouEz8tKYFL3+t vkddqOUs+WQvXk/KEOkkjC4dtcSV0pnLtWRih0J/A2jUGGLJwS8mp1rJ7OGG/31hPXhe h4vgE739OtmEPdvwL7FTaeUEgWGoKOF8OEFVC7q83mRC/m5fq7JNXRxMQZZflmYRoRe8 EW/A==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=oC3RDTlO; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [85.214.62.61]) by mx.google.com with ESMTPS id a1e0cc1a2514c-871a33f44cfsi1356532241.146.2025.03.31.05.34.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:34:08 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) client-ip=85.214.62.61; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=oC3RDTlO; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 85.214.62.61 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id D22A981F59; Mon, 31 Mar 2025 14:32:02 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="oC3RDTlO"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 34DEE81703; Mon, 31 Mar 2025 14:31:59 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wr1-x443.google.com (mail-wr1-x443.google.com [IPv6:2a00:1450:4864:20::443]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id E9C9282047 for ; Mon, 31 Mar 2025 14:31:56 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=jerome.forissier@linaro.org Received: by mail-wr1-x443.google.com with SMTP id ffacd0b85a97d-399737f4fa4so2151486f8f.0 for ; Mon, 31 Mar 2025 05:31:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1743424316; x=1744029116; darn=lists.denx.de; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Iz+pWSRan5RcbHuOAmgsyvPmB2L2wa+fNWwBN2HgjWU=; b=oC3RDTlOsQx5CDiSonf6Xg4x2cIL0Ip5LLVLQjAnudBWdfCQuc2NB6IV+f9apT2si2 mf1n4qZGE8fjUoLMYWalABiRFfAZyCQ0bWbzyR47Mdo6hgtFHL4s92kiOumgl19CmeW9 50H/9KWS7v4AIe80pvi+LFjXEUnvhwhtl9tZH1AzL6p+UfbQDi2oXxfhIwbhZV6i1oys b196AKyWxQOVGSklxF0YNBE3PGNUyH5GXrCpFVRXrNxDLW48txuSDA63/+Cp5yJo8P7Z xFOm3zX6FImxoZoUpn+pMRDQpLQQY1VEstTap5nhjy4sa35Z7ONOXCPZy2k++tU/mCqM wvrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743424316; x=1744029116; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Iz+pWSRan5RcbHuOAmgsyvPmB2L2wa+fNWwBN2HgjWU=; b=DkT0L+n50jzznjhTw3CzlXRGpvDCHdYdxsjQCGWHuKPfwaRymBh7HB5BYzcuVyVRKJ X6IjN1OcJXNKSlWKv/+AWU8HZU5UF+rCBtRIlho8RxHZkRbN7hMk6AFHhYOp2dnRa397 uKzWWrXvzQ84QTHC6yQu7jyCDnDyz5KvXj/P/6R1rXjDMbenOpomeO9xtFGUgEX/SkPZ mtI+kDWviEqet0nH9chxvVivxTm7YJ7PVPPih5jSGZpgjMfkFC0sPpdPmhANIuU4ewIS UonaSmEf9JrFmagfUnZrCpEJ4RUxdrdinWHpszOVFwPrHHpCHr7QC4hOBWArajmiXvAa cUoQ== X-Gm-Message-State: AOJu0Yxoesp/2wvJqy8ZmqP4HuUN3TAnsN0s58e1G4bSEdevMONZHEFs AAbE/Pb3BkdVnXK4uC9h5CVFQnhS7SjIYUM64TFp7II7zJ+VYZtBm4uFztHfo1lSNkM1T5Ejpl3 xzgLfxyTf X-Gm-Gg: ASbGnculS88GwobkUcXGkvdyXN6BYJhnI/EQaJP8RvjnzCurw8e3NDksAoVYjj0ikyM 5K/CcpqWzvpWOgaV79pFVGH7x/fV6G1k8qtWDzmsD45TxYPP3d2W6JnaB1awY9rjjho+6MTP+sX 8vrJNvuEdqEskg4gr59PLnFOA93XRy9nYFctp/mDSTokCLA/C3c78AzGr+AAx9d70pwPYxNQUU1 L0NOpp+BVxO9FIcQSrPU1Wo2D1Mileq0V+S0m3uQR/ZfYj8Xj0VyT40jCDnZvRnCWub8IoK8LPp FTlswZO9oJO7l452LIUx+iJ901xQmmYUxPjjX391GNG5Jr5n0niC X-Received: by 2002:a5d:47a7:0:b0:391:487f:280b with SMTP id ffacd0b85a97d-39c120cb53amr7228862f8f.10.1743424316108; Mon, 31 Mar 2025 05:31:56 -0700 (PDT) Received: from builder.. ([2a01:e0a:3cb:7bb0:f23c:25c9:dc74:be1]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-39c0b6589e4sm11337029f8f.10.2025.03.31.05.31.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Mar 2025 05:31:55 -0700 (PDT) From: Jerome Forissier To: u-boot@lists.denx.de Cc: Ilias Apalodimas , Jerome Forissier , Tom Rini , Greg Malysa , Nathan Barrett-Morrison , Simon Glass , Oliver Gaskell , Mattijs Korpershoek , Caleb Connolly , Sam Protsenko , Robert Marko , Sumit Garg Subject: [PATCH v5 16/16] MAINTAINERS: add UTHREAD Date: Mon, 31 Mar 2025 14:31:10 +0200 Message-ID: <20250331123120.2025062-17-jerome.forissier@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250331123120.2025062-1-jerome.forissier@linaro.org> References: <20250331123120.2025062-1-jerome.forissier@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Add myself as the maintainer for the UTHREAD framework, the spawn/wait commands and the associated tests. Signed-off-by: Jerome Forissier Acked-by: Ilias Apalodimas --- MAINTAINERS | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index 92d4a158fd0..4b6bb7dfcd7 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -1817,6 +1817,15 @@ T: git https://source.denx.de/u-boot/custodians/u-boot-usb.git topic-xhci F: drivers/usb/host/xhci* F: include/usb/xhci.h +UTHREAD +M: Jerome Forissier +S: Maintained +F: lib/uthread.c +F: include/uthread.h +F: test/lib/uthread.c +F: cmd/spawn.c +F: test/cmd/spawn.c + UUID testing M: Abdellatif El Khlifi S: Maintained