1 /* 2 * Copyright (c) 2019 Apple Inc. All rights reserved. 3 * 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. The rights granted to you under the License 10 * may not be used to create, or enable the creation or redistribution of, 11 * unlawful or unlicensed copies of an Apple operating system, or to 12 * circumvent, violate, or enable the circumvention or violation of, any 13 * terms of an Apple operating system software license agreement. 14 * 15 * Please obtain a copy of the License at 16 * http://www.opensource.apple.com/apsl/ and read it before using this file. 17 * 18 * The Original Code and all software distributed under the License are 19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23 * Please see the License for the specific language governing rights and 24 * limitations under the License. 25 * 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27 */ 28 29 #ifndef _ARM64_PAC_ASM_H_ 30 #define _ARM64_PAC_ASM_H_ 31 32 #ifndef __ASSEMBLER__ 33 #error "This header should only be used in .s files" 34 #endif 35 36 #include <pexpert/arm64/board_config.h> 37 #include <arm64/proc_reg.h> 38 #if HAS_PARAVIRTUALIZED_PAC 39 #include <arm64/hv_hvc.h> 40 #include "smccc_asm.h" 41 #endif 42 #include "assym.s" 43 44 #if defined(HAS_APPLE_PAC) 45 46 47 /* BEGIN IGNORE CODESTYLE */ 48 49 /** 50 * REPROGRAM_JOP_KEYS 51 * 52 * Loads a userspace process's JOP key (task->jop_pid) into the CPU, and 53 * updates current_cpu_datap()->jop_key accordingly. This reprogramming process 54 * is skipped whenever the "new" JOP key has already been loaded into the CPU. 55 * 56 * skip_label - branch to this label if new_jop_key is already loaded into CPU 57 * new_jop_key - process's jop_pid 58 * cpudatap - current cpu_data_t * 59 * tmp - scratch register 60 */ 61 .macro REPROGRAM_JOP_KEYS skip_label, new_jop_key, cpudatap, tmp 62 ldr \tmp, [\cpudatap, CPU_JOP_KEY] 63 cmp \new_jop_key, \tmp 64 b.eq \skip_label 65 SET_JOP_KEY_REGISTERS \new_jop_key, \tmp 66 str \new_jop_key, [\cpudatap, CPU_JOP_KEY] 67 .endmacro 68 69 /** 70 * REPROGRAM_ROP_KEYS 71 * 72 * Loads a userspace process's ROP key (task->rop_pid) into the CPU, and 73 * updates current_cpu_datap()->rop_key accordingly. This reprogramming process 74 * is skipped whenever the "new" ROP key has already been loaded into the CPU. 75 * 76 * skip_label - branch to this label if new_rop_key is already loaded into CPU 77 * new_rop_key - process's rop_pid 78 * cpudatap - current cpu_data_t * 79 * tmp - scratch register 80 */ 81 .macro REPROGRAM_ROP_KEYS skip_label, new_rop_key, cpudatap, tmp 82 ldr \tmp, [\cpudatap, CPU_ROP_KEY] 83 cmp \new_rop_key, \tmp 84 b.eq \skip_label 85 SET_ROP_KEY_REGISTERS \new_rop_key, \tmp 86 str \new_rop_key, [\cpudatap, CPU_ROP_KEY] 87 .endmacro 88 89 /** 90 * SET_JOP_KEY_REGISTERS 91 * 92 * Unconditionally loads a userspace process's JOP key (task->jop_pid) into the 93 * CPU. The caller is responsible for updating current_cpu_datap()->jop_key as 94 * needed. 95 * 96 * new_jop_key - process's jop_pid 97 * tmp - scratch register 98 */ 99 .macro SET_JOP_KEY_REGISTERS new_jop_key, tmp 100 #if HAS_PARAVIRTUALIZED_PAC 101 SAVE_SMCCC_CLOBBERED_REGISTERS 102 /* 103 * We're deliberately calling PAC_SET_EL0_DIVERSIFIER here, even though the 104 * EL0 diversifier affects both A (JOP) and B (ROP) keys. We don't want 105 * SET_JOP_KEY_REGISTERS to have an impact on the EL1 A key state, since 106 * these are the keys the kernel uses to sign pointers on the heap. 107 * 108 * Using new_jop_key as the EL0 diversifer has the same net effect of giving 109 * userspace its own set of JOP keys, but doesn't affect EL1 A key state. 110 */ 111 MOV64 x0, VMAPPLE_PAC_SET_EL0_DIVERSIFIER 112 mov x1, \new_jop_key 113 hvc #0 114 LOAD_SMCCC_CLOBBERED_REGISTERS 115 #endif /* HAS_PARAVIRTUALIZED_PAC */ 116 .endmacro 117 118 /** 119 * SET_ROP_KEY_REGISTERS 120 * 121 * Unconditionally loads a userspace process's ROP key (task->rop_pid) into the 122 * CPU. The caller is responsible for updating current_cpu_datap()->rop_key as 123 * needed. 124 * 125 * new_rop_key - process's rop_pid 126 * tmp - scratch register 127 */ 128 .macro SET_ROP_KEY_REGISTERS new_rop_key, tmp 129 #if HAS_PARAVIRTUALIZED_PAC 130 SAVE_SMCCC_CLOBBERED_REGISTERS 131 MOV64 x0, VMAPPLE_PAC_SET_B_KEYS 132 mov x1, \new_rop_key 133 hvc #0 134 LOAD_SMCCC_CLOBBERED_REGISTERS 135 #endif /* HAS_PARAVIRTUALIZED_PAC */ 136 .endmacro 137 138 /** 139 * PAC_INIT_KEY_STATE 140 * 141 * Sets the initial PAC key state, but does not enable the keys. 142 * 143 * tmp - scratch register 144 * tmp2 - scratch register 145 */ 146 .macro PAC_INIT_KEY_STATE tmp, tmp2 147 #if HAS_PARAVIRTUALIZED_PAC 148 #if HIBERNATION 149 #error PAC_INIT_KEY_STATE is not implemented for HAS_PARAVIRTUALIZED_PAC && HIBERNATION 150 #endif 151 /* 152 * This call clobbers x0-x3. However we only initialize PAC at a point in 153 * common_start where x0-x3 are safe to clobber, and where we don't yet have 154 * a working stack to stash the existing values anyway. 155 */ 156 mov x0, #VMAPPLE_PAC_SET_INITIAL_STATE 157 hvc #0 158 #endif /* HAS_PARAVIRTUALIZED_PAC */ 159 .endmacro 160 161 /* END IGNORE CODESTYLE */ 162 163 #endif /* defined(HAS_APPLE_PAC) */ 164 165 #endif /* _ARM64_PAC_ASM_H_ */ 166 167 /* vim: set ts=4 ft=asm: */ 168