UefiCpuPkg/CpuDxe: Enable protection for newly added page table
[mirror_edk2.git] / UefiCpuPkg / CpuDxe / CpuMp.c
CommitLineData
6022e28c 1/** @file\r
7fadaacd 2 CPU DXE Module to produce CPU MP Protocol.\r
6022e28c 3\r
af9bce40 4 Copyright (c) 2008 - 2017, Intel Corporation. All rights reserved.<BR>\r
6022e28c
JJ
5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include "CpuDxe.h"\r
16#include "CpuMp.h"\r
17\r
9840b129 18EFI_HANDLE mMpServiceHandle = NULL;\r
7fadaacd 19UINTN mNumberOfProcessors = 1;\r
acb2172d 20\r
003973d9 21EFI_MP_SERVICES_PROTOCOL mMpServicesTemplate = {\r
d894d8b7 22 GetNumberOfProcessors,\r
e7938b5a 23 GetProcessorInfo,\r
5fee172f 24 StartupAllAPs,\r
3f4f0af8 25 StartupThisAP,\r
b7c05ba5 26 SwitchBSP,\r
fa7ce675 27 EnableDisableAP,\r
cfa2fac1 28 WhoAmI\r
003973d9
CF
29};\r
30\r
d894d8b7
CF
31/**\r
32 This service retrieves the number of logical processor in the platform\r
33 and the number of those logical processors that are enabled on this boot.\r
34 This service may only be called from the BSP.\r
35\r
36 This function is used to retrieve the following information:\r
37 - The number of logical processors that are present in the system.\r
38 - The number of enabled logical processors in the system at the instant\r
39 this call is made.\r
40\r
41 Because MP Service Protocol provides services to enable and disable processors\r
42 dynamically, the number of enabled logical processors may vary during the\r
43 course of a boot session.\r
44\r
45 If this service is called from an AP, then EFI_DEVICE_ERROR is returned.\r
46 If NumberOfProcessors or NumberOfEnabledProcessors is NULL, then\r
47 EFI_INVALID_PARAMETER is returned. Otherwise, the total number of processors\r
48 is returned in NumberOfProcessors, the number of currently enabled processor\r
49 is returned in NumberOfEnabledProcessors, and EFI_SUCCESS is returned.\r
50\r
51 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL\r
52 instance.\r
53 @param[out] NumberOfProcessors Pointer to the total number of logical\r
54 processors in the system, including the BSP\r
55 and disabled APs.\r
56 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical\r
57 processors that exist in system, including\r
58 the BSP.\r
59\r
60 @retval EFI_SUCCESS The number of logical processors and enabled\r
61 logical processors was retrieved.\r
62 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
63 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL.\r
64 @retval EFI_INVALID_PARAMETER NumberOfEnabledProcessors is NULL.\r
65\r
66**/\r
67EFI_STATUS\r
68EFIAPI\r
69GetNumberOfProcessors (\r
70 IN EFI_MP_SERVICES_PROTOCOL *This,\r
71 OUT UINTN *NumberOfProcessors,\r
72 OUT UINTN *NumberOfEnabledProcessors\r
73 )\r
74{\r
75 if ((NumberOfProcessors == NULL) || (NumberOfEnabledProcessors == NULL)) {\r
76 return EFI_INVALID_PARAMETER;\r
77 }\r
78\r
7fadaacd
JF
79 return MpInitLibGetNumberOfProcessors (\r
80 NumberOfProcessors,\r
81 NumberOfEnabledProcessors\r
82 );\r
d894d8b7
CF
83}\r
84\r
e7938b5a
CF
85/**\r
86 Gets detailed MP-related information on the requested processor at the\r
87 instant this call is made. This service may only be called from the BSP.\r
88\r
89 This service retrieves detailed MP-related information about any processor\r
90 on the platform. Note the following:\r
91 - The processor information may change during the course of a boot session.\r
92 - The information presented here is entirely MP related.\r
93\r
94 Information regarding the number of caches and their sizes, frequency of operation,\r
95 slot numbers is all considered platform-related information and is not provided\r
96 by this service.\r
97\r
98 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL\r
99 instance.\r
100 @param[in] ProcessorNumber The handle number of processor.\r
101 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for\r
102 the requested processor is deposited.\r
103\r
104 @retval EFI_SUCCESS Processor information was returned.\r
105 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
106 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.\r
107 @retval EFI_NOT_FOUND The processor with the handle specified by\r
108 ProcessorNumber does not exist in the platform.\r
109\r
110**/\r
111EFI_STATUS\r
112EFIAPI\r
113GetProcessorInfo (\r
114 IN EFI_MP_SERVICES_PROTOCOL *This,\r
115 IN UINTN ProcessorNumber,\r
116 OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer\r
117 )\r
118{\r
7fadaacd 119 return MpInitLibGetProcessorInfo (ProcessorNumber, ProcessorInfoBuffer, NULL);\r
e7938b5a
CF
120}\r
121\r
5fee172f
CF
122/**\r
123 This service executes a caller provided function on all enabled APs. APs can\r
124 run either simultaneously or one at a time in sequence. This service supports\r
125 both blocking and non-blocking requests. The non-blocking requests use EFI\r
126 events so the BSP can detect when the APs have finished. This service may only\r
127 be called from the BSP.\r
128\r
129 This function is used to dispatch all the enabled APs to the function specified\r
130 by Procedure. If any enabled AP is busy, then EFI_NOT_READY is returned\r
131 immediately and Procedure is not started on any AP.\r
132\r
133 If SingleThread is TRUE, all the enabled APs execute the function specified by\r
134 Procedure one by one, in ascending order of processor handle number. Otherwise,\r
135 all the enabled APs execute the function specified by Procedure simultaneously.\r
136\r
137 If WaitEvent is NULL, execution is in blocking mode. The BSP waits until all\r
138 APs finish or TimeoutInMicroseconds expires. Otherwise, execution is in non-blocking\r
139 mode, and the BSP returns from this service without waiting for APs. If a\r
140 non-blocking mode is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT\r
141 is signaled, then EFI_UNSUPPORTED must be returned.\r
142\r
143 If the timeout specified by TimeoutInMicroseconds expires before all APs return\r
144 from Procedure, then Procedure on the failed APs is terminated. All enabled APs\r
145 are always available for further calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()\r
146 and EFI_MP_SERVICES_PROTOCOL.StartupThisAP(). If FailedCpuList is not NULL, its\r
147 content points to the list of processor handle numbers in which Procedure was\r
148 terminated.\r
149\r
150 Note: It is the responsibility of the consumer of the EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()\r
151 to make sure that the nature of the code that is executed on the BSP and the\r
152 dispatched APs is well controlled. The MP Services Protocol does not guarantee\r
153 that the Procedure function is MP-safe. Hence, the tasks that can be run in\r
154 parallel are limited to certain independent tasks and well-controlled exclusive\r
155 code. EFI services and protocols may not be called by APs unless otherwise\r
156 specified.\r
157\r
158 In blocking execution mode, BSP waits until all APs finish or\r
159 TimeoutInMicroseconds expires.\r
160\r
161 In non-blocking execution mode, BSP is freed to return to the caller and then\r
162 proceed to the next task without having to wait for APs. The following\r
163 sequence needs to occur in a non-blocking execution mode:\r
164\r
165 -# The caller that intends to use this MP Services Protocol in non-blocking\r
166 mode creates WaitEvent by calling the EFI CreateEvent() service. The caller\r
167 invokes EFI_MP_SERVICES_PROTOCOL.StartupAllAPs(). If the parameter WaitEvent\r
168 is not NULL, then StartupAllAPs() executes in non-blocking mode. It requests\r
169 the function specified by Procedure to be started on all the enabled APs,\r
170 and releases the BSP to continue with other tasks.\r
171 -# The caller can use the CheckEvent() and WaitForEvent() services to check\r
172 the state of the WaitEvent created in step 1.\r
173 -# When the APs complete their task or TimeoutInMicroSecondss expires, the MP\r
174 Service signals WaitEvent by calling the EFI SignalEvent() function. If\r
175 FailedCpuList is not NULL, its content is available when WaitEvent is\r
176 signaled. If all APs returned from Procedure prior to the timeout, then\r
177 FailedCpuList is set to NULL. If not all APs return from Procedure before\r
178 the timeout, then FailedCpuList is filled in with the list of the failed\r
179 APs. The buffer is allocated by MP Service Protocol using AllocatePool().\r
180 It is the caller's responsibility to free the buffer with FreePool() service.\r
181 -# This invocation of SignalEvent() function informs the caller that invoked\r
182 EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() that either all the APs completed\r
183 the specified task or a timeout occurred. The contents of FailedCpuList\r
184 can be examined to determine which APs did not complete the specified task\r
185 prior to the timeout.\r
186\r
187 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL\r
188 instance.\r
189 @param[in] Procedure A pointer to the function to be run on\r
190 enabled APs of the system. See type\r
191 EFI_AP_PROCEDURE.\r
192 @param[in] SingleThread If TRUE, then all the enabled APs execute\r
193 the function specified by Procedure one by\r
194 one, in ascending order of processor handle\r
195 number. If FALSE, then all the enabled APs\r
196 execute the function specified by Procedure\r
197 simultaneously.\r
198 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
199 service. If it is NULL, then execute in\r
200 blocking mode. BSP waits until all APs finish\r
201 or TimeoutInMicroseconds expires. If it's\r
202 not NULL, then execute in non-blocking mode.\r
203 BSP requests the function specified by\r
204 Procedure to be started on all the enabled\r
205 APs, and go on executing immediately. If\r
206 all return from Procedure, or TimeoutInMicroseconds\r
207 expires, this event is signaled. The BSP\r
208 can use the CheckEvent() or WaitForEvent()\r
209 services to check the state of event. Type\r
210 EFI_EVENT is defined in CreateEvent() in\r
211 the Unified Extensible Firmware Interface\r
212 Specification.\r
213 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
214 APs to return from Procedure, either for\r
215 blocking or non-blocking mode. Zero means\r
216 infinity. If the timeout expires before\r
217 all APs return from Procedure, then Procedure\r
218 on the failed APs is terminated. All enabled\r
219 APs are available for next function assigned\r
220 by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()\r
221 or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().\r
222 If the timeout expires in blocking mode,\r
223 BSP returns EFI_TIMEOUT. If the timeout\r
224 expires in non-blocking mode, WaitEvent\r
225 is signaled with SignalEvent().\r
226 @param[in] ProcedureArgument The parameter passed into Procedure for\r
227 all APs.\r
228 @param[out] FailedCpuList If NULL, this parameter is ignored. Otherwise,\r
229 if all APs finish successfully, then its\r
230 content is set to NULL. If not all APs\r
231 finish before timeout expires, then its\r
232 content is set to address of the buffer\r
233 holding handle numbers of the failed APs.\r
234 The buffer is allocated by MP Service Protocol,\r
235 and it's the caller's responsibility to\r
236 free the buffer with FreePool() service.\r
237 In blocking mode, it is ready for consumption\r
238 when the call returns. In non-blocking mode,\r
239 it is ready when WaitEvent is signaled. The\r
240 list of failed CPU is terminated by\r
241 END_OF_CPU_LIST.\r
242\r
243 @retval EFI_SUCCESS In blocking mode, all APs have finished before\r
244 the timeout expired.\r
245 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched\r
246 to all enabled APs.\r
247 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the\r
248 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
249 signaled.\r
250 @retval EFI_DEVICE_ERROR Caller processor is AP.\r
251 @retval EFI_NOT_STARTED No enabled APs exist in the system.\r
252 @retval EFI_NOT_READY Any enabled APs are busy.\r
253 @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r
254 all enabled APs have finished.\r
255 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
256\r
257**/\r
258EFI_STATUS\r
259EFIAPI\r
260StartupAllAPs (\r
261 IN EFI_MP_SERVICES_PROTOCOL *This,\r
262 IN EFI_AP_PROCEDURE Procedure,\r
263 IN BOOLEAN SingleThread,\r
264 IN EFI_EVENT WaitEvent OPTIONAL,\r
265 IN UINTN TimeoutInMicroseconds,\r
266 IN VOID *ProcedureArgument OPTIONAL,\r
267 OUT UINTN **FailedCpuList OPTIONAL\r
268 )\r
269{\r
7fadaacd
JF
270 return MpInitLibStartupAllAPs (\r
271 Procedure,\r
272 SingleThread,\r
273 WaitEvent,\r
274 TimeoutInMicroseconds,\r
275 ProcedureArgument,\r
276 FailedCpuList\r
277 );\r
5fee172f
CF
278}\r
279\r
3f4f0af8
CF
280/**\r
281 This service lets the caller get one enabled AP to execute a caller-provided\r
282 function. The caller can request the BSP to either wait for the completion\r
283 of the AP or just proceed with the next task by using the EFI event mechanism.\r
284 See EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() for more details on non-blocking\r
285 execution support. This service may only be called from the BSP.\r
286\r
287 This function is used to dispatch one enabled AP to the function specified by\r
288 Procedure passing in the argument specified by ProcedureArgument. If WaitEvent\r
289 is NULL, execution is in blocking mode. The BSP waits until the AP finishes or\r
290 TimeoutInMicroSecondss expires. Otherwise, execution is in non-blocking mode.\r
291 BSP proceeds to the next task without waiting for the AP. If a non-blocking mode\r
292 is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled,\r
293 then EFI_UNSUPPORTED must be returned.\r
294\r
295 If the timeout specified by TimeoutInMicroseconds expires before the AP returns\r
296 from Procedure, then execution of Procedure by the AP is terminated. The AP is\r
297 available for subsequent calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() and\r
298 EFI_MP_SERVICES_PROTOCOL.StartupThisAP().\r
299\r
300 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL\r
301 instance.\r
f3b91fa0
JF
302 @param[in] Procedure A pointer to the function to be run on the\r
303 designated AP of the system. See type\r
3f4f0af8
CF
304 EFI_AP_PROCEDURE.\r
305 @param[in] ProcessorNumber The handle number of the AP. The range is\r
306 from 0 to the total number of logical\r
307 processors minus 1. The total number of\r
308 logical processors can be retrieved by\r
309 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
310 @param[in] WaitEvent The event created by the caller with CreateEvent()\r
311 service. If it is NULL, then execute in\r
f3b91fa0
JF
312 blocking mode. BSP waits until this AP finish\r
313 or TimeoutInMicroSeconds expires. If it's\r
3f4f0af8
CF
314 not NULL, then execute in non-blocking mode.\r
315 BSP requests the function specified by\r
f3b91fa0
JF
316 Procedure to be started on this AP,\r
317 and go on executing immediately. If this AP\r
318 return from Procedure or TimeoutInMicroSeconds\r
3f4f0af8
CF
319 expires, this event is signaled. The BSP\r
320 can use the CheckEvent() or WaitForEvent()\r
321 services to check the state of event. Type\r
322 EFI_EVENT is defined in CreateEvent() in\r
323 the Unified Extensible Firmware Interface\r
324 Specification.\r
367284e7 325 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
f3b91fa0 326 this AP to finish this Procedure, either for\r
3f4f0af8
CF
327 blocking or non-blocking mode. Zero means\r
328 infinity. If the timeout expires before\r
f3b91fa0
JF
329 this AP returns from Procedure, then Procedure\r
330 on the AP is terminated. The\r
331 AP is available for next function assigned\r
3f4f0af8
CF
332 by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()\r
333 or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().\r
334 If the timeout expires in blocking mode,\r
335 BSP returns EFI_TIMEOUT. If the timeout\r
336 expires in non-blocking mode, WaitEvent\r
337 is signaled with SignalEvent().\r
f3b91fa0
JF
338 @param[in] ProcedureArgument The parameter passed into Procedure on the\r
339 specified AP.\r
3f4f0af8
CF
340 @param[out] Finished If NULL, this parameter is ignored. In\r
341 blocking mode, this parameter is ignored.\r
342 In non-blocking mode, if AP returns from\r
343 Procedure before the timeout expires, its\r
344 content is set to TRUE. Otherwise, the\r
345 value is set to FALSE. The caller can\r
346 determine if the AP returned from Procedure\r
347 by evaluating this value.\r
348\r
349 @retval EFI_SUCCESS In blocking mode, specified AP finished before\r
350 the timeout expires.\r
351 @retval EFI_SUCCESS In non-blocking mode, the function has been\r
352 dispatched to specified AP.\r
353 @retval EFI_UNSUPPORTED A non-blocking mode request was made after the\r
354 UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
355 signaled.\r
356 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
357 @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r
358 the specified AP has finished.\r
359 @retval EFI_NOT_READY The specified AP is busy.\r
360 @retval EFI_NOT_FOUND The processor with the handle specified by\r
361 ProcessorNumber does not exist.\r
362 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.\r
363 @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
364\r
365**/\r
366EFI_STATUS\r
367EFIAPI\r
368StartupThisAP (\r
369 IN EFI_MP_SERVICES_PROTOCOL *This,\r
370 IN EFI_AP_PROCEDURE Procedure,\r
371 IN UINTN ProcessorNumber,\r
372 IN EFI_EVENT WaitEvent OPTIONAL,\r
373 IN UINTN TimeoutInMicroseconds,\r
374 IN VOID *ProcedureArgument OPTIONAL,\r
375 OUT BOOLEAN *Finished OPTIONAL\r
376 )\r
377{\r
7fadaacd
JF
378 return MpInitLibStartupThisAP (\r
379 Procedure,\r
380 ProcessorNumber,\r
381 WaitEvent,\r
382 TimeoutInMicroseconds,\r
383 ProcedureArgument,\r
384 Finished\r
385 );\r
3f4f0af8
CF
386}\r
387\r
b7c05ba5
CF
388/**\r
389 This service switches the requested AP to be the BSP from that point onward.\r
390 This service changes the BSP for all purposes. This call can only be performed\r
391 by the current BSP.\r
392\r
393 This service switches the requested AP to be the BSP from that point onward.\r
394 This service changes the BSP for all purposes. The new BSP can take over the\r
395 execution of the old BSP and continue seamlessly from where the old one left\r
396 off. This service may not be supported after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT\r
397 is signaled.\r
398\r
399 If the BSP cannot be switched prior to the return from this service, then\r
400 EFI_UNSUPPORTED must be returned.\r
401\r
402 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
403 @param[in] ProcessorNumber The handle number of AP that is to become the new\r
404 BSP. The range is from 0 to the total number of\r
405 logical processors minus 1. The total number of\r
406 logical processors can be retrieved by\r
407 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
408 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an\r
409 enabled AP. Otherwise, it will be disabled.\r
410\r
411 @retval EFI_SUCCESS BSP successfully switched.\r
412 @retval EFI_UNSUPPORTED Switching the BSP cannot be completed prior to\r
413 this service returning.\r
414 @retval EFI_UNSUPPORTED Switching the BSP is not supported.\r
29b237f8 415 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
b7c05ba5
CF
416 @retval EFI_NOT_FOUND The processor with the handle specified by\r
417 ProcessorNumber does not exist.\r
418 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the current BSP or\r
419 a disabled AP.\r
420 @retval EFI_NOT_READY The specified AP is busy.\r
421\r
422**/\r
423EFI_STATUS\r
424EFIAPI\r
425SwitchBSP (\r
426 IN EFI_MP_SERVICES_PROTOCOL *This,\r
427 IN UINTN ProcessorNumber,\r
428 IN BOOLEAN EnableOldBSP\r
429 )\r
430{\r
7fadaacd 431 return MpInitLibSwitchBSP (ProcessorNumber, EnableOldBSP);\r
b7c05ba5
CF
432}\r
433\r
fa7ce675
CF
434/**\r
435 This service lets the caller enable or disable an AP from this point onward.\r
436 This service may only be called from the BSP.\r
437\r
438 This service allows the caller enable or disable an AP from this point onward.\r
439 The caller can optionally specify the health status of the AP by Health. If\r
440 an AP is being disabled, then the state of the disabled AP is implementation\r
441 dependent. If an AP is enabled, then the implementation must guarantee that a\r
442 complete initialization sequence is performed on the AP, so the AP is in a state\r
443 that is compatible with an MP operating system. This service may not be supported\r
444 after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled.\r
445\r
446 If the enable or disable AP operation cannot be completed prior to the return\r
447 from this service, then EFI_UNSUPPORTED must be returned.\r
448\r
449 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
f3b91fa0
JF
450 @param[in] ProcessorNumber The handle number of AP.\r
451 The range is from 0 to the total number of\r
fa7ce675
CF
452 logical processors minus 1. The total number of\r
453 logical processors can be retrieved by\r
454 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
455 @param[in] EnableAP Specifies the new state for the processor for\r
456 enabled, FALSE for disabled.\r
457 @param[in] HealthFlag If not NULL, a pointer to a value that specifies\r
458 the new health status of the AP. This flag\r
459 corresponds to StatusFlag defined in\r
460 EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only\r
461 the PROCESSOR_HEALTH_STATUS_BIT is used. All other\r
462 bits are ignored. If it is NULL, this parameter\r
463 is ignored.\r
464\r
465 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.\r
466 @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed\r
467 prior to this service returning.\r
468 @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.\r
469 @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
470 @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber\r
471 does not exist.\r
472 @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.\r
473\r
474**/\r
475EFI_STATUS\r
476EFIAPI\r
477EnableDisableAP (\r
478 IN EFI_MP_SERVICES_PROTOCOL *This,\r
479 IN UINTN ProcessorNumber,\r
480 IN BOOLEAN EnableAP,\r
481 IN UINT32 *HealthFlag OPTIONAL\r
482 )\r
483{\r
7fadaacd 484 return MpInitLibEnableDisableAP (ProcessorNumber, EnableAP, HealthFlag);\r
fa7ce675
CF
485}\r
486\r
cfa2fac1
CF
487/**\r
488 This return the handle number for the calling processor. This service may be\r
489 called from the BSP and APs.\r
490\r
491 This service returns the processor handle number for the calling processor.\r
492 The returned value is in the range from 0 to the total number of logical\r
493 processors minus 1. The total number of logical processors can be retrieved\r
494 with EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors(). This service may be\r
495 called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER\r
496 is returned. Otherwise, the current processors handle number is returned in\r
497 ProcessorNumber, and EFI_SUCCESS is returned.\r
498\r
499 @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
f3b91fa0
JF
500 @param[out] ProcessorNumber Pointer to the handle number of AP.\r
501 The range is from 0 to the total number of\r
cfa2fac1
CF
502 logical processors minus 1. The total number of\r
503 logical processors can be retrieved by\r
504 EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
505\r
506 @retval EFI_SUCCESS The current processor handle number was returned\r
507 in ProcessorNumber.\r
508 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.\r
509\r
510**/\r
511EFI_STATUS\r
512EFIAPI\r
513WhoAmI (\r
514 IN EFI_MP_SERVICES_PROTOCOL *This,\r
515 OUT UINTN *ProcessorNumber\r
516 )\r
517{\r
7fadaacd 518 return MpInitLibWhoAmI (ProcessorNumber);;\r
03673ae1 519}\r
1535c888 520\r
db61e163
JF
521/**\r
522 Collects BIST data from HOB.\r
523\r
524 This function collects BIST data from HOB built from Sec Platform Information\r
525 PPI or SEC Platform Information2 PPI.\r
526\r
527**/\r
528VOID\r
529CollectBistDataFromHob (\r
530 VOID\r
531 )\r
532{\r
533 EFI_HOB_GUID_TYPE *GuidHob;\r
534 EFI_SEC_PLATFORM_INFORMATION_RECORD2 *SecPlatformInformation2;\r
535 EFI_SEC_PLATFORM_INFORMATION_RECORD *SecPlatformInformation;\r
536 UINTN NumberOfData;\r
537 EFI_SEC_PLATFORM_INFORMATION_CPU *CpuInstance;\r
538 EFI_SEC_PLATFORM_INFORMATION_CPU BspCpuInstance;\r
539 UINTN ProcessorNumber;\r
7fadaacd
JF
540 EFI_PROCESSOR_INFORMATION ProcessorInfo;\r
541 EFI_HEALTH_FLAGS BistData;\r
7d17ab47 542 UINTN CpuInstanceNumber;\r
db61e163
JF
543\r
544 SecPlatformInformation2 = NULL;\r
545 SecPlatformInformation = NULL;\r
546\r
547 //\r
548 // Get gEfiSecPlatformInformation2PpiGuid Guided HOB firstly\r
549 //\r
550 GuidHob = GetFirstGuidHob (&gEfiSecPlatformInformation2PpiGuid);\r
551 if (GuidHob != NULL) {\r
552 //\r
553 // Sec Platform Information2 PPI includes BSP/APs' BIST information\r
554 //\r
555 SecPlatformInformation2 = GET_GUID_HOB_DATA (GuidHob);\r
556 NumberOfData = SecPlatformInformation2->NumberOfCpus;\r
557 CpuInstance = SecPlatformInformation2->CpuInstance;\r
558 } else {\r
559 //\r
560 // Otherwise, get gEfiSecPlatformInformationPpiGuid Guided HOB\r
561 //\r
562 GuidHob = GetFirstGuidHob (&gEfiSecPlatformInformationPpiGuid);\r
563 if (GuidHob != NULL) {\r
564 SecPlatformInformation = GET_GUID_HOB_DATA (GuidHob);\r
565 NumberOfData = 1;\r
566 //\r
567 // SEC Platform Information only includes BSP's BIST information\r
568 // does not have BSP's APIC ID\r
569 //\r
570 BspCpuInstance.CpuLocation = GetApicId ();\r
571 BspCpuInstance.InfoRecord.IA32HealthFlags.Uint32 = SecPlatformInformation->IA32HealthFlags.Uint32;\r
572 CpuInstance = &BspCpuInstance;\r
573 } else {\r
af9bce40 574 DEBUG ((DEBUG_INFO, "Does not find any HOB stored CPU BIST information!\n"));\r
db61e163
JF
575 //\r
576 // Does not find any HOB stored BIST information\r
577 //\r
578 return;\r
579 }\r
580 }\r
581\r
7d17ab47
JF
582 for (ProcessorNumber = 0; ProcessorNumber < mNumberOfProcessors; ProcessorNumber++) {\r
583 MpInitLibGetProcessorInfo (ProcessorNumber, &ProcessorInfo, &BistData);\r
584 for (CpuInstanceNumber = 0; CpuInstanceNumber < NumberOfData; CpuInstanceNumber++) {\r
585 if (ProcessorInfo.ProcessorId == CpuInstance[CpuInstanceNumber].CpuLocation) {\r
db61e163
JF
586 //\r
587 // Update CPU health status for MP Services Protocol according to BIST data.\r
588 //\r
7d17ab47 589 BistData = CpuInstance[CpuInstanceNumber].InfoRecord.IA32HealthFlags;\r
db61e163
JF
590 }\r
591 }\r
7d17ab47
JF
592 if (BistData.Uint32 != 0) {\r
593 //\r
594 // Report Status Code that self test is failed\r
595 //\r
596 REPORT_STATUS_CODE (\r
597 EFI_ERROR_CODE | EFI_ERROR_MAJOR,\r
598 (EFI_COMPUTING_UNIT_HOST_PROCESSOR | EFI_CU_HP_EC_SELF_TEST)\r
599 );\r
600 }\r
db61e163
JF
601 }\r
602}\r
603\r
01953ce6
JW
604/**\r
605 Get GDT register value.\r
606\r
607 This function is mainly for AP purpose because AP may have different GDT\r
608 table than BSP.\r
609\r
610 @param[in,out] Buffer The pointer to private data buffer.\r
611\r
612**/\r
613VOID\r
614EFIAPI\r
615GetGdtr (\r
616 IN OUT VOID *Buffer\r
617 )\r
618{\r
619 AsmReadGdtr ((IA32_DESCRIPTOR *)Buffer);\r
620}\r
621\r
622/**\r
623 Initializes CPU exceptions handlers for the sake of stack switch requirement.\r
624\r
625 This function is a wrapper of InitializeCpuExceptionHandlersEx. It's mainly\r
626 for the sake of AP's init because of EFI_AP_PROCEDURE API requirement.\r
627\r
628 @param[in,out] Buffer The pointer to private data buffer.\r
629\r
630**/\r
631VOID\r
632EFIAPI\r
633InitializeExceptionStackSwitchHandlers (\r
634 IN OUT VOID *Buffer\r
635 )\r
636{\r
637 CPU_EXCEPTION_INIT_DATA *EssData;\r
638 IA32_DESCRIPTOR Idtr;\r
639 EFI_STATUS Status;\r
640\r
641 EssData = Buffer;\r
642 //\r
643 // We don't plan to replace IDT table with a new one, but we should not assume\r
644 // the AP's IDT is the same as BSP's IDT either.\r
645 //\r
646 AsmReadIdtr (&Idtr);\r
647 EssData->Ia32.IdtTable = (VOID *)Idtr.Base;\r
648 EssData->Ia32.IdtTableSize = Idtr.Limit + 1;\r
649 Status = InitializeCpuExceptionHandlersEx (NULL, EssData);\r
650 ASSERT_EFI_ERROR (Status);\r
651}\r
652\r
653/**\r
654 Initializes MP exceptions handlers for the sake of stack switch requirement.\r
655\r
656 This function will allocate required resources required to setup stack switch\r
657 and pass them through CPU_EXCEPTION_INIT_DATA to each logic processor.\r
658\r
659**/\r
660VOID\r
661InitializeMpExceptionStackSwitchHandlers (\r
662 VOID\r
663 )\r
664{\r
665 UINTN Index;\r
666 UINTN Bsp;\r
667 UINTN ExceptionNumber;\r
668 UINTN OldGdtSize;\r
669 UINTN NewGdtSize;\r
670 UINTN NewStackSize;\r
671 IA32_DESCRIPTOR Gdtr;\r
672 CPU_EXCEPTION_INIT_DATA EssData;\r
673 UINT8 *GdtBuffer;\r
674 UINT8 *StackTop;\r
675\r
676 if (!PcdGetBool (PcdCpuStackGuard)) {\r
677 return;\r
678 }\r
679\r
680 ExceptionNumber = FixedPcdGetSize (PcdCpuStackSwitchExceptionList);\r
681 NewStackSize = FixedPcdGet32 (PcdCpuKnownGoodStackSize) * ExceptionNumber;\r
682\r
683 StackTop = AllocateRuntimeZeroPool (NewStackSize * mNumberOfProcessors);\r
684 ASSERT (StackTop != NULL);\r
685 StackTop += NewStackSize * mNumberOfProcessors;\r
686\r
687 //\r
688 // The default exception handlers must have been initialized. Let's just skip\r
689 // it in this method.\r
690 //\r
691 EssData.Ia32.Revision = CPU_EXCEPTION_INIT_DATA_REV;\r
692 EssData.Ia32.InitDefaultHandlers = FALSE;\r
693\r
694 EssData.Ia32.StackSwitchExceptions = FixedPcdGetPtr(PcdCpuStackSwitchExceptionList);\r
695 EssData.Ia32.StackSwitchExceptionNumber = ExceptionNumber;\r
696 EssData.Ia32.KnownGoodStackSize = FixedPcdGet32(PcdCpuKnownGoodStackSize);\r
697\r
698 MpInitLibWhoAmI (&Bsp);\r
699 for (Index = 0; Index < mNumberOfProcessors; ++Index) {\r
700 //\r
701 // To support stack switch, we need to re-construct GDT but not IDT.\r
702 //\r
703 if (Index == Bsp) {\r
704 GetGdtr (&Gdtr);\r
705 } else {\r
706 //\r
707 // AP might have different size of GDT from BSP.\r
708 //\r
709 MpInitLibStartupThisAP (GetGdtr, Index, NULL, 0, (VOID *)&Gdtr, NULL);\r
710 }\r
711\r
712 //\r
713 // X64 needs only one TSS of current task working for all exceptions\r
714 // because of its IST feature. IA32 needs one TSS for each exception\r
715 // in addition to current task. Since AP is not supposed to allocate\r
716 // memory, we have to do it in BSP. To simplify the code, we allocate\r
717 // memory for IA32 case to cover both IA32 and X64 exception stack\r
718 // switch.\r
719 //\r
720 // Layout of memory to allocate for each processor:\r
721 // --------------------------------\r
722 // | Alignment | (just in case)\r
723 // --------------------------------\r
724 // | |\r
725 // | Original GDT |\r
726 // | |\r
727 // --------------------------------\r
728 // | Current task descriptor |\r
729 // --------------------------------\r
730 // | |\r
731 // | Exception task descriptors | X ExceptionNumber\r
732 // | |\r
733 // --------------------------------\r
734 // | Current task-state segment |\r
735 // --------------------------------\r
736 // | |\r
737 // | Exception task-state segment | X ExceptionNumber\r
738 // | |\r
739 // --------------------------------\r
740 //\r
741 OldGdtSize = Gdtr.Limit + 1;\r
742 EssData.Ia32.ExceptionTssDescSize = sizeof (IA32_TSS_DESCRIPTOR) *\r
743 (ExceptionNumber + 1);\r
744 EssData.Ia32.ExceptionTssSize = sizeof (IA32_TASK_STATE_SEGMENT) *\r
745 (ExceptionNumber + 1);\r
746 NewGdtSize = sizeof (IA32_TSS_DESCRIPTOR) +\r
747 OldGdtSize +\r
748 EssData.Ia32.ExceptionTssDescSize +\r
749 EssData.Ia32.ExceptionTssSize;\r
750\r
751 GdtBuffer = AllocateRuntimeZeroPool (NewGdtSize);\r
752 ASSERT (GdtBuffer != NULL);\r
753\r
754 //\r
755 // Make sure GDT table alignment\r
756 //\r
757 EssData.Ia32.GdtTable = ALIGN_POINTER(GdtBuffer, sizeof (IA32_TSS_DESCRIPTOR));\r
758 NewGdtSize -= ((UINT8 *)EssData.Ia32.GdtTable - GdtBuffer);\r
759 EssData.Ia32.GdtTableSize = NewGdtSize;\r
760\r
761 EssData.Ia32.ExceptionTssDesc = ((UINT8 *)EssData.Ia32.GdtTable + OldGdtSize);\r
762 EssData.Ia32.ExceptionTss = ((UINT8 *)EssData.Ia32.GdtTable + OldGdtSize +\r
763 EssData.Ia32.ExceptionTssDescSize);\r
764\r
765 EssData.Ia32.KnownGoodStackTop = (UINTN)StackTop;\r
766 DEBUG ((DEBUG_INFO,\r
767 "Exception stack top[cpu%lu]: 0x%lX\n",\r
768 (UINT64)(UINTN)Index,\r
769 (UINT64)(UINTN)StackTop));\r
770\r
771 if (Index == Bsp) {\r
772 InitializeExceptionStackSwitchHandlers (&EssData);\r
773 } else {\r
774 MpInitLibStartupThisAP (\r
775 InitializeExceptionStackSwitchHandlers,\r
776 Index,\r
777 NULL,\r
778 0,\r
779 (VOID *)&EssData,\r
780 NULL\r
781 );\r
782 }\r
783\r
784 StackTop -= NewStackSize;\r
785 }\r
786}\r
787\r
6022e28c
JJ
788/**\r
789 Initialize Multi-processor support.\r
790\r
791**/\r
792VOID\r
793InitializeMpSupport (\r
794 VOID\r
795 )\r
796{\r
1aa6bf52 797 EFI_STATUS Status;\r
7fadaacd
JF
798 UINTN NumberOfProcessors;\r
799 UINTN NumberOfEnabledProcessors;\r
4a50c272 800\r
6a26a597 801 //\r
0a55f3bd 802 // Wakeup APs to do initialization\r
6a26a597 803 //\r
0a55f3bd
JF
804 Status = MpInitLibInitialize ();\r
805 ASSERT_EFI_ERROR (Status);\r
fe078dd5 806\r
0a55f3bd
JF
807 MpInitLibGetNumberOfProcessors (&NumberOfProcessors, &NumberOfEnabledProcessors);\r
808 mNumberOfProcessors = NumberOfProcessors;\r
d2f0ff1e 809 DEBUG ((DEBUG_INFO, "Detect CPU count: %d\n", mNumberOfProcessors));\r
1aa6bf52 810\r
01953ce6
JW
811 //\r
812 // Initialize exception stack switch handlers for each logic processor.\r
813 //\r
814 InitializeMpExceptionStackSwitchHandlers ();\r
815\r
db61e163
JF
816 //\r
817 // Update CPU healthy information from Guided HOB\r
818 //\r
819 CollectBistDataFromHob ();\r
820\r
4a50c272
CF
821 Status = gBS->InstallMultipleProtocolInterfaces (\r
822 &mMpServiceHandle,\r
823 &gEfiMpServiceProtocolGuid, &mMpServicesTemplate,\r
824 NULL\r
825 );\r
826 ASSERT_EFI_ERROR (Status);\r
6a26a597 827}\r
7fadaacd 828\r