blob: a5c1f2934e16933cf4737991901400d29c1a83d8 [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
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
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010017#include <stdalign.h>
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010018#include <stddef.h>
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010019#include <stdnoreturn.h>
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010020
Andrew Scull18c78fc2018-08-20 12:57:41 +010021#include "hf/api.h"
22#include "hf/boot_params.h"
23#include "hf/cpio.h"
24#include "hf/cpu.h"
25#include "hf/dlog.h"
26#include "hf/load.h"
27#include "hf/mm.h"
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000028#include "hf/mpool.h"
Andrew Scull18c78fc2018-08-20 12:57:41 +010029#include "hf/std.h"
30#include "hf/vm.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010031
Andrew Scull19503262018-09-20 14:48:39 +010032#include "vmapi/hf/call.h"
33
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000034alignas(sizeof(
35 struct mm_page_table)) char ptable_buf[sizeof(struct mm_page_table) *
36 HEAP_PAGES];
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010037
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010038/**
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010039 * Blocks the hypervisor.
40 *
41 * TODO: Determine if we want to omit strings on non-debug builds.
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010042 */
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010043noreturn void panic(const char *fmt, ...)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010044{
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010045 va_list args;
46
47 /* TODO: Block all CPUs. */
48
Andrew Scullcb0a7412018-11-06 17:28:14 +000049 dlog_nosync("Panic: ");
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010050
51 va_start(args, fmt);
Andrew Scullcb0a7412018-11-06 17:28:14 +000052 vdlog_nosync(fmt, args);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010053 va_end(args);
54
Andrew Scullcb0a7412018-11-06 17:28:14 +000055 dlog_nosync("\n");
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010056
57 for (;;) {
Andrew Scull7364a8e2018-07-19 15:39:29 +010058 }
Wedson Almeida Filho87009642018-07-02 10:20:07 +010059}
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010060
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010061/**
62 * Performs one-time initialisation of the hypervisor.
63 */
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010064static void one_time_init(void)
65{
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010066 struct boot_params params;
67 struct boot_params_update update;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010068 struct memiter primary_initrd;
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010069 struct memiter cpio;
Andrew Scull80871322018-08-06 12:04:09 +010070 void *initrd;
Andrew Walbran34ce72e2018-09-13 16:47:44 +010071 size_t i;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000072 struct mpool ppool;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010073
Andrew Scullcb0a7412018-11-06 17:28:14 +000074 dlog_nosync("Initialising hafnium\n");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010075
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000076 mpool_init(&ppool, sizeof(struct mm_page_table));
77 mpool_add_chunk(&ppool, ptable_buf, sizeof(ptable_buf));
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010078
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000079 cpu_module_init();
80
81 if (!mm_init(&ppool)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010082 panic("mm_init failed");
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010083 }
84
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +000085 /* Enable locks now that mm is initialised. */
86 mpool_enable_locks();
87
88 if (!plat_get_boot_params(&params, &ppool)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010089 panic("unable to retrieve boot params");
90 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010091
Andrew Walbran34ce72e2018-09-13 16:47:44 +010092 for (i = 0; i < params.mem_ranges_count; ++i) {
93 dlog("Memory range: 0x%x - 0x%x\n",
94 pa_addr(params.mem_ranges[i].begin),
95 pa_addr(params.mem_ranges[i].end) - 1);
96 }
97
Andrew Scull265ada92018-07-30 15:19:01 +010098 dlog("Ramdisk range: 0x%x - 0x%x\n", pa_addr(params.initrd_begin),
99 pa_addr(params.initrd_end) - 1);
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100100
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100101 /* Map initrd in, and initialise cpio parser. */
Andrew Scull80871322018-08-06 12:04:09 +0100102 initrd = mm_identity_map(params.initrd_begin, params.initrd_end,
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000103 MM_MODE_R, &ppool);
Andrew Scull80871322018-08-06 12:04:09 +0100104 if (!initrd) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100105 panic("unable to map initrd in");
106 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100107
Andrew Scull80871322018-08-06 12:04:09 +0100108 memiter_init(&cpio, initrd,
Andrew Scull265ada92018-07-30 15:19:01 +0100109 pa_addr(params.initrd_end) - pa_addr(params.initrd_begin));
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100110
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100111 /* Load all VMs. */
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000112 if (!load_primary(&cpio, params.kernel_arg, &primary_initrd, &ppool)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100113 panic("unable to load primary VM");
114 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100115
Wedson Almeida Filhob2c159e2018-10-25 13:27:47 +0100116 /*
117 * load_secondary will add regions assigned to the secondary VMs from
118 * mem_ranges to reserved_ranges.
119 */
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100120 update.initrd_begin = pa_from_va(va_from_ptr(primary_initrd.next));
121 update.initrd_end = pa_from_va(va_from_ptr(primary_initrd.limit));
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100122 update.reserved_ranges_count = 0;
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000123 if (!load_secondary(&cpio, &params, &update, &ppool)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +0100124 panic("unable to load secondary VMs");
125 }
126
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100127 /* Prepare to run by updating bootparams as seen by primary VM. */
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000128 if (!plat_update_boot_params(&update, &ppool)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100129 panic("plat_update_boot_params failed");
130 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100131
Wedson Almeida Filho22d5eaa2018-12-16 00:38:49 +0000132 mm_defrag(&ppool);
133
134 /* Initialise the API page pool. ppool will be empty from now on. */
135 api_init(&ppool);
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100136
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100137 dlog("Hafnium initialisation completed\n");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100138}
139
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100140/**
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100141 * The entry point of CPUs when they are turned on. It is supposed to initialise
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100142 * all state and return the first vCPU to run.
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100143 */
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100144struct vcpu *cpu_main(struct cpu *c)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100145{
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100146 struct vcpu *vcpu;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100147
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100148 /*
149 * Do global one-time initialisation just once. We avoid using atomics
150 * by only touching the variable from cpu 0.
151 */
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100152 static volatile bool inited = false;
153 if (cpu_index(c) == 0 && !inited) {
154 inited = true;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100155 one_time_init();
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100156 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100157
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100158 dlog("Starting up cpu %d\n", cpu_index(c));
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100159
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100160 if (!mm_cpu_init()) {
161 panic("mm_cpu_init failed");
162 }
163
Wedson Almeida Filho1f81b752018-10-24 15:15:49 +0100164 vcpu = &vm_get(HF_PRIMARY_VM_ID)->vcpus[cpu_index(c)];
Wedson Almeida Filho00df6c72018-10-18 11:19:24 +0100165 vcpu->cpu = c;
166 return vcpu;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100167}