initial commit
[dos_imgv] / src / util_s.asm
1         bits 32
2         section .text
3
4 F_ID equ 0x200000
5
6         global read_cpuid_
7 read_cpuid_:
8         ; determine if cpuid is available
9         pushf
10         pop eax
11         mov edx, eax    ; keep a copy of the original eflags in edx
12         xor eax, F_ID
13         push eax
14         popf
15         pushf
16         pop eax
17         cmp edx, eax
18         jnz .havecpuid
19         ; failed to flip ID bit, CPUID not supported
20         mov eax, -1
21         ret
22 .havecpuid:
23         push ebp
24         mov ebp, esp
25         push ebx
26         push edi
27         sub esp, 8
28         mov edi, [ebp + 8]
29
30         xor eax, eax
31         mov [esp], eax  ; current index
32         cpuid
33
34         mov [edi], eax
35         ; clamp to the size of our cpuid_info structure
36         cmp eax, 1
37         jbe .skipclamp
38         mov eax, 1
39         mov [esp + 4], eax      ; maximum index
40 .skipclamp:
41
42         mov [edi + 4], ebx
43         mov [edi + 8], edx
44         mov [edi + 12], ecx
45         add edi, 16
46
47 cpuid_loop:
48         mov eax, [esp]
49         inc eax
50         cmp eax, [esp + 4]
51         ja cpuid_done
52         mov [esp], eax
53         cpuid
54         mov [edi], eax
55         mov [edi + 4], ebx
56         mov [edi + 8], edx
57         mov [edi + 12], ecx
58         add edi, 16
59         jmp cpuid_loop
60
61 cpuid_done:
62         add esp, 8
63         pop edi
64         pop ebx
65         pop ebp
66         xor eax, eax
67         ret