Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | /* |
Andrew Walbran | 692b325 | 2019-03-07 15:51:31 +0000 | [diff] [blame^] | 2 | * Copyright 2018 The Hafnium Authors. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 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 | fbc938a | 2018-08-20 14:09:28 +0100 | [diff] [blame] | 17 | #pragma once |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 18 | |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 19 | #include <assert.h> |
| 20 | #include <stdalign.h> |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 21 | #include <stdbool.h> |
Wedson Almeida Filho | 84a30a0 | 2018-07-23 20:05:05 +0100 | [diff] [blame] | 22 | #include <stdint.h> |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 23 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 24 | #include "hf/arch/mm.h" |
| 25 | |
| 26 | #include "hf/addr.h" |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 27 | #include "hf/mpool.h" |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 28 | |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 29 | /* Keep macro alignment */ |
| 30 | /* clang-format off */ |
| 31 | |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 32 | #define PAGE_SIZE (1 << PAGE_BITS) |
| 33 | #define MM_PTE_PER_PAGE (PAGE_SIZE / sizeof(pte_t)) |
| 34 | |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 35 | |
| 36 | /* The following are arch-independent page mapping modes. */ |
| 37 | #define MM_MODE_R 0x0001 /* read */ |
| 38 | #define MM_MODE_W 0x0002 /* write */ |
| 39 | #define MM_MODE_X 0x0004 /* execute */ |
| 40 | #define MM_MODE_D 0x0008 /* device */ |
| 41 | |
| 42 | /* |
| 43 | * Memory in stage-1 is either valid (present) or invalid (absent). |
| 44 | * |
| 45 | * Memory in stage-2 has more states to track sharing, borrowing and giving of |
| 46 | * memory. The states are made up of three parts: |
| 47 | * |
| 48 | * 1. V = valid/invalid : Whether the memory is part of the VM's address |
| 49 | * space. A fault will be generated if accessed when |
| 50 | * invalid. |
| 51 | * 2. O = owned/unowned : Whether the memory is owned by the VM. |
| 52 | * 3. X = exclusive/shared : Whether access is exclusive to the VM or shared |
| 53 | * with at most one other. |
| 54 | * |
| 55 | * These parts compose to form the following state: |
| 56 | * |
| 57 | * - V O X : Owner of memory with exclusive access. |
| 58 | * - V O !X : Owner of memory with access shared with at most one other VM. |
| 59 | * - V !O X : Borrower of memory with exclusive access. |
| 60 | * - V !O !X : Borrower of memory where access is shared with the owner. |
| 61 | * - !V O X : Owner of memory lent to a VM that has exclusive access. |
| 62 | * |
| 63 | * - !V O !X : Unused. Owner of shared memory always has access. |
| 64 | * |
| 65 | * - !V !O X : Invalid memory. Memory is unrelated to the VM. |
| 66 | * - !V !O !X : Invalid memory. Memory is unrelated to the VM. |
| 67 | * |
| 68 | * Modes are selected so that owner of exclusive memory is the default. |
| 69 | */ |
| 70 | #define MM_MODE_INVALID 0x0010 |
| 71 | #define MM_MODE_UNOWNED 0x0020 |
| 72 | #define MM_MODE_SHARED 0x0040 |
| 73 | |
Andrew Scull | c66a04d | 2018-12-07 13:41:56 +0000 | [diff] [blame] | 74 | /* clang-format on */ |
| 75 | |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 76 | struct mm_page_table { |
| 77 | alignas(PAGE_SIZE) pte_t entries[MM_PTE_PER_PAGE]; |
| 78 | }; |
| 79 | static_assert(sizeof(struct mm_page_table) == PAGE_SIZE, |
| 80 | "A page table must take exactly one page."); |
| 81 | static_assert(alignof(struct mm_page_table) == PAGE_SIZE, |
| 82 | "A page table must be page aligned."); |
| 83 | |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 84 | struct mm_ptable { |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame] | 85 | /** Address of the root of the page table. */ |
| 86 | paddr_t root; |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 87 | }; |
| 88 | |
Andrew Scull | da24197 | 2019-01-05 18:17:48 +0000 | [diff] [blame] | 89 | void mm_vm_enable_invalidation(void); |
| 90 | |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 91 | bool mm_vm_init(struct mm_ptable *t, struct mpool *ppool); |
| 92 | void mm_vm_fini(struct mm_ptable *t, struct mpool *ppool); |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 93 | bool mm_vm_identity_map(struct mm_ptable *t, paddr_t begin, paddr_t end, |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 94 | int mode, ipaddr_t *ipa, struct mpool *ppool); |
Andrew Scull | da24197 | 2019-01-05 18:17:48 +0000 | [diff] [blame] | 95 | bool mm_vm_unmap(struct mm_ptable *t, paddr_t begin, paddr_t end, |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 96 | struct mpool *ppool); |
Andrew Scull | da24197 | 2019-01-05 18:17:48 +0000 | [diff] [blame] | 97 | bool mm_vm_unmap_hypervisor(struct mm_ptable *t, struct mpool *ppool); |
Andrew Scull | da3df7f | 2019-01-05 17:49:27 +0000 | [diff] [blame] | 98 | void mm_vm_defrag(struct mm_ptable *t, struct mpool *ppool); |
| 99 | void mm_vm_dump(struct mm_ptable *t); |
Andrew Scull | 81e8509 | 2018-12-12 12:56:20 +0000 | [diff] [blame] | 100 | bool mm_vm_get_mode(struct mm_ptable *t, ipaddr_t begin, ipaddr_t end, |
| 101 | int *mode); |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 102 | |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 103 | bool mm_init(struct mpool *ppool); |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 104 | bool mm_cpu_init(void); |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 105 | void *mm_identity_map(paddr_t begin, paddr_t end, int mode, |
| 106 | struct mpool *ppool); |
Andrew Scull | da24197 | 2019-01-05 18:17:48 +0000 | [diff] [blame] | 107 | bool mm_unmap(paddr_t begin, paddr_t end, struct mpool *ppool); |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 108 | void mm_defrag(struct mpool *ppool); |