| 1 | /* $Id: kLdrMisc.c 2944 2007-01-13 15:55:40Z 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 "kLdr.h"
 | 
|---|
| 32 | #include "kLdrHlp.h"
 | 
|---|
| 33 | #include "kLdrInternal.h"
 | 
|---|
| 34 | 
 | 
|---|
| 35 | 
 | 
|---|
| 36 | /**
 | 
|---|
| 37 |  * Compares arch+cpu some code was generated for with a arch+cpu for executing it
 | 
|---|
| 38 |  * to see if it'll work out fine or not.
 | 
|---|
| 39 |  *
 | 
|---|
| 40 |  * @returns 0 if the code is compatible with the cpu.
 | 
|---|
| 41 |  * @returns KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE if the arch+cpu isn't compatible with the code.
 | 
|---|
| 42 |  * @param   enmCodeArch The architecture the code was generated for.
 | 
|---|
| 43 |  * @param   enmCodeCpu  The cpu the code was generated for.
 | 
|---|
| 44 |  * @param   enmArch     The architecture to run it on.
 | 
|---|
| 45 |  * @param   enmCpu      The cpu to run it on.
 | 
|---|
| 46 |  */
 | 
|---|
| 47 | int kLdrCompareCpus(KLDRARCH enmCodeArch, KLDRCPU enmCodeCpu, KLDRARCH enmArch, KLDRCPU enmCpu)
 | 
|---|
| 48 | {
 | 
|---|
| 49 |     /*
 | 
|---|
| 50 |      * Compare arch and cpu.
 | 
|---|
| 51 |      */
 | 
|---|
| 52 |     if (enmCodeArch != enmArch)
 | 
|---|
| 53 |         return KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
 | 
|---|
| 54 | 
 | 
|---|
| 55 |     /* exact match is nice. */
 | 
|---|
| 56 |     if (enmCodeCpu == enmCpu)
 | 
|---|
| 57 |         return 0;
 | 
|---|
| 58 |     switch (enmArch)
 | 
|---|
| 59 |     {
 | 
|---|
| 60 |         case KLDRARCH_X86_16:
 | 
|---|
| 61 |             if (enmCpu < KLDRCPU_FIRST_X86_16 || enmCpu > KLDRCPU_LAST_X86_16)
 | 
|---|
| 62 |                 return KLDR_ERR_INVALID_PARAMETER;
 | 
|---|
| 63 | 
 | 
|---|
| 64 |             /* intel? */
 | 
|---|
| 65 |             if (enmCodeCpu <= KLDRCPU_CORE2_16)
 | 
|---|
| 66 |             {
 | 
|---|
| 67 |                 /* also intel? */
 | 
|---|
| 68 |                 if (enmCpu <= KLDRCPU_CORE2_16)
 | 
|---|
| 69 |                     return enmCodeCpu <= enmCpu ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
 | 
|---|
| 70 |                 switch (enmCpu)
 | 
|---|
| 71 |                 {
 | 
|---|
| 72 |                     case KLDRCPU_K6_16:
 | 
|---|
| 73 |                         return enmCodeCpu <= KLDRCPU_I586 ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
 | 
|---|
| 74 |                     case KLDRCPU_K7_16:
 | 
|---|
| 75 |                     case KLDRCPU_K8_16:
 | 
|---|
| 76 |                     default:
 | 
|---|
| 77 |                         return enmCodeCpu <= KLDRCPU_I686 ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
 | 
|---|
| 78 |                 }
 | 
|---|
| 79 |             }
 | 
|---|
| 80 |             /* amd */
 | 
|---|
| 81 |             return enmCpu >= KLDRCPU_K6_16 && enmCpu <= KLDRCPU_K8_16
 | 
|---|
| 82 |                     ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
 | 
|---|
| 83 | 
 | 
|---|
| 84 |         case KLDRARCH_X86_32:
 | 
|---|
| 85 |             if (enmCpu < KLDRCPU_FIRST_X86_32 || enmCpu > KLDRCPU_LAST_X86_32)
 | 
|---|
| 86 |                 return KLDR_ERR_INVALID_PARAMETER;
 | 
|---|
| 87 | 
 | 
|---|
| 88 |             /* blend? */
 | 
|---|
| 89 |             if (enmCodeCpu == KLDRCPU_X86_32_BLEND)
 | 
|---|
| 90 |                 return 0;
 | 
|---|
| 91 | 
 | 
|---|
| 92 |             /* intel? */
 | 
|---|
| 93 |             if (enmCodeCpu <= KLDRCPU_CORE2_32)
 | 
|---|
| 94 |             {
 | 
|---|
| 95 |                 /* also intel? */
 | 
|---|
| 96 |                 if (enmCpu <= KLDRCPU_CORE2_32)
 | 
|---|
| 97 |                     return enmCodeCpu <= enmCpu ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
 | 
|---|
| 98 |                 switch (enmCpu)
 | 
|---|
| 99 |                 {
 | 
|---|
| 100 |                     case KLDRCPU_K6:
 | 
|---|
| 101 |                         return enmCodeCpu <= KLDRCPU_I586 ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
 | 
|---|
| 102 |                     case KLDRCPU_K7:
 | 
|---|
| 103 |                     case KLDRCPU_K8_32:
 | 
|---|
| 104 |                     default:
 | 
|---|
| 105 |                         return enmCodeCpu <= KLDRCPU_I686 ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
 | 
|---|
| 106 |                 }
 | 
|---|
| 107 |             }
 | 
|---|
| 108 |             /* amd */
 | 
|---|
| 109 |             return enmCpu >= KLDRCPU_K6 && enmCpu <= KLDRCPU_K8_32
 | 
|---|
| 110 |                     ? 0 : KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
 | 
|---|
| 111 | 
 | 
|---|
| 112 |         case KLDRARCH_AMD64:
 | 
|---|
| 113 |             if (enmCpu < KLDRCPU_FIRST_AMD64 || enmCpu > KLDRCPU_LAST_AMD64)
 | 
|---|
| 114 |                 return KLDR_ERR_INVALID_PARAMETER;
 | 
|---|
| 115 | 
 | 
|---|
| 116 |             /* blend? */
 | 
|---|
| 117 |             if (enmCodeCpu == KLDRCPU_AMD64_BLEND)
 | 
|---|
| 118 |                 return 0;
 | 
|---|
| 119 |             /* this is simple for now. */
 | 
|---|
| 120 |             return KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
 | 
|---|
| 121 | 
 | 
|---|
| 122 |         default:
 | 
|---|
| 123 |             break;
 | 
|---|
| 124 |     }
 | 
|---|
| 125 |     return KLDR_ERR_ARCH_CPU_NOT_COMPATIBLE;
 | 
|---|
| 126 | }
 | 
