Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 The Hafnium Authors. |
| 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 | |
| 17 | #include "hf/arch/init.h" |
| 18 | |
| 19 | #include <stdalign.h> |
| 20 | #include <stddef.h> |
| 21 | |
| 22 | #include "hf/api.h" |
| 23 | #include "hf/boot_flow.h" |
| 24 | #include "hf/boot_params.h" |
| 25 | #include "hf/cpio.h" |
| 26 | #include "hf/cpu.h" |
| 27 | #include "hf/dlog.h" |
| 28 | #include "hf/load.h" |
| 29 | #include "hf/mm.h" |
| 30 | #include "hf/mpool.h" |
| 31 | #include "hf/panic.h" |
| 32 | #include "hf/plat/console.h" |
| 33 | #include "hf/std.h" |
| 34 | #include "hf/vm.h" |
| 35 | |
| 36 | #include "vmapi/hf/call.h" |
| 37 | |
| 38 | alignas(alignof( |
| 39 | struct mm_page_table)) char ptable_buf[sizeof(struct mm_page_table) * |
| 40 | HEAP_PAGES]; |
| 41 | |
| 42 | static struct mpool ppool; |
| 43 | |
| 44 | /** |
| 45 | * Performs one-time initialisation of memory management for the hypervisor. |
| 46 | * |
| 47 | * This is the only C code entry point called with MMU and caching disabled. The |
| 48 | * page table returned is used to set up the MMU and caches for all subsequent |
| 49 | * code. |
| 50 | */ |
| 51 | void one_time_init_mm(void) |
| 52 | { |
| 53 | /* Make sure the console is initialised before calling dlog. */ |
| 54 | plat_console_init(); |
| 55 | |
| 56 | dlog("Initialising hafnium\n"); |
| 57 | |
| 58 | mpool_init(&ppool, sizeof(struct mm_page_table)); |
| 59 | mpool_add_chunk(&ppool, ptable_buf, sizeof(ptable_buf)); |
| 60 | |
| 61 | if (!mm_init(&ppool)) { |
| 62 | panic("mm_init failed"); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Performs one-time initialisation of the hypervisor. |
| 68 | */ |
| 69 | void one_time_init(void) |
| 70 | { |
| 71 | struct manifest manifest; |
| 72 | struct boot_params params; |
| 73 | struct boot_params_update update; |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 74 | struct memiter cpio; |
| 75 | void *initrd; |
| 76 | size_t i; |
| 77 | struct mm_stage1_locked mm_stage1_locked; |
| 78 | |
| 79 | arch_one_time_init(); |
| 80 | |
| 81 | /* Enable locks now that mm is initialised. */ |
| 82 | dlog_enable_lock(); |
| 83 | mpool_enable_locks(); |
| 84 | |
| 85 | mm_stage1_locked = mm_lock_stage1(); |
| 86 | |
| 87 | if (!boot_flow_init(mm_stage1_locked, &manifest, ¶ms, &ppool)) { |
| 88 | panic("Could not parse data from FDT."); |
| 89 | } |
| 90 | |
| 91 | cpu_module_init(params.cpu_ids, params.cpu_count); |
| 92 | |
| 93 | for (i = 0; i < params.mem_ranges_count; ++i) { |
| 94 | dlog("Memory range: %#x - %#x\n", |
| 95 | pa_addr(params.mem_ranges[i].begin), |
| 96 | pa_addr(params.mem_ranges[i].end) - 1); |
| 97 | } |
| 98 | |
| 99 | dlog("Ramdisk range: %#x - %#x\n", pa_addr(params.initrd_begin), |
| 100 | pa_addr(params.initrd_end) - 1); |
| 101 | |
| 102 | /* Map initrd in, and initialise cpio parser. */ |
| 103 | initrd = mm_identity_map(mm_stage1_locked, params.initrd_begin, |
| 104 | params.initrd_end, MM_MODE_R, &ppool); |
| 105 | if (!initrd) { |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 106 | panic("Unable to map initrd."); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | memiter_init(&cpio, initrd, |
| 110 | pa_difference(params.initrd_begin, params.initrd_end)); |
| 111 | |
| 112 | /* Load all VMs. */ |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 113 | update.reserved_ranges_count = 0; |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 114 | if (!load_vms(mm_stage1_locked, &manifest, &cpio, ¶ms, &update, |
| 115 | &ppool)) { |
| 116 | panic("Unable to load VMs."); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 117 | } |
| 118 | |
Andrew Scull | c377107 | 2019-09-19 13:30:42 +0100 | [diff] [blame^] | 119 | if (!boot_flow_update(mm_stage1_locked, &update, &ppool)) { |
| 120 | panic("Unable to update boot flow."); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | mm_defrag(mm_stage1_locked, &ppool); |
| 124 | mm_unlock_stage1(&mm_stage1_locked); |
| 125 | |
| 126 | /* Initialise the API page pool. ppool will be empty from now on. */ |
| 127 | api_init(&ppool); |
| 128 | |
| 129 | /* Enable TLB invalidation for VM page table updates. */ |
| 130 | mm_vm_enable_invalidation(); |
| 131 | |
| 132 | dlog("Hafnium initialisation completed\n"); |
| 133 | } |