From patchwork Wed May 12 10:54:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 12253335 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3178AC433ED for ; Wed, 12 May 2021 10:54:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DFF45613F9 for ; Wed, 12 May 2021 10:54:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230289AbhELKzy (ORCPT ); Wed, 12 May 2021 06:55:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230211AbhELKzw (ORCPT ); Wed, 12 May 2021 06:55:52 -0400 Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4216EC06175F for ; Wed, 12 May 2021 03:54:44 -0700 (PDT) Received: by mail-ej1-x635.google.com with SMTP id c22so6197893ejd.12 for ; Wed, 12 May 2021 03:54:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=XWgAsOmnprNK2skeWGppDbTh40fKVhmJp00QypGoM2k=; b=MraJAz0HI7UmqDdHOLpC69mGNLqWbEVwA0L7nKwLIS/+vXQlFGpS2+92ERofya2IO3 32ygsxDvJRJ+Dorery3U1PUb/sNOAZIqqVSzL4j/zpGgYH4MvufElz0geWAgJ/tvmz/V w9ePMTRtH//TRbZ7p+mW+qZcwAiL4OwzlCVjP7WKCNKPotuM7N3L3ymwh0xjZpf8eYGQ Mu247fjQyAzRA686m7FAnO6YyShacqnPAFDRFelh58xMNwlAvK5CibBOLzqcnByOV1MU YLI4I8bKgUpteXkBOkF6jV2mLEhnYedata+o7IQADh63tzE02YvHWzxRYgWVekzsjFYL Ppgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=XWgAsOmnprNK2skeWGppDbTh40fKVhmJp00QypGoM2k=; b=CTbl8HJBUilaN/+jdiKx6Hee/e/3+pKsI6qAmw0etzesI/dq3IaJg9nHZQy/j63HTZ 3Op5YomXgWsnUR6EzAgRfCyL/J7jTm40LVwNx7/3X9jG2++RiZxXxVKY0CBrPrilnJjm +CBRO7oumeqUJblZHG3iBm5WB1M+31I9FJ+uZk9N7M7QpRBhPL5c7Af5I4NNzNjLNDLv JtgK3pU1pw+8Eh1sxmNeXS6ZCtDNVn+soU4ZvMCAJABCzglnHPAx3u+dpVHFQ88T6jnP 8U91zjTDFoKwm+UrToOV9+Jv+CuG2BPXR4+XXC6bh3L7IpcSOonmz+gYPtaI0NCZDcMb S8Kw== X-Gm-Message-State: AOAM532qaQAKebKdXsrHJLaMZK8HpC1v3mIzRzdHUwwfLSqg4Y7UtYFv /4nt4II2QfgTyWzlAf8Ke2EdtzKd8AI= X-Google-Smtp-Source: ABdhPJzigDhlUEFhg9q6amk4thrZa+of/18EGmVGYFbGF3OGaz8W5MPMklCYns/17wJN3V0j1ZleeA== X-Received: by 2002:a17:906:1f54:: with SMTP id d20mr18427747ejk.94.1620816883017; Wed, 12 May 2021 03:54:43 -0700 (PDT) Received: from avogadro.lan ([2001:b07:6468:f312:c8dd:75d4:99ab:290a]) by smtp.gmail.com with ESMTPSA id b19sm16829624edd.66.2021.05.12.03.54.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 May 2021 03:54:42 -0700 (PDT) Sender: Paolo Bonzini From: Paolo Bonzini To: kvm@vger.kernel.org Cc: Alexandru Elisei Subject: [PATCH v2 kvm-unit-tests 1/2] libcflat: clean up and complete long division routines Date: Wed, 12 May 2021 12:54:39 +0200 Message-Id: <20210512105440.748153-2-pbonzini@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210512105440.748153-1-pbonzini@redhat.com> References: <20210512105440.748153-1-pbonzini@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Avoid possible uninitialized variables on machines where division by zero does not trap. Add __divmoddi4, and use it in __moddi3 and __divdi3. Reported-by: Alexandru Elisei Signed-off-by: Paolo Bonzini Reviewed-by: Alexandru Elisei --- lib/ldiv32.c | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/lib/ldiv32.c b/lib/ldiv32.c index 96f4b35..897a4b9 100644 --- a/lib/ldiv32.c +++ b/lib/ldiv32.c @@ -1,6 +1,7 @@ #include extern uint64_t __udivmoddi4(uint64_t num, uint64_t den, uint64_t *p_rem); +extern int64_t __divmoddi4(int64_t num, int64_t den, int64_t *p_rem); extern int64_t __moddi3(int64_t num, int64_t den); extern int64_t __divdi3(int64_t num, int64_t den); extern uint64_t __udivdi3(uint64_t num, uint64_t den); @@ -11,8 +12,11 @@ uint64_t __udivmoddi4(uint64_t num, uint64_t den, uint64_t *p_rem) uint64_t quot = 0; /* Trigger a division by zero at run time (trick taken from iPXE). */ - if (den == 0) + if (den == 0) { + if (p_rem) + *p_rem = 0; return 1/((unsigned)den); + } if (num >= den) { /* Align den to num to avoid wasting time on leftmost zero bits. */ @@ -35,31 +39,35 @@ uint64_t __udivmoddi4(uint64_t num, uint64_t den, uint64_t *p_rem) return quot; } -int64_t __moddi3(int64_t num, int64_t den) +int64_t __divmoddi4(int64_t num, int64_t den, int64_t *p_rem) { - uint64_t mask = num < 0 ? -1 : 0; + int32_t nmask = num < 0 ? -1 : 0; + int32_t qmask = (num ^ den) < 0 ? -1 : 0; + uint64_t quot; /* Compute absolute values and do an unsigned division. */ - num = (num + mask) ^ mask; + num = (num + nmask) ^ nmask; if (den < 0) den = -den; - /* Copy sign of num into result. */ - return (__umoddi3(num, den) + mask) ^ mask; + /* Copy sign of num^den into quotient, sign of num into remainder. */ + quot = (__udivmoddi4(num, den, (uint64_t *)p_rem) + qmask) ^ qmask; + if (p_rem) + *p_rem = (*p_rem + nmask) ^ nmask; + return quot; } -int64_t __divdi3(int64_t num, int64_t den) +int64_t __moddi3(int64_t num, int64_t den) { - uint64_t mask = (num ^ den) < 0 ? -1 : 0; - - /* Compute absolute values and do an unsigned division. */ - if (num < 0) - num = -num; - if (den < 0) - den = -den; + int64_t rem; + __divmoddi4(num, den, &rem); + return rem; +} - /* Copy sign of num^den into result. */ - return (__udivdi3(num, den) + mask) ^ mask; +int64_t __divdi3(int64_t num, int64_t den) +{ + int64_t rem; + return __divmoddi4(num, den, &rem); } uint64_t __udivdi3(uint64_t num, uint64_t den) From patchwork Wed May 12 10:54:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 12253339 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0894CC43460 for ; Wed, 12 May 2021 10:54:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C11FA613FB for ; Wed, 12 May 2021 10:54:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230316AbhELKz5 (ORCPT ); Wed, 12 May 2021 06:55:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230246AbhELKzy (ORCPT ); Wed, 12 May 2021 06:55:54 -0400 Received: from mail-ed1-x531.google.com (mail-ed1-x531.google.com [IPv6:2a00:1450:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA38DC061761 for ; Wed, 12 May 2021 03:54:44 -0700 (PDT) Received: by mail-ed1-x531.google.com with SMTP id c22so26502615edn.7 for ; Wed, 12 May 2021 03:54:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NPXzGpmso8Te8IRLGmmznTWDVnaW8I7l+0F2ZmGTikk=; b=WBwhP4kSQeL/Rzy5eduUceKcWKhU7XRl9kE6Hsqnt7EkH3iNZy2hFCTGDpUjAsZZ/7 VcEoKdDQuztgQNI0Fl9E34PwAzunDLxEcqemp6V2TNC1cf7v0d+M5VBt3hwqJgRvG1vH SAqckcUAGLG4W46IYiWG6fvaKoJ44/SR/dAtFYSM3d52ZT93om1YAZmuQR2kye74wByk WHm8CBcfJpCLT/8swpPIaUrtmssl++SDIwmb09UBNcYn/5GRMHwY38Ucxk29bOI340Cr pnxjM5AeY2epGNHTXfj8Tl+ZYCMadgtC5RE+ZTYzi0FP6xKlseXKwdm8NzkT2CjofX/Y eENg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=NPXzGpmso8Te8IRLGmmznTWDVnaW8I7l+0F2ZmGTikk=; b=uBCpNtqlNpFhzkDhnQ48ggHr8yeGtNqSedWcrwl6iha7/A5ebeNUVYdBwDlhjejgDL psvoeTjdEdRgsKlo0vwtnv1w3nVJV6VmUI8X3tqdJLdtdKZxTWTdHmKwOXBSvBQuJQKd fJx4Qzyq9XysS5vLwCoZiMbzoFGKFd7XW03nSZx8okOp+DSwGmdkecPTuCedAxm8oqtY GsgA3kC4cX6SrkOqZSeeDH3WLsx6hhuJMioo0i5hk5Yks0ct0+TKDYdGEzDrGSe8ofLw 6iH3EGFnuyiC8QqYbCCr26019kHQSgItiOVDAyKd6vUnEkx8B9R2YA03iYQcY/IbOOB1 kOEA== X-Gm-Message-State: AOAM532zr4Fv99ccnoSnaq0YHa0vVP+f9WHP3DQi2AqfnyUCWWv8hqtV Zje2OFjNZJj62xPwVHS1/Ef0c3WiC4E= X-Google-Smtp-Source: ABdhPJxx1vATkhTNvJOlK9tTb/FsDaxl+CQ55MaKjd7u6oOtMjk488IFc6dxRkqtfm3+mQovFFKVzQ== X-Received: by 2002:aa7:c30c:: with SMTP id l12mr42319992edq.217.1620816883622; Wed, 12 May 2021 03:54:43 -0700 (PDT) Received: from avogadro.lan ([2001:b07:6468:f312:c8dd:75d4:99ab:290a]) by smtp.gmail.com with ESMTPSA id b19sm16829624edd.66.2021.05.12.03.54.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 May 2021 03:54:43 -0700 (PDT) Sender: Paolo Bonzini From: Paolo Bonzini To: kvm@vger.kernel.org Subject: [PATCH v2 kvm-unit-tests 2/2] arm: add eabi version of 64-bit division functions Date: Wed, 12 May 2021 12:54:40 +0200 Message-Id: <20210512105440.748153-3-pbonzini@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210512105440.748153-1-pbonzini@redhat.com> References: <20210512105440.748153-1-pbonzini@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org eabi prescribes different entry points for 64-bit division on 32-bit platforms. Implement a wrapper for the GCC-style __divmoddi4 and __udivmoddi4 functions. Signed-off-by: Paolo Bonzini Reviewed-by: Alexandru Elisei --- arm/Makefile.arm | 1 + lib/arm/ldivmod.S | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 lib/arm/ldivmod.S diff --git a/arm/Makefile.arm b/arm/Makefile.arm index 687a8ed..3a4cc6b 100644 --- a/arm/Makefile.arm +++ b/arm/Makefile.arm @@ -24,6 +24,7 @@ cflatobjs += lib/arm/spinlock.o cflatobjs += lib/arm/processor.o cflatobjs += lib/arm/stack.o cflatobjs += lib/ldiv32.o +cflatobjs += lib/arm/ldivmod.o # arm specific tests tests = diff --git a/lib/arm/ldivmod.S b/lib/arm/ldivmod.S new file mode 100644 index 0000000..de11ac9 --- /dev/null +++ b/lib/arm/ldivmod.S @@ -0,0 +1,32 @@ +// EABI ldivmod and uldivmod implementation based on libcompiler-rt +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. + + .syntax unified + .align 2 + .globl __aeabi_uldivmod + .type __aeabi_uldivmod, %function +__aeabi_uldivmod: + push {r11, lr} + sub sp, sp, #16 + add r12, sp, #8 + str r12, [sp] // third argument to __udivmoddi4 + bl __udivmoddi4 + ldr r2, [sp, #8] // remainder returned in r2-r3 + ldr r3, [sp, #12] + add sp, sp, #16 + pop {r11, pc} + + .globl __aeabi_ldivmod + .type __aeabi_ldivmod, %function +__aeabi_ldivmod: + push {r11, lr} + sub sp, sp, #16 + add r12, sp, #8 + str r12, [sp] // third argument to __divmoddi4 + bl __divmoddi4 + ldr r2, [sp, #8] // remainder returned in r2-r3 + ldr r3, [sp, #12] + add sp, sp, #16 + pop {r11, pc}