• 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 
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/vm_manager/crosvm_manager.h"
33 #include "host/libs/vm_manager/gem5_manager.h"
34 #include "host/libs/vm_manager/qemu_manager.h"
35 
36 namespace cuttlefish {
37 namespace vm_manager {
38 
GetVmManager(const std::string & name,Arch arch)39 std::unique_ptr<VmManager> GetVmManager(const std::string& name, Arch arch) {
40   std::unique_ptr<VmManager> vmm;
41   if (name == QemuManager::name()) {
42     vmm.reset(new QemuManager(arch));
43   } else if (name == Gem5Manager::name()) {
44     vmm.reset(new Gem5Manager(arch));
45   } else if (name == CrosvmManager::name()) {
46     vmm.reset(new CrosvmManager());
47   }
48   if (!vmm) {
49     LOG(ERROR) << "Invalid VM manager: " << name;
50     return {};
51   }
52   if (!vmm->IsSupported()) {
53     LOG(ERROR) << "VM manager " << name << " is not supported on this machine.";
54     return {};
55   }
56   return vmm;
57 }
58 
59 Result<std::unordered_map<std::string, std::string>>
ConfigureMultipleBootDevices(const std::string & pci_path,int pci_offset,int num_disks)60 ConfigureMultipleBootDevices(const std::string& pci_path, int pci_offset,
61                              int num_disks) {
62   int num_boot_devices =
63       (num_disks < VmManager::kDefaultNumBootDevices) ? num_disks : VmManager::kDefaultNumBootDevices;
64   std::string boot_devices_prop_val = "";
65   for (int i = 0; i < num_boot_devices; i++) {
66     std::stringstream stream;
67     stream << std::setfill('0') << std::setw(2) << std::hex
68            << pci_offset + i + VmManager::kDefaultNumHvcs + VmManager::kMaxDisks - num_disks;
69     boot_devices_prop_val += pci_path + stream.str() + ".0,";
70   }
71   boot_devices_prop_val.pop_back();
72   return {{{"androidboot.boot_devices", boot_devices_prop_val}}};
73 }
74 
75 class VmmCommands : public CommandSource {
76  public:
INJECT(VmmCommands (const CuttlefishConfig & config,VmManager & vmm))77   INJECT(VmmCommands(const CuttlefishConfig& config, VmManager& vmm))
78       : config_(config), vmm_(vmm) {}
79 
80   // CommandSource
Commands()81   Result<std::vector<MonitorCommand>> Commands() override {
82     return vmm_.StartCommands(config_);
83   }
84 
85   // SetupFeature
Name() const86   std::string Name() const override { return "VirtualMachineManager"; }
Enabled() const87   bool Enabled() const override { return true; }
88 
89  private:
Dependencies() const90   std::unordered_set<SetupFeature*> Dependencies() const override { return {}; }
Setup()91   bool Setup() override { return true; }
92 
93   const CuttlefishConfig& config_;
94   VmManager& vmm_;
95 };
96 
97 fruit::Component<fruit::Required<const CuttlefishConfig,
98                                  const CuttlefishConfig::InstanceSpecific>,
99                  VmManager>
VmManagerComponent()100 VmManagerComponent() {
101   return fruit::createComponent()
102       .registerProvider([](const CuttlefishConfig& config,
103                            const CuttlefishConfig::InstanceSpecific& instance) {
104         auto vmm = GetVmManager(config.vm_manager(), instance.target_arch());
105         CHECK(vmm) << "Invalid VMM/Arch: \"" << config.vm_manager() << "\""
106                    << (int)instance.target_arch() << "\"";
107         return vmm.release();  // fruit takes ownership of raw pointers
108       })
109       .addMultibinding<CommandSource, VmmCommands>()
110       .addMultibinding<SetupFeature, VmmCommands>();
111 }
112 
113 } // namespace vm_manager
114 } // namespace cuttlefish
115