|
| 1 | +.. _modularity: |
| 2 | + |
| 3 | +ACRN Hypervisor: Modular Design |
| 4 | +############################### |
| 5 | + |
| 6 | +Overview |
| 7 | +******** |
| 8 | + |
| 9 | +ACRN highly emphasizes modular design, i.e. the separation of functionality info |
| 10 | +modules that define a concise set of interfaces. The goals of modular design |
| 11 | +include: |
| 12 | + |
| 13 | +* **Understandability** A modular design is easier to understand due to |
| 14 | + encapsulation. |
| 15 | +* **Testability** Modules can be integrated and tested in the reverse order of |
| 16 | + dependencies among them. White-box integration tests help improve the coverage |
| 17 | + of tests and identify corner cases that are hard to trigger when testing the |
| 18 | + hypervisor as a whole. |
| 19 | +* **Configurability** Modular design makes it easy to configure certain |
| 20 | + functionalities in or out. This is crucial in safety-critical scenarios |
| 21 | + because absence of irrelevant code is required in both MISRA-C and functional |
| 22 | + safety standards. |
| 23 | +* **Meet functional safety requirements** Functional safety standards explicitly |
| 24 | + require a hierarchical structure of modules in software architectural |
| 25 | + design. This applies to any safety integrity level defined in IEC 61508 |
| 26 | + [IEC_61508-3]_ and ISO 26262 [ISO_26262-6]_. |
| 27 | + |
| 28 | +Principles |
| 29 | +********** |
| 30 | + |
| 31 | +* Each source file shall belong to one module only. One module may consist of |
| 32 | + one or multiple source files, though. A source file can be a C source, a C |
| 33 | + header or an assembly file. |
| 34 | +* Each module shall have well-defined interfaces, including the exported |
| 35 | + functions and global variables. Functions and variables that are not |
| 36 | + interfaces shall be static and used inside the module only. |
| 37 | +* Dependencies among the modules should be acyclic. Any cyclic dependency must |
| 38 | + be deviated explicitly. |
| 39 | +* The complexity of a module shall be limited. |
| 40 | + |
| 41 | +Minimizing Cyclic Dependencies |
| 42 | +============================== |
| 43 | + |
| 44 | +Cyclic dependencies can be mostly avoided by carefully defining the boundary of |
| 45 | +modules. The following methods can be used when certain cyclic dependency cannot |
| 46 | +be resolved by design. |
| 47 | + |
| 48 | +* **Use callbacks** Callback registration and invocation help reverse dependencies |
| 49 | + between two modules and break cyclic dependencies. However callbacks shall be |
| 50 | + used with care due to its dynamic behavior. Send proposals or patches to the |
| 51 | + `acrn-dev mailing list <https://lists.projectacrn.org/g/acrn-dev>`_ for |
| 52 | + discussing if specific callbacks are appropriate. |
| 53 | +* **Making the cyclic dependency an exception** A specific cyclic dependency can |
| 54 | + be regarded as an exception if it is well justified and a work around is |
| 55 | + available to break the cyclic dependency for integration testing. |
| 56 | + |
| 57 | +Measuring Complexity |
| 58 | +==================== |
| 59 | + |
| 60 | +ACRN uses the number of functions and the cyclomatic complexity [CC]_ of each |
| 61 | +function to measure the complexity of a module. Concrete criterias on complexity |
| 62 | +will be determined while enhancing the modularity of the hypervisor. The current |
| 63 | +recommendation is to limit the cyclomatic complexity of a function under 20. |
| 64 | + |
| 65 | +Architecture |
| 66 | +************ |
| 67 | + |
| 68 | +The following figure shows the high-level components of ACRN hypervisor. |
| 69 | + |
| 70 | +.. figure:: images/modularity-architecture.png |
| 71 | + :align: center |
| 72 | + :name: modularity-architecture |
| 73 | + |
| 74 | + Layered Architecture of ACRN Hypervisor |
| 75 | + |
| 76 | +The components are listed as follows. |
| 77 | + |
| 78 | +* **Boot** This component carries out the most basic hardware initialization to |
| 79 | + enable the execution of C code. |
| 80 | +* **Library** This component consists of subroutines that require no explicit |
| 81 | + initialization. Examples include standard memory and string manipulation |
| 82 | + functions like strncpy, atomic operations and bitmap operations. This |
| 83 | + component is independent from and widely used in the other components. |
| 84 | +* **Hardware Management and Utilities** This component abstract hardware |
| 85 | + resources and provide services like timers and physical interrupt handler |
| 86 | + registration to the upper layers. |
| 87 | +* **Virtual CPU** This component implements CPU, memory and interrupt |
| 88 | + virtualization. The vCPU loop module in this component handles VM exit events |
| 89 | + by calling the proper handler in the other components. Hypercalls are |
| 90 | + implemented as a special type of VM exit event. This component is also able to |
| 91 | + inject upcall interrupts to SOS. |
| 92 | +* **Device Emulation** This component implements devices that are emulated in |
| 93 | + the hypervisor itself, such as the virtual programmable interrupt controllers |
| 94 | + including vPIC, vLAPIC and vIOAPIC. |
| 95 | +* **Passthru Management** This component manages devices that are passed-through |
| 96 | + to specific VMs. |
| 97 | +* **Extended Device Emulation** This component implements an I/O request |
| 98 | + mechanism that allow the hypervisor to forward I/O accesses from UOSes to SOS |
| 99 | + for emulation. |
| 100 | +* **VM Management** This component manages the creation, deletion and other |
| 101 | + lifecycle operations of VMs. |
| 102 | +* **Hypervisor Initialization** This component invokes the initialization |
| 103 | + subroutines in the other components to bring up the hypervisor and start up |
| 104 | + SOS in sharing mode or all the VMs in partitioning mode. |
| 105 | + |
| 106 | +ACRN hypervisor adopts a layered design where higher layers can invoke the |
| 107 | +interfaces of lower layers but not vice versa. The only exception is the |
| 108 | +invocation of initialization routine in the **Boot** component, illustrated as |
| 109 | +the arrow from bottom to top on the left side of figure |
| 110 | +:numref:`modularity-architecture`. This exception is made due to the following |
| 111 | +reasons. |
| 112 | + |
| 113 | +* **Boot** enables the execution of C code and thus has to be the lowest layer |
| 114 | + in the architecture. |
| 115 | +* **Hypervisor Initialization** contains the hypervisor initialization function |
| 116 | + that calls the initialization functions of each layer. Thus this component is |
| 117 | + the highest layer to minimize reverse dependencies. |
| 118 | +* **Boot** shall invoke the hypervisor initialization routine after bringing up |
| 119 | + the hardware. This inevitably causes a reverse dependency from **Boot** to |
| 120 | + **Hypervisor Initialization**. |
| 121 | + |
| 122 | +To enable integration testing of a layer in the middle (e.g. **Virtual CPU**), |
| 123 | +**Boot** will invoke a customized function that only invokes the initialization |
| 124 | +functions of that layer as well as the layers below. |
| 125 | + |
| 126 | +References |
| 127 | +********** |
| 128 | + |
| 129 | +.. [IEC_61508-3] IEC 61508-3:2010, Functional safety of electrical/electronic/programmable electronic safety-related systems – Part 3: Software requirements |
| 130 | +
|
| 131 | +.. [ISO_26262-6] ISO 26262-6:2011, Road vehicles - Functional safety - Part 6: Product development at the software level |
| 132 | +
|
| 133 | +.. [CC] Cyclomatic complexity - Wikipedia, https://en.wikipedia.org/wiki/Cyclomatic_complexity |
0 commit comments