Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 1 | #include "api.h" |
| 2 | |
| 3 | #include "arch_api.h" |
| 4 | #include "vm.h" |
| 5 | |
| 6 | struct vm secondary_vm[MAX_VMS]; |
| 7 | uint32_t secondary_vm_count; |
| 8 | struct vm primary_vm; |
| 9 | |
| 10 | /** |
| 11 | * Returns the number of VMs configured to run. |
| 12 | */ |
| 13 | int32_t api_vm_get_count(void) |
| 14 | { |
| 15 | return secondary_vm_count; |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Returns the number of vcpus configured in the given VM. |
| 20 | */ |
| 21 | int32_t api_vcpu_get_count(uint32_t vm_idx) |
| 22 | { |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 23 | if (vm_idx >= secondary_vm_count) { |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 24 | return -1; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 25 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 26 | |
| 27 | return secondary_vm[vm_idx].vcpu_count; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Runs the given vcpu of the given vm. |
| 32 | */ |
| 33 | int32_t api_vcpu_run(uint32_t vm_idx, uint32_t vcpu_idx, struct vcpu **next) |
| 34 | { |
| 35 | struct vm *vm = secondary_vm + vm_idx; |
| 36 | struct vcpu *vcpu; |
| 37 | |
| 38 | /* Only the primary VM can switch vcpus. */ |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 39 | if (cpu()->current->vm != &primary_vm) { |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 40 | return HF_VCPU_WAIT_FOR_INTERRUPT; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 41 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 42 | |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 43 | if (vm_idx >= secondary_vm_count) { |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 44 | return HF_VCPU_WAIT_FOR_INTERRUPT; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 45 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 46 | |
| 47 | vcpu = vm->vcpus + vcpu_idx; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 48 | if (vcpu_idx >= vm->vcpu_count || !vcpu->is_on) { |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 49 | return HF_VCPU_WAIT_FOR_INTERRUPT; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 50 | } |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 51 | |
Wedson Almeida Filho | 84a30a0 | 2018-07-23 20:05:05 +0100 | [diff] [blame] | 52 | vm_set_current(vm); |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 53 | *next = vcpu; |
| 54 | |
| 55 | return HF_VCPU_YIELD; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Puts current vcpu in wait for interrupt mode, and returns to the primary |
| 60 | * vm. |
| 61 | */ |
| 62 | struct vcpu *api_wait_for_interrupt(void) |
| 63 | { |
| 64 | struct vcpu *vcpu = &primary_vm.vcpus[cpu_index(cpu())]; |
| 65 | |
| 66 | /* Switch back to primary VM. */ |
Wedson Almeida Filho | 84a30a0 | 2018-07-23 20:05:05 +0100 | [diff] [blame] | 67 | vm_set_current(&primary_vm); |
Wedson Almeida Filho | 3fcbcff | 2018-07-10 23:53:39 +0100 | [diff] [blame] | 68 | |
| 69 | /* |
| 70 | * Inidicate to primary VM that this vcpu blocked waiting for an |
| 71 | * interrupt. |
| 72 | */ |
| 73 | arch_regs_set_retval(&vcpu->regs, HF_VCPU_WAIT_FOR_INTERRUPT); |
| 74 | |
| 75 | return vcpu; |
| 76 | } |