From patchwork Fri Aug 25 10:19: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: 1825912 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org 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=FfbfokMl; dkim-atps=neutral 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=patchwork.ozlabs.org) 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 4RXGJC0ccCz1yZs for ; Fri, 25 Aug 2023 20:23:39 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qZTxj-00075T-Qw; Fri, 25 Aug 2023 06:22:43 -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 1qZTxi-00075B-GD for qemu-devel@nongnu.org; Fri, 25 Aug 2023 06:22:42 -0400 Received: from mail-pf1-x42e.google.com ([2607:f8b0:4864:20::42e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qZTxg-0004js-58 for qemu-devel@nongnu.org; Fri, 25 Aug 2023 06:22:42 -0400 Received: by mail-pf1-x42e.google.com with SMTP id d2e1a72fcca58-68a42d06d02so654340b3a.0 for ; Fri, 25 Aug 2023 03:22:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692958958; x=1693563758; 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=6wwFlKewAD3EbUW7aELNycuHcgFIWkZV/jDzQISrvf8=; b=FfbfokMldMkxgQQXx0kKqbmxCFZvS4cb+cs9FR7hxKAzVLc7NdGcwunSQxwL7zEIfX zffFNeD3Sgykfh+oywkQ47bSaVTVz1CRYe+buxa7dFP1rccdum5moqOGvnWQuh9Z2Lwp 52gda9/lT/sM+qCK/4ubwmn8rTrmKEBkNtHc5bZvWb3u014KbunxMc4QsDSoJB/KU+Oc kaETOUZKuu+tmXtte6hJUaTxsiBVL3AZ9Y287JP1yWtJGU0oAX9idJYnzezlale0itKC +vJ7TIpnweU8BXYYRb76y7DLc2DpmjKtZ3ong07L+tTERBu629ZVb/vd+sLLeaa7wW27 sBuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692958958; x=1693563758; 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=6wwFlKewAD3EbUW7aELNycuHcgFIWkZV/jDzQISrvf8=; b=SLkFOIEZH0THVjyhpquHn+nQ3elf4VEFtm2s5KRhbIgXDhubg5yYZlrx7rRB5NyrtQ wGacOWMxCdhC7gRpnqqpVcbjXqVTYk2KJq9KdY/Kd2346uFqnCBHuYDJRRqCOHip9K2Z 2b/oVK6ebdBDCeovQ9rMyuLGPCFHn4yxuAZbd5DVlSheeSzOcnI8om3qEDxMpuCJgKyR WSlOyCWwpzahglfSCXPyeM9euuNhaWIdeBjvK2XN0BvAi3ZcjBL8EnOAhtvA+JenJk9t eLLgwIqXh1127336Do/avzKcRQw4xI0UP6d98DPzf74sC6gqEau1TjErgszKNIFu3x75 vMEg== X-Gm-Message-State: AOJu0YyOJN3Iu7xHOkD3p1e+Wo0g4i9LUuC4d0QRni8+xh2orhMty60i HhsCuHk9CyXYcDEZcL73mtI= X-Google-Smtp-Source: AGHT+IHTLBBeFysjH/KCnxtzk5zxyxnDw7bEsW1aQq7X7CPHSC7HM6yuHRVCt9FAhaCrU1bPcjfWiw== X-Received: by 2002:a05:6a00:2312:b0:689:efa6:5c02 with SMTP id h18-20020a056a00231200b00689efa65c02mr16781748pfh.11.1692958958538; Fri, 25 Aug 2023 03:22:38 -0700 (PDT) Received: from localhost.localdomain ([223.104.40.15]) by smtp.gmail.com with ESMTPSA id c5-20020aa781c5000000b0068a077847c7sm1220954pfn.135.2023.08.25.03.22.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Aug 2023 03:22:38 -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 Subject: [RFC v5 01/10] build: Implement logic for sharing cross-building config files Date: Fri, 25 Aug 2023 18:19:59 +0800 Message-Id: <20230825102009.1754699-2-fufuyqqqqqq@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230825102009.1754699-1-fufuyqqqqqq@gmail.com> References: <20230825102009.1754699-1-fufuyqqqqqq@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::42e; envelope-from=fufuyqqqqqq@gmail.com; helo=mail-pf1-x42e.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 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 Since both TCG tests and libnative libraries require cross-building, the config files for cross-building, config_target_mak, are now saved in the cross-build directory for sharing. This allows TCG tests and libnative libraries to use these config files through symbolic links when cross-building configuration is needed. Since config_host_mak essentially contains all the information from the original tests/tcg/config-host.mak, the original config-host.mak has been deleted and replaced with a symbolic link to config_host_mak. Signed-off-by: Yeqi Fu --- configure | 61 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/configure b/configure index 2b41c49c0d..7a1e463d9c 100755 --- a/configure +++ b/configure @@ -1751,32 +1751,23 @@ 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 the config files for cross building. +# This process generates 'cross-build//config-target.mak' files. +# These files are then symlinked to the directories that need them which +# including the TCG tests (tests/tcg/) and the libnative library +# for linux-user (common/native//). +mkdir -p cross-build -# versioned checked in the main config_host.mak above -if test -n "$gdb_bin"; then - echo "HAVE_GDB_BIN=$gdb_bin" >> $config_host_mak -fi -if test "$plugins" = "yes" ; then - echo "CONFIG_PLUGIN=y" >> $config_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 + # the toolchain for tests/tcg is not complete with headers continue ;; *-softmmu) - test -f "$source_path/tests/tcg/$arch/Makefile.softmmu-target" || continue + # skip installing config-target.mak when we have no tests to build + test -f "${source_path}/tests/tcg/${arch}/Makefile.softmmu-target" || continue qemu="qemu-system-$arch" ;; *-linux-user|*-bsd-user) @@ -1786,22 +1777,46 @@ for target in $target_list; do 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" + # get the interpreter prefix and the path of libnative required for native call tests + if test -n "$target_cc" && [ -d "/usr/$(echo "$target_cc" | sed 's/-gcc//')" ]; then + echo "LD_PREFIX=/usr/$(echo "$target_cc" | sed 's/-gcc//')" >> "$config_target_mak" + fi + # will GDB work with these binaries? 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 +# create a symlink to the config-host.mak file in the tests/tcg +ln -srf $config_host_mak tests/tcg/config-host.mak +echo "HOST_CC=$host_cc" >> $config_host_mak + +tcg_tests_targets= +for target in $target_list; do + case $target in + *-softmmu) + test -f "${source_path}/tests/tcg/${arch}/Makefile.softmmu-target" || continue + ;; + esac - echo "run-tcg-tests-$target: $qemu\$(EXESUF)" >> Makefile.prereqs + if test -f cross-build/${target}/config-target.mak; 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 done From patchwork Fri Aug 25 10:20: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: 1825915 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org 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=X2rwomXo; dkim-atps=neutral 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=patchwork.ozlabs.org) 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 4RXGJf2h7Fz1yg8 for ; Fri, 25 Aug 2023 20:24:02 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qZTxr-00076P-3u; Fri, 25 Aug 2023 06:22: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 1qZTxq-000769-6z for qemu-devel@nongnu.org; Fri, 25 Aug 2023 06:22:50 -0400 Received: from mail-pf1-x431.google.com ([2607:f8b0:4864:20::431]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qZTxm-0004kT-7z for qemu-devel@nongnu.org; Fri, 25 Aug 2023 06:22:49 -0400 Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-68a3ced3ec6so652281b3a.1 for ; Fri, 25 Aug 2023 03:22:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692958965; x=1693563765; 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=6XKF8yloHlp3OItWzVeWkoBwxtc6EI5o63l2lDH6G7s=; b=X2rwomXoy1NnARDSUMbGjqO8UdHxRAE+3N2J3prmgrt+X1ivOYTT0fv/3NaWRS81uG LJvRwAW5citblQSoj0wrWCc7K6AoZ4ZdVpjlLPMNGMk+UC6BR2jT0Q9tRDmfR8R7SCDy BkHpEhasgkrwtlSv9QkJEdrQ/IMiKg8MDMmK/jxRcO4LT/pHTZMkIEZAoCx5d48TlzJs C17MPXLFCJd7PvRdmyA66V65sxjHt4egzB/kd4QOgFFY9rFcBycVLV8lyHkl+sqX/R1g 2ZZJAnRD4h1CX+2JiqEDrZMuNn3XmOUyW4+StnEMULkcmSx6sxX1PCrpPkxaDkfpSQ9t DnaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692958965; x=1693563765; 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=6XKF8yloHlp3OItWzVeWkoBwxtc6EI5o63l2lDH6G7s=; b=UDRvY0FnwuRgEu8F18+rS3kaLd9V6dRUNB7AjiOuDiW3TBxM+8vywav3jdIpzOjTaN h0on0JVwuriDuEJAGkxo2hIT0rbwjMCqcu4qpOnUWSH6sx9E3PUXnYE/c4NQXNesbGO5 LDkVqr+P1XUYebM+YeZPCpnxMEgdtb8TfoKr7+HuJPdLVqc9Fi7HCdg3nK1YArXElBY+ NbvIJBHe4L1BIY+e9wKqaF8XV/0t/sXD0ZSVWJR4uYMaCJtWcqiurfWpHghjZfud82NU aHchyBZO6QxexiOJzAs/C5EwU2ffCVK2eJpY82FeBrZXaI2nfYmhBIYz1mFEbIGzgDoq al4Q== X-Gm-Message-State: AOJu0YwAh5QnWF6ZX5RZ5VuWsRlE4lXZtiWImdag6MBy+yK5+lrNxJOu qhsU9PN0+41UjlueNoRdRwo= X-Google-Smtp-Source: AGHT+IH5m/D4OrRR+0CWRqrPib+kmaG33rkS2G4XQRnjkBxYoKuEq8pYFD+zREkhQhfgL3Rg4owneQ== X-Received: by 2002:aa7:8881:0:b0:68b:dbad:7ae0 with SMTP id z1-20020aa78881000000b0068bdbad7ae0mr7919717pfe.21.1692958964764; Fri, 25 Aug 2023 03:22:44 -0700 (PDT) Received: from localhost.localdomain ([223.104.40.15]) by smtp.gmail.com with ESMTPSA id c5-20020aa781c5000000b0068a077847c7sm1220954pfn.135.2023.08.25.03.22.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Aug 2023 03:22:44 -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 v5 02/10] build: Implement libnative library and the build machinery for libnative Date: Fri, 25 Aug 2023 18:20:00 +0800 Message-Id: <20230825102009.1754699-3-fufuyqqqqqq@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230825102009.1754699-1-fufuyqqqqqq@gmail.com> References: <20230825102009.1754699-1-fufuyqqqqqq@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::431; envelope-from=fufuyqqqqqq@gmail.com; helo=mail-pf1-x431.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 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 special instructions. At runtime, user programs load the shared library, and special 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 | 22 +++++++++ common-user/native/libnative.S | 69 +++++++++++++++++++++++++++++ configure | 39 ++++++++++++++++ 5 files changed, 141 insertions(+) create mode 100644 common-user/native/Makefile.include create mode 100644 common-user/native/Makefile.target create mode 100644 common-user/native/libnative.S diff --git a/Makefile b/Makefile index 5d48dfac18..6f6147b40f 100644 --- a/Makefile +++ b/Makefile @@ -182,6 +182,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..65d90102e2 --- /dev/null +++ b/common-user/native/Makefile.target @@ -0,0 +1,22 @@ +# -*- Mode: makefile -*- +# +# Library for native calls +# + +all: +-include ../../../config-host.mak +-include config-target.mak + +CFLAGS+=-shared -D $(TARGET_NAME) +LDFLAGS+= + +SRC = $(SRC_PATH)/common-user/native/libnative.S +LIBNATIVE = libnative.so + +all: $(LIBNATIVE) + +$(LIBNATIVE): $(SRC) + $(CC) $(CFLAGS) $(EXTRA_CFLAGS) $(EXTRA_NATIVE_CALL_FLAGS) $< -o $@ $(LDFLAGS) + +clean: + rm -f $(LIBNATIVE) diff --git a/common-user/native/libnative.S b/common-user/native/libnative.S new file mode 100644 index 0000000000..3692eaa3cf --- /dev/null +++ b/common-user/native/libnative.S @@ -0,0 +1,69 @@ +#if defined(i386) || defined(x86_64) +/* + * An unused instruction is utilized to mark a native call. + */ +#define __SPECIAL_INSTR .byte 0x0f, 0xff; +#define __RET_INSTR ret; +#endif + +#if defined(arm) || defined(aarch64) +/* + * HLT is an invalid instruction for userspace programs, + * and is used to mark a native call. + */ +#define __SPECIAL_INSTR hlt 0xffff; +#if defined(aarch64) +#define __RET_INSTR ret; +#else +#define __RET_INSTR bx lr; +#endif +#endif + + +#if defined(mips) || defined(mips64) +/* + * The syscall instruction contains 20 unused bits, which are typically + * set to 0. These bits can be used to store non-zero data, + * distinguishing them from a regular syscall instruction. + */ +#define __SPECIAL_INSTR syscall 0xffff; +#define __RET_INSTR jr $ra; +#endif + +/* Symbols of native functions */ +.section .data +sym_memset: .asciz "memset" +sym_memcpy: .asciz "memcpy" +sym_strncpy: .asciz "strncpy" +sym_memcmp: .asciz "memcmp" +sym_strncmp: .asciz "strncmp" +sym_strcpy: .asciz "strcpy" +sym_strcat: .asciz "strcat" +sym_strcmp: .asciz "strcmp" + +.macro define_function name +\name: +#if defined(x86_64) || defined(aarch64) + __SPECIAL_INSTR + .quad sym_\name + __RET_INSTR +#elif defined(mips64) +.align 4 + __SPECIAL_INSTR + .quad sym_\name + __RET_INSTR +#elif defined(i386) || defined(mips) || defined(arm) + __SPECIAL_INSTR + .long sym_\name + __RET_INSTR +#endif +.endm + +define_function memcpy +define_function strncpy +define_function memset +define_function memcmp +define_function strncmp +define_function strcpy +define_function strcat +define_function strcmp diff --git a/configure b/configure index 7a1e463d9c..de533b27a2 100755 --- a/configure +++ b/configure @@ -1826,6 +1826,45 @@ if test "$tcg" = "enabled"; then fi ) +# common-user/native configuration +(mkdir -p common-user/native + +native_targets= +for target in $target_list; do + case $target in + *-softmmu) + continue + ;; + esac + + # native call is only supported on these architectures + arch=${target%%-*} + config_target_mak=common-user/native/${target}/config-target.mak + case $arch in + i386|x86_64|arm|aarch64|mips|mips64) + if test -f cross-build/${target}/config-target.mak; then + mkdir -p "common-user/native/${target}" + ln -srf cross-build/${target}/config-target.mak "$config_target_mak" + if test $arch = arm; then + echo "EXTRA_NATIVE_CALL_FLAGS=-marm" >> "$config_target_mak" + fi + if test $arch = $cpu || \ + { test $arch = i386 && test $cpu = x86_64; } || \ + { test $arch = arm && test $cpu = aarch64; } || \ + { test $arch = mips && test $cpu = mips64; }; then + echo "LD_PREFIX=/" >> "$config_target_mak" + fi + echo "LIBNATIVE=$PWD/common-user/native/${target}/libnative.so" >> "$config_target_mak" + ln -sf ${source_path}/common-user/native/Makefile.target common-user/native/${target}/Makefile + native_targets="$native_targets ${target}" + fi + ;; + esac +done + +echo "NATIVE_TARGETS=$native_targets" >> config-host.mak +) + if test "$skip_meson" = no; then cross="config-meson.cross.new" meson_quote() { From patchwork Fri Aug 25 10:20: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: 1825914 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org 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=e9htfI59; dkim-atps=neutral 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=patchwork.ozlabs.org) 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 4RXGJf0ms9z1yZs for ; Fri, 25 Aug 2023 20:24:02 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qZTxu-00077J-Ar; Fri, 25 Aug 2023 06:22:54 -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 1qZTxs-000776-PD for qemu-devel@nongnu.org; Fri, 25 Aug 2023 06:22:52 -0400 Received: from mail-pf1-x431.google.com ([2607:f8b0:4864:20::431]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qZTxq-0004ku-9Z for qemu-devel@nongnu.org; Fri, 25 Aug 2023 06:22:52 -0400 Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-68a3082c771so592480b3a.0 for ; Fri, 25 Aug 2023 03:22:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692958969; x=1693563769; 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=ZnMgbb3eHMu2WVFHDfLveza5RGGaFqQpAry19Wrh14U=; b=e9htfI59SrsvUUiHG6PMRf1mGsP8on3W8g5mYDKEAjksF+oEzRZQBhlQ1aE2vZ86Rz mYNW0dFu/Wm+5W6iF4tc4nqoKeBVylRqkKu6LLY1lQGFMfd7LXmb6u97oDWt07coV+nE wrCe0Gu1h0DmRzITbis0yG2/S1Qkn6G6LFNVIwjBtaNnzMVkYj1GxEGrGOdYkALTpuN5 QvpC6fQ08nQwS+7BGeDsXtJ7VvIthlCtCxBh4i/4HoMZlIzcNbHXZoPhPd3jgIQRPuS1 YzSGfehqLvcvlATt7N9987fCExq1ylCqiyZaSJJOOeAXxuvpLkMcxcymlMSzjkAWX7Gx MFSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692958969; x=1693563769; 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=ZnMgbb3eHMu2WVFHDfLveza5RGGaFqQpAry19Wrh14U=; b=TUhsrDk0Yg6klIlYM3d4EFXscPutRBG0jWZhAYKg7RzXNnQvSNDpnFs0bnHU/+sHGo vbQARPflHvE/RF2xrTgktxAWuoEESa369gXWyv10+wvkNaGAQ4exaGU8qpNJbKMWEOPk cWXdXjWu8A3izXxNhMDtjTDC2FszT3DTHWda6MqB21Gex2BmBcZLk7qWaT6A+4jtFPQh 7hloKbJteKcMKj1DyiAADq1OW5opi64tlu22dD8Jw672x/bKjVi7YtG3Y+SWqImxqOoP GR84xNlVeOmIyYa/S6vppgTDlWdBXKaoCFJiyDW1s+FYcCwbRqdVMK3UFDWq7LGdqxtR p1UQ== X-Gm-Message-State: AOJu0Yy+f/lFV2OilzNY89aIuDDr4+0kpyHfJLdoD8StX/UMwaHlTpVs goP6uWKBwKPIo6uorx8vL5mlYXpOt6Hdeg== X-Google-Smtp-Source: AGHT+IEBVblPXsON54LrLMyRtcVJsWNFf0/x3C/dZJfTnqAwZCBv8g3t/A6EICBJ/s7oPQImk73GgA== X-Received: by 2002:a05:6a00:301e:b0:68a:593a:333f with SMTP id ay30-20020a056a00301e00b0068a593a333fmr20528413pfb.13.1692958968922; Fri, 25 Aug 2023 03:22:48 -0700 (PDT) Received: from localhost.localdomain ([223.104.40.15]) by smtp.gmail.com with ESMTPSA id c5-20020aa781c5000000b0068a077847c7sm1220954pfn.135.2023.08.25.03.22.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Aug 2023 03:22:48 -0700 (PDT) From: Yeqi Fu To: alex.bennee@linaro.org Cc: richard.henderson@linaro.org, qemu-devel@nongnu.org, Yeqi Fu Subject: [RFC v5 03/10] linux-user: Implement envlist_appendenv and add tests for envlist Date: Fri, 25 Aug 2023 18:20:01 +0800 Message-Id: <20230825102009.1754699-4-fufuyqqqqqq@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230825102009.1754699-1-fufuyqqqqqq@gmail.com> References: <20230825102009.1754699-1-fufuyqqqqqq@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::431; envelope-from=fufuyqqqqqq@gmail.com; helo=mail-pf1-x431.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 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 | 13 ++++++ tests/unit/meson.build | 1 + tests/unit/test-envlist.c | 94 +++++++++++++++++++++++++++++++++++++++ util/envlist.c | 67 +++++++++++++++++++++++----- 4 files changed, 165 insertions(+), 10 deletions(-) create mode 100644 tests/unit/test-envlist.c diff --git a/include/qemu/envlist.h b/include/qemu/envlist.h index 6006dfae44..dc0fe4e644 100644 --- a/include/qemu/envlist.h +++ b/include/qemu/envlist.h @@ -1,12 +1,25 @@ #ifndef ENVLIST_H #define ENVLIST_H +#include "qemu/queue.h" + +struct envlist_entry { + char *ev_var; /* actual env value */ + QLIST_ENTRY(envlist_entry) ev_link; +}; + +struct envlist { + QLIST_HEAD(, envlist_entry) el_entries; /* actual entries */ + size_t el_count; /* number of entries */ +}; + typedef struct envlist envlist_t; 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/tests/unit/meson.build b/tests/unit/meson.build index 93977cc32d..9b25b45271 100644 --- a/tests/unit/meson.build +++ b/tests/unit/meson.build @@ -50,6 +50,7 @@ tests = { 'test-qapi-util': [], 'test-interval-tree': [], 'test-xs-node': [qom], + 'test-envlist': [], } if have_system or have_tools diff --git a/tests/unit/test-envlist.c b/tests/unit/test-envlist.c new file mode 100644 index 0000000000..d583c6ee33 --- /dev/null +++ b/tests/unit/test-envlist.c @@ -0,0 +1,94 @@ +/* + * testenvlist unit-tests. + */ + +#include "qemu/osdep.h" +#include "qemu/envlist.h" + +static void envlist_create_free_test(void) +{ + envlist_t *testenvlist; + + testenvlist = envlist_create(); + g_assert(testenvlist != NULL); + g_assert(testenvlist->el_count == 0); + + envlist_free(testenvlist); +} + +static void envlist_set_unset_test(void) +{ + envlist_t *testenvlist; + const char *env = "TEST=123"; + struct envlist_entry *entry; + + testenvlist = envlist_create(); + g_assert(envlist_setenv(testenvlist, env) == 0); + g_assert(testenvlist->el_count == 1); + entry = testenvlist->el_entries.lh_first; + g_assert_cmpstr(entry->ev_var, ==, "TEST=123"); + g_assert(envlist_unsetenv(testenvlist, "TEST") == 0); + g_assert(testenvlist->el_count == 0); + + envlist_free(testenvlist); +} + +static void envlist_length_test(void) +{ + envlist_t *testenvlist; + const char *env = "TEST1=123,TEST2=456"; + + testenvlist = envlist_create(); + g_assert(envlist_parse_set(testenvlist, env) == 0); + g_assert(testenvlist->el_count == 2); + g_assert(envlist_parse_unset(testenvlist, "TEST1,TEST2") == 0); + g_assert(testenvlist->el_count == 0); + + envlist_free(testenvlist); +} + +static void envlist_appendenv_test(void) +{ + envlist_t *testenvlist; + const char *env = "TEST=123"; + struct envlist_entry *entry; + + testenvlist = envlist_create(); + g_assert(envlist_setenv(testenvlist, env) == 0); + g_assert(envlist_appendenv(testenvlist, "TEST=456", ";") == 0); + entry = testenvlist->el_entries.lh_first; + g_assert_cmpstr(entry->ev_var, ==, "TEST=123;456"); + + envlist_free(testenvlist); +} + +static void envlist_to_environ_test(void) +{ + envlist_t *testenvlist; + const char *env = "TEST1=123,TEST2=456"; + size_t count; + char **environ; + + testenvlist = envlist_create(); + g_assert(envlist_parse_set(testenvlist, env) == 0); + g_assert(testenvlist->el_count == 2); + environ = envlist_to_environ(testenvlist, &count); + g_assert(count == 2); + g_assert_cmpstr(environ[1], ==, "TEST1=123"); + g_assert_cmpstr(environ[0], ==, "TEST2=456"); + + envlist_free(testenvlist); +} + +int main(int argc, char **argv) +{ + g_test_init(&argc, &argv, NULL); + + g_test_add_func("/envlist/create_free", envlist_create_free_test); + g_test_add_func("/envlist/set_unset", envlist_set_unset_test); + g_test_add_func("/envlist/length", envlist_length_test); + g_test_add_func("/envlist/appendenv", envlist_appendenv_test); + g_test_add_func("/envlist/to_environ", envlist_to_environ_test); + + return g_test_run(); +} diff --git a/util/envlist.c b/util/envlist.c index db937c0427..2dfead4a0f 100644 --- a/util/envlist.c +++ b/util/envlist.c @@ -2,16 +2,6 @@ #include "qemu/queue.h" #include "qemu/envlist.h" -struct envlist_entry { - const char *ev_var; /* actual env value */ - QLIST_ENTRY(envlist_entry) ev_link; -}; - -struct envlist { - QLIST_HEAD(, envlist_entry) el_entries; /* actual entries */ - size_t el_count; /* number of entries */ -}; - static int envlist_parse(envlist_t *envlist, const char *env, int (*)(envlist_t *, const char *)); @@ -201,6 +191,63 @@ 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) { + GString *new_val = g_string_new(entry->ev_var); + g_string_append(new_val, separator); + g_string_append(new_val, eq_sign + 1); + g_free(entry->ev_var); + entry->ev_var = g_string_free(new_val, false); + } 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 Fri Aug 25 10:20: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: 1825913 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org 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=lKRU2MRy; dkim-atps=neutral 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=patchwork.ozlabs.org) 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 4RXGJH400hz1yZs for ; Fri, 25 Aug 2023 20:23:43 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qZTxz-00078B-MA; Fri, 25 Aug 2023 06:22:59 -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 1qZTxx-00077k-FV for qemu-devel@nongnu.org; Fri, 25 Aug 2023 06:22:57 -0400 Received: from mail-oi1-x234.google.com ([2607:f8b0:4864:20::234]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qZTxv-0004lm-0Q for qemu-devel@nongnu.org; Fri, 25 Aug 2023 06:22:57 -0400 Received: by mail-oi1-x234.google.com with SMTP id 5614622812f47-3a741f46fadso527227b6e.0 for ; Fri, 25 Aug 2023 03:22:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692958974; x=1693563774; 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=bID9qI0h3G1/dqYn59S/QrAa2isHzV5tXKIg0NXmPWU=; b=lKRU2MRyfvWIgfR9z8/urSCMjKiCgMbhIHW2vx6FCRO/WwQy22KHbLQlzb4Og+0Nde NcVlnN3mMvsSvwpSj6MEUo2gMiUyTcAuU7uAkCT1Jv2eGuLhc7SxTZs77TgKrXS9IHWp TL94oDh58Wehb1bh3l+K5t8ctwmwkGxknisjWTNw2t0KYSpg5NyheTHVQWOglwoa8wIV 842z9QFFugeA+cAa3kLI+DwEtNvlEvy2Y3W8gX4eOLJ4/OjODKyxrWJZaNQ9Me3uxhOC /TvDEBVAMYwTQy9yiYfczsCwWrbqkUTz3MQBEqpg20aiKRPviTbXFeBeasKalnL7ZNDO H2GQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692958974; x=1693563774; 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=bID9qI0h3G1/dqYn59S/QrAa2isHzV5tXKIg0NXmPWU=; b=NoJ7netvQCfk1cucCtwfn6Oa8NxitX97oD47b2khhfPK42F1Qs6278AopsBbuEn9w7 jWcNLcbYZn9gurAs2ldb5Dm+GsyCef7vLKmrBRrJysIq0nYAI8rteGzgW5GrPH0QfUX4 yeKOuLoClv28RG1uhfsJI+4+oVDpwtSt0QWGvGLgCmbQOORVEuHJM/9CljeKnoKdxVQZ 68ncg5cXMelwrc1OfNynjGY6XxAUKHVgYUx5tP1lhnkJ2MPj+tP0O5y+SBATtjaTB4b+ J9iQ6gRsOs1olCEjzG/KODUAJb8EGKfCHJnEoDuKOPxzoCn9hC3oXQeL+6p64wF3hgvS Xvig== X-Gm-Message-State: AOJu0YyGN+Pi+jIp0g1LkKFqmzwGT14tnutuwtz8WfhyvKBtUlnP26pZ d8m6HRRdyJsQ6/eVHH4e16D95ZtQceVwbQ== X-Google-Smtp-Source: AGHT+IGbcoD2kAcB0u8Yczp2FTlOTwz3CuaRM4YOv5othW6rSXgOeUTtJX4f1WscX7gRN1YHJuiZ3w== X-Received: by 2002:a54:4890:0:b0:3a8:1727:5af4 with SMTP id r16-20020a544890000000b003a817275af4mr2171239oic.24.1692958973826; Fri, 25 Aug 2023 03:22:53 -0700 (PDT) Received: from localhost.localdomain ([223.104.40.15]) by smtp.gmail.com with ESMTPSA id c5-20020aa781c5000000b0068a077847c7sm1220954pfn.135.2023.08.25.03.22.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Aug 2023 03:22:53 -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 v5 04/10] linux-user: Implement native-bypass option support Date: Fri, 25 Aug 2023 18:20:02 +0800 Message-Id: <20230825102009.1754699-5-fufuyqqqqqq@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230825102009.1754699-1-fufuyqqqqqq@gmail.com> References: <20230825102009.1754699-1-fufuyqqqqqq@gmail.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::234; envelope-from=fufuyqqqqqq@gmail.com; helo=mail-oi1-x234.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 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 --- include/native/native.h | 9 +++++++++ linux-user/main.c | 38 ++++++++++++++++++++++++++++++++++++++ linux-user/syscall.c | 21 +++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 include/native/native.h diff --git a/include/native/native.h b/include/native/native.h new file mode 100644 index 0000000000..7d1baadfcf --- /dev/null +++ b/include/native/native.h @@ -0,0 +1,9 @@ +/* + * Check if the native bypass feature is enabled. + */ +#if defined(CONFIG_USER_ONLY) && defined(CONFIG_NATIVE_CALL) +extern char *native_lib_path; +#define native_bypass_enabled() (native_lib_path != NULL) +#else +#define native_bypass_enabled() false +#endif diff --git a/linux-user/main.c b/linux-user/main.c index dba67ffa36..5cf02c071b 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -60,6 +60,11 @@ #include "semihosting/semihost.h" #endif +#if defined(CONFIG_NATIVE_CALL) +#include "native/native.h" +char *native_lib_path; +#endif + #ifndef AT_FLAGS_PRESERVE_ARGV0 #define AT_FLAGS_PRESERVE_ARGV0_BIT 0 #define AT_FLAGS_PRESERVE_ARGV0 (1 << AT_FLAGS_PRESERVE_ARGV0_BIT) @@ -293,6 +298,17 @@ 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_path = g_strdup(arg); +} +#endif + static void handle_arg_unset_env(const char *arg) { char *r, *p, *token; @@ -522,6 +538,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"}, +#endif {NULL, NULL, false, NULL, NULL, NULL} }; @@ -834,6 +854,24 @@ int main(int argc, char **argv, char **envp) } } +#if defined(CONFIG_NATIVE_CALL) + /* Set the library for native bypass */ + if (native_lib_path) { + if (g_file_test(native_lib_path, G_FILE_TEST_IS_REGULAR)) { + GString *lib = g_string_new(native_lib_path); + lib = g_string_prepend(lib, "LD_PRELOAD="); + if (envlist_appendenv(envlist, g_string_free(lib, false), ":")) { + fprintf(stderr, + "failed to append the native library to environment.\n"); + exit(EXIT_FAILURE); + } + } else { + fprintf(stderr, "native library %s does not exist.\n", + native_lib_path); + exit(EXIT_FAILURE); + } + } +#endif target_environ = envlist_to_environ(envlist, NULL); envlist_free(envlist); diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 08162cc966..bd4c3045ff 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -143,6 +143,7 @@ #include "fd-trans.h" #include "tcg/tcg.h" #include "cpu_loop-common.h" +#include "native/native.h" #ifndef CLONE_IO #define CLONE_IO 0x80000000 /* Clone io context */ @@ -8626,6 +8627,7 @@ static int do_execveat(CPUArchState *cpu_env, int dirfd, abi_ulong addr; char **q; void *p; + unsigned int i; argc = 0; @@ -8696,6 +8698,25 @@ static int do_execveat(CPUArchState *cpu_env, int dirfd, goto execve_efault; } + /* + * An error may occur when executing execv, stating that the + * shared library from LD_PRELOAD cannot be preloaded on a + * different arch. So, we find LD_PRELOAD and remove it from + * envp before executing the execv. + */ + if (native_bypass_enabled()) { + i = 0; + while (envp[i] != NULL) { + if (strncmp(envp[i], "LD_PRELOAD=", 11) == 0) { + for (int j = i; envp[j] != NULL; j++) { + envp[j] = envp[j + 1]; + } + } else { + i++; + } + } + } + if (is_proc_myself(p, "exe")) { ret = get_errno(safe_execveat(dirfd, exec_path, argp, envp, flags)); } else {