Lawrence Tang | db1b7ce | 2022-07-06 15:40:26 +0100 | [diff] [blame] | 1 | /** |
| 2 | * Describes functions for converting VT-d specific DMAr CPER sections from binary and JSON format |
| 3 | * into an intermediate format. |
| 4 | * |
| 5 | * Author: Lawrence.Tang@arm.com |
| 6 | **/ |
| 7 | #include <stdio.h> |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 8 | #include <string.h> |
Lawrence Tang | db1b7ce | 2022-07-06 15:40:26 +0100 | [diff] [blame] | 9 | #include "json.h" |
Lawrence Tang | ce0f82b | 2022-07-07 16:14:28 +0100 | [diff] [blame] | 10 | #include "b64.h" |
Lawrence Tang | db1b7ce | 2022-07-06 15:40:26 +0100 | [diff] [blame] | 11 | #include "../edk/Cper.h" |
| 12 | #include "../cper-utils.h" |
| 13 | #include "cper-section-dmar-vtd.h" |
| 14 | |
| 15 | //Converts a single VT-d specific DMAr CPER section into JSON IR. |
| 16 | json_object* cper_section_dmar_vtd_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor) |
| 17 | { |
| 18 | EFI_DIRECTED_IO_DMAR_ERROR_DATA* vtd_error = (EFI_DIRECTED_IO_DMAR_ERROR_DATA*)section; |
| 19 | json_object* section_ir = json_object_new_object(); |
| 20 | |
| 21 | //Version, revision and OEM ID, as defined in the VT-d architecture. |
| 22 | UINT64 oem_id = (vtd_error->OemId[0] << 16) + (vtd_error->OemId[1] << 8) + vtd_error->OemId[2]; |
| 23 | json_object_object_add(section_ir, "version", json_object_new_int(vtd_error->Version)); |
| 24 | json_object_object_add(section_ir, "revision", json_object_new_int(vtd_error->Revision)); |
| 25 | json_object_object_add(section_ir, "oemID", json_object_new_uint64(oem_id)); |
| 26 | |
| 27 | //Registers. |
| 28 | json_object_object_add(section_ir, "capabilityRegister", json_object_new_uint64(vtd_error->Capability)); |
| 29 | json_object_object_add(section_ir, "extendedCapabilityRegister", json_object_new_uint64(vtd_error->CapabilityEx)); |
| 30 | json_object_object_add(section_ir, "globalCommandRegister", json_object_new_uint64(vtd_error->GlobalCommand)); |
| 31 | json_object_object_add(section_ir, "globalStatusRegister", json_object_new_uint64(vtd_error->GlobalStatus)); |
| 32 | json_object_object_add(section_ir, "faultStatusRegister", json_object_new_uint64(vtd_error->FaultStatus)); |
| 33 | |
Lawrence Tang | ce0f82b | 2022-07-07 16:14:28 +0100 | [diff] [blame] | 34 | //Fault record basic fields. |
| 35 | json_object* fault_record_ir = json_object_new_object(); |
| 36 | EFI_VTD_FAULT_RECORD* fault_record = (EFI_VTD_FAULT_RECORD*)vtd_error->FaultRecord; |
| 37 | json_object_object_add(fault_record_ir, "faultInformation", json_object_new_uint64(fault_record->FaultInformation)); |
| 38 | json_object_object_add(fault_record_ir, "sourceIdentifier", json_object_new_uint64(fault_record->SourceIdentifier)); |
| 39 | json_object_object_add(fault_record_ir, "privelegeModeRequested", |
| 40 | json_object_new_boolean(fault_record->PrivelegeModeRequested)); |
| 41 | json_object_object_add(fault_record_ir, "executePermissionRequested", |
| 42 | json_object_new_boolean(fault_record->ExecutePermissionRequested)); |
| 43 | json_object_object_add(fault_record_ir, "pasidPresent", json_object_new_boolean(fault_record->PasidPresent)); |
| 44 | json_object_object_add(fault_record_ir, "faultReason", json_object_new_uint64(fault_record->FaultReason)); |
| 45 | json_object_object_add(fault_record_ir, "pasidValue", json_object_new_uint64(fault_record->PasidValue)); |
| 46 | json_object_object_add(fault_record_ir, "addressType", json_object_new_uint64(fault_record->AddressType)); |
| 47 | |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 48 | //Fault record type. |
Lawrence Tang | ce0f82b | 2022-07-07 16:14:28 +0100 | [diff] [blame] | 49 | json_object* fault_record_type = integer_to_readable_pair(fault_record->Type, 2, |
| 50 | VTD_FAULT_RECORD_TYPES_KEYS, |
| 51 | VTD_FAULT_RECORD_TYPES_VALUES, |
| 52 | "Unknown"); |
| 53 | json_object_object_add(fault_record_ir, "type", fault_record_type); |
| 54 | json_object_object_add(section_ir, "faultRecord", fault_record_ir); |
| 55 | |
| 56 | //Root entry. |
| 57 | char* encoded = b64_encode((unsigned char*)vtd_error->RootEntry, 16); |
| 58 | json_object_object_add(section_ir, "rootEntry", json_object_new_string(encoded)); |
| 59 | free(encoded); |
| 60 | |
| 61 | //Context entry. |
| 62 | encoded = b64_encode((unsigned char*)vtd_error->ContextEntry, 16); |
| 63 | json_object_object_add(section_ir, "contextEntry", json_object_new_string(encoded)); |
| 64 | free(encoded); |
Lawrence Tang | db1b7ce | 2022-07-06 15:40:26 +0100 | [diff] [blame] | 65 | |
| 66 | //PTE entry for all page levels. |
| 67 | json_object_object_add(section_ir, "pageTableEntry_Level6", json_object_new_uint64(vtd_error->PteL6)); |
| 68 | json_object_object_add(section_ir, "pageTableEntry_Level5", json_object_new_uint64(vtd_error->PteL5)); |
| 69 | json_object_object_add(section_ir, "pageTableEntry_Level4", json_object_new_uint64(vtd_error->PteL4)); |
| 70 | json_object_object_add(section_ir, "pageTableEntry_Level3", json_object_new_uint64(vtd_error->PteL3)); |
| 71 | json_object_object_add(section_ir, "pageTableEntry_Level2", json_object_new_uint64(vtd_error->PteL2)); |
| 72 | json_object_object_add(section_ir, "pageTableEntry_Level1", json_object_new_uint64(vtd_error->PteL1)); |
| 73 | |
| 74 | return section_ir; |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | //Converts a single VT-d DMAR CPER-JSON segment into CPER binary, outputting to the given stream. |
| 78 | void ir_section_dmar_vtd_to_cper(json_object* section, FILE* out) |
| 79 | { |
| 80 | EFI_DIRECTED_IO_DMAR_ERROR_DATA* section_cper = |
| 81 | (EFI_DIRECTED_IO_DMAR_ERROR_DATA*)calloc(1, sizeof(EFI_DIRECTED_IO_DMAR_ERROR_DATA)); |
| 82 | |
| 83 | //OEM ID. |
| 84 | UINT64 oem_id = json_object_get_uint64(json_object_object_get(section, "oemID")); |
| 85 | section_cper->OemId[0] = oem_id >> 16; |
| 86 | section_cper->OemId[1] = (oem_id >> 8) & 0xFF; |
| 87 | section_cper->OemId[1] = oem_id & 0xFF; |
| 88 | |
| 89 | //Registers & basic numeric fields. |
| 90 | section_cper->Version = (UINT8)json_object_get_int(json_object_object_get(section, "version")); |
| 91 | section_cper->Revision = (UINT8)json_object_get_int(json_object_object_get(section, "revision")); |
| 92 | section_cper->Capability = json_object_get_uint64(json_object_object_get(section, "capabilityRegister")); |
| 93 | section_cper->CapabilityEx = json_object_get_uint64(json_object_object_get(section, "extendedCapabilityRegister")); |
| 94 | |
| 95 | //Fault record. |
| 96 | json_object* fault_record = json_object_object_get(section, "faultRecord"); |
| 97 | EFI_VTD_FAULT_RECORD* fault_record_cper = (EFI_VTD_FAULT_RECORD*)section_cper->FaultRecord; |
| 98 | fault_record_cper->FaultInformation = |
| 99 | json_object_get_uint64(json_object_object_get(fault_record, "faultInformation")); |
| 100 | fault_record_cper->SourceIdentifier = |
| 101 | json_object_get_uint64(json_object_object_get(fault_record, "sourceIdentifier")); |
| 102 | fault_record_cper->PrivelegeModeRequested = |
| 103 | json_object_get_boolean(json_object_object_get(fault_record, "privelegeModeRequested")); |
| 104 | fault_record_cper->ExecutePermissionRequested = |
| 105 | json_object_get_boolean(json_object_object_get(fault_record, "executePermissionRequested")); |
| 106 | fault_record_cper->PasidPresent = |
| 107 | json_object_get_boolean(json_object_object_get(fault_record, "pasidPresent")); |
| 108 | fault_record_cper->FaultReason = |
| 109 | json_object_get_uint64(json_object_object_get(fault_record, "faultReason")); |
| 110 | fault_record_cper->PasidValue = |
| 111 | json_object_get_uint64(json_object_object_get(fault_record, "pasidValue")); |
| 112 | fault_record_cper->AddressType = |
| 113 | json_object_get_uint64(json_object_object_get(fault_record, "addressType")); |
| 114 | fault_record_cper->Type = |
| 115 | readable_pair_to_integer(json_object_object_get(fault_record, "type")); |
| 116 | |
| 117 | //Root entry. |
| 118 | json_object* encoded = json_object_object_get(section, "rootEntry"); |
| 119 | UINT8* decoded = b64_decode(json_object_get_string(encoded), json_object_get_string_len(encoded)); |
| 120 | memcpy(section_cper->RootEntry, decoded, 16); |
| 121 | free(decoded); |
| 122 | |
| 123 | //Context entry. |
| 124 | encoded = json_object_object_get(section, "contextEntry"); |
| 125 | decoded = b64_decode(json_object_get_string(encoded), json_object_get_string_len(encoded)); |
| 126 | memcpy(section_cper->ContextEntry, decoded, 16); |
| 127 | free(decoded); |
| 128 | |
| 129 | //Page table entries. |
| 130 | section_cper->PteL1 = json_object_get_uint64(json_object_object_get(section, "pageTableEntry_Level1")); |
| 131 | section_cper->PteL2 = json_object_get_uint64(json_object_object_get(section, "pageTableEntry_Level2")); |
| 132 | section_cper->PteL3 = json_object_get_uint64(json_object_object_get(section, "pageTableEntry_Level3")); |
| 133 | section_cper->PteL4 = json_object_get_uint64(json_object_object_get(section, "pageTableEntry_Level4")); |
| 134 | section_cper->PteL5 = json_object_get_uint64(json_object_object_get(section, "pageTableEntry_Level5")); |
| 135 | section_cper->PteL6 = json_object_get_uint64(json_object_object_get(section, "pageTableEntry_Level6")); |
| 136 | |
| 137 | //Write to stream, free resources. |
Lawrence Tang | 3ab351f | 2022-07-20 16:09:34 +0100 | [diff] [blame^] | 138 | fwrite(section_cper, sizeof(EFI_DIRECTED_IO_DMAR_ERROR_DATA), 1, out); |
Lawrence Tang | 205dd1d | 2022-07-14 16:23:38 +0100 | [diff] [blame] | 139 | fflush(out); |
| 140 | free(section_cper); |
Lawrence Tang | db1b7ce | 2022-07-06 15:40:26 +0100 | [diff] [blame] | 141 | } |