blob: 61be67b30bea9c9edf463e5be0df3c02f35c954e [file] [log] [blame]
Andrew Scull11a4a0c2018-12-29 11:38:31 +00001/*
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 Scull9a6384b2019-01-02 12:08:40 +000017#include "hf/arch/cpu.h"
18
Andrew Scull11a4a0c2018-12-29 11:38:31 +000019#include <stdbool.h>
20#include <stddef.h>
21#include <stdint.h>
22
Andrew Walbran4a53ba62019-03-05 17:26:12 +000023#include "hf/arch/std.h"
24
Andrew Scull11a4a0c2018-12-29 11:38:31 +000025#include "hf/addr.h"
26
27void arch_irq_disable(void)
28{
29 __asm__ volatile("msr DAIFSet, #0xf");
30}
31
32void arch_irq_enable(void)
33{
34 __asm__ volatile("msr DAIFClr, #0xf");
35}
36
Andrew Scullbb3ab6c2018-11-26 20:38:49 +000037void arch_regs_reset(struct arch_regs *r, bool is_primary, uint64_t vm_id,
38 uint64_t vcpu_id, paddr_t table)
Andrew Scull11a4a0c2018-12-29 11:38:31 +000039{
Andrew Scullc960c032018-10-24 15:13:35 +010040 uintreg_t pc = r->pc;
41 uintreg_t arg = r->r[0];
Andrew Scull11a4a0c2018-12-29 11:38:31 +000042 uintreg_t hcr;
43 uintreg_t cptr;
44 uintreg_t cnthctl;
45
Andrew Scullc960c032018-10-24 15:13:35 +010046 memset(r, 0, sizeof(*r));
47
48 r->pc = pc;
49 r->r[0] = arg;
50
Andrew Scull11a4a0c2018-12-29 11:38:31 +000051 /* TODO: Determine if we need to set TSW. */
52 hcr = (1u << 31) | /* RW bit. */
53 (1u << 21) | /* TACR, trap access to ACTRL_EL1. */
54 (1u << 19) | /* TSC, trap SMC instructions. */
55 (1u << 20) | /* TIDCP, trap impl-defined funct. */
56 (1u << 2) | /* PTW, Protected Table Walk. */
57 (1u << 0); /* VM: enable stage-2 translation. */
58
59 cptr = 0;
60 cnthctl = 0;
61
62 if (is_primary) {
63 cnthctl |=
64 (1u << 0) | /* EL1PCTEN, don't trap phys cnt access. */
65 (1u << 1); /* EL1PCEN, don't trap phys timer access. */
66 } else {
67 hcr |= (7u << 3) | /* AMO, IMO, FMO bits. */
68 (1u << 9) | /* FB bit. */
69 (1u << 10) | /* BSU bits set to inner-sh. */
70 (3u << 13); /* TWI, TWE bits. */
71
72 cptr |= (1u << 10); /* TFP, trap fp access. */
73 }
74
75 r->lazy.hcr_el2 = hcr;
76 r->lazy.cptr_el2 = cptr;
77 r->lazy.cnthctl_el2 = cnthctl;
Andrew Scullbb3ab6c2018-11-26 20:38:49 +000078 r->lazy.vttbr_el2 = pa_addr(table) | (vm_id << 48);
79 r->lazy.vmpidr_el2 = vcpu_id;
Andrew Scull11a4a0c2018-12-29 11:38:31 +000080 /* TODO: Use constant here. */
81 r->spsr = 5 | /* M bits, set to EL1h. */
82 (0xf << 6); /* DAIF bits set; disable interrupts. */
83}
84
85void arch_regs_set_pc_arg(struct arch_regs *r, ipaddr_t pc, uintreg_t arg)
86{
87 r->pc = ipa_addr(pc);
88 r->r[0] = arg;
89}
90
91void arch_regs_set_retval(struct arch_regs *r, uintreg_t v)
92{
93 r->r[0] = v;
94}