-
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
base: hfo2
Are you sure you want to change the base?
Changes from 3 commits
4ce3d22
1a79b49
92a0b71
0c9cee6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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,15 +584,18 @@ 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(); | ||
let mut is_on = vcpu.is_on.lock(); | ||
let vcpu_was_off = *is_on == false; | ||
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. | ||
// Set vCPU registers to a clean state ready for boot. | ||
let mut state = vcpu.inner.lock(); | ||
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. lock order를 바꾼다면 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. reset 을 위해서는 inner 의 락을 결국 걸어야 할 것 같습니다. |
||
debug_assert_eq!(state.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. | ||
state.regs.reset(false, vm, cpu_id_t::from(vcpu.index())); | ||
state.on(entry, arg); | ||
*is_on = true; | ||
} | ||
|
||
vcpu_was_off | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
nightly-2019-12-20 | ||
nightly-2020-09-12 |
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
함수가 사라져서 동일한 의미의 코드로 대체하였습니다.