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
17 #include "host/libs/vm_manager/vm_manager.h"
18
19 #include <iomanip>
20 #include <memory>
21 #include <string>
22 #include <unordered_map>
23 #include <unordered_set>
24 #include <vector>
25
26 #include <android-base/logging.h>
27 #include <fruit/fruit.h>
28
29 #include "common/libs/utils/result.h"
30 #include "host/libs/config/command_source.h"
31 #include "host/libs/config/cuttlefish_config.h"
32 #include "host/libs/config/inject.h"
33 #include "host/libs/vm_manager/crosvm_manager.h"
34 #include "host/libs/vm_manager/gem5_manager.h"
35 #include "host/libs/vm_manager/qemu_manager.h"
36
37 namespace cuttlefish {
38 namespace vm_manager {
39
GetVmManager(VmmMode vmm_mode,Arch arch)40 std::unique_ptr<VmManager> GetVmManager(VmmMode vmm_mode, Arch arch) {
41 std::unique_ptr<VmManager> vmm;
42 if (vmm_mode == VmmMode::kQemu) {
43 vmm.reset(new QemuManager(arch));
44 } else if (vmm_mode == VmmMode::kGem5) {
45 vmm.reset(new Gem5Manager(arch));
46 } else if (vmm_mode == VmmMode::kCrosvm) {
47 vmm.reset(new CrosvmManager());
48 }
49 if (!vmm) {
50 LOG(ERROR) << "Invalid VM manager: " << vmm_mode;
51 return {};
52 }
53 if (!vmm->IsSupported()) {
54 LOG(ERROR) << "VM manager " << vmm_mode
55 << " is not supported on this machine.";
56 return {};
57 }
58 return vmm;
59 }
60
61 Result<std::unordered_map<std::string, std::string>>
ConfigureMultipleBootDevices(const std::string & pci_path,int pci_offset,int num_disks)62 ConfigureMultipleBootDevices(const std::string& pci_path, int pci_offset,
63 int num_disks) {
64 int num_boot_devices =
65 (num_disks < VmManager::kDefaultNumBootDevices) ? num_disks : VmManager::kDefaultNumBootDevices;
66 std::string boot_devices_prop_val = "";
67 for (int i = 0; i < num_boot_devices; i++) {
68 std::stringstream stream;
69 stream << std::setfill('0') << std::setw(2) << std::hex
70 << pci_offset + i + VmManager::kDefaultNumHvcs + VmManager::kMaxDisks - num_disks;
71 boot_devices_prop_val += pci_path + stream.str() + ".0,";
72 }
73 boot_devices_prop_val.pop_back();
74 return {{{"androidboot.boot_devices", boot_devices_prop_val}}};
75 }
76
77 class VmmCommands : public CommandSource, public LateInjected {
78 public:
INJECT(VmmCommands (const CuttlefishConfig & config,VmManager & vmm))79 INJECT(VmmCommands(const CuttlefishConfig& config, VmManager& vmm))
80 : config_(config), vmm_(vmm) {}
81
82 // CommandSource
Commands()83 Result<std::vector<MonitorCommand>> Commands() override {
84 return vmm_.StartCommands(config_, dependencyCommands_);
85 }
86
87 // SetupFeature
Name() const88 std::string Name() const override { return "VirtualMachineManager"; }
Enabled() const89 bool Enabled() const override { return true; }
90
91 // LateInjected
LateInject(fruit::Injector<> & injector)92 Result<void> LateInject(fruit::Injector<>& injector) override {
93 dependencyCommands_ = injector.getMultibindings<VmmDependencyCommand>();
94
95 return {};
96 }
97
98 private:
Dependencies() const99 std::unordered_set<SetupFeature*> Dependencies() const override { return {}; }
ResultSetup()100 Result<void> ResultSetup() override { return {}; }
101
102 const CuttlefishConfig& config_;
103 VmManager& vmm_;
104 std::vector<VmmDependencyCommand*> dependencyCommands_;
105 };
106
107 fruit::Component<fruit::Required<const CuttlefishConfig,
108 const CuttlefishConfig::InstanceSpecific>,
109 VmManager>
VmManagerComponent()110 VmManagerComponent() {
111 return fruit::createComponent()
112 .registerProvider([](const CuttlefishConfig& config,
113 const CuttlefishConfig::InstanceSpecific& instance) {
114 auto vmm = GetVmManager(config.vm_manager(), instance.target_arch());
115 CHECK(vmm) << "Invalid VMM/Arch: \"" << config.vm_manager() << "\""
116 << (int)instance.target_arch() << "\"";
117 return vmm.release(); // fruit takes ownership of raw pointers
118 })
119 .addMultibinding<CommandSource, VmmCommands>()
120 .addMultibinding<LateInjected, VmmCommands>()
121 .addMultibinding<SetupFeature, VmmCommands>();
122 }
123
124 } // namespace vm_manager
125 } // namespace cuttlefish
126