github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/cpvmm/vmm/host/policy_manager.c (about) 1 /* 2 * Copyright (c) 2013 Intel Corporation 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 * http://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 17 #include "policy_manager.h" 18 //#include "libc.h" 19 #include "vmm_dbg.h" 20 #include "file_codes.h" 21 #define VMM_DEADLOOP() VMM_DEADLOOP_LOG(POLICY_MANAGER_C) 22 #define VMM_ASSERT(__condition) VMM_ASSERT_LOG(POLICY_MANAGER_C, __condition) 23 #ifdef JLMDEBUG 24 #include "jlmdebug.h" 25 #endif 26 27 #define FIELD_MASK(size, offset) ((BIT_VALUE64((size)) - 1) << (offset)) 28 29 #define POL_PG_FIELD_OFFS 0 30 #define POL_PG_FIELD_SIZE 10 31 //#define POL_PG_MASK ((BIT_VALUE64(POL_PG_FIELD_SIZE) - 1) << POL_PG_FIELD_OFFS) 32 #define POL_PG_MASK FIELD_MASK(POL_PG_FIELD_SIZE, POL_PG_FIELD_OFFS) 33 34 #define POL_CACHE_FIELD_OFFS (POL_PG_FIELD_OFFS + POL_PG_FIELD_SIZE) 35 #define POL_CACHE_FIELD_SIZE 2 36 //#define POL_CACHE_MASK ((BIT_VALUE64(POL_CACHE_FIELD_SIZE) - 1) << POL_CACHE_FIELD_OFFS) 37 #define POL_CACHE_MASK FIELD_MASK(POL_CACHE_FIELD_SIZE, POL_CACHE_FIELD_OFFS) 38 39 40 static VMM_POLICY g_vmm_policy; 41 static BOOLEAN g_init_done = FALSE; 42 extern VMM_PAGING_POLICY g_pg_policy; 43 44 45 // 46 // Policy Manager 47 // 48 49 50 // Setup the global policy. 51 // Called by BSP main() before any initializations to setup the uVMM policy. 52 POL_RETVAL global_policy_setup(const VMM_POLICY *policy) 53 { 54 if (!g_init_done) { 55 clear_policy(&g_vmm_policy); 56 g_init_done = TRUE; 57 } 58 return copy_policy(&g_vmm_policy, policy); 59 } 60 61 62 BOOLEAN global_policy_uses_vtlb(void) 63 { 64 VMM_PAGING_POLICY pg_policy; 65 66 get_paging_policy(&g_vmm_policy, &pg_policy); 67 return (pg_policy == POL_PG_VTLB); 68 } 69 70 71 BOOLEAN global_policy_uses_ept(void) 72 { 73 VMM_PAGING_POLICY pg_policy; 74 75 get_paging_policy(&g_vmm_policy, &pg_policy); 76 return (pg_policy == POL_PG_EPT); 77 } 78 79 80 POL_RETVAL get_global_policy(VMM_POLICY *policy) 81 { 82 return copy_policy(policy, &g_vmm_policy); 83 } 84 85 86 BOOLEAN global_policy_is_cache_dis_virtualized(void) 87 { 88 VMM_CACHE_POLICY cache_policy; 89 90 get_cache_policy(&g_vmm_policy, &cache_policy); 91 return (cache_policy == POL_CACHE_DIS_VIRTUALIZATION); 92 } 93 94 95 // Policy Manipulation APIs 96 POL_RETVAL clear_policy(VMM_POLICY *policy) 97 { 98 *policy = 0; 99 100 return POL_RETVAL_SUCCESS; 101 } 102 103 104 POL_RETVAL copy_policy(VMM_POLICY *dst_policy, const VMM_POLICY *src_policy) 105 { 106 VMM_ASSERT(dst_policy != NULL); 107 108 *dst_policy = *src_policy; 109 return POL_RETVAL_SUCCESS; 110 } 111 112 113 static POL_RETVAL get_policy(const VMM_POLICY *policy, void *policy_enum, UINT32 offs, 114 UINT32 size, UINT32 err_val) 115 { 116 UINT64 bit = BIT_VALUE64(offs); 117 UINT32 count; 118 POL_RETVAL ret = POL_RETVAL_SUCCESS; 119 120 VMM_ASSERT(policy != NULL); 121 VMM_ASSERT(policy_enum != NULL); 122 123 for (count = 0; (*policy & bit) == 0 && count < size; count++, bit <<= 1) ; 124 125 if (count == size) { 126 ret = POL_RETVAL_BAD_VALUE; 127 *(UINT32 *) policy_enum = err_val; 128 } 129 else 130 *(UINT32 *) policy_enum = count; 131 132 return ret; 133 } 134 135 #ifdef INCLUDE_UNUSED_CODE 136 POL_RETVAL clear_paging_policy(VMM_POLICY *policy) 137 { 138 VMM_ASSERT(policy != NULL); 139 140 BITMAP_CLR64(*policy, POL_PG_MASK); 141 142 return POL_RETVAL_SUCCESS; 143 } 144 #endif 145 146 POL_RETVAL set_paging_policy(VMM_POLICY *policy, VMM_PAGING_POLICY pg_policy) 147 { 148 // BEFORE_VMLAUNCH. PARANOID check. 149 VMM_ASSERT(policy != NULL); 150 151 BITMAP_ASSIGN64(*policy, POL_PG_MASK, BIT_VALUE64((int) pg_policy + POL_PG_FIELD_OFFS)); 152 g_pg_policy = pg_policy; 153 return POL_RETVAL_SUCCESS; 154 } 155 156 157 POL_RETVAL get_paging_policy(const VMM_POLICY *policy, VMM_PAGING_POLICY *pg_policy) 158 { 159 return get_policy(policy, pg_policy, POL_PG_FIELD_OFFS, POL_PG_FIELD_SIZE, POL_CACHE_DIS_ILLEGAL); 160 } 161 162 #ifdef INCLUDE_UNUSED_CODE 163 POL_RETVAL clear_cache_policy(VMM_POLICY *policy) 164 { 165 VMM_ASSERT(policy != NULL); 166 BITMAP_CLR64(*policy, POL_CACHE_MASK); 167 return POL_RETVAL_SUCCESS; 168 } 169 #endif 170 171 POL_RETVAL set_cache_policy(VMM_POLICY *policy, VMM_CACHE_POLICY cache_policy) 172 { 173 // BEFORE_VMLAUNCH. PARANOID check. 174 VMM_ASSERT(policy != NULL); 175 176 BITMAP_ASSIGN64(*policy, POL_CACHE_MASK, BIT_VALUE64((int) cache_policy + POL_CACHE_FIELD_OFFS)); 177 178 return POL_RETVAL_SUCCESS; 179 } 180 181 POL_RETVAL get_cache_policy(const VMM_POLICY *policy, VMM_CACHE_POLICY *cache_policy) 182 { 183 return get_policy(policy, cache_policy, POL_CACHE_FIELD_OFFS, POL_CACHE_FIELD_SIZE, POL_CACHE_DIS_ILLEGAL); 184 } 185 186