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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
#include "paging.h"
#include "window.h"
#include "drive.h"
#include "panic.h"
#include "pmap.h"
#include "task.h"
#include "util.h"
#include "elf.h"
#include "idt.h"
#include "kbd.h"
#include "log.h"
enum {
IDT_PRESENT = 0x80,
IDT_INT = 0x0e,
};
struct idt_entry {
uint16_t addr_low;
uint16_t cs;
uint8_t zero;
uint8_t flags;
uint16_t addr_high;
} __attribute__ ((packed));
struct idt_entry idt[256];
struct {
uint16_t limit;
uint32_t start;
} __attribute__ ((packed)) idtr = {
.limit = 256 * sizeof(struct idt_entry) - 1,
.start = (uint32_t)idt
};
//file handles as (drive_number << 8) + file_id_t
uint32_t sc_open_file(uint32_t drive_number, char *path) { //not static to ensure sysv abi
return (drive_number << 8) + drives[drive_number].get_file(drives + drive_number, path);
//logf(LOG_INFO, "sc_open_file(%d, \"%s\") -> %d", drive_number, path, handle);
}
void sc_close_file(uint32_t handle) { //not static to ensure sysv abi
if (!handle)
return;
drives[handle >> 8].free_file(drives + (handle >> 8), handle & 0xff);
}
uint32_t sc_file_get_size(uint32_t handle) { //not static to ensure sysv abi
//logf(LOG_INFO, "sc_file_get_size(%d)", handle);
if (!handle)
return 0;
return drives[handle >> 8].get_file_length(drives + (handle >> 8), handle & 0xff);
}
uint32_t sc_file_read(uint32_t handle, uint32_t file_offset, uint32_t count, void *buffer) { //not static to ensure sysv abi
if (!handle)
return 0;
uint32_t len = sc_file_get_size(handle);
if (file_offset + count > len)
count = len - file_offset;
fmcpy(buffer, drives + (handle >> 8), handle & 0xff, file_offset, count);
return count;
}
uint32_t sc_start_task(uint32_t drive_number, char *path, const char *pass, uint32_t esi_dummy, uint32_t io_task) { //not static to ensure sysv abi
switch_to_kernel_cr3();
uint32_t process_id = try_elf_run(drives + drive_number, vma_to_pma(active_task->page_directory, path), pass, io_task);
switch_to_task_cr3();
return process_id;
}
void *sc_allocate_ram(uint32_t pages) { //not static to ensure sysv abi
return pd_user_allocate_anywhere_writable(active_task->page_directory, pages);
}
enum mi_arg {
MI_KERNEL_MAX,
MI_KERNEL_LEFT,
MI_USER_MAX,
MI_USER_LEFT,
MI_TASK_LEFT
};
__attribute__ ((pure))
uint32_t sc_memory_info(enum mi_arg arg) { //not static to ensure sysv abi
switch (arg) {
case MI_KERNEL_MAX:
return max_kernel_pages;
case MI_KERNEL_LEFT:
return kernel_pages_left;
case MI_USER_MAX:
return max_user_pages;
case MI_USER_LEFT:
return user_pages_left;
case MI_TASK_LEFT:
PANIC("TODO: memory info task left");
default:
return -1;
}
}
void sc_wait_for_task(uint32_t handle) { //not static to ensure sysv abi
add_wait((struct wait){.mode = PROCESS_END, .task = tasks + handle - 1});
}
uint32_t sc_enumerate_dir(uint32_t drive_number, const char *path, struct directory_content_info *buffer, uint32_t max_entries) { //not static to ensure sysv abi
return drives[drive_number].enumerate_dir(drives + drive_number, path, buffer, max_entries);
}
uint32_t sc_count_of_dir(uint32_t drive_number, const char *path) { //not static to ensure sysv abi
return drives[drive_number].n_dir_entries(drives + drive_number, path);
}
void sc_get_next_window_action(struct window *w, struct window_action *action) { //not static to ensure sysv abi
*action = next_window_action(w);
}
void sc_wait_window_action() {
add_wait((struct wait){.mode = WINDOW_ACTION});
}
void sc_wait_ipc(uint32_t task_handle) {
add_wait((struct wait){.mode = IPC_RECEIVE, .task = tasks + task_handle - 1});
}
void sc_system_log(const char *sz) {
logf(LOG_USER, "[%s] %s", active_task->name, sz);
}
void sc_wait_any_ipc() {
add_wait((struct wait){.mode = IPC_RECEIVE_ANY});
}
void sc_wait_ipc_read(uint32_t handle) {
add_wait((struct wait){.mode = IPC_SEND, .task = tasks + handle - 1});
}
__attribute__ ((pure))
bool sc_is_task_running(uint32_t handle) {
return tasks[handle - 1].page_directory;
}
void const *syscall_table[] = {
&sc_open_file,
&sc_close_file,
&sc_file_read,
&sc_file_get_size,
&sc_start_task,
&ipc_send,
&ipc_read,
&sc_allocate_ram,
&sc_memory_info,
&sc_wait_for_task,
&sc_enumerate_dir,
&sc_system_log,
&sc_count_of_dir,
&new_window,
&del_window,
&resize_window,
&reassign_pixel_buffer,
&push_window_paint,
&sc_get_next_window_action,
&sc_wait_window_action,
&sc_wait_ipc,
&sc_wait_any_ipc,
&find_unread_ipc,
&sc_wait_ipc_read,
&sc_is_task_running
};
//these aren't really void ()'s, but gcc complains if we take an address of a void, so we give it a type
typedef void isr_t();
extern isr_t syscall_isr;
extern isr_t quit_isr;
extern isr_t yield_isr;
extern isr_t kbd_isr;
extern isr_t udf_isr;
extern isr_t dfa_isr;
extern isr_t tsf_isr;
extern isr_t npf_isr;
extern isr_t ssf_isr;
extern isr_t gpf_isr;
extern isr_t pff_isr;
enum {
F_ID = 0x00200000,
F_VIP = 0x00100000,
F_VIF = 0x00080000,
F_AC = 0x00040000,
F_VM = 0x00020000,
F_RF = 0x00010000,
F_NT = 0x00004000,
F_OF = 0x00000800,
F_DF = 0x00000400,
F_IF = 0x00000200,
F_TF = 0x00000100,
F_SF = 0x00000080,
F_ZF = 0x00000040,
F_AF = 0x00000010,
F_PF = 0x00000004,
F_CF = 0x00000001,
F_IOPL_MASK = 0x00003000
};
__attribute__ ((noreturn))
void exception_halt(uint32_t eax, uint32_t ebx, uint32_t ecx,
uint32_t edx, uint32_t esi, uint32_t edi,
const char *id, uint32_t code,
uint32_t eip, uint32_t cs, uint32_t eflags,
uint32_t esp, uint32_t ss) {
if (code)
logf(LOG_ERROR, "Exception #%s (0x%h) in %s", id, code, active_task->name);
else
logf(LOG_ERROR, "Exception #%s in %s:", id, active_task->name);
logf(LOG_ERROR, " cs: 0x%hb ss: 0x%hb", cs, ss);
logf(LOG_ERROR, " eip: 0x%h esp: 0x%h", eip, esp);
logf(LOG_ERROR, " eax: 0x%h ebx: 0x%h", eax, ebx);
logf(LOG_ERROR, " ecx: 0x%h edx: 0x%h", ecx, edx);
logf(LOG_ERROR, " esi: 0x%h edi: 0x%h", esi, edi);
logf(LOG_ERROR, " eflags:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
eflags & F_ID ? " ID" : "", eflags & F_VIP ? " VIP" : "",
eflags & F_VIF ? " VIF" : "", eflags & F_AC ? " AC" : "",
eflags & F_VM ? " VM" : "", eflags & F_RF ? " RF" : "",
eflags & F_NT ? " NT" : "", eflags & F_OF ? " OF" : "",
eflags & F_DF ? " DF" : "", eflags & F_IF ? " IF" : "",
eflags & F_TF ? " TF" : "", eflags & F_SF ? " SF" : "",
eflags & F_ZF ? " ZF" : "", eflags & F_AF ? " AF" : "",
eflags & F_PF ? " PF" : "", eflags & F_CF ? " CF" : "",
eflags & ~F_IOPL_MASK ? "" : " none");
logf(LOG_ERROR, " iopl: %d", (eflags >> 12) & 3);
logf(LOG_INFO, "Killing %s.", active_task->name);
quit_isr();
__builtin_unreachable();
}
//returns true if stack was expanded
bool pf_check_stack(uint32_t cr2/*, uint32_t edx, uint32_t ecx, uint32_t eax,
uint32_t code, uint32_t eip*/) {
//logf(LOG_INFO, "page fault in %s at 0x%h trying to access 0x%h", active_task->name, eip, cr2);
//logf(LOG_INFO, "stack bottom is 0x%h", active_task->stack_bottom);
if (cr2 >= active_task->stack_bottom - 0x1000) {
//logf(LOG_INFO, "expanding stack");
switch_to_kernel_cr3();
pd_user_allocate(active_task->page_directory, active_task->stack_bottom -= 4096, 1, true);
switch_to_task_cr3();
//logf(LOG_INFO, "new stack bottom is 0x%h", active_task->stack_bottom);
return true;
}
else {
logf(LOG_ERROR, "Illegal access of 0x%h", cr2);
return false;
}
}
static void register_int(uint8_t n, isr_t *isr, uint8_t dpl) {
idt[n].addr_low = (uint32_t)isr & 0xffff;
idt[n].addr_high = (uint32_t)isr >> 16;
idt[n].cs = 0x10;
idt[n].flags = IDT_PRESENT | (dpl << 5) | IDT_INT;
}
enum {
PIC_MCMD = 0x0020,
PIC_MDATA = 0x0021,
PIC_SCMD = 0x00a0,
PIC_SDATA = 0x00a1
};
enum {
PIC_RESET = 0x11
};
void init_idt() {
for (uint16_t i = 0; i < 256; ++i) {
idt[i].flags = 0;
idt[i].zero = 0;
}
register_int(0x30, &syscall_isr, 3);
register_int(0x38, &quit_isr, 3);
register_int(0x39, &yield_isr, 3);
register_int(0x21, &kbd_isr, 0);
register_int(0x08, &udf_isr, 0);
register_int(0x08, &dfa_isr, 0);
register_int(0x0a, &tsf_isr, 0);
register_int(0x0b, &npf_isr, 0);
register_int(0x0c, &ssf_isr, 0);
register_int(0x0d, &gpf_isr, 0);
register_int(0x0e, &pff_isr, 0);
outb(PIC_MCMD, PIC_RESET);
outb(PIC_SCMD, PIC_RESET);
outb(PIC_MDATA, 0x20);
outb(PIC_SDATA, 0x28);
outb(PIC_MDATA, 0x04);
outb(PIC_SDATA, 0x02);
outb(PIC_MDATA, 0x01);
outb(PIC_SDATA, 0x01);
outb(PIC_MDATA, 0xfd);
outb(PIC_SDATA, 0xff);
asm volatile (
"lidt %0"
: : "m" (idtr) : "al");
}
|