Skip to content

Commit

Permalink
Use ok_or! / unwrap_or!
Browse files Browse the repository at this point in the history
  • Loading branch information
efenniht committed Sep 11, 2019
1 parent 8de583d commit 31b1169
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 55 deletions.
39 changes: 21 additions & 18 deletions hfo2/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,16 +373,19 @@ pub unsafe extern "C" fn api_vcpu_run(
}

// The requested VM must exist.
let vm = ok_or_return!(hafnium().vm_manager.get(vm_id).ok_or(()), ret.into_raw());
let vm = unwrap_or!(
hafnium().vm_manager.get(vm_id),
return ret.into_raw()
);

// The requested vcpu must exist.
let vcpu = ok_or_return!(vm.vcpus.get(vcpu_idx as usize).ok_or(()), ret.into_raw());
let vcpu = unwrap_or!(
vm.vcpus.get(vcpu_idx as usize),
return ret.into_raw()
);

// Update state if allowed.
let mut vcpu_locked = match vcpu_prepare_run(&current, vcpu, ret) {
Ok(locked) => locked,
Err(ret) => return ret.into_raw(),
};
let mut vcpu_locked = ok_or!(vcpu_prepare_run(&current, vcpu, ret), return ret.into_raw());

// Inject timer interrupt if timer has expired. It's safe to access
// vcpu->regs here because vcpu_prepare_run already made sure that
Expand Down Expand Up @@ -519,12 +522,9 @@ pub unsafe extern "C" fn api_spci_msg_send(
}

