Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp1713272pxj; Wed, 19 May 2021 12:07:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzMamYfQL2+M0x2NARQdIP8NUsvPys3BTb2isreb1jsRM8umkHrfHtC6a4pZIaAX+xj2CuZ X-Received: by 2002:a05:6402:1518:: with SMTP id f24mr554429edw.217.1621451279196; Wed, 19 May 2021 12:07:59 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1621451279; cv=pass; d=google.com; s=arc-20160816; b=1CF9TvV/PA654cJsv/R+A3yyOWIUiz8wl9VNg2caVlWQUqV4f8zjccmA/qtyWkA7ov 6LwWs/YF6BU9p0rpIS30G+21Ux6Q3DVFBUQiPIoo9qon2npGe06TbwoPSLQAE0aTqFap Q828tDXtU9Qbdq6NjeHAP9ug9d1VZQliMhT66Bxf0S7neuqj59BMZB18Mi9D0RjATg9D 7sYZ4CwB7yHCJlgDrCFCLe2s9n9O9nn/qws3/dKrS4GX0bi80sPHVkUO0ZyY6SUwVk4G fpaDte+mDuNRG1pe0C96xxyvRZ/33vMLvq6lUrLOlKuBH1Qcdgqy9yyWx3n+pjQ7RFci 2l7w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:wdcipoutbound:mime-version :content-transfer-encoding:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:ironport-sdr:dkim-signature; bh=K0zEAWt5nN+c/1Ue2/v6Uk28lHYYsAZ8gCFDu+0xb6E=; b=DDSongec9XlrUx6NvR9rJa6xeNSwAzIiBNBJ9K6pvSxNV/Nlg1wWgpuTLeniuLFL+Y BGBGxEPg5nG/cbbdd02b7wP6GBbTt9IUZ3MENmn1EHIYAcb8NBlCGD3zaZir2kzDqGxF 0M3kOiL1RhNZdAxet67sYeHObHibX53jjW3J/IqjHQm0XfbQHN27ZDaXjY91CFbGIRh4 lkwi+UT3z40oQa7FHkKzwOvwhE3UpTkyB3jojQvtrgFY+TBH8+83o1Ww7Q9t90leRvMZ +XMKMUqWxmyRZqshzFz1L1R5CPBDPTK0mq/wn+9C/j2OxqmphwMbLoHSrQNfLQrJ9EQ4 nAeQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=fail header.i=@wdc.com header.s=dkim.wdc.com header.b=edzoFxzA; dkim=pass header.i=@sharedspace.onmicrosoft.com header.s=selector2-sharedspace-onmicrosoft-com header.b=YsXR+pRO; arc=pass (i=1 spf=pass spfdomain=wdc.com dkim=pass dkdomain=wdc.com dmarc=pass fromdomain=wdc.com); 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=fail (p=NONE sp=NONE dis=NONE) header.from=wdc.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d2si444634ejd.638.2021.05.19.12.07.34; Wed, 19 May 2021 12:07:59 -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=fail header.i=@wdc.com header.s=dkim.wdc.com header.b=edzoFxzA; dkim=pass header.i=@sharedspace.onmicrosoft.com header.s=selector2-sharedspace-onmicrosoft-com header.b=YsXR+pRO; arc=pass (i=1 spf=pass spfdomain=wdc.com dkim=pass dkdomain=wdc.com dmarc=pass fromdomain=wdc.com); 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=fail (p=NONE sp=NONE dis=NONE) header.from=wdc.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353106AbhESDiH (ORCPT + 99 others); Tue, 18 May 2021 23:38:07 -0400 Received: from esa4.hgst.iphmx.com ([216.71.154.42]:26380 "EHLO esa4.hgst.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353057AbhESDiC (ORCPT ); Tue, 18 May 2021 23:38:02 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1621395403; x=1652931403; h=from:to:cc:subject:date:message-id:in-reply-to: references:content-transfer-encoding:mime-version; bh=wpgrVX/0/6lKhyMAHKwQGzbWtLL9n9HO2Q9a3T+j6jY=; b=edzoFxzAiGfRk3ARihajrX+EwruCG+8a37w+nTI8kfKwSIXW38gy+f8J tzLc1pJKHLvDEl0fHPhGx0aAdS3EcjBVZVBOUrXG195OZpAv1VKio9Ak8 fu9hzgWMnhhXXEkjwu/cRyGvdjydMeyHa5CtlGqkkBCsVV4Noey2aTW6j ALRaLvfEUBHdGxuewJf6IrsTamVrs9xhK0ENCi7k6Drpl1rCsrXMIy9mb QGnx4/UD8GjTuHZwmhEzhc9mUnIXu7X/YARoVrqxANKtcjqTxGvkRBARN SYl82cXwwVhQvoFZAg/jbvGNhKLQDECmXhMAxJwV3IYejg4GThS7B/RI4 Q==; IronPort-SDR: wQQNGZNILIRxPlv3uOepL8ii4W08jWJxuFu0G/GVE9wkE2QiJCweg8HV68mliNgW0t0oD/z4O1 PqRWFwjFocrrEfxMsWoRlyofiG1OUdLFwv5zUsJut4vIB5Sc7nIhwuwrKAGXvYvKSKYw72xq2y fAVHYyxR353Uk8+MM1hJHFvhGXTxxUOqyxqAFHeCZY1AyMEFzFcP/kGbdHHEZm7hgIWFRkxTcs Pl+OtDmsedwBbmZA1ivxYCAlMVdfCF0yPsyWLJ7XPZtxfS+VlI3q668jk8bHsCQCgfXoX+ELL2 E5Y= X-IronPort-AV: E=Sophos;i="5.82,311,1613404800"; d="scan'208";a="167950671" Received: from mail-co1nam11lp2169.outbound.protection.outlook.com (HELO NAM11-CO1-obe.outbound.protection.outlook.com) ([104.47.56.169]) by ob1.hgst.iphmx.com with ESMTP; 19 May 2021 11:36:42 +0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QZWNrKYbp7gmhAOTDqU1BgCO0e9aH30mSbzbTzUN5MwA8M4Fvko5ElHFN1C5+Bpfe2c7UhdLAUa3ae+ESATdgmeUkKCzJ+9KwxuIOW41ngD31adyUC/9A8Ez1/msa1RoI+J1gyTtCkYsrxoK2UMo4Q9YcgzRxn2+oDYhV0Au/kjFiOvWgcicUQgbfgvr14VOBoAsieGc3YQ0ZH71gUeUlPU64THmndzcYFP11ciuulm3du4oH3rfXUzdHS670GV1KT8zkX/kXeqhwXFQVYPN3GbnLoYqBcGX/HkRfBBSMlA00D04mbURnpL9bwGOrK/kwhFAimjkzrmBokAN4EZC4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=K0zEAWt5nN+c/1Ue2/v6Uk28lHYYsAZ8gCFDu+0xb6E=; b=JX2FcnIVSZ9WMuLPYzsF2aMWDlMfb6teeOyZavLCInY9zdO6il1qcA5ZnJkjJv0mmdLzfqX4ALgDIKUZgdx51D1ixy/SU/7dd/zlhwauGHVmTpqjHk5ve0XK3OlWsDd4ydtdUW9vz6ZIoSaVVJTHJdNcRO3qP5W1ipnAq/+HEM2OSzbV8yYhfLYdyk8MNAFPndhQ1kZvbRK1291AgGbQqI9hQAr2KW5qR1kD9Vgj4lcoidLSg/51JDnXwDPCuGqKB5mt0A5Rc18KbAbSzVjwxhSzn25Rnmh7EN0q1EbHh4Re5rzYpmQU5dkfIt24JoA663takaijZzdxDpfA34BZdA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=wdc.com; dmarc=pass action=none header.from=wdc.com; dkim=pass header.d=wdc.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sharedspace.onmicrosoft.com; s=selector2-sharedspace-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=K0zEAWt5nN+c/1Ue2/v6Uk28lHYYsAZ8gCFDu+0xb6E=; b=YsXR+pRO+c9JD/jq5sW8hSFz6UxDA4A8Y9t755AOu+4qQIsum2RtKUXq7WjiBYvpuy52Vvc601Lw9Gq1WS7ogRmINwuCr/O/pRPaMSZSMEyqMlvHqVBbPWYuKN4rMOhMJVbmGKGsYhufPWjfBDovkmOngYAR1ueQ+u3XwGmyPAQ= Authentication-Results: dabbelt.com; dkim=none (message not signed) header.d=none;dabbelt.com; dmarc=none action=none header.from=wdc.com; Received: from CO6PR04MB7812.namprd04.prod.outlook.com (2603:10b6:303:138::6) by CO6PR04MB7761.namprd04.prod.outlook.com (2603:10b6:5:35f::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4129.26; Wed, 19 May 2021 03:36:42 +0000 Received: from CO6PR04MB7812.namprd04.prod.outlook.com ([fe80::88a0:bf18:b01d:1a50]) by CO6PR04MB7812.namprd04.prod.outlook.com ([fe80::88a0:bf18:b01d:1a50%4]) with mapi id 15.20.4129.033; Wed, 19 May 2021 03:36:42 +0000 From: Anup Patel To: Palmer Dabbelt , Palmer Dabbelt , Paul Walmsley , Albert Ou , Paolo Bonzini , Jonathan Corbet , Greg Kroah-Hartman Cc: Alexander Graf , Atish Patra , Alistair Francis , Damien Le Moal , Anup Patel , kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-staging@lists.linux.dev, Anup Patel Subject: [PATCH v18 05/18] RISC-V: KVM: Implement KVM_GET_ONE_REG/KVM_SET_ONE_REG ioctls Date: Wed, 19 May 2021 09:05:40 +0530 Message-Id: <20210519033553.1110536-6-anup.patel@wdc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210519033553.1110536-1-anup.patel@wdc.com> References: <20210519033553.1110536-1-anup.patel@wdc.com> Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [122.179.32.148] X-ClientProxiedBy: MA1PR0101CA0057.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a00:20::19) To CO6PR04MB7812.namprd04.prod.outlook.com (2603:10b6:303:138::6) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from wdc.com (122.179.32.148) by MA1PR0101CA0057.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a00:20::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4129.25 via Frontend Transport; Wed, 19 May 2021 03:36:37 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: f53078d4-9f07-4a92-2370-08d91a77515e X-MS-TrafficTypeDiagnostic: CO6PR04MB7761: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: WDCIPOUTBOUND: EOP-TRUE X-MS-Oob-TLC-OOBClassifiers: OLM:7219; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: lRX5/Hqp2w8usAibY7L6OhDDIbnDCFw8Eo6ddHT//hZl71mw1M7LlKNN6SzhnhbNapvmjzUqkGkr2Ey39k/7LDyWJLSXWxr2UPxjqf2Py/zIu19MQHkOSb7feuS9g+uBHOl+HJ0xsU/d9sCkJRf+ywjKqDu5raD0F8jVcGD7KJBs3EhXW5RBPvBnXG+22uelRznBsE+xyduGu2Z0VZE1Qi9CePCsj1ALxIzB9KagvDdmUuCwiL/Oj2K5tc014rDvFC2juBZVvOHylOL8k83DvYR8woS9/TnnYIyoPQjKM6NLK8tI12CNiY7fH5O3fkxzYHac4cpb7T6/m69t71/0FlwtJ9r+n8uMbtYka1h1x23mYpxWRBuebqefDD1P8SnKq8nWyTAyB6LLzaRE0wmbHLDGZ3eL8qjTTU3VCdi7dtoKofhS64lp/29nbc1jayPTvWTbc4D+5hma35+PDauoaETsfIHnIWrCNwuaTsKfQ1tg9+NGJwMP4rxtRK98J7Pj17x45vvoni94PsKLoQVIYFoiGEvG0ZxE2TNo8YyVfUih/zrB5EiP132W/dkxNxg3ZL+K/xfMCQxzntwAGlGQeWv1AvlvPoixEFljpMS6+cv2QQ/OvuShz1eFUQeYjzAf42QCh4lmCVtRIru6FJTzvQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CO6PR04MB7812.namprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(396003)(136003)(376002)(39860400002)(346002)(316002)(38350700002)(55016002)(38100700002)(66476007)(66946007)(1076003)(2616005)(186003)(5660300002)(4326008)(26005)(8936002)(478600001)(86362001)(2906002)(52116002)(7696005)(16526019)(110136005)(83380400001)(8676002)(6666004)(66556008)(36756003)(30864003)(7416002)(956004)(54906003)(44832011)(8886007);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData: =?us-ascii?Q?p/evojtauWERodj8u9C0zvgAu22u8KV6OxaBZSvEKumbfkwR1/Vn7AXog+Aa?= =?us-ascii?Q?6nkr3QTLFeEGFqm0tc8+SmmhEq3bXDuGceNNppd1l2tmp9BZtKLyG4iV0+XM?= =?us-ascii?Q?N8ar2V3zwlW2DQJSHR1CJQbs0b9tzHm9C1pHyM3xnJ/k39NI2wieix5iAXdj?= =?us-ascii?Q?VfaljYAe/XDjn68hOAoUMByrDx2eqjSIiqWSOmIkTdJu0JpwtNdjbKeUMtpM?= =?us-ascii?Q?vYbpoRs7tuDKYcn+TGQbUTI1TLpD5OpjnuD3MQVzaarcblEI4TivoKeOQ9wD?= =?us-ascii?Q?P4777Yg5Z4hxwRyl6vSzqdaNMYsScV/hoei4kyGtUjsEdQOxnP+B3dp4mV1+?= =?us-ascii?Q?h0AcalsbNAH0ZZQ777Z+EkRxJsH17wheLsw95u3c1kamn1FbOwEuLn/LewJl?= =?us-ascii?Q?B0OBymsOIsWEkrehQ7vyyCtAZ3o0vI4g8XhWoCPmJxQDS37NUUSPdqzVYTM+?= =?us-ascii?Q?oTgmp5SkaegRVcSUkN92m2Qd3rwtmjnO61Xxwt8Gt3IMcBPrykXv2/DLt6/x?= =?us-ascii?Q?LkYx9J6I8Fuii9MlX4I2ZmLp09x+n8W20Qn5Npqk0DH6iHOROTcojp9dYjrf?= =?us-ascii?Q?8iKRWSo62j6ozfono+UNLt8H7M9VtDp1DR+hkikbP+QntI/CJYUWgPxUWRZx?= =?us-ascii?Q?DTVMGH6xIoHx63FrC217pjCuZt5MlV3AkyWDk8vPqCUH4rZYBwPuEykTcNV+?= =?us-ascii?Q?b8TO1SAdCumVzjFkmTDr3baPH6WUgIj7+3BCiAtOLaIejp7RVk2EXbu2YBLE?= =?us-ascii?Q?2tY8jo1VqrHRbgyObQg6uy2vimvcqNWjUPblIkuzpBJ8Fo6hX145UsKskrf6?= =?us-ascii?Q?B8Ce75NCA+9PCtd2oTXgsDdn5f5G0L+McYsC1al0aAiDqy7VaV6WsrKIIajq?= =?us-ascii?Q?eWEPsuTgCIhRdN9U0ZdLhC67Gg69uKLKaFq/5VnoPIMsFJEFnj93bVWfHo4N?= =?us-ascii?Q?IF/mgVqK2kz9TSWfgEz76JzZ2MD4gB8os8azggsUHSSZBXXIOiu+33z2IB+F?= =?us-ascii?Q?k92qsdtrJPk2Uf49m7tI3rykq7Jkdk/0lomMaz2R1jGmGbHzAQMNhpk2B0k8?= =?us-ascii?Q?IGaIn0UU8amNnwKGoLEon0kiVik61OXmYjSCuNKwEUviCuAC+n/aSCAjaAl7?= =?us-ascii?Q?eFqW4TREqR64M+KIMduOucGRcBFhRyMuMy0wVM+AAQkQGbEjjHGjG1yMObND?= =?us-ascii?Q?uS5XsmUFi207vg5gSIMnEnliKdvgrYZU7SHz5ZgknyfaVJizhsIMdm4fd8Zb?= =?us-ascii?Q?D9RU2rQCCgNUuhdqDmfpAVjitpBRaUF5ygW2/o9F9YKNxUBlrqZNv8T05O3K?= =?us-ascii?Q?rfaHjlGc8u4ZDlhOEvhXyJ+P?= X-OriginatorOrg: wdc.com X-MS-Exchange-CrossTenant-Network-Message-Id: f53078d4-9f07-4a92-2370-08d91a77515e X-MS-Exchange-CrossTenant-AuthSource: CO6PR04MB7812.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 May 2021 03:36:41.9564 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: b61c8803-16f3-4c35-9b17-6f65f441df86 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: r7yXQqzv/KzcSgN8DvpAeHyEz59tfcdrwg3X2V14BMa6tnOQN9iVqyZbo/KHVjzE9oAjtlnf/Z9XX32O7A4J1w== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CO6PR04MB7761 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org For KVM RISC-V, we use KVM_GET_ONE_REG/KVM_SET_ONE_REG ioctls to access VCPU config and registers from user-space. We have three types of VCPU registers: 1. CONFIG - these are VCPU config and capabilities 2. CORE - these are VCPU general purpose registers 3. CSR - these are VCPU control and status registers The CONFIG register available to user-space is ISA. The ISA register is a read and write register where user-space can only write the desired VCPU ISA capabilities before running the VCPU. The CORE registers available to user-space are PC, RA, SP, GP, TP, A0-A7, T0-T6, S0-S11 and MODE. Most of these are RISC-V general registers except PC and MODE. The PC register represents program counter whereas the MODE register represent VCPU privilege mode (i.e. S/U-mode). The CSRs available to user-space are SSTATUS, SIE, STVEC, SSCRATCH, SEPC, SCAUSE, STVAL, SIP, and SATP. All of these are read/write registers. In future, more VCPU register types will be added (such as FP) for the KVM_GET_ONE_REG/KVM_SET_ONE_REG ioctls. Signed-off-by: Anup Patel Acked-by: Paolo Bonzini Reviewed-by: Paolo Bonzini --- arch/riscv/include/uapi/asm/kvm.h | 53 ++++++- arch/riscv/kvm/vcpu.c | 246 +++++++++++++++++++++++++++++- 2 files changed, 295 insertions(+), 4 deletions(-) diff --git a/arch/riscv/include/uapi/asm/kvm.h b/arch/riscv/include/uapi/asm/kvm.h index 3d3d703713c6..f7e9dc388d54 100644 --- a/arch/riscv/include/uapi/asm/kvm.h +++ b/arch/riscv/include/uapi/asm/kvm.h @@ -41,10 +41,61 @@ struct kvm_guest_debug_arch { struct kvm_sync_regs { }; -/* dummy definition */ +/* for KVM_GET_SREGS and KVM_SET_SREGS */ struct kvm_sregs { }; +/* CONFIG registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */ +struct kvm_riscv_config { + unsigned long isa; +}; + +/* CORE registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */ +struct kvm_riscv_core { + struct user_regs_struct regs; + unsigned long mode; +}; + +/* Possible privilege modes for kvm_riscv_core */ +#define KVM_RISCV_MODE_S 1 +#define KVM_RISCV_MODE_U 0 + +/* CSR registers for KVM_GET_ONE_REG and KVM_SET_ONE_REG */ +struct kvm_riscv_csr { + unsigned long sstatus; + unsigned long sie; + unsigned long stvec; + unsigned long sscratch; + unsigned long sepc; + unsigned long scause; + unsigned long stval; + unsigned long sip; + unsigned long satp; + unsigned long scounteren; +}; + +#define KVM_REG_SIZE(id) \ + (1U << (((id) & KVM_REG_SIZE_MASK) >> KVM_REG_SIZE_SHIFT)) + +/* If you need to interpret the index values, here is the key: */ +#define KVM_REG_RISCV_TYPE_MASK 0x00000000FF000000 +#define KVM_REG_RISCV_TYPE_SHIFT 24 + +/* Config registers are mapped as type 1 */ +#define KVM_REG_RISCV_CONFIG (0x01 << KVM_REG_RISCV_TYPE_SHIFT) +#define KVM_REG_RISCV_CONFIG_REG(name) \ + (offsetof(struct kvm_riscv_config, name) / sizeof(unsigned long)) + +/* Core registers are mapped as type 2 */ +#define KVM_REG_RISCV_CORE (0x02 << KVM_REG_RISCV_TYPE_SHIFT) +#define KVM_REG_RISCV_CORE_REG(name) \ + (offsetof(struct kvm_riscv_core, name) / sizeof(unsigned long)) + +/* Control and status registers are mapped as type 3 */ +#define KVM_REG_RISCV_CSR (0x03 << KVM_REG_RISCV_TYPE_SHIFT) +#define KVM_REG_RISCV_CSR_REG(name) \ + (offsetof(struct kvm_riscv_csr, name) / sizeof(unsigned long)) + #endif #endif /* __LINUX_KVM_RISCV_H */ diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index 1c3c3bd72df9..1df21f9a0d6a 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -18,7 +18,6 @@ #include #include #include -#include #include struct kvm_stats_debugfs_item debugfs_entries[] = { @@ -133,6 +132,225 @@ vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) return VM_FAULT_SIGBUS; } +static int kvm_riscv_vcpu_get_reg_config(struct kvm_vcpu *vcpu, + const struct kvm_one_reg *reg) +{ + unsigned long __user *uaddr = + (unsigned long __user *)(unsigned long)reg->addr; + unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK | + KVM_REG_SIZE_MASK | + KVM_REG_RISCV_CONFIG); + unsigned long reg_val; + + if (KVM_REG_SIZE(reg->id) != sizeof(unsigned long)) + return -EINVAL; + + switch (reg_num) { + case KVM_REG_RISCV_CONFIG_REG(isa): + reg_val = vcpu->arch.isa; + break; + default: + return -EINVAL; + }; + + if (copy_to_user(uaddr, ®_val, KVM_REG_SIZE(reg->id))) + return -EFAULT; + + return 0; +} + +static int kvm_riscv_vcpu_set_reg_config(struct kvm_vcpu *vcpu, + const struct kvm_one_reg *reg) +{ + unsigned long __user *uaddr = + (unsigned long __user *)(unsigned long)reg->addr; + unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK | + KVM_REG_SIZE_MASK | + KVM_REG_RISCV_CONFIG); + unsigned long reg_val; + + if (KVM_REG_SIZE(reg->id) != sizeof(unsigned long)) + return -EINVAL; + + if (copy_from_user(®_val, uaddr, KVM_REG_SIZE(reg->id))) + return -EFAULT; + + switch (reg_num) { + case KVM_REG_RISCV_CONFIG_REG(isa): + if (!vcpu->arch.ran_atleast_once) { + vcpu->arch.isa = reg_val; + vcpu->arch.isa &= riscv_isa_extension_base(NULL); + vcpu->arch.isa &= KVM_RISCV_ISA_ALLOWED; + } else { + return -EOPNOTSUPP; + } + break; + default: + return -EINVAL; + }; + + return 0; +} + +static int kvm_riscv_vcpu_get_reg_core(struct kvm_vcpu *vcpu, + const struct kvm_one_reg *reg) +{ + struct kvm_cpu_context *cntx = &vcpu->arch.guest_context; + unsigned long __user *uaddr = + (unsigned long __user *)(unsigned long)reg->addr; + unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK | + KVM_REG_SIZE_MASK | + KVM_REG_RISCV_CORE); + unsigned long reg_val; + + if (KVM_REG_SIZE(reg->id) != sizeof(unsigned long)) + return -EINVAL; + if (reg_num >= sizeof(struct kvm_riscv_core) / sizeof(unsigned long)) + return -EINVAL; + + if (reg_num == KVM_REG_RISCV_CORE_REG(regs.pc)) + reg_val = cntx->sepc; + else if (KVM_REG_RISCV_CORE_REG(regs.pc) < reg_num && + reg_num <= KVM_REG_RISCV_CORE_REG(regs.t6)) + reg_val = ((unsigned long *)cntx)[reg_num]; + else if (reg_num == KVM_REG_RISCV_CORE_REG(mode)) + reg_val = (cntx->sstatus & SR_SPP) ? + KVM_RISCV_MODE_S : KVM_RISCV_MODE_U; + else + return -EINVAL; + + if (copy_to_user(uaddr, ®_val, KVM_REG_SIZE(reg->id))) + return -EFAULT; + + return 0; +} + +static int kvm_riscv_vcpu_set_reg_core(struct kvm_vcpu *vcpu, + const struct kvm_one_reg *reg) +{ + struct kvm_cpu_context *cntx = &vcpu->arch.guest_context; + unsigned long __user *uaddr = + (unsigned long __user *)(unsigned long)reg->addr; + unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK | + KVM_REG_SIZE_MASK | + KVM_REG_RISCV_CORE); + unsigned long reg_val; + + if (KVM_REG_SIZE(reg->id) != sizeof(unsigned long)) + return -EINVAL; + if (reg_num >= sizeof(struct kvm_riscv_core) / sizeof(unsigned long)) + return -EINVAL; + + if (copy_from_user(®_val, uaddr, KVM_REG_SIZE(reg->id))) + return -EFAULT; + + if (reg_num == KVM_REG_RISCV_CORE_REG(regs.pc)) + cntx->sepc = reg_val; + else if (KVM_REG_RISCV_CORE_REG(regs.pc) < reg_num && + reg_num <= KVM_REG_RISCV_CORE_REG(regs.t6)) + ((unsigned long *)cntx)[reg_num] = reg_val; + else if (reg_num == KVM_REG_RISCV_CORE_REG(mode)) { + if (reg_val == KVM_RISCV_MODE_S) + cntx->sstatus |= SR_SPP; + else + cntx->sstatus &= ~SR_SPP; + } else + return -EINVAL; + + return 0; +} + +static int kvm_riscv_vcpu_get_reg_csr(struct kvm_vcpu *vcpu, + const struct kvm_one_reg *reg) +{ + struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr; + unsigned long __user *uaddr = + (unsigned long __user *)(unsigned long)reg->addr; + unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK | + KVM_REG_SIZE_MASK | + KVM_REG_RISCV_CSR); + unsigned long reg_val; + + if (KVM_REG_SIZE(reg->id) != sizeof(unsigned long)) + return -EINVAL; + if (reg_num >= sizeof(struct kvm_riscv_csr) / sizeof(unsigned long)) + return -EINVAL; + + if (reg_num == KVM_REG_RISCV_CSR_REG(sip)) { + kvm_riscv_vcpu_flush_interrupts(vcpu); + reg_val = csr->hvip >> VSIP_TO_HVIP_SHIFT; + reg_val = reg_val & VSIP_VALID_MASK; + } else if (reg_num == KVM_REG_RISCV_CSR_REG(sie)) { + reg_val = csr->hie >> VSIP_TO_HVIP_SHIFT; + reg_val = reg_val & VSIP_VALID_MASK; + } else + reg_val = ((unsigned long *)csr)[reg_num]; + + if (copy_to_user(uaddr, ®_val, KVM_REG_SIZE(reg->id))) + return -EFAULT; + + return 0; +} + +static int kvm_riscv_vcpu_set_reg_csr(struct kvm_vcpu *vcpu, + const struct kvm_one_reg *reg) +{ + struct kvm_vcpu_csr *csr = &vcpu->arch.guest_csr; + unsigned long __user *uaddr = + (unsigned long __user *)(unsigned long)reg->addr; + unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK | + KVM_REG_SIZE_MASK | + KVM_REG_RISCV_CSR); + unsigned long reg_val; + + if (KVM_REG_SIZE(reg->id) != sizeof(unsigned long)) + return -EINVAL; + if (reg_num >= sizeof(struct kvm_riscv_csr) / sizeof(unsigned long)) + return -EINVAL; + + if (copy_from_user(®_val, uaddr, KVM_REG_SIZE(reg->id))) + return -EFAULT; + + if (reg_num == KVM_REG_RISCV_CSR_REG(sip) || + reg_num == KVM_REG_RISCV_CSR_REG(sie)) { + reg_val = reg_val & VSIP_VALID_MASK; + reg_val = reg_val << VSIP_TO_HVIP_SHIFT; + } + + ((unsigned long *)csr)[reg_num] = reg_val; + + if (reg_num == KVM_REG_RISCV_CSR_REG(sip)) + WRITE_ONCE(vcpu->arch.irqs_pending_mask, 0); + + return 0; +} + +static int kvm_riscv_vcpu_set_reg(struct kvm_vcpu *vcpu, + const struct kvm_one_reg *reg) +{ + if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_CONFIG) + return kvm_riscv_vcpu_set_reg_config(vcpu, reg); + else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_CORE) + return kvm_riscv_vcpu_set_reg_core(vcpu, reg); + else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_CSR) + return kvm_riscv_vcpu_set_reg_csr(vcpu, reg); + + return -EINVAL; +} + +static int kvm_riscv_vcpu_get_reg(struct kvm_vcpu *vcpu, + const struct kvm_one_reg *reg) +{ + if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_CONFIG) + return kvm_riscv_vcpu_get_reg_config(vcpu, reg); + else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_CORE) + return kvm_riscv_vcpu_get_reg_core(vcpu, reg); + else if ((reg->id & KVM_REG_RISCV_TYPE_MASK) == KVM_REG_RISCV_CSR) + return kvm_riscv_vcpu_get_reg_csr(vcpu, reg); + + return -EINVAL; +} + long kvm_arch_vcpu_async_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) { @@ -157,8 +375,30 @@ long kvm_arch_vcpu_async_ioctl(struct file *filp, long kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) { - /* TODO: */ - return -EINVAL; + struct kvm_vcpu *vcpu = filp->private_data; + void __user *argp = (void __user *)arg; + long r = -EINVAL; + + switch (ioctl) { + case KVM_SET_ONE_REG: + case KVM_GET_ONE_REG: { + struct kvm_one_reg reg; + + r = -EFAULT; + if (copy_from_user(®, argp, sizeof(reg))) + break; + + if (ioctl == KVM_SET_ONE_REG) + r = kvm_riscv_vcpu_set_reg(vcpu, ®); + else + r = kvm_riscv_vcpu_get_reg(vcpu, ®); + break; + } + default: + break; + } + + return r; } int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, -- 2.25.1