forked from project-oak/hafnium-verification
-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lock split on VCpuInner #82
Open
efenniht
wants to merge
4
commits into
kaist-cp:hfo2
Choose a base branch
from
efenniht:lock-split-hfo2
base: hfo2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4ce3d22
Bump rustc, becuase cargo-xbuild struggles with old rustc.
efenniht 1a79b49
Make an is_on field on VCpu.
efenniht 92a0b71
Check if the vCPU is off on vcpu_secondary_reset_and_start.
efenniht 0c9cee6
WIP: lock vcpuinner earlier than is_on
efenniht File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
use core::mem::{self, ManuallyDrop, MaybeUninit}; | ||
use core::mem::{self, MaybeUninit}; | ||
use core::ops::Deref; | ||
use core::ptr; | ||
|
||
|
@@ -67,7 +67,7 @@ pub const STACK_SIZE: usize = PAGE_SIZE; | |
pub const INTERRUPT_REGISTER_BITS: usize = 32; | ||
|
||
#[repr(C)] | ||
#[derive(PartialEq)] | ||
#[derive(Debug, PartialEq)] | ||
pub enum VCpuStatus { | ||
/// The vcpu is switched off. | ||
Off, | ||
|
@@ -274,21 +274,21 @@ impl VCpuInner { | |
self.regs.set_pc_arg(entry, arg); | ||
self.state = VCpuStatus::Ready; | ||
} | ||
|
||
/// Check whether self is an off state, for the purpose of turning vCPUs on | ||
/// and off. Note that aborted still counts as on in this context. | ||
pub fn is_off(&self) -> bool { | ||
// Aborted still counts as ON for the purposes of PSCI, because according to the PSCI | ||
// specification (section 5.7.1) a core is only considered to be off if it has been turned | ||
// off with a CPU_OFF call or hasn't yet been turned on with a CPU_ON call. | ||
self.state == VCpuStatus::Off | ||
} | ||
} | ||
|
||
#[repr(C)] | ||
pub struct VCpu { | ||
vm: *mut Vm, | ||
|
||
/// Whether the vCPU is on. | ||
/// | ||
/// Only meaningful for secondary VM. For primary, use Cpu::is_on instead. | ||
/// | ||
/// Aborted still counts as ON for the purposes of PSCI, because according to the PSCI | ||
/// specification (section 5.7.1) a core is only considered to be off if it has been turned off | ||
/// with a CPU_OFF call or hasn't yet been turned on with a CPU_ON call. | ||
pub is_on: SpinLock<bool>, | ||
|
||
/// If a vCPU of secondary VMs is running, its lock is logically held by the running pCPU. | ||
pub inner: SpinLock<VCpuInner>, | ||
pub interrupts: SpinLock<Interrupts>, | ||
|
@@ -298,6 +298,7 @@ impl VCpu { | |
pub fn new(vm: *mut Vm) -> Self { | ||
Self { | ||
vm, | ||
is_on: SpinLock::new(false), | ||
inner: SpinLock::new(VCpuInner::new()), | ||
interrupts: SpinLock::new(Interrupts::new()), | ||
} | ||
|
@@ -309,8 +310,8 @@ impl VCpu { | |
|
||
pub fn index(&self) -> spci_vcpu_index_t { | ||
let vcpus = self.vm().vcpus.as_ptr(); | ||
let index = (self as *const VCpu).wrapping_offset_from(vcpus); | ||
assert!(index < core::u16::MAX as isize); | ||
let index = (self as *const VCpu as usize - vcpus as usize) / core::mem::size_of::<VCpu>(); | ||
assert!(index < core::u16::MAX as _); | ||
index as _ | ||
} | ||
} | ||
|
@@ -398,7 +399,7 @@ pub unsafe fn cpu_get_buffer(cpu_id: cpu_id_t) -> &'static mut RawPage { | |
static mut MESSAGE_BUFFER: MaybeUninit<[RawPage; MAX_CPUS]> = MaybeUninit::uninit(); | ||
assert!(cpu_id < MAX_CPUS as _); | ||
|
||
&mut MESSAGE_BUFFER.get_mut()[cpu_id as usize] | ||
&mut MESSAGE_BUFFER.assume_init_mut()[cpu_id as usize] | ||
} | ||
|
||
pub struct CpuManager { | ||
|
@@ -440,7 +441,7 @@ impl CpuManager { | |
} | ||
|
||
pub fn index_of(&self, c: *const Cpu) -> usize { | ||
c.wrapping_offset_from(self.cpus.as_ptr()) as _ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. again, why? |
||
(c as usize - self.cpus.as_ptr() as usize) / mem::size_of::<Cpu>() | ||
} | ||
|
||
pub fn cpu_on(&self, c: &Cpu, entry: ipaddr_t, arg: uintreg_t, vm_manager: &VmManager) -> bool { | ||
|
@@ -560,18 +561,12 @@ pub unsafe extern "C" fn vcpu_is_interrupted(vcpu: *const VCpu) -> bool { | |
(*vcpu).interrupts.lock().is_interrupted() | ||
} | ||
|
||
/// Check whether the given vcpu_inner is an off state, for the purpose of | ||
/// turning vCPUs on and off. Note that aborted still counts as on in this | ||
/// context. | ||
/// | ||
/// # Safety | ||
/// Check whether the given vcpu is an off state, for the purpose of turning vCPUs on and off. | ||
/// | ||
/// This function is intentionally marked as unsafe because `vcpu` should actually be | ||
/// `VCpuExecutionLocked`. | ||
/// Note that aborted still counts as on in this context. | ||
#[no_mangle] | ||
pub unsafe extern "C" fn vcpu_is_off(vcpu: VCpuExecutionLocked) -> bool { | ||
let vcpu = ManuallyDrop::new(vcpu); | ||
vcpu.get_inner().is_off() | ||
pub unsafe extern "C" fn vcpu_is_off(vcpu: *const VCpu) -> bool { | ||
*(*vcpu).is_on.lock() == false | ||
} | ||
|
||
/// Starts a vCPU of a secondary VM. | ||
|
@@ -589,18 +584,20 @@ pub unsafe extern "C" fn vcpu_secondary_reset_and_start( | |
|
||
assert!(vm.id != HF_PRIMARY_VM_ID); | ||
|
||
let mut state = vcpu.inner.lock(); | ||
let vcpu_was_off = state.is_off(); | ||
if vcpu_was_off { | ||
// Set vCPU registers to a clean state ready for boot. As this | ||
// is a secondary which can migrate between pCPUs, the ID of the | ||
// vCPU is defined as the index and does not match the ID of the | ||
// pCPU it is running on. | ||
state.regs.reset(false, vm, cpu_id_t::from(vcpu.index())); | ||
state.on(entry, arg); | ||
// Running vCPU holds its inner lock. | ||
if let Ok(mut inner) = vcpu.inner.try_lock() { | ||
if inner.state == VCpuStatus::Off { | ||
// As this is a secondary which can migrate between pCPUs, the ID of the vCPU is defined | ||
// as the index and does not match the ID of the pCPU it is running on. | ||
inner.regs.reset(false, vm, cpu_id_t::from(vcpu.index())); | ||
inner.on(entry, arg); | ||
*vcpu.is_on.lock() = true; | ||
|
||
return true; | ||
} | ||
} | ||
|
||
vcpu_was_off | ||
false | ||
} | ||
|
||
/// Handles a page fault. It does so by determining if it's a legitimate or | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
nightly-2019-12-20 | ||
nightly-2020-09-12 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cargo-xbuild 가 오래된 rustc 에서 잘 컴파일이 안 되어서, rustc 를 업데이트하였는데, 그 여파로
wrapping_offset_from
함수가 사라져서 동일한 의미의 코드로 대체하였습니다.