|---|
| 127 | 
 | 
|---|
| 128 | 
 | 
|---|
| 129 | /**
 | 
|---|
| 130 |  * Gets the arch+cpu of the calling cpu.
 | 
|---|
| 131 |  *
 | 
|---|
| 132 |  * @param   penmArch    Where to store the cpu architecture.
 | 
|---|
| 133 |  * @param   penmCpu     Where to store the cpu brand/model.
 | 
|---|
| 134 |  */
 | 
|---|
| 135 | void kLdrGetArchCpu(PKLDRARCH penmArch, PKLDRCPU penmCpu)
 | 
|---|
| 136 | {
 | 
|---|
| 137 | #ifdef __AMD64__
 | 
|---|
| 138 |     *penmArch = KLDRARCH_AMD64;
 | 
|---|
| 139 |     *penmCpu = KLDRCPU_AMD64_BLEND; /** @todo check it using cpu. */
 | 
|---|
| 140 | 
 | 
|---|
| 141 | #elif defined(__X86__)
 | 
|---|
| 142 |     *penmArch = KLDRARCH_X86_32;
 | 
|---|
| 143 |     *penmCpu = KLDRCPU_X86_32_BLEND; /** @todo check it using cpu. */
 | 
|---|
| 144 | 
 | 
|---|
| 145 | #else
 | 
|---|
| 146 | # error "Port me"
 | 
|---|
| 147 | #endif
 | 
|---|
| 148 | }
 | 
|---|
| 149 | 
 | 
|---|