From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail05.groups.io (mail05.groups.io [45.79.224.7]) by spool.mail.gandi.net (Postfix) with ESMTPS id A062D74003A for ; Fri, 17 May 2024 06:39:00 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=VSfnt4hILNevl8BLhFBFlwcBXhrF/LPSyiAp7lx0hb4=; 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=1715927939; v=1; b=bfPG7Mwy5JAKm3CemT2kVH7iqGhXwA5CxFkZMaVpP9dbeg6avs9OS8m4b2zPFpcOOScMT4Aj Ml3B+Ut2syo0FPGeYoNGbdsgf5qQg/EIy2wXkRYxBe4UjuPHYWbpaxq33OmgJ1Zs1YJRKWDAFPw EsO8KySABa32i+MYnwTkZ9WCE/loTSGPJq0ZhgEnSF7uyz3pgEZnE5bPF71wCa+0Isuh3UEJWCA YcfYF3lu3QCRz6GeUOt7IxKwEGYS9LPBJteznDLGH4tqmAbhh0pKQegHh0JYSuSrB7zePEpfNwf pV1HsPFcpkrTZnDy88eNh19BPxnSmEkTYve2lkzvT0a+A== X-Received: by 127.0.0.2 with SMTP id VdQ7YY7687511xLkQtfnlgRs; Thu, 16 May 2024 23:38:59 -0700 X-Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.19]) by mx.groups.io with SMTP id smtpd.web10.34057.1715927936722512407 for ; Thu, 16 May 2024 23:38:58 -0700 X-CSE-ConnectionGUID: QcwLn7PQSbOU9CoO9YqmSg== X-CSE-MsgGUID: C8ont1SgQ2C9Md+USPF3ew== X-IronPort-AV: E=McAfee;i="6600,9927,11074"; a="11936033" X-IronPort-AV: E=Sophos;i="6.08,166,1712646000"; d="scan'208";a="11936033" X-Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa111.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 May 2024 23:38:58 -0700 X-CSE-ConnectionGUID: kESF13lCQ9K4YXbmMJSsKg== X-CSE-MsgGUID: pGYARhTER4G3U9cyc8NKxA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,166,1712646000"; d="scan'208";a="62898408" X-Received: from lchen91-mobl2.gar.corp.intel.com (HELO vishalr1-mobl2.gar.corp.intel.com) ([10.247.116.4]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 May 2024 23:38:56 -0700 From: "Vishal R" To: devel@edk2.groups.io Cc: Vishal R , Rebecca Cran , Michael D Kinney , Jayaprakash N Subject: [edk2-devel] [edk2-libc Patch 1/1] edk2-libc: Add cpuid_ex API to execute CPUID instructions on specific CPU Date: Fri, 17 May 2024 12:08:01 +0530 Message-ID: <20240517063826.987-2-vishal.r@intel.com> In-Reply-To: <20240517063826.987-1-vishal.r@intel.com> References: <20240517063826.987-1-vishal.r@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: Thu, 16 May 2024 23:38:58 -0700 Resent-From: vishal.r@intel.com Reply-To: devel@edk2.groups.io,vishal.r@intel.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: U6GoUoVxZqWKDGhY8NqeCGvFx7686176AA= 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=bfPG7Mwy; 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 45.79.224.7 as permitted sender) smtp.mailfrom=bounce@groups.io REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4749 This commit adds cpuid_ex function to edk2module to provide capability to execute cpuid instruction on specific cpu. This feature was requested via BZ 4749 Cc: Rebecca Cran Cc: Michael D Kinney Cc: Jayaprakash N Signed-off-by: Vishal R --- .../PyMod-3.6.8/Modules/edk2module.c | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) 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 163fc7f..d419428 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 @@ -45,6 +45,15 @@ typedef struct { UINT64 data; // data, to be filled by the AP function } AP_FUNCTION_MSR_ARGS; +typedef struct { + UINT32 eax; // eax value + UINT32 ecx; // ecx value + UINT32 rax_value; // retrun value for eax + UINT32 rbx_value; // return value for ebx + UINT32 rcx_value; // retrun value for ecx + UINT32 rdx_value; // return value for edx +} AP_FUNCTION_CPUID_ARGS; + #define AP_FUNCTION_EXECUTION_TIMEOUT 5000000 // setting the max time out value to be 5 seconds 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 ); @@ -223,6 +232,13 @@ VOID EFIAPI MSRWriteToRunOnAP(IN VOID *context) AsmWriteMsr64(msr, data); } +// CPUID execution function to run on specific cpu core using MPServices Protocol +VOID EFIAPI CPUIDToRunOnAP(IN VOID *context) +{ + AP_FUNCTION_CPUID_ARGS *args = (AP_FUNCTION_CPUID_ARGS *)context; + AsmCpuidEx( args->eax, args->ecx, &args->rax_value, &args->rbx_value, &args->rcx_value, &args->rdx_value); +} + #ifndef UEFI_C_SOURCE /* Return a dictionary corresponding to the POSIX environment table */ extern char **environ; @@ -4153,6 +4169,82 @@ edk2_cpuid(PyObject *self, PyObject *args) return Py_BuildValue("(IIII))", (unsigned long)rax_value, (unsigned long)rbx_value, (unsigned long)rcx_value, (unsigned long)rdx_value); } +PyDoc_STRVAR(efi_cpuid_ex__doc__, +"cpuid_ex(cpu, eax, ecx) -> (eax:ebx:ecx:edx)\n\ +Read the CPUID from a specific cpu.";); + +static PyObject * +edk2_cpuid_ex(PyObject *self, PyObject *args) +{ + UINT32 cpu, eax, ecx, rax_value, rbx_value, rcx_value, rdx_value; + BOOLEAN is_function_finished = FALSE; + EFI_STATUS status = 0; + AP_FUNCTION_CPUID_ARGS cpuid_args = {0}; + + if (!PyArg_ParseTuple(args, "III", &cpu, &eax, &ecx)) + return NULL; + Py_BEGIN_ALLOW_THREADS + + cpuid_args.eax = eax; + cpuid_args.ecx = ecx; + + if (cpu == gCurrentBSPProcessorNumber) + { + // cpu provided as input is same as the current BSP processor + // then directly call the CPUIDToRunOnAP function to execute + // cpuid instruction on current BSP processor itself. + CPUIDToRunOnAP(&cpuid_args); + } + else if (cpu < gNumberOfProcessors) + { + // if cpu provided as input is different from the current + // BSP processor and is less than the number of processors + // on this system, then make use of the MPService protocols + // StartupThisAP function to run the CPUIDToRunOnAP function on + // specific AP indicated by cpu parameter. + // Start the AP with the arguments structure + + status = gpMpService->StartupThisAP( + gpMpService, + CPUIDToRunOnAP, // Function to run + cpu, // AP number + NULL, // WaitEvent (optional) + AP_FUNCTION_EXECUTION_TIMEOUT, // Timeout in microseconds + &cpuid_args, // Buffer to pass to the function + &is_function_finished // Finished (optional) + ); + if (EFI_ERROR(status)) + { + PyErr_SetString(PyExc_OSError, "Could not start the requested cpu"); + Py_INCREF(Py_None); + return Py_None; + } + + if (!is_function_finished) + { + PyErr_SetString(PyExc_OSError, + "Timeout while running the cpuid instruction on given cpu"); + Py_INCREF(Py_None); + return Py_None; + } + } + else + { + // if cpu provided exeeds the number of processors + // then set the ValueError exception and return Py_None + PyErr_SetString(PyExc_ValueError, + "Invalid cpu number provided"); + Py_INCREF(Py_None); + return Py_None; + } + + Py_END_ALLOW_THREADS + return Py_BuildValue("(IIII))", (unsigned long)cpuid_args.rax_value, + (unsigned long)cpuid_args.rbx_value, + (unsigned long)cpuid_args.rcx_value, + (unsigned long)cpuid_args.rdx_value); +} + PyDoc_STRVAR(efi_allocphysmem__doc__, "allocphysmem(length, max_pa) -> (va)\n\ Use malloc to allocate space in memory.";); @@ -4816,6 +4908,7 @@ static PyMethodDef edk2_methods[] = { {"swsmi", posix_swsmi, METH_VARARGS, efi_swsmi__doc__}, {"allocphysmem", posix_allocphysmem, METH_VARARGS, efi_allocphysmem__doc__}, {"cpuid", edk2_cpuid, METH_VARARGS, efi_cpuid__doc__}, + {"cpuid_ex", edk2_cpuid_ex, METH_VARARGS, efi_cpuid_ex__doc__}, {"GetVariable", MiscRT_GetVariable, METH_VARARGS, MiscRT_GetVariable__doc__}, {"GetNextVariableName", MiscRT_GetNextVariableName, METH_VARARGS, MiscRT_GetNextVariableName__doc__}, {"SetVariable", MiscRT_SetVariable, METH_VARARGS, MiscRT_SetVariable__doc__}, -- 2.44.0.windows.1 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#118985): https://edk2.groups.io/g/devel/message/118985 Mute This Topic: https://groups.io/mt/106149329/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-