Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp356004pxb; Wed, 14 Apr 2021 17:40:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx4/odwLdaRtF0VSE9X0TGTx2LlcrGvhukrUz0VfiLEqPFeo0wMayuEGR05aPs4SkdX6lB8 X-Received: by 2002:a17:90a:94c4:: with SMTP id j4mr840680pjw.14.1618447250515; Wed, 14 Apr 2021 17:40:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618447250; cv=none; d=google.com; s=arc-20160816; b=BqCFr8rsW8FERBBO9rlQ1COIAjAxKVujlv9bCVeknN+pbkeeR95PK0cx8+2/qePyfZ ZdbXVEd1Q2jZn/BwxjlriCBx20V5HbFYXMJtL9aIpsPOpZwMhqqK6qIO89EK6gg0G5Sm XtlIW3s/01BTJV116Tyd+4ccZtVvXqd14480OmnEiCc25EH05SnkYX3zGamNeEDUdGsq Ft2CQYP+j6xj4wBcqDJJo8S5HYBlg7F1FDwQc/2xH5nG+w7msqvv903anlj154bAyH4B mJMBOlkyU8HdpGtc5UvmpI1v1YrP0QQX05ldcG1NFXT8ZtlTG074Th+/ABZGScM17B/M Qo8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=qaeXW2/oJs1QXNcuoeTMx1y+mEseX6UZDEqsVj1QpcI=; b=Et9R2Gyo5Vgq+FHmzxsKsXySKy3s11BpCODvOnkk0wrpT/TNMuaNe8StZRsy+oR0Hb qe6jyMNAP565DQa+dnblaO+GL9r+tph/Gpy1/g7pFKbhjKUnw9s3wBcfiCe0WbX9QhMP kR9srXZAA5Q9sl+sKqe/Sq8T+2PTbB6STuIesYywQiGRTNr07PEug+aaTIS3zcf2MbUX ZbfUUzXNzzqZuDin+tXrNA3ANum0/8Gf4DKBYnjf3Sl+EUjC1AiOS+88JulyZmmAe++M 1KgVMHWaiGyyH7Oa9vaNq6gJ/11IHoGgq91rz4Q5T43gD8X+yYugcyVcgf/L8qriCXru kh5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=M1kOBIaT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c15si1305224pfd.266.2021.04.14.17.40.38; Wed, 14 Apr 2021 17:40:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=M1kOBIaT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353121AbhDNSrb (ORCPT + 99 others); Wed, 14 Apr 2021 14:47:31 -0400 Received: from mail.kernel.org ([198.145.29.99]:49298 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353069AbhDNSrI (ORCPT ); Wed, 14 Apr 2021 14:47:08 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 7B90A61179; Wed, 14 Apr 2021 18:46:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1618426006; bh=ghEMDYXSMr60DXNAYd0/tV0Ldo15eYV1g5ZlE1HiFe0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=M1kOBIaTQdZfI1wDWca4LFG6y9IWw+gXmBFAI8PaBM9wJ9EKYunmLHRWCPXnOpLii GatpvCFPlP2EzLefwpDm0QC/6LVOnFMu5m+Pd5H9Vr8zH8SizNvwCT+fFk2tvmdmvY 2ktAY7xMnzAF3vFw6SApQwnly64XLEp1fagWpkBpl884qGwEp16d7uBCIjnuXwnDf6 bvdwK3+LyJAB8qtyLKiRUeIviRIcqfcG47c966lRsqyT3l3p1fqJXZflRSp2Q83It8 3ifHEbyP+wr9+4Q86U2Q+EnA4Tt8QiAflj6fT8DHLlHxCMD7aufuhyJl0KBjhI9GWR uo35OfYSRiHdg== From: ojeda@kernel.org To: Linus Torvalds , Greg Kroah-Hartman Cc: rust-for-linux@vger.kernel.org, linux-kbuild@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, Miguel Ojeda , Alex Gaynor , Geoffrey Thomas , Finn Behrens , Adam Bratschi-Kaye , Wedson Almeida Filho Subject: [PATCH 05/13] Rust: Compiler builtins crate Date: Wed, 14 Apr 2021 20:45:56 +0200 Message-Id: <20210414184604.23473-6-ojeda@kernel.org> In-Reply-To: <20210414184604.23473-1-ojeda@kernel.org> References: <20210414184604.23473-1-ojeda@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Miguel Ojeda Rust provides `compiler_builtins` as a port of LLVM's `compiler-rt`. Since we do not need the vast majority of them, we avoid the dependency by providing our own crate. We also need a helpers C source file to contain some forwarders to C macros and inlined functions. For the moment, we only need it to call the `BUG()` macro, but we will be adding more later. This also allows us to build `core` from Rust's standard library. Co-developed-by: Alex Gaynor Signed-off-by: Alex Gaynor Co-developed-by: Geoffrey Thomas Signed-off-by: Geoffrey Thomas Co-developed-by: Finn Behrens Signed-off-by: Finn Behrens Co-developed-by: Adam Bratschi-Kaye Signed-off-by: Adam Bratschi-Kaye Co-developed-by: Wedson Almeida Filho Signed-off-by: Wedson Almeida Filho Signed-off-by: Miguel Ojeda --- rust/Makefile | 3 + rust/compiler_builtins.rs | 146 ++++++++++++++++++++++++++++++++++++++ rust/helpers.c | 8 +++ 3 files changed, 157 insertions(+) create mode 100644 rust/compiler_builtins.rs create mode 100644 rust/helpers.c diff --git a/rust/Makefile b/rust/Makefile index ba4b13e4fc7f..5b96462b4fef 100644 --- a/rust/Makefile +++ b/rust/Makefile @@ -1,5 +1,8 @@ # SPDX-License-Identifier: GPL-2.0 +obj-$(CONFIG_RUST) += core.o compiler_builtins.o helpers.o +extra-$(CONFIG_RUST) += exports_core_generated.h + RUSTDOC = rustdoc quiet_cmd_rustdoc = RUSTDOC $< diff --git a/rust/compiler_builtins.rs b/rust/compiler_builtins.rs new file mode 100644 index 000000000000..01f2d905e15f --- /dev/null +++ b/rust/compiler_builtins.rs @@ -0,0 +1,146 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Our own `compiler_builtins`. +//! +//! Rust provides [`compiler_builtins`] as a port of LLVM's [`compiler-rt`]. +//! Since we do not need the vast majority of them, we avoid the dependency +//! by providing this file. +//! +//! At the moment, some builtins are required that should not be. For instance, +//! [`core`] has floating-point functionality which we should not be compiling +//! in. For the moment, we define them to [`panic!`] at runtime for simplicity. +//! These are actually a superset of the ones we actually need to define, +//! but it seems simpler to ban entire categories at once. In the future, +//! we might be able to remove all this by providing our own custom [`core`] +//! etc., or perhaps [`core`] itself might provide `cfg` options to disable +//! enough functionality to avoid requiring some of these. +//! +//! In any case, all these symbols are weakened to ensure we do not override +//! those that may be provided by the rest of the kernel. +//! +//! [`compiler_builtins`]: https://github.com/rust-lang/compiler-builtins +//! [`compiler-rt`]: https://compiler-rt.llvm.org/ + +#![feature(compiler_builtins)] +#![compiler_builtins] +#![no_builtins] +#![no_std] +#![deny(clippy::complexity)] +#![deny(clippy::correctness)] +#![deny(clippy::perf)] +#![deny(clippy::style)] + +macro_rules! define_panicking_intrinsics( + ($reason: tt, { $($ident: ident, )* }) => { + $( + #[doc(hidden)] + #[no_mangle] + pub extern "C" fn $ident() { + panic!($reason); + } + )* + } +); + +define_panicking_intrinsics!("non-inline stack probes should not be used", { + __rust_probestack, +}); + +define_panicking_intrinsics!("`f32` should not be used", { + __addsf3, + __addsf3vfp, + __divsf3, + __divsf3vfp, + __eqsf2, + __eqsf2vfp, + __fixsfdi, + __fixsfsi, + __fixsfti, + __fixunssfdi, + __fixunssfsi, + __fixunssfti, + __floatdisf, + __floatsisf, + __floattisf, + __floatundisf, + __floatunsisf, + __floatuntisf, + __gesf2, + __gesf2vfp, + __gtsf2, + __gtsf2vfp, + __lesf2, + __lesf2vfp, + __ltsf2, + __ltsf2vfp, + __mulsf3, + __mulsf3vfp, + __nesf2, + __nesf2vfp, + __powisf2, + __subsf3, + __subsf3vfp, + __unordsf2, +}); + +define_panicking_intrinsics!("`f64` should not be used", { + __adddf3, + __adddf3vfp, + __divdf3, + __divdf3vfp, + __eqdf2, + __eqdf2vfp, + __fixdfdi, + __fixdfsi, + __fixdfti, + __fixunsdfdi, + __fixunsdfsi, + __fixunsdfti, + __floatdidf, + __floatsidf, + __floattidf, + __floatundidf, + __floatunsidf, + __floatuntidf, + __gedf2, + __gedf2vfp, + __gtdf2, + __gtdf2vfp, + __ledf2, + __ledf2vfp, + __ltdf2, + __ltdf2vfp, + __muldf3, + __muldf3vfp, + __nedf2, + __nedf2vfp, + __powidf2, + __subdf3, + __subdf3vfp, + __unorddf2, +}); + +define_panicking_intrinsics!("`i128` should not be used", { + __ashrti3, + __muloti4, + __multi3, +}); + +define_panicking_intrinsics!("`u128` should not be used", { + __ashlti3, + __lshrti3, + __udivmodti4, + __udivti3, + __umodti3, +}); + +extern "C" { + fn rust_helper_BUG() -> !; +} + +#[panic_handler] +fn panic(_info: &core::panic::PanicInfo) -> ! { + unsafe { + rust_helper_BUG(); + } +} diff --git a/rust/helpers.c b/rust/helpers.c new file mode 100644 index 000000000000..5c2346dd379b --- /dev/null +++ b/rust/helpers.c @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include + +void rust_helper_BUG(void) +{ + BUG(); +} -- 2.17.1