Add more intrinsic routines

This commit is contained in:
Rafal Kupiec 2023-01-13 22:32:45 +01:00
parent bff460a879
commit fb60625abc
Signed by: belliash
GPG Key ID: 4E829243E0CFE6B4
6 changed files with 309 additions and 39 deletions

View File

@ -75,6 +75,23 @@ XTCDECL
VOID VOID
HlSetInterruptFlag(); HlSetInterruptFlag();
XTCDECL
VOID
HlStoreGlobalDescriptorTable(OUT PVOID Destination);
XTCDECL
VOID
HlStoreInterruptDescriptorTable(OUT PVOID Destination);
XTCDECL
VOID
HlStoreSegment(IN USHORT Segment,
OUT PVOID Destination);
XTCDECL
VOID
HlStoreTaskRegister(OUT PVOID Destination);
XTCDECL XTCDECL
VOID VOID
HlWriteControlRegister(IN USHORT ControlRegister, HlWriteControlRegister(IN USHORT ControlRegister,

View File

@ -49,6 +49,14 @@
#define CR4_XSAVE 0x00020000 #define CR4_XSAVE 0x00020000
#define CR4_RESERVED3 0xFFFC0000 #define CR4_RESERVED3 0xFFFC0000
/* Segment defintions */
#define SEGMENT_CS 0x2E
#define SEGMENT_DS 0x3E
#define SEGMENT_ES 0x26
#define SEGMENT_SS 0x36
#define SEGMENT_FS 0x64
#define SEGMENT_GS 0x65
/* CPUID features enumeration list */ /* CPUID features enumeration list */
typedef enum _CPUID_FEATURES typedef enum _CPUID_FEATURES
{ {

View File

@ -75,6 +75,23 @@ XTCDECL
VOID VOID
HlSetInterruptFlag(); HlSetInterruptFlag();
XTCDECL
VOID
HlStoreGlobalDescriptorTable(OUT PVOID Destination);
XTCDECL
VOID
HlStoreInterruptDescriptorTable(OUT PVOID Destination);
XTCDECL
VOID
HlStoreSegment(IN USHORT Segment,
OUT PVOID Destination);
XTCDECL
VOID
HlStoreTaskRegister(OUT PVOID Destination);
XTCDECL XTCDECL
VOID VOID
HlWriteControlRegister(IN USHORT ControlRegister, HlWriteControlRegister(IN USHORT ControlRegister,

View File

@ -49,6 +49,14 @@
#define CR4_XSAVE 0x00020000 #define CR4_XSAVE 0x00020000
#define CR4_RESERVED3 0xFFFC0000 #define CR4_RESERVED3 0xFFFC0000
/* Segment defintions */
#define SEGMENT_CS 0x2E
#define SEGMENT_DS 0x3E
#define SEGMENT_ES 0x26
#define SEGMENT_SS 0x36
#define SEGMENT_FS 0x64
#define SEGMENT_GS 0x65
/* CPUID features enumeration list */ /* CPUID features enumeration list */
typedef enum _CPUID_FEATURES typedef enum _CPUID_FEATURES
{ {

View File

@ -97,7 +97,7 @@ HlInvalidateTlbEntry(IN PVOID Address)
{ {
asm volatile("invlpg (%0)" asm volatile("invlpg (%0)"
: :
: "b"(Address) : "b" (Address)
: "memory"); : "memory");
} }
@ -117,8 +117,8 @@ HlIoPortInByte(IN USHORT Port)
{ {
UCHAR Value; UCHAR Value;
asm volatile("inb %1, %0" asm volatile("inb %1, %0"
: "=a"(Value) : "=a" (Value)
: "Nd"(Port)); : "Nd" (Port));
return Value; return Value;
} }
@ -138,8 +138,8 @@ HlIoPortInShort(IN USHORT Port)
{ {
USHORT Value; USHORT Value;
asm volatile("inw %1, %0" asm volatile("inw %1, %0"
: "=a"(Value) : "=a" (Value)
: "Nd"(Port)); : "Nd" (Port));
return Value; return Value;
} }
@ -159,8 +159,8 @@ HlIoPortInLong(IN USHORT Port)
{ {
ULONG Value; ULONG Value;
asm volatile("inl %1, %0" asm volatile("inl %1, %0"
: "=a"(Value) : "=a" (Value)
: "Nd"(Port)); : "Nd" (Port));
return Value; return Value;
} }
@ -184,8 +184,8 @@ HlIoPortOutByte(IN USHORT Port,
{ {
asm volatile("outb %0, %1" asm volatile("outb %0, %1"
: :
: "a"(Value), : "a" (Value),
"Nd"(Port)); "Nd" (Port));
} }
/** /**
@ -208,8 +208,8 @@ HlIoPortOutShort(IN USHORT Port,
{ {
asm volatile("outw %0, %1" asm volatile("outw %0, %1"
: :
: "a"(Value), : "a" (Value),
"Nd"(Port)); "Nd" (Port));
} }
/** /**
@ -232,8 +232,8 @@ HlIoPortOutLong(IN USHORT Port,
{ {
asm volatile("outl %0, %1" asm volatile("outl %0, %1"
: :
: "a"(Value), : "a" (Value),
"Nd"(Port)); "Nd" (Port));
} }
/** /**
@ -316,9 +316,9 @@ HlReadModelSpecificRegister(IN ULONG Register)
ULONG Low, High; ULONG Low, High;
asm volatile("rdmsr" asm volatile("rdmsr"
: "=a"(Low), : "=a" (Low),
"=d"(High) "=d" (High)
: "c"(Register)); : "c" (Register));
return ((ULONGLONG)High << 32) | Low; return ((ULONGLONG)High << 32) | Low;
} }
@ -337,8 +337,8 @@ HlReadTimeStampCounter()
ULONGLONG Low, High; ULONGLONG Low, High;
asm volatile("rdtsc" asm volatile("rdtsc"
: "=a"(Low), : "=a" (Low),
"=d"(High)); "=d" (High));
return ((ULONGLONG)High << 32) | Low; return ((ULONGLONG)High << 32) | Low;
} }
@ -357,6 +357,116 @@ HlSetInterruptFlag()
asm volatile("sti"); asm volatile("sti");
} }
/**
* Stores GDT register into the given memory area.
*
* @param Destination
* Supplies a pointer to the memory area where GDT will be stored.
*
* @return This routine does not return any value.
*
* @since XT 1.0
*/
XTCDECL
VOID
HlStoreGlobalDescriptorTable(OUT PVOID Destination)
{
asm volatile("sgdt %0"
:
: "m" (*(PSHORT)Destination)
: "memory");
}
/**
* Stores IDT register into the given memory area.
*
* @param Destination
* Supplies a pointer to the memory area where IDT will be stored.
*
* @return This routine does not return any value.
*
* @since XT 1.0
*/
XTCDECL
VOID
HlStoreInterruptDescriptorTable(OUT PVOID Destination)
{
asm volatile("sidt %0"
:
: "m" (*(PSHORT)Destination)
: "memory");
}
/**
* Stores specified segment into the given memory area.
*
* @param Segment
* Supplies a segment identification.
*
* @param Destination
* Supplies a pointer to the memory area where segment data will be stored.
*
* @return This routine does not return any value.
*
* @since XT 1.0
*/
XTCDECL
VOID
HlStoreSegment(IN USHORT Segment,
OUT PVOID Destination)
{
switch(Segment)
{
case SEGMENT_CS:
asm volatile("movl %%cs, %0"
: "=r" (*(PUINT)Destination));
break;
case SEGMENT_DS:
asm volatile("movl %%ds, %0"
: "=r" (*(PUINT)Destination));
break;
case SEGMENT_ES:
asm volatile("movl %%es, %0"
: "=r" (*(PUINT)Destination));
break;
case SEGMENT_FS:
asm volatile("movl %%fs, %0"
: "=r" (*(PUINT)Destination));
break;
case SEGMENT_GS:
asm volatile("movl %%gs, %0"
: "=r" (*(PUINT)Destination));
break;
case SEGMENT_SS:
asm volatile("movl %%ss, %0"
: "=r" (*(PUINT)Destination));
break;
default:
Destination = NULL;
break;
}
}
/**
* Stores TR into the given memory area.
*
* @param Destination
* Supplies a pointer to the memory area where TR will be stores.
*
* @return This routine does not return any value.
*
* @since XT 1.0
*/
XTCDECL
VOID
HlStoreTaskRegister(OUT PVOID Destination)
{
asm volatile("str %0"
:
: "m" (*(PULONG)Destination)
: "memory");
}
/** /**
* Writes a value to the specified CPU control register. * Writes a value to the specified CPU control register.
* *
@ -439,7 +549,7 @@ HlWriteModelSpecificRegister(IN ULONG Register,
asm volatile("wrmsr" asm volatile("wrmsr"
: :
: "c"(Register), : "c" (Register),
"a"(Low), "a" (Low),
"d"(High)); "d" (High));
} }

View File

@ -97,7 +97,7 @@ HlInvalidateTlbEntry(PVOID Address)
{ {
asm volatile("invlpg (%0)" asm volatile("invlpg (%0)"
: :
: "b"(Address) : "b" (Address)
: "memory"); : "memory");
} }
@ -117,8 +117,8 @@ HlIoPortInByte(IN USHORT Port)
{ {
UCHAR Value; UCHAR Value;
asm volatile("inb %1, %0" asm volatile("inb %1, %0"
: "=a"(Value) : "=a" (Value)
: "Nd"(Port)); : "Nd" (Port));
return Value; return Value;
} }
@ -138,8 +138,8 @@ HlIoPortInShort(IN USHORT Port)
{ {
USHORT Value; USHORT Value;
asm volatile("inw %1, %0" asm volatile("inw %1, %0"
: "=a"(Value) : "=a" (Value)
: "Nd"(Port)); : "Nd" (Port));
return Value; return Value;
} }
@ -159,8 +159,8 @@ HlIoPortInLong(IN USHORT Port)
{ {
ULONG Value; ULONG Value;
asm volatile("inl %1, %0" asm volatile("inl %1, %0"
: "=a"(Value) : "=a" (Value)
: "Nd"(Port)); : "Nd" (Port));
return Value; return Value;
} }
@ -184,8 +184,8 @@ HlIoPortOutByte(IN USHORT Port,
{ {
asm volatile("outb %0, %1" asm volatile("outb %0, %1"
: :
: "a"(Value), : "a" (Value),
"Nd"(Port)); "Nd" (Port));
} }
/** /**
@ -208,8 +208,8 @@ HlIoPortOutShort(IN USHORT Port,
{ {
asm volatile("outw %0, %1" asm volatile("outw %0, %1"
: :
: "a"(Value), : "a" (Value),
"Nd"(Port)); "Nd" (Port));
} }
/** /**
@ -232,8 +232,8 @@ HlIoPortOutLong(IN USHORT Port,
{ {
asm volatile("outl %0, %1" asm volatile("outl %0, %1"
: :
: "a"(Value), : "a" (Value),
"Nd"(Port)); "Nd" (Port));
} }
/** /**
@ -329,7 +329,7 @@ HlReadTimeStampCounter()
ULONGLONG Value; ULONGLONG Value;
asm volatile("rdtsc" asm volatile("rdtsc"
: "=A"(Value)); : "=A" (Value));
return Value; return Value;
} }
@ -348,6 +348,116 @@ HlSetInterruptFlag()
asm volatile("sti"); asm volatile("sti");
} }
/**
* Stores GDT register into the given memory area.
*
* @param Destination
* Supplies a pointer to the memory area where GDT will be stored.
*
* @return This routine does not return any value.
*
* @since XT 1.0
*/
XTCDECL
VOID
HlStoreGlobalDescriptorTable(OUT PVOID Destination)
{
asm volatile("sgdt %0"
:
: "m" (*(PSHORT)Destination)
: "memory");
}
/**
* Stores IDT register into the given memory area.
*
* @param Destination
* Supplies a pointer to the memory area where IDT will be stored.
*
* @return This routine does not return any value.
*
* @since XT 1.0
*/
XTCDECL
VOID
HlStoreInterruptDescriptorTable(OUT PVOID Destination)
{
asm volatile("sidt %0"
:
: "m" (*(PSHORT)Destination)
: "memory");
}
/**
* Stores specified segment into the given memory area.
*
* @param Segment
* Supplies a segment identification.
*
* @param Destination
* Supplies a pointer to the memory area where segment data will be stored.
*
* @return This routine does not return any value.
*
* @since XT 1.0
*/
XTCDECL
VOID
HlStoreSegment(IN USHORT Segment,
OUT PVOID Destination)
{
switch(Segment)
{
case SEGMENT_CS:
asm volatile("movl %%cs, %0"
: "=r" (*(PUINT)Destination));
break;
case SEGMENT_DS:
asm volatile("movl %%ds, %0"
: "=r" (*(PUINT)Destination));
break;
case SEGMENT_ES:
asm volatile("movl %%es, %0"
: "=r" (*(PUINT)Destination));
break;
case SEGMENT_FS:
asm volatile("movl %%fs, %0"
: "=r" (*(PUINT)Destination));
break;
case SEGMENT_GS:
asm volatile("movl %%gs, %0"
: "=r" (*(PUINT)Destination));
break;
case SEGMENT_SS:
asm volatile("movl %%ss, %0"
: "=r" (*(PUINT)Destination));
break;
default:
Destination = NULL;
break;
}
}
/**
* Stores TR into the given memory area.
*
* @param Destination
* Supplies a pointer to the memory area where TR will be stores.
*
* @return This routine does not return any value.
*
* @since XT 1.0
*/
XTCDECL
VOID
HlStoreTaskRegister(OUT PVOID Destination)
{
asm volatile("str %0"
:
: "m" (*(PULONG)Destination)
: "memory");
}
/** /**
* Writes a value to the specified CPU control register. * Writes a value to the specified CPU control register.
* *
@ -373,28 +483,28 @@ HlWriteControlRegister(IN USHORT ControlRegister,
/* Write value to CR0 */ /* Write value to CR0 */
asm volatile("mov %0, %%cr0" asm volatile("mov %0, %%cr0"
: :
: "r"(Value) : "r" (Value)
: "memory"); : "memory");
break; break;
case 2: case 2:
/* Write value to CR2 */ /* Write value to CR2 */
asm volatile("mov %0, %%cr2" asm volatile("mov %0, %%cr2"
: :
: "r"(Value) : "r" (Value)
: "memory"); : "memory");
break; break;
case 3: case 3:
/* Write value to CR3 */ /* Write value to CR3 */
asm volatile("mov %0, %%cr3" asm volatile("mov %0, %%cr3"
: :
: "r"(Value) : "r" (Value)
: "memory"); : "memory");
break; break;
case 4: case 4:
/* Write value to CR4 */ /* Write value to CR4 */
asm volatile("mov %0, %%cr4" asm volatile("mov %0, %%cr4"
: :
: "r"(Value) : "r" (Value)
: "memory"); : "memory");
break; break;
} }