VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/bootsectors/bs3kit/bs3-mode-CpuDetect.asm@ 60600

Last change on this file since 60600 was 60556, checked in by vboxsync, 9 years ago

bs3kit: far updates.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.1 KB
Line 
1; $Id: bs3-mode-CpuDetect.asm 60556 2016-04-18 19:26:35Z vboxsync $
2;; @file
3; BS3Kit - Bs3CpuDetect
4;
5
6;
7; Copyright (C) 2007-2016 Oracle Corporation
8;
9; This file is part of VirtualBox Open Source Edition (OSE), as
10; available from http://www.virtualbox.org. This file is free software;
11; you can redistribute it and/or modify it under the terms of the GNU
12; General Public License (GPL) as published by the Free Software
13; Foundation, in version 2 as it comes in the "COPYING" file of the
14; VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15; hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16;
17; The contents of this file may alternatively be used under the terms
18; of the Common Development and Distribution License Version 1.0
19; (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20; VirtualBox OSE distribution, in which case the provisions of the
21; CDDL are applicable instead of those of the GPL.
22;
23; You may elect to license modified versions of this file under the
24; terms and conditions of either the GPL or the CDDL or both.
25;
26
27%include "bs3kit-template-header.mac"
28
29BS3_EXTERN_DATA16 g_uBs3CpuDetected
30
31
32;;
33; Rough CPU detection, mainly for detecting really old CPUs.
34;
35; A Bs3CpuDetectEx can be added if this is insufficient.
36;
37; @returns BS3CPU_xxx in xAX.
38; @cproto BS3_DECL(BS3CPU) Bs3CpuDetect(void);
39;
40; @uses xAX.
41;
42; @remarks ASSUMES we're in ring-0 when not in some kind of real mode.
43;
44; @note We put the real mode version of this code in the RMTEXT16 segment
45; to save space elsewhere. We generate a far call stub that goes
46; to the right segment.
47;
48%if TMPL_MODE == BS3_MODE_RM
49BS3_BEGIN_RMTEXT16
50BS3_PROC_BEGIN_MODE Bs3CpuDetect, BS3_PBC_FAR
51%else
52TMPL_BEGIN_TEXT
53BS3_PROC_BEGIN_MODE Bs3CpuDetect, BS3_PBC_HYBRID
54%endif
55CPU 8086
56 push xBP
57 mov xBP, xSP
58 pushf
59 push xCX
60 push xDX
61 push xBX
62
63%ifndef TMPL_CMN_PAGING
64 %ifdef TMPL_RM
65 %if 1 ; this is simpler
66 ;
67 ; FLAGS bits 15:12 are always set on 8086, 8088, V20, V30, 80186, and
68 ; 80188. FLAGS bit 15 is always zero on 286+, whereas bit 14 is NT and
69 ; bits 13:12 are IOPL.
70 ;
71 test byte [xBP - xCB + 1], 80h ; Top byte of saved flags.
72 jz .286plus
73 %else
74 ;
75 ; When executing 'PUSH SP' the 8086, 8088, V20, V30, 80186, and 80188
76 ; should be pushing the updated SP value instead of the initial one.
77 ;
78 push xSP
79 pop xAX
80 cmp xAX, xSP
81 je .286plus
82 %endif
83
84 ;
85 ; Older than 286.
86 ;
87 ; Detect 8086/8088/V20/V30 vs. 80186/80188 by checking for pre 80186
88 ; shift behavior. the 80186/188 and later will mask the CL value according
89 ; to the width of the destination register, whereas 8086/88 and V20/30 will
90 ; perform the exact number of shifts specified.
91 ;
92 mov cl, 20h ; Shift count; 80186/88 and later will mask this by 0x1f (or 0xf)?
93 mov dx, 7fh
94 shl dx, cl
95 cmp dx, 7fh ; If no change, this is a 80186/88.
96 mov xAX, BS3CPU_80186
97 je .return
98
99 ;
100 ; Detect 8086/88 vs V20/30 by exploiting undocumented POP CS encoding
101 ; that was redefined on V20/30 to SET1.
102 ;
103 xor ax, ax ; clear
104 push cs
105 db 0fh ; 8086/88: pop cs V20/30: set1 bl,cl
106 db 14h, 3ch ; 8086/88: add al, 3ch
107 ; 8086/88: al = 3ch V20/30: al = 0, cs on stack, bl modified.
108 cmp al, 3ch
109 jne .is_v20_or_v30
110 mov xAX, BS3CPU_8086
111 jmp .return
112
113.is_v20_or_v30:
114 pop xCX ; unclaimed CS
115 mov xAX, BS3CPU_V20
116 jmp .return
117
118 %endif ; TMPL_RM
119
120CPU 286
121.286plus:
122 ;
123 ; The 4th bit of the machine status word / CR0 indicates the precense
124 ; of a 80387 or later co-processor (a 80287+80386 => ET=0). 486 and
125 ; later should be hardcoding this to 1, according to the documentation
126 ; (need to test on 486SX). The initial idea here then would be to
127 ; assume 386+ if ET=1.
128 ;
129 ; However, it turns out the 286 I've got here has bits 4 thru 15 all
130 ; set. This is very nice though, because only bits 4 and 5 are defined
131 ; on later CPUs and the remainder MBZ. So, check whether any of the MBZ
132 ; bits are set, if so, then it's 286.
133 ;
134 smsw ax
135 test ax, ~(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS | X86_CR0_ET | X86_CR0_NE)
136 jnz .is_286
137
138 ;
139 ; Detect 80286 by checking whether the IOPL and NT bits of EFLAGS can be
140 ; modified or not. There are different accounts of these bits. Dr.Dobb's
141 ; (http://www.drdobbs.com/embedded-systems/processor-detection-schemes/184409011)
142 ; say they are undefined on 286es and will always be zero. Whereas Intel
143 ; iAPX 286 Programmer's Reference Manual (both order #210498-001 and
144 ; #210498-003) documents both IOPL and NT, but with comment 4 on page
145 ; C-43 stating that they cannot be POPFed in real mode and will both
146 ; remain 0. This is different from the 386+, where the NT flag isn't
147 ; privileged according to page 3-37 in #230985-003. Later Intel docs
148 ; (#235383-052US, page 4-192) documents real mode as taking both NT and
149 ; IOPL from what POPF reads off the stack - which is the behavior
150 ; observed a 386SX here.
151 ;
152 test al, X86_CR0_PE ; This flag test doesn't work in protected mode, ...
153 jnz .386plus ; ... so ASSUME 386plus if in PE for now.
154
155 pushf ; Save a copy of the original flags for restoring IF.
156 pushf
157 pop ax
158 xor ax, X86_EFL_IOPL | X86_EFL_NT ; Try modify IOPL and NT.
159 and ax, ~X86_EFL_IF ; Try clear IF.
160 push ax ; Load modified flags.
161 popf
162 pushf ; Get actual flags.
163 pop dx
164 popf ; Restore IF, IOPL and NT.
165 cmp ax, dx
166 je .386plus ; If any of the flags are set, we're on 386+.
167
168 ; While we could in theory be in v8086 mode at this point and be fooled
169 ; by a flaky POPF implementation, we assume this isn't the case in our
170 ; execution environment.
171
172.is_286:
173 mov ax, BS3CPU_80286
174 jmp .return
175%endif ; !TMPL_CMN_PAGING
176
177CPU 386
178.386plus:
179 ;
180 ; Check for CPUID and AC. The former flag indicates CPUID support, the
181 ; latter was introduced with the 486.
182 ;
183 mov ebx, esp ; Save esp.
184 and esp, 0fffch ; Clear high word and don't trigger ACs.
185 pushfd
186 mov eax, [esp] ; eax = original EFLAGS.
187 xor dword [esp], X86_EFL_ID | X86_EFL_AC ; Flip the ID and AC flags.
188 popfd ; Load modified flags.
189 pushfd ; Save actual flags.
190 xchg eax, [esp] ; Switch, so the stack has the original flags.
191 xor eax, [esp] ; Calc changed flags.
192 popf ; Restore EFLAGS.
193 mov esp, ebx ; Restore possibly unaligned ESP.
194 test eax, X86_EFL_ID
195 jnz .have_cpuid ; If ID changed, we've got CPUID.
196 test eax, X86_EFL_AC
197 mov xAX, BS3CPU_80486
198 jnz .return ; If AC changed, we've got a 486 without CPUID (or similar).
199 mov xAX, BS3CPU_80386
200 jmp .return
201
202CPU 586
203.have_cpuid:
204 ;
205 ; Do a very simple minded check here using the (standard) family field.
206 ; While here, we also check for PAE.
207 ;
208 mov eax, 1
209 cpuid
210
211 ; Calc the extended family and model values before we mess up EAX.
212 mov cl, ah
213 and cl, 0fh
214 cmp cl, 0fh
215 jnz .not_extended_family
216 mov ecx, eax
217 shr ecx, 20
218 and cl, 7fh
219 add cl, 0fh
220.not_extended_family: ; cl = family
221 mov ch, al
222 shr ch, 4
223 cmp cl, 0fh
224 jae .extended_model
225 cmp cl, 06h ; actually only intel, but we'll let this slip for now.
226 jne .done_model
227.extended_model:
228 shr eax, 12
229 and al, 0f0h
230 or ch, al
231.done_model: ; ch = model
232
233 ; Start assembling return flags, checking for PSE + PAE.
234 mov eax, X86_CPUID_FEATURE_EDX_PSE | X86_CPUID_FEATURE_EDX_PAE
235 and eax, edx
236 mov ah, al
237 AssertCompile(X86_CPUID_FEATURE_EDX_PAE_BIT > BS3CPU_F_PAE_BIT - 8) ; 6 vs 10-8=2
238 and al, X86_CPUID_FEATURE_EDX_PAE
239 shr al, X86_CPUID_FEATURE_EDX_PAE_BIT - (BS3CPU_F_PAE_BIT - 8)
240 AssertCompile(X86_CPUID_FEATURE_EDX_PSE_BIT == BS3CPU_F_PSE_BIT - 8) ; 3 vs 11-8=3
241 and ah, X86_CPUID_FEATURE_EDX_PSE
242 or ah, al
243 or ah, (BS3CPU_F_CPUID >> 8)
244
245 ; Add the CPU type based on the family and model values.
246 cmp cl, 6
247 jne .not_family_06h
248 mov al, BS3CPU_PPro
249 cmp ch, 1
250 jbe .return
251 mov al, BS3CPU_PProOrNewer
252 jmp .NewerThanPPro
253
254.not_family_06h:
255 mov al, BS3CPU_PProOrNewer
256 ja .NewerThanPPro
257 cmp cl, 5
258 mov al, BS3CPU_Pentium
259 je .return
260 cmp cl, 4
261 mov al, BS3CPU_80486
262 je .return
263 cmp cl, 3
264 mov al, BS3CPU_80386
265 je .return
266
267.NewerThanPPro:
268
269 ; Check for extended leaves and long mode.
270 push xAX ; save PAE+PProOrNewer
271 mov eax, 0x80000000
272 cpuid
273 sub eax, 0x80000001 ; Minimum leaf 0x80000001
274 cmp eax, 0x00010000 ; At most 0x10000 leaves.
275 ja .no_ext_leaves
276
277 mov eax, 0x80000001
278 cpuid
279 pop xAX ; restore PAE+PProOrNewer
280 test edx, X86_CPUID_EXT_FEATURE_EDX_LONG_MODE
281 jz .no_long_mode
282 or ax, BS3CPU_F_CPUID_EXT_LEAVES | BS3CPU_F_LONG_MODE
283 jmp .return
284.no_long_mode:
285 or ax, BS3CPU_F_CPUID_EXT_LEAVES
286 jmp .return
287.no_ext_leaves:
288 pop xAX ; restore PAE+PProOrNewer
289
290CPU 8086
291.return:
292 ;
293 ; Save the return value.
294 ;
295 mov [BS3_DATA16_WRT(g_uBs3CpuDetected)], ax
296
297 ;
298 ; Epilogue.
299 ;
300 pop xBX
301 pop xDX
302 pop xCX
303 popf
304 pop xBP
305 BS3_HYBRID_RET
306
307BS3_PROC_END_MODE Bs3CpuDetect
308
309
310%if TMPL_MODE == BS3_MODE_RM
311BS3_BEGIN_TEXT16_NEARSTUBS
312BS3_PROC_BEGIN_MODE Bs3CpuDetect, BS3_PBC_NEAR
313 call far TMPL_FAR_NM(Bs3CpuDetect)
314 ret
315BS3_PROC_END_MODE Bs3CpuDetect
316%endif
317
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette