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 | 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 | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 27 | |
Andrew Scull | 4e5f814 | 2018-10-12 14:37:19 +0100 | [diff] [blame] | 28 | #define PAGE_SIZE (1 << PAGE_BITS) |
| 29 | #define MM_PTE_PER_PAGE (PAGE_SIZE / sizeof(pte_t)) |
| 30 | |
| 31 | struct mm_page_table { |
| 32 | alignas(PAGE_SIZE) pte_t entries[MM_PTE_PER_PAGE]; |
| 33 | }; |
| 34 | static_assert(sizeof(struct mm_page_table) == PAGE_SIZE, |
| 35 | "A page table must take exactly one page."); |
| 36 | static_assert(alignof(struct mm_page_table) == PAGE_SIZE, |
| 37 | "A page table must be page aligned."); |
| 38 | |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 39 | struct mm_ptable { |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame^] | 40 | /** Address of the root of the page table. */ |
| 41 | paddr_t root; |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 42 | }; |
| 43 | |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 44 | /* The following are arch-independent page mapping modes. */ |
| 45 | #define MM_MODE_R 0x01 /* read */ |
| 46 | #define MM_MODE_W 0x02 /* write */ |
| 47 | #define MM_MODE_X 0x04 /* execute */ |
| 48 | #define MM_MODE_D 0x08 /* device */ |
| 49 | |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 50 | /** |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 51 | * This flag indicates that memory allocation must not use locks. This is |
| 52 | * relevant in systems where interlocked operations are only available after |
| 53 | * virtual memory is enabled. |
| 54 | */ |
| 55 | #define MM_MODE_NOSYNC 0x10 |
| 56 | |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 57 | /** |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 58 | * This flag indicates that the mapping is intended to be used in a first |
| 59 | * stage translation table, which might have different encodings for the |
| 60 | * attribute bits than the second stage table. |
| 61 | */ |
| 62 | #define MM_MODE_STAGE1 0x20 |
| 63 | |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 64 | /** |
Wedson Almeida Filho | 84a30a0 | 2018-07-23 20:05:05 +0100 | [diff] [blame] | 65 | * This flag indicates that no TLB invalidations should be issued for the |
| 66 | * changes in the page table. |
| 67 | */ |
| 68 | #define MM_MODE_NOINVALIDATE 0x40 |
| 69 | |
Andrew Scull | 8c3a63a | 2018-09-20 13:38:34 +0100 | [diff] [blame] | 70 | bool mm_ptable_init(struct mm_ptable *t, int mode); |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame^] | 71 | void mm_ptable_fini(struct mm_ptable *t, int mode); |
Wedson Almeida Filho | 84a30a0 | 2018-07-23 20:05:05 +0100 | [diff] [blame] | 72 | void mm_ptable_dump(struct mm_ptable *t, int mode); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 73 | void mm_ptable_defrag(struct mm_ptable *t, int mode); |
| 74 | |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 75 | bool mm_vm_identity_map(struct mm_ptable *t, paddr_t begin, paddr_t end, |
| 76 | int mode, ipaddr_t *ipa); |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 77 | bool mm_vm_unmap(struct mm_ptable *t, paddr_t begin, paddr_t end, int mode); |
Andrew Scull | 1ba470e | 2018-10-31 15:14:31 +0000 | [diff] [blame^] | 78 | bool mm_vm_unmap_hypervisor(struct mm_ptable *t, int mode); |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 79 | bool mm_vm_is_mapped(struct mm_ptable *t, ipaddr_t ipa, int mode); |
| 80 | bool mm_vm_translate(struct mm_ptable *t, ipaddr_t ipa, paddr_t *pa); |
| 81 | |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 82 | bool mm_init(void); |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 83 | bool mm_cpu_init(void); |
Andrew Scull | 8087132 | 2018-08-06 12:04:09 +0100 | [diff] [blame] | 84 | void *mm_identity_map(paddr_t begin, paddr_t end, int mode); |
| 85 | bool mm_unmap(paddr_t begin, paddr_t end, int mode); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 86 | void mm_defrag(void); |