Received: by 2002:ac0:e34a:0:0:0:0:0 with SMTP id g10csp341560imn; Mon, 25 Jul 2022 18:37:06 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uZc0DisLM4MqupGzuoGAFVRmN/lao9NMeeJx6kB8UG10taenCreFtmm54wbosT5s0gpnWZ X-Received: by 2002:a65:6b8a:0:b0:3db:7dc5:fec2 with SMTP id d10-20020a656b8a000000b003db7dc5fec2mr12807542pgw.223.1658799426331; Mon, 25 Jul 2022 18:37:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658799426; cv=none; d=google.com; s=arc-20160816; b=Bl+ChP+EU9CLV/jfHc6EBxOMLzT/LYZLC7gxQBo4wONNdM3LAp+Mto5XtY8vEcmsS5 BnKJY8H/XgIL/Pg1EszZt86aKPL+FsbhqaMoS8z9a/C+I29XRAO21waEQW/Fvhws+V8o UShFgTnwuBjV7CidiBTbxmyFSZXl6E+UoDn08f0+BFAk3LuLdTyVhoHlSdjUPMY19pb3 mOyxCT3kNhB1xBs7uDma/CLyKDiFjaMD68Eo0XZeKs6lpfKS3ivycJR8qUYDtovKc0GB 7w1PutU0WPwc6kpIxpu0tJ3zp/hoe7YRfoaksNYEVgX1fz8vMuGiOTCQxsU9XRnIH2eE LaMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=UKkB3HULRj3pUMmgT7OAFIB5pdUVGm/GcYLhlJ2fbiM=; b=fBj2po7PMZAVV5dHWO4RuTFn3x8oqy3P8MNV7tBcaDo+Vb5D8YWJDFGDpf0jvBBfZ1 EIIfPcO0oUno0M2Gc67N955+oAa1DUXc4hJb/BMpngJRFCsfoWXM1DtOB5XpkqxLymRo E1jseRMPf8j2+tvz7a1md6sMv4KrHQPtFlgDLVFvakInPt2yzM4agjEnwsbCvWJYVZ3p DkiNtxdioRXRc4IFzx8urWthP95DRPIFCn7DkR23bJxB8TGeXXjlMqxM5RqXT2j562wr BaaZZzXvNGqX+I23uzRUy72ExMLchWTLDs5XZ3TwGqRjoi1LwS3yQkrWhtn6XplrVsZc 0QBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=ZOPuhM7S; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i2-20020a17090332c200b0016d0a9f9c4esi17610878plr.103.2022.07.25.18.36.50; Mon, 25 Jul 2022 18:37:06 -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=@gmail.com header.s=20210112 header.b=ZOPuhM7S; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237017AbiGZBUp (ORCPT + 99 others); Mon, 25 Jul 2022 21:20:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55328 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236868AbiGZBUo (ORCPT ); Mon, 25 Jul 2022 21:20:44 -0400 Received: from mail-qt1-x82e.google.com (mail-qt1-x82e.google.com [IPv6:2607:f8b0:4864:20::82e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EFA86B1D1 for ; Mon, 25 Jul 2022 18:20:43 -0700 (PDT) Received: by mail-qt1-x82e.google.com with SMTP id r21so9523999qtn.11 for ; Mon, 25 Jul 2022 18:20:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=UKkB3HULRj3pUMmgT7OAFIB5pdUVGm/GcYLhlJ2fbiM=; b=ZOPuhM7S6MsLqjdVjiwtnjsXjl6yFboh+slRXuHNgPB//aoSd0w/GAXGC2AYR7E6Ww KZbRpBwMS8hOOUPcRAHwax4aiQOqgSq5TyOjDlx72wnBdsbpVJU7ShDIyE0ky6LDWKm8 a2ebzRMMAXZRf6f1GG8ZH/BchCamLL0Fl7Tvd8AnuNLoyyzSE37PwNn4POWee0MeOeF3 BUlSOHoWwtqHlZxRodeb+1WR5R2jl9Kus9PvQ1Dv0S5uzOFf9MdIH+5ph6vhADxxjH/R uROg486VwASsvEs/jABOqwC3t2SY3WpVAHBBkTPw3r/wK/YzYuE3oL9/wuzhUPBnQ5Mi ZbLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=UKkB3HULRj3pUMmgT7OAFIB5pdUVGm/GcYLhlJ2fbiM=; b=t6JVAMhod+CsiA7P3OTdajznKud8l4GcHFijdA7vrPy5bXAzzuR1Jr+yqSNWxxwqxj 0UvCcXi9zDXOt+Q/GwTB96irBjkf7yNJUch07Urxdx6y+42CKpAtAIg93226faNi7car J9XFCqrwxiK8CWJdImQore/sp2UYt5cRXKQko/AFvapMLTttyzdmMiCyCLMJKiUGa6hU AIrA5ygNzWB5r9na3b5MvF4a7qGmsMz97ioQwi71NKQuSQovBHGgBqnghrtin900yXEL 9Hp9DVQE3qXUncn/TErfi9HfwkgjfnQ6TRrjKBj+h3hgy9PvW9CpVMX53I8+naO+1ZJR +YkA== X-Gm-Message-State: AJIora+HAwzbavmuXTji9naQzfLnjiacOk1MgohYXvER4Lg3A0GwhPu6 GIQUQUNI8WpCQc7hkdAh9iaSgYlBp1lvGGV4dSZWyROJ X-Received: by 2002:a05:622a:100d:b0:31f:25e3:7a45 with SMTP id d13-20020a05622a100d00b0031f25e37a45mr12446508qte.365.1658798443069; Mon, 25 Jul 2022 18:20:43 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Bin Meng Date: Tue, 26 Jul 2022 09:20:29 +0800 Message-ID: Subject: Re: How to read RISC-V mcycle CSR from Linux userspace app? To: Maxim Blinov Cc: "qemu-devel@nongnu.org Developers" , "open list:RISC-V" , linux-kernel Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham 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 On Tue, Jul 26, 2022 at 12:58 AM Maxim Blinov wrote: > > Hi all, stupid question but I can't for the life of me figure this out > even with all the docs open. > > I need to get an estimate figure for the cyclecount of a busy loop in > one of my small Linux userspace apps. The app in question is running > in qemu-system-riscv64. I've compiled QEMU myself, and the full code > is like this: > > #include > #include > #include > #include > > uint64_t get_mcycle() { > uint64_t mcycle = 0; > > asm volatile ("csrr %0,mcycle" : "=r" (mcycle) ); Change this to "csrr %0,cycle" and you should be able to run your program. > > return mcycle; > } > > int main(int argc, char **argv) { > printf("Hello\n"); > printf("mcycle is %lu\n", get_mcycle()); > > return 0; > } > > Now I get SIGILL when I hit the `csrr` insn, which makes sense. > According to the "Privileged Architecture Version 1.10", page 32, [1] > we need to set mcounteren, hcounteren, and scounteren low bits to 1 in > order to get the mcycle csr to become available in userspace. So I add > the following function: > > void enable_mcount() { > /* Enable IR, TM, CY */ > uint64_t mcounteren = 0x5; > asm volatile ("csrw mcounteren,%0" : "=r" (mcounteren)); > asm volatile ("csrw hcounteren,%0" : "=r" (mcounteren)); > asm volatile ("csrw scounteren,%0" : "=r" (mcounteren)); > } > > And call it before I call get_mcycle(), but this triggers SIGILL > (unsurprisingly) also, since these CSRs are also privileged. So > there's a bit of a chicken and egg problem. > > Could someone more knowledgeable please suggest what the course of > action here is? I've got QEMU revision f45fd24c90 checked out, and I'm > staring at qemu/target/riscv/csr.c:71, which seems to deal with > whether or not to raise a SIGILL upon access. I can see a condition > for when we're in 'S' mode, but nothing for 'U' mode. Does that mean > there is fundamentally no access to these CSR's from 'U' mode? Is it > possible to just hack it in? In the user space, you can only read the U-mode CSRs like cycle, but NOT mcycle. > > Maxim > > [1]: https://riscv.org/wp-content/uploads/2017/05/riscv-privileged-v1.10.pdf > Regards, Bin