| 1 | /* $Id: kLdrMisc.c 3573 2007-08-31 04:09:23Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | *
|
|---|
| 4 | * kLdr - The Dynamic Loader, Misc APIs.
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright (c) 2006-2007 knut st. osmundsen <bird@anduin.net>
|
|---|
| 7 | *
|
|---|
| 8 | *
|
|---|
| 9 | * This file is part of kLdr.
|
|---|
| 10 | *
|
|---|
| 11 | * kLdr is free software; you can redistribute it and/or modify
|
|---|
| 12 | * it under the terms of the GNU General Public License as published by
|
|---|
| 13 | * the Free Software Foundation; either version 2 of the License, or
|
|---|
| 14 | * (at your option) any later version.
|
|---|
| 15 | *
|
|---|
| 16 | * kLdr is distributed in the hope that it will be useful,
|
|---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 19 | * GNU General Public License for more details.
|
|---|
| 20 | *
|
|---|
| 21 | * You should have received a copy of the GNU General Public License
|
|---|
| 22 | * along with kLdr; if not, write to the Free Software
|
|---|
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|---|
| 24 | *
|
|---|
| 25 | */
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 | /*******************************************************************************
|
|---|
| 29 | * Header Files *
|
|---|
| 30 | *******************************************************************************/
|
|---|
| 31 | #include <k/kLdr.h>
|
|---|
| 32 | #include "kLdrInternal.h"
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 | /**
|
|---|
| 36 | * Compares arch+cpu some code was generated for with a arch+cpu for executing it
|
|---|
| 37 | * to see if it'll work out fine or not.
|
|---|
| 38 | *
|
|---|
| 39 | * @returns 0 if the code is compatible with the cpu.
|
|---|
| 40 | * @returns KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE if the arch+cpu isn't compatible with the code.
|
|---|
| 41 | * @param enmCodeArch The architecture the code was generated for.
|
|---|
| 42 | * @param enmCodeCpu The cpu the code was generated for.
|
|---|
| 43 | * @param enmArch The architecture to run it on.
|
|---|
| 44 | * @param enmCpu The cpu to run it on.
|
|---|
| 45 | */
|
|---|
| 46 | int kLdrCompareCpus(KLDRARCH enmCodeArch, KLDRCPU enmCodeCpu, KLDRARCH enmArch, KLDRCPU enmCpu)
|
|---|
| 47 | {
|
|---|
| 48 | /*
|
|---|
| 49 | * Compare arch and cpu.
|
|---|
| 50 | */
|
|---|
| 51 | if (enmCodeArch != enmArch)
|
|---|
| 52 | return KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
|---|
| 53 |
|
|---|
| 54 | /* exact match is nice. */
|
|---|
| 55 | if (enmCodeCpu == enmCpu)
|
|---|
| 56 | return 0;
|
|---|
| 57 | switch (enmArch)
|
|---|
| 58 | {
|
|---|
| 59 | case KLDRARCH_X86_16:
|
|---|
| 60 | if (enmCpu < KLDRCPU_FIRST_X86_16 || enmCpu > KLDRCPU_LAST_X86_16)
|
|---|
| 61 | return KLDR_ERR_INVALID_PARAMETER;
|
|---|
| 62 |
|
|---|
| 63 | /* intel? */
|
|---|
| 64 | if (enmCodeCpu <= KLDRCPU_CORE2_16)
|
|---|
| 65 | {
|
|---|
| 66 | /* also intel? */
|
|---|
| 67 | if (enmCpu <= KLDRCPU_CORE2_16)
|
|---|
| 68 | return enmCodeCpu <= enmCpu ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
|---|
| 69 | switch (enmCpu)
|
|---|
| 70 | {
|
|---|
| 71 | case KLDRCPU_K6_16:
|
|---|
| 72 | return enmCodeCpu <= KLDRCPU_I586 ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
|---|
| 73 | case KLDRCPU_K7_16:
|
|---|
| 74 | case KLDRCPU_K8_16:
|
|---|
| 75 | default:
|
|---|
| 76 | return enmCodeCpu <= KLDRCPU_I686 ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
|---|
| 77 | }
|
|---|
| 78 | }
|
|---|
| 79 | /* amd */
|
|---|
| 80 | return enmCpu >= KLDRCPU_K6_16 && enmCpu <= KLDRCPU_K8_16
|
|---|
| 81 | ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
|---|
| 82 |
|
|---|
| 83 | case KLDRARCH_X86_32:
|
|---|
| 84 | if (enmCpu < KLDRCPU_FIRST_X86_32 || enmCpu > KLDRCPU_LAST_X86_32)
|
|---|
| 85 | return KLDR_ERR_INVALID_PARAMETER;
|
|---|
| 86 |
|
|---|
| 87 | /* blend? */
|
|---|
| 88 | if (enmCodeCpu == KLDRCPU_X86_32_BLEND)
|
|---|
| 89 | return 0;
|
|---|
| 90 |
|
|---|
| 91 | /* intel? */
|
|---|
| 92 | if (enmCodeCpu <= KLDRCPU_CORE2_32)
|
|---|
| 93 | {
|
|---|
| 94 | /* also intel? */
|
|---|
| 95 | if (enmCpu <= KLDRCPU_CORE2_32)
|
|---|
| 96 | return enmCodeCpu <= enmCpu ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
|---|
| 97 | switch (enmCpu)
|
|---|
| 98 | {
|
|---|
| 99 | case KLDRCPU_K6:
|
|---|
| 100 | return enmCodeCpu <= KLDRCPU_I586 ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
|---|
| 101 | case KLDRCPU_K7:
|
|---|
| 102 | case KLDRCPU_K8_32:
|
|---|
| 103 | default:
|
|---|
| 104 | return enmCodeCpu <= KLDRCPU_I686 ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
|---|
| 105 | }
|
|---|
| 106 | }
|
|---|
| 107 | /* amd */
|
|---|
| 108 | return enmCpu >= KLDRCPU_K6 && enmCpu <= KLDRCPU_K8_32
|
|---|
| 109 | ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
|---|
| 110 |
|
|---|
| 111 | case KLDRARCH_AMD64:
|
|---|
| 112 | if (enmCpu < KLDRCPU_FIRST_AMD64 || enmCpu > KLDRCPU_LAST_AMD64)
|
|---|
| 113 | return KLDR_ERR_INVALID_PARAMETER;
|
|---|
| 114 |
|
|---|
| 115 | /* blend? */
|
|---|
| 116 | if (enmCodeCpu == KLDRCPU_AMD64_BLEND)
|
|---|
| 117 | return 0;
|
|---|
| 118 | /* this is simple for now. */
|
|---|
| 119 | return KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
|---|
| 120 |
|
|---|
| 121 | default:
|
|---|
| 122 | break;
|
|---|
| 123 | }
|
|---|
| 124 | return KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 | /**
|
|---|
| 129 | * Gets the arch+cpu of the calling cpu.
|
|---|
| 130 | *
|
|---|
| 131 | * @param penmArch Where to store the cpu architecture.
|
|---|
| 132 | * @param penmCpu Where to store the cpu brand/model.
|
|---|
| 133 | */
|
|---|
| 134 | void kLdrGetArchCpu(PKLDRARCH penmArch, PKLDRCPU penmCpu)
|
|---|
| 135 | {
|
|---|
| 136 | #ifdef __AMD64__
|
|---|
| 137 | *penmArch = KLDRARCH_AMD64;
|
|---|
| 138 | *penmCpu = KLDRCPU_AMD64_BLEND; /** @todo check it using cpu. */
|
|---|
| 139 |
|
|---|
| 140 | #elif defined(__X86__)
|
|---|
| 141 | *penmArch = KLDRARCH_X86_32;
|
|---|
| 142 | *penmCpu = KLDRCPU_X86_32_BLEND; /** @todo check it using cpu. */
|
|---|
| 143 |
|
|---|
| 144 | #else
|
|---|
| 145 | # error "Port me"
|
|---|
| 146 | #endif
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|