Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp2504pxb; Fri, 15 Jan 2021 04:23:18 -0800 (PST) X-Google-Smtp-Source: ABdhPJywDhLw8HUQbDd8DnKaTDH0pQeTDlFPN/LeIPbrQBLcfVdhai9a9xauHLpQZDjGtMLcSvdN X-Received: by 2002:a17:906:3094:: with SMTP id 20mr8436992ejv.218.1610713398405; Fri, 15 Jan 2021 04:23:18 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1610713398; cv=pass; d=google.com; s=arc-20160816; b=F3Ks1+ZeuhYxpbpZy3aWqAXlI1AjwxyTlj3o8ADmkU/oKT5Q9vuHMrPrnV4eEEx0t8 Tp4wbQeiRGSKB52zY5K74mjuTsY3efYkGtiDrJhTP26Ecll5La9Wdl3XtguYrmjnEm7N 6aLB+173rhec9jKu2G24d/GD6g85w29pm1KXMVNaVHQWVQNlTyEnbFYNUDuuT5bfbAIF ugXdMNbldJ8n3RhVtzBc0VbqIKq7d+WkBSXXfXu01Y6XBrMcAvbBUZewXcLom3BFZQHt Jj2oiPN7jl6YryONb71xlh0n8pqsdyLt+QIZpDix3yJcXOuH2BYXDGrVSfzKdU8FXK9l 8DRA== 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=U6LMW+fHg5VBUnqkV8Ik1nLvno9fSpvyyG6lGVDAUu8=; b=0EddgJ+w1pAmgIn9NY0cOoKwlsczueujpgd06XiEIia0WE5L9Qt1bdcR/YhRkFFEl+ xHa9BvtgRm4RLI8mYw4Sw258d0OUUjBvsBwOKE06iwl4QiHzpiBtgVXG5V1NLFQwG+YK FSk7BCiYlrOE47dyQqs3MdTGKaGgv5uD/bKeL5X6iywUhw07L09au+kAbopezPltDd9m whBqordGqRocTgSJmM9U8doHRXF8N19yWbBr4wIbMh9TxlmfaZ2Ifwjl0M4npBK2IpUB HTIkgv+JML1yqGyOFoFV7miwcO0oJ8k89/EdBQMhPm+9WnTY1JLNU/E4EbaH62ABjq8a 0xVQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=fail header.i=@wdc.com header.s=dkim.wdc.com header.b=DIad4o76; dkim=pass header.i=@sharedspace.onmicrosoft.com header.s=selector2-sharedspace-onmicrosoft-com header.b=YqdCaHIk; 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 t1si3464667ejc.524.2021.01.15.04.22.54; Fri, 15 Jan 2021 04:23:18 -0800 (PST) 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=DIad4o76; dkim=pass header.i=@sharedspace.onmicrosoft.com header.s=selector2-sharedspace-onmicrosoft-com header.b=YqdCaHIk; 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 S1731147AbhAOMV1 (ORCPT + 99 others); Fri, 15 Jan 2021 07:21:27 -0500 Received: from esa3.hgst.iphmx.com ([216.71.153.141]:11584 "EHLO esa3.hgst.iphmx.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730642AbhAOMVZ (ORCPT ); Fri, 15 Jan 2021 07:21:25 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1610713284; x=1642249284; h=from:to:cc:subject:date:message-id:in-reply-to: references:content-transfer-encoding:mime-version; bh=FncAVryU/X1o20r0vZwUwJRglWAImrSDWppEwgMxLbw=; b=DIad4o76e/TPWr2SWoXOxKQw6bwKCW8rd9Mgs9p/mjg1H/2zV84bLPTv hjDgcAxtbQb69xVrt/Lygb+vXzVsXUoRVEXgjOMEI8ODwV6AQVUhD6hg0 yLoLoLfV7NnCVjG9VuqWiWiLW9ipk//Z2RMjj6gWHxdSyjGyzt7lDyvI/ RjY39h9iK2XHToyr8CvwKE81qAmndiKD7AWdN/ikYpFpqgXwlz+emy3aK tRafsUL9UA1hU3ABLeS99qJL727Myf6ZKZqjsxVs+xVXlGulbsQg9DUQ5 LnOylnz90j6ZPAT2KBG4ezqXmqhOVjwy/JdvEpzCPxU7AHSL1FsjvLkeK Q==; IronPort-SDR: EJ6hstZI3XASv8EXbwD6X0omuCHGr37jcxFn41/5vn+CZNNV34boA4kPtgSLK5Q1YIhBvPaUy6 eFmf2iKChuxJ4gg0+ysyzu07iINVH1cW/eFCf0LJ8ojV3VvshNBCUKOB6ZZglQen2sAXylw5Qw 8sQ/R5uWc9V/0g2AcKyOBbX8FRoh1yhgcnNcJHETV0N2+n5cmoWF0ex/KA5bLCzrhAQ/3Z/NG4 ajD4ZflOuGvF4B0xPp6Si/eSj8WbOtgj+RdhnK7+er2akH/wqF2EcZT6WiSCVtd3Emwp13ODoZ Go4= X-IronPort-AV: E=Sophos;i="5.79,349,1602518400"; d="scan'208";a="161949537" Received: from mail-bl2nam02lp2055.outbound.protection.outlook.com (HELO NAM02-BL2-obe.outbound.protection.outlook.com) ([104.47.38.55]) by ob1.hgst.iphmx.com with ESMTP; 15 Jan 2021 20:19:41 +0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kL2zyBmRCob28wtmFN+9RsnoTiCOewgDa/9qfLCWXTJ2Q0GcanaAUA8Ws9zNmZND47J97Oo5QECI7tkAr+qnir4uQVOMuArkNVCyNb7h3dhKLVQaceUWcZ/8bO680tX/zByZK93VDe356+DKEP1vSIwjjrKUYwBQuMndkt36XX99GgHD274yeeCyqh6e0JXsnlpfplulM2eMqCjvTgqgRIzpNj2mpkUWp/db1LxphibLBk5pUzYZxV4chqIXC8CNmiXNAwjWfEVF42Ni51zktk58PWUnKZV89Y9nTN+2NbDHAFxoVlok8QhR8oM+/1jP20P3gNIjBim0/JTETMxXbw== 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=U6LMW+fHg5VBUnqkV8Ik1nLvno9fSpvyyG6lGVDAUu8=; b=mcVrlobdkzFHiiUt5Xf7DFmPnbXv63LDcbk1No54ufjv4KAiCuTgydu1/zXLjuWbJOhsnj8oRDejTuimDWe094RnTgHl0S8UN9SxxTE+71DGQbIPl3ykF9XviIXPlwJHK3GB5rzRsk2bIh9TMB4Shut3o/ldw/8KEyVK8uAKm0l5itk+QpHLE84YCYBCl4GclAGHXD+fnLPgtnbljyBhShoXasyJga4vO1bqpO1s1BeujRaz7TCkeY7ncT+Lgra2YBBQRHZwPM2oEPNtG4st4xH77er9rVWJfqhGyA0G/2X+U4ebo1o5zdOmH6kbMnVgDAaDu/ekuxhNdzrWIXz12g== 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=U6LMW+fHg5VBUnqkV8Ik1nLvno9fSpvyyG6lGVDAUu8=; b=YqdCaHIkR4qAHgZudLkdK3qjlc8DYzGX9m+OmtRaE0k+3+zTxfAgV0650HvnGrSPByhzLP1Mc/Xqv1rNzERwiiWs/qvRpCk7MBudVCDGDUfHYVxSvQKwOVuArWQuW3z1FrtubxgcPdfTJs5XbQDntkA/V9ljG3fdH8RhPAblVBY= Authentication-Results: dabbelt.com; dkim=none (message not signed) header.d=none;dabbelt.com; dmarc=none action=none header.from=wdc.com; Received: from DM6PR04MB6201.namprd04.prod.outlook.com (2603:10b6:5:127::32) by DM5PR04MB3769.namprd04.prod.outlook.com (2603:10b6:3:fc::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3763.10; Fri, 15 Jan 2021 12:19:40 +0000 Received: from DM6PR04MB6201.namprd04.prod.outlook.com ([fe80::2513:b200:bdc8:b97]) by DM6PR04MB6201.namprd04.prod.outlook.com ([fe80::2513:b200:bdc8:b97%5]) with mapi id 15.20.3742.012; Fri, 15 Jan 2021 12:19:40 +0000 From: Anup Patel To: Palmer Dabbelt , Palmer Dabbelt , Paul Walmsley , Albert Ou , Paolo Bonzini 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-kernel@vger.kernel.org, Anup Patel Subject: [PATCH v16 05/17] RISC-V: KVM: Implement KVM_GET_ONE_REG/KVM_SET_ONE_REG ioctls Date: Fri, 15 Jan 2021 17:48:34 +0530 Message-Id: <20210115121846.114528-6-anup.patel@wdc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210115121846.114528-1-anup.patel@wdc.com> References: <20210115121846.114528-1-anup.patel@wdc.com> Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [122.167.152.18] X-ClientProxiedBy: MAXPR0101CA0024.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a00:c::34) To DM6PR04MB6201.namprd04.prod.outlook.com (2603:10b6:5:127::32) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from wdc.com (122.167.152.18) by MAXPR0101CA0024.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a00:c::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3763.10 via Frontend Transport; Fri, 15 Jan 2021 12:19:36 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 1a8a8efb-cbc0-471c-f091-08d8b94fd55e X-MS-TrafficTypeDiagnostic: DM5PR04MB3769: 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: qx5xVW8ws+O13iFWS+oO5/VYQ8H0OgpCkJDyn+EIjH/svhREIKrVWv8PCK5BWrhNAAT5425HBkltwBAML+20UL8EQ7jbX5gjdAlqjFaSM0eMu0focXr3FvcoV5UGusOHv0Gnv2EJVbUFL7Dz6Yvjc6zCr9BmW66LvQz/Beo7dfDVTbho4dtDFXcC9LO8rdWRn3ZRUareVtyJ8kf/QYLf9J74S6l04afNFs5OU0Xz6SzTqI3YHWZWWBAUyF3Vu8xO99A7jJvk6RiZi4FxDLut+4iJmyxJCM5Zv8xFJekftFkmjtJOv4zeIwLv+cjKBqmeKT3nTyoO35DdOfO5ro9V0HiXssWRdkcYYH5QtS+yRqCFsF/90jIPee256RKmDnjb X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR04MB6201.namprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(39860400002)(346002)(136003)(366004)(376002)(396003)(66476007)(55016002)(7696005)(54906003)(66946007)(110136005)(956004)(5660300002)(2616005)(186003)(52116002)(44832011)(478600001)(66556008)(8936002)(26005)(86362001)(2906002)(83380400001)(7416002)(30864003)(16526019)(8676002)(316002)(4326008)(36756003)(8886007)(1076003);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData: =?us-ascii?Q?g4f2WsCwnBIETMTAfAE2LXGwuJN3dp6YbEj77MXlsQEES/R88CVAWtFlLq8p?= =?us-ascii?Q?wLQDBEEJdq5xXbow2mphqu7wjvN3Yu15yveHAMDcw+U+QmHIjADOPzgh7daE?= =?us-ascii?Q?0bGw9tq+9RAKpANuYpQxYOBr5GInvpiRd/gsWWTZXexg+giPUFfsXFHSV3Lu?= =?us-ascii?Q?R0MDSAapXHQUJ1+mo0Oj4iAOv9rgyzoiiuvjNFL7/rrBbtT+5ALGZLOsfSSn?= =?us-ascii?Q?SRvj1Pv6Cx4KNmFzyf0Z8IMrniQpt1293fzVPA61eaTJG5sbnnvabu+d38wm?= =?us-ascii?Q?7bHC4fty0NJs2SQFHzkpR3tWDeyN5P0Lqu4HH9OEQ0D3q2m7vQB1C34IyJet?= =?us-ascii?Q?gxZrwOa5+3zZXOM86mqRys54kdOwGytWrvc2ZMF34iNGEKvaDQtLzdsS7E2u?= =?us-ascii?Q?qVXxNFTM8XfJWLhVwLxl3pu36+HXXsYc66+Xso/jmLsabbOYKfaBcEMO440F?= =?us-ascii?Q?MQqWPXAG3aKcoptb1XvFnfYHtujZBW2q/ErEkCdBx5XzZMvfB3IAmfAxmRUE?= =?us-ascii?Q?hlhkTvMsDMA9QQrHn1J9MguyxVfbqBMPzbcew6ce6dHqGJWzO/LI3abwmn1y?= =?us-ascii?Q?vIPldgHpNni0gCKwW+iaFuNAvA6MlbF6pHJvslgYG0DqSEbXnU/2FaZc1+4P?= =?us-ascii?Q?tUZ4k/fxX+dmVlM8A+bwaSyyZcS0ZVzLGCD8g+lZbA981nBbzfFkkVqvkXFZ?= =?us-ascii?Q?rC+KgTTRCG6sXzuDQ9YglmysUqBnmCRKbXD5NudPCMDq45V4BMu0RyYD9Lpg?= =?us-ascii?Q?ZwuXK1a5UGjWnZjJmDlkYyYz+rOE0VFQUN3Y9k9j6+M/bYnOuR5LoRr+9Nx/?= =?us-ascii?Q?C09Dw0Ju6eG1a3B+PZMFJDc855vNlHCrk5JWg1IOC3UkouBRyXRJVsO78GdY?= =?us-ascii?Q?aQByiZ6okOwoxgQnU/RaBsozafBCqenW0LIsSMMpwDDXBdN9blwJ081MPPSg?= =?us-ascii?Q?/B9/meHTDm6XIlTy+vXJ9VvwLdXK6iOCkT+S5GiphaRuEw9+Aa7dODPnGHUQ?= =?us-ascii?Q?XXCQ?= X-OriginatorOrg: wdc.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1a8a8efb-cbc0-471c-f091-08d8b94fd55e X-MS-Exchange-CrossTenant-AuthSource: DM6PR04MB6201.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Jan 2021 12:19:40.8807 (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: qpUKOLCxxk2KKGZXt1F6/f5XhZLT3zz2fx+K4jd9kpP4EVek7+g9eQsEbXBgJeMoJ3YQhfhsDhXM9Ekwwpey9g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR04MB3769 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 7acb2e622597..e38eedc784a7 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[] = { @@ -148,6 +147,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) { @@ -172,8 +390,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