From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id AAF3F7803CE for ; Wed, 10 Apr 2024 06:19:01 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=LGR1mSuYQQqIywO5J/q/r0PwkwoewzjSdIKG81DeVMQ=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Resent-Date:Resent-From:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20240206; t=1712729940; v=1; b=e9DtCFUB0uzfZPYAlFMwqyoNWz3AWZj9hkxWWIv4dDgiLBQMudepAsFzIBrAlenoOO0X5rVB WqIkuweRbizxRfj6EprWIdL6MGOZegOxWD0AyUo0abODxK2gilOb8A6vxW+Y0SC3I6Qhn/woh+Q 5p6iatLjYTG36EweKa+MT5V3qoUNCMyn9SS+ASEWfXKAZ59R4t5SvPlMD05d9ipSBdQZgG0b0TK 3Z1hokceDJvnuQ99NjQ7+M3s8KchMoNrYikwbqCWW+QKknYDJy1DqoDC1MkruaBAYvb/J0blOAb q29xP6cmryQCLk6qBVb/+EQs+EihsoQuJpMbisk9pX0tA== X-Received: by 127.0.0.2 with SMTP id YOxbYY7687511xJWFdPrNRh6; Tue, 09 Apr 2024 23:19:00 -0700 X-Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.12]) by mx.groups.io with SMTP id smtpd.web11.160356.1712729938026651132 for ; Tue, 09 Apr 2024 23:18:59 -0700 X-CSE-ConnectionGUID: pRg1XwNlTL+G1ePVdWvAXQ== X-CSE-MsgGUID: uuB8W1xQQNKmF3da8ScDlw== X-IronPort-AV: E=McAfee;i="6600,9927,11039"; a="11848163" X-IronPort-AV: E=Sophos;i="6.07,190,1708416000"; d="scan'208";a="11848163" X-Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by fmvoesa106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Apr 2024 23:18:59 -0700 X-CSE-ConnectionGUID: 0lnLdbrpQn+o2iAY+QRakw== X-CSE-MsgGUID: k18NVaT0S5eieuI+a/V9xg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,190,1708416000"; d="scan'208";a="20490091" X-Received: from njayapra-mobl.gar.corp.intel.com ([10.247.147.19]) by fmviesa009-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Apr 2024 23:18:58 -0700 From: "Jayaprakash, N" To: devel@edk2.groups.io Cc: Jayaprakash N , Rebecca Cran , Michael D Kinney Subject: [edk2-devel] [edk2-libc Patch 1/1] edk2-libc:add rdmsr_ex & wrmsr_ex functions to read/write cpu specific msrs Date: Wed, 10 Apr 2024 11:48:49 +0530 Message-Id: <20240410061849.984-2-n.jayaprakash@intel.com> In-Reply-To: <20240410061849.984-1-n.jayaprakash@intel.com> References: <20240410061849.984-1-n.jayaprakash@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Resent-Date: Tue, 09 Apr 2024 23:18:59 -0700 Resent-From: n.jayaprakash@intel.com Reply-To: devel@edk2.groups.io,n.jayaprakash@intel.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: ADykckaYGhpnebqOyVyPZcgpx7686176AA= Content-Transfer-Encoding: 8bit X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20240206 header.b=e9DtCFUB; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=intel.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4746 The rdmsr_ex and wrmsr_ex are extension APIs to the rdmsr and wrmsr APIs supported in edk2 module. These extension APIs makes it possible to read / write MSRs from specific processors and fills an existing gap in this area. Cc: Rebecca Cran Cc: Michael D Kinney Cc: Jayaprakash N Signed-off-by: Jayaprakash N --- .../PyMod-3.6.8/Modules/edk2module.c | 159 +++++++++++++++++- .../Python/Python-3.6.8/Python368.inf | 3 + 2 files changed, 158 insertions(+), 4 deletions(-) diff --git a/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Modules/edk2module.c b/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Modules/edk2module.c index d6af8da..f1b13a6 100644 --- a/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Modules/edk2module.c +++ b/AppPkg/Applications/Python/Python-3.6.8/PyMod-3.6.8/Modules/edk2module.c @@ -3,7 +3,7 @@ Derived from posixmodule.c in Python 2.7.2. Copyright (c) 2015, Daryl McDaniel. All rights reserved.
- Copyright (c) 2011 - 2023, Intel Corporation. All rights reserved.
+ Copyright (c) 2011 - 2024, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License that accompanies this distribution. The full text of the license may be found at @@ -22,16 +22,23 @@ #include #include #include +#include // Needed for the definition of EFI_AP_PROCEDURE #include #include #include #include +#include +#include #ifdef __cplusplus extern "C" { #endif PyTypeObject EfiGuidType; +EFI_MP_SERVICES_PROTOCOL *gpMpService = NULL; +UINTN gBSPProcessorNumber = 0; +UINTN gNumberOfProcessors = 0; +UINTN gNumberOfEnabledProcessors = 0; extern void _swsmi( unsigned int smi_code_data, unsigned int rax_value, unsigned int rbx_value, unsigned int rcx_value, unsigned int rdx_value, unsigned int rsi_value, unsigned int rdi_value ); // -- Support routines @@ -169,6 +176,35 @@ PyDoc_STRVAR(edk2__doc__, /* dummy version. _PyVerify_fd() is already defined in fileobject.h */ #define _PyVerify_fd_dup2(A, B) (1) +static EFI_STATUS +MpServicesWhoAmI ( + IN EFI_MP_SERVICES_PROTOCOL *pMpService, + OUT UINTN *pProcessorNumber + ) +{ + return pMpService->WhoAmI (pMpService, pProcessorNumber); +} + +static EFI_STATUS +MpServicesGetNumberOfProcessors ( + IN EFI_MP_SERVICES_PROTOCOL *pMpService, + OUT UINTN *pNumberOfProcessors, + OUT UINTN *pNumberOfEnabledProcessors + + ) +{ + return pMpService->GetNumberOfProcessors (pMpService, pNumberOfProcessors, pNumberOfEnabledProcessors); +} + +static EFI_STATUS +MpServicesSwitchBSP ( + IN EFI_MP_SERVICES_PROTOCOL *pMpService, + IN UINTN ProcessorNumber + ) +{ + return pMpService->SwitchBSP(pMpService, ProcessorNumber, TRUE); +} + #ifndef UEFI_C_SOURCE /* Return a dictionary corresponding to the POSIX environment table */ extern char **environ; @@ -3865,6 +3901,56 @@ edk2_rdmsr(PyObject *self, PyObject *args) return Py_BuildValue("(II)", (unsigned long)veax, (unsigned long)vedx); } +PyDoc_STRVAR(efi_rdmsr_ex__doc__, +"rdmsr_ex(cpu, msr) -> (lower_32bits, higher_32bits)\n\ +\n\ +Read the given msr by switching to cpu and return the data as tuple.\n\ +\n\ +Parameters:\n\ + cpu - The cpu number in hex or int format\n\ + msr - The msr in hex or int format\n\ +\n\ +Return Value:\n\ + a tuple with lower and higher 32 bit values read from the msr\n\ +"); + +static PyObject * +edk2_rdmsr_ex(PyObject *self, PyObject *args) +{ + unsigned int cpu, vecx, veax, vedx; + unsigned int bsp_switched = 0; + EFI_STATUS status = 0; + UINT64 data = 0; + + if (!PyArg_ParseTuple(args, "II", &cpu, &vecx)) + return NULL; + + Py_BEGIN_ALLOW_THREADS + if (cpu != gBSPProcessorNumber && cpu < gNumberOfProcessors) + { + //switch the BSP to the cpu + status = MpServicesSwitchBSP(gpMpService, cpu); + if (!EFI_ERROR(status)) + { + bsp_switched = 1; + } + } + + data = AsmReadMsr64(vecx); + + if (bsp_switched) + { + // switch BSP to the saved BSP processor + MpServicesSwitchBSP(gpMpService, gBSPProcessorNumber); + // update the saved BSP processor + MpServicesWhoAmI(gpMpService, &gBSPProcessorNumber); + } + Py_END_ALLOW_THREADS + veax = (UINT32)data; + vedx = (UINT64)data >> 32; + return Py_BuildValue("(II)", (unsigned long)veax, (unsigned long)vedx); +} + PyDoc_STRVAR(efi_wrmsr__doc__, "wrmsr(msr, lower_32bits, higher_32bits) -> None\n\ \n\ @@ -3889,6 +3975,58 @@ edk2_wrmsr(PyObject *self, PyObject *args) data = vedx << 32 | veax; Py_BEGIN_ALLOW_THREADS AsmWriteMsr64(vecx, data); + Py_END_ALLOW_THREADS + Py_INCREF(Py_None); + return Py_None; +} + +PyDoc_STRVAR(efi_wrmsr_ex__doc__, +"wrmsr_ex(cpu, msr, lower_32bits, higher_32bits) -> None\n\ +\n\ +Writes higher_32bits:lower_32bits to the given msr.\n\ +\n\ +Parameters:\n\ + cpu - The cpu number in hex or int format\n\ + msr - The msr in hex or int format\n\ + lower_32bits - The lower 32 bit data for the msr\n\ + higher_32bits - The higher 32 bit data for the msr\n\ +\n\ +Return Value:\n\ + None\n\ +"); + +static PyObject * +edk2_wrmsr_ex(PyObject *self, PyObject *args) +{ + unsigned int cpu, msr, veax, vedx; + unsigned int bsp_switched = 0; + EFI_STATUS status = 0; + UINT64 data = 0; + + if (!PyArg_ParseTuple(args, "IIII", &cpu, &msr, &veax, &vedx)) + return NULL; + data = (((UINT64)vedx) << 32) | veax; + + Py_BEGIN_ALLOW_THREADS + if (cpu != gBSPProcessorNumber && cpu < gNumberOfProcessors) + { + //switch the BSP to the cpu + status = MpServicesSwitchBSP(gpMpService, cpu); + if (!EFI_ERROR(status)) + { + bsp_switched = 1; + } + } + // write to MSR + AsmWriteMsr64(msr, data); + + if (bsp_switched) + { + // switch BSP to the saved BSP processor + MpServicesSwitchBSP(gpMpService, gBSPProcessorNumber); + // update the saved BSP processor + MpServicesWhoAmI(gpMpService, &gBSPProcessorNumber); + } Py_END_ALLOW_THREADS Py_INCREF(Py_None); return Py_None; @@ -4576,7 +4714,9 @@ static PyMethodDef edk2_methods[] = { #endif {"abort", edk2_abort, METH_NOARGS, edk2_abort__doc__}, {"rdmsr", edk2_rdmsr, METH_VARARGS, efi_rdmsr__doc__}, + {"rdmsr_ex", edk2_rdmsr_ex, METH_VARARGS, efi_rdmsr_ex__doc__}, {"wrmsr", edk2_wrmsr, METH_VARARGS, efi_wrmsr__doc__}, + {"wrmsr_ex", edk2_wrmsr_ex, METH_VARARGS, efi_wrmsr_ex__doc__}, {"readpci", edk2_readpci, METH_VARARGS, efi_readpci__doc__}, {"writepci", edk2_writepci, METH_VARARGS, efi_writepci__doc__}, {"readmem", posix_readmem, METH_VARARGS, efi_readmem__doc__}, @@ -4813,13 +4953,24 @@ static struct PyModuleDef edk2module = { PyMODINIT_FUNC PyEdk2__Init(void) { - PyObject *m; + PyObject *m; + EFI_STATUS Status = 0; #ifndef UEFI_C_SOURCE PyObject *v; #endif + Status = gBS->LocateProtocol(&gEfiMpServiceProtocolGuid, NULL, &gpMpService); + if (EFI_ERROR(Status)) + { + printf("Unable to locate the Protocol MpServices protocol: %r\n", Status); + return NULL; + } + // Get the current BSP processor number + MpServicesWhoAmI(gpMpService, &gBSPProcessorNumber); + // Get the number of processors + MpServicesGetNumberOfProcessors(gpMpService, &gNumberOfProcessors, &gNumberOfEnabledProcessors); - m = PyModule_Create(&edk2module); + m = PyModule_Create(&edk2module); if (m == NULL) return m; @@ -4870,7 +5021,7 @@ PyEdk2__Init(void) //PyModule_AddObject(m, "statvfs_result", // (PyObject*) &StatVFSResultType); initialized = 1; - return m; + return m; } diff --git a/AppPkg/Applications/Python/Python-3.6.8/Python368.inf b/AppPkg/Applications/Python/Python-3.6.8/Python368.inf index 8b7f677..eca98b5 100644 --- a/AppPkg/Applications/Python/Python-3.6.8/Python368.inf +++ b/AppPkg/Applications/Python/Python-3.6.8/Python368.inf @@ -46,6 +46,9 @@ #BsdSocketLib #EfiSocketLib +[Protocols] + gEfiMpServiceProtocolGuid ## CONSUMES + [FixedPcd] gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask|0x0F gEfiMdePkgTokenSpaceGuid.PcdDebugPrintErrorLevel|0x80000040 -- 2.40.0.windows.1 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#117570): https://edk2.groups.io/g/devel/message/117570 Mute This Topic: https://groups.io/mt/105437325/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-