From patchwork Fri Apr 7 23:11:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Evan Green X-Patchwork-Id: 81086 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp580056vqo; Fri, 7 Apr 2023 16:33:30 -0700 (PDT) X-Google-Smtp-Source: AKy350ZyU9nMutHK5+FkuQTThUZaSozjbARR771MJg+sgwkO8S0JzVK4F0+9UM1LNhannbkbB111 X-Received: by 2002:a17:90b:685:b0:237:40a5:7cb9 with SMTP id m5-20020a17090b068500b0023740a57cb9mr599050pjz.5.1680910410489; Fri, 07 Apr 2023 16:33:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680910410; cv=none; d=google.com; s=arc-20160816; b=VsdTN9ItnNLGrwmzatkuldxQurBt8qW0NrWaIkm2AvemzrFv5KJfjim+7aoIVUD+Ud 4Ok1m/RRdzly8Jo7BFGHTywPg1DT2ZLeYkfsCn2XN6kBUTTyNa4ni5CiTlycGfvOsmGc 8UH057BMQ1uB8wG4vKO9dmUlozR09vl8Y/VCaS7FZQNXwPrwgDumt69VUHC8eo9sqdsS tUSePB+UpkJY2kos9K8tQWMRWhDXRuczt26apcmxi6T25TOjednktJhWwqMIrXNysFEh N0JwrCi/Sly3nqSROREKlsuDBiIrjQbMF93E6vksvB6upOjXPk9+BHJY8Qk1M46200Kb 0VVg== 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=EeiF7h/sLSez/TlRCMWgLKqMk/ne4YncC8dJfp6Awr0=; b=QDHoZ2n47n2FeX+Lw54bw5PYxCOBakYk7HytlRUMPQMGtDJgswncYERf/tqG7XlMft +kSGyJN9rhMYyULgZxuGufJSbN2EzveZ14bhmnjHBmlV+C7iNWmOTslo/XmltPmTrszC h8H4+V9USLTBUc3QT8mLIXwZ7v7OukXtneEghAOPFe2/b+1i/eGMcCvXc0Xw104Wi72b v0QJl02RWUPCdnAzhoUrjGpd10cWgzvfREOk0NxyPkSKr5ZMfN9RbsALOhwS0wRX3jDb LCoxMjRV8RRKEHuV59Pj8a+p7d41v7b+/Ba4Bn8HuOZN5mJmg3ja2a+zFAeSKkLpTtnt MsUw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=Ks+i1Ri+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id pi9-20020a17090b1e4900b00240ad93edfesi4898414pjb.5.2023.04.07.16.33.18; Fri, 07 Apr 2023 16:33:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=Ks+i1Ri+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229886AbjDGXLu (ORCPT + 99 others); Fri, 7 Apr 2023 19:11:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39878 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229806AbjDGXLh (ORCPT ); Fri, 7 Apr 2023 19:11:37 -0400 Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6786AEFA8 for ; Fri, 7 Apr 2023 16:11:30 -0700 (PDT) Received: by mail-pj1-x102a.google.com with SMTP id c3so424469pjg.1 for ; Fri, 07 Apr 2023 16:11:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; t=1680909090; x=1683501090; 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=EeiF7h/sLSez/TlRCMWgLKqMk/ne4YncC8dJfp6Awr0=; b=Ks+i1Ri+o5JpPSrcf84VoquzeV7CKM7pStzjr83VTMhq6fjWwm2LjLq5bt/eNi3+8U Uhj9o76hF1GgF6f3ujr8zvStpxmcnZrllP0aUaVS9pb5Ta80CwV3U4yDlVlwkM55lfGh /hs3btz2LoWTHYf2TwYILOLNigEvgxSmjjJUg8sJctVAva9GjTc/w8qzH+gy2/WTmPeI 8UsL7WlZ9G84Yqy6eYyml4jktIavJtmyCZ3ZgvztEV+afyksWpt65a796kWvCxhx6vA9 0MIQF6MWmMOtoRd2OEJ6LYA4M4Qe9ZBFHv5W6U969zVFl6l9jgH293WkBxb7PULQQu+H /Kpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680909090; x=1683501090; 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=EeiF7h/sLSez/TlRCMWgLKqMk/ne4YncC8dJfp6Awr0=; b=ImnOgZEO7nIO3lUGjW+RenyUH2urFUZh0iQZgUUV6h2V1csZBl4yHkojg58tuuaNn5 OiOPMlINDU6/TXeK6X0ZCy6OBTD3g+33QIwdBoxxD16EJL0k9oQ1HqYlP/PouEPJ3UtV 1NzOxlW/xDxei/89BaPGCWdXBKRe0L+jUNzAZUgqL17sq2ViPAaDKU2fQvkNXzNOLeV5 353u32C+PkO+8XKXVk488Kqyvr4Rk0Z9TrlbTCm3PktCQwOv9xH3J8XK0y9b7XF9RhaN JcE/V/ZPV7/1QtBN8Okyr1Z3OFwwDTbFRM077tsrDLJ0jlUYUQsJdVKUX9mc+uaFMQ1u 8f2w== X-Gm-Message-State: AAQBX9c+FyOalspV26gysRKD37Hhlo3yXCp+qd3QBPvFeGydXd11I55s amgLr/x60UqrKY6xXuO+fOiUWA== X-Received: by 2002:a17:902:e54f:b0:1a0:67ef:dc61 with SMTP id n15-20020a170902e54f00b001a067efdc61mr129076plf.17.1680909089701; Fri, 07 Apr 2023 16:11:29 -0700 (PDT) Received: from evan.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id r5-20020a170902be0500b001a076568da9sm3361526pls.216.2023.04.07.16.11.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Apr 2023 16:11:29 -0700 (PDT) From: Evan Green To: Palmer Dabbelt Cc: slewis@rivosinc.com, heiko@sntech.de, Conor Dooley , vineetg@rivosinc.com, Evan Green , Albert Ou , Catalin Marinas , Mark Brown , Palmer Dabbelt , Paul Walmsley , Shuah Khan , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-riscv@lists.infradead.org Subject: [PATCH v6 5/6] selftests: Test the new RISC-V hwprobe interface Date: Fri, 7 Apr 2023 16:11:02 -0700 Message-Id: <20230407231103.2622178-6-evan@rivosinc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230407231103.2622178-1-evan@rivosinc.com> References: <20230407231103.2622178-1-evan@rivosinc.com> MIME-Version: 1.0 X-Spam-Status: No, score=0.0 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762562314449019459?= X-GMAIL-MSGID: =?utf-8?q?1762562314449019459?= This adds a test for the recently added RISC-V interface for probing hardware capabilities. It happens to be the first selftest we have for RISC-V, so I've added some infrastructure for those as well. Co-developed-by: Palmer Dabbelt Signed-off-by: Palmer Dabbelt Signed-off-by: Evan Green --- Changes in v6: - Update copyrights (Paul) Changes in v4: - Fixed selftests commit description, no more tiny libc (Mark Brown) - Fixed selftest syscall prototype types to match v4. Changes in v2: - Updated the selftests to the new API and added some more. - Fixed indentation, comments in .S, and general checkpatch complaints. --- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/riscv/Makefile | 58 ++++++++++++ .../testing/selftests/riscv/hwprobe/Makefile | 10 +++ .../testing/selftests/riscv/hwprobe/hwprobe.c | 90 +++++++++++++++++++ .../selftests/riscv/hwprobe/sys_hwprobe.S | 12 +++ 5 files changed, 171 insertions(+) create mode 100644 tools/testing/selftests/riscv/Makefile create mode 100644 tools/testing/selftests/riscv/hwprobe/Makefile create mode 100644 tools/testing/selftests/riscv/hwprobe/hwprobe.c create mode 100644 tools/testing/selftests/riscv/hwprobe/sys_hwprobe.S diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile index 13a6837a0c6b..4bea26109450 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile @@ -63,6 +63,7 @@ TARGETS += pstore TARGETS += ptrace TARGETS += openat2 TARGETS += resctrl +TARGETS += riscv TARGETS += rlimits TARGETS += rseq TARGETS += rtc diff --git a/tools/testing/selftests/riscv/Makefile b/tools/testing/selftests/riscv/Makefile new file mode 100644 index 000000000000..32a72902d045 --- /dev/null +++ b/tools/testing/selftests/riscv/Makefile @@ -0,0 +1,58 @@ +# SPDX-License-Identifier: GPL-2.0 +# Originally tools/testing/arm64/Makefile + +# When ARCH not overridden for crosscompiling, lookup machine +ARCH ?= $(shell uname -m 2>/dev/null || echo not) + +ifneq (,$(filter $(ARCH),riscv)) +RISCV_SUBTARGETS ?= hwprobe +else +RISCV_SUBTARGETS := +endif + +CFLAGS := -Wall -O2 -g + +# A proper top_srcdir is needed by KSFT(lib.mk) +top_srcdir = $(realpath ../../../../) + +# Additional include paths needed by kselftest.h and local headers +CFLAGS += -I$(top_srcdir)/tools/testing/selftests/ + +CFLAGS += $(KHDR_INCLUDES) + +export CFLAGS +export top_srcdir + +all: + @for DIR in $(RISCV_SUBTARGETS); do \ + BUILD_TARGET=$(OUTPUT)/$$DIR; \ + mkdir -p $$BUILD_TARGET; \ + $(MAKE) OUTPUT=$$BUILD_TARGET -C $$DIR $@; \ + done + +install: all + @for DIR in $(RISCV_SUBTARGETS); do \ + BUILD_TARGET=$(OUTPUT)/$$DIR; \ + $(MAKE) OUTPUT=$$BUILD_TARGET -C $$DIR $@; \ + done + +run_tests: all + @for DIR in $(RISCV_SUBTARGETS); do \ + BUILD_TARGET=$(OUTPUT)/$$DIR; \ + $(MAKE) OUTPUT=$$BUILD_TARGET -C $$DIR $@; \ + done + +# Avoid any output on non riscv on emit_tests +emit_tests: all + @for DIR in $(RISCV_SUBTARGETS); do \ + BUILD_TARGET=$(OUTPUT)/$$DIR; \ + $(MAKE) OUTPUT=$$BUILD_TARGET -C $$DIR $@; \ + done + +clean: + @for DIR in $(RISCV_SUBTARGETS); do \ + BUILD_TARGET=$(OUTPUT)/$$DIR; \ + $(MAKE) OUTPUT=$$BUILD_TARGET -C $$DIR $@; \ + done + +.PHONY: all clean install run_tests emit_tests diff --git a/tools/testing/selftests/riscv/hwprobe/Makefile b/tools/testing/selftests/riscv/hwprobe/Makefile new file mode 100644 index 000000000000..ebdbb3c22e54 --- /dev/null +++ b/tools/testing/selftests/riscv/hwprobe/Makefile @@ -0,0 +1,10 @@ +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2021 ARM Limited +# Originally tools/testing/arm64/abi/Makefile + +TEST_GEN_PROGS := hwprobe + +include ../../lib.mk + +$(OUTPUT)/hwprobe: hwprobe.c sys_hwprobe.S + $(CC) -o$@ $(CFLAGS) $(LDFLAGS) $^ diff --git a/tools/testing/selftests/riscv/hwprobe/hwprobe.c b/tools/testing/selftests/riscv/hwprobe/hwprobe.c new file mode 100644 index 000000000000..09f290a67420 --- /dev/null +++ b/tools/testing/selftests/riscv/hwprobe/hwprobe.c @@ -0,0 +1,90 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include + +/* + * Rather than relying on having a new enough libc to define this, just do it + * ourselves. This way we don't need to be coupled to a new-enough libc to + * contain the call. + */ +long riscv_hwprobe(struct riscv_hwprobe *pairs, size_t pair_count, + size_t cpu_count, unsigned long *cpus, unsigned int flags); + +int main(int argc, char **argv) +{ + struct riscv_hwprobe pairs[8]; + unsigned long cpus; + long out; + + /* Fake the CPU_SET ops. */ + cpus = -1; + + /* + * Just run a basic test: pass enough pairs to get up to the base + * behavior, and then check to make sure it's sane. + */ + for (long i = 0; i < 8; i++) + pairs[i].key = i; + out = riscv_hwprobe(pairs, 8, 1, &cpus, 0); + if (out != 0) + return -1; + for (long i = 0; i < 4; ++i) { + /* Fail if the kernel claims not to recognize a base key. */ + if ((i < 4) && (pairs[i].key != i)) + return -2; + + if (pairs[i].key != RISCV_HWPROBE_KEY_BASE_BEHAVIOR) + continue; + + if (pairs[i].value & RISCV_HWPROBE_BASE_BEHAVIOR_IMA) + continue; + + return -3; + } + + /* + * This should also work with a NULL CPU set, but should not work + * with an improperly supplied CPU set. + */ + out = riscv_hwprobe(pairs, 8, 0, 0, 0); + if (out != 0) + return -4; + + out = riscv_hwprobe(pairs, 8, 0, &cpus, 0); + if (out == 0) + return -5; + + out = riscv_hwprobe(pairs, 8, 1, 0, 0); + if (out == 0) + return -6; + + /* + * Check that keys work by providing one that we know exists, and + * checking to make sure the resultig pair is what we asked for. + */ + pairs[0].key = RISCV_HWPROBE_KEY_BASE_BEHAVIOR; + out = riscv_hwprobe(pairs, 1, 1, &cpus, 0); + if (out != 0) + return -7; + if (pairs[0].key != RISCV_HWPROBE_KEY_BASE_BEHAVIOR) + return -8; + + /* + * Check that an unknown key gets overwritten with -1, + * but doesn't block elements after it. + */ + pairs[0].key = 0x5555; + pairs[1].key = 1; + pairs[1].value = 0xAAAA; + out = riscv_hwprobe(pairs, 2, 0, 0, 0); + if (out != 0) + return -9; + + if (pairs[0].key != -1) + return -10; + + if ((pairs[1].key != 1) || (pairs[1].value == 0xAAAA)) + return -11; + + return 0; +} diff --git a/tools/testing/selftests/riscv/hwprobe/sys_hwprobe.S b/tools/testing/selftests/riscv/hwprobe/sys_hwprobe.S new file mode 100644 index 000000000000..a4773c88d267 --- /dev/null +++ b/tools/testing/selftests/riscv/hwprobe/sys_hwprobe.S @@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* Copyright (C) 2023 Rivos, Inc */ + +.text +.global riscv_hwprobe +riscv_hwprobe: + # Put __NR_riscv_hwprobe in the syscall number register, then just shim + # back the kernel's return. This doesn't do any sort of errno + # handling, the caller can deal with it. + li a7, 258 + ecall + ret