1 // Copyright 2017 The Fuchsia Authors
2 //
3 // Use of this source code is governed by a MIT-style
4 // license that can be found in the LICENSE file or at
5 // https://opensource.org/licenses/MIT
6 
7 #pragma once
8 
9 #include <fbl/array.h>
10 #include <ktl/unique_ptr.h>
11 #include <hypervisor/id_allocator.h>
12 #include <hypervisor/page.h>
13 #include <kernel/mp.h>
14 
15 class El2TranslationTable {
16 public:
17     zx_status_t Init();
18     zx_paddr_t Base() const;
19 
20 private:
21     hypervisor::Page l0_page_;
22     hypervisor::Page l1_page_;
23 };
24 
25 // Represents a stack for use with EL2/
26 class El2Stack {
27 public:
28     zx_status_t Alloc();
29     zx_paddr_t Top() const;
30 
31 private:
32     hypervisor::Page page_;
33 };
34 
35 // Maintains the EL2 state for each CPU.
36 class El2CpuState : public hypervisor::IdAllocator<uint8_t, 64> {
37 public:
38     static zx_status_t Create(ktl::unique_ptr<El2CpuState>* out);
39     ~El2CpuState();
40 
41 private:
42     cpu_mask_t cpu_mask_ = 0;
43     El2TranslationTable table_;
44     fbl::Array<El2Stack> stacks_;
45 
46     El2CpuState() = default;
47 
48     static zx_status_t OnTask(void* context, uint cpu_num);
49 };
50 
51 // Allocate and free virtual machine IDs.
52 zx_status_t alloc_vmid(uint8_t* vmid);
53 zx_status_t free_vmid(uint8_t vmid);
54 
55 // Allocate and free virtual processor IDs.
56 zx_status_t alloc_vpid(uint8_t* vpid);
57 zx_status_t free_vpid(uint8_t vpid);
58