VirtualBox

source: vbox/trunk/include/iprt/asm-amd64-x86.h@ 69411

Last change on this file since 69411 was 69105, checked in by vboxsync, 7 years ago

include/iprt/: (C) year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 74.6 KB
Line 
1/** @file
2 * IPRT - AMD64 and x86 Specific Assembly Functions.
3 */
4
5/*
6 * Copyright (C) 2006-2017 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef ___iprt_asm_amd64_x86_h
27#define ___iprt_asm_amd64_x86_h
28
29#include <iprt/types.h>
30#include <iprt/assert.h>
31#if !defined(RT_ARCH_AMD64) && !defined(RT_ARCH_X86)
32# error "Not on AMD64 or x86"
33#endif
34
35#if defined(_MSC_VER) && RT_INLINE_ASM_USES_INTRIN
36# pragma warning(push)
37# pragma warning(disable:4668) /* Several incorrect __cplusplus uses. */
38# pragma warning(disable:4255) /* Incorrect __slwpcb prototype. */
39# include <intrin.h>
40# pragma warning(pop)
41 /* Emit the intrinsics at all optimization levels. */
42# pragma intrinsic(_ReadWriteBarrier)
43# pragma intrinsic(__cpuid)
44# pragma intrinsic(_enable)
45# pragma intrinsic(_disable)
46# pragma intrinsic(__rdtsc)
47# pragma intrinsic(__readmsr)
48# pragma intrinsic(__writemsr)
49# pragma intrinsic(__outbyte)
50# pragma intrinsic(__outbytestring)
51# pragma intrinsic(__outword)
52# pragma intrinsic(__outwordstring)
53# pragma intrinsic(__outdword)
54# pragma intrinsic(__outdwordstring)
55# pragma intrinsic(__inbyte)
56# pragma intrinsic(__inbytestring)
57# pragma intrinsic(__inword)
58# pragma intrinsic(__inwordstring)
59# pragma intrinsic(__indword)
60# pragma intrinsic(__indwordstring)
61# pragma intrinsic(__invlpg)
62# pragma intrinsic(__wbinvd)
63# pragma intrinsic(__readcr0)
64# pragma intrinsic(__readcr2)
65# pragma intrinsic(__readcr3)
66# pragma intrinsic(__readcr4)
67# pragma intrinsic(__writecr0)
68# pragma intrinsic(__writecr3)
69# pragma intrinsic(__writecr4)
70# pragma intrinsic(__readdr)
71# pragma intrinsic(__writedr)
72# ifdef RT_ARCH_AMD64
73# pragma intrinsic(__readcr8)
74# pragma intrinsic(__writecr8)
75# endif
76# if RT_INLINE_ASM_USES_INTRIN >= 14
77# pragma intrinsic(__halt)
78# endif
79# if RT_INLINE_ASM_USES_INTRIN >= 15
80# pragma intrinsic(__readeflags)
81# pragma intrinsic(__writeeflags)
82# pragma intrinsic(__rdtscp)
83# endif
84#endif
85
86
87/*
88 * Include #pragma aux definitions for Watcom C/C++.
89 */
90#if defined(__WATCOMC__) && ARCH_BITS == 16
91# include "asm-amd64-x86-watcom-16.h"
92#elif defined(__WATCOMC__) && ARCH_BITS == 32
93# include "asm-amd64-x86-watcom-32.h"
94#endif
95
96
97/** @defgroup grp_rt_asm_amd64_x86 AMD64 and x86 Specific ASM Routines
98 * @ingroup grp_rt_asm
99 * @{
100 */
101
102/** @todo find a more proper place for these structures? */
103
104#pragma pack(1)
105/** IDTR */
106typedef struct RTIDTR
107{
108 /** Size of the IDT. */
109 uint16_t cbIdt;
110 /** Address of the IDT. */
111#if ARCH_BITS != 64
112 uint32_t pIdt;
113#else
114 uint64_t pIdt;
115#endif
116} RTIDTR, RT_FAR *PRTIDTR;
117#pragma pack()
118
119#pragma pack(1)
120/** @internal */
121typedef struct RTIDTRALIGNEDINT
122{
123 /** Alignment padding. */
124 uint16_t au16Padding[ARCH_BITS == 64 ? 3 : 1];
125 /** The IDTR structure. */
126 RTIDTR Idtr;
127} RTIDTRALIGNEDINT;
128#pragma pack()
129
130/** Wrapped RTIDTR for preventing misalignment exceptions. */
131typedef union RTIDTRALIGNED
132{
133 /** Try make sure this structure has optimal alignment. */
134 uint64_t auAlignmentHack[ARCH_BITS == 64 ? 2 : 1];
135 /** Aligned structure. */
136 RTIDTRALIGNEDINT s;
137} RTIDTRALIGNED;
138AssertCompileSize(RTIDTRALIGNED, ((ARCH_BITS == 64) + 1) * 8);
139/** Pointer to a an RTIDTR alignment wrapper. */
140typedef RTIDTRALIGNED RT_FAR *PRIDTRALIGNED;
141
142
143#pragma pack(1)
144/** GDTR */
145typedef struct RTGDTR
146{
147 /** Size of the GDT. */
148 uint16_t cbGdt;
149 /** Address of the GDT. */
150#if ARCH_BITS != 64
151 uint32_t pGdt;
152#else
153 uint64_t pGdt;
154#endif
155} RTGDTR, RT_FAR *PRTGDTR;
156#pragma pack()
157
158#pragma pack(1)
159/** @internal */
160typedef struct RTGDTRALIGNEDINT
161{
162 /** Alignment padding. */
163 uint16_t au16Padding[ARCH_BITS == 64 ? 3 : 1];
164 /** The GDTR structure. */
165 RTGDTR Gdtr;
166} RTGDTRALIGNEDINT;
167#pragma pack()
168
169/** Wrapped RTGDTR for preventing misalignment exceptions. */
170typedef union RTGDTRALIGNED
171{
172 /** Try make sure this structure has optimal alignment. */
173 uint64_t auAlignmentHack[ARCH_BITS == 64 ? 2 : 1];
174 /** Aligned structure. */
175 RTGDTRALIGNEDINT s;
176} RTGDTRALIGNED;
177AssertCompileSize(RTIDTRALIGNED, ((ARCH_BITS == 64) + 1) * 8);
178/** Pointer to a an RTGDTR alignment wrapper. */
179typedef RTGDTRALIGNED RT_FAR *PRGDTRALIGNED;
180
181
182/**
183 * Gets the content of the IDTR CPU register.
184 * @param pIdtr Where to store the IDTR contents.
185 */
186#if RT_INLINE_ASM_EXTERNAL
187DECLASM(void) ASMGetIDTR(PRTIDTR pIdtr);
188#else
189DECLINLINE(void) ASMGetIDTR(PRTIDTR pIdtr)
190{
191# if RT_INLINE_ASM_GNU_STYLE
192 __asm__ __volatile__("sidt %0" : "=m" (*pIdtr));
193# else
194 __asm
195 {
196# ifdef RT_ARCH_AMD64
197 mov rax, [pIdtr]
198 sidt [rax]
199# else
200 mov eax, [pIdtr]
201 sidt [eax]
202# endif
203 }
204# endif
205}
206#endif
207
208
209/**
210 * Gets the content of the IDTR.LIMIT CPU register.
211 * @returns IDTR limit.
212 */
213#if RT_INLINE_ASM_EXTERNAL
214DECLASM(uint16_t) ASMGetIdtrLimit(void);
215#else
216DECLINLINE(uint16_t) ASMGetIdtrLimit(void)
217{
218 RTIDTRALIGNED TmpIdtr;
219# if RT_INLINE_ASM_GNU_STYLE
220 __asm__ __volatile__("sidt %0" : "=m" (TmpIdtr.s.Idtr));
221# else
222 __asm
223 {
224 sidt [TmpIdtr.s.Idtr]
225 }
226# endif
227 return TmpIdtr.s.Idtr.cbIdt;
228}
229#endif
230
231
232/**
233 * Sets the content of the IDTR CPU register.
234 * @param pIdtr Where to load the IDTR contents from
235 */
236#if RT_INLINE_ASM_EXTERNAL
237DECLASM(void) ASMSetIDTR(const RTIDTR RT_FAR *pIdtr);
238#else
239DECLINLINE(void) ASMSetIDTR(const RTIDTR RT_FAR *pIdtr)
240{
241# if RT_INLINE_ASM_GNU_STYLE
242 __asm__ __volatile__("lidt %0" : : "m" (*pIdtr));
243# else
244 __asm
245 {
246# ifdef RT_ARCH_AMD64
247 mov rax, [pIdtr]
248 lidt [rax]
249# else
250 mov eax, [pIdtr]
251 lidt [eax]
252# endif
253 }
254# endif
255}
256#endif
257
258
259/**
260 * Gets the content of the GDTR CPU register.
261 * @param pGdtr Where to store the GDTR contents.
262 */
263#if RT_INLINE_ASM_EXTERNAL
264DECLASM(void) ASMGetGDTR(PRTGDTR pGdtr);
265#else
266DECLINLINE(void) ASMGetGDTR(PRTGDTR pGdtr)
267{
268# if RT_INLINE_ASM_GNU_STYLE
269 __asm__ __volatile__("sgdt %0" : "=m" (*pGdtr));
270# else
271 __asm
272 {
273# ifdef RT_ARCH_AMD64
274 mov rax, [pGdtr]
275 sgdt [rax]
276# else
277 mov eax, [pGdtr]
278 sgdt [eax]
279# endif
280 }
281# endif
282}
283#endif
284
285
286/**
287 * Sets the content of the GDTR CPU register.
288 * @param pGdtr Where to load the GDTR contents from
289 */
290#if RT_INLINE_ASM_EXTERNAL
291DECLASM(void) ASMSetGDTR(const RTGDTR RT_FAR *pGdtr);
292#else
293DECLINLINE(void) ASMSetGDTR(const RTGDTR RT_FAR *pGdtr)
294{
295# if RT_INLINE_ASM_GNU_STYLE
296 __asm__ __volatile__("lgdt %0" : : "m" (*pGdtr));
297# else
298 __asm
299 {
300# ifdef RT_ARCH_AMD64
301 mov rax, [pGdtr]
302 lgdt [rax]
303# else
304 mov eax, [pGdtr]
305 lgdt [eax]
306# endif
307 }
308# endif
309}
310#endif
311
312
313
314/**
315 * Get the cs register.
316 * @returns cs.
317 */
318#if RT_INLINE_ASM_EXTERNAL
319DECLASM(RTSEL) ASMGetCS(void);
320#else
321DECLINLINE(RTSEL) ASMGetCS(void)
322{
323 RTSEL SelCS;
324# if RT_INLINE_ASM_GNU_STYLE
325 __asm__ __volatile__("movw %%cs, %0\n\t" : "=r" (SelCS));
326# else
327 __asm
328 {
329 mov ax, cs
330 mov [SelCS], ax
331 }
332# endif
333 return SelCS;
334}
335#endif
336
337
338/**
339 * Get the DS register.
340 * @returns DS.
341 */
342#if RT_INLINE_ASM_EXTERNAL
343DECLASM(RTSEL) ASMGetDS(void);
344#else
345DECLINLINE(RTSEL) ASMGetDS(void)
346{
347 RTSEL SelDS;
348# if RT_INLINE_ASM_GNU_STYLE
349 __asm__ __volatile__("movw %%ds, %0\n\t" : "=r" (SelDS));
350# else
351 __asm
352 {
353 mov ax, ds
354 mov [SelDS], ax
355 }
356# endif
357 return SelDS;
358}
359#endif
360
361
362/**
363 * Get the ES register.
364 * @returns ES.
365 */
366#if RT_INLINE_ASM_EXTERNAL
367DECLASM(RTSEL) ASMGetES(void);
368#else
369DECLINLINE(RTSEL) ASMGetES(void)
370{
371 RTSEL SelES;
372# if RT_INLINE_ASM_GNU_STYLE
373 __asm__ __volatile__("movw %%es, %0\n\t" : "=r" (SelES));
374# else
375 __asm
376 {
377 mov ax, es
378 mov [SelES], ax
379 }
380# endif
381 return SelES;
382}
383#endif
384
385
386/**
387 * Get the FS register.
388 * @returns FS.
389 */
390#if RT_INLINE_ASM_EXTERNAL
391DECLASM(RTSEL) ASMGetFS(void);
392#else
393DECLINLINE(RTSEL) ASMGetFS(void)
394{
395 RTSEL SelFS;
396# if RT_INLINE_ASM_GNU_STYLE
397 __asm__ __volatile__("movw %%fs, %0\n\t" : "=r" (SelFS));
398# else
399 __asm
400 {
401 mov ax, fs
402 mov [SelFS], ax
403 }
404# endif
405 return SelFS;
406}
407# endif
408
409
410/**
411 * Get the GS register.
412 * @returns GS.
413 */
414#if RT_INLINE_ASM_EXTERNAL
415DECLASM(RTSEL) ASMGetGS(void);
416#else
417DECLINLINE(RTSEL) ASMGetGS(void)
418{
419 RTSEL SelGS;
420# if RT_INLINE_ASM_GNU_STYLE
421 __asm__ __volatile__("movw %%gs, %0\n\t" : "=r" (SelGS));
422# else
423 __asm
424 {
425 mov ax, gs
426 mov [SelGS], ax
427 }
428# endif
429 return SelGS;
430}
431#endif
432
433
434/**
435 * Get the SS register.
436 * @returns SS.
437 */
438#if RT_INLINE_ASM_EXTERNAL
439DECLASM(RTSEL) ASMGetSS(void);
440#else
441DECLINLINE(RTSEL) ASMGetSS(void)
442{
443 RTSEL SelSS;
444# if RT_INLINE_ASM_GNU_STYLE
445 __asm__ __volatile__("movw %%ss, %0\n\t" : "=r" (SelSS));
446# else
447 __asm
448 {
449 mov ax, ss
450 mov [SelSS], ax
451 }
452# endif
453 return SelSS;
454}
455#endif
456
457
458/**
459 * Get the TR register.
460 * @returns TR.
461 */
462#if RT_INLINE_ASM_EXTERNAL
463DECLASM(RTSEL) ASMGetTR(void);
464#else
465DECLINLINE(RTSEL) ASMGetTR(void)
466{
467 RTSEL SelTR;
468# if RT_INLINE_ASM_GNU_STYLE
469 __asm__ __volatile__("str %w0\n\t" : "=r" (SelTR));
470# else
471 __asm
472 {
473 str ax
474 mov [SelTR], ax
475 }
476# endif
477 return SelTR;
478}
479#endif
480
481
482/**
483 * Get the LDTR register.
484 * @returns LDTR.
485 */
486#if RT_INLINE_ASM_EXTERNAL
487DECLASM(RTSEL) ASMGetLDTR(void);
488#else
489DECLINLINE(RTSEL) ASMGetLDTR(void)
490{
491 RTSEL SelLDTR;
492# if RT_INLINE_ASM_GNU_STYLE
493 __asm__ __volatile__("sldt %w0\n\t" : "=r" (SelLDTR));
494# else
495 __asm
496 {
497 sldt ax
498 mov [SelLDTR], ax
499 }
500# endif
501 return SelLDTR;
502}
503#endif
504
505
506/**
507 * Get the access rights for the segment selector.
508 *
509 * @returns The access rights on success or UINT32_MAX on failure.
510 * @param uSel The selector value.
511 *
512 * @remarks Using UINT32_MAX for failure is chosen because valid access rights
513 * always have bits 0:7 as 0 (on both Intel & AMD).
514 */
515#if RT_INLINE_ASM_EXTERNAL
516DECLASM(uint32_t) ASMGetSegAttr(uint32_t uSel);
517#else
518DECLINLINE(uint32_t) ASMGetSegAttr(uint32_t uSel)
519{
520 uint32_t uAttr;
521 /* LAR only accesses 16-bit of the source operand, but eax for the
522 destination operand is required for getting the full 32-bit access rights. */
523# if RT_INLINE_ASM_GNU_STYLE
524 __asm__ __volatile__("lar %1, %%eax\n\t"
525 "jz done%=\n\t"
526 "movl $0xffffffff, %%eax\n\t"
527 "done%=:\n\t"
528 "movl %%eax, %0\n\t"
529 : "=r" (uAttr)
530 : "r" (uSel)
531 : "cc", "%eax");
532# else
533 __asm
534 {
535 lar eax, [uSel]
536 jz done
537 mov eax, 0ffffffffh
538 done:
539 mov [uAttr], eax
540 }
541# endif
542 return uAttr;
543}
544#endif
545
546
547/**
548 * Get the [RE]FLAGS register.
549 * @returns [RE]FLAGS.
550 */
551#if RT_INLINE_ASM_EXTERNAL && RT_INLINE_ASM_USES_INTRIN < 15
552DECLASM(RTCCUINTREG) ASMGetFlags(void);
553#else
554DECLINLINE(RTCCUINTREG) ASMGetFlags(void)
555{
556 RTCCUINTREG uFlags;
557# if RT_INLINE_ASM_GNU_STYLE
558# ifdef RT_ARCH_AMD64
559 __asm__ __volatile__("pushfq\n\t"
560 "popq %0\n\t"
561 : "=r" (uFlags));
562# else
563 __asm__ __volatile__("pushfl\n\t"
564 "popl %0\n\t"
565 : "=r" (uFlags));
566# endif
567# elif RT_INLINE_ASM_USES_INTRIN >= 15
568 uFlags = __readeflags();
569# else
570 __asm
571 {
572# ifdef RT_ARCH_AMD64
573 pushfq
574 pop [uFlags]
575# else
576 pushfd
577 pop [uFlags]
578# endif
579 }
580# endif
581 return uFlags;
582}
583#endif
584
585
586/**
587 * Set the [RE]FLAGS register.
588 * @param uFlags The new [RE]FLAGS value.
589 */
590#if RT_INLINE_ASM_EXTERNAL && RT_INLINE_ASM_USES_INTRIN < 15
591DECLASM(void) ASMSetFlags(RTCCUINTREG uFlags);
592#else
593DECLINLINE(void) ASMSetFlags(RTCCUINTREG uFlags)
594{
595# if RT_INLINE_ASM_GNU_STYLE
596# ifdef RT_ARCH_AMD64
597 __asm__ __volatile__("pushq %0\n\t"
598 "popfq\n\t"
599 : : "g" (uFlags));
600# else
601 __asm__ __volatile__("pushl %0\n\t"
602 "popfl\n\t"
603 : : "g" (uFlags));
604# endif
605# elif RT_INLINE_ASM_USES_INTRIN >= 15
606 __writeeflags(uFlags);
607# else
608 __asm
609 {
610# ifdef RT_ARCH_AMD64
611 push [uFlags]
612 popfq
613# else
614 push [uFlags]
615 popfd
616# endif
617 }
618# endif
619}
620#endif
621
622
623/**
624 * Modifies the [RE]FLAGS register.
625 * @returns Original value.
626 * @param fAndEfl Flags to keep (applied first).
627 * @param fOrEfl Flags to be set.
628 */
629#if RT_INLINE_ASM_EXTERNAL && RT_INLINE_ASM_USES_INTRIN < 15
630DECLASM(RTCCUINTREG) ASMChangeFlags(RTCCUINTREG fAndEfl, RTCCUINTREG fOrEfl);
631#else
632DECLINLINE(RTCCUINTREG) ASMChangeFlags(RTCCUINTREG fAndEfl, RTCCUINTREG fOrEfl)
633{
634 RTCCUINTREG fOldEfl;
635# if RT_INLINE_ASM_GNU_STYLE
636# ifdef RT_ARCH_AMD64
637 __asm__ __volatile__("pushfq\n\t"
638 "movq (%%rsp), %0\n\t"
639 "andq %0, %1\n\t"
640 "orq %3, %1\n\t"
641 "mov %1, (%%rsp)\n\t"
642 "popfq\n\t"
643 : "=&r" (fOldEfl),
644 "=r" (fAndEfl)
645 : "1" (fAndEfl),
646 "rn" (fOrEfl) );
647# else
648 __asm__ __volatile__("pushfl\n\t"
649 "movl (%%esp), %0\n\t"
650 "andl %1, (%%esp)\n\t"
651 "orl %2, (%%esp)\n\t"
652 "popfl\n\t"
653 : "=&r" (fOldEfl)
654 : "rn" (fAndEfl),
655 "rn" (fOrEfl) );
656# endif
657# elif RT_INLINE_ASM_USES_INTRIN >= 15
658 fOldEfl = __readeflags();
659 __writeeflags((fOldEfl & fAndEfl) | fOrEfl);
660# else
661 __asm
662 {
663# ifdef RT_ARCH_AMD64
664 mov rdx, [fAndEfl]
665 mov rcx, [fOrEfl]
666 pushfq
667 mov rax, [rsp]
668 and rdx, rax
669 or rdx, rcx
670 mov [rsp], rdx
671 popfq
672 mov [fOldEfl], rax
673# else
674 mov edx, [fAndEfl]
675 mov ecx, [fOrEfl]
676 pushfd
677 mov eax, [esp]
678 and edx, eax
679 or edx, ecx
680 mov [esp], edx
681 popfd
682 mov [fOldEfl], eax
683# endif
684 }
685# endif
686 return fOldEfl;
687}
688#endif
689
690
691/**
692 * Modifies the [RE]FLAGS register by ORing in one or more flags.
693 * @returns Original value.
694 * @param fOrEfl The flags to be set (ORed in).
695 */
696#if RT_INLINE_ASM_EXTERNAL && RT_INLINE_ASM_USES_INTRIN < 15
697DECLASM(RTCCUINTREG) ASMAddFlags(RTCCUINTREG fOrEfl);
698#else
699DECLINLINE(RTCCUINTREG) ASMAddFlags(RTCCUINTREG fOrEfl)
700{
701 RTCCUINTREG fOldEfl;
702# if RT_INLINE_ASM_GNU_STYLE
703# ifdef RT_ARCH_AMD64
704 __asm__ __volatile__("pushfq\n\t"
705 "movq (%%rsp), %0\n\t"
706 "orq %1, (%%rsp)\n\t"
707 "popfq\n\t"
708 : "=&r" (fOldEfl)
709 : "rn" (fOrEfl) );
710# else
711 __asm__ __volatile__("pushfl\n\t"
712 "movl (%%esp), %0\n\t"
713 "orl %1, (%%esp)\n\t"
714 "popfl\n\t"
715 : "=&r" (fOldEfl)
716 : "rn" (fOrEfl) );
717# endif
718# elif RT_INLINE_ASM_USES_INTRIN >= 15
719 fOldEfl = __readeflags();
720 __writeeflags(fOldEfl | fOrEfl);
721# else
722 __asm
723 {
724# ifdef RT_ARCH_AMD64
725 mov rcx, [fOrEfl]
726 pushfq
727 mov rdx, [rsp]
728 or [rsp], rcx
729 popfq
730 mov [fOldEfl], rax
731# else
732 mov ecx, [fOrEfl]
733 pushfd
734 mov edx, [esp]
735 or [esp], ecx
736 popfd
737 mov [fOldEfl], eax
738# endif
739 }
740# endif
741 return fOldEfl;
742}
743#endif
744
745
746/**
747 * Modifies the [RE]FLAGS register by AND'ing out one or more flags.
748 * @returns Original value.
749 * @param fAndEfl The flags to keep.
750 */
751#if RT_INLINE_ASM_EXTERNAL && RT_INLINE_ASM_USES_INTRIN < 15
752DECLASM(RTCCUINTREG) ASMClearFlags(RTCCUINTREG fAndEfl);
753#else
754DECLINLINE(RTCCUINTREG) ASMClearFlags(RTCCUINTREG fAndEfl)
755{
756 RTCCUINTREG fOldEfl;
757# if RT_INLINE_ASM_GNU_STYLE
758# ifdef RT_ARCH_AMD64
759 __asm__ __volatile__("pushfq\n\t"
760 "movq (%%rsp), %0\n\t"
761 "andq %1, (%%rsp)\n\t"
762 "popfq\n\t"
763 : "=&r" (fOldEfl)
764 : "rn" (fAndEfl) );
765# else
766 __asm__ __volatile__("pushfl\n\t"
767 "movl (%%esp), %0\n\t"
768 "andl %1, (%%esp)\n\t"
769 "popfl\n\t"
770 : "=&r" (fOldEfl)
771 : "rn" (fAndEfl) );
772# endif
773# elif RT_INLINE_ASM_USES_INTRIN >= 15
774 fOldEfl = __readeflags();
775 __writeeflags(fOldEfl & fAndEfl);
776# else
777 __asm
778 {
779# ifdef RT_ARCH_AMD64
780 mov rdx, [fAndEfl]
781 pushfq
782 mov rdx, [rsp]
783 and [rsp], rdx
784 popfq
785 mov [fOldEfl], rax
786# else
787 mov edx, [fAndEfl]
788 pushfd
789 mov edx, [esp]
790 and [esp], edx
791 popfd
792 mov [fOldEfl], eax
793# endif
794 }
795# endif
796 return fOldEfl;
797}
798#endif
799
800
801/**
802 * Gets the content of the CPU timestamp counter register.
803 *
804 * @returns TSC.
805 */
806#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
807DECLASM(uint64_t) ASMReadTSC(void);
808#else
809DECLINLINE(uint64_t) ASMReadTSC(void)
810{
811 RTUINT64U u;
812# if RT_INLINE_ASM_GNU_STYLE
813 __asm__ __volatile__("rdtsc\n\t" : "=a" (u.s.Lo), "=d" (u.s.Hi));
814# else
815# if RT_INLINE_ASM_USES_INTRIN
816 u.u = __rdtsc();
817# else
818 __asm
819 {
820 rdtsc
821 mov [u.s.Lo], eax
822 mov [u.s.Hi], edx
823 }
824# endif
825# endif
826 return u.u;
827}
828#endif
829
830
831/**
832 * Gets the content of the CPU timestamp counter register and the
833 * assoicated AUX value.
834 *
835 * @returns TSC.
836 * @param puAux Where to store the AUX value.
837 */
838#if RT_INLINE_ASM_EXTERNAL && RT_INLINE_ASM_USES_INTRIN < 15
839DECLASM(uint64_t) ASMReadTscWithAux(uint32_t RT_FAR *puAux);
840#else
841DECLINLINE(uint64_t) ASMReadTscWithAux(uint32_t RT_FAR *puAux)
842{
843 RTUINT64U u;
844# if RT_INLINE_ASM_GNU_STYLE
845 /* rdtscp is not supported by ancient linux build VM of course :-( */
846 /*__asm__ __volatile__("rdtscp\n\t" : "=a" (u.s.Lo), "=d" (u.s.Hi), "=c" (*puAux)); */
847 __asm__ __volatile__(".byte 0x0f,0x01,0xf9\n\t" : "=a" (u.s.Lo), "=d" (u.s.Hi), "=c" (*puAux));
848# else
849# if RT_INLINE_ASM_USES_INTRIN >= 15
850 u.u = __rdtscp(puAux);
851# else
852 __asm
853 {
854 rdtscp
855 mov [u.s.Lo], eax
856 mov [u.s.Hi], edx
857 mov eax, [puAux]
858 mov [eax], ecx
859 }
860# endif
861# endif
862 return u.u;
863}
864#endif
865
866
867/**
868 * Performs the cpuid instruction returning all registers.
869 *
870 * @param uOperator CPUID operation (eax).
871 * @param pvEAX Where to store eax.
872 * @param pvEBX Where to store ebx.
873 * @param pvECX Where to store ecx.
874 * @param pvEDX Where to store edx.
875 * @remark We're using void pointers to ease the use of special bitfield structures and such.
876 */
877#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
878DECLASM(void) ASMCpuId(uint32_t uOperator, void RT_FAR *pvEAX, void RT_FAR *pvEBX, void RT_FAR *pvECX, void RT_FAR *pvEDX);
879#else
880DECLINLINE(void) ASMCpuId(uint32_t uOperator, void RT_FAR *pvEAX, void RT_FAR *pvEBX, void RT_FAR *pvECX, void RT_FAR *pvEDX)
881{
882# if RT_INLINE_ASM_GNU_STYLE
883# ifdef RT_ARCH_AMD64
884 RTCCUINTREG uRAX, uRBX, uRCX, uRDX;
885 __asm__ __volatile__ ("cpuid\n\t"
886 : "=a" (uRAX),
887 "=b" (uRBX),
888 "=c" (uRCX),
889 "=d" (uRDX)
890 : "0" (uOperator), "2" (0));
891 *(uint32_t RT_FAR *)pvEAX = (uint32_t)uRAX;
892 *(uint32_t RT_FAR *)pvEBX = (uint32_t)uRBX;
893 *(uint32_t RT_FAR *)pvECX = (uint32_t)uRCX;
894 *(uint32_t RT_FAR *)pvEDX = (uint32_t)uRDX;
895# else
896 __asm__ __volatile__ ("xchgl %%ebx, %1\n\t"
897 "cpuid\n\t"
898 "xchgl %%ebx, %1\n\t"
899 : "=a" (*(uint32_t *)pvEAX),
900 "=r" (*(uint32_t *)pvEBX),
901 "=c" (*(uint32_t *)pvECX),
902 "=d" (*(uint32_t *)pvEDX)
903 : "0" (uOperator), "2" (0));
904# endif
905
906# elif RT_INLINE_ASM_USES_INTRIN
907 int aInfo[4];
908 __cpuid(aInfo, uOperator);
909 *(uint32_t RT_FAR *)pvEAX = aInfo[0];
910 *(uint32_t RT_FAR *)pvEBX = aInfo[1];
911 *(uint32_t RT_FAR *)pvECX = aInfo[2];
912 *(uint32_t RT_FAR *)pvEDX = aInfo[3];
913
914# else
915 uint32_t uEAX;
916 uint32_t uEBX;
917 uint32_t uECX;
918 uint32_t uEDX;
919 __asm
920 {
921 push ebx
922 mov eax, [uOperator]
923 cpuid
924 mov [uEAX], eax
925 mov [uEBX], ebx
926 mov [uECX], ecx
927 mov [uEDX], edx
928 pop ebx
929 }
930 *(uint32_t RT_FAR *)pvEAX = uEAX;
931 *(uint32_t RT_FAR *)pvEBX = uEBX;
932 *(uint32_t RT_FAR *)pvECX = uECX;
933 *(uint32_t RT_FAR *)pvEDX = uEDX;
934# endif
935}
936#endif
937
938
939/**
940 * Performs the CPUID instruction with EAX and ECX input returning ALL output
941 * registers.
942 *
943 * @param uOperator CPUID operation (eax).
944 * @param uIdxECX ecx index
945 * @param pvEAX Where to store eax.
946 * @param pvEBX Where to store ebx.
947 * @param pvECX Where to store ecx.
948 * @param pvEDX Where to store edx.
949 * @remark We're using void pointers to ease the use of special bitfield structures and such.
950 */
951#if RT_INLINE_ASM_EXTERNAL || RT_INLINE_ASM_USES_INTRIN
952DECLASM(void) ASMCpuId_Idx_ECX(uint32_t uOperator, uint32_t uIdxECX, void RT_FAR *pvEAX, void RT_FAR *pvEBX, void RT_FAR *pvECX, void RT_FAR *pvEDX);
953#else
954DECLINLINE(void) ASMCpuId_Idx_ECX(uint32_t uOperator, uint32_t uIdxECX, void RT_FAR *pvEAX, void RT_FAR *pvEBX, void RT_FAR *pvECX, void RT_FAR *pvEDX)
955{
956# if RT_INLINE_ASM_GNU_STYLE
957# ifdef RT_ARCH_AMD64
958 RTCCUINTREG uRAX, uRBX, uRCX, uRDX;
959 __asm__ ("cpuid\n\t"
960 : "=a" (uRAX),
961 "=b" (uRBX),
962 "=c" (uRCX),
963 "=d" (uRDX)
964 : "0" (uOperator),
965 "2" (uIdxECX));
966 *(uint32_t RT_FAR *)pvEAX = (uint32_t)uRAX;
967 *(uint32_t RT_FAR *)pvEBX = (uint32_t)uRBX;
968 *(uint32_t RT_FAR *)pvECX = (uint32_t)uRCX;
969 *(uint32_t RT_FAR *)pvEDX = (uint32_t)uRDX;
970# else
971 __asm__ ("xchgl %%ebx, %1\n\t"
972 "cpuid\n\t"
973 "xchgl %%ebx, %1\n\t"
974 : "=a" (*(uint32_t *)pvEAX),
975 "=r" (*(uint32_t *)pvEBX),
976 "=c" (*(uint32_t *)pvECX),
977 "=d" (*(uint32_t *)pvEDX)
978 : "0" (uOperator),
979 "2" (uIdxECX));
980# endif
981
982# elif RT_INLINE_ASM_USES_INTRIN
983 int aInfo[4];
984 __cpuidex(aInfo, uOperator, uIdxECX);
985 *(uint32_t RT_FAR *)pvEAX = aInfo[0];
986 *(uint32_t RT_FAR *)pvEBX = aInfo[1];
987 *(uint32_t RT_FAR *)pvECX = aInfo[2];
988 *(uint32_t RT_FAR *)pvEDX = aInfo[3];
989
990# else
991 uint32_t uEAX;
992 uint32_t uEBX;
993 uint32_t uECX;
994 uint32_t uEDX;
995 __asm
996 {
997 push ebx
998 mov eax, [uOperator]
999 mov ecx, [uIdxECX]
1000 cpuid
1001 mov [uEAX], eax
1002 mov [uEBX], ebx
1003 mov [uECX], ecx
1004 mov [uEDX], edx
1005 pop ebx
1006 }
1007 *(uint32_t RT_FAR *)pvEAX = uEAX;
1008 *(uint32_t RT_FAR *)pvEBX = uEBX;
1009 *(uint32_t RT_FAR *)pvECX = uECX;
1010 *(uint32_t RT_FAR *)pvEDX = uEDX;
1011# endif
1012}
1013#endif
1014
1015
1016/**
1017 * CPUID variant that initializes all 4 registers before the CPUID instruction.
1018 *
1019 * @returns The EAX result value.
1020 * @param uOperator CPUID operation (eax).
1021 * @param uInitEBX The value to assign EBX prior to the CPUID instruction.
1022 * @param uInitECX The value to assign ECX prior to the CPUID instruction.
1023 * @param uInitEDX The value to assign EDX prior to the CPUID instruction.
1024 * @param pvEAX Where to store eax. Optional.
1025 * @param pvEBX Where to store ebx. Optional.
1026 * @param pvECX Where to store ecx. Optional.
1027 * @param pvEDX Where to store edx. Optional.
1028 */
1029DECLASM(uint32_t) ASMCpuIdExSlow(uint32_t uOperator, uint32_t uInitEBX, uint32_t uInitECX, uint32_t uInitEDX,
1030 void RT_FAR *pvEAX, void RT_FAR *pvEBX, void RT_FAR *pvECX, void RT_FAR *pvEDX);
1031
1032
1033/**
1034 * Performs the cpuid instruction returning ecx and edx.
1035 *
1036 * @param uOperator CPUID operation (eax).
1037 * @param pvECX Where to store ecx.
1038 * @param pvEDX Where to store edx.
1039 * @remark We're using void pointers to ease the use of special bitfield structures and such.
1040 */
1041#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
1042DECLASM(void) ASMCpuId_ECX_EDX(uint32_t uOperator, void RT_FAR *pvECX, void RT_FAR *pvEDX);
1043#else
1044DECLINLINE(void) ASMCpuId_ECX_EDX(uint32_t uOperator, void RT_FAR *pvECX, void RT_FAR *pvEDX)
1045{
1046 uint32_t uEBX;
1047 ASMCpuId(uOperator, &uOperator, &uEBX, pvECX, pvEDX);
1048}
1049#endif
1050
1051
1052/**
1053 * Performs the cpuid instruction returning eax.
1054 *
1055 * @param uOperator CPUID operation (eax).
1056 * @returns EAX after cpuid operation.
1057 */
1058#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
1059DECLASM(uint32_t) ASMCpuId_EAX(uint32_t uOperator);
1060#else
1061DECLINLINE(uint32_t) ASMCpuId_EAX(uint32_t uOperator)
1062{
1063 RTCCUINTREG xAX;
1064# if RT_INLINE_ASM_GNU_STYLE
1065# ifdef RT_ARCH_AMD64
1066 __asm__ ("cpuid"
1067 : "=a" (xAX)
1068 : "0" (uOperator)
1069 : "rbx", "rcx", "rdx");
1070# elif (defined(PIC) || defined(__PIC__)) && defined(__i386__)
1071 __asm__ ("push %%ebx\n\t"
1072 "cpuid\n\t"
1073 "pop %%ebx\n\t"
1074 : "=a" (xAX)
1075 : "0" (uOperator)
1076 : "ecx", "edx");
1077# else
1078 __asm__ ("cpuid"
1079 : "=a" (xAX)
1080 : "0" (uOperator)
1081 : "edx", "ecx", "ebx");
1082# endif
1083
1084# elif RT_INLINE_ASM_USES_INTRIN
1085 int aInfo[4];
1086 __cpuid(aInfo, uOperator);
1087 xAX = aInfo[0];
1088
1089# else
1090 __asm
1091 {
1092 push ebx
1093 mov eax, [uOperator]
1094 cpuid
1095 mov [xAX], eax
1096 pop ebx
1097 }
1098# endif
1099 return (uint32_t)xAX;
1100}
1101#endif
1102
1103
1104/**
1105 * Performs the cpuid instruction returning ebx.
1106 *
1107 * @param uOperator CPUID operation (eax).
1108 * @returns EBX after cpuid operation.
1109 */
1110#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
1111DECLASM(uint32_t) ASMCpuId_EBX(uint32_t uOperator);
1112#else
1113DECLINLINE(uint32_t) ASMCpuId_EBX(uint32_t uOperator)
1114{
1115 RTCCUINTREG xBX;
1116# if RT_INLINE_ASM_GNU_STYLE
1117# ifdef RT_ARCH_AMD64
1118 RTCCUINTREG uSpill;
1119 __asm__ ("cpuid"
1120 : "=a" (uSpill),
1121 "=b" (xBX)
1122 : "0" (uOperator)
1123 : "rdx", "rcx");
1124# elif (defined(PIC) || defined(__PIC__)) && defined(__i386__)
1125 __asm__ ("push %%ebx\n\t"
1126 "cpuid\n\t"
1127 "mov %%ebx, %%edx\n\t"
1128 "pop %%ebx\n\t"
1129 : "=a" (uOperator),
1130 "=d" (xBX)
1131 : "0" (uOperator)
1132 : "ecx");
1133# else
1134 __asm__ ("cpuid"
1135 : "=a" (uOperator),
1136 "=b" (xBX)
1137 : "0" (uOperator)
1138 : "edx", "ecx");
1139# endif
1140
1141# elif RT_INLINE_ASM_USES_INTRIN
1142 int aInfo[4];
1143 __cpuid(aInfo, uOperator);
1144 xBX = aInfo[1];
1145
1146# else
1147 __asm
1148 {
1149 push ebx
1150 mov eax, [uOperator]
1151 cpuid
1152 mov [xBX], ebx
1153 pop ebx
1154 }
1155# endif
1156 return (uint32_t)xBX;
1157}
1158#endif
1159
1160
1161/**
1162 * Performs the cpuid instruction returning ecx.
1163 *
1164 * @param uOperator CPUID operation (eax).
1165 * @returns ECX after cpuid operation.
1166 */
1167#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
1168DECLASM(uint32_t) ASMCpuId_ECX(uint32_t uOperator);
1169#else
1170DECLINLINE(uint32_t) ASMCpuId_ECX(uint32_t uOperator)
1171{
1172 RTCCUINTREG xCX;
1173# if RT_INLINE_ASM_GNU_STYLE
1174# ifdef RT_ARCH_AMD64
1175 RTCCUINTREG uSpill;
1176 __asm__ ("cpuid"
1177 : "=a" (uSpill),
1178 "=c" (xCX)
1179 : "0" (uOperator)
1180 : "rbx", "rdx");
1181# elif (defined(PIC) || defined(__PIC__)) && defined(__i386__)
1182 __asm__ ("push %%ebx\n\t"
1183 "cpuid\n\t"
1184 "pop %%ebx\n\t"
1185 : "=a" (uOperator),
1186 "=c" (xCX)
1187 : "0" (uOperator)
1188 : "edx");
1189# else
1190 __asm__ ("cpuid"
1191 : "=a" (uOperator),
1192 "=c" (xCX)
1193 : "0" (uOperator)
1194 : "ebx", "edx");
1195
1196# endif
1197
1198# elif RT_INLINE_ASM_USES_INTRIN
1199 int aInfo[4];
1200 __cpuid(aInfo, uOperator);
1201 xCX = aInfo[2];
1202
1203# else
1204 __asm
1205 {
1206 push ebx
1207 mov eax, [uOperator]
1208 cpuid
1209 mov [xCX], ecx
1210 pop ebx
1211 }
1212# endif
1213 return (uint32_t)xCX;
1214}
1215#endif
1216
1217
1218/**
1219 * Performs the cpuid instruction returning edx.
1220 *
1221 * @param uOperator CPUID operation (eax).
1222 * @returns EDX after cpuid operation.
1223 */
1224#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
1225DECLASM(uint32_t) ASMCpuId_EDX(uint32_t uOperator);
1226#else
1227DECLINLINE(uint32_t) ASMCpuId_EDX(uint32_t uOperator)
1228{
1229 RTCCUINTREG xDX;
1230# if RT_INLINE_ASM_GNU_STYLE
1231# ifdef RT_ARCH_AMD64
1232 RTCCUINTREG uSpill;
1233 __asm__ ("cpuid"
1234 : "=a" (uSpill),
1235 "=d" (xDX)
1236 : "0" (uOperator)
1237 : "rbx", "rcx");
1238# elif (defined(PIC) || defined(__PIC__)) && defined(__i386__)
1239 __asm__ ("push %%ebx\n\t"
1240 "cpuid\n\t"
1241 "pop %%ebx\n\t"
1242 : "=a" (uOperator),
1243 "=d" (xDX)
1244 : "0" (uOperator)
1245 : "ecx");
1246# else
1247 __asm__ ("cpuid"
1248 : "=a" (uOperator),
1249 "=d" (xDX)
1250 : "0" (uOperator)
1251 : "ebx", "ecx");
1252# endif
1253
1254# elif RT_INLINE_ASM_USES_INTRIN
1255 int aInfo[4];
1256 __cpuid(aInfo, uOperator);
1257 xDX = aInfo[3];
1258
1259# else
1260 __asm
1261 {
1262 push ebx
1263 mov eax, [uOperator]
1264 cpuid
1265 mov [xDX], edx
1266 pop ebx
1267 }
1268# endif
1269 return (uint32_t)xDX;
1270}
1271#endif
1272
1273
1274/**
1275 * Checks if the current CPU supports CPUID.
1276 *
1277 * @returns true if CPUID is supported.
1278 */
1279#ifdef __WATCOMC__
1280DECLASM(bool) ASMHasCpuId(void);
1281#else
1282DECLINLINE(bool) ASMHasCpuId(void)
1283{
1284# ifdef RT_ARCH_AMD64
1285 return true; /* ASSUME that all amd64 compatible CPUs have cpuid. */
1286# else /* !RT_ARCH_AMD64 */
1287 bool fRet = false;
1288# if RT_INLINE_ASM_GNU_STYLE
1289 uint32_t u1;
1290 uint32_t u2;
1291 __asm__ ("pushf\n\t"
1292 "pop %1\n\t"
1293 "mov %1, %2\n\t"
1294 "xorl $0x200000, %1\n\t"
1295 "push %1\n\t"
1296 "popf\n\t"
1297 "pushf\n\t"
1298 "pop %1\n\t"
1299 "cmpl %1, %2\n\t"
1300 "setne %0\n\t"
1301 "push %2\n\t"
1302 "popf\n\t"
1303 : "=m" (fRet), "=r" (u1), "=r" (u2));
1304# else
1305 __asm
1306 {
1307 pushfd
1308 pop eax
1309 mov ebx, eax
1310 xor eax, 0200000h
1311 push eax
1312 popfd
1313 pushfd
1314 pop eax
1315 cmp eax, ebx
1316 setne fRet
1317 push ebx
1318 popfd
1319 }
1320# endif
1321 return fRet;
1322# endif /* !RT_ARCH_AMD64 */
1323}
1324#endif
1325
1326
1327/**
1328 * Gets the APIC ID of the current CPU.
1329 *
1330 * @returns the APIC ID.
1331 */
1332#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
1333DECLASM(uint8_t) ASMGetApicId(void);
1334#else
1335DECLINLINE(uint8_t) ASMGetApicId(void)
1336{
1337 RTCCUINTREG xBX;
1338# if RT_INLINE_ASM_GNU_STYLE
1339# ifdef RT_ARCH_AMD64
1340 RTCCUINTREG uSpill;
1341 __asm__ __volatile__ ("cpuid"
1342 : "=a" (uSpill),
1343 "=b" (xBX)
1344 : "0" (1)
1345 : "rcx", "rdx");
1346# elif (defined(PIC) || defined(__PIC__)) && defined(__i386__)
1347 RTCCUINTREG uSpill;
1348 __asm__ __volatile__ ("mov %%ebx,%1\n\t"
1349 "cpuid\n\t"
1350 "xchgl %%ebx,%1\n\t"
1351 : "=a" (uSpill),
1352 "=rm" (xBX)
1353 : "0" (1)
1354 : "ecx", "edx");
1355# else
1356 RTCCUINTREG uSpill;
1357 __asm__ __volatile__ ("cpuid"
1358 : "=a" (uSpill),
1359 "=b" (xBX)
1360 : "0" (1)
1361 : "ecx", "edx");
1362# endif
1363
1364# elif RT_INLINE_ASM_USES_INTRIN
1365 int aInfo[4];
1366 __cpuid(aInfo, 1);
1367 xBX = aInfo[1];
1368
1369# else
1370 __asm
1371 {
1372 push ebx
1373 mov eax, 1
1374 cpuid
1375 mov [xBX], ebx
1376 pop ebx
1377 }
1378# endif
1379 return (uint8_t)(xBX >> 24);
1380}
1381#endif
1382
1383
1384/**
1385 * Tests if it a genuine Intel CPU based on the ASMCpuId(0) output.
1386 *
1387 * @returns true/false.
1388 * @param uEBX EBX return from ASMCpuId(0)
1389 * @param uECX ECX return from ASMCpuId(0)
1390 * @param uEDX EDX return from ASMCpuId(0)
1391 */
1392DECLINLINE(bool) ASMIsIntelCpuEx(uint32_t uEBX, uint32_t uECX, uint32_t uEDX)
1393{
1394 return uEBX == UINT32_C(0x756e6547)
1395 && uECX == UINT32_C(0x6c65746e)
1396 && uEDX == UINT32_C(0x49656e69);
1397}
1398
1399
1400/**
1401 * Tests if this is a genuine Intel CPU.
1402 *
1403 * @returns true/false.
1404 * @remarks ASSUMES that cpuid is supported by the CPU.
1405 */
1406DECLINLINE(bool) ASMIsIntelCpu(void)
1407{
1408 uint32_t uEAX, uEBX, uECX, uEDX;
1409 ASMCpuId(0, &uEAX, &uEBX, &uECX, &uEDX);
1410 return ASMIsIntelCpuEx(uEBX, uECX, uEDX);
1411}
1412
1413
1414/**
1415 * Tests if it an authentic AMD CPU based on the ASMCpuId(0) output.
1416 *
1417 * @returns true/false.
1418 * @param uEBX EBX return from ASMCpuId(0)
1419 * @param uECX ECX return from ASMCpuId(0)
1420 * @param uEDX EDX return from ASMCpuId(0)
1421 */
1422DECLINLINE(bool) ASMIsAmdCpuEx(uint32_t uEBX, uint32_t uECX, uint32_t uEDX)
1423{
1424 return uEBX == UINT32_C(0x68747541)
1425 && uECX == UINT32_C(0x444d4163)
1426 && uEDX == UINT32_C(0x69746e65);
1427}
1428
1429
1430/**
1431 * Tests if this is an authentic AMD CPU.
1432 *
1433 * @returns true/false.
1434 * @remarks ASSUMES that cpuid is supported by the CPU.
1435 */
1436DECLINLINE(bool) ASMIsAmdCpu(void)
1437{
1438 uint32_t uEAX, uEBX, uECX, uEDX;
1439 ASMCpuId(0, &uEAX, &uEBX, &uECX, &uEDX);
1440 return ASMIsAmdCpuEx(uEBX, uECX, uEDX);
1441}
1442
1443
1444/**
1445 * Tests if it a centaur hauling VIA CPU based on the ASMCpuId(0) output.
1446 *
1447 * @returns true/false.
1448 * @param uEBX EBX return from ASMCpuId(0).
1449 * @param uECX ECX return from ASMCpuId(0).
1450 * @param uEDX EDX return from ASMCpuId(0).
1451 */
1452DECLINLINE(bool) ASMIsViaCentaurCpuEx(uint32_t uEBX, uint32_t uECX, uint32_t uEDX)
1453{
1454 return uEBX == UINT32_C(0x746e6543)
1455 && uECX == UINT32_C(0x736c7561)
1456 && uEDX == UINT32_C(0x48727561);
1457}
1458
1459
1460/**
1461 * Tests if this is a centaur hauling VIA CPU.
1462 *
1463 * @returns true/false.
1464 * @remarks ASSUMES that cpuid is supported by the CPU.
1465 */
1466DECLINLINE(bool) ASMIsViaCentaurCpu(void)
1467{
1468 uint32_t uEAX, uEBX, uECX, uEDX;
1469 ASMCpuId(0, &uEAX, &uEBX, &uECX, &uEDX);
1470 return ASMIsViaCentaurCpuEx(uEBX, uECX, uEDX);
1471}
1472
1473
1474/**
1475 * Checks whether ASMCpuId_EAX(0x00000000) indicates a valid range.
1476 *
1477 *
1478 * @returns true/false.
1479 * @param uEAX The EAX value of CPUID leaf 0x00000000.
1480 *
1481 * @note This only succeeds if there are at least two leaves in the range.
1482 * @remarks The upper range limit is just some half reasonable value we've
1483 * picked out of thin air.
1484 */
1485DECLINLINE(bool) ASMIsValidStdRange(uint32_t uEAX)
1486{
1487 return uEAX >= UINT32_C(0x00000001) && uEAX <= UINT32_C(0x000fffff);
1488}
1489
1490
1491/**
1492 * Checks whether ASMCpuId_EAX(0x80000000) indicates a valid range.
1493 *
1494 * This only succeeds if there are at least two leaves in the range.
1495 *
1496 * @returns true/false.
1497 * @param uEAX The EAX value of CPUID leaf 0x80000000.
1498 *
1499 * @note This only succeeds if there are at least two leaves in the range.
1500 * @remarks The upper range limit is just some half reasonable value we've
1501 * picked out of thin air.
1502 */
1503DECLINLINE(bool) ASMIsValidExtRange(uint32_t uEAX)
1504{
1505 return uEAX >= UINT32_C(0x80000001) && uEAX <= UINT32_C(0x800fffff);
1506}
1507
1508
1509/**
1510 * Extracts the CPU family from ASMCpuId(1) or ASMCpuId(0x80000001)
1511 *
1512 * @returns Family.
1513 * @param uEAX EAX return from ASMCpuId(1) or ASMCpuId(0x80000001).
1514 */
1515DECLINLINE(uint32_t) ASMGetCpuFamily(uint32_t uEAX)
1516{
1517 return ((uEAX >> 8) & 0xf) == 0xf
1518 ? ((uEAX >> 20) & 0x7f) + 0xf
1519 : ((uEAX >> 8) & 0xf);
1520}
1521
1522
1523/**
1524 * Extracts the CPU model from ASMCpuId(1) or ASMCpuId(0x80000001), Intel variant.
1525 *
1526 * @returns Model.
1527 * @param uEAX EAX from ASMCpuId(1) or ASMCpuId(0x80000001).
1528 */
1529DECLINLINE(uint32_t) ASMGetCpuModelIntel(uint32_t uEAX)
1530{
1531 return ((uEAX >> 8) & 0xf) == 0xf || (((uEAX >> 8) & 0xf) == 0x6) /* family! */
1532 ? ((uEAX >> 4) & 0xf) | ((uEAX >> 12) & 0xf0)
1533 : ((uEAX >> 4) & 0xf);
1534}
1535
1536
1537/**
1538 * Extracts the CPU model from ASMCpuId(1) or ASMCpuId(0x80000001), AMD variant.
1539 *
1540 * @returns Model.
1541 * @param uEAX EAX from ASMCpuId(1) or ASMCpuId(0x80000001).
1542 */
1543DECLINLINE(uint32_t) ASMGetCpuModelAMD(uint32_t uEAX)
1544{
1545 return ((uEAX >> 8) & 0xf) == 0xf
1546 ? ((uEAX >> 4) & 0xf) | ((uEAX >> 12) & 0xf0)
1547 : ((uEAX >> 4) & 0xf);
1548}
1549
1550
1551/**
1552 * Extracts the CPU model from ASMCpuId(1) or ASMCpuId(0x80000001)
1553 *
1554 * @returns Model.
1555 * @param uEAX EAX from ASMCpuId(1) or ASMCpuId(0x80000001).
1556 * @param fIntel Whether it's an intel CPU. Use ASMIsIntelCpuEx() or ASMIsIntelCpu().
1557 */
1558DECLINLINE(uint32_t) ASMGetCpuModel(uint32_t uEAX, bool fIntel)
1559{
1560 return ((uEAX >> 8) & 0xf) == 0xf || (((uEAX >> 8) & 0xf) == 0x6 && fIntel) /* family! */
1561 ? ((uEAX >> 4) & 0xf) | ((uEAX >> 12) & 0xf0)
1562 : ((uEAX >> 4) & 0xf);
1563}
1564
1565
1566/**
1567 * Extracts the CPU stepping from ASMCpuId(1) or ASMCpuId(0x80000001)
1568 *
1569 * @returns Model.
1570 * @param uEAX EAX from ASMCpuId(1) or ASMCpuId(0x80000001).
1571 */
1572DECLINLINE(uint32_t) ASMGetCpuStepping(uint32_t uEAX)
1573{
1574 return uEAX & 0xf;
1575}
1576
1577
1578/**
1579 * Get cr0.
1580 * @returns cr0.
1581 */
1582#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
1583DECLASM(RTCCUINTXREG) ASMGetCR0(void);
1584#else
1585DECLINLINE(RTCCUINTXREG) ASMGetCR0(void)
1586{
1587 RTCCUINTXREG uCR0;
1588# if RT_INLINE_ASM_USES_INTRIN
1589 uCR0 = __readcr0();
1590
1591# elif RT_INLINE_ASM_GNU_STYLE
1592# ifdef RT_ARCH_AMD64
1593 __asm__ __volatile__("movq %%cr0, %0\t\n" : "=r" (uCR0));
1594# else
1595 __asm__ __volatile__("movl %%cr0, %0\t\n" : "=r" (uCR0));
1596# endif
1597# else
1598 __asm
1599 {
1600# ifdef RT_ARCH_AMD64
1601 mov rax, cr0
1602 mov [uCR0], rax
1603# else
1604 mov eax, cr0
1605 mov [uCR0], eax
1606# endif
1607 }
1608# endif
1609 return uCR0;
1610}
1611#endif
1612
1613
1614/**
1615 * Sets the CR0 register.
1616 * @param uCR0 The new CR0 value.
1617 */
1618#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
1619DECLASM(void) ASMSetCR0(RTCCUINTXREG uCR0);
1620#else
1621DECLINLINE(void) ASMSetCR0(RTCCUINTXREG uCR0)
1622{
1623# if RT_INLINE_ASM_USES_INTRIN
1624 __writecr0(uCR0);
1625
1626# elif RT_INLINE_ASM_GNU_STYLE
1627# ifdef RT_ARCH_AMD64
1628 __asm__ __volatile__("movq %0, %%cr0\n\t" :: "r" (uCR0));
1629# else
1630 __asm__ __volatile__("movl %0, %%cr0\n\t" :: "r" (uCR0));
1631# endif
1632# else
1633 __asm
1634 {
1635# ifdef RT_ARCH_AMD64
1636 mov rax, [uCR0]
1637 mov cr0, rax
1638# else
1639 mov eax, [uCR0]
1640 mov cr0, eax
1641# endif
1642 }
1643# endif
1644}
1645#endif
1646
1647
1648/**
1649 * Get cr2.
1650 * @returns cr2.
1651 */
1652#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
1653DECLASM(RTCCUINTXREG) ASMGetCR2(void);
1654#else
1655DECLINLINE(RTCCUINTXREG) ASMGetCR2(void)
1656{
1657 RTCCUINTXREG uCR2;
1658# if RT_INLINE_ASM_USES_INTRIN
1659 uCR2 = __readcr2();
1660
1661# elif RT_INLINE_ASM_GNU_STYLE
1662# ifdef RT_ARCH_AMD64
1663 __asm__ __volatile__("movq %%cr2, %0\t\n" : "=r" (uCR2));
1664# else
1665 __asm__ __volatile__("movl %%cr2, %0\t\n" : "=r" (uCR2));
1666# endif
1667# else
1668 __asm
1669 {
1670# ifdef RT_ARCH_AMD64
1671 mov rax, cr2
1672 mov [uCR2], rax
1673# else
1674 mov eax, cr2
1675 mov [uCR2], eax
1676# endif
1677 }
1678# endif
1679 return uCR2;
1680}
1681#endif
1682
1683
1684/**
1685 * Sets the CR2 register.
1686 * @param uCR2 The new CR0 value.
1687 */
1688#if RT_INLINE_ASM_EXTERNAL
1689DECLASM(void) ASMSetCR2(RTCCUINTXREG uCR2);
1690#else
1691DECLINLINE(void) ASMSetCR2(RTCCUINTXREG uCR2)
1692{
1693# if RT_INLINE_ASM_GNU_STYLE
1694# ifdef RT_ARCH_AMD64
1695 __asm__ __volatile__("movq %0, %%cr2\n\t" :: "r" (uCR2));
1696# else
1697 __asm__ __volatile__("movl %0, %%cr2\n\t" :: "r" (uCR2));
1698# endif
1699# else
1700 __asm
1701 {
1702# ifdef RT_ARCH_AMD64
1703 mov rax, [uCR2]
1704 mov cr2, rax
1705# else
1706 mov eax, [uCR2]
1707 mov cr2, eax
1708# endif
1709 }
1710# endif
1711}
1712#endif
1713
1714
1715/**
1716 * Get cr3.
1717 * @returns cr3.
1718 */
1719#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
1720DECLASM(RTCCUINTXREG) ASMGetCR3(void);
1721#else
1722DECLINLINE(RTCCUINTXREG) ASMGetCR3(void)
1723{
1724 RTCCUINTXREG uCR3;
1725# if RT_INLINE_ASM_USES_INTRIN
1726 uCR3 = __readcr3();
1727
1728# elif RT_INLINE_ASM_GNU_STYLE
1729# ifdef RT_ARCH_AMD64
1730 __asm__ __volatile__("movq %%cr3, %0\t\n" : "=r" (uCR3));
1731# else
1732 __asm__ __volatile__("movl %%cr3, %0\t\n" : "=r" (uCR3));
1733# endif
1734# else
1735 __asm
1736 {
1737# ifdef RT_ARCH_AMD64
1738 mov rax, cr3
1739 mov [uCR3], rax
1740# else
1741 mov eax, cr3
1742 mov [uCR3], eax
1743# endif
1744 }
1745# endif
1746 return uCR3;
1747}
1748#endif
1749
1750
1751/**
1752 * Sets the CR3 register.
1753 *
1754 * @param uCR3 New CR3 value.
1755 */
1756#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
1757DECLASM(void) ASMSetCR3(RTCCUINTXREG uCR3);
1758#else
1759DECLINLINE(void) ASMSetCR3(RTCCUINTXREG uCR3)
1760{
1761# if RT_INLINE_ASM_USES_INTRIN
1762 __writecr3(uCR3);
1763
1764# elif RT_INLINE_ASM_GNU_STYLE
1765# ifdef RT_ARCH_AMD64
1766 __asm__ __volatile__("movq %0, %%cr3\n\t" : : "r" (uCR3));
1767# else
1768 __asm__ __volatile__("movl %0, %%cr3\n\t" : : "r" (uCR3));
1769# endif
1770# else
1771 __asm
1772 {
1773# ifdef RT_ARCH_AMD64
1774 mov rax, [uCR3]
1775 mov cr3, rax
1776# else
1777 mov eax, [uCR3]
1778 mov cr3, eax
1779# endif
1780 }
1781# endif
1782}
1783#endif
1784
1785
1786/**
1787 * Reloads the CR3 register.
1788 */
1789#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
1790DECLASM(void) ASMReloadCR3(void);
1791#else
1792DECLINLINE(void) ASMReloadCR3(void)
1793{
1794# if RT_INLINE_ASM_USES_INTRIN
1795 __writecr3(__readcr3());
1796
1797# elif RT_INLINE_ASM_GNU_STYLE
1798 RTCCUINTXREG u;
1799# ifdef RT_ARCH_AMD64
1800 __asm__ __volatile__("movq %%cr3, %0\n\t"
1801 "movq %0, %%cr3\n\t"
1802 : "=r" (u));
1803# else
1804 __asm__ __volatile__("movl %%cr3, %0\n\t"
1805 "movl %0, %%cr3\n\t"
1806 : "=r" (u));
1807# endif
1808# else
1809 __asm
1810 {
1811# ifdef RT_ARCH_AMD64
1812 mov rax, cr3
1813 mov cr3, rax
1814# else
1815 mov eax, cr3
1816 mov cr3, eax
1817# endif
1818 }
1819# endif
1820}
1821#endif
1822
1823
1824/**
1825 * Get cr4.
1826 * @returns cr4.
1827 */
1828#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
1829DECLASM(RTCCUINTXREG) ASMGetCR4(void);
1830#else
1831DECLINLINE(RTCCUINTXREG) ASMGetCR4(void)
1832{
1833 RTCCUINTXREG uCR4;
1834# if RT_INLINE_ASM_USES_INTRIN
1835 uCR4 = __readcr4();
1836
1837# elif RT_INLINE_ASM_GNU_STYLE
1838# ifdef RT_ARCH_AMD64
1839 __asm__ __volatile__("movq %%cr4, %0\t\n" : "=r" (uCR4));
1840# else
1841 __asm__ __volatile__("movl %%cr4, %0\t\n" : "=r" (uCR4));
1842# endif
1843# else
1844 __asm
1845 {
1846# ifdef RT_ARCH_AMD64
1847 mov rax, cr4
1848 mov [uCR4], rax
1849# else
1850 push eax /* just in case */
1851 /*mov eax, cr4*/
1852 _emit 0x0f
1853 _emit 0x20
1854 _emit 0xe0
1855 mov [uCR4], eax
1856 pop eax
1857# endif
1858 }
1859# endif
1860 return uCR4;
1861}
1862#endif
1863
1864
1865/**
1866 * Sets the CR4 register.
1867 *
1868 * @param uCR4 New CR4 value.
1869 */
1870#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
1871DECLASM(void) ASMSetCR4(RTCCUINTXREG uCR4);
1872#else
1873DECLINLINE(void) ASMSetCR4(RTCCUINTXREG uCR4)
1874{
1875# if RT_INLINE_ASM_USES_INTRIN
1876 __writecr4(uCR4);
1877
1878# elif RT_INLINE_ASM_GNU_STYLE
1879# ifdef RT_ARCH_AMD64
1880 __asm__ __volatile__("movq %0, %%cr4\n\t" : : "r" (uCR4));
1881# else
1882 __asm__ __volatile__("movl %0, %%cr4\n\t" : : "r" (uCR4));
1883# endif
1884# else
1885 __asm
1886 {
1887# ifdef RT_ARCH_AMD64
1888 mov rax, [uCR4]
1889 mov cr4, rax
1890# else
1891 mov eax, [uCR4]
1892 _emit 0x0F
1893 _emit 0x22
1894 _emit 0xE0 /* mov cr4, eax */
1895# endif
1896 }
1897# endif
1898}
1899#endif
1900
1901
1902/**
1903 * Get cr8.
1904 * @returns cr8.
1905 * @remark The lock prefix hack for access from non-64-bit modes is NOT used and 0 is returned.
1906 */
1907#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
1908DECLASM(RTCCUINTXREG) ASMGetCR8(void);
1909#else
1910DECLINLINE(RTCCUINTXREG) ASMGetCR8(void)
1911{
1912# ifdef RT_ARCH_AMD64
1913 RTCCUINTXREG uCR8;
1914# if RT_INLINE_ASM_USES_INTRIN
1915 uCR8 = __readcr8();
1916
1917# elif RT_INLINE_ASM_GNU_STYLE
1918 __asm__ __volatile__("movq %%cr8, %0\t\n" : "=r" (uCR8));
1919# else
1920 __asm
1921 {
1922 mov rax, cr8
1923 mov [uCR8], rax
1924 }
1925# endif
1926 return uCR8;
1927# else /* !RT_ARCH_AMD64 */
1928 return 0;
1929# endif /* !RT_ARCH_AMD64 */
1930}
1931#endif
1932
1933
1934/**
1935 * Get XCR0 (eXtended feature Control Register 0).
1936 * @returns xcr0.
1937 */
1938DECLASM(uint64_t) ASMGetXcr0(void);
1939
1940/**
1941 * Sets the XCR0 register.
1942 * @param uXcr0 The new XCR0 value.
1943 */
1944DECLASM(void) ASMSetXcr0(uint64_t uXcr0);
1945
1946struct X86XSAVEAREA;
1947/**
1948 * Save extended CPU state.
1949 * @param pXStateArea Where to save the state.
1950 * @param fComponents Which state components to save.
1951 */
1952DECLASM(void) ASMXSave(struct X86XSAVEAREA RT_FAR *pXStateArea, uint64_t fComponents);
1953
1954/**
1955 * Loads extended CPU state.
1956 * @param pXStateArea Where to load the state from.
1957 * @param fComponents Which state components to load.
1958 */
1959DECLASM(void) ASMXRstor(struct X86XSAVEAREA const RT_FAR *pXStateArea, uint64_t fComponents);
1960
1961
1962struct X86FXSTATE;
1963/**
1964 * Save FPU and SSE CPU state.
1965 * @param pXStateArea Where to save the state.
1966 */
1967DECLASM(void) ASMFxSave(struct X86FXSTATE RT_FAR *pXStateArea);
1968
1969/**
1970 * Load FPU and SSE CPU state.
1971 * @param pXStateArea Where to load the state from.
1972 */
1973DECLASM(void) ASMFxRstor(struct X86FXSTATE const RT_FAR *pXStateArea);
1974
1975
1976/**
1977 * Enables interrupts (EFLAGS.IF).
1978 */
1979#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
1980DECLASM(void) ASMIntEnable(void);
1981#else
1982DECLINLINE(void) ASMIntEnable(void)
1983{
1984# if RT_INLINE_ASM_GNU_STYLE
1985 __asm("sti\n");
1986# elif RT_INLINE_ASM_USES_INTRIN
1987 _enable();
1988# else
1989 __asm sti
1990# endif
1991}
1992#endif
1993
1994
1995/**
1996 * Disables interrupts (!EFLAGS.IF).
1997 */
1998#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
1999DECLASM(void) ASMIntDisable(void);
2000#else
2001DECLINLINE(void) ASMIntDisable(void)
2002{
2003# if RT_INLINE_ASM_GNU_STYLE
2004 __asm("cli\n");
2005# elif RT_INLINE_ASM_USES_INTRIN
2006 _disable();
2007# else
2008 __asm cli
2009# endif
2010}
2011#endif
2012
2013
2014/**
2015 * Disables interrupts and returns previous xFLAGS.
2016 */
2017#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
2018DECLASM(RTCCUINTREG) ASMIntDisableFlags(void);
2019#else
2020DECLINLINE(RTCCUINTREG) ASMIntDisableFlags(void)
2021{
2022 RTCCUINTREG xFlags;
2023# if RT_INLINE_ASM_GNU_STYLE
2024# ifdef RT_ARCH_AMD64
2025 __asm__ __volatile__("pushfq\n\t"
2026 "cli\n\t"
2027 "popq %0\n\t"
2028 : "=r" (xFlags));
2029# else
2030 __asm__ __volatile__("pushfl\n\t"
2031 "cli\n\t"
2032 "popl %0\n\t"
2033 : "=r" (xFlags));
2034# endif
2035# elif RT_INLINE_ASM_USES_INTRIN && !defined(RT_ARCH_X86)
2036 xFlags = ASMGetFlags();
2037 _disable();
2038# else
2039 __asm {
2040 pushfd
2041 cli
2042 pop [xFlags]
2043 }
2044# endif
2045 return xFlags;
2046}
2047#endif
2048
2049
2050/**
2051 * Are interrupts enabled?
2052 *
2053 * @returns true / false.
2054 */
2055DECLINLINE(bool) ASMIntAreEnabled(void)
2056{
2057 RTCCUINTREG uFlags = ASMGetFlags();
2058 return uFlags & 0x200 /* X86_EFL_IF */ ? true : false;
2059}
2060
2061
2062/**
2063 * Halts the CPU until interrupted.
2064 */
2065#if RT_INLINE_ASM_EXTERNAL && RT_INLINE_ASM_USES_INTRIN < 14
2066DECLASM(void) ASMHalt(void);
2067#else
2068DECLINLINE(void) ASMHalt(void)
2069{
2070# if RT_INLINE_ASM_GNU_STYLE
2071 __asm__ __volatile__("hlt\n\t");
2072# elif RT_INLINE_ASM_USES_INTRIN
2073 __halt();
2074# else
2075 __asm {
2076 hlt
2077 }
2078# endif
2079}
2080#endif
2081
2082
2083/**
2084 * Reads a machine specific register.
2085 *
2086 * @returns Register content.
2087 * @param uRegister Register to read.
2088 */
2089#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
2090DECLASM(uint64_t) ASMRdMsr(uint32_t uRegister);
2091#else
2092DECLINLINE(uint64_t) ASMRdMsr(uint32_t uRegister)
2093{
2094 RTUINT64U u;
2095# if RT_INLINE_ASM_GNU_STYLE
2096 __asm__ __volatile__("rdmsr\n\t"
2097 : "=a" (u.s.Lo),
2098 "=d" (u.s.Hi)
2099 : "c" (uRegister));
2100
2101# elif RT_INLINE_ASM_USES_INTRIN
2102 u.u = __readmsr(uRegister);
2103
2104# else
2105 __asm
2106 {
2107 mov ecx, [uRegister]
2108 rdmsr
2109 mov [u.s.Lo], eax
2110 mov [u.s.Hi], edx
2111 }
2112# endif
2113
2114 return u.u;
2115}
2116#endif
2117
2118
2119/**
2120 * Writes a machine specific register.
2121 *
2122 * @returns Register content.
2123 * @param uRegister Register to write to.
2124 * @param u64Val Value to write.
2125 */
2126#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
2127DECLASM(void) ASMWrMsr(uint32_t uRegister, uint64_t u64Val);
2128#else
2129DECLINLINE(void) ASMWrMsr(uint32_t uRegister, uint64_t u64Val)
2130{
2131 RTUINT64U u;
2132
2133 u.u = u64Val;
2134# if RT_INLINE_ASM_GNU_STYLE
2135 __asm__ __volatile__("wrmsr\n\t"
2136 ::"a" (u.s.Lo),
2137 "d" (u.s.Hi),
2138 "c" (uRegister));
2139
2140# elif RT_INLINE_ASM_USES_INTRIN
2141 __writemsr(uRegister, u.u);
2142
2143# else
2144 __asm
2145 {
2146 mov ecx, [uRegister]
2147 mov edx, [u.s.Hi]
2148 mov eax, [u.s.Lo]
2149 wrmsr
2150 }
2151# endif
2152}
2153#endif
2154
2155
2156/**
2157 * Reads a machine specific register, extended version (for AMD).
2158 *
2159 * @returns Register content.
2160 * @param uRegister Register to read.
2161 * @param uXDI RDI/EDI value.
2162 */
2163#if RT_INLINE_ASM_EXTERNAL
2164DECLASM(uint64_t) ASMRdMsrEx(uint32_t uRegister, RTCCUINTXREG uXDI);
2165#else
2166DECLINLINE(uint64_t) ASMRdMsrEx(uint32_t uRegister, RTCCUINTXREG uXDI)
2167{
2168 RTUINT64U u;
2169# if RT_INLINE_ASM_GNU_STYLE
2170 __asm__ __volatile__("rdmsr\n\t"
2171 : "=a" (u.s.Lo),
2172 "=d" (u.s.Hi)
2173 : "c" (uRegister),
2174 "D" (uXDI));
2175
2176# else
2177 __asm
2178 {
2179 mov ecx, [uRegister]
2180 xchg edi, [uXDI]
2181 rdmsr
2182 mov [u.s.Lo], eax
2183 mov [u.s.Hi], edx
2184 xchg edi, [uXDI]
2185 }
2186# endif
2187
2188 return u.u;
2189}
2190#endif
2191
2192
2193/**
2194 * Writes a machine specific register, extended version (for AMD).
2195 *
2196 * @returns Register content.
2197 * @param uRegister Register to write to.
2198 * @param uXDI RDI/EDI value.
2199 * @param u64Val Value to write.
2200 */
2201#if RT_INLINE_ASM_EXTERNAL
2202DECLASM(void) ASMWrMsrEx(uint32_t uRegister, RTCCUINTXREG uXDI, uint64_t u64Val);
2203#else
2204DECLINLINE(void) ASMWrMsrEx(uint32_t uRegister, RTCCUINTXREG uXDI, uint64_t u64Val)
2205{
2206 RTUINT64U u;
2207
2208 u.u = u64Val;
2209# if RT_INLINE_ASM_GNU_STYLE
2210 __asm__ __volatile__("wrmsr\n\t"
2211 ::"a" (u.s.Lo),
2212 "d" (u.s.Hi),
2213 "c" (uRegister),
2214 "D" (uXDI));
2215
2216# else
2217 __asm
2218 {
2219 mov ecx, [uRegister]
2220 xchg edi, [uXDI]
2221 mov edx, [u.s.Hi]
2222 mov eax, [u.s.Lo]
2223 wrmsr
2224 xchg edi, [uXDI]
2225 }
2226# endif
2227}
2228#endif
2229
2230
2231
2232/**
2233 * Reads low part of a machine specific register.
2234 *
2235 * @returns Register content.
2236 * @param uRegister Register to read.
2237 */
2238#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
2239DECLASM(uint32_t) ASMRdMsr_Low(uint32_t uRegister);
2240#else
2241DECLINLINE(uint32_t) ASMRdMsr_Low(uint32_t uRegister)
2242{
2243 uint32_t u32;
2244# if RT_INLINE_ASM_GNU_STYLE
2245 __asm__ __volatile__("rdmsr\n\t"
2246 : "=a" (u32)
2247 : "c" (uRegister)
2248 : "edx");
2249
2250# elif RT_INLINE_ASM_USES_INTRIN
2251 u32 = (uint32_t)__readmsr(uRegister);
2252
2253#else
2254 __asm
2255 {
2256 mov ecx, [uRegister]
2257 rdmsr
2258 mov [u32], eax
2259 }
2260# endif
2261
2262 return u32;
2263}
2264#endif
2265
2266
2267/**
2268 * Reads high part of a machine specific register.
2269 *
2270 * @returns Register content.
2271 * @param uRegister Register to read.
2272 */
2273#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
2274DECLASM(uint32_t) ASMRdMsr_High(uint32_t uRegister);
2275#else
2276DECLINLINE(uint32_t) ASMRdMsr_High(uint32_t uRegister)
2277{
2278 uint32_t u32;
2279# if RT_INLINE_ASM_GNU_STYLE
2280 __asm__ __volatile__("rdmsr\n\t"
2281 : "=d" (u32)
2282 : "c" (uRegister)
2283 : "eax");
2284
2285# elif RT_INLINE_ASM_USES_INTRIN
2286 u32 = (uint32_t)(__readmsr(uRegister) >> 32);
2287
2288# else
2289 __asm
2290 {
2291 mov ecx, [uRegister]
2292 rdmsr
2293 mov [u32], edx
2294 }
2295# endif
2296
2297 return u32;
2298}
2299#endif
2300
2301
2302/**
2303 * Gets dr0.
2304 *
2305 * @returns dr0.
2306 */
2307#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
2308DECLASM(RTCCUINTXREG) ASMGetDR0(void);
2309#else
2310DECLINLINE(RTCCUINTXREG) ASMGetDR0(void)
2311{
2312 RTCCUINTXREG uDR0;
2313# if RT_INLINE_ASM_USES_INTRIN
2314 uDR0 = __readdr(0);
2315# elif RT_INLINE_ASM_GNU_STYLE
2316# ifdef RT_ARCH_AMD64
2317 __asm__ __volatile__("movq %%dr0, %0\n\t" : "=r" (uDR0));
2318# else
2319 __asm__ __volatile__("movl %%dr0, %0\n\t" : "=r" (uDR0));
2320# endif
2321# else
2322 __asm
2323 {
2324# ifdef RT_ARCH_AMD64
2325 mov rax, dr0
2326 mov [uDR0], rax
2327# else
2328 mov eax, dr0
2329 mov [uDR0], eax
2330# endif
2331 }
2332# endif
2333 return uDR0;
2334}
2335#endif
2336
2337
2338/**
2339 * Gets dr1.
2340 *
2341 * @returns dr1.
2342 */
2343#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
2344DECLASM(RTCCUINTXREG) ASMGetDR1(void);
2345#else
2346DECLINLINE(RTCCUINTXREG) ASMGetDR1(void)
2347{
2348 RTCCUINTXREG uDR1;
2349# if RT_INLINE_ASM_USES_INTRIN
2350 uDR1 = __readdr(1);
2351# elif RT_INLINE_ASM_GNU_STYLE
2352# ifdef RT_ARCH_AMD64
2353 __asm__ __volatile__("movq %%dr1, %0\n\t" : "=r" (uDR1));
2354# else
2355 __asm__ __volatile__("movl %%dr1, %0\n\t" : "=r" (uDR1));
2356# endif
2357# else
2358 __asm
2359 {
2360# ifdef RT_ARCH_AMD64
2361 mov rax, dr1
2362 mov [uDR1], rax
2363# else
2364 mov eax, dr1
2365 mov [uDR1], eax
2366# endif
2367 }
2368# endif
2369 return uDR1;
2370}
2371#endif
2372
2373
2374/**
2375 * Gets dr2.
2376 *
2377 * @returns dr2.
2378 */
2379#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
2380DECLASM(RTCCUINTXREG) ASMGetDR2(void);
2381#else
2382DECLINLINE(RTCCUINTXREG) ASMGetDR2(void)
2383{
2384 RTCCUINTXREG uDR2;
2385# if RT_INLINE_ASM_USES_INTRIN
2386 uDR2 = __readdr(2);
2387# elif RT_INLINE_ASM_GNU_STYLE
2388# ifdef RT_ARCH_AMD64
2389 __asm__ __volatile__("movq %%dr2, %0\n\t" : "=r" (uDR2));
2390# else
2391 __asm__ __volatile__("movl %%dr2, %0\n\t" : "=r" (uDR2));
2392# endif
2393# else
2394 __asm
2395 {
2396# ifdef RT_ARCH_AMD64
2397 mov rax, dr2
2398 mov [uDR2], rax
2399# else
2400 mov eax, dr2
2401 mov [uDR2], eax
2402# endif
2403 }
2404# endif
2405 return uDR2;
2406}
2407#endif
2408
2409
2410/**
2411 * Gets dr3.
2412 *
2413 * @returns dr3.
2414 */
2415#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
2416DECLASM(RTCCUINTXREG) ASMGetDR3(void);
2417#else
2418DECLINLINE(RTCCUINTXREG) ASMGetDR3(void)
2419{
2420 RTCCUINTXREG uDR3;
2421# if RT_INLINE_ASM_USES_INTRIN
2422 uDR3 = __readdr(3);
2423# elif RT_INLINE_ASM_GNU_STYLE
2424# ifdef RT_ARCH_AMD64
2425 __asm__ __volatile__("movq %%dr3, %0\n\t" : "=r" (uDR3));
2426# else
2427 __asm__ __volatile__("movl %%dr3, %0\n\t" : "=r" (uDR3));
2428# endif
2429# else
2430 __asm
2431 {
2432# ifdef RT_ARCH_AMD64
2433 mov rax, dr3
2434 mov [uDR3], rax
2435# else
2436 mov eax, dr3
2437 mov [uDR3], eax
2438# endif
2439 }
2440# endif
2441 return uDR3;
2442}
2443#endif
2444
2445
2446/**
2447 * Gets dr6.
2448 *
2449 * @returns dr6.
2450 */
2451#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
2452DECLASM(RTCCUINTXREG) ASMGetDR6(void);
2453#else
2454DECLINLINE(RTCCUINTXREG) ASMGetDR6(void)
2455{
2456 RTCCUINTXREG uDR6;
2457# if RT_INLINE_ASM_USES_INTRIN
2458 uDR6 = __readdr(6);
2459# elif RT_INLINE_ASM_GNU_STYLE
2460# ifdef RT_ARCH_AMD64
2461 __asm__ __volatile__("movq %%dr6, %0\n\t" : "=r" (uDR6));
2462# else
2463 __asm__ __volatile__("movl %%dr6, %0\n\t" : "=r" (uDR6));
2464# endif
2465# else
2466 __asm
2467 {
2468# ifdef RT_ARCH_AMD64
2469 mov rax, dr6
2470 mov [uDR6], rax
2471# else
2472 mov eax, dr6
2473 mov [uDR6], eax
2474# endif
2475 }
2476# endif
2477 return uDR6;
2478}
2479#endif
2480
2481
2482/**
2483 * Reads and clears DR6.
2484 *
2485 * @returns DR6.
2486 */
2487#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
2488DECLASM(RTCCUINTXREG) ASMGetAndClearDR6(void);
2489#else
2490DECLINLINE(RTCCUINTXREG) ASMGetAndClearDR6(void)
2491{
2492 RTCCUINTXREG uDR6;
2493# if RT_INLINE_ASM_USES_INTRIN
2494 uDR6 = __readdr(6);
2495 __writedr(6, 0xffff0ff0U); /* 31-16 and 4-11 are 1's, 12 and 63-31 are zero. */
2496# elif RT_INLINE_ASM_GNU_STYLE
2497 RTCCUINTXREG uNewValue = 0xffff0ff0U;/* 31-16 and 4-11 are 1's, 12 and 63-31 are zero. */
2498# ifdef RT_ARCH_AMD64
2499 __asm__ __volatile__("movq %%dr6, %0\n\t"
2500 "movq %1, %%dr6\n\t"
2501 : "=r" (uDR6)
2502 : "r" (uNewValue));
2503# else
2504 __asm__ __volatile__("movl %%dr6, %0\n\t"
2505 "movl %1, %%dr6\n\t"
2506 : "=r" (uDR6)
2507 : "r" (uNewValue));
2508# endif
2509# else
2510 __asm
2511 {
2512# ifdef RT_ARCH_AMD64
2513 mov rax, dr6
2514 mov [uDR6], rax
2515 mov rcx, rax
2516 mov ecx, 0ffff0ff0h; /* 31-16 and 4-11 are 1's, 12 and 63-31 are zero. */
2517 mov dr6, rcx
2518# else
2519 mov eax, dr6
2520 mov [uDR6], eax
2521 mov ecx, 0ffff0ff0h; /* 31-16 and 4-11 are 1's, 12 is zero. */
2522 mov dr6, ecx
2523# endif
2524 }
2525# endif
2526 return uDR6;
2527}
2528#endif
2529
2530
2531/**
2532 * Gets dr7.
2533 *
2534 * @returns dr7.
2535 */
2536#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
2537DECLASM(RTCCUINTXREG) ASMGetDR7(void);
2538#else
2539DECLINLINE(RTCCUINTXREG) ASMGetDR7(void)
2540{
2541 RTCCUINTXREG uDR7;
2542# if RT_INLINE_ASM_USES_INTRIN
2543 uDR7 = __readdr(7);
2544# elif RT_INLINE_ASM_GNU_STYLE
2545# ifdef RT_ARCH_AMD64
2546 __asm__ __volatile__("movq %%dr7, %0\n\t" : "=r" (uDR7));
2547# else
2548 __asm__ __volatile__("movl %%dr7, %0\n\t" : "=r" (uDR7));
2549# endif
2550# else
2551 __asm
2552 {
2553# ifdef RT_ARCH_AMD64
2554 mov rax, dr7
2555 mov [uDR7], rax
2556# else
2557 mov eax, dr7
2558 mov [uDR7], eax
2559# endif
2560 }
2561# endif
2562 return uDR7;
2563}
2564#endif
2565
2566
2567/**
2568 * Sets dr0.
2569 *
2570 * @param uDRVal Debug register value to write
2571 */
2572#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
2573DECLASM(void) ASMSetDR0(RTCCUINTXREG uDRVal);
2574#else
2575DECLINLINE(void) ASMSetDR0(RTCCUINTXREG uDRVal)
2576{
2577# if RT_INLINE_ASM_USES_INTRIN
2578 __writedr(0, uDRVal);
2579# elif RT_INLINE_ASM_GNU_STYLE
2580# ifdef RT_ARCH_AMD64
2581 __asm__ __volatile__("movq %0, %%dr0\n\t" : : "r" (uDRVal));
2582# else
2583 __asm__ __volatile__("movl %0, %%dr0\n\t" : : "r" (uDRVal));
2584# endif
2585# else
2586 __asm
2587 {
2588# ifdef RT_ARCH_AMD64
2589 mov rax, [uDRVal]
2590 mov dr0, rax
2591# else
2592 mov eax, [uDRVal]
2593 mov dr0, eax
2594# endif
2595 }
2596# endif
2597}
2598#endif
2599
2600
2601/**
2602 * Sets dr1.
2603 *
2604 * @param uDRVal Debug register value to write
2605 */
2606#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
2607DECLASM(void) ASMSetDR1(RTCCUINTXREG uDRVal);
2608#else
2609DECLINLINE(void) ASMSetDR1(RTCCUINTXREG uDRVal)
2610{
2611# if RT_INLINE_ASM_USES_INTRIN
2612 __writedr(1, uDRVal);
2613# elif RT_INLINE_ASM_GNU_STYLE
2614# ifdef RT_ARCH_AMD64
2615 __asm__ __volatile__("movq %0, %%dr1\n\t" : : "r" (uDRVal));
2616# else
2617 __asm__ __volatile__("movl %0, %%dr1\n\t" : : "r" (uDRVal));
2618# endif
2619# else
2620 __asm
2621 {
2622# ifdef RT_ARCH_AMD64
2623 mov rax, [uDRVal]
2624 mov dr1, rax
2625# else
2626 mov eax, [uDRVal]
2627 mov dr1, eax
2628# endif
2629 }
2630# endif
2631}
2632#endif
2633
2634
2635/**
2636 * Sets dr2.
2637 *
2638 * @param uDRVal Debug register value to write
2639 */
2640#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
2641DECLASM(void) ASMSetDR2(RTCCUINTXREG uDRVal);
2642#else
2643DECLINLINE(void) ASMSetDR2(RTCCUINTXREG uDRVal)
2644{
2645# if RT_INLINE_ASM_USES_INTRIN
2646 __writedr(2, uDRVal);
2647# elif RT_INLINE_ASM_GNU_STYLE
2648# ifdef RT_ARCH_AMD64
2649 __asm__ __volatile__("movq %0, %%dr2\n\t" : : "r" (uDRVal));
2650# else
2651 __asm__ __volatile__("movl %0, %%dr2\n\t" : : "r" (uDRVal));
2652# endif
2653# else
2654 __asm
2655 {
2656# ifdef RT_ARCH_AMD64
2657 mov rax, [uDRVal]
2658 mov dr2, rax
2659# else
2660 mov eax, [uDRVal]
2661 mov dr2, eax
2662# endif
2663 }
2664# endif
2665}
2666#endif
2667
2668
2669/**
2670 * Sets dr3.
2671 *
2672 * @param uDRVal Debug register value to write
2673 */
2674#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
2675DECLASM(void) ASMSetDR3(RTCCUINTXREG uDRVal);
2676#else
2677DECLINLINE(void) ASMSetDR3(RTCCUINTXREG uDRVal)
2678{
2679# if RT_INLINE_ASM_USES_INTRIN
2680 __writedr(3, uDRVal);
2681# elif RT_INLINE_ASM_GNU_STYLE
2682# ifdef RT_ARCH_AMD64
2683 __asm__ __volatile__("movq %0, %%dr3\n\t" : : "r" (uDRVal));
2684# else
2685 __asm__ __volatile__("movl %0, %%dr3\n\t" : : "r" (uDRVal));
2686# endif
2687# else
2688 __asm
2689 {
2690# ifdef RT_ARCH_AMD64
2691 mov rax, [uDRVal]
2692 mov dr3, rax
2693# else
2694 mov eax, [uDRVal]
2695 mov dr3, eax
2696# endif
2697 }
2698# endif
2699}
2700#endif
2701
2702
2703/**
2704 * Sets dr6.
2705 *
2706 * @param uDRVal Debug register value to write
2707 */
2708#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
2709DECLASM(void) ASMSetDR6(RTCCUINTXREG uDRVal);
2710#else
2711DECLINLINE(void) ASMSetDR6(RTCCUINTXREG uDRVal)
2712{
2713# if RT_INLINE_ASM_USES_INTRIN
2714 __writedr(6, uDRVal);
2715# elif RT_INLINE_ASM_GNU_STYLE
2716# ifdef RT_ARCH_AMD64
2717 __asm__ __volatile__("movq %0, %%dr6\n\t" : : "r" (uDRVal));
2718# else
2719 __asm__ __volatile__("movl %0, %%dr6\n\t" : : "r" (uDRVal));
2720# endif
2721# else
2722 __asm
2723 {
2724# ifdef RT_ARCH_AMD64
2725 mov rax, [uDRVal]
2726 mov dr6, rax
2727# else
2728 mov eax, [uDRVal]
2729 mov dr6, eax
2730# endif
2731 }
2732# endif
2733}
2734#endif
2735
2736
2737/**
2738 * Sets dr7.
2739 *
2740 * @param uDRVal Debug register value to write
2741 */
2742#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
2743DECLASM(void) ASMSetDR7(RTCCUINTXREG uDRVal);
2744#else
2745DECLINLINE(void) ASMSetDR7(RTCCUINTXREG uDRVal)
2746{
2747# if RT_INLINE_ASM_USES_INTRIN
2748 __writedr(7, uDRVal);
2749# elif RT_INLINE_ASM_GNU_STYLE
2750# ifdef RT_ARCH_AMD64
2751 __asm__ __volatile__("movq %0, %%dr7\n\t" : : "r" (uDRVal));
2752# else
2753 __asm__ __volatile__("movl %0, %%dr7\n\t" : : "r" (uDRVal));
2754# endif
2755# else
2756 __asm
2757 {
2758# ifdef RT_ARCH_AMD64
2759 mov rax, [uDRVal]
2760 mov dr7, rax
2761# else
2762 mov eax, [uDRVal]
2763 mov dr7, eax
2764# endif
2765 }
2766# endif
2767}
2768#endif
2769
2770
2771/**
2772 * Writes a 8-bit unsigned integer to an I/O port, ordered.
2773 *
2774 * @param Port I/O port to write to.
2775 * @param u8 8-bit integer to write.
2776 */
2777#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
2778DECLASM(void) ASMOutU8(RTIOPORT Port, uint8_t u8);
2779#else
2780DECLINLINE(void) ASMOutU8(RTIOPORT Port, uint8_t u8)
2781{
2782# if RT_INLINE_ASM_GNU_STYLE
2783 __asm__ __volatile__("outb %b1, %w0\n\t"
2784 :: "Nd" (Port),
2785 "a" (u8));
2786
2787# elif RT_INLINE_ASM_USES_INTRIN
2788 __outbyte(Port, u8);
2789
2790# else
2791 __asm
2792 {
2793 mov dx, [Port]
2794 mov al, [u8]
2795 out dx, al
2796 }
2797# endif
2798}
2799#endif
2800
2801
2802/**
2803 * Reads a 8-bit unsigned integer from an I/O port, ordered.
2804 *
2805 * @returns 8-bit integer.
2806 * @param Port I/O port to read from.
2807 */
2808#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
2809DECLASM(uint8_t) ASMInU8(RTIOPORT Port);
2810#else
2811DECLINLINE(uint8_t) ASMInU8(RTIOPORT Port)
2812{
2813 uint8_t u8;
2814# if RT_INLINE_ASM_GNU_STYLE
2815 __asm__ __volatile__("inb %w1, %b0\n\t"
2816 : "=a" (u8)
2817 : "Nd" (Port));
2818
2819# elif RT_INLINE_ASM_USES_INTRIN
2820 u8 = __inbyte(Port);
2821
2822# else
2823 __asm
2824 {
2825 mov dx, [Port]
2826 in al, dx
2827 mov [u8], al
2828 }
2829# endif
2830 return u8;
2831}
2832#endif
2833
2834
2835/**
2836 * Writes a 16-bit unsigned integer to an I/O port, ordered.
2837 *
2838 * @param Port I/O port to write to.
2839 * @param u16 16-bit integer to write.
2840 */
2841#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
2842DECLASM(void) ASMOutU16(RTIOPORT Port, uint16_t u16);
2843#else
2844DECLINLINE(void) ASMOutU16(RTIOPORT Port, uint16_t u16)
2845{
2846# if RT_INLINE_ASM_GNU_STYLE
2847 __asm__ __volatile__("outw %w1, %w0\n\t"
2848 :: "Nd" (Port),
2849 "a" (u16));
2850
2851# elif RT_INLINE_ASM_USES_INTRIN
2852 __outword(Port, u16);
2853
2854# else
2855 __asm
2856 {
2857 mov dx, [Port]
2858 mov ax, [u16]
2859 out dx, ax
2860 }
2861# endif
2862}
2863#endif
2864
2865
2866/**
2867 * Reads a 16-bit unsigned integer from an I/O port, ordered.
2868 *
2869 * @returns 16-bit integer.
2870 * @param Port I/O port to read from.
2871 */
2872#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
2873DECLASM(uint16_t) ASMInU16(RTIOPORT Port);
2874#else
2875DECLINLINE(uint16_t) ASMInU16(RTIOPORT Port)
2876{
2877 uint16_t u16;
2878# if RT_INLINE_ASM_GNU_STYLE
2879 __asm__ __volatile__("inw %w1, %w0\n\t"
2880 : "=a" (u16)
2881 : "Nd" (Port));
2882
2883# elif RT_INLINE_ASM_USES_INTRIN
2884 u16 = __inword(Port);
2885
2886# else
2887 __asm
2888 {
2889 mov dx, [Port]
2890 in ax, dx
2891 mov [u16], ax
2892 }
2893# endif
2894 return u16;
2895}
2896#endif
2897
2898
2899/**
2900 * Writes a 32-bit unsigned integer to an I/O port, ordered.
2901 *
2902 * @param Port I/O port to write to.
2903 * @param u32 32-bit integer to write.
2904 */
2905#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
2906DECLASM(void) ASMOutU32(RTIOPORT Port, uint32_t u32);
2907#else
2908DECLINLINE(void) ASMOutU32(RTIOPORT Port, uint32_t u32)
2909{
2910# if RT_INLINE_ASM_GNU_STYLE
2911 __asm__ __volatile__("outl %1, %w0\n\t"
2912 :: "Nd" (Port),
2913 "a" (u32));
2914
2915# elif RT_INLINE_ASM_USES_INTRIN
2916 __outdword(Port, u32);
2917
2918# else
2919 __asm
2920 {
2921 mov dx, [Port]
2922 mov eax, [u32]
2923 out dx, eax
2924 }
2925# endif
2926}
2927#endif
2928
2929
2930/**
2931 * Reads a 32-bit unsigned integer from an I/O port, ordered.
2932 *
2933 * @returns 32-bit integer.
2934 * @param Port I/O port to read from.
2935 */
2936#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
2937DECLASM(uint32_t) ASMInU32(RTIOPORT Port);
2938#else
2939DECLINLINE(uint32_t) ASMInU32(RTIOPORT Port)
2940{
2941 uint32_t u32;
2942# if RT_INLINE_ASM_GNU_STYLE
2943 __asm__ __volatile__("inl %w1, %0\n\t"
2944 : "=a" (u32)
2945 : "Nd" (Port));
2946
2947# elif RT_INLINE_ASM_USES_INTRIN
2948 u32 = __indword(Port);
2949
2950# else
2951 __asm
2952 {
2953 mov dx, [Port]
2954 in eax, dx
2955 mov [u32], eax
2956 }
2957# endif
2958 return u32;
2959}
2960#endif
2961
2962
2963/**
2964 * Writes a string of 8-bit unsigned integer items to an I/O port, ordered.
2965 *
2966 * @param Port I/O port to write to.
2967 * @param pau8 Pointer to the string buffer.
2968 * @param c The number of items to write.
2969 */
2970#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
2971DECLASM(void) ASMOutStrU8(RTIOPORT Port, uint8_t const RT_FAR *pau8, size_t c);
2972#else
2973DECLINLINE(void) ASMOutStrU8(RTIOPORT Port, uint8_t const RT_FAR *pau8, size_t c)
2974{
2975# if RT_INLINE_ASM_GNU_STYLE
2976 __asm__ __volatile__("rep; outsb\n\t"
2977 : "+S" (pau8),
2978 "+c" (c)
2979 : "d" (Port));
2980
2981# elif RT_INLINE_ASM_USES_INTRIN
2982 __outbytestring(Port, (unsigned char RT_FAR *)pau8, (unsigned long)c);
2983
2984# else
2985 __asm
2986 {
2987 mov dx, [Port]
2988 mov ecx, [c]
2989 mov eax, [pau8]
2990 xchg esi, eax
2991 rep outsb
2992 xchg esi, eax
2993 }
2994# endif
2995}
2996#endif
2997
2998
2999/**
3000 * Reads a string of 8-bit unsigned integer items from an I/O port, ordered.
3001 *
3002 * @param Port I/O port to read from.
3003 * @param pau8 Pointer to the string buffer (output).
3004 * @param c The number of items to read.
3005 */
3006#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
3007DECLASM(void) ASMInStrU8(RTIOPORT Port, uint8_t RT_FAR *pau8, size_t c);
3008#else
3009DECLINLINE(void) ASMInStrU8(RTIOPORT Port, uint8_t RT_FAR *pau8, size_t c)
3010{
3011# if RT_INLINE_ASM_GNU_STYLE
3012 __asm__ __volatile__("rep; insb\n\t"
3013 : "+D" (pau8),
3014 "+c" (c)
3015 : "d" (Port));
3016
3017# elif RT_INLINE_ASM_USES_INTRIN
3018 __inbytestring(Port, pau8, (unsigned long)c);
3019
3020# else
3021 __asm
3022 {
3023 mov dx, [Port]
3024 mov ecx, [c]
3025 mov eax, [pau8]
3026 xchg edi, eax
3027 rep insb
3028 xchg edi, eax
3029 }
3030# endif
3031}
3032#endif
3033
3034
3035/**
3036 * Writes a string of 16-bit unsigned integer items to an I/O port, ordered.
3037 *
3038 * @param Port I/O port to write to.
3039 * @param pau16 Pointer to the string buffer.
3040 * @param c The number of items to write.
3041 */
3042#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
3043DECLASM(void) ASMOutStrU16(RTIOPORT Port, uint16_t const RT_FAR *pau16, size_t c);
3044#else
3045DECLINLINE(void) ASMOutStrU16(RTIOPORT Port, uint16_t const RT_FAR *pau16, size_t c)
3046{
3047# if RT_INLINE_ASM_GNU_STYLE
3048 __asm__ __volatile__("rep; outsw\n\t"
3049 : "+S" (pau16),
3050 "+c" (c)
3051 : "d" (Port));
3052
3053# elif RT_INLINE_ASM_USES_INTRIN
3054 __outwordstring(Port, (unsigned short RT_FAR *)pau16, (unsigned long)c);
3055
3056# else
3057 __asm
3058 {
3059 mov dx, [Port]
3060 mov ecx, [c]
3061 mov eax, [pau16]
3062 xchg esi, eax
3063 rep outsw
3064 xchg esi, eax
3065 }
3066# endif
3067}
3068#endif
3069
3070
3071/**
3072 * Reads a string of 16-bit unsigned integer items from an I/O port, ordered.
3073 *
3074 * @param Port I/O port to read from.
3075 * @param pau16 Pointer to the string buffer (output).
3076 * @param c The number of items to read.
3077 */
3078#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
3079DECLASM(void) ASMInStrU16(RTIOPORT Port, uint16_t RT_FAR *pau16, size_t c);
3080#else
3081DECLINLINE(void) ASMInStrU16(RTIOPORT Port, uint16_t RT_FAR *pau16, size_t c)
3082{
3083# if RT_INLINE_ASM_GNU_STYLE
3084 __asm__ __volatile__("rep; insw\n\t"
3085 : "+D" (pau16),
3086 "+c" (c)
3087 : "d" (Port));
3088
3089# elif RT_INLINE_ASM_USES_INTRIN
3090 __inwordstring(Port, pau16, (unsigned long)c);
3091
3092# else
3093 __asm
3094 {
3095 mov dx, [Port]
3096 mov ecx, [c]
3097 mov eax, [pau16]
3098 xchg edi, eax
3099 rep insw
3100 xchg edi, eax
3101 }
3102# endif
3103}
3104#endif
3105
3106
3107/**
3108 * Writes a string of 32-bit unsigned integer items to an I/O port, ordered.
3109 *
3110 * @param Port I/O port to write to.
3111 * @param pau32 Pointer to the string buffer.
3112 * @param c The number of items to write.
3113 */
3114#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
3115DECLASM(void) ASMOutStrU32(RTIOPORT Port, uint32_t const RT_FAR *pau32, size_t c);
3116#else
3117DECLINLINE(void) ASMOutStrU32(RTIOPORT Port, uint32_t const RT_FAR *pau32, size_t c)
3118{
3119# if RT_INLINE_ASM_GNU_STYLE
3120 __asm__ __volatile__("rep; outsl\n\t"
3121 : "+S" (pau32),
3122 "+c" (c)
3123 : "d" (Port));
3124
3125# elif RT_INLINE_ASM_USES_INTRIN
3126 __outdwordstring(Port, (unsigned long RT_FAR *)pau32, (unsigned long)c);
3127
3128# else
3129 __asm
3130 {
3131 mov dx, [Port]
3132 mov ecx, [c]
3133 mov eax, [pau32]
3134 xchg esi, eax
3135 rep outsd
3136 xchg esi, eax
3137 }
3138# endif
3139}
3140#endif
3141
3142
3143/**
3144 * Reads a string of 32-bit unsigned integer items from an I/O port, ordered.
3145 *
3146 * @param Port I/O port to read from.
3147 * @param pau32 Pointer to the string buffer (output).
3148 * @param c The number of items to read.
3149 */
3150#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
3151DECLASM(void) ASMInStrU32(RTIOPORT Port, uint32_t RT_FAR *pau32, size_t c);
3152#else
3153DECLINLINE(void) ASMInStrU32(RTIOPORT Port, uint32_t RT_FAR *pau32, size_t c)
3154{
3155# if RT_INLINE_ASM_GNU_STYLE
3156 __asm__ __volatile__("rep; insl\n\t"
3157 : "+D" (pau32),
3158 "+c" (c)
3159 : "d" (Port));
3160
3161# elif RT_INLINE_ASM_USES_INTRIN
3162 __indwordstring(Port, (unsigned long RT_FAR *)pau32, (unsigned long)c);
3163
3164# else
3165 __asm
3166 {
3167 mov dx, [Port]
3168 mov ecx, [c]
3169 mov eax, [pau32]
3170 xchg edi, eax
3171 rep insd
3172 xchg edi, eax
3173 }
3174# endif
3175}
3176#endif
3177
3178
3179/**
3180 * Invalidate page.
3181 *
3182 * @param uPtr Address of the page to invalidate.
3183 */
3184#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
3185DECLASM(void) ASMInvalidatePage(RTCCUINTXREG uPtr);
3186#else
3187DECLINLINE(void) ASMInvalidatePage(RTCCUINTXREG uPtr)
3188{
3189# if RT_INLINE_ASM_USES_INTRIN
3190 __invlpg((void RT_FAR *)uPtr);
3191
3192# elif RT_INLINE_ASM_GNU_STYLE
3193 __asm__ __volatile__("invlpg %0\n\t"
3194 : : "m" (*(uint8_t RT_FAR *)(uintptr_t)uPtr));
3195# else
3196 __asm
3197 {
3198# ifdef RT_ARCH_AMD64
3199 mov rax, [uPtr]
3200 invlpg [rax]
3201# else
3202 mov eax, [uPtr]
3203 invlpg [eax]
3204# endif
3205 }
3206# endif
3207}
3208#endif
3209
3210
3211/**
3212 * Write back the internal caches and invalidate them.
3213 */
3214#if RT_INLINE_ASM_EXTERNAL && !RT_INLINE_ASM_USES_INTRIN
3215DECLASM(void) ASMWriteBackAndInvalidateCaches(void);
3216#else
3217DECLINLINE(void) ASMWriteBackAndInvalidateCaches(void)
3218{
3219# if RT_INLINE_ASM_USES_INTRIN
3220 __wbinvd();
3221
3222# elif RT_INLINE_ASM_GNU_STYLE
3223 __asm__ __volatile__("wbinvd");
3224# else
3225 __asm
3226 {
3227 wbinvd
3228 }
3229# endif
3230}
3231#endif
3232
3233
3234/**
3235 * Invalidate internal and (perhaps) external caches without first
3236 * flushing dirty cache lines. Use with extreme care.
3237 */
3238#if RT_INLINE_ASM_EXTERNAL
3239DECLASM(void) ASMInvalidateInternalCaches(void);
3240#else
3241DECLINLINE(void) ASMInvalidateInternalCaches(void)
3242{
3243# if RT_INLINE_ASM_GNU_STYLE
3244 __asm__ __volatile__("invd");
3245# else
3246 __asm
3247 {
3248 invd
3249 }
3250# endif
3251}
3252#endif
3253
3254
3255/**
3256 * Memory load/store fence, waits for any pending writes and reads to complete.
3257 * Requires the X86_CPUID_FEATURE_EDX_SSE2 CPUID bit set.
3258 */
3259DECLINLINE(void) ASMMemoryFenceSSE2(void)
3260{
3261#if RT_INLINE_ASM_GNU_STYLE
3262 __asm__ __volatile__ (".byte 0x0f,0xae,0xf0\n\t");
3263#elif RT_INLINE_ASM_USES_INTRIN
3264 _mm_mfence();
3265#else
3266 __asm
3267 {
3268 _emit 0x0f
3269 _emit 0xae
3270 _emit 0xf0
3271 }
3272#endif
3273}
3274
3275
3276/**
3277 * Memory store fence, waits for any writes to complete.
3278 * Requires the X86_CPUID_FEATURE_EDX_SSE CPUID bit set.
3279 */
3280DECLINLINE(void) ASMWriteFenceSSE(void)
3281{
3282#if RT_INLINE_ASM_GNU_STYLE
3283 __asm__ __volatile__ (".byte 0x0f,0xae,0xf8\n\t");
3284#elif RT_INLINE_ASM_USES_INTRIN
3285 _mm_sfence();
3286#else
3287 __asm
3288 {
3289 _emit 0x0f
3290 _emit 0xae
3291 _emit 0xf8
3292 }
3293#endif
3294}
3295
3296
3297/**
3298 * Memory load fence, waits for any pending reads to complete.
3299 * Requires the X86_CPUID_FEATURE_EDX_SSE2 CPUID bit set.
3300 */
3301DECLINLINE(void) ASMReadFenceSSE2(void)
3302{
3303#if RT_INLINE_ASM_GNU_STYLE
3304 __asm__ __volatile__ (".byte 0x0f,0xae,0xe8\n\t");
3305#elif RT_INLINE_ASM_USES_INTRIN
3306 _mm_lfence();
3307#else
3308 __asm
3309 {
3310 _emit 0x0f
3311 _emit 0xae
3312 _emit 0xe8
3313 }
3314#endif
3315}
3316
3317#if !defined(_MSC_VER) || !defined(RT_ARCH_AMD64)
3318
3319/*
3320 * Clear the AC bit in the EFLAGS register.
3321 * Requires the X86_CPUID_STEXT_FEATURE_EBX_SMAP CPUID bit set.
3322 * Requires to be executed in R0.
3323 */
3324DECLINLINE(void) ASMClearAC(void)
3325{
3326#if RT_INLINE_ASM_GNU_STYLE
3327 __asm__ __volatile__ (".byte 0x0f,0x01,0xca\n\t");
3328#else
3329 __asm
3330 {
3331 _emit 0x0f
3332 _emit 0x01
3333 _emit 0xca
3334 }
3335#endif
3336}
3337
3338
3339/*
3340 * Set the AC bit in the EFLAGS register.
3341 * Requires the X86_CPUID_STEXT_FEATURE_EBX_SMAP CPUID bit set.
3342 * Requires to be executed in R0.
3343 */
3344DECLINLINE(void) ASMSetAC(void)
3345{
3346#if RT_INLINE_ASM_GNU_STYLE
3347 __asm__ __volatile__ (".byte 0x0f,0x01,0xcb\n\t");
3348#else
3349 __asm
3350 {
3351 _emit 0x0f
3352 _emit 0x01
3353 _emit 0xcb
3354 }
3355#endif
3356}
3357
3358#endif /* !_MSC_VER) || !RT_ARCH_AMD64 */
3359
3360/** @} */
3361#endif
3362
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