summaryrefslogtreecommitdiff
path: root/asm/x86.s
blob: 97941114f2a0785f5196aea6028076bebeb464f1 (plain)
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
132
133
134
135
136
137
// Basic low-level x86 32-bit calls //

// reading and writing ports

.global x86_outb    // 8 bit out
.global x86_inb     // 8 bit in
.global x86_outw    // 16 bit out
.global x86_inw     // 16 bit in
.global x86_outl    // 32 bit out
.global x86_inl     // 32 bit in

// interrupts

.global x86_cli    // disable interrupts
.global x86_sti    // enable interrupts

// xchg
.global x86_xchg   // exchange (for semaphors etc.)

// invlpg
.global x86_invlpg // invalidate translation lookaside buffer (tlb)

// control registers
.global x86_get_cr
.global x86_set_cr

x86_outb:
    mov 4(%esp), %edx
    mov 8(%esp), %eax
    outb %al,%dx
    ret

x86_inb:
    mov 4(%esp), %edx
    inb %dx,%al
    ret

x86_outw:
    mov 4(%esp), %edx
    mov 8(%esp), %eax
    outw %ax,%dx
    ret

x86_inw:
    mov 4(%esp), %edx
    inw %dx,%ax
    ret

x86_outl:
    mov 4(%esp), %edx
    mov 8(%esp), %eax
    outl %eax,%dx
    ret

x86_inl:
    mov 4(%esp), %edx
    inl %dx,%eax
    ret

x86_cli:
    cli
    ret

x86_sti:
    sti
    ret

x86_xchg:
// --   uint8_t result;
// -- 
// --   // The + in "+m" denotes a read-modify-write operand.
// --   asm volatile("lock xchgb %0, %1" :
// --                "+m" (*addr), "=a" (result) :
// --                "1" (val) :
// --                "cc");
// -- 
// --   return result;
// -- } 
    ret

x86_get_cr:
    mov 4(%esp), %ecx
    cmp $0,%ecx
    je get_cr0
    cmp $2,%ecx
    je get_cr2
    cmp $3,%ecx
    je get_cr3
    cmp $4,%ecx
    je get_cr4
    ret

x86_set_cr:
    mov 4(%esp), %ecx
    mov 8(%esp), %eax
    cmp $0,%ecx
    je set_cr0
    cmp $2,%ecx
    je set_cr2
    cmp $3,%ecx
    je set_cr3
    cmp $4,%ecx
    je set_cr4
    ret

get_cr0:
    mov %cr0,%eax
    ret

get_cr2:
    mov %cr2,%eax
    ret

get_cr3:
    mov %cr3,%eax
    ret

get_cr4:
    mov %cr4,%eax
    ret

set_cr0:
    mov %eax,%cr0
    ret

set_cr2:
    mov %eax,%cr2
    ret

set_cr3:
    mov %eax,%cr3
    ret

set_cr4:
    mov %eax,%cr4
    ret