diff --git a/hfo2/src/api.rs b/hfo2/src/api.rs index 0dbdaf3aa..ca5871319 100644 --- a/hfo2/src/api.rs +++ b/hfo2/src/api.rs @@ -217,7 +217,7 @@ pub unsafe extern "C" fn api_vcpu_get_count( return 0; } - let vm = unwrap_or!(hafnium().vm_manager.get(vm_id), return 0); + let vm = some_or!(hafnium().vm_manager.get(vm_id), return 0); vm.vcpus.len() as spci_vcpu_count_t } @@ -373,10 +373,10 @@ pub unsafe extern "C" fn api_vcpu_run( } // The requested VM must exist. - let vm = unwrap_or!(hafnium().vm_manager.get(vm_id), return ret.into_raw()); + let vm = some_or!(hafnium().vm_manager.get(vm_id), return ret.into_raw()); // The requested vcpu must exist. - let vcpu = unwrap_or!(vm.vcpus.get(vcpu_idx as usize), return ret.into_raw()); + let vcpu = some_or!(vm.vcpus.get(vcpu_idx as usize), return ret.into_raw()); // Update state if allowed. let mut vcpu_locked = match vcpu_prepare_run(¤t, vcpu, ret) { @@ -519,7 +519,7 @@ pub unsafe extern "C" fn api_spci_msg_send( } // Ensure the target VM exists. - let to = unwrap_or!( + let to = some_or!( hafnium().vm_manager.get(from_msg_replica.target_vm_id), return SpciReturn::InvalidParameters ); @@ -699,7 +699,7 @@ pub unsafe extern "C" fn api_mailbox_waiter_get(vm_id: spci_vm_id_t, current: *c return -1; } - let vm = unwrap_or!(hafnium().vm_manager.get(vm_id), return -1); + let vm = some_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(); @@ -799,7 +799,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 = unwrap_or!(hafnium().vm_manager.get(target_vm_id), return -1); + let target_vm = some_or!(hafnium().vm_manager.get(target_vm_id), return -1); if intid >= HF_NUM_INTIDS { return -1; @@ -809,7 +809,7 @@ pub unsafe extern "C" fn api_interrupt_inject( return -1; } - let target_vcpu = unwrap_or!(target_vm.vcpus.get(target_vcpu_idx as usize), return -1); + let target_vcpu = some_or!(target_vm.vcpus.get(target_vcpu_idx as usize), return -1); dlog!( "Injecting IRQ {} for VM {} VCPU {} from VM {} VCPU {}\n", diff --git a/hfo2/src/fdt.rs b/hfo2/src/fdt.rs index 5c0411c48..969951610 100644 --- a/hfo2/src/fdt.rs +++ b/hfo2/src/fdt.rs @@ -193,7 +193,7 @@ impl FdtTokenizer { return None; } - let name = unwrap_or!(self.str(), { + let name = some_or!(self.str(), { // Move cursor to the end so that caller won't get any new // tokens. self.cur = self.end; @@ -317,7 +317,7 @@ impl FdtHeader { } // Traverse the whole thing. - let node = unwrap_or!(FdtNode::new_root(self), { + let node = some_or!(FdtNode::new_root(self), { dlog!("FDT failed validation.\n"); return; }); @@ -423,7 +423,7 @@ pub unsafe extern "C" fn fdt_dump(hdr: *mut FdtHeader) { #[no_mangle] pub unsafe extern "C" fn fdt_root_node(node: *mut FdtNode, hdr: *const FdtHeader) -> bool { - let n = unwrap_or!(FdtNode::new_root(&*hdr), return false); + let n = some_or!(FdtNode::new_root(&*hdr), return false); ptr::write(node, n); true } @@ -435,7 +435,7 @@ pub unsafe extern "C" fn fdt_find_child(node: *mut FdtNode, child: *const u8) -> #[no_mangle] pub unsafe extern "C" fn fdt_first_child(node: *mut FdtNode, child_name: *mut *const u8) -> bool { - let name = unwrap_or!((*node).first_child(), return false); + let name = some_or!((*node).first_child(), return false); ptr::write(child_name, name); true } @@ -445,7 +445,7 @@ pub unsafe extern "C" fn fdt_next_sibling( node: *mut FdtNode, sibling_name: *mut *const u8, ) -> bool { - let name = unwrap_or!((*node).next_sibling(), return false); + let name = some_or!((*node).next_sibling(), return false); ptr::write(sibling_name, name); true } diff --git a/hfo2/src/load.rs b/hfo2/src/load.rs index b3d09e69f..17c88a950 100644 --- a/hfo2/src/load.rs +++ b/hfo2/src/load.rs @@ -74,7 +74,7 @@ pub unsafe fn load_primary( ) -> Result { let primary_begin = layout_primary_begin(); - let it = unwrap_or!(find_file(&mut cpio.clone(), "vmlinuz\0".as_ptr()), { + let it = some_or!(find_file(&mut cpio.clone(), "vmlinuz\0".as_ptr()), { dlog!("Unable to find vmlinuz\n"); return Err(()); }); @@ -95,7 +95,7 @@ pub unsafe fn load_primary( return Err(()); } - let initrd = unwrap_or!(find_file(&mut cpio.clone(), "initrd.img\0".as_ptr()), { + let initrd = some_or!(find_file(&mut cpio.clone(), "initrd.img\0".as_ptr()), { dlog!("Unable to find initrd.img\n"); return Err(()); }); @@ -104,7 +104,7 @@ pub unsafe fn load_primary( .new_vm(MAX_CPUS as spci_vcpu_count_t, ppool) .ok_or_else(|| { dlog!("Unable to initialise primary vm\n"); - (()) + () })?; if vm.id != HF_PRIMARY_VM_ID { @@ -214,7 +214,7 @@ pub unsafe fn load_secondary( mem_ranges_available.clone_from_slice(¶ms.mem_ranges); mem_ranges_available.truncate(params.mem_ranges_count); - let mut it = unwrap_or!(find_file(&mut cpio.clone(), "vms.txt\0".as_ptr()), { + let mut it = some_or!(find_file(&mut cpio.clone(), "vms.txt\0".as_ptr()), { dlog!("vms.txt is missing\n"); return Ok(()); }); @@ -227,14 +227,14 @@ pub unsafe fn load_secondary( loop { // Note(HfO2): There is `while let (Some(x), Some(y)) = (...) {}` but it // is not short-circuiting. - let mut mem = unwrap_or!(it.parse_uint(), break); - let cpu = unwrap_or!(it.parse_uint(), break); - let name = unwrap_or!(it.parse_str(), break); + let mut mem = some_or!(it.parse_uint(), break); + let cpu = some_or!(it.parse_uint(), break); + let name = some_or!(it.parse_str(), break); let name_str = str::from_utf8_unchecked(name.as_slice()); dlog!("Loading {}\n", name_str); - let kernel = unwrap_or!(find_file_memiter(&mut cpio.clone(), &name), { + let kernel = some_or!(find_file_memiter(&mut cpio.clone(), &name), { dlog!("Unable to load kernel\n"); continue; }); @@ -280,7 +280,7 @@ pub unsafe fn load_secondary( return Err(()); } - let vm = unwrap_or!(vm_manager.new_vm(cpu as spci_vcpu_count_t, ppool), { + let vm = some_or!(vm_manager.new_vm(cpu as spci_vcpu_count_t, ppool), { dlog!("Unable to initialise VM\n"); continue; }); diff --git a/hfo2/src/mm.rs b/hfo2/src/mm.rs index 4c84899b0..114f8f213 100644 --- a/hfo2/src/mm.rs +++ b/hfo2/src/mm.rs @@ -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 = unwrap_or!(MemoryManager::new(&*mpool), return false); + let mm = some_or!(MemoryManager::new(&*mpool), return false); ptr::write(&mut HAFNIUM.get_mut().memory_manager, mm); true diff --git a/hfo2/src/utils.rs b/hfo2/src/utils.rs index 5814e55b8..bbc7b257d 100644 --- a/hfo2/src/utils.rs +++ b/hfo2/src/utils.rs @@ -27,7 +27,7 @@ macro_rules! ok_or { } #[macro_export] -macro_rules! unwrap_or { +macro_rules! some_or { ($e:expr, $err:expr) => {{ match $e { Some(r) => r,