-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaiaos_kernel.c
More file actions
190 lines (160 loc) · 9.67 KB
/
aiaos_kernel.c
File metadata and controls
190 lines (160 loc) · 9.67 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
/* aiaos.h - v0.1 - public domain data structures - nickscha 2025
A C89 nostdlib bare metal 64bit operating system.
LICENSE
Placed in the public domain and also MIT licensed.
See end of file for detailed license information.
*/
#include "aiaos_kernel_types.h"
#include "aiaos_kernel_memory.h" /* Memory/Malloc */
#include "drivers/aiaos_driver_vga.h" /* VGA display output */
#include "drivers/aiaos_driver_pci.h" /* PCI device scanning */
#include "drivers/aiaos_driver_e1000.h" /* Network E1000 NIC */
typedef struct aiaos_perf_type_llu
{
unsigned long low_part;
unsigned long high_part;
} aiaos_perf_type_llu;
static double aiaos_perf_type_llu_to_double(aiaos_perf_type_llu a)
{
return ((double)a.high_part * 4294967296.0 + (double)a.low_part);
}
static unsigned long aiaos_perf_rdtsc(void)
{
aiaos_perf_type_llu rdtsc_value = {0};
__asm __volatile("rdtsc" : "=a"(rdtsc_value.low_part), "=d"(rdtsc_value.high_part));
return ((unsigned long)aiaos_perf_type_llu_to_double(rdtsc_value));
}
void _start_kernel(void)
{
int aiaos_logo_length = 23;
int aiaos_logo_col_offset = (AIAOS_DRIVER_VGA_COLUMNS_NUM - aiaos_logo_length) / 2;
int aiaos_logo_row_offset = 0;
int aiaos_logo_long_col_offset = (AIAOS_DRIVER_VGA_COLUMNS_NUM - 34) / 2;
char buf[31];
int i;
unsigned long vgaclear_cpu_cycles_start;
unsigned long vgaclear_cpu_cycles_end;
unsigned long meminit_cpu_cycles_start;
unsigned long meminit_cpu_cycles_end;
unsigned long memzero_cpu_cycles_start;
unsigned long memzero_cpu_cycles_end;
unsigned long stack_total;
unsigned long stack_usage;
aiaos_driver_pci_device e1000;
meminit_cpu_cycles_start = aiaos_perf_rdtsc();
aiaos_kernel_memory_initialize();
meminit_cpu_cycles_end = aiaos_perf_rdtsc();
vgaclear_cpu_cycles_start = aiaos_perf_rdtsc();
aiaos_driver_vga_clear_screen();
vgaclear_cpu_cycles_end = aiaos_perf_rdtsc();
aiaos_driver_pci_init();
if (aiaos_driver_pci_device_find(&e1000, AIAOS_DRIVER_E1000_VENDOR_ID, AIAOS_DRIVER_E1000_DEVICE_ID))
{
aiaos_driver_vga_write_string("> E1000 NIC: found", 19, 0, AIAOS_DRIVER_VGA_COLOR_GREEN);
}
/* Logo and Header Text */
aiaos_driver_vga_write_string(" _ _ _ _ _", aiaos_logo_row_offset++, aiaos_logo_col_offset, AIAOS_DRIVER_VGA_COLOR_GREEN);
aiaos_driver_vga_write_string(" / \\ / \\ / \\ / \\ / \\", aiaos_logo_row_offset++, aiaos_logo_col_offset, AIAOS_DRIVER_VGA_COLOR_GREEN);
aiaos_driver_vga_write_string(" ( A | I | A | O | S )", aiaos_logo_row_offset++, aiaos_logo_col_offset, AIAOS_DRIVER_VGA_COLOR_GREEN);
aiaos_driver_vga_write_string(" \\_/ \\_/ \\_/ \\_/ \\_/", aiaos_logo_row_offset++, aiaos_logo_col_offset, AIAOS_DRIVER_VGA_COLOR_GREEN);
aiaos_driver_vga_write_string("Application is an operating system", aiaos_logo_row_offset + 1, aiaos_logo_long_col_offset, AIAOS_DRIVER_VGA_COLOR_GREEN);
/* General Information */
aiaos_driver_vga_write_string("> Info: x86_64 (64bit) v0.1", 8, 0, AIAOS_DRIVER_VGA_COLOR_GREEN);
aiaos_driver_vga_write_string("> Hello from AIAOS C89 Kernel", 9, 0, AIAOS_DRIVER_VGA_COLOR_GREEN);
/* Available Memory Infomration */
aiaos_driver_vga_write_string("> Memory Base:", 10, 0, AIAOS_DRIVER_VGA_COLOR_GREEN);
aiaos_driver_vga_write_string((char *)aiaos_kernel_types_ptr_to_string(aiaos_kernel_memory), 10, 16, aiaos_driver_vga_make_color(AIAOS_DRIVER_VGA_COLOR_WHITE, AIAOS_DRIVER_VGA_COLOR_GREEN));
aiaos_kernel_types_ultoa(aiaos_kernel_memory_size, buf);
aiaos_driver_vga_write_string("> Memory Size:", 11, 0, AIAOS_DRIVER_VGA_COLOR_GREEN);
aiaos_driver_vga_write_string(buf, 11, 16, AIAOS_DRIVER_VGA_COLOR_GREEN);
aiaos_kernel_types_uint_to_hex(buf, AIAOS_KERNEL_MEMORY_OFFSET, 16);
aiaos_driver_vga_write_string("> Stack Base:", 12, 0, AIAOS_DRIVER_VGA_COLOR_GREEN);
aiaos_driver_vga_write_string(buf, 12, 16, AIAOS_DRIVER_VGA_COLOR_GREEN);
aiaos_kernel_types_ultoa(vgaclear_cpu_cycles_end - vgaclear_cpu_cycles_start, buf);
aiaos_driver_vga_write_string("> VGAc. Cycles:", 14, 0, AIAOS_DRIVER_VGA_COLOR_GREEN);
aiaos_driver_vga_write_string(buf, 14, 16, AIAOS_DRIVER_VGA_COLOR_GREEN);
aiaos_kernel_types_ultoa(meminit_cpu_cycles_end - meminit_cpu_cycles_start, buf);
aiaos_driver_vga_write_string("> Memi. Cycles:", 15, 0, AIAOS_DRIVER_VGA_COLOR_GREEN);
aiaos_driver_vga_write_string(buf, 15, 16, AIAOS_DRIVER_VGA_COLOR_GREEN);
/* PCI Devices */
aiaos_kernel_types_ultoa(aiaos_driver_pci_devices_count, buf);
aiaos_driver_vga_write_string("> PCI devices:", 16, 0, AIAOS_DRIVER_VGA_COLOR_GREEN);
aiaos_driver_vga_write_string(buf, 16, 16, AIAOS_DRIVER_VGA_COLOR_GREEN);
aiaos_driver_vga_write_string("PCI Device List:", 8, 40, AIAOS_DRIVER_VGA_COLOR_GREEN);
aiaos_driver_vga_write_string("ven_id dev_id", 9, 40, AIAOS_DRIVER_VGA_COLOR_GREEN);
aiaos_driver_vga_write_string("--------------", 10, 40, AIAOS_DRIVER_VGA_COLOR_GREEN);
for (i = 0; i < (int)aiaos_driver_pci_devices_count; ++i)
{
aiaos_driver_pci_device d = aiaos_driver_pci_devices[i];
aiaos_kernel_types_uint_to_hex(buf, d.vendor_id, 6);
aiaos_driver_vga_write_string(buf, 11 + i, 40, AIAOS_DRIVER_VGA_COLOR_GREEN);
aiaos_kernel_types_uint_to_hex(buf, d.device_id, 6);
aiaos_driver_vga_write_string(buf, 11 + i, 40 + 8, AIAOS_DRIVER_VGA_COLOR_GREEN);
}
/* Stack usage */
stack_total = AIAOS_KERNEL_STACK_SIZE;
aiaos_kernel_types_ultoa(stack_total, buf);
aiaos_driver_vga_write_string("> Stack total:", 17, 0, AIAOS_DRIVER_VGA_COLOR_GREEN);
aiaos_driver_vga_write_string(buf, 17, 16, AIAOS_DRIVER_VGA_COLOR_GREEN);
stack_usage = aiaos_kernel_memory_stack_usage();
aiaos_kernel_types_ultoa(stack_usage, buf);
aiaos_driver_vga_write_string("> Stack used:", 18, 0, AIAOS_DRIVER_VGA_COLOR_GREEN);
aiaos_driver_vga_write_string(buf, 18, 16, AIAOS_DRIVER_VGA_COLOR_GREEN);
/* Footer */
aiaos_driver_vga_write_string("https://github.com/nickscha/aiaos", 23, aiaos_logo_long_col_offset, AIAOS_DRIVER_VGA_COLOR_GREEN);
for (i = 0; i < AIAOS_DRIVER_VGA_COLOR_COUNT; i++)
{
aiaos_driver_vga_color color = (aiaos_driver_vga_color)i;
aiaos_driver_vga_write_string("#", 24, i, aiaos_driver_vga_make_color(color, color));
}
/* After printing basic information zero all memory */
aiaos_driver_vga_write_string("[mem] zero all memory started", 0, 0, AIAOS_DRIVER_VGA_COLOR_WHITE);
memzero_cpu_cycles_start = aiaos_perf_rdtsc();
aiaos_kernel_memory_zero(aiaos_kernel_memory, aiaos_kernel_memory_size);
memzero_cpu_cycles_end = aiaos_perf_rdtsc();
aiaos_driver_vga_write_string("[mem] zero all memory finish ", 0, 0, AIAOS_DRIVER_VGA_COLOR_GREEN);
aiaos_kernel_types_ultoa(memzero_cpu_cycles_end - memzero_cpu_cycles_start, buf);
aiaos_driver_vga_write_string("> Memz. Cycles:", 13, 0, AIAOS_DRIVER_VGA_COLOR_GREEN);
aiaos_driver_vga_write_string(buf, 13, 16, AIAOS_DRIVER_VGA_COLOR_GREEN);
}
/*
------------------------------------------------------------------------------
This software is available under 2 licenses -- choose whichever you prefer.
------------------------------------------------------------------------------
ALTERNATIVE A - MIT License
Copyright (c) 2025 nickscha
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
------------------------------------------------------------------------------
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------------------------------
*/