-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathx86.h
More file actions
131 lines (113 loc) · 2.27 KB
/
x86.h
File metadata and controls
131 lines (113 loc) · 2.27 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#ifndef INCLUDE_X86_H
#define INCLUDE_X86_H
#include "common.h"
// Layout of the trap frame built on the stack by the
// hardware and by trapasm.S, and passed to trap().
struct trapframe {
// registers as pushed by pusha - Pushes edi,esi,ebp,esp,ebx,edx,ecx,eax
uint32_t edi;
uint32_t esi;
uint32_t ebp;
uint32_t oesp; // useless & ignored
uint32_t ebx;
uint32_t edx;
uint32_t ecx;
uint32_t eax;
// rest of trap frame
uint16_t gs;
uint16_t padding1;
uint16_t fs;
uint16_t padding2;
uint16_t es;
uint16_t padding3;
uint16_t ds;
uint16_t padding4;
uint32_t trapno;
// below here defined by x86 hardware
uint32_t err;
uint32_t eip;
uint16_t cs;
uint16_t padding5;
uint32_t eflags;
// below here only when crossing rings, such as from user to kernel
uint32_t esp;
uint16_t ss;
uint16_t padding6;
};
static inline uint8_t
inb(uint16_t port)
{
uint8_t data;
asm volatile("in %1,%0" : "=a" (data) : "d" (port));
return data;
}
// load GDT
static inline void
lgdt(struct segdesc *p, int size)
{
volatile uint16_t pd[3];
pd[0] = size-1;
pd[1] = (uint32_t)p;
pd[2] = (uint32_t)p >> 16;
asm volatile("lgdt (%0)" : : "r" (pd));
}
// load task segment register
static inline void
ltr(uint16_t sel)
{
asm volatile("ltr %0" : : "r" (sel));
}
static inline uint32_t
readeflags(void)
{
uint32_t eflags;
asm volatile("pushfl; popl %0" : "=r" (eflags));
return eflags;
}
static inline void
cli(void)
{
asm volatile("cli");
}
static inline void
sti(void)
{
asm volatile("sti");
}
static inline uint32_t
xchg(volatile uint32_t *addr, uint32_t newval)
{
uint32_t result;
// The + in "+m" denotes a read-modify-write operand.
asm volatile("lock; xchgl %0, %1" :
"+m" (*addr), "=a" (result) :
"1" (newval) :
"cc");
return result;
}
static inline uint32_t
rcr2(void)
{
uint32_t val;
asm volatile("movl %%cr2,%0" : "=r" (val));
return val;
}
static inline void
lcr3(uint32_t val)
{
asm volatile("movl %0,%%cr3" : : "r" (val));
}
static inline void
enablepag(void)
{
uint32_t cr0;
asm volatile("mov %%cr0, %0": "=r"(cr0));
cr0 |= 0x80000000; // Enable paging!
asm volatile("mov %0, %%cr0":: "r"(cr0));
}
static inline void
bbrk(void)
{
asm volatile("xchg %bx, %bx");
}
#endif