forked from derekalyne/small-linux-kernel-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.h
186 lines (165 loc) · 6.08 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* lib.h - Defines for useful library functions
* vim:ts=4 noexpandtab
*/
#ifndef _LIB_H
#define _LIB_H
#include "types.h"
#define VIDEO 0xB8000
#define NUM_COLS 80
#define STATUS_BAR_HEIGHT 1
#define NUM_ROWS 25
#define ATTRIB 0x7
#define TIME_BAR_WIDTH 10
int32_t printf(int8_t *format, ...);
void putc(uint8_t c);
void putc_multi(uint8_t c, uint8_t* ptr_vid, uint32_t terminal_idx);
void setc_multi(int x, int y, uint8_t c, uint8_t* ptr_vid);
uint8_t getc(int x, int y);
uint8_t getc_multi(int x, int y, uint8_t* ptr_vid);
int32_t puts(int8_t *s);
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);
void clear_multi(uint8_t* ptr_vid, uint32_t terminal_idx);
void set_screen_position(int x, int y);
void draw_status_bar(void);
void update_status_bar_time(uint8_t h, uint8_t m, uint8_t s);
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);
int8_t* strtrimlead(const int8_t* s);
int8_t* strtrimtail(const int8_t* s);
int8_t* strchr(const int8_t* s, const int8_t c);
/* Calculates log base 2 */
uint32_t log2(const uint32_t x);
// calculates max
int32_t max(const int32_t a, const int32_t b);
// calculates min
int32_t min(const int32_t a, const int32_t b);
/* 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);
/* Function: increments video memory. To be used to test rtc */
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 (" \n\
xorl %0, %0 \n\
inb (%w1), %b0 \n\
"
: "=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 (" \n\
xorl %0, %0 \n\
inw (%w1), %w0 \n\
"
: "=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 (" \n\
pushfl \n\
popl %0 \n\
cli \n\
" \
: "=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 (" \n\
pushl %0 \n\
popfl \n\
" \
: \
: "r"(flags) \
: "memory", "cc" \
); \
} while (0)
#endif /* _LIB_H */