aboutsummaryrefslogtreecommitdiff
path: root/host/libs/vm_manager/vm_manager.cpp
blob: 7860d3cb2fba72a6218bfb41f16c436de2f03dc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "host/libs/vm_manager/vm_manager.h"

#include <iomanip>
#include <memory>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <android-base/logging.h>
#include <fruit/fruit.h>

#include "common/libs/utils/result.h"
#include "host/libs/config/command_source.h"
#include "host/libs/config/cuttlefish_config.h"
#include "host/libs/config/inject.h"
#include "host/libs/vm_manager/crosvm_manager.h"
#include "host/libs/vm_manager/gem5_manager.h"
#include "host/libs/vm_manager/qemu_manager.h"

namespace cuttlefish {
namespace vm_manager {

std::unique_ptr<VmManager> GetVmManager(VmmMode vmm_mode, Arch arch) {
  std::unique_ptr<VmManager> vmm;
  if (vmm_mode == VmmMode::kQemu) {
    vmm.reset(new QemuManager(arch));
  } else if (vmm_mode == VmmMode::kGem5) {
    vmm.reset(new Gem5Manager(arch));
  } else if (vmm_mode == VmmMode::kCrosvm) {
    vmm.reset(new CrosvmManager());
  }
  if (!vmm) {
    LOG(ERROR) << "Invalid VM manager: " << vmm_mode;
    return {};
  }
  if (!vmm->IsSupported()) {
    LOG(ERROR) << "VM manager " << vmm_mode
               << " is not supported on this machine.";
    return {};
  }
  return vmm;
}

Result<std::unordered_map<std::string, std::string>>
ConfigureMultipleBootDevices(const std::string& pci_path, int pci_offset,
                             int num_disks) {
  int num_boot_devices =
      (num_disks < VmManager::kDefaultNumBootDevices) ? num_disks : VmManager::kDefaultNumBootDevices;
  std::string boot_devices_prop_val = "";
  for (int i = 0; i < num_boot_devices; i++) {
    std::stringstream stream;
    stream << std::setfill('0') << std::setw(2) << std::hex
           << pci_offset + i + VmManager::kDefaultNumHvcs + VmManager::kMaxDisks - num_disks;
    boot_devices_prop_val += pci_path + stream.str() + ".0,";
  }
  boot_devices_prop_val.pop_back();
  return {{{"androidboot.boot_devices", boot_devices_prop_val}}};
}

class VmmCommands : public CommandSource, public LateInjected {
 public:
  INJECT(VmmCommands(const CuttlefishConfig& config, VmManager& vmm))
      : config_(config), vmm_(vmm) {}

  // CommandSource
  Result<std::vector<MonitorCommand>> Commands() override {
    return vmm_.StartCommands(config_, dependencyCommands_);
  }

  // SetupFeature
  std::string Name() const override { return "VirtualMachineManager"; }
  bool Enabled() const override { return true; }

  // LateInjected
  Result<void> LateInject(fruit::Injector<>& injector) override {
    dependencyCommands_ = injector.getMultibindings<VmmDependencyCommand>();

    return {};
  }

 private:
  std::unordered_set<SetupFeature*> Dependencies() const override { return {}; }
  Result<void> ResultSetup() override { return {}; }

  const CuttlefishConfig& config_;
  VmManager& vmm_;
  std::vector<VmmDependencyCommand*> dependencyCommands_;
};

fruit::Component<fruit::Required<const CuttlefishConfig,
                                 const CuttlefishConfig::InstanceSpecific>,
                 VmManager>
VmManagerComponent() {
  return fruit::createComponent()
      .registerProvider([](const CuttlefishConfig& config,
                           const CuttlefishConfig::InstanceSpecific& instance) {
        auto vmm = GetVmManager(config.vm_manager(), instance.target_arch());
        CHECK(vmm) << "Invalid VMM/Arch: \"" << config.vm_manager() << "\""
                   << (int)instance.target_arch() << "\"";
        return vmm.release();  // fruit takes ownership of raw pointers
      })
      .addMultibinding<CommandSource, VmmCommands>()
      .addMultibinding<LateInjected, VmmCommands>()
      .addMultibinding<SetupFeature, VmmCommands>();
}

} // namespace vm_manager
} // namespace cuttlefish