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