1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #pragma once 17 18 #include <memory> 19 #include <string> 20 #include <unordered_map> 21 #include <vector> 22 23 #include <fruit/fruit.h> 24 25 #include "common/libs/utils/result.h" 26 #include "host/libs/config/command_source.h" 27 #include "host/libs/config/cuttlefish_config.h" 28 29 namespace cuttlefish { 30 namespace vm_manager { 31 32 // Class for tagging that the CommandSource is a dependency command for the 33 // VmManager. 34 class VmmDependencyCommand : public virtual StatusCheckCommandSource {}; 35 36 // Superclass of every guest VM manager. 37 class VmManager { 38 public: 39 // This is the number of HVC virtual console ports that should be configured 40 // by the VmManager. Because crosvm currently allocates these ports as the 41 // first PCI devices, and it does not control the allocation of PCI ID 42 // assignments, the number of these ports affects the PCI paths for 43 // subsequent PCI devices, and these paths are hard-coded in SEPolicy. 44 // Fortunately, HVC virtual console ports can be set up to be "sink" devices, 45 // so even if they are disabled and the guest isn't using them, they don't 46 // need to consume host resources, except for the PCI ID. Use this trick to 47 // keep the number of PCI IDs assigned constant for all flags/vm manager 48 // combinations. 49 // - /dev/hvc0 = kernel console 50 // - /dev/hvc1 = serial console 51 // - /dev/hvc2 = serial logging 52 // - /dev/hvc3 = keymaster 53 // - /dev/hvc4 = gatekeeper 54 // - /dev/hvc5 = bt 55 // - /dev/hvc6 = gnss 56 // - /dev/hvc7 = location 57 // - /dev/hvc8 = confirmationui 58 // - /dev/hvc9 = uwb 59 // - /dev/hvc10 = oemlock 60 // - /dev/hvc11 = keymint 61 // - /dev/hvc12 = NFC 62 // - /dev/hvc13 = sensors 63 // - /dev/hvc14 = MCU control 64 // - /dev/hvc15 = MCU UART 65 static const int kDefaultNumHvcs = 16; 66 67 // This is the number of virtual disks (block devices) that should be 68 // configured by the VmManager. Related to the description above regarding 69 // HVC ports, this problem can also affect block devices (which are 70 // enumerated second) if not all of the block devices are available. Unlike 71 // HVC virtual console ports, block devices cannot be configured to be sinks, 72 // so we once again leverage HVC virtual console ports to "bump up" the last 73 // assigned virtual disk PCI ID (i.e. 2 disks = 7 hvcs, 1 disks = 8 hvcs) 74 static constexpr int kMaxDisks = 3; 75 76 // This is the number of virtual disks that contribute to the named partition 77 // list (/dev/block/by-name/*) under Android. The partitions names from 78 // multiple disks *must not* collide. Normally we have one set of partitions 79 // from the powerwashed disk (operating system disk) and another set from 80 // the persistent disk 81 static const int kDefaultNumBootDevices = 2; 82 83 static constexpr const int kNetPciDeviceNum = 1; 84 85 // LINT.IfChange(virtio_gpu_pci_address) 86 static constexpr const int kGpuPciSlotNum = 2; 87 // LINT.ThenChange(../../../shared/sepolicy/vendor/genfs_contexts:virtio_gpu_pci_address) 88 89 virtual ~VmManager() = default; 90 91 virtual bool IsSupported() = 0; 92 93 virtual Result<std::unordered_map<std::string, std::string>> 94 ConfigureGraphics(const CuttlefishConfig::InstanceSpecific& instance) = 0; 95 96 virtual Result<std::unordered_map<std::string, std::string>> 97 ConfigureBootDevices(const CuttlefishConfig::InstanceSpecific& instance) = 0; 98 99 // Starts the VMM. It will usually build a command and pass it to the 100 // command_starter function, although it may start more than one. The 101 // command_starter function allows to customize the way vmm commands are 102 // started/tracked/etc. 103 virtual Result<std::vector<MonitorCommand>> StartCommands( 104 const CuttlefishConfig& config, 105 std::vector<VmmDependencyCommand*>& dependencyCommands) = 0; 106 107 // Block until the restore work is finished and the guest is running. Only 108 // called if a snapshot is being restored. 109 // 110 // If FD becomes readable or closed, gives up and returns false. 111 // 112 // Must be thread safe. WaitForRestoreComplete(SharedFD)113 virtual Result<bool> WaitForRestoreComplete(SharedFD) const { 114 return CF_ERR("not implemented"); 115 } 116 }; 117 118 fruit::Component<fruit::Required<const CuttlefishConfig, 119 const CuttlefishConfig::InstanceSpecific>, 120 VmManager> 121 VmManagerComponent(); 122 123 std::unique_ptr<VmManager> GetVmManager(VmmMode vmm, Arch arch); 124 125 Result<std::unordered_map<std::string, std::string>> 126 ConfigureMultipleBootDevices(const std::string& pci_path, int pci_offset, 127 int num_disks); 128 129 } // namespace vm_manager 130 } // namespace cuttlefish 131