Skip to contentSkip to conversation

add cpumodelname mod for in grub.cfg to get cpu_mode_name, load different versions of the kernel through the path composed of names. like this: linux /cpu_mode_name/vmlinuz-5.7.7-amd64-desktop

PATCH In review ut000745 2 participants 2 messages Dec 13, 2021 – Dec 20, 2021

lore
Share & tools
lore

ut000745

author
#0 lore raw

PATCH 1/1 add cpumodelname mod for in grub.cfg to get cpu_mode_name, load different versions of the kernel through the path composed of names. like this: linux /cpu_mode_name/vmlinuz-5.7.7-amd64-desktop

---
 grub-core/Makefile.core.def            |  5 +++
 grub-core/commands/i386/cpumodelname.c | 60 ++++++++++++++++++++++++++
 2 files changed, 65 insertions(+)
 create mode 100644 grub-core/commands/i386/cpumodelname.c
grub-core/Makefile.core.def +5 -0
index 8022e1c0a..063a59063 100644
@@ -2527,3 +2527,8 @@ module = {
   common = commands/i386/wrmsr.c;
   enable = x86;
 };
+module = {
+  name = cpu_model_name;
+  common = commands/i386/cpumodelname.c;
+  enable = x86;
+};
\ No newline at end of file
grub-core/commands/i386/cpumodelname.c new file +60 -0

61 lines not on this page · raw message

Signature · 1 line
2.20.1
#1 lore raw
Quoted 35 lines · On Mon, Dec 13, 2021 at 10:04:40AM +0800, ut000745 wrote:
---
 grub-core/Makefile.core.def            |  5 +++
 grub-core/commands/i386/cpumodelname.c | 60 ++++++++++++++++++++++++++
 2 files changed, 65 insertions(+)
 create mode 100644 grub-core/commands/i386/cpumodelname.c
grub-core/Makefile.core.def +5 -0
index 8022e1c0a..063a59063 100644
@@ -2527,3 +2527,8 @@ module = {
   common = commands/i386/wrmsr.c;
   enable = x86;
 };
+module = {
+  name = cpu_model_name;
+  common = commands/i386/cpumodelname.c;
+  enable = x86;
+};
\ No newline at end of file
grub-core/commands/i386/cpumodelname.c new file +8 -0
new file mode 100644
index 000000000..dc63be6d0
@@ -0,0 +1,60 @@
+#include <grub/mm.h>
+#include <grub/dl.h>
+#include <grub/env.h>
+#include <grub/command.h>
+
+GRUB_MOD_LICENSE ("GPLv3+");
+
+static void

grub-core/commands/i386/cpumodelname.c line 18

Quoted 10 lines
grub-core/commands/i386/cpumodelname.c new file +10 -0
+grub_cpuid_model(unsigned int op, grub_uint32_t *a, grub_uint32_t *b, grub_uint32_t *c, grub_uint32_t *d)
+{
+    __asm__ __volatile__ (".byte 0x53\n\tcpuid\n\t"
+    "movl %%ebx, %%esi\n\t.byte 0x5b"
+    : "=a" (*a),
+    "=S" (*b),
+    "=c" (*c),
+    "=d" (*d)
+    : "a" (op));
+}

You can find grub_cpuid() macro in the include/grub/i386/cpuid.h file. Please use it.

Quoted 4 lines
grub-core/commands/i386/cpumodelname.c new file +4 -0
+static grub_err_t grub_get_model_name(struct grub_command *cmd __attribute__((unused)),
+    int argc __attribute__((unused)),
+    char *argv[] __attribute__((unused)))
+{

grub-core/commands/i386/cpumodelname.c line 33

Quoted 10 lines
grub-core/commands/i386/cpumodelname.c new file +10 -0
+    char *ret = NULL;
+    grub_uint32_t x86_model_id[12];
+
+    char cpumodelname[6] = {0};
+    const char *list[] = {"Intel","AMD","zhaoxin"};
+    unsigned i;
+
+    grub_cpuid_model(0x80000002, &x86_model_id[0], &x86_model_id[1], &x86_model_id[2], &x86_model_id[3]);
+    grub_cpuid_model(0x80000003, &x86_model_id[4], &x86_model_id[5], &x86_model_id[6], &x86_model_id[7]);
+    grub_cpuid_model(0x80000004, &x86_model_id[8], &x86_model_id[9], &x86_model_id[10], &x86_model_id[11]);

Missing spaces before "(". Same below. You can use grub-core/kern/efi/sb.c as a reference for GRUB coding convention. Please take a look at [1] too.

grub-core/commands/i386/cpumodelname.c line 43

Quoted 9 lines
grub-core/commands/i386/cpumodelname.c new file +9 -0
+    for (i = 0; i < sizeof(list)/sizeof(list[0]); i++)
+    {
+        ret = grub_strstr ((char *)x86_model_id, list[i]);
+        if (ret != NULL)
+        {
+            grub_strncpy(cpumodelname, list[i], grub_strlen(list[i]) + 1);
+            break;
+        }
+    }

In general I think this implementation is lacking at least some minimal cpuid checks. Please take a look at the arch/x86/kernel/cpu/common.c file in the Linux kernel. I think this is good reference point.

grub-core/commands/i386/cpumodelname.c line 55

Quoted 11 lines
grub-core/commands/i386/cpumodelname.c new file +11 -0
+    grub_env_set ("cpu_model_name", cpumodelname);
+    grub_env_export("cpu_model_name");
+
+    return 0;
+}
+
+static grub_command_t cmd_cpu_model_name;
+
+GRUB_MOD_INIT(cpu_model_name) {
+    cmd_cpu_model_name = grub_register_command("cpu_model_name", grub_get_model_name,
+        0, "cpu model name");

We do not need a command for this. The variable should be defined unconditionally.

grub-core/commands/i386/cpumodelname.c line 60

Quoted 5 lines
grub-core/commands/i386/cpumodelname.c new file +5 -0
+}
+
+GRUB_MOD_FINI(cpu_model_name) {
+    grub_unregister_command(cmd_cpu_model_name);
+}

This whole cpu_model_name init code should go into the GRUB kernel. Please take a look how grub_tsc_init() is defined and used. I think you should do something similar.

Daniel

[1] https://www.gnu.org/software/grub/manual/grub-dev/grub-dev.html#Coding-style