kernel/vmm: Untangle a too deeply nested if/else.
authorSascha Wildner <saw@online.de>
Mon, 17 Aug 2015 17:23:18 +0000 (19:23 +0200)
committerSascha Wildner <saw@online.de>
Mon, 17 Aug 2015 17:23:57 +0000 (19:23 +0200)
Get rid of if/goto/else cases. Check errors first and return early where
possible.

sys/platform/pc64/vmm/vmm_utils.c

index 9e65f29..362d01a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003-2013 The DragonFly Project.  All rights reserved.
+ * Copyright (c) 2013 The DragonFly Project.  All rights reserved.
  *
  * This code is derived from software contributed to The DragonFly Project
  * by Mihai Carabas <mihai.carabas@gmail.com>
@@ -44,7 +44,6 @@
 int
 instr_check(struct instr_decode *instr, void *ip, uint8_t instr_length)
 {
-
        uint8_t i;
        uint8_t *instr_ip;
        uint8_t *instr_opcode;
@@ -97,7 +96,8 @@ error:
 }
 
 int
-guest_phys_addr(struct vmspace *vm, register_t *gpa, register_t guest_cr3, vm_offset_t uaddr)
+guest_phys_addr(struct vmspace *vm, register_t *gpa, register_t guest_cr3,
+    vm_offset_t uaddr)
 {
        pt_entry_t pml4e;
        pt_entry_t pdpe;
@@ -110,57 +110,58 @@ guest_phys_addr(struct vmspace *vm, register_t *gpa, register_t guest_cr3, vm_of
                kprintf("%s: could not get pml4e\n", __func__);
                goto error;
        }
-       if (pml4e & kernel_pmap.pmap_bits[PG_V_IDX]) {
-               err = get_pt_entry(vm, &pdpe, pml4e & PG_FRAME, (uaddr & PML4MASK) >> PDPSHIFT);
-               if (err) {
-                       kprintf("%s: could not get pdpe\n", __func__);
-                       goto error;
-               }
-               if (pdpe & kernel_pmap.pmap_bits[PG_V_IDX]) {
-                       if (pdpe & kernel_pmap.pmap_bits[PG_PS_IDX]) {
-                               *gpa = (pdpe & PG_FRAME) | (uaddr & PDPMASK);
-                               goto out;
-                       } else {
-                               err = get_pt_entry(vm, &pde, pdpe & PG_FRAME, (uaddr & PDPMASK) >> PDRSHIFT);
-                               if(err) {
-                                       kprintf("%s: could not get pdpe\n", __func__);
-                                       goto error;
-                               }
-                               if (pde & kernel_pmap.pmap_bits[PG_V_IDX]) {
-                                       if (pde & kernel_pmap.pmap_bits[PG_PS_IDX]) {
-                                               *gpa = (pde & PG_FRAME) | (uaddr & PDRMASK);
-                                               goto out;
-                                       } else {
-                                               err = get_pt_entry(vm, &pte, pde & PG_FRAME, (uaddr & PDRMASK) >> PAGE_SHIFT);
-                                               if (err) {
-                                                       kprintf("%s: could not get pte\n", __func__);
-                                                       goto error;
-                                               }
-                                               if (pte & kernel_pmap.pmap_bits[PG_V_IDX]) {
-                                                       *gpa = (pte & PG_FRAME) | (uaddr & PAGE_MASK);
-                                               } else {
-                                                       kprintf("%s: pte not valid\n", __func__);
-                                                       err = EFAULT;
-                                                       goto error;
-                                               }
-                                       }
-                               } else {
-                                       kprintf("%s: pde not valid\n", __func__);
-                                       err = EFAULT;
-                                       goto error;
-                               }
-                       }
-               } else {
-                       kprintf("%s: pdpe not valid\n", __func__);
-                       err = EFAULT;
-                       goto error;
-               }
-       } else {
+       if (!(pml4e & kernel_pmap.pmap_bits[PG_V_IDX])) {
                kprintf("%s: pml4e not valid\n", __func__);
                err = EFAULT;
                goto error;
        }
-out:
+
+       err = get_pt_entry(vm, &pdpe, pml4e & PG_FRAME,
+           (uaddr & PML4MASK) >> PDPSHIFT);
+       if (err) {
+               kprintf("%s: could not get pdpe\n", __func__);
+               goto error;
+       }
+       if (!(pdpe & kernel_pmap.pmap_bits[PG_V_IDX])) {
+               kprintf("%s: pdpe not valid\n", __func__);
+               err = EFAULT;
+               goto error;
+       }
+       if (pdpe & kernel_pmap.pmap_bits[PG_PS_IDX]) {
+               *gpa = (pdpe & PG_FRAME) | (uaddr & PDPMASK);
+               return 0;
+       }
+
+       err = get_pt_entry(vm, &pde, pdpe & PG_FRAME,
+           (uaddr & PDPMASK) >> PDRSHIFT);
+       if (err) {
+               kprintf("%s: could not get pdpe\n", __func__);
+               goto error;
+       }
+       if (!(pde & kernel_pmap.pmap_bits[PG_V_IDX])) {
+               kprintf("%s: pde not valid\n", __func__);
+               err = EFAULT;
+               goto error;
+       }
+       if (pde & kernel_pmap.pmap_bits[PG_PS_IDX]) {
+               *gpa = (pde & PG_FRAME) | (uaddr & PDRMASK);
+               return 0;
+       }
+
+       err = get_pt_entry(vm, &pte, pde & PG_FRAME,
+           (uaddr & PDRMASK) >> PAGE_SHIFT);
+       if (err) {
+               kprintf("%s: could not get pte\n", __func__);
+               goto error;
+       }
+       if (!(pte & kernel_pmap.pmap_bits[PG_V_IDX])) {
+               kprintf("%s: pte not valid\n", __func__);
+               err = EFAULT;
+               goto error;
+       }
+       *gpa = (pte & PG_FRAME) | (uaddr & PAGE_MASK);
+       return 0;
+
 error:
        return err;
 }