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 // Superclass of every guest VM manager. 33 class VmManager { 34 public: 35 // This is the number of HVC virtual console ports that should be configured 36 // by the VmManager. Because crosvm currently allocates these ports as the 37 // first PCI devices, and it does not control the allocation of PCI ID 38 // assignments, the number of these ports affects the PCI paths for 39 // subsequent PCI devices, and these paths are hard-coded in SEPolicy. 40 // Fortunately, HVC virtual console ports can be set up to be "sink" devices, 41 // so even if they are disabled and the guest isn't using them, they don't 42 // need to consume host resources, except for the PCI ID. Use this trick to 43 // keep the number of PCI IDs assigned constant for all flags/vm manager 44 // combinations. 45 // - /dev/hvc0 = kernel console 46 // - /dev/hvc1 = serial console 47 // - /dev/hvc2 = serial logging 48 // - /dev/hvc3 = keymaster 49 // - /dev/hvc4 = gatekeeper 50 // - /dev/hvc5 = bt 51 // - /dev/hvc6 = gnss 52 // - /dev/hvc7 = location 53 // - /dev/hvc8 = confirmationui 54 // - /dev/hvc9 = uwb 55 // - /dev/hvc10 = oemlock 56 // - /dev/hvc11 = keymint 57 static const int kDefaultNumHvcs = 12; 58 59 // This is the number of virtual disks (block devices) that should be 60 // configured by the VmManager. Related to the description above regarding 61 // HVC ports, this problem can also affect block devices (which are 62 // enumerated second) if not all of the block devices are available. Unlike 63 // HVC virtual console ports, block devices cannot be configured to be sinks, 64 // so we once again leverage HVC virtual console ports to "bump up" the last 65 // assigned virtual disk PCI ID (i.e. 2 disks = 7 hvcs, 1 disks = 8 hvcs) 66 static constexpr int kMaxDisks = 3; 67 68 // This is the number of virtual disks that contribute to the named partition 69 // list (/dev/block/by-name/*) under Android. The partitions names from 70 // multiple disks *must not* collide. Normally we have one set of partitions 71 // from the powerwashed disk (operating system disk) and another set from 72 // the persistent disk 73 static const int kDefaultNumBootDevices = 2; 74 75 virtual ~VmManager() = default; 76 77 virtual bool IsSupported() = 0; 78 79 virtual Result<std::unordered_map<std::string, std::string>> 80 ConfigureGraphics(const CuttlefishConfig::InstanceSpecific& instance) = 0; 81 82 virtual Result<std::unordered_map<std::string, std::string>> 83 ConfigureBootDevices(int num_disks, bool have_gpu) = 0; 84 85 // Starts the VMM. It will usually build a command and pass it to the 86 // command_starter function, although it may start more than one. The 87 // command_starter function allows to customize the way vmm commands are 88 // started/tracked/etc. 89 virtual Result<std::vector<MonitorCommand>> StartCommands( 90 const CuttlefishConfig& config) = 0; 91 }; 92 93 fruit::Component<fruit::Required<const CuttlefishConfig, 94 const CuttlefishConfig::InstanceSpecific>, 95 VmManager> 96 VmManagerComponent(); 97 98 std::unique_ptr<VmManager> GetVmManager(const std::string&, Arch arch); 99 100 Result<std::unordered_map<std::string, std::string>> 101 ConfigureMultipleBootDevices(const std::string& pci_path, int pci_offset, 102 int num_disks); 103 104 } // namespace vm_manager 105 } // namespace cuttlefish 106