Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google LLC |
| 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 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 17 | #include "hf/vm.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 18 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 19 | #include "hf/api.h" |
| 20 | #include "hf/cpu.h" |
| 21 | #include "hf/std.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 22 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 23 | #include "vmapi/hf/call.h" |
| 24 | |
| 25 | static struct vm vms[MAX_VMS]; |
| 26 | static uint32_t vm_count; |
| 27 | |
| 28 | bool vm_init(uint32_t vcpu_count, struct vm **new_vm) |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 29 | { |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 30 | uint32_t i; |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 31 | struct vm *vm; |
| 32 | |
| 33 | if (vm_count >= MAX_VMS) { |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | vm = &vms[vm_count]; |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 38 | |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 39 | memset(vm, 0, sizeof(*vm)); |
| 40 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 41 | vm->id = vm_count; |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 42 | vm->vcpu_count = vcpu_count; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 43 | vm->mailbox.state = mailbox_state_empty; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 44 | |
| 45 | /* Do basic initialization of vcpus. */ |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 46 | for (i = 0; i < vcpu_count; i++) { |
Andrew Scull | f3d4559 | 2018-09-20 14:30:22 +0100 | [diff] [blame] | 47 | vcpu_init(&vm->vcpus[i], vm); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 48 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 49 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 50 | ++vm_count; |
| 51 | *new_vm = vm; |
| 52 | |
Andrew Scull | 8c3a63a | 2018-09-20 13:38:34 +0100 | [diff] [blame] | 53 | return mm_ptable_init(&vm->ptable, 0); |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 54 | } |
| 55 | |
Andrew Scull | 1950326 | 2018-09-20 14:48:39 +0100 | [diff] [blame] | 56 | uint32_t vm_get_count(void) |
| 57 | { |
| 58 | return vm_count; |
| 59 | } |
| 60 | |
| 61 | struct vm *vm_get(uint32_t id) |
| 62 | { |
| 63 | /* Ensure the VM is initialized. */ |
| 64 | if (id >= vm_count) { |
| 65 | return NULL; |
| 66 | } |
| 67 | |
| 68 | return &vms[id]; |
| 69 | } |
| 70 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 71 | /* TODO: Shall we use index or id here? */ |
Andrew Scull | 3740287 | 2018-10-24 14:23:06 +0100 | [diff] [blame] | 72 | void vm_start_vcpu(struct vm *vm, size_t index, ipaddr_t entry, uintreg_t arg) |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 73 | { |
Andrew Scull | f3d4559 | 2018-09-20 14:30:22 +0100 | [diff] [blame] | 74 | struct vcpu *vcpu = &vm->vcpus[index]; |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 75 | if (index < vm->vcpu_count) { |
Wedson Almeida Filho | 1f81b75 | 2018-10-24 15:15:49 +0100 | [diff] [blame] | 76 | arch_regs_init(&vcpu->regs, vm->id == HF_PRIMARY_VM_ID, vm->id, |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame^] | 77 | vm->ptable.root, entry, arg); |
Wedson Almeida Filho | 8700964 | 2018-07-02 10:20:07 +0100 | [diff] [blame] | 78 | vcpu_on(vcpu); |
| 79 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 80 | } |