1 /**
2  * Describes functions for converting processor-generic 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-generic.h"
13 
14 //Converts the given processor-generic CPER section into JSON IR.
15 json_object* cper_section_generic_to_ir(void* section, EFI_ERROR_SECTION_DESCRIPTOR* descriptor)
16 {
17     EFI_PROCESSOR_GENERIC_ERROR_DATA* section_generic = (EFI_PROCESSOR_GENERIC_ERROR_DATA*)section;
18     json_object* section_ir = json_object_new_object();
19 
20     //Validation bits.
21     json_object* validation = json_object_new_object();
22     json_object_object_add(validation, "processorTypeValid", json_object_new_boolean(section_generic->ValidFields >> 63));
23     json_object_object_add(validation, "processorISAValid", json_object_new_boolean((section_generic->ValidFields >> 62) & 0x1));
24     json_object_object_add(validation, "processorErrorTypeValid", json_object_new_boolean((section_generic->ValidFields >> 61) & 0x1));
25     json_object_object_add(validation, "operationValid", json_object_new_boolean((section_generic->ValidFields >> 60) & 0x1));
26     json_object_object_add(validation, "flagsValid", json_object_new_boolean((section_generic->ValidFields >> 59) & 0x1));
27     json_object_object_add(validation, "levelValid", json_object_new_boolean((section_generic->ValidFields >> 58) & 0x1));
28     json_object_object_add(validation, "cpuVersionValid", json_object_new_boolean((section_generic->ValidFields >> 57) & 0x1));
29     json_object_object_add(validation, "cpuBrandInfoValid", json_object_new_boolean((section_generic->ValidFields >> 56) & 0x1));
30     json_object_object_add(validation, "cpuIDValid", json_object_new_boolean((section_generic->ValidFields >> 55) & 0x1));
31     json_object_object_add(validation, "targetAddressValid", json_object_new_boolean((section_generic->ValidFields >> 54) & 0x1));
32     json_object_object_add(validation, "requesterIDValid", json_object_new_boolean((section_generic->ValidFields >> 53) & 0x1));
33     json_object_object_add(validation, "responderIDValid", json_object_new_boolean((section_generic->ValidFields >> 52) & 0x1));
34     json_object_object_add(validation, "instructionIPValid", json_object_new_boolean((section_generic->ValidFields >> 51) & 0x1));
35     json_object_object_add(section_ir, "validationBits", validation);
36 
37     //Processor type, with human readable name if possible.
38     json_object* processor_type = integer_to_readable_pair(section_generic->Type,
39         sizeof(GENERIC_PROC_TYPES_KEYS) / sizeof(int),
40         GENERIC_PROC_TYPES_KEYS,
41         GENERIC_PROC_TYPES_VALUES,
42         "Unknown (Reserved)");
43     json_object_object_add(section_ir, "processorType", processor_type);
44 
45     //Processor ISA, with human readable name if possible.
46     json_object* processor_isa = integer_to_readable_pair(section_generic->Isa,
47         sizeof(GENERIC_ISA_TYPES_KEYS) / sizeof(int),
48         GENERIC_ISA_TYPES_KEYS,
49         GENERIC_ISA_TYPES_VALUES,
50         "Unknown (Reserved");
51     json_object_object_add(section_ir, "processorISA", processor_isa);
52 
53     //Processor error type, with human readable name if possible.
54     json_object* processor_error_type = integer_to_readable_pair(section_generic->ErrorType,
55         sizeof(GENERIC_ERROR_TYPES_KEYS) / sizeof(int),
56         GENERIC_ERROR_TYPES_KEYS,
57         GENERIC_ERROR_TYPES_VALUES,
58         "Unknown (Reserved");
59     json_object_object_add(section_ir, "errorType", processor_error_type);
60 
61     //The operation performed, with a human readable name if possible.
62     json_object* operation = integer_to_readable_pair(section_generic->Operation,
63         sizeof(GENERIC_OPERATION_TYPES_KEYS) / sizeof(int),
64         GENERIC_OPERATION_TYPES_KEYS,
65         GENERIC_OPERATION_TYPES_VALUES,
66         "Unknown (Reserved");
67     json_object_object_add(section_ir, "operation", operation);
68 
69     //Flags, additional information about the error.
70     json_object* flags = json_object_new_object();
71     json_object_object_add(flags, "restartable", json_object_new_boolean(section_generic->Flags >> 7));
72     json_object_object_add(flags, "preciseIP", json_object_new_boolean((section_generic->Flags >> 6) & 0b1));
73     json_object_object_add(flags, "overflow", json_object_new_boolean((section_generic->Flags >> 5) & 0b1));
74     json_object_object_add(flags, "corrected", json_object_new_boolean((section_generic->Flags >> 4) & 0b1));
75     json_object_object_add(section_ir, "flags", flags);
76 
77     //The level of the error.
78     json_object_object_add(section_ir, "level", json_object_new_int(section_generic->Level));
79 
80     //CPU version information (todo)
81     //...
82 
83     //CPU brand string. May not exist if on ARM.
84     json_object_object_add(section_ir, "cpuBrandString", json_object_new_string(section_generic->BrandString));
85 
86     //Remaining 64-bit fields.
87     json_object_object_add(section_ir, "processorID", json_object_new_uint64(section_generic->ApicId));
88     json_object_object_add(section_ir, "targetAddress", json_object_new_uint64(section_generic->TargetAddr));
89     json_object_object_add(section_ir, "requestorID", json_object_new_uint64(section_generic->RequestorId));
90     json_object_object_add(section_ir, "responderID", json_object_new_uint64(section_generic->ResponderId));
91     json_object_object_add(section_ir, "instructionIP", json_object_new_uint64(section_generic->InstructionIP));
92 
93     return section_ir;
94 }