From 62f2cf57840e3b08122b6326b0ed9f4b25ce15d9 Mon Sep 17 00:00:00 2001 From: Ray Ni Date: Thu, 28 Jan 2021 11:42:43 +0800 Subject: [PATCH] UefiCpuPkg/MpInitLib: Use XADD to avoid lock acquire/release When AP firstly wakes up, MpFuncs.nasm contains below logic to assign an unique ApIndex to each AP according to who comes first: ---ASM--- TestLock: xchg [edi], eax cmp eax, NotVacantFlag jz TestLock mov ecx, esi add ecx, ApIndexLocation inc dword [ecx] mov ebx, [ecx] Releaselock: mov eax, VacantFlag xchg [edi], eax ---ASM END--- "lock inc" cannot be used to increase ApIndex because not only the global ApIndex should be increased, but also the result should be stored to a local general purpose register EBX. This patch learns from the NASM implementation of InternalSyncIncrement() to use "XADD" instruction which can increase the global ApIndex and store the original ApIndex to EBX in one instruction. With this patch, OVMF when running in a 255 threads QEMU spends about one second to wakeup all APs. Original implementation needs more than 10 seconds. Signed-off-by: Ray Ni Cc: Eric Dong Cc: Laszlo Ersek Cc: Rahul Kumar Reviewed-by: Michael D Kinney Acked-by: Laszlo Ersek Reviewed-by: Eric Dong --- .../Library/MpInitLib/Ia32/MpFuncs.nasm | 20 ++++++------------- UefiCpuPkg/Library/MpInitLib/X64/MpFuncs.nasm | 18 ++++++----------- 2 files changed, 12 insertions(+), 26 deletions(-) diff --git a/UefiCpuPkg/Library/MpInitLib/Ia32/MpFuncs.nasm b/UefiCpuPkg/Library/MpInitLib/Ia32/MpFuncs.nasm index 7e81d24aa6..2eaddc93bc 100644 --- a/UefiCpuPkg/Library/MpInitLib/Ia32/MpFuncs.nasm +++ b/UefiCpuPkg/Library/MpInitLib/Ia32/MpFuncs.nasm @@ -1,5 +1,5 @@ ;------------------------------------------------------------------------------ ; -; Copyright (c) 2015 - 2019, Intel Corporation. All rights reserved.
+; Copyright (c) 2015 - 2021, Intel Corporation. All rights reserved.
; SPDX-License-Identifier: BSD-2-Clause-Patent ; ; Module Name: @@ -125,19 +125,11 @@ SkipEnableExecuteDisable: add edi, LockLocation mov eax, NotVacantFlag -TestLock: - xchg [edi], eax - cmp eax, NotVacantFlag - jz TestLock - - mov ecx, esi - add ecx, ApIndexLocation - inc dword [ecx] - mov ebx, [ecx] - -Releaselock: - mov eax, VacantFlag - xchg [edi], eax + mov edi, esi + add edi, ApIndexLocation + mov ebx, 1 + lock xadd dword [edi], ebx ; EBX = ApIndex++ + inc ebx ; EBX is CpuNumber mov edi, esi add edi, StackSizeLocation diff --git a/UefiCpuPkg/Library/MpInitLib/X64/MpFuncs.nasm b/UefiCpuPkg/Library/MpInitLib/X64/MpFuncs.nasm index aecfd07bc0..5b588f2dcb 100644 --- a/UefiCpuPkg/Library/MpInitLib/X64/MpFuncs.nasm +++ b/UefiCpuPkg/Library/MpInitLib/X64/MpFuncs.nasm @@ -1,5 +1,5 @@ ;------------------------------------------------------------------------------ ; -; Copyright (c) 2015 - 2019, Intel Corporation. All rights reserved.
+; Copyright (c) 2015 - 2021, Intel Corporation. All rights reserved.
; SPDX-License-Identifier: BSD-2-Clause-Patent ; ; Module Name: @@ -161,18 +161,12 @@ LongModeStart: add edi, LockLocation mov rax, NotVacantFlag -TestLock: - xchg qword [edi], rax - cmp rax, NotVacantFlag - jz TestLock - - lea ecx, [esi + ApIndexLocation] - inc dword [ecx] - mov ebx, [ecx] + mov edi, esi + add edi, ApIndexLocation + mov ebx, 1 + lock xadd dword [edi], ebx ; EBX = ApIndex++ + inc ebx ; EBX is CpuNumber -Releaselock: - mov rax, VacantFlag - xchg qword [edi], rax ; program stack mov edi, esi add edi, StackSizeLocation -- 2.39.2