Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [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 | 9a6384b | 2019-01-02 12:08:40 +0000 | [diff] [blame] | 17 | #include "hf/arch/cpu.h" |
| 18 | |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 19 | #include <stdbool.h> |
| 20 | #include <stddef.h> |
| 21 | #include <stdint.h> |
| 22 | |
Andrew Walbran | 4a53ba6 | 2019-03-05 17:26:12 +0000 | [diff] [blame^] | 23 | #include "hf/arch/std.h" |
| 24 | |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 25 | #include "hf/addr.h" |
| 26 | |
| 27 | void arch_irq_disable(void) |
| 28 | { |
| 29 | __asm__ volatile("msr DAIFSet, #0xf"); |
| 30 | } |
| 31 | |
| 32 | void arch_irq_enable(void) |
| 33 | { |
| 34 | __asm__ volatile("msr DAIFClr, #0xf"); |
| 35 | } |
| 36 | |
Andrew Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 37 | void arch_regs_reset(struct arch_regs *r, bool is_primary, uint64_t vm_id, |
| 38 | uint64_t vcpu_id, paddr_t table) |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 39 | { |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 40 | uintreg_t pc = r->pc; |
| 41 | uintreg_t arg = r->r[0]; |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 42 | uintreg_t hcr; |
| 43 | uintreg_t cptr; |
| 44 | uintreg_t cnthctl; |
| 45 | |
Andrew Scull | c960c03 | 2018-10-24 15:13:35 +0100 | [diff] [blame] | 46 | memset(r, 0, sizeof(*r)); |
| 47 | |
| 48 | r->pc = pc; |
| 49 | r->r[0] = arg; |
| 50 | |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 51 | /* 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 Scull | bb3ab6c | 2018-11-26 20:38:49 +0000 | [diff] [blame] | 78 | r->lazy.vttbr_el2 = pa_addr(table) | (vm_id << 48); |
| 79 | r->lazy.vmpidr_el2 = vcpu_id; |
Andrew Scull | 11a4a0c | 2018-12-29 11:38:31 +0000 | [diff] [blame] | 80 | /* TODO: Use constant here. */ |
| 81 | r->spsr = 5 | /* M bits, set to EL1h. */ |
| 82 | (0xf << 6); /* DAIF bits set; disable interrupts. */ |
| 83 | } |
| 84 | |
| 85 | void 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 | |
| 91 | void arch_regs_set_retval(struct arch_regs *r, uintreg_t v) |
| 92 | { |
| 93 | r->r[0] = v; |
| 94 | } |