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_uint64(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 & 0b1));
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_uint64(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     json_object* context_info_array = json_object_new_array();
72     for (int i=0; i<record->ContextInfoNum; i++)
73     {
74         EFI_ARM_CONTEXT_INFORMATION_HEADER* header = (EFI_ARM_CONTEXT_INFORMATION_HEADER*)cur_pos;
75         json_object* processor_context = cper_arm_processor_context_to_ir(header, &cur_pos);
76         json_object_array_add(context_info_array, processor_context);
77     }
78     json_object_object_add(section_ir, "contextInfo", context_info_array);
79 
80     //Is there any vendor-specific information following?
81     if (cur_pos < section + record->SectionLength)
82     {
83         json_object* vendor_specific = json_object_new_object();
84         char* encoded = b64_encode((unsigned char*)cur_pos, section + record->SectionLength - cur_pos);
85         json_object_object_add(vendor_specific, "data", json_object_new_string(encoded));
86         free(encoded);
87 
88         json_object_object_add(section_ir, "vendorSpecificInfo", vendor_specific);
89     }
90 
91     return section_ir;
92 }
93 
94 //Converts a single ARM Process Error Information structure into JSON IR.
95 json_object* cper_arm_error_info_to_ir(EFI_ARM_ERROR_INFORMATION_ENTRY* error_info)
96 {
97     json_object* error_info_ir = json_object_new_object();
98 
99     //Version, length.
100     json_object_object_add(error_info_ir, "version", json_object_new_int(error_info->Version));
101     json_object_object_add(error_info_ir, "length", json_object_new_int(error_info->Length));
102 
103     //Validation bitfield.
104     json_object* validation = bitfield_to_ir(error_info->ValidationBits, 5, ARM_ERROR_INFO_ENTRY_VALID_BITFIELD_NAMES);
105     json_object_object_add(error_info_ir, "validationBits", validation);
106 
107     //The type of error information in this log.
108     json_object* error_type = integer_to_readable_pair(error_info->Type, 4,
109         ARM_ERROR_INFO_ENTRY_INFO_TYPES_KEYS,
110         ARM_ERROR_INFO_ENTRY_INFO_TYPES_VALUES,
111         "Unknown (Reserved)");
112     json_object_object_add(error_info_ir, "errorType", error_type);
113 
114     //Multiple error count.
115     json_object* multiple_error = json_object_new_object();
116     json_object_object_add(multiple_error, "value", json_object_new_int(error_info->MultipleError));
117     json_object_object_add(multiple_error, "type",
118         json_object_new_string(error_info->MultipleError < 1 ? "Single Error" : "Multiple Errors"));
119     json_object_object_add(error_info_ir, "multipleError", multiple_error);
120 
121     //Flags.
122     json_object* flags = bitfield_to_ir(error_info->Flags, 4, ARM_ERROR_INFO_ENTRY_FLAGS_NAMES);
123     json_object_object_add(error_info_ir, "flags", flags);
124 
125     //Error information, split by type.
126     json_object* error_subinfo = NULL;
127     switch (error_info->Type)
128     {
129         case 0: //Cache
130         case 1: //TLB
131             error_subinfo = cper_arm_cache_tlb_error_to_ir((EFI_ARM_CACHE_ERROR_STRUCTURE*)&error_info->ErrorInformation, error_info);
132             break;
133         case 2: //Bus
134             error_subinfo = cper_arm_bus_error_to_ir((EFI_ARM_BUS_ERROR_STRUCTURE*)&error_info->ErrorInformation);
135             break;
136     }
137     json_object_object_add(error_info_ir, "errorInformation", error_subinfo);
138 
139     //Virtual fault address, physical fault address.
140     json_object_object_add(error_info_ir, "virtualFaultAddress", json_object_new_uint64(error_info->VirtualFaultAddress));
141     json_object_object_add(error_info_ir, "physicalFaultAddress", json_object_new_uint64(error_info->PhysicalFaultAddress));
142 
143     return error_info_ir;
144 }
145 
146 //Converts a single ARM cache/TLB error information structure into JSON IR format.
147 json_object* cper_arm_cache_tlb_error_to_ir(EFI_ARM_CACHE_ERROR_STRUCTURE* cache_tlb_error, EFI_ARM_ERROR_INFORMATION_ENTRY* error_info)
148 {
149     json_object* cache_tlb_error_ir = json_object_new_object();
150 
151     //Validation bitfield.
152     json_object* validation = bitfield_to_ir(cache_tlb_error->ValidationBits, 7, ARM_CACHE_TLB_ERROR_VALID_BITFIELD_NAMES);
153     json_object_object_add(cache_tlb_error_ir, "validationBits", validation);
154 
155     //Transaction type.
156     json_object* transaction_type = integer_to_readable_pair(cache_tlb_error->TransactionType, 3,
157         ARM_ERROR_TRANSACTION_TYPES_KEYS,
158         ARM_ERROR_TRANSACTION_TYPES_VALUES,
159         "Unknown (Reserved)");
160     json_object_object_add(cache_tlb_error_ir, "transactionType", transaction_type);
161 
162     //Operation.
163     json_object* operation;
164     if (error_info->Type == 0)
165     {
166         //Cache operation.
167         operation = integer_to_readable_pair(cache_tlb_error->Operation, 11,
168             ARM_CACHE_BUS_OPERATION_TYPES_KEYS,
169             ARM_CACHE_BUS_OPERATION_TYPES_VALUES,
170             "Unknown (Reserved)");
171     }
172     else
173     {
174         //TLB operation.
175         operation = integer_to_readable_pair(cache_tlb_error->Operation, 9,
176             ARM_TLB_OPERATION_TYPES_KEYS,
177             ARM_TLB_OPERATION_TYPES_VALUES,
178             "Unknown (Reserved)");
179     }
180     json_object_object_add(cache_tlb_error_ir, "operation", operation);
181 
182     //Miscellaneous remaining fields.
183     json_object_object_add(cache_tlb_error_ir, "level", json_object_new_int(cache_tlb_error->Level));
184     json_object_object_add(cache_tlb_error_ir, "processorContextCorrupt", json_object_new_boolean(cache_tlb_error->ProcessorContextCorrupt));
185     json_object_object_add(cache_tlb_error_ir, "corrected", json_object_new_boolean(cache_tlb_error->Corrected));
186     json_object_object_add(cache_tlb_error_ir, "precisePC", json_object_new_boolean(cache_tlb_error->PrecisePC));
187     json_object_object_add(cache_tlb_error_ir, "restartablePC", json_object_new_boolean(cache_tlb_error->RestartablePC));
188     return cache_tlb_error_ir;
189 }
190 
191 //Converts a single ARM bus error information structure into JSON IR format.
192 json_object* cper_arm_bus_error_to_ir(EFI_ARM_BUS_ERROR_STRUCTURE* bus_error)
193 {
194     json_object* bus_error_ir = json_object_new_object();
195 
196     //Validation bits.
197     json_object* validation = bitfield_to_ir(bus_error->ValidationBits, 7, ARM_BUS_ERROR_VALID_BITFIELD_NAMES);
198     json_object_object_add(bus_error_ir, "validationBits", validation);
199 
200     //Transaction type.
201     json_object* transaction_type = integer_to_readable_pair(bus_error->TransactionType, 3,
202         ARM_ERROR_TRANSACTION_TYPES_KEYS,
203         ARM_ERROR_TRANSACTION_TYPES_VALUES,
204         "Unknown (Reserved)");
205     json_object_object_add(bus_error_ir, "transactionType", transaction_type);
206 
207     //Operation.
208     json_object* operation = integer_to_readable_pair(bus_error->Operation, 7,
209         ARM_CACHE_BUS_OPERATION_TYPES_KEYS,
210         ARM_CACHE_BUS_OPERATION_TYPES_VALUES,
211         "Unknown (Reserved)");
212     json_object_object_add(bus_error_ir, "operation", operation);
213 
214     //Affinity level of bus error, + miscellaneous fields.
215     json_object_object_add(bus_error_ir, "level", json_object_new_int(bus_error->Level));
216     json_object_object_add(bus_error_ir, "processorContextCorrupt", json_object_new_boolean(bus_error->ProcessorContextCorrupt));
217     json_object_object_add(bus_error_ir, "corrected", json_object_new_boolean(bus_error->Corrected));
218     json_object_object_add(bus_error_ir, "precisePC", json_object_new_boolean(bus_error->PrecisePC));
219     json_object_object_add(bus_error_ir, "restartablePC", json_object_new_boolean(bus_error->RestartablePC));
220     json_object_object_add(bus_error_ir, "timedOut", json_object_new_boolean(bus_error->TimeOut));
221 
222     //Participation type.
223     json_object* participation_type = integer_to_readable_pair(bus_error->ParticipationType, 4,
224         ARM_BUS_PARTICIPATION_TYPES_KEYS,
225         ARM_BUS_PARTICIPATION_TYPES_VALUES,
226         "Unknown");
227     json_object_object_add(bus_error_ir, "participationType", participation_type);
228 
229     //Address space.
230     json_object* address_space = integer_to_readable_pair(bus_error->AddressSpace, 3,
231         ARM_BUS_ADDRESS_SPACE_TYPES_KEYS,
232         ARM_BUS_ADDRESS_SPACE_TYPES_VALUES,
233         "Unknown");
234     json_object_object_add(bus_error_ir, "addressSpace", address_space);
235 
236     //Memory access attributes.
237     //todo: find the specification of these in the ARM ARM
238     json_object_object_add(bus_error_ir, "memoryAttributes", json_object_new_int(bus_error->MemoryAddressAttributes));
239 
240     //Access Mode
241     json_object* access_mode = json_object_new_object();
242     json_object_object_add(access_mode, "value", json_object_new_int(bus_error->AccessMode));
243     json_object_object_add(access_mode, "name", json_object_new_string(bus_error->AccessMode == 0 ? "Secure" : "Normal"));
244     json_object_object_add(bus_error_ir, "accessMode", access_mode);
245 
246     return bus_error_ir;
247 }
248 
249 //Converts a single ARM processor context block into JSON IR.
250 json_object* cper_arm_processor_context_to_ir(EFI_ARM_CONTEXT_INFORMATION_HEADER* header, void** cur_pos)
251 {
252     json_object* context_ir = json_object_new_object();
253 
254     //Add the context type.
255     json_object* context_type = integer_to_readable_pair(header->RegisterContextType, 9,
256         ARM_PROCESSOR_INFO_REGISTER_CONTEXT_TYPES_KEYS,
257         ARM_PROCESSOR_INFO_REGISTER_CONTEXT_TYPES_VALUES,
258         "Unknown (Reserved)");
259     json_object_object_add(context_ir, "registerContextType", context_type);
260 
261     //Register array size (bytes).
262     json_object_object_add(context_ir, "registerArraySize", json_object_new_uint64(header->RegisterArraySize));
263 
264     //The register array itself.
265     *cur_pos = (void*)(header + 1);
266     json_object* register_array = NULL;
267     switch (header->RegisterContextType)
268     {
269         case EFI_ARM_CONTEXT_TYPE_AARCH32_GPR:
270             register_array = uniform_struct_to_ir((UINT32*)cur_pos,
271                 sizeof(EFI_ARM_V8_AARCH32_GPR) / sizeof(UINT32), ARM_AARCH32_GPR_NAMES);
272             break;
273         case EFI_ARM_CONTEXT_TYPE_AARCH32_EL1:
274             register_array = uniform_struct_to_ir((UINT32*)cur_pos,
275                 sizeof(EFI_ARM_AARCH32_EL1_CONTEXT_REGISTERS) / sizeof(UINT32), ARM_AARCH32_EL1_REGISTER_NAMES);
276             break;
277         case EFI_ARM_CONTEXT_TYPE_AARCH32_EL2:
278             register_array = uniform_struct_to_ir((UINT32*)cur_pos,
279                 sizeof(EFI_ARM_AARCH32_EL2_CONTEXT_REGISTERS) / sizeof(UINT32), ARM_AARCH32_EL2_REGISTER_NAMES);
280             break;
281         case EFI_ARM_CONTEXT_TYPE_AARCH32_SECURE:
282             register_array = uniform_struct_to_ir((UINT32*)cur_pos,
283                 sizeof(EFI_ARM_AARCH32_SECURE_CONTEXT_REGISTERS) / sizeof(UINT32), ARM_AARCH32_SECURE_REGISTER_NAMES);
284             break;
285         case EFI_ARM_CONTEXT_TYPE_AARCH64_GPR:
286             register_array = uniform_struct64_to_ir((UINT64*)cur_pos,
287                 sizeof(EFI_ARM_V8_AARCH64_GPR) / sizeof(UINT64), ARM_AARCH64_GPR_NAMES);
288             break;
289         case EFI_ARM_CONTEXT_TYPE_AARCH64_EL1:
290             register_array = uniform_struct64_to_ir((UINT64*)cur_pos,
291                 sizeof(EFI_ARM_AARCH64_EL1_CONTEXT_REGISTERS) / sizeof(UINT64), ARM_AARCH64_EL1_REGISTER_NAMES);
292             break;
293         case EFI_ARM_CONTEXT_TYPE_AARCH64_EL2:
294             register_array = uniform_struct64_to_ir((UINT64*)cur_pos,
295                 sizeof(EFI_ARM_AARCH64_EL2_CONTEXT_REGISTERS) / sizeof(UINT64), ARM_AARCH64_EL2_REGISTER_NAMES);
296             break;
297         case EFI_ARM_CONTEXT_TYPE_AARCH64_EL3:
298             register_array = uniform_struct64_to_ir((UINT64*)cur_pos,
299                 sizeof(EFI_ARM_AARCH64_EL3_CONTEXT_REGISTERS) / sizeof(UINT64), ARM_AARCH64_EL3_REGISTER_NAMES);
300             break;
301         case EFI_ARM_CONTEXT_TYPE_MISC:
302             register_array = cper_arm_misc_register_array_to_ir((EFI_ARM_MISC_CONTEXT_REGISTER*)cur_pos);
303             break;
304         default:
305             //Unknown register array type, add as base64 data instead.
306             register_array = json_object_new_object();
307             char* encoded = b64_encode((unsigned char*)cur_pos, header->RegisterArraySize);
308             json_object_object_add(register_array, "data", json_object_new_string(encoded));
309             free(encoded);
310             break;
311     }
312     json_object_object_add(context_ir, "registerArray", register_array);
313 
314     //Set the current position to after the processor context structure.
315     *cur_pos = (UINT8*)(*cur_pos) + header->RegisterArraySize;
316 
317     return context_ir;
318 }
319 
320 //Converts a single CPER ARM miscellaneous register array to JSON IR format.
321 json_object* cper_arm_misc_register_array_to_ir(EFI_ARM_MISC_CONTEXT_REGISTER* misc_register)
322 {
323     json_object* register_array = json_object_new_object();
324     json_object* mrs_encoding = json_object_new_object();
325     json_object_object_add(mrs_encoding, "op2", json_object_new_uint64(misc_register->MrsOp2));
326     json_object_object_add(mrs_encoding, "crm", json_object_new_uint64(misc_register->MrsCrm));
327     json_object_object_add(mrs_encoding, "crn", json_object_new_uint64(misc_register->MrsCrn));
328     json_object_object_add(mrs_encoding, "op1", json_object_new_uint64(misc_register->MrsOp1));
329     json_object_object_add(mrs_encoding, "o0", json_object_new_uint64(misc_register->MrsO0));
330     json_object_object_add(register_array, "mrsEncoding", mrs_encoding);
331     json_object_object_add(register_array, "value", json_object_new_uint64(misc_register->Value));
332 
333     return register_array;
334 }