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