// Ensure the target VM exists.
let to = ok_or_return!(
hafnium()
.vm_manager
.get(from_msg_replica.target_vm_id)
.ok_or(()),
SpciReturn::InvalidParameters
let to = unwrap_or!(
hafnium().vm_manager.get(from_msg_replica.target_vm_id),
return SpciReturn::InvalidParameters
);

// Hf needs to hold the lock on `to` before the mailbox state is checked.
Expand Down Expand Up @@ -702,7 +702,7 @@ pub unsafe extern "C" fn api_mailbox_waiter_get(vm_id: spci_vm_id_t, current: *c
return -1;
}

let vm = ok_or_return!(hafnium().vm_manager.get(vm_id).ok_or(()), -1);
let vm = unwrap_or!(hafnium().vm_manager.get(vm_id), return -1);

// Check if there are outstanding notifications from given vm.
let entry = (*vm).inner.lock().fetch_waiter();
Expand Down Expand Up @@ -802,7 +802,7 @@ pub unsafe extern "C" fn api_interrupt_inject(
next: *mut *mut VCpu,
) -> i64 {
let mut current = ManuallyDrop::new(VCpuExecutionLocked::from_raw(current));
let target_vm = ok_or_return!(hafnium().vm_manager.get(target_vm_id).ok_or(()), -1);
let target_vm = unwrap_or!(hafnium().vm_manager.get(target_vm_id), return -1);

if intid >= HF_NUM_INTIDS {
return -1;
Expand All @@ -812,7 +812,10 @@ pub unsafe extern "C" fn api_interrupt_inject(
return -1;
}

let target_vcpu = ok_or_return!(target_vm.vcpus.get(target_vcpu_idx as usize).ok_or(()), -1);
let target_vcpu = unwrap_or!(
target_vm.vcpus.get(target_vcpu_idx as usize),
return -1
);

dlog!(
"Injecting IRQ {} for VM {} VCPU {} from VM {} VCPU {}\n",
Expand Down Expand Up @@ -897,7 +900,7 @@ pub fn spci_share_memory(
// Check if the state transition is lawful for both VMs involved in the
// memory exchange, ensure that all constituents of a memory region being
// shared are at the same state.
let (orig_from_mode, from_mode, to_mode) = ok_or_return!(
let (orig_from_mode, from_mode, to_mode) = ok_or!(
spci_msg_check_transition(
&to_locked,
&from_locked,
Expand All @@ -906,7 +909,7 @@ pub fn spci_share_memory(
end,
memory_to_attributes,
),
SpciReturn::InvalidParameters
return SpciReturn::InvalidParameters
);

let pa_begin = pa_from_ipa(begin);
Expand Down Expand Up @@ -1070,7 +1073,7 @@ pub unsafe extern "C" fn api_share_memory(
) -> i64 {
// Convert the sharing request to memory management modes.
// The input is untrusted so might not be a valid value.
let share = ok_or_return!(HfShare::try_from(share), -1);
let share = ok_or!(HfShare::try_from(share), return -1);

match share_memory(vm_id, addr, size, share, &*current) {
Ok(_) => 0,
Expand Down
19 changes: 9 additions & 10 deletions hfo2/src/fdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,14 @@ impl FdtTokenizer {
return None;
}

match self.str() {
Some(name) => Some(name),
None => {
// Move cursor to the end so that caller won't get any new
// tokens.
self.cur = self.end;
None
}
}
let name = unwrap_or!(self.str(), {
// Move cursor to the end so that caller won't get any new
// tokens.
self.cur = self.end;
return None
});

Some(name)
}

unsafe fn skip_properties(&mut self) {
Expand Down Expand Up @@ -458,7 +457,7 @@ pub unsafe extern "C" fn fdt_read_property(
buf: *mut *const u8,
size: *mut u32,
) -> bool {
let (prop_buf, prop_size) = ok_or_return!((*node).read_property(name), false);
let (prop_buf, prop_size) = ok_or!((*node).read_property(name), return false);
*buf = prop_buf;
*size = prop_size;
true
Expand Down
8 changes: 4 additions & 4 deletions hfo2/src/fdt_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ impl FdtNode {
return false;
}

let initrd_begin = ok_or_return!(self.read_number("linux,initrd-start\0".as_ptr()), {
let initrd_begin = ok_or!(self.read_number("linux,initrd-start\0".as_ptr()), {
dlog!("Unable to read linux,initrd-start\n");
false
return false;
});

let initrd_end = ok_or_return!(self.read_number("linux,initrd-end\0".as_ptr()), {
let initrd_end = ok_or!(self.read_number("linux,initrd-end\0".as_ptr()), {
dlog!("Unable to read linux,initrd-end\n");
false
return false;
});

*begin = pa_init(initrd_begin as usize);
Expand Down
22 changes: 8 additions & 14 deletions hfo2/src/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,10 @@ pub unsafe fn load_secondary(
}

let (secondary_mem_begin, secondary_mem_end) =
match carve_out_mem_range(&mut mem_ranges_available, mem as u64) {
Ok(range) => range,
Err(_) => {
dlog!("Not enough memory ({} bytes)\n", mem);
continue;
}
};
ok_or!(carve_out_mem_range(&mut mem_ranges_available, mem as u64), {
dlog!("Not enough memory ({} bytes)\n", mem);
continue;
});

if !copy_to_unmapped(
hypervisor_ptable,
Expand All @@ -285,13 +282,10 @@ pub unsafe fn load_secondary(
return Err(());
}

let vm = match vm_manager.new_vm(cpu as spci_vcpu_count_t, ppool) {
Some(vm) => vm,
None => {
dlog!("Unable to initialise VM\n");
continue;
}
};
let vm = unwrap_or!(vm_manager.new_vm(cpu as spci_vcpu_count_t, ppool), {
dlog!("Unable to initialise VM\n");
continue;
});

// Grant the VM access to the memory.
if vm
Expand Down
14 changes: 7 additions & 7 deletions hfo2/src/mm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1173,17 +1173,17 @@ pub unsafe extern "C" fn mm_vm_unmap_hypervisor(
// TODO: If we add pages dynamically, they must be included here too.
let t = &mut *t;
let mpool = &*mpool;
ok_or_return!(
ok_or!(
t.unmap(layout_text_begin(), layout_text_end(), mpool),
false
return false
);
ok_or_return!(
ok_or!(
t.unmap(layout_rodata_begin(), layout_rodata_end(), mpool),
false
return false
);
ok_or_return!(
ok_or!(
t.unmap(layout_data_begin(), layout_data_end(), mpool),
false
return false
);
true
}
Expand Down Expand Up @@ -1242,7 +1242,7 @@ pub unsafe extern "C" fn mm_unmap(
/// Unsafety doesn't really matter.
#[no_mangle]
pub unsafe extern "C" fn mm_init(mpool: *const MPool) -> bool {
let mm = ok_or_return!(MemoryManager::new(&*mpool).ok_or(()), false);
let mm = unwrap_or!(MemoryManager::new(&*mpool), return false);
ptr::write(&mut HAFNIUM.get_mut().memory_manager, mm);

true
Expand Down
4 changes: 2 additions & 2 deletions hfo2/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
use core::sync::atomic::spin_loop_hint;

#[macro_export]
macro_rules! ok_or_return {
macro_rules! ok_or {
($e:expr, $err:expr) => {{
match $e {
Ok(r) => r,
Err(_) => return $err,
Err(_) => $err,
}
}};
}
Expand Down

0 comments on commit 31b1169

Please sign in to comment.