blob: cfaace7ebcac1caa706c5613a2fb0173a1e0d822 [file] [log] [blame]
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +01001/**
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 Tang205dd1d2022-07-14 16:23:38 +01008#include <string.h>
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +01009#include "json.h"
Lawrence Tangce0f82b2022-07-07 16:14:28 +010010#include "b64.h"
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +010011#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.
16json_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.
Lawrence Tang0a4b3f22022-07-21 10:40:10 +010022 UINT64 oem_id = 0;
23 for (int i=0; i<6; i++)
24 oem_id |= (UINT64)vtd_error->OemId[i] << (i * 8);
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +010025 json_object_object_add(section_ir, "version", json_object_new_int(vtd_error->Version));
26 json_object_object_add(section_ir, "revision", json_object_new_int(vtd_error->Revision));
27 json_object_object_add(section_ir, "oemID", json_object_new_uint64(oem_id));
28
29 //Registers.
30 json_object_object_add(section_ir, "capabilityRegister", json_object_new_uint64(vtd_error->Capability));
31 json_object_object_add(section_ir, "extendedCapabilityRegister", json_object_new_uint64(vtd_error->CapabilityEx));
32 json_object_object_add(section_ir, "globalCommandRegister", json_object_new_uint64(vtd_error->GlobalCommand));
33 json_object_object_add(section_ir, "globalStatusRegister", json_object_new_uint64(vtd_error->GlobalStatus));
34 json_object_object_add(section_ir, "faultStatusRegister", json_object_new_uint64(vtd_error->FaultStatus));
35
Lawrence Tangce0f82b2022-07-07 16:14:28 +010036 //Fault record basic fields.
37 json_object* fault_record_ir = json_object_new_object();
38 EFI_VTD_FAULT_RECORD* fault_record = (EFI_VTD_FAULT_RECORD*)vtd_error->FaultRecord;
39 json_object_object_add(fault_record_ir, "faultInformation", json_object_new_uint64(fault_record->FaultInformation));
40 json_object_object_add(fault_record_ir, "sourceIdentifier", json_object_new_uint64(fault_record->SourceIdentifier));
41 json_object_object_add(fault_record_ir, "privelegeModeRequested",
42 json_object_new_boolean(fault_record->PrivelegeModeRequested));
43 json_object_object_add(fault_record_ir, "executePermissionRequested",
44 json_object_new_boolean(fault_record->ExecutePermissionRequested));
45 json_object_object_add(fault_record_ir, "pasidPresent", json_object_new_boolean(fault_record->PasidPresent));
46 json_object_object_add(fault_record_ir, "faultReason", json_object_new_uint64(fault_record->FaultReason));
47 json_object_object_add(fault_record_ir, "pasidValue", json_object_new_uint64(fault_record->PasidValue));
48 json_object_object_add(fault_record_ir, "addressType", json_object_new_uint64(fault_record->AddressType));
49
Lawrence Tang205dd1d2022-07-14 16:23:38 +010050 //Fault record type.
Lawrence Tangce0f82b2022-07-07 16:14:28 +010051 json_object* fault_record_type = integer_to_readable_pair(fault_record->Type, 2,
52 VTD_FAULT_RECORD_TYPES_KEYS,
53 VTD_FAULT_RECORD_TYPES_VALUES,
54 "Unknown");
55 json_object_object_add(fault_record_ir, "type", fault_record_type);
56 json_object_object_add(section_ir, "faultRecord", fault_record_ir);
57
58 //Root entry.
59 char* encoded = b64_encode((unsigned char*)vtd_error->RootEntry, 16);
60 json_object_object_add(section_ir, "rootEntry", json_object_new_string(encoded));
61 free(encoded);
62
63 //Context entry.
64 encoded = b64_encode((unsigned char*)vtd_error->ContextEntry, 16);
65 json_object_object_add(section_ir, "contextEntry", json_object_new_string(encoded));
66 free(encoded);
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +010067
68 //PTE entry for all page levels.
69 json_object_object_add(section_ir, "pageTableEntry_Level6", json_object_new_uint64(vtd_error->PteL6));
70 json_object_object_add(section_ir, "pageTableEntry_Level5", json_object_new_uint64(vtd_error->PteL5));
71 json_object_object_add(section_ir, "pageTableEntry_Level4", json_object_new_uint64(vtd_error->PteL4));
72 json_object_object_add(section_ir, "pageTableEntry_Level3", json_object_new_uint64(vtd_error->PteL3));
73 json_object_object_add(section_ir, "pageTableEntry_Level2", json_object_new_uint64(vtd_error->PteL2));
74 json_object_object_add(section_ir, "pageTableEntry_Level1", json_object_new_uint64(vtd_error->PteL1));
75
76 return section_ir;
Lawrence Tang205dd1d2022-07-14 16:23:38 +010077}
78
79//Converts a single VT-d DMAR CPER-JSON segment into CPER binary, outputting to the given stream.
80void ir_section_dmar_vtd_to_cper(json_object* section, FILE* out)
81{
82 EFI_DIRECTED_IO_DMAR_ERROR_DATA* section_cper =
83 (EFI_DIRECTED_IO_DMAR_ERROR_DATA*)calloc(1, sizeof(EFI_DIRECTED_IO_DMAR_ERROR_DATA));
84
85 //OEM ID.
86 UINT64 oem_id = json_object_get_uint64(json_object_object_get(section, "oemID"));
Lawrence Tang0a4b3f22022-07-21 10:40:10 +010087 for (int i=0; i<6; i++)
88 section_cper->OemId[i] = (oem_id >> (i * 8)) & 0xFF;
Lawrence Tang205dd1d2022-07-14 16:23:38 +010089
90 //Registers & basic numeric fields.
91 section_cper->Version = (UINT8)json_object_get_int(json_object_object_get(section, "version"));
92 section_cper->Revision = (UINT8)json_object_get_int(json_object_object_get(section, "revision"));
93 section_cper->Capability = json_object_get_uint64(json_object_object_get(section, "capabilityRegister"));
94 section_cper->CapabilityEx = json_object_get_uint64(json_object_object_get(section, "extendedCapabilityRegister"));
Lawrence Tang0a4b3f22022-07-21 10:40:10 +010095 section_cper->GlobalCommand = json_object_get_uint64(json_object_object_get(section, "globalCommandRegister"));
96 section_cper->GlobalStatus = json_object_get_uint64(json_object_object_get(section, "globalStatusRegister"));
97 section_cper->FaultStatus = json_object_get_uint64(json_object_object_get(section, "faultStatusRegister"));
Lawrence Tang205dd1d2022-07-14 16:23:38 +010098
99 //Fault record.
100 json_object* fault_record = json_object_object_get(section, "faultRecord");
101 EFI_VTD_FAULT_RECORD* fault_record_cper = (EFI_VTD_FAULT_RECORD*)section_cper->FaultRecord;
102 fault_record_cper->FaultInformation =
103 json_object_get_uint64(json_object_object_get(fault_record, "faultInformation"));
104 fault_record_cper->SourceIdentifier =
105 json_object_get_uint64(json_object_object_get(fault_record, "sourceIdentifier"));
106 fault_record_cper->PrivelegeModeRequested =
107 json_object_get_boolean(json_object_object_get(fault_record, "privelegeModeRequested"));
108 fault_record_cper->ExecutePermissionRequested =
109 json_object_get_boolean(json_object_object_get(fault_record, "executePermissionRequested"));
110 fault_record_cper->PasidPresent =
111 json_object_get_boolean(json_object_object_get(fault_record, "pasidPresent"));
112 fault_record_cper->FaultReason =
113 json_object_get_uint64(json_object_object_get(fault_record, "faultReason"));
114 fault_record_cper->PasidValue =
115 json_object_get_uint64(json_object_object_get(fault_record, "pasidValue"));
116 fault_record_cper->AddressType =
117 json_object_get_uint64(json_object_object_get(fault_record, "addressType"));
118 fault_record_cper->Type =
119 readable_pair_to_integer(json_object_object_get(fault_record, "type"));
120
121 //Root entry.
122 json_object* encoded = json_object_object_get(section, "rootEntry");
123 UINT8* decoded = b64_decode(json_object_get_string(encoded), json_object_get_string_len(encoded));
124 memcpy(section_cper->RootEntry, decoded, 16);
125 free(decoded);
126
127 //Context entry.
128 encoded = json_object_object_get(section, "contextEntry");
129 decoded = b64_decode(json_object_get_string(encoded), json_object_get_string_len(encoded));
130 memcpy(section_cper->ContextEntry, decoded, 16);
131 free(decoded);
132
133 //Page table entries.
134 section_cper->PteL1 = json_object_get_uint64(json_object_object_get(section, "pageTableEntry_Level1"));
135 section_cper->PteL2 = json_object_get_uint64(json_object_object_get(section, "pageTableEntry_Level2"));
136 section_cper->PteL3 = json_object_get_uint64(json_object_object_get(section, "pageTableEntry_Level3"));
137 section_cper->PteL4 = json_object_get_uint64(json_object_object_get(section, "pageTableEntry_Level4"));
138 section_cper->PteL5 = json_object_get_uint64(json_object_object_get(section, "pageTableEntry_Level5"));
139 section_cper->PteL6 = json_object_get_uint64(json_object_object_get(section, "pageTableEntry_Level6"));
140
141 //Write to stream, free resources.
Lawrence Tang3ab351f2022-07-20 16:09:34 +0100142 fwrite(section_cper, sizeof(EFI_DIRECTED_IO_DMAR_ERROR_DATA), 1, out);
Lawrence Tang205dd1d2022-07-14 16:23:38 +0100143 fflush(out);
144 free(section_cper);
Lawrence Tangdb1b7ce2022-07-06 15:40:26 +0100145}