-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.h
More file actions
274 lines (237 loc) · 9.63 KB
/
Copy pathkernel.h
File metadata and controls
274 lines (237 loc) · 9.63 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
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
#ifndef KERNEL_H
#define KERNEL_H
/* The user-window memory layout (load base, stack, brk cap, graphics
* back-buffers, framebuffer, kernel heap) is defined ONCE in the ABI header
* shared with ring-3 programs. The kernel derives its own constants from it,
* so growing the window or moving a back-buffer cannot leave the two sides
* disagreeing -- that mismatch is what made the lua/MicroPython in-OS tests
* and DOOM's window go dark when the layout moved. Do not hardcode a layout
* address in the kernel or in a ring-3 program; put it in minios_abi.h. */
#include "minios_abi.h"
/* ========== Port I/O helpers ========== */
#ifndef PORT_IO_DEFINED
#define PORT_IO_DEFINED
static inline void outb(unsigned short port, unsigned char val) {
__asm__ volatile("outb %0, %1" : : "a"(val), "Nd"(port));
}
static inline unsigned char inb(unsigned short port) {
unsigned char r;
__asm__ volatile("inb %1, %0" : "=a"(r) : "Nd"(port));
return r;
}
static inline void outw(unsigned short port, unsigned short val) {
__asm__ volatile("outw %0, %1" : : "a"(val), "Nd"(port));
}
static inline unsigned short inw(unsigned short port) {
unsigned short r;
__asm__ volatile("inw %1, %0" : "=a"(r) : "Nd"(port));
return r;
}
#endif
/* ========== VGA text mode ========== */
#define VGA_BASE ((volatile char *)0xB8000)
#define VGA_COLS 80
#define VGA_ROWS 25
void vga_clear(void);
void vga_putc(char c);
void vga_puts(const char *s);
void vga_scroll(void);
void vga_set_cursor(int x, int y);
void vga_newline(void);
/* ========== Serial console (COM1) ========== */
void serial_init(void);
void serial_putc(char c);
void serial_puts(const char *s);
int serial_available(void);
int serial_getc(void);
/* ========== Keyboard (PS/2) ========== */
#define KEY_BACKSPACE 0x0E
#define KEY_ENTER 0x1C
#define KEY_LSHIFT 0x2A
#define KEY_RSHIFT 0x36
#define KEY_CAPS 0x3A
#define KEY_E0 0xE0
#define KEY_UP 0x48
#define KEY_DOWN 0x50
#define KEY_PGUP 0x49
#define KEY_PGDN 0x51
#define KEY_ESC 0x1B
#define KEY_CSI '['
#define KEY_ARR_UP 'A'
#define KEY_ARR_DOWN 'B'
#define KEY_ARR_RIGHT 'C'
#define KEY_ARR_LEFT 'D'
#define KEY_HOME_SEQ 'H'
#define KEY_END_SEQ 'F'
#define KEY_PGUP_SEQ '5'
#define KEY_PGDN_SEQ '6'
#define KEY_LEFT 0x4B
#define KEY_RIGHT 0x4D
#define KEY_LCTRL 0x1D
#define KEY_F5 0x3F
#define KEY_F11 0x57
#define KEY_TILDE '~'
#define KEY_LALT 0x38
#define KEY_RALT 0x38
#define KEY_HOME 0x47
#define KEY_END 0x4F
int kbd_read(void);
int kbd_available(void);
void kbd_reset_for_shell(void);
void mouse_disable(void);
void mouse_enable(void);
/* ========== VGA cursor ========== */
void vga_cursor_enable(int on);
/* ========== User window and memory allocator ==========
* All values derive from progs/minios_abi.h (single source of truth for the
* kernel-ABI memory layout). USER_WIN_LO/HI are the asm-safe (no UL suffix)
* mirrors the syscall return discriminator string-literal needs; static
* asserts in kernel.c prove they cannot drift from the ABI header. */
#define USER_LOAD_BASE MINIOS_USER_LOAD_BASE
#define USER_LOAD_END MINIOS_USER_LOAD_END
#define USER_STACK_SIZE MINIOS_USER_STACK_SIZE
#define USER_STACK_TOP MINIOS_USER_STACK_TOP
#define USER_STACK_BASE MINIOS_USER_STACK_BASE
#define USER_BRK_END MINIOS_USER_BRK_END
/* Syscall kernel stack: a dedicated region below the kernel image, exchanged
* on syscall entry so the kernel never runs on a user stack. The 32 KB region
* [0x80000, 0x88000) also holds the per-SPAWN-child stack carved by k_exec_user. */
#define SYS_KSTK_TOP 0x00088000UL
#define SYS_KSTK_BASE (SYS_KSTK_TOP - 0x8000)
#define HEAP_BASE MINIOS_HEAP_BASE
#define HEAP_SIZE MINIOS_HEAP_SIZE
void *kmalloc(unsigned long size);
void kfree(void *ptr);
void *kcalloc(unsigned long nmemb, unsigned long size);
void *krealloc(void *ptr, unsigned long size);
void kallocator_init(void);
/* dlmalloc backend (third_party/dlmalloc): an mspace rooted over the fixed
* kernel heap. The kernel's allocator delegates to these. */
void dlmalloc_init(void);
void *dlmalloc_malloc(unsigned long size);
void dlmalloc_free(void *ptr);
void *dlmalloc_calloc(unsigned long nmemb, unsigned long size);
void *dlmalloc_realloc(void *ptr, unsigned long size);
/* ========== Ramdisk file system ========== */
#define RAMDISK_MAX_FILES 128
#define RAMDISK_FNAME_LEN 64
typedef struct {
char name[RAMDISK_FNAME_LEN];
unsigned size;
unsigned offset;
} RDFile;
void ramdisk_init(void);
RDFile *ramdisk_open(const char *name);
int ramdisk_read(RDFile *f, void *buf, unsigned offset, unsigned len);
int ramdisk_write(RDFile *f, const void *buf, unsigned offset, unsigned len);
RDFile *ramdisk_create(const char *name, unsigned size);
int ramdisk_resize(RDFile *f, unsigned newsize);
int ramdisk_delete(RDFile *f);
int ramdisk_list(RDFile **out, int max);
void ramdisk_setup_from(void *data, unsigned size);
/* Path resolution choke point shared by the shell builtins and the zip
* builtins. fs_resolve resolves a path against the cwd into `out`
* (cap >= RAMDISK_FNAME_LEN); a name that does not fit is rejected like a
* missing file, never truncated. */
int fs_resolve(const char *path, char *out, unsigned cap);
int fs_dir_exists(const char *dir);
int fs_is_dir(const char *resolved);
/* ========== Simple FILE interface (for libc compat) ========== */
#define EOF (-1)
typedef struct {
RDFile *rf;
unsigned pos;
char *wbuf; /* write buffer for created files */
unsigned wsize;
unsigned wcap;
int mode; /* 0=read, 1=write (truncate), 2=append */
int is_console; /* 1 = stdin/stdout/stderr, routed to the console */
int minifs_ino; /* >=0 when backed by minifs, -1 = ramdisk */
unsigned minifs_size;
} KFILE;
KFILE *kfopen(const char *path, const char *mode);
int kfclose(KFILE *f);
int kfgetc(KFILE *f);
char *kfgets(char *buf, int size, KFILE *f);
int kfungetc(int c, KFILE *f);
unsigned long kfread(void *ptr, unsigned long size, unsigned long nmemb, KFILE *f);
unsigned long kfwrite(const void *ptr, unsigned long size, unsigned long nmemb, KFILE *f);
int kfseek(KFILE *f, long offset, int whence);
long kftell(KFILE *f);
int kfputs(const char *s, KFILE *f);
int kfputc(int c, KFILE *f);
int kfflush(KFILE *f);
void krewind(KFILE *f);
/* ========== String functions ========== */
unsigned long kstrlen(const char *s);
char *kstrcpy(char *dst, const char *src);
char *kstrncpy(char *dst, const char *src, unsigned long n);
char *kstrncat(char *dst, const char *src, unsigned long n);
int kstrcmp(const char *a, const char *b);
int kstrncmp(const char *a, const char *b, unsigned long n);
char *kstrchr(const char *s, int c);
char *kstrstr(const char *hay, const char *ndl);
void *kmemcpy(void *dst, const void *src, unsigned long n);
void *kmemset(void *dst, int c, unsigned long n);
int kmemcmp(const void *a, const void *b, unsigned long n);
void *kmemmove(void *dst, const void *src, unsigned long n);
/* ========== printf ========== */
int kprintf(const char *fmt, ...);
int kfprintf(KFILE *f, const char *fmt, ...);
int ksprintf(char *buf, const char *fmt, ...);
int ksnprintf(char *buf, unsigned long size, const char *fmt, ...);
/* ========== Shell ========== */
void shell_init(void);
void shell_run(void);
/* ========== Program execution ========== */
typedef int (*prog_entry_t)(int argc, char **argv);
int k_spawn(const char *name, int argc, char **argv);
void k_register_program(const char *name, prog_entry_t entry);
void k_register_process(const char *name, void *proc_entry);
void k_register_symbol(const char *name, void *addr);
/* ========== Process execution (Linux ELF binaries) ========== */
int k_exec_user(void *entry, int argc, char **argv);
int k_run_rel(prog_entry_t entry, int argc, char **argv);
void kexit(int code);
/* ========== Desktop shortcut launch ========== */
void desktop_launch(const char *cmd);
void shell_queue_launch(const char *cmd);
/* ========== ELF loader ========== */
void *elf_load(void *data, unsigned size); /* ET_REL relocatable .o */
void *load_exec_elf(void *data, unsigned size); /* ET_EXEC / ET_DYN */
/* ========== Linux syscall interface ========== */
void syscall_init(void);
/* ========== Syscall table ========== */
void *ksym_resolve(const char *name);
/* ========== Kernel info ========== */
extern unsigned long kernel_end;
extern char ramdisk_start[];
extern char ramdisk_end[];
/* ========== Kernel clock ========== */
unsigned long ktime_ms(void);
/* ========== PC speaker ========== */
void pcspk_init(void);
void pcspk_tone(unsigned freq);
void pcspk_off(void);
void pcspk_set_volume(unsigned volume);
unsigned pcspk_get_volume(void);
/* ========== RTC time-of-day ========== */
int rtc_read_tod(int *hour, int *min, int *sec);
/* ========== IDE driver ========== */
void ide_init(void);
int ide_read_sectors(unsigned int lba, unsigned int count, void *buf);
int ide_write_sectors(unsigned int lba, unsigned int count, const void *buf);
int ide_read_sector(unsigned int lba, void *buf);
int ide_write_sector(unsigned int lba, const void *buf);
unsigned int ide_total_sectors(void);
int ide_present(void);
/* ========== Block device layer ========== */
void block_init(void);
int block_read(unsigned int block_num, void *buf);
int block_write(unsigned int block_num, const void *buf);
int block_read_multi(unsigned int block_num, unsigned int count, void *buf);
int block_write_multi(unsigned int block_num, unsigned int count, const void *buf);
unsigned int block_total(void);
/* ========== User-mode fault recovery ========== */
void k_user_fault_return(void);
#endif