VirtualBox

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

Last change on this file since 60112 was 59949, checked in by vboxsync, 9 years ago

bs3kit: Updates thowards v8086.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.2 KB
Line 
1; $Id: bs3-mode-CpuDetect.asm 59949 2016-03-07 23:15:22Z 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
30TMPL_BEGIN_TEXT
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;
44BS3_PROC_BEGIN_MODE Bs3CpuDetect
45CPU 8086
46 push xBP
47 mov xBP, xSP
48 pushf
49 push xCX
50 push xDX
51 push xBX
52
53%ifndef TMPL_CMN_PAGING
54 %ifdef TMPL_RM
55 %if 1 ; this is simpler
56 ;
57 ; FLAGS bits 15:12 are always set on 8086, 8088, V20, V30, 80186, and
58 ; 80188. FLAGS bit 15 is always zero on 286+, whereas bit 14 is NT and
59 ; bits 13:12 are IOPL.
60 ;
61 test byte [xBP - xCB + 1], 80h ; Top byte of saved flags.
62 jz .286plus
63 %else
64 ;
65 ; When executing 'PUSH SP' the 8086, 8088, V20, V30, 80186, and 80188
66 ; should be pushing the updated SP value instead of the initial one.
67 ;
68 push xSP
69 pop xAX
70 cmp xAX, xSP
71 je .286plus
72 %endif
73
74 ;
75 ; Older than 286.
76 ;
77 ; Detect 8086/8088/V20/V30 vs. 80186/80188 by checking for pre 80186
78 ; shift behavior. the 80186/188 and later will mask the CL value according
79 ; to the width of the destination register, whereas 8086/88 and V20/30 will
80 ; perform the exact number of shifts specified.
81 ;
82 mov cl, 20h ; Shift count; 80186/88 and later will mask this by 0x1f (or 0xf)?
83 mov dx, 7fh
84 shl dx, cl
85 cmp dx, 7fh ; If no change, this is a 80186/88.
86 mov xAX, BS3CPU_80186
87 je .return
88
89 ;
90 ; Detect 8086/88 vs V20/30 by exploiting undocumented POP CS encoding
91 ; that was redefined on V20/30 to SET1.
92 ;
93 xor ax, ax ; clear
94 push cs
95 db 0fh ; 8086/88: pop cs V20/30: set1 bl,cl
96 db 14h, 3ch ; 8086/88: add al, 3ch
97 ; 8086/88: al = 3ch V20/30: al = 0, cs on stack, bl modified.
98 cmp al, 3ch
99 jne .is_v20_or_v30
100 mov xAX, BS3CPU_8086
101 jmp .return
102
103.is_v20_or_v30:
104 pop xCX ; unclaimed CS
105 mov xAX, BS3CPU_V20
106 jmp .return
107
108 %endif ; TMPL_RM
109
110CPU 286
111.286plus:
112 ;
113 ; The 4th bit of the machine status word / CR0 indicates the precense
114 ; of a 80387 or later co-processor (a 80287+80386 => ET=0). 486 and
115 ; later should be hardcoding this to 1, according to the documentation
116 ; (need to test on 486SX). The 286 should never have it set.
117 ;
118 smsw ax
119 test ax, X86_CR0_ET
120 jnz .386plus
121 %ifndef TMPL_RM ; remove once 286plus_protmode is implemented (relevant in DOS context).
122 test ax, X86_CR0_PE
123 jnz .286plus_protmode ;; @todo The test below doesn't work in prot mode, I think...
124 %endif
125
126 ;
127 ; Detect 80286 by checking whether the IOPL and NT bits of EFLAGS can be
128 ; modified or not. There are different accounts of these bits. Dr.Dobb's
129 ; (http://www.drdobbs.com/embedded-systems/processor-detection-schemes/184409011)
130 ; say they are undefined on 286es and will always be zero. Whereas Intel
131 ; iAPX 286 Programmer's Reference Manual (both order #210498-001 and
132 ; #210498-003) documents both IOPL and NT, but with comment 4 on page
133 ; C-43 stating that they cannot be POPFed in real mode and will both
134 ; remain 0. This is different from the 386+, where the NT flag isn't
135 ; privileged according to page 3-37 in #230985-003. Later Intel docs
136 ; (#235383-052US, page 4-192) documents real mode as taking both NT and
137 ; IOPL from what POPF reads off the stack - which is the behavior
138 ; observed a 386SX here.
139 ;
140 cli ; Disable interrupts to be on the safe side.
141 mov xAX, [xBP - xCB]
142 or xAX, X86_EFL_IOPL | X86_EFL_NT
143 push xAX ; Load modified flags.
144 popf
145 pushf ; Get actual flags.
146 pop xAX
147 test ax, X86_EFL_IOPL | X86_EFL_NT
148 jnz .386plus ; If any of the flags are set, we're on 386+.
149
150 ; While we could in theory be in v8086 mode at this point and be fooled
151 ; by a flaky POPF implementation, we assume this isn't the case in our
152 ; execution environment.
153 mov xAX, BS3CPU_80286
154 jmp .return
155%endif
156
157CPU 386
158.386plus:
159 ;
160 ; Check for CPUID and AC. The former flag indicates CPUID support, the
161 ; latter was introduced with the 486.
162 ;
163 mov ebx, esp ; Save esp.
164 and esp, 0fffch ; Clear high word and don't trigger ACs.
165 pushfd
166 mov eax, [esp] ; eax = original EFLAGS.
167 xor dword [esp], X86_EFL_ID | X86_EFL_AC ; Flip the ID and AC flags.
168 popfd ; Load modified flags.
169 pushfd ; Save actual flags.
170 xchg eax, [esp] ; Switch, so the stack has the original flags.
171 xor eax, [esp] ; Calc changed flags.
172 popf ; Restore EFLAGS.
173 mov esp, ebx ; Restore possibly unaligned ESP.
174 test eax, X86_EFL_ID
175 jnz .have_cpuid ; If ID changed, we've got CPUID.
176 test eax, X86_EFL_AC
177 mov xAX, BS3CPU_80486
178 jnz .return ; If AC changed, we've got a 486 without CPUID (or similar).
179 mov xAX, BS3CPU_80386
180 jmp .return
181
182CPU 586
183.have_cpuid:
184 ;
185 ; Do a very simple minded check here using the (standard) family field.
186 ; While here, we also check for PAE.
187 ;
188 mov eax, 1
189 cpuid
190
191 ; Calc the basic family value before we mess up EAX.
192 mov cl, ah
193 and cl, 0fh
194
195 ; Start assembling return flags, checking for PAE.
196 mov xAX, BS3CPU_F_CPUID
197 and dx, X86_CPUID_FEATURE_EDX_PAE
198 shl dx, BS3CPU_F_PAE_BIT - X86_CPUID_FEATURE_EDX_PAE_BIT
199 or ax, dx
200
201 ; Add the CPU type based on the family value.
202 cmp cl, 6
203 or al, BS3CPU_PPro
204 jz .return
205 ja .NewerThanPPro
206 cmp cl, 5
207 or al, BS3CPU_Pentium
208 je .return
209 cmp cl, 4
210 or al, BS3CPU_80486
211 je .return
212 cmp cl, 3
213 or al, BS3CPU_80386
214 je .return
215.NewerThanPPro:
216
217 ; Check for extended leaves and long mode.
218 push xAX ; save PAE flag
219 mov eax, 0x80000000
220 cpuid
221 sub eax, 0x80000001 ; Minimum leaf 0x80000001
222 cmp eax, 0x00010000 ; At most 0x10000 leaves.
223 ja .no_ext_leaves
224
225 mov eax, 0x80000001
226 cpuid
227 pop xAX ; restore PAE flag
228 test edx, X86_CPUID_EXT_FEATURE_EDX_LONG_MODE
229 jz .no_long_mode
230 or xAX, BS3CPU_PProOrNewer | BS3CPU_F_CPUID_EXT_LEAVES | BS3CPU_F_LONG_MODE
231 jmp .return
232.no_long_mode:
233 or xAX, BS3CPU_PProOrNewer | BS3CPU_F_CPUID_EXT_LEAVES
234 jmp .return
235.no_ext_leaves:
236 pop xAX ; restore PAE flag
237 or al, BS3CPU_PProOrNewer
238
239CPU 8086
240.return:
241 ;
242 ; Save the return value.
243 ;
244 BS3_ONLY_16BIT_STMT push ds
245 BS3_ONLY_16BIT_STMT mov bx, seg g_uBs3CpuDetected
246 BS3_ONLY_16BIT_STMT mov ds, bx
247 mov [BS3_DATA16_WRT(g_uBs3CpuDetected)], ax
248 BS3_ONLY_16BIT_STMT pop ds
249
250 ;
251 ; Epilogue.
252 ;
253 popf
254 pop xBX
255 pop xDX
256 pop xCX
257 pop xBP
258 ret
259BS3_PROC_END_MODE Bs3CpuDetect
260
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