-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib.h
150 lines (134 loc) · 4.27 KB
/
lib.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
/* lib.h - Defines for useful library functions
* vim:ts=4 noexpandtab
*/
#ifndef _LIB_H
#define _LIB_H
#include "types.h"
int32_t printf(int8_t *format, ...);
void putc(uint8_t c, int type);
int32_t puts(int8_t *s, int type);
int8_t *itoa(uint32_t value, int8_t* buf, int32_t radix);
int8_t *strrev(int8_t* s);
uint32_t strlen(const int8_t* s);
void clear(void);
extern void update_cursor(int row, int col);
extern void mouse_putc(int x, int y);
extern void mouse_erase(int x, int y);
void* memset(void* s, int32_t c, uint32_t n);
void* memset_word(void* s, int32_t c, uint32_t n);
void* memset_dword(void* s, int32_t c, uint32_t n);
void* memcpy(void* dest, const void* src, uint32_t n);
void* memmove(void* dest, const void* src, uint32_t n);
int32_t strncmp(const int8_t* s1, const int8_t* s2, uint32_t n);
int8_t* strcpy(int8_t* dest, const int8_t*src);
int8_t* strncpy(int8_t* dest, const int8_t*src, uint32_t n);
/* Userspace address-check functions */
int32_t bad_userspace_addr(const void* addr, int32_t len);
int32_t safe_strncpy(int8_t* dest, const int8_t* src, int32_t n);
void print_backspace(void);
void test_interrupts(void);
/* Port read functions */
/* Inb reads a byte and returns its value as a zero-extended 32-bit
* unsigned int */
static inline uint32_t inb(port)
{
uint32_t val;
asm volatile("xorl %0, %0\n \
inb (%w1), %b0"
: "=a"(val)
: "d"(port)
: "memory" );
return val;
}
/* Reads two bytes from two consecutive ports, starting at "port",
* concatenates them little-endian style, and returns them zero-extended
* */
static inline uint32_t inw(port)
{
uint32_t val;
asm volatile("xorl %0, %0\n \
inw (%w1), %w0"
: "=a"(val)
: "d"(port)
: "memory" );
return val;
}
/* Reads four bytes from four consecutive ports, starting at "port",
* concatenates them little-endian style, and returns them */
static inline uint32_t inl(port)
{
uint32_t val;
asm volatile("inl (%w1), %0"
: "=a"(val)
: "d"(port)
: "memory" );
return val;
}
/* Writes a byte to a port */
#define outb(data, port) \
do { \
asm volatile("outb %b1, (%w0)" \
: \
: "d" (port), "a" (data) \
: "memory", "cc" ); \
} while(0)
/* Writes two bytes to two consecutive ports */
#define outw(data, port) \
do { \
asm volatile("outw %w1, (%w0)" \
: \
: "d" (port), "a" (data) \
: "memory", "cc" ); \
} while(0)
/* Writes four bytes to four consecutive ports */
#define outl(data, port) \
do { \
asm volatile("outl %l1, (%w0)" \
: \
: "d" (port), "a" (data) \
: "memory", "cc" ); \
} while(0)
/* Clear interrupt flag - disables interrupts on this processor */
#define cli() \
do { \
asm volatile("cli" \
: \
: \
: "memory", "cc" \
); \
} while(0)
/* Save flags and then clear interrupt flag
* Saves the EFLAGS register into the variable "flags", and then
* disables interrupts on this processor */
#define cli_and_save(flags) \
do { \
asm volatile("pushfl \n \
popl %0 \n \
cli" \
: "=r"(flags) \
: \
: "memory", "cc" \
); \
} while(0)
/* Set interrupt flag - enable interrupts on this processor */
#define sti() \
do { \
asm volatile("sti" \
: \
: \
: "memory", "cc" \
); \
} while(0)
/* Restore flags
* Puts the value in "flags" into the EFLAGS register. Most often used
* after a cli_and_save_flags(flags) */
#define restore_flags(flags) \
do { \
asm volatile("pushl %0 \n \
popfl" \
: \
: "r"(flags) \
: "memory", "cc" \
); \
} while(0)
#endif /* _LIB_H */