From patchwork Sun Jun 25 21:26:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yeqi Fu X-Patchwork-Id: 1799580 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20221208 header.b=awpdXQKt; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Qq3zs0Lh6z20bC for ; Mon, 26 Jun 2023 07:30:33 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qDXGW-0005Ru-3q; Sun, 25 Jun 2023 17:27:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qDXGU-0005RV-Am for qemu-devel@nongnu.org; Sun, 25 Jun 2023 17:27:22 -0400 Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qDXGS-0003P3-J4 for qemu-devel@nongnu.org; Sun, 25 Jun 2023 17:27:22 -0400 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-1b7e1875cc1so3075755ad.1 for ; Sun, 25 Jun 2023 14:27:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687728439; x=1690320439; 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=2iJaGY1IcpK2do/l808hPeHtD6amwR97RNOTv0XTF1E=; b=awpdXQKtVZkYjF7hDlUbQ/jRxu8lzB+Vx+BPoVyGZ0sMaweQ3pSMREXR+tfxRKdRrV 5AXi0GIsSZ26rOiLIwQND8car20tVk4QVSykfJYaam0tH/p6JLV7O72G3PnFbLViSIOE gvfLlNbMIut4W7s27UxQVMrskRGadOMYxlvzmer1Z/KWqmPSnRnmpNaQr46qTyaxZ1/R jvaCU4vj+loJSV0n32j0lnZQCmHedmxMYxCEnDsGzItmXquM/mrgX3hr3J/ONItSRxJ/ /1kzbsqTRJ7AafvcsJOcapmCllx0v8SlXH7rsk7oKG0H8UFuqjKcIQmLxefbb0UKsa9E uw+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687728439; x=1690320439; 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=2iJaGY1IcpK2do/l808hPeHtD6amwR97RNOTv0XTF1E=; b=hqyDP+qDheviK+NHV6+sW0MguoRrNSyO6GQhu2au5nBOdPw3yNv7M62Uk5XrHeiXuv vhxHI6xEe53W2NYCUA8fsULslbIkzTxoyhN1V71E0PR002RBrSr+QkLAqXuKj8M4IWsl iw2bpc5abvAXg+f9wOfpIiGCnKDT6x3Ih57jkcubiIGr3UMbxPXoZZXfhT6dlTuGMpLX VW4SLe+OeLqU/a+fL4ldvtH4WCwzvBCxyWKZ+4B1GK/EeIn+iM6FJGHg0PYMpI4f5e+w 4WvxTW9C1UKK8p2kMTicFWcJjP5Ud9hUnmC9Mf1jgiEW8Grf6cRa2TwN0Q/O0EDcqyRO Z/OQ== X-Gm-Message-State: AC+VfDy6Dad6ka4gizMXCFxvUnO9yTPLmTAoFPxDAO++g6/s7dFbN3r8 VAMg9dF3GPn/3y7tS7z+YPnBda468tQkRnVZ X-Google-Smtp-Source: ACHHUZ7OLMCBsV+h0QWU0THzxWQ/vX0o0YzLRwgO4HhwyHeUsW4VsS4YfgcXxvSDqYdB0goNHPK53w== X-Received: by 2002:a17:903:32d2:b0:1b5:2ec:6ffb with SMTP id i18-20020a17090332d200b001b502ec6ffbmr3114936plr.0.1687728438727; Sun, 25 Jun 2023 14:27:18 -0700 (PDT) Received: from q1iq-virtual-machine.localdomain ([111.201.135.80]) by smtp.gmail.com with ESMTPSA id je5-20020a170903264500b001b8004ff609sm761343plb.270.2023.06.25.14.27.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Jun 2023 14:27:18 -0700 (PDT) From: Yeqi Fu To: alex.bennee@linaro.org Cc: richard.henderson@linaro.org, qemu-devel@nongnu.org, Yeqi Fu Subject: [RFC v3 01/10] docs: Add specification for native library calls Date: Mon, 26 Jun 2023 05:26:58 +0800 Message-Id: <20230625212707.1078951-2-fufuyqqqqqq@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230625212707.1078951-1-fufuyqqqqqq@gmail.com> References: <20230625212707.1078951-1-fufuyqqqqqq@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::634; envelope-from=fufuyqqqqqq@gmail.com; helo=mail-pl1-x634.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HK_RANDOM_ENVFROM=0.001, HK_RANDOM_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Signed-off-by: Yeqi Fu --- docs/native_calls.txt | 70 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 docs/native_calls.txt diff --git a/docs/native_calls.txt b/docs/native_calls.txt new file mode 100644 index 0000000000..8906566b13 --- /dev/null +++ b/docs/native_calls.txt @@ -0,0 +1,70 @@ +Native Library Calls Optimization for QEMU Linux-User +==================== + +Description +=========== +When running under the linux-user mode in QEMU, the entire program, +including all library calls, is translated. Many well-understood +library functions are usually optimized for the processor they run +on. For example, the semantics of memcpy are well-defined and +optimized. Instead of translating these library functions, we can +call their native versions, as the runtime of library functions +is generally biased towards a few core functions. Thus, only a +small subset of functions (such as mem* and str*) would need to +be hooked to be useful. + + +Implementation +============== +This feature introduces a set of specialized instructions for native +calls and provides helpers to translate these instructions to +corresponding native functions. A shared library is also implemented, +where native functions are rewritten as specialized instructions. +At runtime, user programs load the shared library, and specialized +instructions are executed when native functions are called. + +The specialized instructions are implemented using architecture- +specific macros. These macros utilize unused or invalid opcodes or +instruction fields to embed the necessary information for native +function calls. This approach ensures that the specialized +instructions do not conflict with existing instructions. + +For x86 and x86_64, the implementation uses an unused opcode. +For arm and aarch64, the HLT instruction is used, as it is invalid in +userspace and has 16 bits of spare immediate data. +For mips and mips64, the implementation takes advantage of unused +bytes in the syscall instruction. + +Supported Architectures +======================= +This feature is applicable to user programs with the following +architectures now: +- x86 +- x86_64 +- arm +- aarch64 +- mips +- mips64 + + +Usage +===== +1. Install Cross-Compilation Tools +Cross-compilation tools are required to build the shared libraries +that can hook the necessary library functions. For example, a viable +command on Ubuntu is: +``` +apt install libc6:i386 gcc-arm-linux-gnueabihf \ +gcc-aarch64-linux-gnu gcc-mips-linux-gnu gcc-mips64-linux-gnuabi64 +``` +2. Locate the Compiled libnative.so +After compilation, the libnative.so file can be found in the +`./build/common-user/native/-linux-user` directory. + +3. Run the Program with the `--native-bypass` Option +To run your program with native library bypass, use the +`--native-bypass` option to import libnative.so: +``` +./build/qemu- --native-bypass \ +./build/common-user/native/-linux-user/libnative.so ./program +``` From patchwork Sun Jun 25 21:26:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yeqi Fu X-Patchwork-Id: 1799572 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20221208 header.b=axfHZCN6; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Qq3x047fcz20bH for ; Mon, 26 Jun 2023 07:28:04 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qDXGY-0005Um-CB; Sun, 25 Jun 2023 17:27:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qDXGW-0005SE-Qs for qemu-devel@nongnu.org; Sun, 25 Jun 2023 17:27:24 -0400 Received: from mail-pg1-x532.google.com ([2607:f8b0:4864:20::532]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qDXGV-0003Pd-57 for qemu-devel@nongnu.org; Sun, 25 Jun 2023 17:27:24 -0400 Received: by mail-pg1-x532.google.com with SMTP id 41be03b00d2f7-53fbf2c42bfso2267688a12.3 for ; Sun, 25 Jun 2023 14:27:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687728441; x=1690320441; 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=45iYwMbUstRUciB1ggXmjQSsn39gdiC8XNNxy6MC88I=; b=axfHZCN6cfovUyRzehrK2ZD3HIFUJg0okvHzFZluLPc5KNAkhXVBPpN+clfn0ytN0o EBsCEHObvvGHh1SldU1cOe5YvoT8qF889TAMttsiULWNTKqLwdyMqr3zGRxH6/qEinFt YLOj9UI240bR4VfXoy8x+CpE7yexqfYUaGmupOTTb1ua6nfob/EB1cQdRLH2sxxNyCe3 Y+gZxXrjRUK70eRR4KnwJI0xvtWwMqKeQDAPGyJRpH7kEX5rwx645qCJU3g4Ouo85OQw rs+HF2LN8PTdv3WOf0j300ads0PdPIluahmYzlItGvq3JRfyVxDKxlNErMI+/k3m+sJi 4nwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687728441; x=1690320441; 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=45iYwMbUstRUciB1ggXmjQSsn39gdiC8XNNxy6MC88I=; b=f3mERm+d152PJeddCl0g4fk8RshYTQOvoony0jvkX/lrWJhYAAhd3DvUnFa+/AmPMK j2eEQutJ1kTv5QdTTZQ/5ZMdoSA2ow0MBOzQk/yuWBnWnaBikmFDVBYRKeiZ9iU0v8KS 8JPodYtnkMZxp28iYtoQid9UeRm13645yopvyRXUA7qJPxPbq2F5YWEQjRlJmsaCv4bl uyVcE7NjEeLCqrMWOLIHhN40h1iay33nRI+5OYb7lsLRu8Xuf8KeXCqIIaAZJZtR3gLv 4SO4QkoUNv5nFBBkAqCwoxS2EIvJf4+Ye2TKUPYP9eTPY67yPHnLrtKWC/Bnltu/B45i 7WNQ== X-Gm-Message-State: AC+VfDxszezNCfut7KDWEvaTd3boMzC4UF0O8aUTYuZkOzPz2OPmmde/ qLNze4fxVsIKRyiGcD0vR+0= X-Google-Smtp-Source: ACHHUZ6eXXrHOfwftXXwxvXTAb/fhewAKoLHGmBCrg7rRWaiUON1XDPZ6Wycd5ZAG5zXG0zsafMbPg== X-Received: by 2002:a17:902:cecf:b0:1af:cbb1:845 with SMTP id d15-20020a170902cecf00b001afcbb10845mr7001669plg.16.1687728441675; Sun, 25 Jun 2023 14:27:21 -0700 (PDT) Received: from q1iq-virtual-machine.localdomain ([111.201.135.80]) by smtp.gmail.com with ESMTPSA id je5-20020a170903264500b001b8004ff609sm761343plb.270.2023.06.25.14.27.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Jun 2023 14:27:21 -0700 (PDT) From: Yeqi Fu To: alex.bennee@linaro.org Cc: richard.henderson@linaro.org, qemu-devel@nongnu.org, Yeqi Fu , Laurent Vivier , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Jiaxun Yang Subject: [RFC v3 02/10] build: Add configure options for native calls Date: Mon, 26 Jun 2023 05:26:59 +0800 Message-Id: <20230625212707.1078951-3-fufuyqqqqqq@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230625212707.1078951-1-fufuyqqqqqq@gmail.com> References: <20230625212707.1078951-1-fufuyqqqqqq@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::532; envelope-from=fufuyqqqqqq@gmail.com; helo=mail-pg1-x532.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HK_RANDOM_ENVFROM=0.001, HK_RANDOM_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Add CONFIG_NATIVE_CALL to architectures supporting native calls. Signed-off-by: Yeqi Fu --- configs/targets/aarch64-linux-user.mak | 1 + configs/targets/arm-linux-user.mak | 1 + configs/targets/i386-linux-user.mak | 1 + configs/targets/mips-linux-user.mak | 1 + configs/targets/mips64-linux-user.mak | 1 + configs/targets/x86_64-linux-user.mak | 1 + 6 files changed, 6 insertions(+) diff --git a/configs/targets/aarch64-linux-user.mak b/configs/targets/aarch64-linux-user.mak index ba8bc5fe3f..5a8fd98cd9 100644 --- a/configs/targets/aarch64-linux-user.mak +++ b/configs/targets/aarch64-linux-user.mak @@ -4,3 +4,4 @@ TARGET_XML_FILES= gdb-xml/aarch64-core.xml gdb-xml/aarch64-fpu.xml gdb-xml/aarch TARGET_HAS_BFLT=y CONFIG_SEMIHOSTING=y CONFIG_ARM_COMPATIBLE_SEMIHOSTING=y +CONFIG_NATIVE_CALL=y diff --git a/configs/targets/arm-linux-user.mak b/configs/targets/arm-linux-user.mak index 7f5d65794c..f934fb82da 100644 --- a/configs/targets/arm-linux-user.mak +++ b/configs/targets/arm-linux-user.mak @@ -5,3 +5,4 @@ TARGET_XML_FILES= gdb-xml/arm-core.xml gdb-xml/arm-vfp.xml gdb-xml/arm-vfp3.xml TARGET_HAS_BFLT=y CONFIG_SEMIHOSTING=y CONFIG_ARM_COMPATIBLE_SEMIHOSTING=y +CONFIG_NATIVE_CALL=y diff --git a/configs/targets/i386-linux-user.mak b/configs/targets/i386-linux-user.mak index 5b2546a430..2d8bca8f93 100644 --- a/configs/targets/i386-linux-user.mak +++ b/configs/targets/i386-linux-user.mak @@ -2,3 +2,4 @@ TARGET_ARCH=i386 TARGET_SYSTBL_ABI=i386 TARGET_SYSTBL=syscall_32.tbl TARGET_XML_FILES= gdb-xml/i386-32bit.xml +CONFIG_NATIVE_CALL=y diff --git a/configs/targets/mips-linux-user.mak b/configs/targets/mips-linux-user.mak index b4569a9893..fa005d487a 100644 --- a/configs/targets/mips-linux-user.mak +++ b/configs/targets/mips-linux-user.mak @@ -3,3 +3,4 @@ TARGET_ABI_MIPSO32=y TARGET_SYSTBL_ABI=o32 TARGET_SYSTBL=syscall_o32.tbl TARGET_BIG_ENDIAN=y +CONFIG_NATIVE_CALL=y diff --git a/configs/targets/mips64-linux-user.mak b/configs/targets/mips64-linux-user.mak index d2ff509a11..ecfe6bcf73 100644 --- a/configs/targets/mips64-linux-user.mak +++ b/configs/targets/mips64-linux-user.mak @@ -4,3 +4,4 @@ TARGET_BASE_ARCH=mips TARGET_SYSTBL_ABI=n64 TARGET_SYSTBL=syscall_n64.tbl TARGET_BIG_ENDIAN=y +CONFIG_NATIVE_CALL=y diff --git a/configs/targets/x86_64-linux-user.mak b/configs/targets/x86_64-linux-user.mak index 9ceefbb615..a53b017454 100644 --- a/configs/targets/x86_64-linux-user.mak +++ b/configs/targets/x86_64-linux-user.mak @@ -3,3 +3,4 @@ TARGET_BASE_ARCH=i386 TARGET_SYSTBL_ABI=common,64 TARGET_SYSTBL=syscall_64.tbl TARGET_XML_FILES= gdb-xml/i386-64bit.xml +CONFIG_NATIVE_CALL=y From patchwork Sun Jun 25 21:27:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yeqi Fu X-Patchwork-Id: 1799575 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20221208 header.b=VYiY89pg; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Qq3x044fwz20b7 for ; Mon, 26 Jun 2023 07:28:04 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qDXGc-0005WW-7G; Sun, 25 Jun 2023 17:27:30 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qDXGa-0005W1-H6 for qemu-devel@nongnu.org; Sun, 25 Jun 2023 17:27:28 -0400 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qDXGY-0003Qg-3s for qemu-devel@nongnu.org; Sun, 25 Jun 2023 17:27:28 -0400 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-1b8054180acso2652765ad.1 for ; Sun, 25 Jun 2023 14:27:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687728445; x=1690320445; 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=J2/dMof0jXsU0JLgvBiQehLkj/Yhzgw0HXojyG6vukg=; b=VYiY89pg0jzKmVzqyStGcKxrTN9WEDhlXot66MksE4HjbC3V4iK3QRIip5w9vDOrFd 590ndHD+GWHe+kJDIG/hcptdfJFoxrb848mkTkSkE8ohkn8XOeb9ak9t1nz3lW2d/q9i xN0l7pdW21Wv44RiReyFH/7CFD4KhzfhonqeXCJBtDJdfBkOLLU/85vG++VqB/6ipJjO aiehzYHlPe7mfO+Nec1OgkSv2KbDf5DmQeNhNxfieLyDonEc3FzENNvDh55WThWGh8CL 1ErJc3C3oYSm6QQtmH0xPC6p2Ap5/P+8MMALoEhHtvOiv6QzNRtvYheFV2+qEjN6RfFR 70QA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687728445; x=1690320445; 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=J2/dMof0jXsU0JLgvBiQehLkj/Yhzgw0HXojyG6vukg=; b=VbAreOtDCKo0v6ZuK6G9+YRRxYieSLaK7+hDZvWBUJuOqMRTnc/TACEALkm7AY3+Qv nKq9jayZoXvM51BOoe8TWdXhIk9fduJqzGNC/BSCmxtmfbq4BWddzcm1hCRKuN2fb9ut h4QvAoHTpBLVr0FgTQu/sQlIiu603iXCW9osOHhfN7rdBKkuSGfWKMBbmRQrdGvlOlG7 QZ34wJjD+7r4ppOEJkF7JXpKLKCADjmfpIiMhsVKIcShY6raeJCAjBQ1+G9Gvxu+gacY zbRe5AvHO1j3zSw2x4s17o0nZSaIQ88OjhqPJMVQqdxnlE1Oxyu4o/+uijliPBR5jNGK st/A== X-Gm-Message-State: AC+VfDyxvgLcKO+AODobkhRE1ou8ajnWePV7B0gtDmETLNYC+zVMJHDb z9J5hylMa/ZhCnwuat/D+vM= X-Google-Smtp-Source: ACHHUZ5dI1T6NykPlPkgO5aMWRmj9lQrSc7Jm1CNoA8dwLmY8E0LvO9EUQbpv19UCeMzLQqCpd3ZeA== X-Received: by 2002:a17:902:da8c:b0:1af:981b:eeff with SMTP id j12-20020a170902da8c00b001af981beeffmr6346700plx.64.1687728444696; Sun, 25 Jun 2023 14:27:24 -0700 (PDT) Received: from q1iq-virtual-machine.localdomain ([111.201.135.80]) by smtp.gmail.com with ESMTPSA id je5-20020a170903264500b001b8004ff609sm761343plb.270.2023.06.25.14.27.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Jun 2023 14:27:24 -0700 (PDT) From: Yeqi Fu To: alex.bennee@linaro.org Cc: richard.henderson@linaro.org, qemu-devel@nongnu.org, Yeqi Fu , Paolo Bonzini , Thomas Huth , Riku Voipio Subject: [RFC v3 03/10] build: Implement libnative library and configure options Date: Mon, 26 Jun 2023 05:27:00 +0800 Message-Id: <20230625212707.1078951-4-fufuyqqqqqq@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230625212707.1078951-1-fufuyqqqqqq@gmail.com> References: <20230625212707.1078951-1-fufuyqqqqqq@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::632; envelope-from=fufuyqqqqqq@gmail.com; helo=mail-pl1-x632.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HK_RANDOM_ENVFROM=0.001, HK_RANDOM_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This commit implements a shared library, where native functions are rewritten as specialized instructions. At runtime, user programs load the shared library, and specialized instructions are executed when native functions are called. Signed-off-by: Yeqi Fu --- Makefile | 2 + common-user/native/Makefile.include | 9 +++ common-user/native/Makefile.target | 26 +++++++ common-user/native/libnative.c | 112 ++++++++++++++++++++++++++++ configure | 84 ++++++++++++++++----- include/native/libnative.h | 12 +++ include/native/native-defs.h | 65 ++++++++++++++++ 7 files changed, 293 insertions(+), 17 deletions(-) create mode 100644 common-user/native/Makefile.include create mode 100644 common-user/native/Makefile.target create mode 100644 common-user/native/libnative.c create mode 100644 include/native/libnative.h create mode 100644 include/native/native-defs.h diff --git a/Makefile b/Makefile index 3c7d67142f..787b8954a6 100644 --- a/Makefile +++ b/Makefile @@ -185,6 +185,8 @@ SUBDIR_MAKEFLAGS=$(if $(V),,--no-print-directory --quiet) include $(SRC_PATH)/tests/Makefile.include +include $(SRC_PATH)/common-user/native/Makefile.include + all: recurse-all ROMS_RULES=$(foreach t, all clean distclean, $(addsuffix /$(t), $(ROMS))) diff --git a/common-user/native/Makefile.include b/common-user/native/Makefile.include new file mode 100644 index 0000000000..40d20bcd4c --- /dev/null +++ b/common-user/native/Makefile.include @@ -0,0 +1,9 @@ +.PHONY: build-native +build-native: $(NATIVE_TARGETS:%=build-native-library-%) +$(NATIVE_TARGETS:%=build-native-library-%): build-native-library-%: + $(call quiet-command, \ + $(MAKE) -C common-user/native/$* $(SUBDIR_MAKEFLAGS), \ + "BUILD","$* native library") +# endif + +all: build-native diff --git a/common-user/native/Makefile.target b/common-user/native/Makefile.target new file mode 100644 index 0000000000..1bb468a2ec --- /dev/null +++ b/common-user/native/Makefile.target @@ -0,0 +1,26 @@ +# -*- Mode: makefile -*- +# +# Library for native calls +# + +all: +-include ../config-host.mak +-include config-target.mak + +CFLAGS+=-O1 -fPIC -shared -fno-stack-protector -I$(SRC_PATH)/include -D$(TARGET_NAME) +LDFLAGS+= + +ifeq ($(TARGET_NAME),arm) +EXTRA_CFLAGS+=-marm +endif + +SRC = $(SRC_PATH)/common-user/native/libnative.c +LIBNATIVE = libnative.so + +all: $(LIBNATIVE) + +$(LIBNATIVE): $(SRC) + $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $< -o $@ $(LDFLAGS) + +clean: + rm -f $(LIBNATIVE) diff --git a/common-user/native/libnative.c b/common-user/native/libnative.c new file mode 100644 index 0000000000..26a004e3b4 --- /dev/null +++ b/common-user/native/libnative.c @@ -0,0 +1,112 @@ +#include +#include +#include + +#include "native/libnative.h" +#include "native/native-defs.h" + +#if defined(i386) || defined(x86_64) +/* unused opcode */ +#define WRAP_NATIVE_CALL(func_id, abi_map) \ + do { \ + __asm__ volatile(".byte 0x0f,0xff\n\t" \ + ".word %c[imm1],%c[imm2]\n\t" \ + : /* no outputs */ \ + : [imm1] "i"(func_id), [imm2] "i"(abi_map) \ + :); \ + } while (0) + +#endif + +#if defined(arm) || defined(aarch64) +/* + * HLT is an invalid instruction for userspace and usefully has 16 + * bits of spare immeadiate data which we can stuff data in. + */ +#define WRAP_NATIVE_CALL(func_id, abi_map) \ + do { \ + __asm__ volatile("hlt %c0\n\t" \ + "hlt %c1\n\t" \ + : /* no outputs */ \ + : "i"(func_id), "i"(abi_map) \ + : "memory"); \ + } while (0) + +#endif + +#if defined(mips) || defined(mips64) +/* + * There are some unused bytes in the syscall instruction + */ +#define WRAP_NATIVE_CALL(func_id, abi_map) \ + do { \ + __asm__ volatile("syscall %c0\n\t" \ + "syscall %c1\n\t" \ + : /* no outputs */ \ + : "i"(func_id), "i"(abi_map) \ + : "memory"); \ + } while (0) + +#endif + +static inline const uint32_t encode_1out_3in(int rtype, int arg1, int arg2, + int arg3) +{ + return (rtype & 0xf) | ((arg1 & 0xf) << 4) | ((arg2 & 0xf) << 8) | + ((arg3 & 0xf) << 12); +} + +static inline const uint32_t encode_0out_3in(int arg1, int arg2, int arg3) +{ + return encode_1out_3in(TYPE_NO_ARG, arg1, arg2, arg3); +} + +static inline const uint32_t encode_1out_2in(int rtype, int arg1, int arg2) +{ + return encode_1out_3in(rtype, arg1, arg2, TYPE_NO_ARG); +} + +void *memcpy(void *dest, const void *src, size_t n) +{ + const uint32_t args = encode_1out_3in(TYPE_PTR_ARG, TYPE_PTR_ARG_W, + TYPE_PTR_ARG_R, TYPE_IPTR_ARG); + WRAP_NATIVE_CALL(NATIVE_MEMCPY, args); +} + +int memcmp(const void *s1, const void *s2, size_t n) +{ + const uint32_t args = encode_1out_3in(TYPE_INT_ARG, TYPE_PTR_ARG_R, + TYPE_PTR_ARG_R, TYPE_IPTR_ARG); + WRAP_NATIVE_CALL(NATIVE_MEMCMP, args); +} + +void *memset(void *s, int c, size_t n) +{ + const uint32_t args = encode_1out_3in(TYPE_PTR_ARG, TYPE_PTR_ARG_W, + TYPE_INT_ARG, TYPE_IPTR_ARG); + WRAP_NATIVE_CALL(NATIVE_MEMSET, args); +} +char *strncpy(char *dest, const char *src, size_t n) +{ + const uint32_t args = encode_1out_3in(TYPE_PTR_ARG, TYPE_PTR_ARG_W, + TYPE_PTR_ARG_R, TYPE_IPTR_ARG); + WRAP_NATIVE_CALL(NATIVE_STRNCPY, args); +} +int strncmp(const char *s1, const char *s2, size_t n) +{ + const uint32_t args = encode_1out_3in(TYPE_INT_ARG, TYPE_PTR_ARG_R, + TYPE_PTR_ARG_R, TYPE_IPTR_ARG); + WRAP_NATIVE_CALL(NATIVE_STRNCMP, args); +} +char *strcpy(char *dest, const char *src) +{ + const uint32_t args = + encode_1out_2in(TYPE_PTR_ARG, TYPE_PTR_ARG_W, TYPE_PTR_ARG_R); + WRAP_NATIVE_CALL(NATIVE_STRCPY, args); +} +int strcmp(const char *s1, const char *s2) +{ + const uint32_t args = + encode_1out_2in(TYPE_INT_ARG, TYPE_PTR_ARG_R, TYPE_PTR_ARG_R); + WRAP_NATIVE_CALL(NATIVE_STRCMP, args); +} diff --git a/configure b/configure index 2a556d14c9..64edbda892 100755 --- a/configure +++ b/configure @@ -1838,48 +1838,42 @@ if test "$ccache_cpp2" = "yes"; then echo "export CCACHE_CPP2=y" >> $config_host_mak fi -# tests/tcg configuration -(config_host_mak=tests/tcg/config-host.mak -mkdir -p tests/tcg -echo "# Automatically generated by configure - do not modify" > $config_host_mak -echo "SRC_PATH=$source_path" >> $config_host_mak -echo "HOST_CC=$host_cc" >> $config_host_mak +# prepare config files for cross build +config_corss_build_host_mak=cross-build/config-host.mak +mkdir -p cross-build +echo "# Automatically generated by configure - do not modify" > $config_corss_build_host_mak +echo "SRC_PATH=$source_path" >> $config_corss_build_host_mak +echo "HOST_CC=$host_cc" >> $config_corss_build_host_mak # versioned checked in the main config_host.mak above if test -n "$gdb_bin"; then - echo "HAVE_GDB_BIN=$gdb_bin" >> $config_host_mak + echo "HAVE_GDB_BIN=$gdb_bin" >> $config_corss_build_host_mak fi if test "$plugins" = "yes" ; then - echo "CONFIG_PLUGIN=y" >> $config_host_mak + echo "CONFIG_PLUGIN=y" >> $config_corss_build_host_mak fi -tcg_tests_targets= for target in $target_list; do arch=${target%%-*} - case $target in xtensa*-linux-user) - # the toolchain is not complete with headers, only build softmmu tests continue ;; *-softmmu) - test -f "$source_path/tests/tcg/$arch/Makefile.softmmu-target" || continue qemu="qemu-system-$arch" ;; *-linux-user|*-bsd-user) qemu="qemu-$arch" ;; esac - if probe_target_compiler $target || test -n "$container_image"; then test -n "$container_image" && build_static=y - mkdir -p "tests/tcg/$target" - config_target_mak=tests/tcg/$target/config-target.mak - ln -sf "$source_path/tests/tcg/Makefile.target" "tests/tcg/$target/Makefile" + mkdir -p "cross-build/$target" + config_target_mak=cross-build/$target/config-target.mak echo "# Automatically generated by configure - do not modify" > "$config_target_mak" echo "TARGET_NAME=$arch" >> "$config_target_mak" echo "TARGET=$target" >> "$config_target_mak" - write_target_makefile "build-tcg-tests-$target" >> "$config_target_mak" + write_target_makefile "$target" >> "$config_target_mak" echo "BUILD_STATIC=$build_static" >> "$config_target_mak" echo "QEMU=$PWD/$qemu" >> "$config_target_mak" @@ -1887,7 +1881,34 @@ for target in $target_list; do if test "${gdb_arches#*$arch}" != "$gdb_arches"; then echo "HOST_GDB_SUPPORTS_ARCH=y" >> "$config_target_mak" fi + fi +done + + +# tests/tcg configuration +(mkdir -p tests/tcg +ln -srf $config_corss_build_host_mak tests/tcg/config-host.mak + +tcg_tests_targets= +for target in $target_list; do + arch=${target%%-*} + + case $target in + xtensa*-linux-user) + # the toolchain is not complete with headers, only build softmmu tests + continue + ;; + *-softmmu) + test -f "$source_path/tests/tcg/$arch/Makefile.softmmu-target" || continue + ;; + *-linux-user|*-bsd-user) + ;; + esac + if probe_target_compiler $target || test -n "$container_image"; then + mkdir -p "tests/tcg/$target" + ln -srf cross-build/$target/config-target.mak tests/tcg/$target/config-target.mak + ln -sf $source_path/tests/tcg/Makefile.target tests/tcg/$target/Makefile echo "run-tcg-tests-$target: $qemu\$(EXESUF)" >> Makefile.prereqs tcg_tests_targets="$tcg_tests_targets $target" fi @@ -1898,6 +1919,35 @@ if test "$tcg" = "enabled"; then fi ) +# common-user/native configuration +(mkdir -p common-user/native +# reuse the same config-host.mak as tests/tcg +ln -srf $config_corss_build_host_mak common-user/native/config-host.mak + +native_targets= +for target in $target_list; do + arch=${target%%-*} + # Skip targets that are not supported + case "$arch" in + i386|x86_64|arm|aarch64|mips|mips64) + case $target in + *-linux-user|*-bsd-user) + if probe_target_compiler $target || test -n "$container_image"; then + mkdir -p "common-user/native/$target" + # reuse the same config-target.mak as tests/tcg + ln -srf cross-build/$target/config-target.mak common-user/native/$target/config-target.mak + ln -sf $source_path/common-user/native/Makefile.target common-user/native/$target/Makefile + native_targets="$native_targets $target" + fi + ;; + esac + ;; + esac +done + +echo "NATIVE_TARGETS=$native_targets" >> config-host.mak +) + if test "$skip_meson" = no; then cross="config-meson.cross.new" meson_quote() { diff --git a/include/native/libnative.h b/include/native/libnative.h new file mode 100644 index 0000000000..25a59833db --- /dev/null +++ b/include/native/libnative.h @@ -0,0 +1,12 @@ +#ifndef __LIBNATIVE_H__ +#define __LIBNATIVE_H__ + +void *memcpy(void *dest, const void *src, size_t n); +int memcmp(const void *s1, const void *s2, size_t n); +void *memset(void *s, int c, size_t n); +char *strncpy(char *dest, const char *src, size_t n); +int strncmp(const char *s1, const char *s2, size_t n); +char *strcpy(char *dest, const char *src); +int strcmp(const char *s1, const char *s2); + +#endif /* __LIBNATIVE_H__ */ diff --git a/include/native/native-defs.h b/include/native/native-defs.h new file mode 100644 index 0000000000..37ed479408 --- /dev/null +++ b/include/native/native-defs.h @@ -0,0 +1,65 @@ +#ifndef __NATIVE_FUNC_H__ +#define __NATIVE_FUNC_H__ + +/* + * Native function IDs. These are used to identify the native function + */ +#define NATIVE_MEMCPY 0x1001 +#define NATIVE_MEMCMP 0x1002 +#define NATIVE_MEMSET 0x1003 +#define NATIVE_STRNCPY 0x1004 +#define NATIVE_STRNCMP 0x1005 +#define NATIVE_STRCPY 0x2001 +#define NATIVE_STRCMP 0x2002 + +/* + * Argument encoding. We only really care about 3 types. The two base + * register sizes (32 and 64) and if the value is a pointer (in which + * case we need to adjust it g2h before passing to the native + * function). + */ +#define TYPE_NO_ARG 0x0 +#define TYPE_I32_ARG 0x1 /* uint32_t */ +#define TYPE_I64_ARG 0x2 /* uint64_t */ +#define TYPE_INT_ARG 0x3 /* int */ +#define TYPE_PTR_ARG 0x4 /* void* */ +#define TYPE_PTR_ARG_R 0x5 +#define TYPE_PTR_ARG_W 0x6 + +/* + * Add an alias for the natural register size, it might be easier to + * pass this in. + */ +#if UINTPTR_MAX == 0xFFFFFFFF +#define TYPE_IPTR_ARG TYPE_I32_ARG +#elif UINTPTR_MAX == 0xFFFFFFFFFFFFFFFFu +#define TYPE_IPTR_ARG TYPE_I64_ARG +#else +#error TBD pointer size +#endif + +#define GET_RVALUE(types) ((types)&0xf) +#define GET_ARG1(types) (((types) >> 4) & 0xf) +#define GET_ARG2(types) (((types) >> 8) & 0xf) +#define GET_ARG3(types) (((types) >> 12) & 0xf) + +#define IS_PTR_RVALUE(types) (GET_RVALUE(types) >= TYPE_PTR_ARG) +#define IS_PTR_ARG1(types) (GET_ARG1(types) >= TYPE_PTR_ARG) +#define IS_PTR_ARG2(types) (GET_ARG2(types) >= TYPE_PTR_ARG) +#define IS_PTR_ARG3(types) (GET_ARG3(types) >= TYPE_PTR_ARG) + +#define IS_ARG_PTR(type) (type >= TYPE_PTR_ARG) +#define IS_ARG_READ(type) (type == TYPE_PTR_ARG_R) +#define IS_ARG_WRITE(type) (type == TYPE_PTR_ARG_W) + +/* + * Check if the native bypass feature is enabled. + */ +#if defined(CONFIG_USER_ONLY) && defined(CONFIG_NATIVE_CALL) +extern bool native_bypass_enabled; +#define native_call_enabled() (native_bypass_enabled) +#else +#define native_call_enabled() 0 +#endif + +#endif /* __NATIVE_FUNC_H__ */ From patchwork Sun Jun 25 21:27:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yeqi Fu X-Patchwork-Id: 1799574 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20221208 header.b=mKgk3wLr; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Qq3x11BnMz214s for ; Mon, 26 Jun 2023 07:28:04 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qDXGd-0005X9-Vp; Sun, 25 Jun 2023 17:27:32 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qDXGc-0005WY-B5 for qemu-devel@nongnu.org; Sun, 25 Jun 2023 17:27:30 -0400 Received: from mail-pg1-x532.google.com ([2607:f8b0:4864:20::532]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qDXGa-0003RG-Kk for qemu-devel@nongnu.org; Sun, 25 Jun 2023 17:27:30 -0400 Received: by mail-pg1-x532.google.com with SMTP id 41be03b00d2f7-544c0d768b9so2285794a12.0 for ; Sun, 25 Jun 2023 14:27:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687728447; x=1690320447; 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=rpQLE595W5oqS4gd0K1Yg8xwY1BVVo0MaNKXsQUnaeY=; b=mKgk3wLrtCL2djrfdgTWO4ndKpe8Oow0BjJQdjTfltnQGd+B74R3f4y9H0jvS6dqu5 kRMz0qqnJHTRzzpKa2hR5EeuNS3wCApL/xeij8kE57m+G5NuZJuO5aCzF7fhT9De+6gA DJvcKZtJEWixOtuvpc9T6PKzX9yl5MXeXIVeRE/9nUJnh1/imBD+o046JSYpC/5fG6Yo si8wf0klCsL7V4tgWmS4K0M4ErRkKBsLsKUSngIWdtWld54Nz+olxLjM8PjbUD6nIpV4 PYTTtlof+59O4FWic5UxRSY+6Q5skIgMmvKQIjolR1eh4UEL22ZMiPz8wrLvG7hqfKFq WMcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687728447; x=1690320447; 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=rpQLE595W5oqS4gd0K1Yg8xwY1BVVo0MaNKXsQUnaeY=; b=PRIaOSU+mngPANb6NjAxFOH313AnLqLIk6UOwJdkw4q6UBPOCHRimUlYkRuaNgnIWS jOHLrDOxZk4/dLx+jOIIWkjrZBISsOUmw4udSzH9N9x42iCy1dxwO+JhOj8rUm/ecFzk x+nE5WN3PSUt6wp0O09WuV96MFAcrMnRNQyBJe7Kq/x4/jfNTdrDUYzASD1UUQY6QaNd jitXsnXjdAxRH7es1cNYHX+pxf4Z17o54FPmicDpMGMNbUoyROIRPlezuvXinDYQIXVO nc2JP2K3iTC4voEHVVhuEC0+VV95MbrOLS4hZQ8OFnrKOw3saRlhKTDcyZWlVrT2AHfj 1hDQ== X-Gm-Message-State: AC+VfDxjK0zy1VvR6PV+J3bHlEIpNH17fED1S7cMTtyru6KxZ1mEpN8b jh+A01umpky2N0ZHtvQwC0Xf/1RKho2PlGHy X-Google-Smtp-Source: ACHHUZ4tupI9B4rV88jXPK11V4Mkp/r8fS66b8cVYFyQuZHHAvykl+61Gz7KQMXAWLDIf2Jqpk1gdg== X-Received: by 2002:a17:902:7445:b0:1b3:dcf1:97 with SMTP id e5-20020a170902744500b001b3dcf10097mr6607388plt.25.1687728447262; Sun, 25 Jun 2023 14:27:27 -0700 (PDT) Received: from q1iq-virtual-machine.localdomain ([111.201.135.80]) by smtp.gmail.com with ESMTPSA id je5-20020a170903264500b001b8004ff609sm761343plb.270.2023.06.25.14.27.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Jun 2023 14:27:27 -0700 (PDT) From: Yeqi Fu To: alex.bennee@linaro.org Cc: richard.henderson@linaro.org, qemu-devel@nongnu.org, Yeqi Fu Subject: [RFC v3 04/10] linux-user: Implement envlist_appendenv Date: Mon, 26 Jun 2023 05:27:01 +0800 Message-Id: <20230625212707.1078951-5-fufuyqqqqqq@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230625212707.1078951-1-fufuyqqqqqq@gmail.com> References: <20230625212707.1078951-1-fufuyqqqqqq@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::532; envelope-from=fufuyqqqqqq@gmail.com; helo=mail-pg1-x532.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HK_RANDOM_ENVFROM=0.001, HK_RANDOM_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Signed-off-by: Yeqi Fu --- include/qemu/envlist.h | 1 + util/envlist.c | 61 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/include/qemu/envlist.h b/include/qemu/envlist.h index 6006dfae44..865eb18e17 100644 --- a/include/qemu/envlist.h +++ b/include/qemu/envlist.h @@ -7,6 +7,7 @@ envlist_t *envlist_create(void); void envlist_free(envlist_t *); int envlist_setenv(envlist_t *, const char *); int envlist_unsetenv(envlist_t *, const char *); +int envlist_appendenv(envlist_t *, const char *, const char *); int envlist_parse_set(envlist_t *, const char *); int envlist_parse_unset(envlist_t *, const char *); char **envlist_to_environ(const envlist_t *, size_t *); diff --git a/util/envlist.c b/util/envlist.c index db937c0427..635c9c4fab 100644 --- a/util/envlist.c +++ b/util/envlist.c @@ -201,6 +201,67 @@ envlist_unsetenv(envlist_t *envlist, const char *env) return (0); } +/* + * Appends environment value to envlist. If the environment + * variable already exists, the new value is appended to the + * existing one. + * + * Returns 0 in success, errno otherwise. + */ +int +envlist_appendenv(envlist_t *envlist, const char *env, const char *separator) +{ + struct envlist_entry *entry = NULL; + const char *eq_sign; + size_t envname_len; + + if ((envlist == NULL) || (env == NULL) || (separator == NULL)) { + return (EINVAL); + } + + /* find out first equals sign in given env */ + eq_sign = strchr(env, '='); + if (eq_sign == NULL) { + return (EINVAL); + } + + if (strchr(eq_sign + 1, '=') != NULL) { + return (EINVAL); + } + + envname_len = eq_sign - env + 1; + + /* + * If there already exists variable with given name, + * we append the new value to the existing one. + */ + for (entry = envlist->el_entries.lh_first; entry != NULL; + entry = entry->ev_link.le_next) { + if (strncmp(entry->ev_var, env, envname_len) == 0) { + break; + } + } + + if (entry != NULL) { + char *new_env_value = NULL; + size_t new_env_len = strlen(entry->ev_var) + strlen(eq_sign) + + strlen(separator) + 1; + new_env_value = g_malloc(new_env_len); + strcpy(new_env_value, entry->ev_var); + strcat(new_env_value, separator); + strcat(new_env_value, eq_sign + 1); + g_free((char *)entry->ev_var); + entry->ev_var = new_env_value; + } else { + envlist->el_count++; + entry = g_malloc(sizeof(*entry)); + entry->ev_var = g_strdup(env); + QLIST_INSERT_HEAD(&envlist->el_entries, entry, ev_link); + } + + return (0); +} + /* * Returns given envlist as array of strings (in same form that * global variable environ is). Caller must free returned memory From patchwork Sun Jun 25 21:27:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yeqi Fu X-Patchwork-Id: 1799581 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20221208 header.b=W7BMKYky; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Qq3zz62zgz20bC for ; Mon, 26 Jun 2023 07:30:39 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qDXGf-0005Xb-Rx; Sun, 25 Jun 2023 17:27:33 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qDXGe-0005XS-Mu for qemu-devel@nongnu.org; Sun, 25 Jun 2023 17:27:32 -0400 Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qDXGd-0003TN-39 for qemu-devel@nongnu.org; Sun, 25 Jun 2023 17:27:32 -0400 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-1b7f223994fso9910195ad.3 for ; Sun, 25 Jun 2023 14:27:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687728450; x=1690320450; 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=woKauxxnRk032i0YAZuB3emU7zROXu8ro6a8hM18j5o=; b=W7BMKYkygNUIgxVdkALyUDChWylTA2HSNwkw/Ne6r1uPqc+aBcAYBaFZC/4MAi/pZ7 lD15tySLYLsfafVnYsCDdXbKkUzsOrOEVCIMoHwUKDW/saaT9j1EnWU0KqUT0lavNFQo quorygBy14C3KUQdObkRT+LEFHeTA+whZ0TX/psKPqdGqNEY4oHxyy9GniHfa3NPs69f coTY5/bOT7pGzcYumWq3dlGK/Dms8dgmel+FLoK+cBLFoap0Ff0Aq0+skenco5bcp+D4 4QzVqV9BQaJH+23s8A4+JkPOHZra+hkMpfsEnxhfR0POwqClIQAin/x9KOj6HpymNnko n8/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687728450; x=1690320450; 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=woKauxxnRk032i0YAZuB3emU7zROXu8ro6a8hM18j5o=; b=LGq05u7UKFdhWbvRcZuu8JK++jO2yVogs8mYaULW43cCfAjTg6/K51WzoSu48fauK3 WOHXxHm+05oLKAd9j3FDZ6pVyQyujSIo8IiTjsBRBLvzCWpsR5ciZNDXXskahrmJZtX+ KvrSWMwBT3n0Ss7fuEC4ZAbKoYHCfH9BtG5P2eVMbn0ja7/tzeZoXp3q/6lFOHdlNCt6 t73KhvSDirCe6G5U0Jukimt/riO9wb9Wpa3rRZJzvyxdFYOXit2Of43IRb4Ttepk3cnS dERSRQ6ylHpF/P3uQy0QXBeJxOFt507vOq3iFAXU4U+4LpC80vWURCgAvZhChByyIEQv pqrA== X-Gm-Message-State: AC+VfDxseb81dW/hd7jMMSZVamXFNKzpUXzcTPPudMozloJw2RABhb6I FXe+V8iQWJzBfn0vam+9ekw= X-Google-Smtp-Source: ACHHUZ5z1DMD4vOLhIuw1JKfxvDTtft+ji+RICMTE3cJ6X1nW+qTuLfnM797K3LYDFaHcVxNGZlDJw== X-Received: by 2002:a17:902:eac4:b0:1b7:f063:301a with SMTP id p4-20020a170902eac400b001b7f063301amr3867022pld.24.1687728449803; Sun, 25 Jun 2023 14:27:29 -0700 (PDT) Received: from q1iq-virtual-machine.localdomain ([111.201.135.80]) by smtp.gmail.com with ESMTPSA id je5-20020a170903264500b001b8004ff609sm761343plb.270.2023.06.25.14.27.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Jun 2023 14:27:29 -0700 (PDT) From: Yeqi Fu To: alex.bennee@linaro.org Cc: richard.henderson@linaro.org, qemu-devel@nongnu.org, Yeqi Fu , Laurent Vivier Subject: [RFC v3 05/10] linux-user: Implement native-bypass option support Date: Mon, 26 Jun 2023 05:27:02 +0800 Message-Id: <20230625212707.1078951-6-fufuyqqqqqq@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230625212707.1078951-1-fufuyqqqqqq@gmail.com> References: <20230625212707.1078951-1-fufuyqqqqqq@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62d; envelope-from=fufuyqqqqqq@gmail.com; helo=mail-pl1-x62d.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HK_RANDOM_ENVFROM=0.001, HK_RANDOM_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This commit implements the -native-bypass support in linux-user. The native_calls_enabled() function can be true only when the '-native-bypass' option is given. Signed-off-by: Yeqi Fu --- linux-user/main.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/linux-user/main.c b/linux-user/main.c index 5e6b2e1714..98e31c77d5 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -60,6 +60,13 @@ #include "semihosting/semihost.h" #endif +#if defined(CONFIG_NATIVE_CALL) +#include "native/native-defs.h" + +static const char *native_lib; +bool native_bypass_enabled; +#endif + #ifndef AT_FLAGS_PRESERVE_ARGV0 #define AT_FLAGS_PRESERVE_ARGV0_BIT 0 #define AT_FLAGS_PRESERVE_ARGV0 (1 << AT_FLAGS_PRESERVE_ARGV0_BIT) @@ -125,6 +132,7 @@ static void usage(int exitcode); static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX; const char *qemu_uname_release; + #if !defined(TARGET_DEFAULT_STACK_SIZE) /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so we allocate a bigger stack. Need a better solution, for example @@ -293,6 +301,18 @@ static void handle_arg_set_env(const char *arg) free(r); } +#if defined(CONFIG_NATIVE_CALL) +static void handle_arg_native_bypass(const char *arg) +{ + if (access(arg, F_OK) != 0) { + fprintf(stderr, "native library %s does not exist\n", arg); + exit(EXIT_FAILURE); + } + native_lib = arg; + native_bypass_enabled = true; +} +#endif + static void handle_arg_unset_env(const char *arg) { char *r, *p, *token; @@ -522,6 +542,10 @@ static const struct qemu_argument arg_table[] = { "", "Generate a /tmp/perf-${pid}.map file for perf"}, {"jitdump", "QEMU_JITDUMP", false, handle_arg_jitdump, "", "Generate a jit-${pid}.dump file for perf"}, +#if defined(CONFIG_NATIVE_CALL) + {"native-bypass", "QEMU_NATIVE_BYPASS", true, handle_arg_native_bypass, + "", "native bypass for library calls in user mode only."}, +#endif {NULL, NULL, false, NULL, NULL, NULL} }; @@ -826,6 +850,18 @@ int main(int argc, char **argv, char **envp) } } +#if defined(CONFIG_NATIVE_CALL) + /* Set the library for native bypass */ + if (native_bypass_enabled) { + GString *lib = g_string_new(native_lib); + lib = g_string_prepend(lib, "LD_PRELOAD="); + if (envlist_appendenv(envlist, g_string_free(lib, false), ":") != 0) { + fprintf(stderr, + "failed to append the native library to environment.\n"); + exit(EXIT_FAILURE); + } + } +#endif target_environ = envlist_to_environ(envlist, NULL); envlist_free(envlist); From patchwork Sun Jun 25 21:27:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yeqi Fu X-Patchwork-Id: 1799579 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20221208 header.b=NylhQXo9; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Qq3zF1NQjz20b7 for ; Mon, 26 Jun 2023 07:30:01 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qDXGl-0005YP-AS; Sun, 25 Jun 2023 17:27:39 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qDXGi-0005Y0-DQ for qemu-devel@nongnu.org; Sun, 25 Jun 2023 17:27:36 -0400 Received: from mail-pg1-x534.google.com ([2607:f8b0:4864:20::534]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qDXGf-0003U4-TM for qemu-devel@nongnu.org; Sun, 25 Jun 2023 17:27:35 -0400 Received: by mail-pg1-x534.google.com with SMTP id 41be03b00d2f7-544c0d768b9so2285847a12.0 for ; Sun, 25 Jun 2023 14:27:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687728452; x=1690320452; 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=rt6ZHxAj6+7mhUCms5O9p6L2Lf2FaYgxsL9F9giKDX4=; b=NylhQXo90AMxOR7Axwmel6Z3DbqRuBKHqLisVxQ3s/BpgPQFlEHYYtFRNxUV3uFHT0 VAg80W9lNCm0ZkekvCvR/X8P5Rq9Z64xeesikwtG6+qBPVgrQlFUnd2QYivLxvdU6miL yu1Fye8VUm4QOJrnkfeno02vJHi+2vus10X1hX2DNaGfEEuJ/ru2cP8B0WPXUyJFuuGN 8//GxLEBKBLs9wEnqA/Z/DWz+cNPlL4JFw4xIpdk/2BRb4XXUEx7GoKCmD7nivJD947E 2gQHN5COl1NEQwbPH/gqx2WUyov8q2VkfjAM2kto0bWu9rL3lF5aGDoBL4mPMuj43vIP i2kA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687728452; x=1690320452; 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=rt6ZHxAj6+7mhUCms5O9p6L2Lf2FaYgxsL9F9giKDX4=; b=BJLkkn51MoB+Pam5eIObtkOszANsmQawxo/bEgbXLPGABCg9SA9gYQ3hecrX5IwJix Glw4kvcTTjVSkLZjgvyZJY1L/rFP4CjeIbTPsBd9cAM66tFqALSp8Xk0j6ayU85m6mE2 KAe87LCVsmeSfTMmZRH0SztsMYWhV2gGw88ouuLlLZIvth0xTqqPMdReWguYIJSL0dcb h0Z2dO7FLn+OMOWJk4WfP2rkATz/LFCYocCj3uCGSQgCQ0iC0V/kow72MuFG1/+EO+7W E6j0hBNlxZHiohOwHfNDccEKPzdffdPAaL+p55ry3enBbi1gxqS6CApJiE+LCn7PCnMM DBlA== X-Gm-Message-State: AC+VfDyAHWmmkDkLgeot91bD2+4Gjc2xhdPvJsUIHcD+2lM6hmBmc3FT 0WM9+tUKWF7fh8+u9C9etzg= X-Google-Smtp-Source: ACHHUZ4f+Cu8d1RiLxGssLRG9R2vhuDsVe1PlncUIj00IkxQJd1yLb+BEOixG485KJ71kCDG79gzTA== X-Received: by 2002:a17:902:c407:b0:1ac:8717:d436 with SMTP id k7-20020a170902c40700b001ac8717d436mr7152313plk.60.1687728452368; Sun, 25 Jun 2023 14:27:32 -0700 (PDT) Received: from q1iq-virtual-machine.localdomain ([111.201.135.80]) by smtp.gmail.com with ESMTPSA id je5-20020a170903264500b001b8004ff609sm761343plb.270.2023.06.25.14.27.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Jun 2023 14:27:32 -0700 (PDT) From: Yeqi Fu To: alex.bennee@linaro.org Cc: richard.henderson@linaro.org, qemu-devel@nongnu.org, Yeqi Fu , Paolo Bonzini Subject: [RFC v3 06/10] accel/tcg: Add support for native library calls Date: Mon, 26 Jun 2023 05:27:03 +0800 Message-Id: <20230625212707.1078951-7-fufuyqqqqqq@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230625212707.1078951-1-fufuyqqqqqq@gmail.com> References: <20230625212707.1078951-1-fufuyqqqqqq@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::534; envelope-from=fufuyqqqqqq@gmail.com; helo=mail-pg1-x534.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HK_RANDOM_ENVFROM=0.001, HK_RANDOM_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This commit implements several helpers for extracting function id and argument types, and then using this information to invoke native functions. Signed-off-by: Yeqi Fu --- accel/tcg/tcg-runtime.c | 37 +++++++++++++++++ accel/tcg/tcg-runtime.h | 4 ++ include/native/native-calls.h | 75 +++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 include/native/native-calls.h diff --git a/accel/tcg/tcg-runtime.c b/accel/tcg/tcg-runtime.c index e4e030043f..3f173f2a53 100644 --- a/accel/tcg/tcg-runtime.c +++ b/accel/tcg/tcg-runtime.c @@ -148,3 +148,40 @@ void HELPER(exit_atomic)(CPUArchState *env) { cpu_loop_exit_atomic(env_cpu(env), GETPC()); } + +target_ulong helper_native_call(CPUArchState *env, target_ulong arg1, + target_ulong arg2, target_ulong arg3, + uint32_t abi_map, uint32_t func_id, uint32_t mmu_idx) +{ + if (GET_ARG3(abi_map)) { + return do_native_3in1out(env, arg1, arg2, arg3, abi_map, func_id, + mmu_idx); + } else { + return do_native_2in1out(env, arg1, arg2, abi_map, func_id, mmu_idx); + } +} + + +uint32_t helper_native_call_i32(CPUArchState *env, uint32_t arg1, uint32_t arg2, + uint32_t arg3, uint32_t abi_map, + uint32_t func_id, uint32_t mmu_idx) +{ + if (GET_ARG3(abi_map)) { + return do_native_3in1out(env, arg1, arg2, arg3, abi_map, func_id, + mmu_idx); + } else { + return do_native_2in1out(env, arg1, arg2, abi_map, func_id, mmu_idx); + } +} + +uint64_t helper_native_call_i64(CPUArchState *env, uint64_t arg1, uint64_t arg2, + uint64_t arg3, uint32_t abi_map, + uint32_t func_id, uint32_t mmu_idx) +{ + if (GET_ARG3(abi_map)) { + return do_native_3in1out(env, arg1, arg2, arg3, abi_map, func_id, + mmu_idx); + } else { + return do_native_2in1out(env, arg1, arg2, abi_map, func_id, mmu_idx); + } +} diff --git a/accel/tcg/tcg-runtime.h b/accel/tcg/tcg-runtime.h index 6f8c2061d0..3b1fe606e5 100644 --- a/accel/tcg/tcg-runtime.h +++ b/accel/tcg/tcg-runtime.h @@ -300,3 +300,7 @@ DEF_HELPER_FLAGS_4(gvec_leu32, TCG_CALL_NO_RWG, void, ptr, ptr, ptr, i32) DEF_HELPER_FLAGS_4(gvec_leu64, TCG_CALL_NO_RWG, void, ptr, ptr, ptr, i32) DEF_HELPER_FLAGS_5(gvec_bitsel, TCG_CALL_NO_RWG, void, ptr, ptr, ptr, ptr, i32) + +DEF_HELPER_7(native_call, tl, env, tl, tl, tl, i32, i32, i32) +DEF_HELPER_7(native_call_i32, i32, env, i32, i32, i32, i32, i32, i32) +DEF_HELPER_7(native_call_i64, i64, env, i64, i64, i64, i32, i32, i32) diff --git a/include/native/native-calls.h b/include/native/native-calls.h new file mode 100644 index 0000000000..5c99344a65 --- /dev/null +++ b/include/native/native-calls.h @@ -0,0 +1,75 @@ +#include "exec/cpu_ldst.h" +#include "cpu.h" +#include "native-defs.h" + +typedef uintptr_t (*nfunc_2in1out)(uintptr_t, uintptr_t); +typedef uintptr_t (*nfunc_3in1out)(uintptr_t, uintptr_t, uintptr_t); + +static inline uintptr_t decode_arg(CPUArchState *env, abi_ptr arg, + uintptr_t size, uint8_t type, + uint32_t mmu_idx); +abi_ptr do_native_2in1out(CPUArchState *env, abi_ptr arg1, abi_ptr arg2, + uint32_t abi_map, uint32_t func_id, uint32_t mmu_idx); +abi_ptr do_native_3in1out(CPUArchState *env, abi_ptr arg1, abi_ptr arg2, + abi_ptr arg3, uint32_t abi_map, uint32_t func_id, + uint32_t mmu_idx); + +nfunc_3in1out func3in1out_array[] = { + (nfunc_3in1out)NULL, (nfunc_3in1out)memcpy, (nfunc_3in1out)memcmp, + (nfunc_3in1out)memset, (nfunc_3in1out)strncpy, (nfunc_3in1out)strncmp, +}; + +nfunc_2in1out func2in1out_array[] = { + (nfunc_2in1out)NULL, + (nfunc_2in1out)strcpy, + (nfunc_2in1out)strcmp, +}; + +static inline uintptr_t decode_arg(CPUArchState *env, abi_ptr arg, + uintptr_t size, uint8_t type, + uint32_t mmu_idx) +{ + if (IS_ARG_PTR(type)) { + uintptr_t ra = GETPC(); + void *host; + if (IS_ARG_WRITE(type)) { + host = probe_write(env, arg, size, mmu_idx, ra); + return (uintptr_t)host; + } + if (IS_ARG_READ(type)) { + host = probe_read(env, arg, size, mmu_idx, ra); + return (uintptr_t)host; + } + CPUState *cs = env_cpu(env); + return (uintptr_t)g2h(cs, arg); + } + return (uintptr_t)arg; +} + +/* Currently, there is no distinction between int32, int64, and int. Their type + * conversions should be implemented. */ +abi_ptr do_native_3in1out(CPUArchState *env, abi_ptr arg1, abi_ptr arg2, + abi_ptr arg3, uint32_t abi_map, uint32_t func_id, + uint32_t mmu_idx) +{ + uintptr_t n = (uintptr_t)arg3; + uintptr_t n1 = decode_arg(env, arg1, n, GET_ARG1(abi_map), mmu_idx); + uintptr_t n2 = decode_arg(env, arg2, n, GET_ARG2(abi_map), mmu_idx); + nfunc_3in1out fn = func3in1out_array[func_id - 0x1000]; + uintptr_t r = fn(n1, n2, n); + return IS_PTR_RVALUE(abi_map) ? h2g(r) : (target_ulong)r; +} + + +abi_ptr do_native_2in1out(CPUArchState *env, abi_ptr arg1, abi_ptr arg2, + uint32_t abi_map, uint32_t func_id, uint32_t mmu_idx) +{ + CPUState *cs = env_cpu(env); + uintptr_t n1 = + IS_PTR_ARG1(abi_map) ? (uintptr_t)g2h(cs, arg1) : (uintptr_t)arg1; + uintptr_t n2 = + IS_PTR_ARG2(abi_map) ? (uintptr_t)g2h(cs, arg2) : (uintptr_t)arg2; + nfunc_2in1out fn = func2in1out_array[func_id - 0x2000]; + uintptr_t r = fn(n1, n2); + return IS_PTR_RVALUE(abi_map) ? h2g(r) : (target_ulong)r; +} From patchwork Sun Jun 25 21:27:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yeqi Fu X-Patchwork-Id: 1799573 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20221208 header.b=R3UFsDnH; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Qq3x06LvBz214p for ; Mon, 26 Jun 2023 07:28:04 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qDXGm-0005Yz-Rk; Sun, 25 Jun 2023 17:27:40 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qDXGl-0005Yo-Fe for qemu-devel@nongnu.org; Sun, 25 Jun 2023 17:27:39 -0400 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qDXGj-0003Uh-5m for qemu-devel@nongnu.org; Sun, 25 Jun 2023 17:27:38 -0400 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1b5018cb4dcso10926705ad.2 for ; Sun, 25 Jun 2023 14:27:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687728455; x=1690320455; 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=YuwM7zeHGUvJwu2BcTqfY7ibdv6STwS22FgIYvtddB8=; b=R3UFsDnHzsj9+bS6EqZV+89CZt0sINE9ETDqSkgGsxIm7meyXZEK7H3R3MKluRaOJB MEJGqkjEF/WwTIPK0TkemsPhTcGAVTJ50HuUNlfdRAVLtYTtq6okpG6ECA9lmFvdPkT5 WNSz7VlsaHX1H7vToz8jHQ57XMfBpbmHqJGl0ArsRXfpqMBk3H92cQLP/wNtcJt/T4h9 1fXENRF5IQ2z96hLubO+r48DFjk/VPK8Q5BpaDLHPYGFDtLT0OHB2NZKQfmTlJydvj1d 7uNYAOHiSNQBl5ZETY1O5AcXvwz6+MKGgRoI1VJMd0EfHv0JaVZ2990CDYXZE/khuHoI NFzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687728455; x=1690320455; 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=YuwM7zeHGUvJwu2BcTqfY7ibdv6STwS22FgIYvtddB8=; b=hZFPWXyCWLPaiAiYEvtCHewe44654E8byIecBkVRLVf8XbXggj3A7NQ9q3Iy5ayp8/ aVBHKKJf7LXA/xrfLOZAU1wRBsMrzmOHa84+b5udB+l3TVIF9iczL/IQIXDFfUK2McAD 5ULozH7jxaIaVF4QF2Y6Y7sv5eMPpL66NKUiS7JF2oETWgm3vk3Tdr9QonZ/dh4swpo3 eBzuohCb2Sr+suikVT4uH+WkPAynIBsZds2Rcj3mR4/CCz7iwhRu2R5j5fTaZWNe2GJJ QpbIxRs5wGR5e13dVtSFfDSbCfjq8Xb48KqiyKXclL3T4bZ67LR7LEljIOIBTisIFu0v i77A== X-Gm-Message-State: AC+VfDyDW04eXsGQLMpUvzGqQhhSspLaxsMjWLhm15oWdEScqkdSiXJp Ib/ezZe9II4fB6gLVOxRo64= X-Google-Smtp-Source: ACHHUZ4Lnj8bq7PvOzN8AcccEg1THM1rq6GIOs82a85InhHvPiIT8oWki6X2yfMqCmMB9kvzsNb4HQ== X-Received: by 2002:a17:903:41c1:b0:1a9:40d5:b0ae with SMTP id u1-20020a17090341c100b001a940d5b0aemr2911371ple.12.1687728455268; Sun, 25 Jun 2023 14:27:35 -0700 (PDT) Received: from q1iq-virtual-machine.localdomain ([111.201.135.80]) by smtp.gmail.com with ESMTPSA id je5-20020a170903264500b001b8004ff609sm761343plb.270.2023.06.25.14.27.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Jun 2023 14:27:34 -0700 (PDT) From: Yeqi Fu To: alex.bennee@linaro.org Cc: richard.henderson@linaro.org, qemu-devel@nongnu.org, Yeqi Fu , Paolo Bonzini , Eduardo Habkost Subject: [RFC v3 07/10] target/i386: Add support for native library calls Date: Mon, 26 Jun 2023 05:27:04 +0800 Message-Id: <20230625212707.1078951-8-fufuyqqqqqq@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230625212707.1078951-1-fufuyqqqqqq@gmail.com> References: <20230625212707.1078951-1-fufuyqqqqqq@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62c; envelope-from=fufuyqqqqqq@gmail.com; helo=mail-pl1-x62c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HK_RANDOM_ENVFROM=0.001, HK_RANDOM_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Upon encountering specialized instructions reserved for native calls, store the function id and argument types, then invoke helper. Signed-off-by: Yeqi Fu --- target/i386/tcg/translate.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/target/i386/tcg/translate.c b/target/i386/tcg/translate.c index 91c9c0c478..27d5d2231b 100644 --- a/target/i386/tcg/translate.c +++ b/target/i386/tcg/translate.c @@ -33,6 +33,7 @@ #include "helper-tcg.h" #include "exec/log.h" +#include "native/native-defs.h" #define PREFIX_REPZ 0x01 #define PREFIX_REPNZ 0x02 @@ -6806,6 +6807,38 @@ static bool disas_insn(DisasContext *s, CPUState *cpu) case 0x1d0 ... 0x1fe: disas_insn_new(s, cpu, b); break; + case 0x1ff: + if (native_call_enabled()) { + uint16_t sig; + sig = x86_lduw_code(env, s); + TCGv_i32 func_id = tcg_constant_i32(sig); + sig = x86_lduw_code(env, s); + TCGv_i32 abi_map = tcg_constant_i32(sig); + TCGv arg1 = tcg_temp_new(); + TCGv arg2 = tcg_temp_new(); + TCGv arg3 = tcg_temp_new(); +#ifdef TARGET_X86_64 + tcg_gen_mov_tl(arg1, cpu_regs[R_EDI]); + tcg_gen_mov_tl(arg2, cpu_regs[R_ESI]); + tcg_gen_mov_tl(arg3, cpu_regs[R_EDX]); +#else + uintptr_t ra = GETPC(); + uint32_t a1 = cpu_ldl_data_ra(env, env->regs[R_ESP] + 4, ra); + uint32_t a2 = cpu_ldl_data_ra(env, env->regs[R_ESP] + 8, ra); + uint32_t a3 = cpu_ldl_data_ra(env, env->regs[R_ESP] + 12, ra); + tcg_gen_movi_tl(arg1, a1); + tcg_gen_movi_tl(arg2, a2); + tcg_gen_movi_tl(arg3, a3); +#endif + TCGv res = tcg_temp_new(); + TCGv_i32 mmu_idx = tcg_constant_i32(MMU_USER_IDX); + gen_helper_native_call(res, cpu_env, arg1, arg2, arg3, + abi_map, func_id, mmu_idx); + + tcg_gen_mov_tl(cpu_regs[R_EAX], res); + break; + } + break; default: goto unknown_op; } From patchwork Sun Jun 25 21:27:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yeqi Fu X-Patchwork-Id: 1799576 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20221208 header.b=AO5P/zso; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Qq3xx6SMXz20b7 for ; Mon, 26 Jun 2023 07:28:53 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qDXGs-0005a7-TV; Sun, 25 Jun 2023 17:27:46 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qDXGp-0005Zj-6W for qemu-devel@nongnu.org; Sun, 25 Jun 2023 17:27:44 -0400 Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qDXGm-0003VL-8O for qemu-devel@nongnu.org; Sun, 25 Jun 2023 17:27:42 -0400 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-1b80512a7f2so1702465ad.3 for ; Sun, 25 Jun 2023 14:27:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687728459; x=1690320459; 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=+Upd0/w+c+7hR3Rqfjl1w2AES8eESheSb+/xurltoVk=; b=AO5P/zsoY6AN4gozDugA3cRIUpauIc+4qrgTjv2TONkCyeHARFlUq9of+kMWtcQjXR 1d3c7Em6UlsMA7Wjhm9fT5Jse8Zw1RfEKKonvWzmFII45VQ6hSZhRSkvvO2SJJFG3l3/ dgmM+uUMB6MvkqPLIuK5TyLKZBXySbayZ8pdI0nl+f+ewlTbh6rmsG+Wp5KGjyiZswRq 4hBAl/GfFDFmzDe3rdI1dO7IxczxkxmP11U1cBTHQVFs0mwdzeO642DnPYtNlPI4lKPe YwiEEA5+h9/ld6OsTchI3Di92b7AlDASKO1Z7Y7mvbduo++9X4wPenI3qKHRCvC6Ly1v Fa2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687728459; x=1690320459; 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=+Upd0/w+c+7hR3Rqfjl1w2AES8eESheSb+/xurltoVk=; b=U10dZdgJPneCE5nNoi8VPGcOsny+GPcwbJtnNQ93GGlM6Wn2TMss1UMj+uhwqEmWLf pk4FSThf8fkqEEjz8R2jqxNMQYCxcq3MY5G7T3ZwJbBRVJE1kIwIlohVjfTp/un68uUe aEPcDU0SS5wEDpzjQH7iyosnvyLX7s6Y7qbpQ+AfmGv64niw4RJrck2aAxFfry9L1q3P KSq9hggHVHtKN2kmRTgzaCN2ZWJMdhXjprxqfMD+eVmevHqkvp4+NyA9M+FWC1NU+LGQ 52tAfxa+HL5h5jxsW3gWs3oLJbuO+cxWRqohTYjidQcLpK8LFDgqmGl50+AkJ9ft+zeC gDog== X-Gm-Message-State: AC+VfDxtjDRk8C7bZHxxlB4HL+ofBE3PA/8RBYCbqwatAZ85vcZwF9sc DGTGbCoJSLIhQ8rz9EDesNc= X-Google-Smtp-Source: ACHHUZ54sJOvHEW57Ch2w2kYqj/fJWqfTyL2adNNvGFu2ZTNNDKAKi/3qQGmLv/zUX9Fms7kFSO8rA== X-Received: by 2002:a17:903:1205:b0:1af:aafb:64c8 with SMTP id l5-20020a170903120500b001afaafb64c8mr5035549plh.21.1687728458790; Sun, 25 Jun 2023 14:27:38 -0700 (PDT) Received: from q1iq-virtual-machine.localdomain ([111.201.135.80]) by smtp.gmail.com with ESMTPSA id je5-20020a170903264500b001b8004ff609sm761343plb.270.2023.06.25.14.27.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Jun 2023 14:27:38 -0700 (PDT) From: Yeqi Fu To: alex.bennee@linaro.org Cc: richard.henderson@linaro.org, qemu-devel@nongnu.org, Yeqi Fu , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Aurelien Jarno , Jiaxun Yang , Aleksandar Rikalo Subject: [RFC v3 08/10] target/mips: Add support for native library calls Date: Mon, 26 Jun 2023 05:27:05 +0800 Message-Id: <20230625212707.1078951-9-fufuyqqqqqq@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230625212707.1078951-1-fufuyqqqqqq@gmail.com> References: <20230625212707.1078951-1-fufuyqqqqqq@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62e; envelope-from=fufuyqqqqqq@gmail.com; helo=mail-pl1-x62e.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HK_RANDOM_ENVFROM=0.001, HK_RANDOM_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Upon encountering specialized instructions reserved for native calls, store the function id and argument types, then invoke helper. Signed-off-by: Yeqi Fu --- target/mips/tcg/translate.c | 26 ++++++++++++++++++++++++++ target/mips/tcg/translate.h | 2 ++ 2 files changed, 28 insertions(+) diff --git a/target/mips/tcg/translate.c b/target/mips/tcg/translate.c index a6ca2e5a3b..15ab889dca 100644 --- a/target/mips/tcg/translate.c +++ b/target/mips/tcg/translate.c @@ -36,6 +36,7 @@ #include "qemu/qemu-print.h" #include "fpu_helper.h" #include "translate.h" +#include "native/native-defs.h" /* * Many sysemu-only helpers are not reachable for user-only. @@ -13592,6 +13593,31 @@ static void decode_opc_special(CPUMIPSState *env, DisasContext *ctx) #endif break; case OPC_SYSCALL: + uint32_t sig = (ctx->opcode) >> 6; + if (native_call_enabled() && (!ctx->native_call_status) && sig) { + ctx->native_call_status = true; + ctx->native_call_id = sig; + break; + } else if (native_call_enabled() && (ctx->native_call_status) && sig) { + TCGv arg1 = tcg_temp_new(); + TCGv arg2 = tcg_temp_new(); + TCGv arg3 = tcg_temp_new(); + + tcg_gen_mov_tl(arg1, cpu_gpr[4]); + tcg_gen_mov_tl(arg2, cpu_gpr[5]); + tcg_gen_mov_tl(arg3, cpu_gpr[6]); + + TCGv_i32 abi_map = tcg_constant_i32(sig); + TCGv_i32 func_id = tcg_constant_i32(ctx->native_call_id); + TCGv res = tcg_temp_new(); + TCGv_i32 mmu_idx = tcg_constant_i32(MMU_USER_IDX); + gen_helper_native_call(res, cpu_env, arg1, arg2, arg3, + abi_map, func_id, mmu_idx); + tcg_gen_mov_tl(cpu_gpr[2], res); + ctx->native_call_status = false; + ctx->native_call_id = 0; + break; + } generate_exception_end(ctx, EXCP_SYSCALL); break; case OPC_BREAK: diff --git a/target/mips/tcg/translate.h b/target/mips/tcg/translate.h index 69f85841d2..bc603297cc 100644 --- a/target/mips/tcg/translate.h +++ b/target/mips/tcg/translate.h @@ -49,6 +49,8 @@ typedef struct DisasContext { bool saar; bool mi; int gi; + bool native_call_status; + int native_call_id; } DisasContext; #define DISAS_STOP DISAS_TARGET_0 From patchwork Sun Jun 25 21:27:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yeqi Fu X-Patchwork-Id: 1799577 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20221208 header.b=YvlJBQjg; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Qq3yJ22DKz20b7 for ; Mon, 26 Jun 2023 07:29:12 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qDXGu-0005an-Pe; Sun, 25 Jun 2023 17:27:48 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qDXGt-0005aM-2E; Sun, 25 Jun 2023 17:27:47 -0400 Received: from mail-pg1-x535.google.com ([2607:f8b0:4864:20::535]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qDXGp-0003Vr-CC; Sun, 25 Jun 2023 17:27:45 -0400 Received: by mail-pg1-x535.google.com with SMTP id 41be03b00d2f7-54fb3c168fcso2326469a12.0; Sun, 25 Jun 2023 14:27:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687728461; x=1690320461; 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=ELXa0RrQCUVaZ33o/5bUxq4k9BhayvG3Y38hDK3c1b8=; b=YvlJBQjgiPMeFSRavw8zMmmxwPjaDMpCr2Q8vcJvpEJt/SNfeA8Nj3c/Mx7lfKerM8 wCXtStzjjG2qaoQrNaTWw8PK/DadHoExFpqRAac/MP1fCp9qyDv0sYy9h8eodqU8ISE6 adzKOin38TAy2wcXbMtkV465M0mRbG44B4DAX1NnY5PQeBfCfNkHhoAIJ9w9Q9dJBNyH FngiRbjArx6ArU+QSJc/JIeD8i0D1Q4jaYuoGyijOA7CbgT7DBJcP1aYjPWWAq0+Kk2h oN+LQhaB5JgoqR1YMPmHE7fkUFe2K+CInWefFfvmwT+v7mIZv1EDnC9NYFdjQrlMhAPY aPFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687728461; x=1690320461; 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=ELXa0RrQCUVaZ33o/5bUxq4k9BhayvG3Y38hDK3c1b8=; b=lP78iAIWqaPSXPBaL43TRuNhurNP4/JExwlVVr74uu2H2C+GX+ewE3dhgKzGPkGh2H QxrzekWgfHnXguOto2pPqr8OMXEN+PtWLltEQdSglZGcshJyc57TPvuItERJxM9xjk99 djTOdIvQQl2lQ/Mzt2tKVbWQW+vzqgjHIZYgn1NsBj05AjM5XKeK5aPr+dNLsH/2BBiH GJEaeIFEp5ozEqcj8A57D+F5PlYqZW+Wbn9nmH7K4JpAxEemcSncpXHUT5gGeLtz6gFc uLUdVyZHBEpVVp60shqs55XpxlMSNNH2JUP1BmFuydhV8sMGUggBJzS/cXWpg8CswoAB VskQ== X-Gm-Message-State: AC+VfDwZMgBhC9l/7FmoBhvBQ/whgbVk8slBGagAFJvz8mTvHF0x1seh bPPXj06eiNY+QLqYl63m+shDDPw6DlPVuOgS9Js= X-Google-Smtp-Source: ACHHUZ5G+4116E4lEa2fPxs4JiYcOfRCMbSV6YGVwuMCwjqIBlQ20w9cguGffZrloJ1m3Z7WhBbYIw== X-Received: by 2002:a17:902:ab95:b0:1b0:378e:2768 with SMTP id f21-20020a170902ab9500b001b0378e2768mr5641810plr.7.1687728461528; Sun, 25 Jun 2023 14:27:41 -0700 (PDT) Received: from q1iq-virtual-machine.localdomain ([111.201.135.80]) by smtp.gmail.com with ESMTPSA id je5-20020a170903264500b001b8004ff609sm761343plb.270.2023.06.25.14.27.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Jun 2023 14:27:41 -0700 (PDT) From: Yeqi Fu To: alex.bennee@linaro.org Cc: richard.henderson@linaro.org, qemu-devel@nongnu.org, Yeqi Fu , Peter Maydell , qemu-arm@nongnu.org (open list:ARM TCG CPUs) Subject: [RFC v3 09/10] target/arm: Add support for native library calls Date: Mon, 26 Jun 2023 05:27:06 +0800 Message-Id: <20230625212707.1078951-10-fufuyqqqqqq@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230625212707.1078951-1-fufuyqqqqqq@gmail.com> References: <20230625212707.1078951-1-fufuyqqqqqq@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::535; envelope-from=fufuyqqqqqq@gmail.com; helo=mail-pg1-x535.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HK_RANDOM_ENVFROM=0.001, HK_RANDOM_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Upon encountering specialized instructions reserved for native calls, store the function id and argument types, then invoke helper. Signed-off-by: Yeqi Fu --- target/arm/tcg/translate-a64.c | 27 ++++++++++++++++++++++++++- target/arm/tcg/translate.c | 25 +++++++++++++++++++++++-- target/arm/tcg/translate.h | 6 ++++++ 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/target/arm/tcg/translate-a64.c b/target/arm/tcg/translate-a64.c index 741a608739..24a664b928 100644 --- a/target/arm/tcg/translate-a64.c +++ b/target/arm/tcg/translate-a64.c @@ -35,6 +35,7 @@ #include "cpregs.h" #include "translate-a64.h" #include "qemu/atomic128.h" +#include "native/native-defs.h" static TCGv_i64 cpu_X[32]; static TCGv_i64 cpu_pc; @@ -2331,11 +2332,35 @@ static void disas_exc(DisasContext *s, uint32_t insn) /* BRK */ gen_exception_bkpt_insn(s, syn_aa64_bkpt(imm16)); break; - case 2: + case 2: /* HLT */ if (op2_ll != 0) { unallocated_encoding(s); break; } + if (native_call_enabled() && (!s->native_call_status)) { + s->native_call_status = true; + s->native_call_id = imm16; + break; + } else if (native_call_enabled() && (s->native_call_status)) { + TCGv_i64 arg1 = tcg_temp_new_i64(); + TCGv_i64 arg2 = tcg_temp_new_i64(); + TCGv_i64 arg3 = tcg_temp_new_i64(); + + tcg_gen_mov_i64(arg1, cpu_reg(s, 0)); + tcg_gen_mov_i64(arg2, cpu_reg(s, 1)); + tcg_gen_mov_i64(arg3, cpu_reg(s, 2)); + + TCGv_i32 abi_map = tcg_constant_i32(imm16); + TCGv_i32 func_id = tcg_constant_i32(s->native_call_id); + TCGv_i64 res = tcg_temp_new_i64(); + TCGv_i32 mmu_idx = tcg_constant_i32(MMU_USER_IDX); + gen_helper_native_call(res, cpu_env, arg1, arg2, arg3, + abi_map, func_id, mmu_idx); + tcg_gen_mov_i64(cpu_reg(s, 0), res); + s->native_call_status = false; + s->native_call_id = 0; + break; + } /* HLT. This has two purposes. * Architecturally, it is an external halting debug instruction. * Since QEMU doesn't implement external debug, we treat this as diff --git a/target/arm/tcg/translate.c b/target/arm/tcg/translate.c index 7468476724..7b90ce50d0 100644 --- a/target/arm/tcg/translate.c +++ b/target/arm/tcg/translate.c @@ -34,7 +34,7 @@ #include "exec/helper-gen.h" #include "exec/log.h" #include "cpregs.h" - +#include "native/native-defs.h" #define ENABLE_ARCH_4T arm_dc_feature(s, ARM_FEATURE_V4T) #define ENABLE_ARCH_5 arm_dc_feature(s, ARM_FEATURE_V5) @@ -58,6 +58,7 @@ TCGv_i32 cpu_CF, cpu_NF, cpu_VF, cpu_ZF; TCGv_i64 cpu_exclusive_addr; TCGv_i64 cpu_exclusive_val; + #include "exec/gen-icount.h" static const char * const regnames[] = @@ -1147,12 +1148,32 @@ static inline void gen_hlt(DisasContext *s, int imm) * semihosting, to provide some semblance of security * (and for consistency with our 32-bit semihosting). */ + if (native_call_enabled() && (!s->native_call_status)) { + s->native_call_status = true; + s->native_call_id = imm; + return; + } else if (native_call_enabled() && (s->native_call_status)) { + TCGv_i32 arg1 = load_reg(s, 0); + TCGv_i32 arg2 = load_reg(s, 1); + TCGv_i32 arg3 = load_reg(s, 2); + + TCGv_i32 abi_map = tcg_constant_i32(imm); + TCGv_i32 func_id = tcg_constant_i32(s->native_call_id); + TCGv_i32 res = tcg_temp_new_i32(); + TCGv_i32 mmu_idx = tcg_constant_i32(MMU_USER_IDX); + gen_helper_native_call_i32(res, cpu_env, arg1, arg2, arg3, + abi_map, func_id, mmu_idx); + + store_reg(s, 0, res); + s->native_call_status = false; + s->native_call_id = 0; + return; + } if (semihosting_enabled(s->current_el == 0) && (imm == (s->thumb ? 0x3c : 0xf000))) { gen_exception_internal_insn(s, EXCP_SEMIHOST); return; } - unallocated_encoding(s); } diff --git a/target/arm/tcg/translate.h b/target/arm/tcg/translate.h index a9d1f4adc2..280f8ba215 100644 --- a/target/arm/tcg/translate.h +++ b/target/arm/tcg/translate.h @@ -149,6 +149,12 @@ typedef struct DisasContext { int c15_cpar; /* TCG op of the current insn_start. */ TCGOp *insn_start; + /* + * Indicate whether the next instruction is a native function call (true) + * or not (false). + */ + bool native_call_status; + int native_call_id; } DisasContext; typedef struct DisasCompare { From patchwork Sun Jun 25 21:27:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yeqi Fu X-Patchwork-Id: 1799582 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20221208 header.b=QoDA6bti; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Qq4064rz7z20bC for ; Mon, 26 Jun 2023 07:30:46 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qDXGx-0005bZ-CC; Sun, 25 Jun 2023 17:27:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qDXGt-0005aV-IV for qemu-devel@nongnu.org; Sun, 25 Jun 2023 17:27:47 -0400 Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qDXGr-0003WH-2j for qemu-devel@nongnu.org; Sun, 25 Jun 2023 17:27:46 -0400 Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-1b80512a7f2so1702585ad.3 for ; Sun, 25 Jun 2023 14:27:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687728464; x=1690320464; 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=Mnsf664I1dbfqhlUsQXaqr8d9bhL0IvglCtYautfisc=; b=QoDA6btioxfXJJ6OXh/uYSeNP6vmRt3dJn47IppYd9IYEDo4zpLzTojTCfdBd/eDl0 vx/Ey7uDpmNPAcPSfHOkQBI6xZMScxk+6Qwor1c60IrjuPg/w66iQnQejIysY1aNGFuN lE2QWmhJ14rg8tyDFem/WyQl3UQKi+71+JHTtGmmi9wEyNRDUwm7ZEneJ0ZVxhDqiKvH HKtx5qMkA26N+VpH6stF+1fts7eg/y5kreB4YywWi6k8yhldU6EgHFKqyeXMyJMRS1xr 2XKg+DlUd+YaN9+GMMaocFN2IbF5CvZ0PpcteIO/IFzkc/wHZ5QOoeDuwlbSngdrYyNc 5+lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687728464; x=1690320464; 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=Mnsf664I1dbfqhlUsQXaqr8d9bhL0IvglCtYautfisc=; b=Ab+HgNgV7mzloew/1x08H4AUTmrdtOCr/8KuqyNF4snV5GXkT/aoRA1x3MMJhud3E7 ASnSgFo7IY1o9bINguAcmKUPEyefbK1A57HUFbDH0zqi1+dsF/FG/cPqjtmjygGKA1bD ZBpVec2ZLXvN91ic6J/DnCy+j5TNjCSQXOo5Yzvdz+ajn9bwbOGvXt+SkAnhbCSHFTPU 9FDEjV+RE5LV0bKY94p4yLgI9cBHIeIv0OzNGW19j+Ytc+jsx1FwN+l2pUrzv0oAlbTf gAJODGDPZU5+2xEJuWU9KA2E5apot5mk/N/LlZbZEt0VaQE8aVvpVwvdZnKi8s+5v2/w 40sg== X-Gm-Message-State: AC+VfDzSqmJ9M8SgbvGEaQ6pcau42xJIKA6snrf8ClQMCu4Yvo+VeB1B 7LuKFMBuKMUFzWk2qydw1/U= X-Google-Smtp-Source: ACHHUZ5wIWIyAYRcOrGqAH+eTfsevtq5sK6j+fomue4H+0kuSweLKDEQuSEREP+QRfxd70bhcKfdsg== X-Received: by 2002:a17:902:d4c7:b0:1b6:9df6:26c2 with SMTP id o7-20020a170902d4c700b001b69df626c2mr4094587plg.12.1687728463835; Sun, 25 Jun 2023 14:27:43 -0700 (PDT) Received: from q1iq-virtual-machine.localdomain ([111.201.135.80]) by smtp.gmail.com with ESMTPSA id je5-20020a170903264500b001b8004ff609sm761343plb.270.2023.06.25.14.27.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Jun 2023 14:27:43 -0700 (PDT) From: Yeqi Fu To: alex.bennee@linaro.org Cc: richard.henderson@linaro.org, qemu-devel@nongnu.org, Yeqi Fu Subject: [RFC v3 10/10] tests/tcg/multiarch: Add nativecalls.c test Date: Mon, 26 Jun 2023 05:27:07 +0800 Message-Id: <20230625212707.1078951-11-fufuyqqqqqq@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230625212707.1078951-1-fufuyqqqqqq@gmail.com> References: <20230625212707.1078951-1-fufuyqqqqqq@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62f; envelope-from=fufuyqqqqqq@gmail.com; helo=mail-pl1-x62f.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HK_RANDOM_ENVFROM=0.001, HK_RANDOM_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Add a test for native calls to verify the functionality of native functions. This requires cross-compiling test cases and building them as dynamically linked binaries. Also, introduce necessary system libraries in QEMU. Signed-off-by: Yeqi Fu --- tests/tcg/multiarch/Makefile.target | 9 +- tests/tcg/multiarch/native/nativecalls.c | 103 +++++++++++++++++++++++ 2 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 tests/tcg/multiarch/native/nativecalls.c diff --git a/tests/tcg/multiarch/Makefile.target b/tests/tcg/multiarch/Makefile.target index 373db69648..c4ea7117c2 100644 --- a/tests/tcg/multiarch/Makefile.target +++ b/tests/tcg/multiarch/Makefile.target @@ -128,7 +128,14 @@ run-semiconsole: semiconsole run-plugin-semiconsole-with-%: $(call skip-test, $<, "MANUAL ONLY") -TESTS += semihosting semiconsole +nativecalls: native/nativecalls.c + $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $< -o $@ $(filter-out -static,$(LDFLAGS)) + +run-nativecalls: nativecalls + $(call run-test,$<, $(QEMU) -L /usr/$(subst -gcc,,$(CC)) --native-bypass $(SRC_PATH)/build/common-user/native/$(TARGET)/libnative.so $<, \ + "native call") + +TESTS += semihosting semiconsole nativecalls endif # Update TESTS diff --git a/tests/tcg/multiarch/native/nativecalls.c b/tests/tcg/multiarch/native/nativecalls.c new file mode 100644 index 0000000000..2d4ddb3a8e --- /dev/null +++ b/tests/tcg/multiarch/native/nativecalls.c @@ -0,0 +1,103 @@ +#include +#include +#include + +void compare_memory(const void *a, const void *b, size_t n) +{ + const unsigned char *p1 = a; + const unsigned char *p2 = b; + for (size_t i = 0; i < n; i++) { + assert(p1[i] == p2[i]); + } +} + +void test_memcpy() +{ + char src[] = "Hello, world!"; + char dest[20]; + memcpy(dest, src, 13); + compare_memory(dest, src, 13); +} + +void test_memcmp() +{ + char str1[] = "abc"; + char str2[] = "abc"; + char str3[] = "def"; + assert(memcmp(str1, str2, 3) == 0); + assert(memcmp(str1, str3, 3) != 0); +} + +void test_memset() +{ + char buffer[10]; + memset(buffer, 'A', 10); + int i; + for (i = 0; i < 10; i++) { + assert(buffer[i] == 'A'); + } +} + +void test_strncpy() +{ + char src[] = "Hello, world!"; + char dest[20]; + strncpy(dest, src, 13); + compare_memory(dest, src, 13); +} + +void test_strncmp() +{ + char str1[] = "abc"; + char str2[] = "abc"; + char str3[] = "def"; + assert(strncmp(str1, str2, 2) == 0); + assert(strncmp(str1, str3, 2) != 0); +} + + +void test_strcpy() +{ + char src[] = "Hello, world!"; + char dest[20]; + strcpy(dest, src); + compare_memory(dest, src, 13); +} + +void test_strcmp() +{ + char str1[] = "abc"; + char str2[] = "abc"; + char str3[] = "def"; + assert(strcmp(str1, str2) == 0); + assert(strcmp(str1, str3) != 0); +} + +void test_memcpy_bad_addr() +{ + char src[] = "Hello, world!"; + char *dst = (char *)0x1; + memcpy(dst, src, 13); +} + +void test_memset_bad_addr() +{ + char *dst = (char *)0x1; + memset(dst, 'A', 10); +} + +int main() +{ + test_memcpy(); + test_memcmp(); + test_memset(); + test_strncpy(); + test_strncmp(); + test_strcpy(); + test_strcmp(); + + test_memcpy_bad_addr(); + test_memset_bad_addr(); + + return EXIT_SUCCESS; +}