1 /** 2 * Describes functions for converting IA32/x64 CPER sections from binary and JSON format 3 * into an intermediate format. 4 * 5 * Author: Lawrence.Tang@arm.com 6 **/ 7 8 #include <stdio.h> 9 #include "json.h" 10 #include "b64.h" 11 #include "../edk/Cper.h" 12 #include "../cper-utils.h" 13 #include "cper-section-ia32x64.h" 14 15 //Private pre-definitions. 16 json_object* cper_ia32x64_processor_error_info_to_ir(EFI_IA32_X64_PROCESS_ERROR_INFO* error_info); 17 json_object* cper_ia32x64_cache_tlb_check_to_ir(EFI_IA32_X64_CACHE_CHECK_INFO* cache_tlb_check); 18 json_object* cper_ia32x64_bus_check_to_ir(EFI_IA32_X64_BUS_CHECK_INFO* bus_check); 19 json_object* cper_ia32x64_ms_check_to_ir(EFI_IA32_X64_MS_CHECK_INFO* ms_check); 20 json_object* cper_ia32x64_processor_context_info_to_ir(EFI_IA32_X64_PROCESSOR_CONTEXT_INFO* context_info, void** cur_pos); 21 json_object* cper_ia32x64_register_32bit_to_ir(EFI_CONTEXT_IA32_REGISTER_STATE* registers); 22 json_object* cper_ia32x64_register_64bit_to_ir(EFI_CONTEXT_X64_REGISTER_STATE* registers); 23 24 //Converts the IA32/x64 error section described in the given descriptor into intermediate format. 25 json_object* cper_section_ia32x64_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor) 26 { 27 EFI_IA32_X64_PROCESSOR_ERROR_RECORD* record = (EFI_IA32_X64_PROCESSOR_ERROR_RECORD*)section; 28 json_object* record_ir = json_object_new_object(); 29 30 //Flags. 31 json_object* flags = json_object_new_object(); 32 json_object_object_add(flags, "localAPICIDValid", json_object_new_boolean(record->ValidFields & 0b1)); 33 json_object_object_add(flags, "cpuIDInfoValid", json_object_new_boolean((record->ValidFields >> 1) & 0b1)); 34 int processor_error_info_num = (record->ValidFields >> 2) & 0b111111; 35 json_object_object_add(flags, "processorErrorInfoNum", json_object_new_int(processor_error_info_num)); 36 int processor_context_info_num = (record->ValidFields >> 8) & 0b111111; 37 json_object_object_add(flags, "processorContextInfoNum", json_object_new_int(processor_context_info_num)); 38 json_object_object_add(record_ir, "flags", flags); 39 40 //APIC ID. 41 json_object_object_add(record_ir, "localAPICID", json_object_new_uint64(record->ApicId)); 42 43 //CPUID information. 44 json_object* cpuid_info_ir = json_object_new_object(); 45 EFI_IA32_X64_CPU_ID* cpuid_info = (EFI_IA32_X64_CPU_ID*)record->CpuIdInfo; 46 json_object_object_add(cpuid_info_ir, "eax", json_object_new_uint64(cpuid_info->Eax)); 47 json_object_object_add(cpuid_info_ir, "ebx", json_object_new_uint64(cpuid_info->Ebx)); 48 json_object_object_add(cpuid_info_ir, "ecx", json_object_new_uint64(cpuid_info->Ecx)); 49 json_object_object_add(cpuid_info_ir, "edx", json_object_new_uint64(cpuid_info->Edx)); 50 json_object_object_add(record_ir, "cpuidInfo", cpuid_info_ir); 51 52 //Processor error information, of the amount described above. 53 EFI_IA32_X64_PROCESS_ERROR_INFO* current_error_info = (EFI_IA32_X64_PROCESS_ERROR_INFO*)(record + 1); 54 json_object* error_info_array = json_object_new_array(); 55 for (int i=0; i<processor_error_info_num; i++) 56 { 57 json_object_array_add(error_info_array, cper_ia32x64_processor_error_info_to_ir(current_error_info)); 58 current_error_info++; 59 } 60 json_object_object_add(record_ir, "processorErrorInfo", error_info_array); 61 62 //Processor context information, of the amount described above. 63 EFI_IA32_X64_PROCESSOR_CONTEXT_INFO* current_context_info = (EFI_IA32_X64_PROCESSOR_CONTEXT_INFO*)(current_error_info + 1); 64 json_object* context_info_array = json_object_new_array(); 65 for (int i=0; i<processor_context_info_num; i++) 66 { 67 json_object_array_add(context_info_array, cper_ia32x64_processor_context_info_to_ir(current_context_info, (void**)¤t_context_info)); 68 //The context array is a non-fixed size, pointer is shifted within the above function. 69 } 70 json_object_object_add(record_ir, "processorContextInfo", context_info_array); 71 72 return record_ir; 73 } 74 75 //Converts a single IA32/x64 processor error info block into JSON IR format. 76 json_object* cper_ia32x64_processor_error_info_to_ir(EFI_IA32_X64_PROCESS_ERROR_INFO* error_info) 77 { 78 json_object* error_info_ir = json_object_new_object(); 79 80 //Error structure type (as GUID). 81 char error_type[GUID_STRING_LENGTH]; 82 guid_to_string(error_type, &error_info->ErrorType); 83 json_object_object_add(error_info_ir, "type", json_object_new_string(error_type)); 84 85 //Validation bits. 86 json_object* validation = bitfield_to_ir(error_info->ValidFields, 5, IA32X64_PROCESSOR_ERROR_VALID_BITFIELD_NAMES); 87 json_object_object_add(error_info_ir, "validationBits", validation); 88 89 //Add the check information on a per-structure basis. 90 //Cache and TLB check information are identical, so can be equated. 91 json_object* checkInformation = NULL; 92 if (guid_equal(&error_info->ErrorType, &gEfiIa32x64ErrorTypeCacheCheckGuid) 93 || guid_equal(&error_info->ErrorType, &gEfiIa32x64ErrorTypeTlbCheckGuid)) 94 { 95 checkInformation = cper_ia32x64_cache_tlb_check_to_ir((EFI_IA32_X64_CACHE_CHECK_INFO*)&error_info->CheckInfo); 96 } 97 else if (guid_equal(&error_info->ErrorType, &gEfiIa32x64ErrorTypeBusCheckGuid)) 98 checkInformation = cper_ia32x64_bus_check_to_ir((EFI_IA32_X64_BUS_CHECK_INFO*)&error_info->CheckInfo); 99 else if (guid_equal(&error_info->ErrorType, &gEfiIa32x64ErrorTypeMsCheckGuid)) 100 checkInformation = cper_ia32x64_ms_check_to_ir((EFI_IA32_X64_MS_CHECK_INFO*)&error_info->CheckInfo); 101 json_object_object_add(error_info_ir, "checkInfo", checkInformation); 102 103 //Target, requestor, and responder identifiers. 104 json_object_object_add(error_info_ir, "targetIdentifier", json_object_new_uint64(error_info->TargetId)); 105 json_object_object_add(error_info_ir, "requestorIdentifier", json_object_new_uint64(error_info->RequestorId)); 106 json_object_object_add(error_info_ir, "responderIdentifier", json_object_new_uint64(error_info->ResponderId)); 107 json_object_object_add(error_info_ir, "instructionPointer", json_object_new_uint64(error_info->InstructionIP)); 108 109 return error_info_ir; 110 } 111 112 //Converts a single IA32/x64 cache or TLB check check info block into JSON IR format. 113 json_object* cper_ia32x64_cache_tlb_check_to_ir(EFI_IA32_X64_CACHE_CHECK_INFO* cache_tlb_check) 114 { 115 json_object* cache_tlb_check_ir = json_object_new_object(); 116 117 //Validation bits. 118 json_object* validation = bitfield_to_ir(cache_tlb_check->ValidFields, 8, IA32X64_CHECK_INFO_VALID_BITFIELD_NAMES); 119 json_object_object_add(cache_tlb_check_ir, "validationBits", validation); 120 121 //Transaction type. 122 json_object* transaction_type = integer_to_readable_pair(cache_tlb_check->TransactionType, 3, 123 IA32X64_CHECK_INFO_TRANSACTION_TYPES_KEYS, 124 IA32X64_CHECK_INFO_TRANSACTION_TYPES_VALUES, 125 "Unknown (Reserved)"); 126 json_object_object_add(cache_tlb_check_ir, "transactionType", transaction_type); 127 128 //Operation. 129 json_object* operation = integer_to_readable_pair(cache_tlb_check->Operation, 9, 130 IA32X64_CHECK_INFO_OPERATION_TYPES_KEYS, 131 IA32X64_CHECK_INFO_OPERATION_TYPES_VALUES, 132 "Unknown (Reserved)"); 133 json_object_object_add(cache_tlb_check_ir, "operation", operation); 134 135 //Affected cache/TLB level. 136 json_object_object_add(cache_tlb_check_ir, "level", json_object_new_uint64(cache_tlb_check->Level)); 137 138 //Miscellaneous boolean fields. 139 json_object_object_add(cache_tlb_check_ir, "processorContextCorrupt", json_object_new_boolean(cache_tlb_check->ContextCorrupt)); 140 json_object_object_add(cache_tlb_check_ir, "uncorrected", json_object_new_boolean(cache_tlb_check->ErrorUncorrected)); 141 json_object_object_add(cache_tlb_check_ir, "preciseIP", json_object_new_boolean(cache_tlb_check->PreciseIp)); 142 json_object_object_add(cache_tlb_check_ir, "restartableIP", json_object_new_boolean(cache_tlb_check->RestartableIp)); 143 json_object_object_add(cache_tlb_check_ir, "overflow", json_object_new_boolean(cache_tlb_check->Overflow)); 144 145 return cache_tlb_check_ir; 146 } 147 148 //Converts a single IA32/x64 bus check check info block into JSON IR format. 149 json_object* cper_ia32x64_bus_check_to_ir(EFI_IA32_X64_BUS_CHECK_INFO* bus_check) 150 { 151 json_object* bus_check_ir = json_object_new_object(); 152 153 //Validation bits. 154 json_object* validation = bitfield_to_ir(bus_check->ValidFields, 11, IA32X64_CHECK_INFO_VALID_BITFIELD_NAMES); 155 json_object_object_add(bus_check_ir, "validationBits", validation); 156 157 //Transaction type. 158 json_object* transaction_type = integer_to_readable_pair(bus_check->TransactionType, 3, 159 IA32X64_CHECK_INFO_TRANSACTION_TYPES_KEYS, 160 IA32X64_CHECK_INFO_TRANSACTION_TYPES_VALUES, 161 "Unknown (Reserved)"); 162 json_object_object_add(bus_check_ir, "transactionType", transaction_type); 163 164 //Operation. 165 json_object* operation = integer_to_readable_pair(bus_check->Operation, 9, 166 IA32X64_CHECK_INFO_OPERATION_TYPES_KEYS, 167 IA32X64_CHECK_INFO_OPERATION_TYPES_VALUES, 168 "Unknown (Reserved)"); 169 json_object_object_add(bus_check_ir, "operation", operation); 170 171 //Affected bus level. 172 json_object_object_add(bus_check_ir, "level", json_object_new_uint64(bus_check->Level)); 173 174 //Miscellaneous boolean fields. 175 json_object_object_add(bus_check_ir, "processorContextCorrupt", json_object_new_boolean(bus_check->ContextCorrupt)); 176 json_object_object_add(bus_check_ir, "uncorrected", json_object_new_boolean(bus_check->ErrorUncorrected)); 177 json_object_object_add(bus_check_ir, "preciseIP", json_object_new_boolean(bus_check->PreciseIp)); 178 json_object_object_add(bus_check_ir, "restartableIP", json_object_new_boolean(bus_check->RestartableIp)); 179 json_object_object_add(bus_check_ir, "overflow", json_object_new_boolean(bus_check->Overflow)); 180 json_object_object_add(bus_check_ir, "timedOut", json_object_new_boolean(bus_check->TimeOut)); 181 182 //Participation type. 183 json_object* participation_type = integer_to_readable_pair(bus_check->ParticipationType, 4, 184 IA32X64_BUS_CHECK_INFO_PARTICIPATION_TYPES_KEYS, 185 IA32X64_BUS_CHECK_INFO_PARTICIPATION_TYPES_VALUES, 186 "Unknown"); 187 json_object_object_add(bus_check_ir, "participationType", participation_type); 188 189 //Address space. 190 json_object* address_space = integer_to_readable_pair(bus_check->AddressSpace, 4, 191 IA32X64_BUS_CHECK_INFO_ADDRESS_SPACE_TYPES_KEYS, 192 IA32X64_BUS_CHECK_INFO_ADDRESS_SPACE_TYPES_VALUES, 193 "Unknown"); 194 json_object_object_add(bus_check_ir, "addressSpace", address_space); 195 196 return bus_check_ir; 197 } 198 199 //Converts a single IA32/x64 MS check check info block into JSON IR format. 200 json_object* cper_ia32x64_ms_check_to_ir(EFI_IA32_X64_MS_CHECK_INFO* ms_check) 201 { 202 json_object* ms_check_ir = json_object_new_object(); 203 204 //Validation bits. 205 json_object* validation = bitfield_to_ir(ms_check->ValidFields, 6, IA32X64_CHECK_INFO_VALID_BITFIELD_NAMES); 206 json_object_object_add(ms_check_ir, "validationBits", validation); 207 208 //Error type (operation that caused the error). 209 json_object* error_type = integer_to_readable_pair(ms_check->ErrorType, 4, 210 IA32X64_MS_CHECK_INFO_ERROR_TYPES_KEYS, 211 IA32X64_MS_CHECK_INFO_ERROR_TYPES_VALUES, 212 "Unknown (Processor Specific)"); 213 json_object_object_add(ms_check_ir, "errorType", error_type); 214 215 //Miscellaneous fields. 216 json_object_object_add(ms_check_ir, "processorContextCorrupt", json_object_new_boolean(ms_check->ContextCorrupt)); 217 json_object_object_add(ms_check_ir, "uncorrected", json_object_new_boolean(ms_check->ErrorUncorrected)); 218 json_object_object_add(ms_check_ir, "preciseIP", json_object_new_boolean(ms_check->PreciseIp)); 219 json_object_object_add(ms_check_ir, "restartableIP", json_object_new_boolean(ms_check->RestartableIp)); 220 json_object_object_add(ms_check_ir, "overflow", json_object_new_boolean(ms_check->Overflow)); 221 222 return ms_check_ir; 223 } 224 225 //Converts a single IA32/x64 processor context info entry into JSON IR format. 226 json_object* cper_ia32x64_processor_context_info_to_ir(EFI_IA32_X64_PROCESSOR_CONTEXT_INFO* context_info, void** cur_pos) 227 { 228 json_object* context_info_ir = json_object_new_object(); 229 230 //Register context type. 231 json_object* context_type = integer_to_readable_pair(context_info->RegisterType, 8, 232 IA32X64_REGISTER_CONTEXT_TYPES_KEYS, 233 IA32X64_REGISTER_CONTEXT_TYPES_VALUES, 234 "Unknown (Reserved)"); 235 json_object_object_add(context_info_ir, "registerContextType", context_type); 236 237 //Register array size, MSR and MM address. 238 json_object_object_add(context_info_ir, "registerArraySize", json_object_new_uint64(context_info->ArraySize)); 239 json_object_object_add(context_info_ir, "msrAddress", json_object_new_uint64(context_info->MsrAddress)); 240 json_object_object_add(context_info_ir, "mmRegisterAddress", json_object_new_uint64(context_info->MmRegisterAddress)); 241 242 //Register array. 243 json_object* register_array = NULL; 244 if (context_info->RegisterType == 2) 245 { 246 EFI_CONTEXT_IA32_REGISTER_STATE* register_state = (EFI_CONTEXT_IA32_REGISTER_STATE*)(context_info + 1); 247 register_array = cper_ia32x64_register_32bit_to_ir(register_state); 248 *cur_pos = (void*)(register_state + 1); 249 } 250 else if (context_info->RegisterType == 3) 251 { 252 EFI_CONTEXT_X64_REGISTER_STATE* register_state = (EFI_CONTEXT_X64_REGISTER_STATE*)(context_info + 1); 253 register_array = cper_ia32x64_register_64bit_to_ir(register_state); 254 *cur_pos = (void*)(register_state + 1); 255 } 256 else 257 { 258 //No parseable data, just dump as base64 and shift the head to the next item. 259 *cur_pos = (void*)(context_info + 1); 260 261 char* encoded = b64_encode((unsigned char*)cur_pos, context_info->ArraySize); 262 register_array = json_object_new_object(); 263 json_object_object_add(register_array, "data", json_object_new_string(encoded)); 264 free(encoded); 265 266 *cur_pos = (void*)(((char*)*cur_pos) + context_info->ArraySize); 267 } 268 json_object_object_add(context_info_ir, "registerArray", register_array); 269 270 return context_info_ir; 271 } 272 273 //Converts a single CPER IA32 register state into JSON IR format. 274 json_object* cper_ia32x64_register_32bit_to_ir(EFI_CONTEXT_IA32_REGISTER_STATE* registers) 275 { 276 json_object* ia32_registers = json_object_new_object(); 277 json_object_object_add(ia32_registers, "eax", json_object_new_int(registers->Eax)); 278 json_object_object_add(ia32_registers, "ebx", json_object_new_int(registers->Ebx)); 279 json_object_object_add(ia32_registers, "ecx", json_object_new_int(registers->Ecx)); 280 json_object_object_add(ia32_registers, "edx", json_object_new_int(registers->Edx)); 281 json_object_object_add(ia32_registers, "esi", json_object_new_int(registers->Esi)); 282 json_object_object_add(ia32_registers, "edi", json_object_new_int(registers->Edi)); 283 json_object_object_add(ia32_registers, "ebp", json_object_new_int(registers->Ebp)); 284 json_object_object_add(ia32_registers, "esp", json_object_new_int(registers->Esp)); 285 json_object_object_add(ia32_registers, "cs", json_object_new_int(registers->Cs)); 286 json_object_object_add(ia32_registers, "ds", json_object_new_int(registers->Ds)); 287 json_object_object_add(ia32_registers, "ss", json_object_new_int(registers->Ss)); 288 json_object_object_add(ia32_registers, "es", json_object_new_int(registers->Es)); 289 json_object_object_add(ia32_registers, "fs", json_object_new_int(registers->Fs)); 290 json_object_object_add(ia32_registers, "gs", json_object_new_int(registers->Gs)); 291 json_object_object_add(ia32_registers, "eflags", json_object_new_int(registers->Eflags)); 292 json_object_object_add(ia32_registers, "eip", json_object_new_int(registers->Eip)); 293 json_object_object_add(ia32_registers, "cr0", json_object_new_int(registers->Cr0)); 294 json_object_object_add(ia32_registers, "cr1", json_object_new_int(registers->Cr1)); 295 json_object_object_add(ia32_registers, "cr2", json_object_new_int(registers->Cr2)); 296 json_object_object_add(ia32_registers, "cr3", json_object_new_int(registers->Cr3)); 297 json_object_object_add(ia32_registers, "cr4", json_object_new_int(registers->Cr4)); 298 json_object_object_add(ia32_registers, "gdtr", json_object_new_uint64((registers->Gdtr[0] << 16) + registers->Gdtr[1])); 299 json_object_object_add(ia32_registers, "idtr", json_object_new_uint64((registers->Idtr[0] << 16) + registers->Idtr[1])); 300 json_object_object_add(ia32_registers, "ldtr", json_object_new_int(registers->Ldtr)); 301 json_object_object_add(ia32_registers, "tr", json_object_new_int(registers->Tr)); 302 303 return ia32_registers; 304 } 305 306 //Converts a single CPER x64 register state into JSON IR format. 307 json_object* cper_ia32x64_register_64bit_to_ir(EFI_CONTEXT_X64_REGISTER_STATE* registers) 308 { 309 json_object* x64_registers = json_object_new_object(); 310 json_object_object_add(x64_registers, "rax", json_object_new_uint64(registers->Rax)); 311 json_object_object_add(x64_registers, "rbx", json_object_new_uint64(registers->Rbx)); 312 json_object_object_add(x64_registers, "rcx", json_object_new_uint64(registers->Rcx)); 313 json_object_object_add(x64_registers, "rdx", json_object_new_uint64(registers->Rdx)); 314 json_object_object_add(x64_registers, "rsi", json_object_new_uint64(registers->Rsi)); 315 json_object_object_add(x64_registers, "rdi", json_object_new_uint64(registers->Rdi)); 316 json_object_object_add(x64_registers, "rbp", json_object_new_uint64(registers->Rbp)); 317 json_object_object_add(x64_registers, "rsp", json_object_new_uint64(registers->Rsp)); 318 json_object_object_add(x64_registers, "r8", json_object_new_uint64(registers->R8)); 319 json_object_object_add(x64_registers, "r9", json_object_new_uint64(registers->R9)); 320 json_object_object_add(x64_registers, "r10", json_object_new_uint64(registers->R10)); 321 json_object_object_add(x64_registers, "r11", json_object_new_uint64(registers->R11)); 322 json_object_object_add(x64_registers, "r12", json_object_new_uint64(registers->R12)); 323 json_object_object_add(x64_registers, "r13", json_object_new_uint64(registers->R13)); 324 json_object_object_add(x64_registers, "r14", json_object_new_uint64(registers->R14)); 325 json_object_object_add(x64_registers, "r15", json_object_new_uint64(registers->R15)); 326 json_object_object_add(x64_registers, "cs", json_object_new_int(registers->Cs)); 327 json_object_object_add(x64_registers, "ds", json_object_new_int(registers->Ds)); 328 json_object_object_add(x64_registers, "ss", json_object_new_int(registers->Ss)); 329 json_object_object_add(x64_registers, "es", json_object_new_int(registers->Es)); 330 json_object_object_add(x64_registers, "fs", json_object_new_int(registers->Fs)); 331 json_object_object_add(x64_registers, "gs", json_object_new_int(registers->Gs)); 332 json_object_object_add(x64_registers, "rflags", json_object_new_uint64(registers->Rflags)); 333 json_object_object_add(x64_registers, "eip", json_object_new_uint64(registers->Rip)); 334 json_object_object_add(x64_registers, "cr0", json_object_new_uint64(registers->Cr0)); 335 json_object_object_add(x64_registers, "cr1", json_object_new_uint64(registers->Cr1)); 336 json_object_object_add(x64_registers, "cr2", json_object_new_uint64(registers->Cr2)); 337 json_object_object_add(x64_registers, "cr3", json_object_new_uint64(registers->Cr3)); 338 json_object_object_add(x64_registers, "cr4", json_object_new_uint64(registers->Cr4)); 339 json_object_object_add(x64_registers, "cr8", json_object_new_uint64(registers->Cr8)); 340 json_object_object_add(x64_registers, "gdtr_0", json_object_new_uint64(registers->Gdtr[0])); 341 json_object_object_add(x64_registers, "gdtr_1", json_object_new_uint64(registers->Gdtr[1])); 342 json_object_object_add(x64_registers, "idtr_0", json_object_new_uint64(registers->Idtr[0])); 343 json_object_object_add(x64_registers, "idtr_1", json_object_new_uint64(registers->Idtr[1])); 344 json_object_object_add(x64_registers, "ldtr", json_object_new_int(registers->Ldtr)); 345 json_object_object_add(x64_registers, "tr", json_object_new_int(registers->Tr)); 346 347 return x64_registers; 348 }