aboutsummaryrefslogtreecommitdiffstats
path: root/com32/modules/ifcpu64.c
blob: c59791914d042dec7a0d4ab584ca88a0f1e5d7e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2008 H. Peter Anvin - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 *   Boston MA 02110-1301, USA; either version 2 of the License, or
 *   (at your option) any later version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * ifcpu64.c
 *
 * Run one command if the CPU has 64-bit support, and another if it doesn't.
 * Eventually this and other features should get folded into some kind
 * of scripting engine.
 *
 * Usage:
 *
 *    label boot_kernel
 *        kernel ifcpu64.c
 *        append boot_kernel_64 [-- boot_kernel_32pae] -- boot_kernel_32
 *    label boot_kernel_32
 *        kernel vmlinuz_32
 *        append ...
 *    label boot_kernel_64
 *        kernel vmlinuz_64
 *        append ...
 */

#include <alloca.h>
#include <stdlib.h>
#include <string.h>
#include <cpuid.h>
#include <syslinux/boot.h>

static bool __constfunc cpu_has_cpuid(void)
{
  return cpu_has_eflag(X86_EFLAGS_ID);
}

static bool __constfunc cpu_has_level(uint32_t level)
{
  uint32_t group;
  uint32_t limit;

  if (!cpu_has_cpuid())
    return false;

  group = level & 0xffff0000;
  limit = cpuid_eax(group);

  if ((limit & 0xffff0000) != group)
    return false;

  if (level > limit)
    return false;

  return true;
}

/* This only supports feature groups 0 and 1, corresponding to the
   Intel and AMD EDX bit vectors.  We can add more later if need be. */
static bool __constfunc cpu_has_feature(int x)
{
  uint32_t level = ((x & 1) << 31) | 1;

  return cpu_has_level(level) && ((cpuid_edx(level) >> (x & 31) & 1));
}

/* XXX: this really should be librarized */
static void boot_args(char **args)
{
  int len = 0;
  char **pp;
  const char *p;
  char c, *q, *str;

  for (pp = args; *pp; pp++)
    len += strlen(*pp);

  q = str = alloca(len+1);
  for (pp = args; *pp; pp++) {
    p = *pp;
    while ((c = *p++))
      *q++ = c;
  }
  *q = '\0';

  if (!str[0])
    syslinux_run_default();
  else
    syslinux_run_command(str);
}

int main(int argc, char *argv[])
{
  char **args[3];
  int i;
  int n;

  args[0] = &argv[1];
  n = 1;
  for (i = 1; i < argc; i++) {
    if (!strcmp(argv[i], "--")) {
      argv[i] = NULL;
      args[n++] = &argv[i+1];
    }
    if (n >= 3)
      break;
  }
  while (n < 3) {
    args[n] = args[n-1];
    n++;
  }

  boot_args(cpu_has_feature(X86_FEATURE_LM)  ? args[0] :
	    cpu_has_feature(X86_FEATURE_PAE) ? args[1] :
	    args[2]);
  return -1;
}