- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.h
More file actions
Latest commit
128 lines (108 loc) · 2.97 KB
/
Copy pathkernel.h
File metadata and controls
128 lines (108 loc) · 2.97 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
/* NATKERNEL — Universal Grammar Kernel
* N=7 IR kinds: struct, define, typedef, inline, loop, if, return
* Author: Josué Argaña Silguero
* License: MII Open License v1.0
*/
#ifndefNATKERNEL_H
#defineNATKERNEL_H
#defineNULL ((void*)0)
/* IR-define: Architecture constants */
#defineNAT_VERSION "0.2.0"
#defineMAX_PROCESSES 256
#defineSTACK_SIZE 8192
#definePAGE_SIZE 4096
#defineKERNEL_BASE 0xFFFF800000000000
/* IR-typedef: Core types */
typedefunsigned longu64;
typedefunsigned intu32;
typedefunsigned shortu16;
typedefunsigned charu8;
typedeflongi64;
typedefinti32;
/* IR-struct: CPU context for context switching */
typedefstruct {
u64rax, rbx, rcx, rdx, rsi, rdi, rbp, rsp;
u64r8, r9, r10, r11, r12, r13, r14, r15;
u64rip, cs, rflags;
} __attribute__((packed)) CPUContext;
/* IR-struct: Process control block */
typedefstruct {
u32pid;
u32state;
CPUContextctx;
u8stack[STACK_SIZE];
} Process;
#definePROC_READY 1
#definePROC_RUNNING 2
#definePROC_BLOCKED 3
#definePROC_ZOMBIE 4
/* Syscall numbers */
#defineSYS_WRITE 0
#defineSYS_READ 1
#defineSYS_EXIT 2
#defineSYS_SBRK 3
#defineSYS_GETPID 4
#defineSYS_FORK 5
/* IR-inline: IO operations */
staticinlinevoidoutb(u16port, u8val) {
asm volatile("outb %0, %1" : : "a"(val), "Nd"(port));
}
staticinlineu8inb(u16port) {
u8val;
asm volatile("inb %1, %0" : "=a"(val) : "Nd"(port));
returnval;
}
staticinlinevoidkmemcpy(void*d, constvoid*s, u64n) {
for (u64i=0; i<n; i++) ((u8*)d)[i] = ((constu8*)s)[i];
}
staticinlinevoidkmemset(void*p, u8v, u64n) {
for (u64i=0; i<n; i++) ((u8*)p)[i] =v;
}
/* IR-extern: Module declarations */
/* scheduler */
u32sched_spawn(void (*entry)());
voidsched_tick();
Process*sched_current();
u32sched_count();
Process*sched_procs(u32i);
/* memory */
u64alloc_page();
voidfree_page(u64addr);
voidmap_page(u64vaddr, u64paddr, u64flags);
u64get_allocated();
/* syscall */
voidsys_init();
u64syscall_dispatch(u64num, u64a1, u64a2, u64a3);
/* fs */
u32fs_create(constchar*name, u32size);
u32fs_read(u32fd, void*buf, u64len);
u32fs_write(u32fd, constvoid*buf, u64len);
/* fs from_linux */
u32vfs_open(constchar*n, u32fl);
u32vfs_read(u32fd, void*b, u32len);
u32vfs_write(u32fd, constvoid*b, u32len);
voidvfs_close(u32fd);
u32vfs_list(u32s, void*b, u32l);
/* crypto from_linux */
voidsha256(constu8*data, u32len, u8*out);
u32rand_u32(void);
/* memory from_linux */
u64get_free(void);
/* network from_linux */
u32socket_create(u32fam, u32type);
/* from_linux modules */
voidusb_init(void);
voidpci_init(void);
voidnet_init(void);
voidarch_init(void);
voidaudio_init(void);
voidgpu_init(void);
voidvirt_init(void);
voidipc_init(void);
voidcrypto_init(void);
voidblock_init(void);
voidnvme_init(void);
voidinput_init(void);
/* main */
voidkernel_main();
#endif