]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Library/UefiShellAcpiViewCommandLib/Parsers/Madt/MadtParser.c
ShellPkg: acpiview: MADT: Validate global pointers before use
[mirror_edk2.git] / ShellPkg / Library / UefiShellAcpiViewCommandLib / Parsers / Madt / MadtParser.c
1 /** @file
2 MADT table parser
3
4 Copyright (c) 2016 - 2019, ARM Limited. All rights reserved.
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 @par Reference(s):
8 - ACPI 6.3 Specification - January 2019
9 - Arm Generic Interrupt Controller Architecture Specification,
10 GIC architecture version 3 and version 4, issue E
11 - Arm Server Base System Architecture 5.0
12 **/
13
14 #include <IndustryStandard/Acpi.h>
15 #include <Library/UefiLib.h>
16 #include "AcpiParser.h"
17 #include "AcpiTableParser.h"
18 #include "MadtParser.h"
19
20 // Local Variables
21 STATIC CONST UINT8* MadtInterruptControllerType;
22 STATIC CONST UINT8* MadtInterruptControllerLength;
23 STATIC ACPI_DESCRIPTION_HEADER_INFO AcpiHdrInfo;
24
25 /**
26 This function validates the System Vector Base in the GICD.
27
28 @param [in] Ptr Pointer to the start of the field data.
29 @param [in] Context Pointer to context specific information e.g. this
30 could be a pointer to the ACPI table header.
31 **/
32 STATIC
33 VOID
34 EFIAPI
35 ValidateGICDSystemVectorBase (
36 IN UINT8* Ptr,
37 IN VOID* Context
38 )
39 {
40 if (*(UINT32*)Ptr != 0) {
41 IncrementErrorCount ();
42 Print (
43 L"\nERROR: System Vector Base must be zero."
44 );
45 }
46 }
47
48 /**
49 This function validates the SPE Overflow Interrupt in the GICC.
50
51 @param [in] Ptr Pointer to the start of the field data.
52 @param [in] Context Pointer to context specific information e.g. this
53 could be a pointer to the ACPI table header.
54 **/
55 STATIC
56 VOID
57 EFIAPI
58 ValidateSpeOverflowInterrupt (
59 IN UINT8* Ptr,
60 IN VOID* Context
61 )
62 {
63 UINT16 SpeOverflowInterrupt;
64
65 SpeOverflowInterrupt = *(UINT16*)Ptr;
66
67 // SPE not supported by this processor
68 if (SpeOverflowInterrupt == 0) {
69 return;
70 }
71
72 if ((SpeOverflowInterrupt < ARM_PPI_ID_MIN) ||
73 ((SpeOverflowInterrupt > ARM_PPI_ID_MAX) &&
74 (SpeOverflowInterrupt < ARM_PPI_ID_EXTENDED_MIN)) ||
75 (SpeOverflowInterrupt > ARM_PPI_ID_EXTENDED_MAX)) {
76 IncrementErrorCount ();
77 Print (
78 L"\nERROR: SPE Overflow Interrupt ID of %d is not in the allowed PPI ID "
79 L"ranges of %d-%d or %d-%d (for GICv3.1 or later).",
80 SpeOverflowInterrupt,
81 ARM_PPI_ID_MIN,
82 ARM_PPI_ID_MAX,
83 ARM_PPI_ID_EXTENDED_MIN,
84 ARM_PPI_ID_EXTENDED_MAX
85 );
86 } else if (SpeOverflowInterrupt != ARM_PPI_ID_PMBIRQ) {
87 IncrementWarningCount();
88 Print (
89 L"\nWARNING: SPE Overflow Interrupt ID of %d is not compliant with SBSA "
90 L"Level 3 PPI ID assignment: %d.",
91 SpeOverflowInterrupt,
92 ARM_PPI_ID_PMBIRQ
93 );
94 }
95 }
96
97 /**
98 An ACPI_PARSER array describing the GICC Interrupt Controller Structure.
99 **/
100 STATIC CONST ACPI_PARSER GicCParser[] = {
101 {L"Type", 1, 0, L"0x%x", NULL, NULL, NULL, NULL},
102 {L"Length", 1, 1, L"%d", NULL, NULL, NULL, NULL},
103 {L"Reserved", 2, 2, L"0x%x", NULL, NULL, NULL, NULL},
104
105 {L"CPU Interface Number", 4, 4, L"0x%x", NULL, NULL, NULL, NULL},
106 {L"ACPI Processor UID", 4, 8, L"0x%x", NULL, NULL, NULL, NULL},
107 {L"Flags", 4, 12, L"0x%x", NULL, NULL, NULL, NULL},
108 {L"Parking Protocol Version", 4, 16, L"0x%x", NULL, NULL, NULL, NULL},
109
110 {L"Performance Interrupt GSIV", 4, 20, L"0x%x", NULL, NULL, NULL, NULL},
111 {L"Parked Address", 8, 24, L"0x%lx", NULL, NULL, NULL, NULL},
112 {L"Physical Base Address", 8, 32, L"0x%lx", NULL, NULL, NULL, NULL},
113 {L"GICV", 8, 40, L"0x%lx", NULL, NULL, NULL, NULL},
114 {L"GICH", 8, 48, L"0x%lx", NULL, NULL, NULL, NULL},
115 {L"VGIC Maintenance interrupt", 4, 56, L"0x%x", NULL, NULL, NULL, NULL},
116 {L"GICR Base Address", 8, 60, L"0x%lx", NULL, NULL, NULL, NULL},
117 {L"MPIDR", 8, 68, L"0x%lx", NULL, NULL, NULL, NULL},
118 {L"Processor Power Efficiency Class", 1, 76, L"0x%x", NULL, NULL, NULL,
119 NULL},
120 {L"Reserved", 1, 77, L"0x%x", NULL, NULL, NULL, NULL},
121 {L"SPE overflow Interrupt", 2, 78, L"0x%x", NULL, NULL,
122 ValidateSpeOverflowInterrupt, NULL}
123 };
124
125 /**
126 An ACPI_PARSER array describing the GICD Interrupt Controller Structure.
127 **/
128 STATIC CONST ACPI_PARSER GicDParser[] = {
129 {L"Type", 1, 0, L"0x%x", NULL, NULL, NULL, NULL},
130 {L"Length", 1, 1, L"%d", NULL, NULL, NULL, NULL},
131 {L"Reserved", 2, 2, L"0x%x", NULL, NULL, NULL, NULL},
132
133 {L"GIC ID", 4, 4, L"0x%x", NULL, NULL, NULL, NULL},
134 {L"Physical Base Address", 8, 8, L"0x%lx", NULL, NULL, NULL, NULL},
135 {L"System Vector Base", 4, 16, L"0x%x", NULL, NULL,
136 ValidateGICDSystemVectorBase, NULL},
137 {L"GIC Version", 1, 20, L"%d", NULL, NULL, NULL, NULL},
138 {L"Reserved", 3, 21, L"%x %x %x", Dump3Chars, NULL, NULL, NULL}
139 };
140
141 /**
142 An ACPI_PARSER array describing the MSI Frame Interrupt Controller Structure.
143 **/
144 STATIC CONST ACPI_PARSER GicMSIFrameParser[] = {
145 {L"Type", 1, 0, L"0x%x", NULL, NULL, NULL, NULL},
146 {L"Length", 1, 1, L"%d", NULL, NULL, NULL, NULL},
147 {L"Reserved", 2, 2, L"0x%x", NULL, NULL, NULL, NULL},
148
149 {L"MSI Frame ID", 4, 4, L"0x%x", NULL, NULL, NULL, NULL},
150 {L"Physical Base Address", 8, 8, L"0x%lx", NULL, NULL, NULL, NULL},
151 {L"Flags", 4, 16, L"0x%x", NULL, NULL, NULL, NULL},
152
153 {L"SPI Count", 2, 20, L"%d", NULL, NULL, NULL, NULL},
154 {L"SPI Base", 2, 22, L"0x%x", NULL, NULL, NULL, NULL}
155 };
156
157 /**
158 An ACPI_PARSER array describing the GICR Interrupt Controller Structure.
159 **/
160 STATIC CONST ACPI_PARSER GicRParser[] = {
161 {L"Type", 1, 0, L"0x%x", NULL, NULL, NULL, NULL},
162 {L"Length", 1, 1, L"%d", NULL, NULL, NULL, NULL},
163 {L"Reserved", 2, 2, L"0x%x", NULL, NULL, NULL, NULL},
164
165 {L"Discovery Range Base Address", 8, 4, L"0x%lx", NULL, NULL, NULL,
166 NULL},
167 {L"Discovery Range Length", 4, 12, L"0x%x", NULL, NULL, NULL, NULL}
168 };
169
170 /**
171 An ACPI_PARSER array describing the GIC ITS Interrupt Controller Structure.
172 **/
173 STATIC CONST ACPI_PARSER GicITSParser[] = {
174 {L"Type", 1, 0, L"0x%x", NULL, NULL, NULL, NULL},
175 {L"Length", 1, 1, L"%d", NULL, NULL, NULL, NULL},
176 {L"Reserved", 2, 2, L"0x%x", NULL, NULL, NULL, NULL},
177
178 {L"GIC ITS ID", 4, 4, L"0x%x", NULL, NULL, NULL, NULL},
179 {L"Physical Base Address", 8, 8, L"0x%lx", NULL, NULL, NULL, NULL},
180 {L"Reserved", 4, 16, L"0x%x", NULL, NULL, NULL, NULL}
181 };
182
183 /**
184 An ACPI_PARSER array describing the ACPI MADT Table.
185 **/
186 STATIC CONST ACPI_PARSER MadtParser[] = {
187 PARSE_ACPI_HEADER (&AcpiHdrInfo),
188 {L"Local Interrupt Controller Address", 4, 36, L"0x%x", NULL, NULL, NULL,
189 NULL},
190 {L"Flags", 4, 40, L"0x%x", NULL, NULL, NULL, NULL}
191 };
192
193 /**
194 An ACPI_PARSER array describing the MADT Interrupt Controller Structure Header Structure.
195 **/
196 STATIC CONST ACPI_PARSER MadtInterruptControllerHeaderParser[] = {
197 {NULL, 1, 0, NULL, NULL, (VOID**)&MadtInterruptControllerType, NULL, NULL},
198 {L"Length", 1, 1, NULL, NULL, (VOID**)&MadtInterruptControllerLength, NULL,
199 NULL},
200 {L"Reserved", 2, 2, NULL, NULL, NULL, NULL, NULL}
201 };
202
203 /**
204 This function parses the ACPI MADT table.
205 When trace is enabled this function parses the MADT table and
206 traces the ACPI table fields.
207
208 This function currently parses the following Interrupt Controller
209 Structures:
210 - GICC
211 - GICD
212 - GIC MSI Frame
213 - GICR
214 - GIC ITS
215
216 This function also performs validation of the ACPI table fields.
217
218 @param [in] Trace If TRUE, trace the ACPI fields.
219 @param [in] Ptr Pointer to the start of the buffer.
220 @param [in] AcpiTableLength Length of the ACPI table.
221 @param [in] AcpiTableRevision Revision of the ACPI table.
222 **/
223 VOID
224 EFIAPI
225 ParseAcpiMadt (
226 IN BOOLEAN Trace,
227 IN UINT8* Ptr,
228 IN UINT32 AcpiTableLength,
229 IN UINT8 AcpiTableRevision
230 )
231 {
232 UINT32 Offset;
233 UINT8* InterruptContollerPtr;
234 UINT32 GICDCount;
235
236 GICDCount = 0;
237
238 if (!Trace) {
239 return;
240 }
241
242 Offset = ParseAcpi (
243 TRUE,
244 0,
245 "MADT",
246 Ptr,
247 AcpiTableLength,
248 PARSER_PARAMS (MadtParser)
249 );
250 InterruptContollerPtr = Ptr + Offset;
251
252 while (Offset < AcpiTableLength) {
253 // Parse Interrupt Controller Structure to obtain Length.
254 ParseAcpi (
255 FALSE,
256 0,
257 NULL,
258 InterruptContollerPtr,
259 AcpiTableLength - Offset,
260 PARSER_PARAMS (MadtInterruptControllerHeaderParser)
261 );
262
263 // Check if the values used to control the parsing logic have been
264 // successfully read.
265 if ((MadtInterruptControllerType == NULL) ||
266 (MadtInterruptControllerLength == NULL)) {
267 IncrementErrorCount ();
268 Print (
269 L"ERROR: Insufficient remaining table buffer length to read the " \
270 L"Interrupt Controller Structure header. Length = %d.\n",
271 AcpiTableLength - Offset
272 );
273 return;
274 }
275
276 // Make sure forward progress is made.
277 if (*MadtInterruptControllerLength < 2) {
278 IncrementErrorCount ();
279 Print (
280 L"ERROR: Structure length is too small: " \
281 L"MadtInterruptControllerLength = %d. " \
282 L"MadtInterruptControllerType = %d. MADT parsing aborted.\n",
283 *MadtInterruptControllerLength,
284 *MadtInterruptControllerType
285 );
286 return;
287 }
288
289 // Make sure the MADT structure lies inside the table
290 if ((Offset + *MadtInterruptControllerLength) > AcpiTableLength) {
291 IncrementErrorCount ();
292 Print (
293 L"ERROR: Invalid MADT structure length. " \
294 L"MadtInterruptControllerLength = %d. " \
295 L"RemainingTableBufferLength = %d. MADT parsing aborted.\n",
296 *MadtInterruptControllerLength,
297 AcpiTableLength - Offset
298 );
299 return;
300 }
301
302 switch (*MadtInterruptControllerType) {
303 case EFI_ACPI_6_3_GIC: {
304 ParseAcpi (
305 TRUE,
306 2,
307 "GICC",
308 InterruptContollerPtr,
309 *MadtInterruptControllerLength,
310 PARSER_PARAMS (GicCParser)
311 );
312 break;
313 }
314
315 case EFI_ACPI_6_3_GICD: {
316 if (++GICDCount > 1) {
317 IncrementErrorCount ();
318 Print (
319 L"ERROR: Only one GICD must be present,"
320 L" GICDCount = %d\n",
321 GICDCount
322 );
323 }
324 ParseAcpi (
325 TRUE,
326 2,
327 "GICD",
328 InterruptContollerPtr,
329 *MadtInterruptControllerLength,
330 PARSER_PARAMS (GicDParser)
331 );
332 break;
333 }
334
335 case EFI_ACPI_6_3_GIC_MSI_FRAME: {
336 ParseAcpi (
337 TRUE,
338 2,
339 "GIC MSI Frame",
340 InterruptContollerPtr,
341 *MadtInterruptControllerLength,
342 PARSER_PARAMS (GicMSIFrameParser)
343 );
344 break;
345 }
346
347 case EFI_ACPI_6_3_GICR: {
348 ParseAcpi (
349 TRUE,
350 2,
351 "GICR",
352 InterruptContollerPtr,
353 *MadtInterruptControllerLength,
354 PARSER_PARAMS (GicRParser)
355 );
356 break;
357 }
358
359 case EFI_ACPI_6_3_GIC_ITS: {
360 ParseAcpi (
361 TRUE,
362 2,
363 "GIC ITS",
364 InterruptContollerPtr,
365 *MadtInterruptControllerLength,
366 PARSER_PARAMS (GicITSParser)
367 );
368 break;
369 }
370
371 default: {
372 IncrementErrorCount ();
373 Print (
374 L"ERROR: Unknown Interrupt Controller Structure,"
375 L" Type = %d, Length = %d\n",
376 *MadtInterruptControllerType,
377 *MadtInterruptControllerLength
378 );
379 }
380 } // switch
381
382 InterruptContollerPtr += *MadtInterruptControllerLength;
383 Offset += *MadtInterruptControllerLength;
384 } // while
385 }