1 /**
2  * Describes functions for converting ARM 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-arm.h"
14 
15 //Private pre-definitions.
16 json_object* cper_arm_error_info_to_ir(EFI_ARM_ERROR_INFORMATION_ENTRY* error_info);
17 json_object* cper_arm_processor_context_to_ir(EFI_ARM_CONTEXT_INFORMATION_HEADER* header, void** cur_pos);
18 json_object* cper_arm_cache_tlb_error_to_ir(EFI_ARM_CACHE_ERROR_STRUCTURE* cache_tlb_error, EFI_ARM_ERROR_INFORMATION_ENTRY* error_info);
19 json_object* cper_arm_bus_error_to_ir(EFI_ARM_BUS_ERROR_STRUCTURE* bus_error);
20 json_object* cper_arm_misc_register_array_to_ir(EFI_ARM_MISC_CONTEXT_REGISTER* misc_register);
21 
22 //Converts the given processor-generic CPER section into JSON IR.
23 json_object* cper_section_arm_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor)
24 {
25     EFI_ARM_ERROR_RECORD* record = (EFI_ARM_ERROR_RECORD*)section;
26     json_object* section_ir = json_object_new_object();
27 
28     //Validation bits.
29     json_object* validation = bitfield_to_ir(record->ValidFields, 4, ARM_ERROR_VALID_BITFIELD_NAMES);
30     json_object_object_add(section_ir, "validationBits", validation);
31 
32     //Number of error info and context info structures, and length.
33     json_object_object_add(section_ir, "errorInfoNum", json_object_new_int(record->ErrInfoNum));
34     json_object_object_add(section_ir, "contextInfoNum", json_object_new_int(record->ContextInfoNum));
35     json_object_object_add(section_ir, "sectionLength", json_object_new_int(record->SectionLength));
36 
37     //Error affinity.
38     json_object* error_affinity = json_object_new_object();
39     json_object_object_add(error_affinity, "value", json_object_new_int(record->ErrorAffinityLevel));
40     json_object_object_add(error_affinity, "type",
41         json_object_new_string(record->ErrorAffinityLevel < 4 ? "Vendor Defined" : "Reserved"));
42     json_object_object_add(section_ir, "errorAffinity", error_affinity);
43 
44     //Processor ID (MPIDR_EL1) and chip ID (MIDR_EL1).
45     json_object_object_add(section_ir, "mpidrEl1", json_object_new_uint64(record->MPIDR_EL1));
46     json_object_object_add(section_ir, "midrEl1", json_object_new_uint64(record->MIDR_EL1));
47 
48     //Whether the processor is running, and the state of it if so.
49     json_object_object_add(section_ir, "running", json_object_new_boolean(record->RunningState));
50     if (record->RunningState >> 31)
51     {
52         //Bit 32 of running state is on, so PSCI state information is included.
53         //This can't be made human readable, as it is unknown whether this will be the pre-PSCI 1.0 format
54         //or the newer Extended StateID format.
55         json_object_object_add(section_ir, "psciState", json_object_new_int(record->PsciState));
56     }
57 
58     //Processor error structures.
59     json_object* error_info_array = json_object_new_array();
60     EFI_ARM_ERROR_INFORMATION_ENTRY* cur_error = (EFI_ARM_ERROR_INFORMATION_ENTRY*)(record + 1);
61     for (int i=0; i<record->ErrInfoNum; i++)
62     {
63         json_object_array_add(error_info_array, cper_arm_error_info_to_ir(cur_error));
64         cur_error++;
65     }
66     json_object_object_add(section_ir, "errorInfo", error_info_array);
67 
68     //Processor context structures.
69     //The current position is moved within the processing, as it is a dynamic size structure.
70     void* cur_pos = (void*)cur_error;
71     EFI_ARM_CONTEXT_INFORMATION_HEADER* header = (EFI_ARM_CONTEXT_INFORMATION_HEADER*)cur_error;
72     json_object* processor_context = cper_arm_processor_context_to_ir(header, &cur_pos);
73 
74     //Is there any vendor-specific information following?
75     if (cur_pos < section + record->SectionLength)
76     {
77         json_object* vendor_specific = json_object_new_object();
78         char* encoded = b64_encode((unsigned char*)cur_pos, section + record->SectionLength - cur_pos);
79         json_object_object_add(vendor_specific, "data", json_object_new_string(encoded));
80         free(encoded);
81 
82         json_object_object_add(section_ir, "vendorSpecificInfo", vendor_specific);
83     }
84 
85     return section_ir;
86 }
87 
88 //Converts a single ARM Process Error Information structure into JSON IR.
89 json_object* cper_arm_error_info_to_ir(EFI_ARM_ERROR_INFORMATION_ENTRY* error_info)
90 {
91     json_object* error_info_ir = json_object_new_object();
92 
93     //Version, length.
94     json_object_object_add(error_info_ir, "version", json_object_new_int(error_info->Version));
95     json_object_object_add(error_info_ir, "length", json_object_new_int(error_info->Length));
96 
97     //Validation bitfield.
98     json_object* validation = bitfield_to_ir(error_info->ValidationBits, 5, ARM_ERROR_INFO_ENTRY_VALID_BITFIELD_NAMES);
99     json_object_object_add(error_info_ir, "validationBits", validation);
100 
101     //The type of error information in this log.
102     json_object* error_type = integer_to_readable_pair(error_info->Type, 4,
103         ARM_ERROR_INFO_ENTRY_INFO_TYPES_KEYS,
104         ARM_ERROR_INFO_ENTRY_INFO_TYPES_VALUES,
105         "Unknown (Reserved)");
106     json_object_object_add(error_info_ir, "errorType", error_type);
107 
108     //Multiple error count.
109     json_object* multiple_error = json_object_new_object();
110     json_object_object_add(multiple_error, "value", json_object_new_int(error_info->MultipleError));
111     json_object_object_add(multiple_error, "type",
112         json_object_new_string(error_info->MultipleError < 1 ? "Single Error" : "Multiple Errors"));
113     json_object_object_add(error_info_ir, "multipleError", multiple_error);
114 
115     //Flags.
116     json_object* flags = bitfield_to_ir(error_info->Flags, 4, ARM_ERROR_INFO_ENTRY_FLAGS_NAMES);
117     json_object_object_add(error_info_ir, "flags", flags);
118 
119     //Error information, split by type.
120     json_object* error_subinfo = NULL;
121     switch (error_info->Type)
122     {
123         case 0: //Cache
124         case 1: //TLB
125             error_subinfo = cper_arm_cache_tlb_error_to_ir((EFI_ARM_CACHE_ERROR_STRUCTURE*)&error_info->ErrorInformation, error_info);
126             break;
127         case 2: //Bus
128             error_subinfo = cper_arm_bus_error_to_ir((EFI_ARM_BUS_ERROR_STRUCTURE*)&error_info->ErrorInformation);
129             break;
130     }
131     json_object_object_add(error_info_ir, "errorInformation", error_subinfo);
132 
133     //Virtual fault address, physical fault address.
134     json_object_object_add(error_info_ir, "virtualFaultAddress", json_object_new_uint64(error_info->VirtualFaultAddress));
135     json_object_object_add(error_info_ir, "physicalFaultAddress", json_object_new_uint64(error_info->PhysicalFaultAddress));
136 
137     return error_info_ir;
138 }
139 
140 //Converts a single ARM cache/TLB error information structure into JSON IR format.
141 json_object* cper_arm_cache_tlb_error_to_ir(EFI_ARM_CACHE_ERROR_STRUCTURE* cache_tlb_error, EFI_ARM_ERROR_INFORMATION_ENTRY* error_info)
142 {
143     json_object* cache_tlb_error_ir = json_object_new_object();
144 
145     //Validation bitfield.
146     json_object* validation = bitfield_to_ir(cache_tlb_error->ValidationBits, 7, ARM_CACHE_TLB_ERROR_VALID_BITFIELD_NAMES);
147     json_object_object_add(cache_tlb_error_ir, "validationBits", validation);
148 
149     //Transaction type.
150     json_object* transaction_type = integer_to_readable_pair(cache_tlb_error->TransactionType, 3,
151         ARM_ERROR_TRANSACTION_TYPES_KEYS,
152         ARM_ERROR_TRANSACTION_TYPES_VALUES,
153         "Unknown (Reserved)");
154     json_object_object_add(cache_tlb_error_ir, "transactionType", transaction_type);
155 
156     //Operation.
157     json_object* operation;
158     if (error_info->Type == 0)
159     {
160         //Cache operation.
161         operation = integer_to_readable_pair(cache_tlb_error->Operation, 11,
162             ARM_CACHE_BUS_OPERATION_TYPES_KEYS,
163             ARM_CACHE_BUS_OPERATION_TYPES_VALUES,
164             "Unknown (Reserved)");
165     }
166     else
167     {
168         //TLB operation.
169         operation = integer_to_readable_pair(cache_tlb_error->Operation, 9,
170             ARM_TLB_OPERATION_TYPES_KEYS,
171             ARM_TLB_OPERATION_TYPES_VALUES,
172             "Unknown (Reserved)");
173     }
174     json_object_object_add(cache_tlb_error_ir, "operation", operation);
175 
176     //Miscellaneous remaining fields.
177     json_object_object_add(cache_tlb_error_ir, "level", json_object_new_int(cache_tlb_error->Level));
178     json_object_object_add(cache_tlb_error_ir, "processorContextCorrupt", json_object_new_boolean(cache_tlb_error->ProcessorContextCorrupt));
179     json_object_object_add(cache_tlb_error_ir, "corrected", json_object_new_boolean(cache_tlb_error->Corrected));
180     json_object_object_add(cache_tlb_error_ir, "precisePC", json_object_new_boolean(cache_tlb_error->PrecisePC));
181     json_object_object_add(cache_tlb_error_ir, "restartablePC", json_object_new_boolean(cache_tlb_error->RestartablePC));
182     return cache_tlb_error_ir;
183 }
184 
185 //Converts a single ARM bus error information structure into JSON IR format.
186 json_object* cper_arm_bus_error_to_ir(EFI_ARM_BUS_ERROR_STRUCTURE* bus_error)
187 {
188     json_object* bus_error_ir = json_object_new_object();
189 
190     //Validation bits.
191     json_object* validation = bitfield_to_ir(bus_error->ValidationBits, 7, ARM_BUS_ERROR_VALID_BITFIELD_NAMES);
192     json_object_object_add(bus_error_ir, "validationBits", validation);
193 
194     //Transaction type.
195     json_object* transaction_type = integer_to_readable_pair(bus_error->TransactionType, 3,
196         ARM_ERROR_TRANSACTION_TYPES_KEYS,
197         ARM_ERROR_TRANSACTION_TYPES_VALUES,
198         "Unknown (Reserved)");
199     json_object_object_add(bus_error_ir, "transactionType", transaction_type);
200 
201     //Operation.
202     json_object* operation = integer_to_readable_pair(bus_error->Operation, 7,
203         ARM_CACHE_BUS_OPERATION_TYPES_KEYS,
204         ARM_CACHE_BUS_OPERATION_TYPES_VALUES,
205         "Unknown (Reserved)");
206     json_object_object_add(bus_error_ir, "operation", operation);
207 
208     //Affinity level of bus error, + miscellaneous fields.
209     json_object_object_add(bus_error_ir, "level", json_object_new_int(bus_error->Level));
210     json_object_object_add(bus_error_ir, "processorContextCorrupt", json_object_new_boolean(bus_error->ProcessorContextCorrupt));
211     json_object_object_add(bus_error_ir, "corrected", json_object_new_boolean(bus_error->Corrected));
212     json_object_object_add(bus_error_ir, "precisePC", json_object_new_boolean(bus_error->PrecisePC));
213     json_object_object_add(bus_error_ir, "restartablePC", json_object_new_boolean(bus_error->RestartablePC));
214     json_object_object_add(bus_error_ir, "timedOut", json_object_new_boolean(bus_error->TimeOut));
215 
216     //Participation type.
217     json_object* participation_type = integer_to_readable_pair(bus_error->ParticipationType, 4,
218         ARM_BUS_PARTICIPATION_TYPES_KEYS,
219         ARM_BUS_PARTICIPATION_TYPES_VALUES,
220         "Unknown");
221     json_object_object_add(bus_error_ir, "participationType", participation_type);
222 
223     //Address space.
224     json_object* address_space = integer_to_readable_pair(bus_error->AddressSpace, 3,
225         ARM_BUS_ADDRESS_SPACE_TYPES_KEYS,
226         ARM_BUS_ADDRESS_SPACE_TYPES_VALUES,
227         "Unknown");
228     json_object_object_add(bus_error_ir, "addressSpace", address_space);
229 
230     //Memory access attributes.
231     //todo: find the specification of these in the ARM ARM
232     //...
233 
234     //Access Mode
235     json_object* access_mode = json_object_new_object();
236     json_object_object_add(access_mode, "value", json_object_new_int(bus_error->AccessMode));
237     json_object_object_add(access_mode, "name", json_object_new_string(bus_error->AccessMode == 0 ? "Secure" : "Normal"));
238     json_object_object_add(bus_error_ir, "accessMode", access_mode);
239 
240     return bus_error_ir;
241 }
242 
243 //Converts a single ARM processor context block into JSON IR.
244 json_object* cper_arm_processor_context_to_ir(EFI_ARM_CONTEXT_INFORMATION_HEADER* header, void** cur_pos)
245 {
246     json_object* context_ir = json_object_new_object();
247 
248     //Add the context type.
249     json_object* context_type = integer_to_readable_pair(header->RegisterContextType, 9,
250         ARM_PROCESSOR_INFO_REGISTER_CONTEXT_TYPES_KEYS,
251         ARM_PROCESSOR_INFO_REGISTER_CONTEXT_TYPES_VALUES,
252         "Unknown (Reserved)");
253     json_object_object_add(context_ir, "registerContextType", context_type);
254 
255     //Register array size (bytes).
256     json_object_object_add(context_ir, "registerArraySize", json_object_new_uint64(header->RegisterArraySize));
257 
258     //The register array itself.
259     *cur_pos = (void*)(header + 1);
260     json_object* register_array = NULL;
261     switch (header->RegisterContextType)
262     {
263         case EFI_ARM_CONTEXT_TYPE_AARCH32_GPR:
264             register_array = uniform_struct_to_ir((UINT32*)cur_pos,
265                 sizeof(EFI_ARM_V8_AARCH32_GPR) / sizeof(UINT32), ARM_AARCH32_GPR_NAMES);
266             break;
267         case EFI_ARM_CONTEXT_TYPE_AARCH32_EL1:
268             register_array = uniform_struct_to_ir((UINT32*)cur_pos,
269                 sizeof(EFI_ARM_AARCH32_EL1_CONTEXT_REGISTERS) / sizeof(UINT32), ARM_AARCH32_EL1_REGISTER_NAMES);
270             break;
271         case EFI_ARM_CONTEXT_TYPE_AARCH32_EL2:
272             register_array = uniform_struct_to_ir((UINT32*)cur_pos,
273                 sizeof(EFI_ARM_AARCH32_EL2_CONTEXT_REGISTERS) / sizeof(UINT32), ARM_AARCH32_EL2_REGISTER_NAMES);
274             break;
275         case EFI_ARM_CONTEXT_TYPE_AARCH32_SECURE:
276             register_array = uniform_struct_to_ir((UINT32*)cur_pos,
277                 sizeof(EFI_ARM_AARCH32_SECURE_CONTEXT_REGISTERS) / sizeof(UINT32), ARM_AARCH32_SECURE_REGISTER_NAMES);
278             break;
279         case EFI_ARM_CONTEXT_TYPE_AARCH64_GPR:
280             register_array = uniform_struct64_to_ir((UINT64*)cur_pos,
281                 sizeof(EFI_ARM_V8_AARCH64_GPR) / sizeof(UINT64), ARM_AARCH64_GPR_NAMES);
282             break;
283         case EFI_ARM_CONTEXT_TYPE_AARCH64_EL1:
284             register_array = uniform_struct64_to_ir((UINT64*)cur_pos,
285                 sizeof(EFI_ARM_AARCH64_EL1_CONTEXT_REGISTERS) / sizeof(UINT64), ARM_AARCH64_EL1_REGISTER_NAMES);
286             break;
287         case EFI_ARM_CONTEXT_TYPE_AARCH64_EL2:
288             register_array = uniform_struct64_to_ir((UINT64*)cur_pos,
289                 sizeof(EFI_ARM_AARCH64_EL2_CONTEXT_REGISTERS) / sizeof(UINT64), ARM_AARCH64_EL2_REGISTER_NAMES);
290             break;
291         case EFI_ARM_CONTEXT_TYPE_AARCH64_EL3:
292             register_array = uniform_struct64_to_ir((UINT64*)cur_pos,
293                 sizeof(EFI_ARM_AARCH64_EL3_CONTEXT_REGISTERS) / sizeof(UINT64), ARM_AARCH64_EL3_REGISTER_NAMES);
294             break;
295         case EFI_ARM_CONTEXT_TYPE_MISC:
296             register_array = cper_arm_misc_register_array_to_ir((EFI_ARM_MISC_CONTEXT_REGISTER*)cur_pos);
297             break;
298         default:
299             //Unknown register array type, add as base64 data instead.
300             register_array = json_object_new_object();
301             char* encoded = b64_encode((unsigned char*)cur_pos, header->RegisterArraySize);
302             json_object_object_add(register_array, "data", json_object_new_string(encoded));
303             free(encoded);
304             break;
305     }
306 
307     //Set the current position to after the processor context structure.
308     *cur_pos = (UINT8*)(*cur_pos) + header->RegisterArraySize;
309 
310     return context_ir;
311 }
312 
313 //Converts a single CPER ARM miscellaneous register array to JSON IR format.
314 json_object* cper_arm_misc_register_array_to_ir(EFI_ARM_MISC_CONTEXT_REGISTER* misc_register)
315 {
316     json_object* register_array = json_object_new_object();
317     json_object* mrs_encoding = json_object_new_object();
318     json_object_object_add(mrs_encoding, "op2", json_object_new_int(misc_register->MrsOp2));
319     json_object_object_add(mrs_encoding, "crm", json_object_new_int(misc_register->MrsOp2));
320     json_object_object_add(mrs_encoding, "crn", json_object_new_int(misc_register->MrsOp2));
321     json_object_object_add(mrs_encoding, "op1", json_object_new_int(misc_register->MrsOp2));
322     json_object_object_add(mrs_encoding, "o0", json_object_new_int(misc_register->MrsOp2));
323     json_object_object_add(register_array, "mrsEncoding", mrs_encoding);
324     json_object_object_add(register_array, "value", json_object_new_uint64(misc_register->Value));
325 
326     return register_array;
327 }