public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Vishal R" <vishal.r@intel.com>
To: devel@edk2.groups.io
Cc: Vishal R <vishal.r@intel.com>, Rebecca Cran <rebecca@bsdio.com>,
	Michael D Kinney <michael.d.kinney@intel.com>,
	Jayaprakash N <n.jayaprakash@intel.com>
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	[thread overview]
Message-ID: <20240517063826.987-2-vishal.r@intel.com> (raw)
In-Reply-To: <20240517063826.987-1-vishal.r@intel.com>

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 <rebecca@bsdio.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Jayaprakash N <n.jayaprakash@intel.com>
Signed-off-by: Vishal R <vishal.r@intel.com>
---
 .../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]
-=-=-=-=-=-=-=-=-=-=-=-



  reply	other threads:[~2024-05-17  6:39 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-17  6:38 [edk2-devel] [edk2-libc Patch 0/1] Add cpuid_ex API to execute CPUID instructions on specific CPU Vishal R
2024-05-17  6:38 ` Vishal R [this message]
2024-05-17 14:02   ` [edk2-devel] [edk2-libc Patch 1/1] edk2-libc: " Jayaprakash, N

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240517063826.987-2-vishal.r@intel.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox