David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Hafnium Authors. |
| 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 | * https://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 | |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 17 | #include <array> |
| 18 | #include <cstdio> |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 19 | #include <span> |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 20 | #include <sstream> |
| 21 | |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 22 | #include <gmock/gmock.h> |
| 23 | |
| 24 | extern "C" { |
| 25 | #include "hf/manifest.h" |
| 26 | } |
| 27 | |
| 28 | namespace |
| 29 | { |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 30 | using ::testing::ElementsAre; |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 31 | using ::testing::Eq; |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 32 | using ::testing::IsEmpty; |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 33 | using ::testing::NotNull; |
| 34 | |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 35 | template <typename T> |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 36 | void exec(const char *program, const char *args[], const T &stdin, |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 37 | std::vector<char> *stdout) |
| 38 | { |
| 39 | /* Create two pipes, one for stdin and one for stdout. */ |
| 40 | int pipes[2][2]; |
| 41 | pipe(pipes[0]); |
| 42 | pipe(pipes[1]); |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 43 | |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 44 | /* Assign FDs for reading/writing by the parent/child. */ |
| 45 | int parent_read_fd = pipes[1][0]; /* stdout pipe, read FD */ |
| 46 | int parent_write_fd = pipes[0][1]; /* stdin pipe, write FD */ |
| 47 | int child_read_fd = pipes[0][0]; /* stdin pipe, read FD */ |
| 48 | int child_write_fd = pipes[1][1]; /* stdout pipe, write FD */ |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 49 | |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 50 | if (fork()) { |
| 51 | /* Parent process. */ |
| 52 | std::array<char, 128> buf; |
| 53 | ssize_t res; |
| 54 | |
| 55 | /* Close child FDs which won't be used. */ |
| 56 | close(child_read_fd); |
| 57 | close(child_write_fd); |
| 58 | |
| 59 | /* Write to stdin. */ |
| 60 | for (size_t count = 0; count < stdin.size();) { |
| 61 | res = write(parent_write_fd, stdin.data() + count, |
| 62 | stdin.size() - count); |
| 63 | if (res < 0) { |
| 64 | std::cerr << "IO error" << std::endl; |
| 65 | exit(1); |
| 66 | } |
| 67 | count += res; |
| 68 | } |
| 69 | close(parent_write_fd); |
| 70 | |
| 71 | /* Read from stdout. */ |
| 72 | while (true) { |
| 73 | res = read(parent_read_fd, buf.data(), buf.size()); |
| 74 | if (res == 0) { |
| 75 | /* EOF */ |
| 76 | break; |
| 77 | } else if (res < 0) { |
| 78 | std::cerr << "IO error" << std::endl; |
| 79 | exit(1); |
| 80 | } |
| 81 | stdout->insert(stdout->end(), buf.begin(), |
| 82 | buf.begin() + res); |
| 83 | } |
| 84 | close(parent_read_fd); |
| 85 | } else { |
| 86 | /* Child process. */ |
| 87 | |
| 88 | /* Redirect stdin/stdout to read/write FDs. */ |
| 89 | dup2(child_read_fd, STDIN_FILENO); |
| 90 | dup2(child_write_fd, STDOUT_FILENO); |
| 91 | |
| 92 | /* Close all FDs which are now unused. */ |
| 93 | close(child_read_fd); |
| 94 | close(child_write_fd); |
| 95 | close(parent_read_fd); |
| 96 | close(parent_write_fd); |
| 97 | |
| 98 | /* Execute the given program. */ |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 99 | execv(program, const_cast<char *const *>(args)); |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Class for programatically building a Device Tree. |
| 105 | * |
| 106 | * Usage: |
| 107 | * std::vector<char> dtb = ManifestDtBuilder() |
| 108 | * .Command1() |
| 109 | * .Command2() |
| 110 | * ... |
| 111 | * .CommandN() |
| 112 | * .Build(); |
| 113 | */ |
| 114 | class ManifestDtBuilder |
| 115 | { |
| 116 | public: |
| 117 | ManifestDtBuilder() |
| 118 | { |
| 119 | dts_ << "/dts-v1/;" << std::endl; |
| 120 | dts_ << std::endl; |
| 121 | |
| 122 | /* Start root node. */ |
| 123 | StartChild("/"); |
| 124 | } |
| 125 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 126 | std::vector<char> Build(bool dump = false) |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 127 | { |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 128 | const char *program = "./build/image/dtc.py"; |
| 129 | const char *dtc_args[] = {program, "compile", NULL}; |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 130 | std::vector<char> dtc_stdout; |
| 131 | |
| 132 | /* Finish root node. */ |
| 133 | EndChild(); |
| 134 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 135 | if (dump) { |
| 136 | Dump(); |
| 137 | } |
| 138 | |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 139 | exec(program, dtc_args, dts_.str(), &dtc_stdout); |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 140 | return dtc_stdout; |
| 141 | } |
| 142 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 143 | void Dump() |
| 144 | { |
| 145 | std::cerr << dts_.str() << std::endl; |
| 146 | } |
| 147 | |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 148 | ManifestDtBuilder &StartChild(const std::string_view &name) |
| 149 | { |
| 150 | dts_ << name << " {" << std::endl; |
| 151 | return *this; |
| 152 | } |
| 153 | |
| 154 | ManifestDtBuilder &EndChild() |
| 155 | { |
| 156 | dts_ << "};" << std::endl; |
| 157 | return *this; |
| 158 | } |
| 159 | |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 160 | ManifestDtBuilder &Compatible(const std::vector<std::string_view> |
| 161 | &value = {"hafnium,hafnium"}) |
| 162 | { |
| 163 | return StringListProperty("compatible", value); |
| 164 | } |
| 165 | |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 166 | ManifestDtBuilder &DebugName(const std::string_view &value) |
| 167 | { |
| 168 | return StringProperty("debug_name", value); |
| 169 | } |
| 170 | |
| 171 | ManifestDtBuilder &KernelFilename(const std::string_view &value) |
| 172 | { |
| 173 | return StringProperty("kernel_filename", value); |
| 174 | } |
| 175 | |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 176 | ManifestDtBuilder &RamdiskFilename(const std::string_view &value) |
| 177 | { |
| 178 | return StringProperty("ramdisk_filename", value); |
| 179 | } |
| 180 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 181 | ManifestDtBuilder &VcpuCount(uint32_t value) |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 182 | { |
| 183 | return IntegerProperty("vcpu_count", value); |
| 184 | } |
| 185 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 186 | ManifestDtBuilder &MemSize(uint32_t value) |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 187 | { |
| 188 | return IntegerProperty("mem_size", value); |
| 189 | } |
| 190 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 191 | ManifestDtBuilder &SmcWhitelist(const std::vector<uint32_t> &value) |
| 192 | { |
| 193 | return IntegerListProperty("smc_whitelist", value); |
| 194 | } |
| 195 | |
| 196 | ManifestDtBuilder &SmcWhitelistPermissive() |
| 197 | { |
| 198 | return BooleanProperty("smc_whitelist_permissive"); |
| 199 | } |
| 200 | |
| 201 | ManifestDtBuilder &Property(const std::string_view &name, |
| 202 | const std::string_view &value) |
| 203 | { |
| 204 | dts_ << name << " = " << value << ";" << std::endl; |
| 205 | return *this; |
| 206 | } |
| 207 | |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 208 | private: |
| 209 | ManifestDtBuilder &StringProperty(const std::string_view &name, |
| 210 | const std::string_view &value) |
| 211 | { |
| 212 | dts_ << name << " = \"" << value << "\";" << std::endl; |
| 213 | return *this; |
| 214 | } |
| 215 | |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 216 | ManifestDtBuilder &StringListProperty( |
| 217 | const std::string_view &name, |
| 218 | const std::vector<std::string_view> &value) |
| 219 | { |
| 220 | bool is_first = true; |
| 221 | |
| 222 | dts_ << name << " = "; |
| 223 | for (const std::string_view &entry : value) { |
| 224 | if (is_first) { |
| 225 | is_first = false; |
| 226 | } else { |
| 227 | dts_ << ", "; |
| 228 | } |
| 229 | dts_ << "\"" << entry << "\""; |
| 230 | } |
| 231 | dts_ << ";" << std::endl; |
| 232 | return *this; |
| 233 | } |
| 234 | |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 235 | ManifestDtBuilder &IntegerProperty(const std::string_view &name, |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 236 | uint32_t value) |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 237 | { |
| 238 | dts_ << name << " = <" << value << ">;" << std::endl; |
| 239 | return *this; |
| 240 | } |
| 241 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 242 | ManifestDtBuilder &IntegerListProperty( |
| 243 | const std::string_view &name, |
| 244 | const std::vector<uint32_t> &value) |
| 245 | { |
| 246 | dts_ << name << " = < "; |
| 247 | for (const uint32_t entry : value) { |
| 248 | dts_ << entry << " "; |
| 249 | } |
| 250 | dts_ << ">;" << std::endl; |
| 251 | return *this; |
| 252 | } |
| 253 | |
| 254 | ManifestDtBuilder &BooleanProperty(const std::string_view &name) |
| 255 | { |
Andrew Scull | 5dc089e | 2019-11-04 13:21:03 +0000 | [diff] [blame] | 256 | dts_ << name << ";" << std::endl; |
| 257 | return *this; |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 258 | } |
| 259 | |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 260 | std::stringstream dts_; |
| 261 | }; |
| 262 | |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 263 | static bool get_fdt_root(const std::vector<char> &dtb, |
| 264 | struct fdt_node *fdt_root) |
| 265 | { |
| 266 | const struct fdt_header *fdt_header; |
| 267 | |
| 268 | fdt_header = reinterpret_cast<const struct fdt_header *>(dtb.data()); |
| 269 | return fdt_root_node(fdt_root, fdt_header) && |
| 270 | fdt_find_child(fdt_root, ""); |
| 271 | } |
| 272 | |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 273 | TEST(manifest, no_hypervisor_node) |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 274 | { |
| 275 | struct manifest m; |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 276 | struct fdt_node fdt_root; |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 277 | std::vector<char> dtb = ManifestDtBuilder().Build(); |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 278 | |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 279 | ASSERT_TRUE(get_fdt_root(dtb, &fdt_root)); |
| 280 | ASSERT_EQ(manifest_init(&m, &fdt_root), |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 281 | MANIFEST_ERROR_NO_HYPERVISOR_FDT_NODE); |
| 282 | } |
| 283 | |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 284 | TEST(manifest, no_compatible_property) |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 285 | { |
| 286 | struct manifest m; |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 287 | struct fdt_node fdt_root; |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 288 | |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 289 | /* clang-format off */ |
| 290 | std::vector<char> dtb = ManifestDtBuilder() |
| 291 | .StartChild("hypervisor") |
| 292 | .EndChild() |
| 293 | .Build(); |
| 294 | /* clang-format on */ |
| 295 | |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 296 | ASSERT_TRUE(get_fdt_root(dtb, &fdt_root)); |
| 297 | ASSERT_EQ(manifest_init(&m, &fdt_root), |
| 298 | MANIFEST_ERROR_PROPERTY_NOT_FOUND); |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 299 | } |
| 300 | |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 301 | TEST(manifest, not_compatible) |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 302 | { |
| 303 | struct manifest m; |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 304 | struct fdt_node fdt_root; |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 305 | |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 306 | /* clang-format off */ |
| 307 | std::vector<char> dtb = ManifestDtBuilder() |
| 308 | .StartChild("hypervisor") |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 309 | .Compatible({ "foo,bar" }) |
| 310 | .EndChild() |
| 311 | .Build(); |
| 312 | /* clang-format on */ |
| 313 | |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 314 | ASSERT_TRUE(get_fdt_root(dtb, &fdt_root)); |
| 315 | ASSERT_EQ(manifest_init(&m, &fdt_root), MANIFEST_ERROR_NOT_COMPATIBLE); |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | TEST(manifest, compatible_one_of_many) |
| 319 | { |
| 320 | struct manifest m; |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 321 | struct fdt_node fdt_root; |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 322 | |
| 323 | /* clang-format off */ |
| 324 | std::vector<char> dtb = ManifestDtBuilder() |
| 325 | .StartChild("hypervisor") |
| 326 | .Compatible({ "foo,bar", "hafnium,hafnium" }) |
| 327 | .StartChild("vm1") |
| 328 | .DebugName("primary") |
| 329 | .EndChild() |
| 330 | .EndChild() |
| 331 | .Build(); |
| 332 | /* clang-format on */ |
| 333 | |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 334 | ASSERT_TRUE(get_fdt_root(dtb, &fdt_root)); |
| 335 | ASSERT_EQ(manifest_init(&m, &fdt_root), MANIFEST_SUCCESS); |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | TEST(manifest, no_vm_nodes) |
| 339 | { |
| 340 | struct manifest m; |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 341 | struct fdt_node fdt_root; |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 342 | |
| 343 | /* clang-format off */ |
| 344 | std::vector<char> dtb = ManifestDtBuilder() |
| 345 | .StartChild("hypervisor") |
| 346 | .Compatible() |
| 347 | .EndChild() |
| 348 | .Build(); |
| 349 | /* clang-format on */ |
| 350 | |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 351 | ASSERT_TRUE(get_fdt_root(dtb, &fdt_root)); |
| 352 | ASSERT_EQ(manifest_init(&m, &fdt_root), MANIFEST_ERROR_NO_PRIMARY_VM); |
| 353 | } |
| 354 | |
| 355 | static std::vector<char> gen_long_string_dtb(bool valid) |
| 356 | { |
| 357 | const char last_valid[] = "1234567890123456789012345678901"; |
| 358 | const char first_invalid[] = "12345678901234567890123456789012"; |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 359 | static_assert(sizeof(last_valid) == STRING_MAX_SIZE); |
| 360 | static_assert(sizeof(first_invalid) == STRING_MAX_SIZE + 1); |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 361 | |
| 362 | /* clang-format off */ |
| 363 | return ManifestDtBuilder() |
| 364 | .StartChild("hypervisor") |
| 365 | .Compatible() |
| 366 | .StartChild("vm1") |
| 367 | .DebugName(valid ? last_valid : first_invalid) |
| 368 | .EndChild() |
| 369 | .EndChild() |
| 370 | .Build(); |
| 371 | /* clang-format on */ |
| 372 | } |
| 373 | |
| 374 | TEST(manifest, long_string) |
| 375 | { |
| 376 | struct manifest m; |
| 377 | struct fdt_node fdt_root; |
| 378 | |
| 379 | std::vector<char> dtb_last_valid = gen_long_string_dtb(true); |
| 380 | std::vector<char> dtb_first_invalid = gen_long_string_dtb(false); |
| 381 | |
| 382 | ASSERT_TRUE(get_fdt_root(dtb_last_valid, &fdt_root)); |
| 383 | ASSERT_EQ(manifest_init(&m, &fdt_root), MANIFEST_SUCCESS); |
| 384 | |
| 385 | ASSERT_TRUE(get_fdt_root(dtb_first_invalid, &fdt_root)); |
| 386 | ASSERT_EQ(manifest_init(&m, &fdt_root), MANIFEST_ERROR_STRING_TOO_LONG); |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | TEST(manifest, reserved_vm_id) |
| 390 | { |
| 391 | struct manifest m; |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 392 | struct fdt_node fdt_root; |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 393 | |
| 394 | /* clang-format off */ |
| 395 | std::vector<char> dtb = ManifestDtBuilder() |
| 396 | .StartChild("hypervisor") |
| 397 | .Compatible() |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 398 | .StartChild("vm1") |
| 399 | .DebugName("primary_vm") |
| 400 | .EndChild() |
| 401 | .StartChild("vm0") |
| 402 | .DebugName("reserved_vm") |
| 403 | .VcpuCount(1) |
| 404 | .MemSize(0x1000) |
| 405 | .KernelFilename("kernel") |
| 406 | .EndChild() |
| 407 | .EndChild() |
| 408 | .Build(); |
| 409 | /* clang-format on */ |
| 410 | |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 411 | ASSERT_TRUE(get_fdt_root(dtb, &fdt_root)); |
| 412 | ASSERT_EQ(manifest_init(&m, &fdt_root), MANIFEST_ERROR_RESERVED_VM_ID); |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 413 | } |
| 414 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 415 | static std::vector<char> gen_vcpu_count_limit_dtb(uint32_t vcpu_count) |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 416 | { |
| 417 | /* clang-format off */ |
| 418 | return ManifestDtBuilder() |
| 419 | .StartChild("hypervisor") |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 420 | .Compatible() |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 421 | .StartChild("vm1") |
| 422 | .DebugName("primary_vm") |
| 423 | .EndChild() |
| 424 | .StartChild("vm2") |
| 425 | .DebugName("secondary_vm") |
| 426 | .VcpuCount(vcpu_count) |
| 427 | .MemSize(0x1000) |
| 428 | .KernelFilename("kernel") |
| 429 | .EndChild() |
| 430 | .EndChild() |
| 431 | .Build(); |
| 432 | /* clang-format on */ |
| 433 | } |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 434 | |
| 435 | TEST(manifest, vcpu_count_limit) |
| 436 | { |
| 437 | struct manifest m; |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 438 | struct fdt_node fdt_root; |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 439 | std::vector<char> dtb_last_valid = gen_vcpu_count_limit_dtb(UINT16_MAX); |
| 440 | std::vector<char> dtb_first_invalid = |
| 441 | gen_vcpu_count_limit_dtb(UINT16_MAX + 1); |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 442 | |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 443 | ASSERT_TRUE(get_fdt_root(dtb_last_valid, &fdt_root)); |
| 444 | ASSERT_EQ(manifest_init(&m, &fdt_root), MANIFEST_SUCCESS); |
David Brazdil | 0251b94 | 2019-09-10 15:59:50 +0100 | [diff] [blame] | 445 | ASSERT_EQ(m.vm_count, 2); |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 446 | ASSERT_EQ(m.vm[1].secondary.vcpu_count, UINT16_MAX); |
| 447 | |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 448 | ASSERT_TRUE(get_fdt_root(dtb_first_invalid, &fdt_root)); |
| 449 | ASSERT_EQ(manifest_init(&m, &fdt_root), |
| 450 | MANIFEST_ERROR_INTEGER_OVERFLOW); |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 451 | } |
| 452 | |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 453 | TEST(manifest, no_ramdisk_primary) |
| 454 | { |
| 455 | struct manifest m; |
| 456 | struct fdt_node fdt_root; |
| 457 | |
| 458 | /* clang-format off */ |
| 459 | std::vector<char> dtb = ManifestDtBuilder() |
| 460 | .StartChild("hypervisor") |
| 461 | .Compatible() |
| 462 | .StartChild("vm1") |
| 463 | .DebugName("primary_vm") |
| 464 | .EndChild() |
| 465 | .EndChild() |
| 466 | .Build(); |
| 467 | /* clang-format on */ |
| 468 | |
| 469 | ASSERT_TRUE(get_fdt_root(dtb, &fdt_root)); |
| 470 | ASSERT_EQ(manifest_init(&m, &fdt_root), MANIFEST_SUCCESS); |
| 471 | ASSERT_EQ(m.vm_count, 1); |
| 472 | ASSERT_STREQ(string_data(&m.vm[0].debug_name), "primary_vm"); |
| 473 | ASSERT_STREQ(string_data(&m.vm[0].primary.ramdisk_filename), ""); |
| 474 | } |
| 475 | |
Andrew Scull | b2c3a24 | 2019-11-04 13:52:36 +0000 | [diff] [blame^] | 476 | static std::vector<char> gen_malformed_boolean_dtb( |
| 477 | const std::string_view &value) |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 478 | { |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 479 | /* clang-format off */ |
Andrew Scull | b2c3a24 | 2019-11-04 13:52:36 +0000 | [diff] [blame^] | 480 | return ManifestDtBuilder() |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 481 | .StartChild("hypervisor") |
| 482 | .Compatible() |
| 483 | .StartChild("vm1") |
| 484 | .DebugName("primary_vm") |
Andrew Scull | b2c3a24 | 2019-11-04 13:52:36 +0000 | [diff] [blame^] | 485 | .Property("smc_whitelist_permissive", value) |
Andrew Scull | 5dc089e | 2019-11-04 13:21:03 +0000 | [diff] [blame] | 486 | .EndChild() |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 487 | .EndChild() |
| 488 | .Build(); |
| 489 | /* clang-format on */ |
Andrew Scull | b2c3a24 | 2019-11-04 13:52:36 +0000 | [diff] [blame^] | 490 | } |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 491 | |
Andrew Scull | b2c3a24 | 2019-11-04 13:52:36 +0000 | [diff] [blame^] | 492 | TEST(manifest, malformed_booleans) |
| 493 | { |
| 494 | struct manifest m; |
| 495 | struct fdt_node fdt_root; |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 496 | |
Andrew Scull | b2c3a24 | 2019-11-04 13:52:36 +0000 | [diff] [blame^] | 497 | std::vector<char> dtb_false = gen_malformed_boolean_dtb("\"false\""); |
| 498 | std::vector<char> dtb_true = gen_malformed_boolean_dtb("\"true\""); |
| 499 | std::vector<char> dtb_0 = gen_malformed_boolean_dtb("\"<0>\""); |
| 500 | std::vector<char> dtb_1 = gen_malformed_boolean_dtb("\"<1>\""); |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 501 | |
Andrew Scull | b2c3a24 | 2019-11-04 13:52:36 +0000 | [diff] [blame^] | 502 | ASSERT_TRUE(get_fdt_root(dtb_false, &fdt_root)); |
| 503 | ASSERT_EQ(manifest_init(&m, &fdt_root), |
| 504 | MANIFEST_ERROR_MALFORMED_BOOLEAN); |
| 505 | |
| 506 | ASSERT_TRUE(get_fdt_root(dtb_true, &fdt_root)); |
| 507 | ASSERT_EQ(manifest_init(&m, &fdt_root), |
| 508 | MANIFEST_ERROR_MALFORMED_BOOLEAN); |
| 509 | |
| 510 | ASSERT_TRUE(get_fdt_root(dtb_0, &fdt_root)); |
| 511 | ASSERT_EQ(manifest_init(&m, &fdt_root), |
| 512 | MANIFEST_ERROR_MALFORMED_BOOLEAN); |
| 513 | |
| 514 | ASSERT_TRUE(get_fdt_root(dtb_1, &fdt_root)); |
| 515 | ASSERT_EQ(manifest_init(&m, &fdt_root), |
| 516 | MANIFEST_ERROR_MALFORMED_BOOLEAN); |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 517 | } |
| 518 | |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 519 | TEST(manifest, valid) |
| 520 | { |
| 521 | struct manifest m; |
| 522 | struct manifest_vm *vm; |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 523 | struct fdt_node fdt_root; |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 524 | |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 525 | /* clang-format off */ |
| 526 | std::vector<char> dtb = ManifestDtBuilder() |
| 527 | .StartChild("hypervisor") |
David Brazdil | 74e9c3b | 2019-08-28 11:09:08 +0100 | [diff] [blame] | 528 | .Compatible() |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 529 | .StartChild("vm1") |
| 530 | .DebugName("primary_vm") |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 531 | .KernelFilename("primary_kernel") |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 532 | .RamdiskFilename("primary_ramdisk") |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 533 | .SmcWhitelist({0x32000000, 0x33001111}) |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 534 | .EndChild() |
| 535 | .StartChild("vm3") |
| 536 | .DebugName("second_secondary_vm") |
| 537 | .VcpuCount(43) |
| 538 | .MemSize(0x12345) |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 539 | .KernelFilename("second_secondary_kernel") |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 540 | .EndChild() |
| 541 | .StartChild("vm2") |
| 542 | .DebugName("first_secondary_vm") |
| 543 | .VcpuCount(42) |
| 544 | .MemSize(12345) |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 545 | .SmcWhitelist({0x04000000, 0x30002222, 0x31445566}) |
| 546 | .SmcWhitelistPermissive() |
David Brazdil | 52256ff | 2019-08-23 15:15:15 +0100 | [diff] [blame] | 547 | .EndChild() |
| 548 | .EndChild() |
| 549 | .Build(); |
| 550 | /* clang-format on */ |
| 551 | |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 552 | ASSERT_TRUE(get_fdt_root(dtb, &fdt_root)); |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 553 | |
David Brazdil | 0dbb41f | 2019-09-09 18:03:35 +0100 | [diff] [blame] | 554 | ASSERT_EQ(manifest_init(&m, &fdt_root), MANIFEST_SUCCESS); |
David Brazdil | 0251b94 | 2019-09-10 15:59:50 +0100 | [diff] [blame] | 555 | ASSERT_EQ(m.vm_count, 3); |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 556 | |
| 557 | vm = &m.vm[0]; |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 558 | ASSERT_STREQ(string_data(&vm->debug_name), "primary_vm"); |
| 559 | ASSERT_STREQ(string_data(&vm->kernel_filename), "primary_kernel"); |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 560 | ASSERT_STREQ(string_data(&vm->primary.ramdisk_filename), |
| 561 | "primary_ramdisk"); |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 562 | ASSERT_THAT( |
| 563 | std::span(vm->smc_whitelist.smcs, vm->smc_whitelist.smc_count), |
| 564 | ElementsAre(0x32000000, 0x33001111)); |
| 565 | ASSERT_FALSE(vm->smc_whitelist.permissive); |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 566 | |
| 567 | vm = &m.vm[1]; |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 568 | ASSERT_STREQ(string_data(&vm->debug_name), "first_secondary_vm"); |
| 569 | ASSERT_STREQ(string_data(&vm->kernel_filename), ""); |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 570 | ASSERT_EQ(vm->secondary.vcpu_count, 42); |
| 571 | ASSERT_EQ(vm->secondary.mem_size, 12345); |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 572 | ASSERT_THAT( |
| 573 | std::span(vm->smc_whitelist.smcs, vm->smc_whitelist.smc_count), |
| 574 | ElementsAre(0x04000000, 0x30002222, 0x31445566)); |
| 575 | ASSERT_TRUE(vm->smc_whitelist.permissive); |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 576 | |
| 577 | vm = &m.vm[2]; |
David Brazdil | 136f294 | 2019-09-23 14:11:03 +0100 | [diff] [blame] | 578 | ASSERT_STREQ(string_data(&vm->debug_name), "second_secondary_vm"); |
| 579 | ASSERT_STREQ(string_data(&vm->kernel_filename), |
| 580 | "second_secondary_kernel"); |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 581 | ASSERT_EQ(vm->secondary.vcpu_count, 43); |
| 582 | ASSERT_EQ(vm->secondary.mem_size, 0x12345); |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 583 | ASSERT_THAT( |
| 584 | std::span(vm->smc_whitelist.smcs, vm->smc_whitelist.smc_count), |
| 585 | IsEmpty()); |
| 586 | ASSERT_FALSE(vm->smc_whitelist.permissive); |
David Brazdil | 7a462ec | 2019-08-15 12:27:47 +0100 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | } /* namespace */ |