Skip to content
This repository was archived by the owner on Nov 6, 2024. It is now read-only.

Commit b5c9205

Browse files
RuoqingHerbradford
authored andcommitted
riscv64: Add create VM with one vCPU example
Add example which triggers an MMIO exit after creating a dirty page. Signed-off-by: Ruoqing He <[email protected]>
1 parent f4e96e9 commit b5c9205

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/lib.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Copyright © 2024 Institute of Software, CAS. All rights reserved.
2+
//
13
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
24
// SPDX-License-Identifier: Apache-2.0 OR MIT
35
//
@@ -21,6 +23,7 @@
2123
//!
2224
//! - x86_64
2325
//! - arm64 (experimental)
26+
//! - riscv64 (experimental)
2427
//!
2528
//! **NOTE:** The list of available ioctls is not extensive.
2629
//!
@@ -30,6 +33,7 @@
3033
//! On the vCPU we are running machine specific code. This example is based on
3134
//! the [LWN article](https://lwn.net/Articles/658511/) on using the KVM API.
3235
//! The aarch64 example was modified accordingly.
36+
//! The riscv64 example was modified accordingly.
3337
//!
3438
//! To get code running on the vCPU we are going through the following steps:
3539
//!
@@ -93,6 +97,15 @@
9397
//! 0x14, /* b <this address>; shouldn't get here, but if so loop forever */
9498
//! ];
9599
//! }
100+
//! #[cfg(target_arch = "riscv64")]
101+
//! {
102+
//! asm_code = &[
103+
//! 0x17, 0x03, 0x00, 0x00, // auipc t1, 0; <this address> -> t1
104+
//! 0xa3, 0x23, 0x73, 0x00, // sw t2, t1 + 7; dirty current page
105+
//! 0x23, 0x20, 0x75, 0x00, // sw t2, a0; trigger MMIO exit
106+
//! 0x6f, 0x00, 0x00, 0x00, // j .;shouldn't get here, but if so loop forever
107+
//! ];
108+
//! }
96109
//!
97110
//! // 1. Instantiate KVM.
98111
//! let kvm = Kvm::new().unwrap();
@@ -165,6 +178,17 @@
165178
//! vcpu_fd.set_one_reg(core_reg_base + 2 * 0, &mmio_addr.to_le_bytes());
166179
//! }
167180
//!
181+
//! #[cfg(target_arch = "riscv64")]
182+
//! {
183+
//! // riscv64 specific register setup.
184+
//! let core_reg_base: u64 = 0x8030_0000_0200_0000;
185+
//! let mmio_addr: u64 = guest_addr + mem_size as u64;
186+
//! // set PC
187+
//! vcpu_fd.set_one_reg(core_reg_base, &guest_addr.to_le_bytes());
188+
//! // set A0
189+
//! vcpu_fd.set_one_reg(core_reg_base + 10, &mmio_addr.to_le_bytes());
190+
//! }
191+
//!
168192
//! // 6. Run code on the vCPU.
169193
//! loop {
170194
//! match vcpu_fd.run().expect("run failed") {
@@ -195,7 +219,7 @@
195219
//! // Since on aarch64 there is not halt instruction,
196220
//! // we break immediately after the last known instruction
197221
//! // of the asm code example so that we avoid an infinite loop.
198-
//! #[cfg(target_arch = "aarch64")]
222+
//! #[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]
199223
//! break;
200224
//! }
201225
//! VcpuExit::Hlt => {

0 commit comments

Comments
 (0)