Skip to content

Commit 81cf3e1

Browse files
fyin1jren1
authored andcommitted
DM: main loop cleanup
Move all virtual devices init/deinit to function to simplify the failure path of main loop. In the future, new virtual device will not touch main loop. Signed-off-by: Yin Fengwei <fengwei.yin@intel.com> Acked-by: Anthony Xu <anthony.xu@intel.com> Acked-by: Eddie Dong <Eddie.dong@intel.com>
1 parent edc5846 commit 81cf3e1

File tree

1 file changed

+68
-34
lines changed

1 file changed

+68
-34
lines changed

devicemodel/core/main.c

Lines changed: 68 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,63 @@ handle_vmexit(struct vmctx *ctx, struct vhm_request *vhm_req, int vcpu)
459459
vm_notify_request_done(ctx, vcpu);
460460
}
461461

462+
static int
463+
vm_init_vdevs(struct vmctx *ctx)
464+
{
465+
int ret;
466+
467+
init_mem();
468+
init_inout();
469+
pci_irq_init(ctx);
470+
atkbdc_init(ctx);
471+
ioapic_init(ctx);
472+
473+
/*
474+
* We don't care ioc_init return value so far.
475+
* Will add return value check once ioc is full function.
476+
*/
477+
ret = ioc_init(ctx);
478+
479+
ret = vrtc_init(ctx);
480+
if (ret < 0)
481+
goto vrtc_fail;
482+
483+
sci_init(ctx);
484+
init_bvmcons();
485+
486+
ret = monitor_init(ctx);
487+
if (ret < 0)
488+
goto monitor_fail;
489+
490+
ret = init_pci(ctx);
491+
if (ret < 0)
492+
goto pci_fail;
493+
494+
return 0;
495+
pci_fail:
496+
monitor_close();
497+
monitor_fail:
498+
deinit_bvmcons();
499+
vrtc_deinit(ctx);
500+
vrtc_fail:
501+
ioc_deinit(ctx);
502+
atkbdc_deinit(ctx);
503+
pci_irq_deinit(ctx);
504+
return -1;
505+
}
506+
507+
static void
508+
vm_deinit_vdevs(struct vmctx *ctx)
509+
{
510+
deinit_pci(ctx);
511+
monitor_close();
512+
deinit_bvmcons();
513+
vrtc_deinit(ctx);
514+
ioc_deinit(ctx);
515+
atkbdc_deinit(ctx);
516+
pci_irq_deinit(ctx);
517+
}
518+
462519
static void
463520
vm_loop(struct vmctx *ctx)
464521
{
@@ -776,29 +833,14 @@ main(int argc, char *argv[])
776833
goto mevent_fail;
777834
}
778835

779-
init_mem();
780-
init_inout();
781-
pci_irq_init(ctx);
782-
atkbdc_init(ctx);
783-
ioapic_init(ctx);
784-
ioc_init(ctx);
785-
786-
vrtc_init(ctx);
787-
sci_init(ctx);
788-
init_bvmcons();
789-
monitor_init(ctx);
790-
791-
/*
792-
* Exit if a device emulation finds an error in its
793-
* initialization
794-
*/
795-
if (init_pci(ctx) != 0) {
796-
goto pci_fail;
797-
}
798-
799836
if (gdb_port != 0)
800837
fprintf(stderr, "dbgport not supported\n");
801838

839+
if (vm_init_vdevs(ctx) < 0) {
840+
fprintf(stderr, "Unable to init vdev (%d)\n", errno);
841+
goto dev_fail;
842+
}
843+
802844
/*
803845
* build the guest tables, MP etc.
804846
*/
@@ -847,28 +889,20 @@ main(int argc, char *argv[])
847889
if (vm_get_suspend_mode() != VM_SUSPEND_RESET)
848890
break;
849891

850-
pci_irq_deinit(ctx);
851-
deinit_pci(ctx);
852-
monitor_close();
853-
deinit_bvmcons();
854-
vrtc_deinit(ctx);
855-
atkbdc_deinit(ctx);
856-
vm_unsetup_memory(ctx);
892+
vm_deinit_vdevs(ctx);
857893
mevent_deinit();
894+
vm_unsetup_memory(ctx);
858895
vm_destroy(ctx);
859896
vm_close(ctx);
860897
_ctx = 0;
861898

862899
vm_set_suspend_mode(VM_SUSPEND_NONE);
863900
}
901+
864902
vm_fail:
865-
pci_irq_deinit(ctx);
866-
deinit_pci(ctx);
867-
pci_fail:
868-
monitor_close();
869-
deinit_bvmcons();
870-
vrtc_deinit(ctx);
871-
atkbdc_deinit(ctx);
903+
vm_deinit_vdevs(ctx);
904+
dev_fail:
905+
mevent_deinit();
872906
mevent_fail:
873907
vm_unsetup_memory(ctx);
874908
fail:

0 commit comments

Comments
 (0)