-
Notifications
You must be signed in to change notification settings - Fork 3
/
x86.h
88 lines (71 loc) · 1.87 KB
/
x86.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#ifndef _X86_HARDWARE_H_
#define _X86_HARDWARE_H_
#include "sys/types.h"
#define DONT_EMIT extern inline __attribute__ ((gnu_inline, always_inline))
#define PACKED __attribute__((packed))
#define HTONL(n) (((((unsigned long)(n) & 0xFF)) << 24) | \
((((unsigned long)(n) & 0xFF00)) << 8) | \
((((unsigned long)(n) & 0xFF0000)) >> 8) | \
((((unsigned long)(n) & 0xFF000000)) >> 24))
#define NTOHL(n) (((((unsigned long)(n) & 0xFF)) << 24) | \
((((unsigned long)(n) & 0xFF00)) << 8) | \
((((unsigned long)(n) & 0xFF0000)) >> 8) | \
((((unsigned long)(n) & 0xFF000000)) >> 24))
// x86 hardware I/O primitives
// in a exception handler
struct registers {
u32 unused_eax, ecx, edx, ebx, esp, ebp, esi, edi;
u32 eax, eflags, eip, cs;
};
void dump_regs(const struct registers *regs);
DONT_EMIT void yield()
{
asm volatile ("hlt");
}
DONT_EMIT void halt()
{
while (1)
yield();
}
DONT_EMIT unsigned long long rdtsc(void) // read time-stamp counter
{
unsigned long long int x;
asm volatile (".byte 0x0f, 0x31" : "=A" (x));
return x;
}
DONT_EMIT void out8(unsigned short port, u8 val)
{
asm volatile( "outb %0, %1" : : "a"(val), "Nd"(port) );
}
DONT_EMIT u8
in8(unsigned int port)
{
u8 ret;
asm volatile ("inb %%dx,%%al":"=a" (ret):"d" (port));
return ret;
}
DONT_EMIT void
out16(unsigned short port, u16 val)
{
asm volatile( "outw %0, %1" : : "a"(val), "Nd"(port) );
}
DONT_EMIT u16
in16(unsigned int port)
{
u16 ret;
asm volatile ("inw %%dx,%%ax":"=a" (ret):"d" (port));
return ret;
}
DONT_EMIT void
out32(unsigned short port, u32 val)
{
asm volatile( "outl %0, %1" : : "a"(val), "Nd"(port) );
}
DONT_EMIT u32
in32(unsigned int port)
{
u32 ret;
asm volatile ("inl %%dx,%%eax":"=a" (ret):"d" (port));
return ret;
}
#endif