blob: 6ae6607da90c7eafeae1355fc62ad6f5c74c0805 [file] [log] [blame]
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +01001#include <stdalign.h>
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +01002#include <stddef.h>
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +01003#include <stdnoreturn.h>
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +01004
Andrew Scull18c78fc2018-08-20 12:57:41 +01005#include "hf/alloc.h"
6#include "hf/api.h"
7#include "hf/boot_params.h"
8#include "hf/cpio.h"
9#include "hf/cpu.h"
10#include "hf/dlog.h"
11#include "hf/load.h"
12#include "hf/mm.h"
13#include "hf/std.h"
14#include "hf/vm.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010015
Andrew Scull19503262018-09-20 14:48:39 +010016#include "vmapi/hf/call.h"
17
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +010018char ptable_buf[PAGE_SIZE * 40];
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010019
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010020/**
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010021 * Blocks the hypervisor.
22 *
23 * TODO: Determine if we want to omit strings on non-debug builds.
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010024 */
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010025noreturn void panic(const char *fmt, ...)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010026{
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010027 va_list args;
28
29 /* TODO: Block all CPUs. */
30
31 dlog("Panic: ");
32
33 va_start(args, fmt);
34 vdlog(fmt, args);
35 va_end(args);
36
37 dlog("\n");
38
39 for (;;) {
Andrew Scull7364a8e2018-07-19 15:39:29 +010040 }
Wedson Almeida Filho87009642018-07-02 10:20:07 +010041}
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010042
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010043/**
44 * Performs one-time initialisation of the hypervisor.
45 */
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010046static void one_time_init(void)
47{
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010048 struct boot_params params;
49 struct boot_params_update update;
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010050 struct memiter primary_initrd;
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010051 struct memiter cpio;
Andrew Scull80871322018-08-06 12:04:09 +010052 void *initrd;
Andrew Walbran34ce72e2018-09-13 16:47:44 +010053 size_t i;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010054
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010055 dlog("Initialising hafnium\n");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010056
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +010057 cpu_module_init();
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010058 halloc_init((size_t)ptable_buf, sizeof(ptable_buf));
59
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010060 if (!mm_init()) {
61 panic("mm_init failed");
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010062 }
63
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010064 if (!plat_get_boot_params(&params)) {
65 panic("unable to retrieve boot params");
66 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010067
Andrew Walbran34ce72e2018-09-13 16:47:44 +010068 for (i = 0; i < params.mem_ranges_count; ++i) {
69 dlog("Memory range: 0x%x - 0x%x\n",
70 pa_addr(params.mem_ranges[i].begin),
71 pa_addr(params.mem_ranges[i].end) - 1);
72 }
73
Andrew Scull265ada92018-07-30 15:19:01 +010074 dlog("Ramdisk range: 0x%x - 0x%x\n", pa_addr(params.initrd_begin),
75 pa_addr(params.initrd_end) - 1);
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010076
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010077 /* Map initrd in, and initialise cpio parser. */
Andrew Scull80871322018-08-06 12:04:09 +010078 initrd = mm_identity_map(params.initrd_begin, params.initrd_end,
79 MM_MODE_R);
80 if (!initrd) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010081 panic("unable to map initrd in");
82 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010083
Andrew Scull80871322018-08-06 12:04:09 +010084 memiter_init(&cpio, initrd,
Andrew Scull265ada92018-07-30 15:19:01 +010085 pa_addr(params.initrd_end) - pa_addr(params.initrd_begin));
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010086
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010087 /* Load all VMs. */
Wedson Almeida Filho9ee60e92018-07-23 18:56:56 +010088 if (!load_primary(&cpio, params.kernel_arg, &primary_initrd)) {
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010089 panic("unable to load primary VM");
90 }
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010091
Andrew Walbran34ce72e2018-09-13 16:47:44 +010092 update.initrd_begin = pa_from_va(va_from_ptr(primary_initrd.next));
93 update.initrd_end = pa_from_va(va_from_ptr(primary_initrd.limit));
94 /* load_secondary will add regions assigned to the secondary VMs from
95 * mem_ranges to reserved_ranges. */
96 update.reserved_ranges_count = 0;
97 if (!load_secondary(&cpio, &params, &update)) {
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +010098 panic("unable to load secondary VMs");
99 }
100
Andrew Walbran34ce72e2018-09-13 16:47:44 +0100101 /* Prepare to run by updating bootparams as seen by primary VM. */
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100102 if (!plat_update_boot_params(&update)) {
103 panic("plat_update_boot_params failed");
104 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100105
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100106 mm_defrag();
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100107
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100108 dlog("Hafnium initialisation completed\n");
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100109}
110
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100111/**
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100112 * The entry point of CPUs when they are turned on. It is supposed to initialise
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100113 * all state and return the first vCPU to run.
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100114 */
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100115struct vcpu *cpu_main(void)
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100116{
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100117 struct cpu *c = cpu();
Andrew Scull19503262018-09-20 14:48:39 +0100118 struct vm *primary;
Wedson Almeida Filho87009642018-07-02 10:20:07 +0100119
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +0100120 /*
121 * Do global one-time initialisation just once. We avoid using atomics
122 * by only touching the variable from cpu 0.
123 */
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100124 static volatile bool inited = false;
125 if (cpu_index(c) == 0 && !inited) {
126 inited = true;
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100127 one_time_init();
Wedson Almeida Filhofed69022018-07-11 15:39:12 +0100128 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100129
Wedson Almeida Filho3fcbcff2018-07-10 23:53:39 +0100130 dlog("Starting up cpu %d\n", cpu_index(c));
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100131
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100132 if (!mm_cpu_init()) {
133 panic("mm_cpu_init failed");
134 }
135
Andrew Scull19503262018-09-20 14:48:39 +0100136 primary = vm_get(HF_PRIMARY_VM_ID);
137 vm_set_current(primary);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +0100138
Andrew Scull19503262018-09-20 14:48:39 +0100139 return &primary->vcpus[cpu_index(c)];
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100140}