-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.c
More file actions
1688 lines (1553 loc) · 57.9 KB
/
Copy pathkernel.c
File metadata and controls
1688 lines (1553 loc) · 57.9 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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Global Definitions and Structures
#define VGA_WIDTH 80
#define VGA_HEIGHT 25
#define VGA_BUFFER 0xB8000
#define PAGE_SIZE 4096
#define MAX_PROCESSES 8
#define MAX_FILES 32 // Reduced from 32 to 16 to test memory constraints
#define MAX_INODES 8
#define KERNEL_BASE 0xC0000000
#define USER_BASE 0x100000
#define FILE_WRITE_MAX 4096
#define MAX_FILE_SIZE 4096
// System call numbers
#define SYS_WRITE 1
#define SYS_OPEN 2
#define SYS_EXIT 3
#define SYS_PS 4
#define SYS_KILL 5
#define SYS_READ 6
#define SYS_CLOSE 7
#define SYS_CREATE 8
#define SYS_LS 9
// Diary Global Variables
static char diary_buffer[256];
static int diary_index = 0;
static int diary_active = 0;
// File Write Buffer
static char file_write_buffer[FILE_WRITE_MAX];
static int file_write_index = 0;
static int file_write_active = 0;
static int current_file_fd = -1;
// Process structure for task management
typedef struct {
void (*task)(); // Task function pointer
int state; // 0: ready, 1: running, 2: terminated
int esp; // Stack pointer
int pid; // Process ID
int priority; // Process priority (1-10)
unsigned int user_stack; // User stack address
unsigned int code_segment;// Code segment
int privilege; // 0: kernel, 3: user
unsigned int page_dir; // Page directory address
} Process;
// Inode structure for file system
// Update your Inode definition:
typedef struct {
int id;
char name[32];
int size;
int used;
char data[MAX_FILE_SIZE]; // UPDATED
} Inode;
// Virtual File System mount structure
typedef struct {
char device[16]; // Device name
char mount_point[16]; // Mount point path
char fs_type[16]; // Filesystem type
int inodes_used; // Number of used inodes
int files; // Number of files
Inode inodes[MAX_INODES]; // Inode table
} VFS_Mount;
// File descriptor structure
typedef struct {
int inode_id; // Associated inode
int used; // 1: in use, 0: free
int offset; // Current file offset
} FileDescriptor;
// Global Variables
Process processes[MAX_PROCESSES]; // Array of processes
int current_process = 0; // Index of currently running process
char keyboard_buffer[256]; // Buffer for keyboard input
int buffer_index = 0; // Current index in keyboard buffer
char shell_buffer[256]; // Buffer for shell commands
int shell_index = 0; // Current index in shell buffer
char command_log[512]; // Buffer for command history
int log_index = 0; // Current index in command log
volatile int schedule_flag = 0; // Flag to trigger scheduling
int menu_active = 0; // Menu state: 0 (off), 1 (on)
int shell_active = 0; // Shell state: 0 (off), 1 (on)
VFS_Mount vfs; // Single VFS mount
FileDescriptor fds[MAX_FILES]; // File descriptor table
unsigned int* kernel_page_dir; // Kernel page directory
int vfs_initialized = 0; // Flag to track VFS initialization
// Function Prototypes
void DiaryNote(void);
void FileWrite(const char* filename);
void display_shell_prompt(void);
// String manipulation functions
void custom_strcpy(char* dest, const char* src) {
while (*src) {
*dest++ = *src++;
}
*dest = 0;
}
int strcmp(const char* s1, const char* s2) {
while (*s1 && *s2 && *s1 == *s2) {
s1++;
s2++;
}
return *s1 - *s2;
}
int strncmp(const char* s1, const char* s2, int n) {
while (n > 0 && *s1 && *s2 && *s1 == *s2) {
s1++;
s2++;
n--;
}
if (n == 0) return 0;
return *s1 - *s2;
}
// VGA Display Functions
void clear_screen() {
unsigned short* vga = (unsigned short*)VGA_BUFFER;
for (int i = 0; i < VGA_WIDTH * VGA_HEIGHT; i++) {
vga[i] = 0x0700; // White on black
}
}
void print_string(const char* str, int row, int col) {
unsigned short* vga = (unsigned short*)VGA_BUFFER;
int index = row * VGA_WIDTH + col;
while (*str && index < VGA_WIDTH * VGA_HEIGHT) {
vga[index] = 0x0700 | (*str & 0xFF); // Ensure ASCII
str++;
index++;
}
}
void print_string_with_attr(const char* str, int row, int col, unsigned char attr) {
unsigned short* vga = (unsigned short*)VGA_BUFFER;
int index = row * VGA_WIDTH + col;
while (*str && index < VGA_WIDTH * VGA_HEIGHT) {
vga[index] = (attr << 8) | (*str & 0xFF);
str++;
index++;
}
}
void print_hex_byte(unsigned char value, int row, int col) {
unsigned short* vga = (unsigned short*)VGA_BUFFER;
const char hex[] = "0123456789ABCDEF";
if (row * VGA_WIDTH + col + 1 < VGA_WIDTH * VGA_HEIGHT) {
vga[row * VGA_WIDTH + col] = 0x4F00 | hex[(value >> 4) & 0xF];
vga[row * VGA_WIDTH + col + 1] = 0x4F00 | hex[value & 0xF];
}
}
void print_number(int value, int row, int col) {
char buf[16];
int i = 0, temp = value;
if (temp == 0) {
buf[i++] = '0';
} else {
while (temp) {
buf[i++] = (temp % 10) + '0';
temp /= 10;
}
}
buf[i] = 0;
for (int j = 0; j < i / 2; j++) {
char t = buf[j];
buf[j] = buf[i - j - 1];
buf[i - j - 1] = t;
}
print_string(buf, row, col);
}
void redraw_write_buffer(int text_row_start, int text_col, int rect_width, int rect_height) {
unsigned short* vga = (unsigned short*)VGA_BUFFER;
const int visible_rows = rect_height - 6;
const int visible_cols = rect_width - 4;
int start_pos = file_write_index - (visible_rows * visible_cols);
if (start_pos < 0) start_pos = 0;
for (int i = 0; i < visible_rows * visible_cols; i++) {
int buffer_pos = start_pos + i;
int r = i / visible_cols;
int c = i % visible_cols;
char ch = (buffer_pos < file_write_index) ? file_write_buffer[buffer_pos] : ' ';
vga[(text_row_start + r) * VGA_WIDTH + text_col + c] = 0x2F00 | ch;
}
}
// Virtual Memory Management
void init_paging() {
print_string("Initializing paging...", 1, 0);
kernel_page_dir = (unsigned int*)0x100000; // Page directory at 1MB
for (int i = 0; i < 1024; i++) {
kernel_page_dir[i] = 0;
}
unsigned int* page_table = (unsigned int*)0x101000; // First page table
for (int i = 0; i < 1024; i++) {
page_table[i] = (i * PAGE_SIZE) | 0x3; // Present, R/W, Supervisor
}
kernel_page_dir[0] = (unsigned int)page_table | 0x3;
kernel_page_dir[768] = (unsigned int)page_table | 0x3;
unsigned int vga_addr = 0xB8000;
int pt_index = vga_addr / PAGE_SIZE;
page_table[pt_index] = vga_addr | 0x3;
//print_string("Page directory set up", 2, 0);
asm volatile(
"mov %0, %%cr3\n\t"
"mov %%cr0, %%eax\n\t"
"or $0x80000000, %%eax\n\t"
"mov %%eax, %%cr0"
: : "r"(kernel_page_dir) : "eax"
);
print_string("Paging enabled", 2, 0);
}
unsigned int* create_user_page_dir() {
unsigned int* page_dir = (unsigned int*)0x200000;
for (int i = 0; i < 1024; i++) {
page_dir[i] = 0;
}
unsigned int* page_table = (unsigned int*)0x201000;
for (int i = 0; i < 1024; i++) {
page_table[i] = (i * PAGE_SIZE + USER_BASE) | 0x7; // Present, R/W, User
}
page_dir[0] = (unsigned int)page_table | 0x7;
page_dir[768] = kernel_page_dir[768];
return page_dir;
}
// Virtual File System
void init_vfs() {
print_string("Initializing VFS...", 3, 0);
// Debug: Step 1
//print_string("Step 1: Setting device", 4, 0);
custom_strcpy(vfs.device, "hda");
// Debug: Step 2
//print_string("Step 2: Setting mount point", 5, 0);
custom_strcpy(vfs.mount_point, "/");
// Debug: Step 3
//print_string("Step 3: Setting fs type", 6, 0);
custom_strcpy(vfs.fs_type, "ext2");
// Debug: Step 4
//print_string("Step 4: Initializing counters", 7, 0);
vfs.inodes_used = 0;
vfs.files = 0;
// Debug: Step 5
//print_string("Step 5: Initializing inodes", 8, 0);
for (int i = 0; i < MAX_INODES; i++) {
vfs.inodes[i].used = 0;
vfs.inodes[i].id = 0;
vfs.inodes[i].size = 0;
for (int j = 0; j < 32; j++) {
vfs.inodes[i].name[j] = 0;
}
for (int j = 0; j < 128; j++) {
vfs.inodes[i].data[j] = 0;
}
}
// Debug: Step 6
//print_string("Step 6: Initializing file descriptors", 9, 0);
for (int i = 0; i < MAX_FILES; i++) {
fds[i].used = 0;
fds[i].inode_id = -1;
fds[i].offset = 0;
}
// Debug: Step 7
//print_string("Step 7: Setting VFS flag", 10, 0);
vfs_initialized = 1;
// Final confirmation
print_string("VFS initialized", 4, 0);
}
int vfs_create_file(const char* name) {
if (!vfs_initialized) {
print_string("VFS not initialized in create", 16, 0);
return -1;
}
if (vfs.inodes_used >= MAX_INODES) {
print_string("No free inodes", 16, 0);
return -1;
}
for (int i = 0; i < MAX_INODES; i++) {
if (!vfs.inodes[i].used) {
vfs.inodes[i].used = 1;
vfs.inodes[i].id = i;
int j = 0;
while (name[j] && j < 31) {
vfs.inodes[i].name[j] = name[j];
j++;
}
vfs.inodes[i].name[j] = 0;
vfs.inodes[i].size = 0;
vfs.inodes_used++;
vfs.files++;
print_string("Created inode: ", 16, 0);
print_number(i, 16, 15);
return i;
}
}
print_string("No free inodes found", 16, 0);
return -1;
}
int vfs_open_file(const char* name) {
if (!vfs_initialized) {
print_string("VFS not initialized in open", 16, 0);
return -1;
}
for (int i = 0; i < MAX_INODES; i++) {
if (vfs.inodes[i].used && strcmp(vfs.inodes[i].name, name) == 0) {
for (int j = 0; j < MAX_FILES; j++) {
if (!fds[j].used) {
fds[j].used = 1;
fds[j].inode_id = i;
fds[j].offset = 0;
print_string("Opened fd: ", 16, 0);
print_number(j, 16, 11);
return j;
}
}
print_string("No free file descriptors", 16, 0);
return -1;
}
}
print_string("File not found: ", 16, 0);
print_string(name, 16, 16);
return -1;
}
// Fix vfs_read_file to allow full reads:
int vfs_read_file(int fd, char* buf, int len) {
if (!vfs_initialized) {
print_string("VFS not initialized in read", 17, 0);
return -1;
}
if (fd < 0 || fd >= MAX_FILES || !fds[fd].used) {
print_string("Invalid file descriptor: ", 17, 0);
print_number(fd, 17, 25);
return -1;
}
Inode* inode = &vfs.inodes[fds[fd].inode_id];
if (!inode->used) {
print_string("Inode not used: ", 17, 0);
print_number(fds[fd].inode_id, 17, 16);
return -1;
}
if (inode->size == 0 || fds[fd].offset >= inode->size) {
print_string("File empty or offset beyond size", 17, 0);
return 0;
}
int bytes = 0;
while (bytes < len && fds[fd].offset < inode->size) { // REMOVED 128 cap
buf[bytes] = inode->data[fds[fd].offset];
bytes++;
fds[fd].offset++;
}
print_string("Read bytes: ", 17, 0);
print_number(bytes, 17, 12);
return bytes;
}
// Fix vfs_write_file to match new MAX_FILE_SIZE
int vfs_write_file(int fd, const char* buf, int len) {
if (!vfs_initialized) {
print_string("VFS not initialized in write", 18, 0);
return -1;
}
if (fd < 0 || fd >= MAX_FILES || !fds[fd].used) {
print_string("Invalid file descriptor in write: ", 18, 0);
print_number(fd, 18, 34);
return -1;
}
Inode* inode = &vfs.inodes[fds[fd].inode_id];
if (!inode->used) {
print_string("Inode not used in write: ", 18, 0);
print_number(fds[fd].inode_id, 18, 25);
return -1;
}
int bytes = 0;
while (bytes < len && fds[fd].offset < MAX_FILE_SIZE) { // UPDATED
inode->data[fds[fd].offset] = buf[bytes];
bytes++;
fds[fd].offset++;
}
if (fds[fd].offset > inode->size) {
inode->size = fds[fd].offset;
}
print_string("Wrote bytes: ", 18, 0);
print_number(bytes, 18, 13);
return bytes;
}
void vfs_close_file(int fd) {
if (!vfs_initialized) {
print_string("VFS not initialized in close", 19, 0);
return;
}
if (fd >= 0 && fd < MAX_FILES && fds[fd].used) {
fds[fd].used = 0;
fds[fd].inode_id = -1;
fds[fd].offset = 0;
print_string("Closed fd: ", 19, 0);
print_number(fd, 19, 11);
}
}
void vfs_list_files(char* buf, int* len) {
if (!vfs_initialized) {
print_string("VFS not initialized in ls", 16, 0);
*len = 0;
buf[0] = 0;
return;
}
int pos = 0;
for (int i = 0; i < MAX_INODES; i++) {
if (vfs.inodes[i].used) {
int j = 0;
while (vfs.inodes[i].name[j] && j < 31 && pos < 255) {
char c = vfs.inodes[i].name[j];
if (c >= 32 && c <= 126) {
buf[pos++] = c;
}
j++;
}
if (pos < 255) {
buf[pos++] = ' ';
}
}
}
buf[pos] = 0;
*len = pos;
}
// File Write Interface
void FileWrite(const char* filename) {
if (!vfs_initialized) {
clear_screen();
print_string("VFS not initialized. File operations disabled.", 12, 10);
for (volatile int i = 0; i < 1000000; i++);
clear_screen();
display_shell_prompt();
shell_active = 1;
return;
}
unsigned short* vga = (unsigned short*)VGA_BUFFER;
const int rect_width = 60;
const int rect_height = 15;
const int rect_start_row = (VGA_HEIGHT - rect_height) / 2;
const int rect_start_col = (VGA_WIDTH - rect_width) / 2;
const int rect_end_row = rect_start_row + rect_height - 1;
const int rect_end_col = rect_start_col + rect_width - 1;
clear_screen();
for (int row = rect_start_row; row <= rect_end_row; row++) {
for (int col = rect_start_col; col <= rect_end_col; col++) {
vga[row * VGA_WIDTH + col] = 0x2F00 | ' ';
}
}
for (int col = rect_start_col; col <= rect_end_col; col++) {
vga[rect_start_row * VGA_WIDTH + col] = 0x2F00 | '-';
vga[rect_end_row * VGA_WIDTH + col] = 0x2F00 | '-';
}
for (int row = rect_start_row + 1; row < rect_end_row; row++) {
vga[row * VGA_WIDTH + rect_start_col] = 0x2F00 | '|';
vga[row * VGA_WIDTH + rect_end_col] = 0x2F00 | '|';
}
vga[rect_start_row * VGA_WIDTH + rect_start_col] = 0x2F00 | '+';
vga[rect_start_row * VGA_WIDTH + rect_end_col] = 0x2F00 | '+';
vga[rect_end_row * VGA_WIDTH + rect_start_col] = 0x2F00 | '+';
vga[rect_end_row * VGA_WIDTH + rect_end_col] = 0x2F00 | '+';
int text_row = rect_start_row + 1;
int text_col = rect_start_col + 2;
print_string_with_attr("Write to File", text_row++, text_col, 0x2F);
print_string_with_attr("File: ", text_row, text_col, 0x2F);
print_string_with_attr(filename, text_row++, text_col + 6, 0x2F);
print_string_with_attr("Press Enter to save, Esc to cancel.", text_row++, text_col, 0x2F);
print_string_with_attr("----------------------", text_row++, text_col, 0x2F);
//print_string_with_attr("Enter text:", text_row++, text_col, 0x2F);
for (int i = 0; i < 128; i++) {
file_write_buffer[i] = 0;
}
file_write_index = 0;
file_write_active = 1;
}
// Shell Display Functions
void clear_shell_input() {
unsigned short* vga = (unsigned short*)VGA_BUFFER;
for (int i = 0; i < VGA_WIDTH; i++) {
vga[23 * VGA_WIDTH + i] = 0x0700;
}
shell_index = 0;
for (int i = 0; i < 256; i++) {
shell_buffer[i] = 0;
}
}
void clear_shell_output() {
unsigned short* vga = (unsigned short*)VGA_BUFFER;
for (int i = 0; i < VGA_WIDTH; i++) {
vga[15 * VGA_WIDTH + i] = 0x0700;
vga[22 * VGA_WIDTH + i] = 0x0700;
}
}
void clear_shell_command_prompt() {
unsigned short* vga = (unsigned short*)VGA_BUFFER;
for (int i = 0; i < VGA_WIDTH; i++) {
vga[20 * VGA_WIDTH + i] = 0x0700;
}
}
void clear_shell() {
unsigned short* vga = (unsigned short*)VGA_BUFFER;
for (int i = 15; i <= 23; i++) {
for (int j = 0; j < VGA_WIDTH; j++) {
vga[i * VGA_WIDTH + j] = 0x0700;
}
}
shell_index = 0;
for (int i = 0; i < 256; i++) {
shell_buffer[i] = 0;
}
}
void display_shell_prompt() {
clear_shell_input();
print_string_with_attr("SHELL>> ", 23, 0, 0x2F);
}
// Menu Functions
void display_menu() {
unsigned short* vga = (unsigned short*)VGA_BUFFER;
for (int i = 0; i < VGA_WIDTH * 2; i++) {
vga[(VGA_HEIGHT - 2) * VGA_WIDTH + i] = 0x0700;
}
print_string("Menu: ", 24, 0);
print_string_with_attr("1", 24, 6, 0x0F);
print_string(".Show/Hide ", 24, 7);
print_string_with_attr("2", 24, 18, 0x0F);
print_string(".Exit ", 24, 19);
print_string_with_attr("3", 24, 25, 0x0F);
print_string(".Crash (BSOD) ", 24, 26);
print_string_with_attr("S", 24, 40, 0x0F);
print_string(".Shell ", 24, 41);
print_string_with_attr("V", 24, 48, 0x0F);
print_string(".Virtual Memory ", 24, 49);
}
void hide_menu() {
unsigned short* vga = (unsigned short*)VGA_BUFFER;
for (int i = 0; i < VGA_WIDTH * 2; i++) {
vga[(VGA_HEIGHT - 2) * VGA_WIDTH + i] = 0x0700;
}
}
// System Control Functions
void halt_system() {
clear_screen();
print_string("System halted.", 12, 33);
asm volatile("hlt");
while (1);
}
void display_bsod() {
unsigned short* vga = (unsigned short*)VGA_BUFFER;
for (int i = 0; i < VGA_WIDTH * VGA_HEIGHT; i++) {
vga[i] = 0x2F00;
}
print_string_with_attr("*** A fatal error has occurred ***", 5, 24, 0x2F);
print_string_with_attr("Sebria OS has encountered a critical error and must halt.", 7, 12, 0x2F);
print_string_with_attr("Error Code: 0xDEADBEEF", 9, 29, 0x2F);
asm volatile("hlt");
while (1);
}
// Screen Dump Function
void dump_screen() {
unsigned short* vga = (unsigned short*)VGA_BUFFER;
char screen_buffer[VGA_HEIGHT * (VGA_WIDTH + 1)];
int buffer_pos = 0;
for (int row = 0; row < VGA_HEIGHT; row++) {
for (int col = 0; col < VGA_WIDTH; col++) {
char c = (char)(vga[row * VGA_WIDTH + col] & 0xFF);
if (c == 0) c = ' ';
if (buffer_pos < VGA_HEIGHT * (VGA_WIDTH + 1) - 1) {
screen_buffer[buffer_pos++] = c;
}
}
if (buffer_pos < VGA_HEIGHT * (VGA_WIDTH + 1) - 1) {
screen_buffer[buffer_pos++] = '\n';
}
}
screen_buffer[buffer_pos ? buffer_pos - 1 : 0] = 0;
clear_shell_output();
print_string("Dumping screen contents...", 18, 0);
for (volatile int i = 0; i < 100000; i++);
const int rect_width = 60;
const int rect_height = 15;
const int rect_start_row = (VGA_HEIGHT - rect_height) / 2;
const int rect_start_col = (VGA_WIDTH - rect_width) / 2;
const int rect_end_row = rect_start_row + rect_height - 1;
const int rect_end_col = rect_start_col + rect_width - 1;
clear_screen();
for (int col = rect_start_col; col <= rect_end_col; col++) {
vga[rect_start_row * VGA_WIDTH + col] = 0x2F00 | '-';
vga[rect_end_row * VGA_WIDTH + col] = 0x2F00 | '-';
}
for (int row = rect_start_row + 1; row < rect_end_row; row++) {
vga[row * VGA_WIDTH + rect_start_col] = 0x2F00 | '|';
vga[row * VGA_WIDTH + rect_end_col] = 0x2F00 | '|';
}
vga[rect_start_row * VGA_WIDTH + rect_start_col] = 0x2F00 | '+';
vga[rect_start_row * VGA_WIDTH + rect_end_col] = 0x2F00 | '+';
vga[rect_end_row * VGA_WIDTH + rect_start_col] = 0x2F00 | '+';
vga[rect_end_row * VGA_WIDTH + rect_end_col] = 0x2F00 | '+';
int display_row = rect_start_row + 1;
int display_col = rect_start_col + 1;
int scrollAddr = 0;
for (int row = scrollAddr; row < scrollAddr + (rect_height - 2) && row < VGA_HEIGHT; row++) {
for (int col = 0; col < rect_width - 2 && col < VGA_WIDTH; col++) {
int buf_index = row * (VGA_WIDTH + 1) + col;
char c = (buf_index < buffer_pos) ? screen_buffer[buf_index] : ' ';
if (c == '\n' || c == 0) c = ' ';
vga[display_row * VGA_WIDTH + display_col + col] = 0x2F00 | c;
}
display_row++;
}
print_string_with_attr("Press S to return to shell, Q to exit.", 21, 10, 0x2F);
while (1) {
unsigned char status, scancode;
asm volatile("inb $0x64, %0" : "=a"(status));
if (status & 0x01) {
asm volatile("inb $0x60, %0" : "=a"(scancode));
if (!(scancode & 0x80)) {
if (scancode == 0x1F) {
shell_index = 0;
for (int i = 0; i < 256; i++) {
shell_buffer[i] = 0;
}
clear_screen();
clear_shell();
display_shell_prompt();
shell_active = 1;
return;
}
if (scancode == 0x10) {
halt_system();
return;
}
}
}
for (volatile int i = 0; i < 10000; i++);
}
}
// Diary Note Function
void DiaryNote() {
if (!vfs_initialized) {
clear_screen();
print_string("VFS not initialized. Diary feature disabled.", 12, 10);
for (volatile int i = 0; i < 1000000; i++);
clear_screen();
display_shell_prompt();
shell_active = 1;
return;
}
unsigned short* vga = (unsigned short*)VGA_BUFFER;
const int rect_width = 60;
const int rect_height = 15;
const int rect_start_row = (VGA_HEIGHT - rect_height) / 2;
const int rect_start_col = (VGA_WIDTH - rect_width) / 2;
const int rect_end_row = rect_start_row + rect_height - 1;
const int rect_end_col = rect_start_col + rect_width - 1;
clear_screen();
for (int row = rect_start_row; row <= rect_end_row; row++) {
for (int col = rect_start_col; col <= rect_end_col; col++) {
vga[row * VGA_WIDTH + col] = 0x2F00 | ' ';
}
}
for (int col = rect_start_col; col <= rect_end_col; col++) {
vga[rect_start_row * VGA_WIDTH + col] = 0x2F00 | '-';
vga[rect_end_row * VGA_WIDTH + col] = 0x2F00 | '-';
}
for (int row = rect_start_row + 1; row < rect_end_row; row++) {
vga[row * VGA_WIDTH + rect_start_col] = 0x2F00 | '|';
vga[row * VGA_WIDTH + rect_end_col] = 0x2F00 | '|';
}
vga[rect_start_row * VGA_WIDTH + rect_start_col] = 0x2F00 | '+';
vga[rect_start_row * VGA_WIDTH + rect_end_col] = 0x2F00 | '+';
vga[rect_end_row * VGA_WIDTH + rect_start_col] = 0x2F00 | '+';
vga[rect_end_row * VGA_WIDTH + rect_end_col] = 0x2F00 | '+';
int text_row = rect_start_row + 1;
int text_col = rect_start_col + 2;
print_string_with_attr("Diary Note", text_row++, text_col, 0x2F);
print_string_with_attr("Press Enter to save, Esc to cancel.", text_row++, text_col, 0x2F);
print_string_with_attr("----------------------", text_row++, text_col, 0x2F);
print_string_with_attr("Tell me about your day?", text_row++, text_col, 0x2F);
for (int i = 0; i < 256; i++) {
diary_buffer[i] = 0;
}
diary_index = 0;
diary_active = 1;
}
// Virtual Memory Information Display
void display_vm_info() {
clear_screen();
print_string_with_attr("Sebria OS Virtual Memory Management", 2, 20, 0x0F);
print_string("Virtual Memory Status:", 4, 5);
print_string("Paging: Disabled", 6, 5);
print_string("Virtual File System (VFS):", 10, 5);
print_string("Status: Not initialized", 12, 5);
print_string("Files: ", 13, 5);
print_number(vfs.files, 13, 21);
print_string("Inodes Used: ", 14, 5);
print_number(vfs.inodes_used, 14, 21);
print_string("User-Space Processes:", 16, 5);
print_string("PID Name State Priority", 18, 5);
int row = 19;
for (int i = 0; i < MAX_PROCESSES; i++) {
if (processes[i].pid) {
char buf[64];
int pos = 0;
print_number(processes[i].pid, row, 5);
buf[pos++] = ' ';
buf[pos++] = ' ';
buf[pos++] = ' ';
buf[pos++] = ' ';
const char* name = processes[i].privilege == 0 ? "kernel" : "user";
for (int j = 0; name[j]; j++) {
buf[pos++] = name[j];
}
while (pos < 16) buf[pos++] = ' ';
const char* state = processes[i].state == 1 ? "Running" : processes[i].state == 0 ? "Ready" : "Terminated";
for (int j = 0; state[j]; j++) {
buf[pos++] = state[j];
}
while (pos < 24) buf[pos++] = ' ';
print_number(processes[i].priority, row, 24);
buf[pos] = 0;
print_string(buf, row++, 8);
}
}
print_string("Press any key to return to menu...", 22, 20);
unsigned char status, scancode;
while (1) {
asm volatile("inb $0x64, %0" : "=a"(status));
if (status & 0x01) {
asm volatile("inb $0x60, %0" : "=a"(scancode));
if (!(scancode & 0x80)) {
asm volatile("mov $0x20, %%al\n\tout %%al, $0x20" : : : "eax");
break;
}
}
}
clear_screen();
}
void append_to_log(const char* command) {
if (command[0] == 0) return;
int i = 0;
while (command[i] && log_index < 511) {
command_log[log_index++] = command[i++];
}
if (log_index < 511) {
command_log[log_index++] = '\n';
}
}
// Process Management
int create_process(void (*task)(), int priority, int privilege) {
for (int i = 0; i < MAX_PROCESSES; i++) {
if (!processes[i].pid) {
processes[i].task = task;
processes[i].state = 0;
processes[i].pid = i + 1;
processes[i].priority = priority;
processes[i].privilege = privilege;
processes[i].user_stack = privilege == 3 ? USER_BASE + PAGE_SIZE * 2 : 0;
processes[i].page_dir = privilege == 3 ? (unsigned int)create_user_page_dir() : (unsigned int)kernel_page_dir;
return i;
}
}
return -1;
}
void kill_process(int pid) {
for (int i = 0; i < MAX_PROCESSES; i++) {
if (processes[i].pid == pid) {
processes[i].state = 2;
processes[i].pid = 0;
if (i == current_process) {
schedule_flag = 1;
}
break;
}
}
}
void schedule() {
int next = -1;
int max_priority = -1;
for (int i = 0; i < MAX_PROCESSES; i++) {
if (processes[i].state == 0 && processes[i].priority > max_priority) {
max_priority = processes[i].priority;
next = i;
}
}
if (next != -1 && next != current_process) {
processes[current_process].state = 0;
current_process = next;
processes[current_process].state = 1;
// Skip page directory switch since paging is disabled
// asm volatile("mov %0, %%cr3" : : "r"(processes[current_process].page_dir) : "memory");
}
}
// System Call Handler
void syscall_handler() {
unsigned int syscall_num, arg1, arg2, arg3;
asm volatile(
"mov %%eax, %0\n\t"
"mov %%ebx, %1\n\t"
"mov %%ecx, %2\n\t"
"mov %%edx, %3"
: "=r"(syscall_num), "=r"(arg1), "=r"(arg2), "=r"(arg3)
);
int result = 0;
switch (syscall_num) {
case SYS_WRITE:
print_string((const char*)arg1, 15, 0);
break;
case SYS_OPEN:
result = vfs_open_file((const char*)arg1);
break;
case SYS_READ:
result = vfs_read_file(arg1, (char*)arg2, arg3);
break;
case SYS_CLOSE:
vfs_close_file(arg1);
break;
case SYS_CREATE:
result = vfs_create_file((const char*)arg1);
break;
case SYS_LS:
vfs_list_files((char*)arg1, (int*)arg2);
break;
case SYS_PS:
result = 0;
for (int i = 0; i < MAX_PROCESSES; i++) {
if (processes[i].pid) result++;
}
break;
case SYS_KILL:
kill_process(arg1);
break;
case SYS_EXIT:
kill_process(processes[current_process].pid);
break;
default:
print_string("Unknown syscall", 15, 0);
}
asm volatile("mov %0, %%eax" : : "r"(result) : "eax");
asm volatile("mov $0x20, %%al\n\tout %%al, $0x20" : : : "eax");
}
// Interrupt Handlers
void default_handler() {
unsigned short* vga = (unsigned short*)VGA_BUFFER;
vga[2] = 0x4F44; // 'D'
while (1);
}
void double_fault_handler() {
unsigned short* vga = (unsigned short*)VGA_BUFFER;
vga[4] = 0x4F46; // 'F'
while (1);
}
void timer_handler() {
unsigned short* vga = (unsigned short*)VGA_BUFFER;
vga[6] = 0x4F54; // 'T'
schedule_flag = 1;
asm volatile("mov $0x20, %%al\n\tout %%al, $0x20" : : : "eax");
}
void keyboard_handler() {
static const char scancode_to_ascii[] = {
0, 0, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 0,
0, 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',
0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', 0,
'\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 0, 0, 0,
' ', 0
};
unsigned char scancode;
asm volatile("inb $0x60, %0" : "=a"(scancode));
unsigned short* vga = (unsigned short*)VGA_BUFFER;
vga[0] = 0x4F4B; // 'K'
if (scancode & 0x80) { // Key release
asm volatile("mov $0x20, %%al\n\tout %%al, $0x20" : : : "eax");
return;
}
print_hex_byte(scancode, 0, 2);
char c = (scancode < sizeof(scancode_to_ascii) && scancode_to_ascii[scancode]) ? scancode_to_ascii[scancode] : 0;
if (file_write_active) {
const int rect_width = 60;
const int rect_height = 15;
const int rect_start_row = (VGA_HEIGHT - rect_height) / 2;
const int rect_start_col = (VGA_WIDTH - rect_width) / 2;
const int text_row_start = rect_start_row + 5;
const int text_col = rect_start_col + 2;
if (scancode == 0x0E && file_write_index > 0) { // Backspace
file_write_index--;
file_write_buffer[file_write_index] = 0;
redraw_write_buffer(text_row_start, text_col, rect_width, rect_height);
} else if (scancode == 0x1C) { // Enter - Save
file_write_buffer[file_write_index] = 0;
if (file_write_index > 0 && current_file_fd >= 0) {
int bytes_written = vfs_write_file(current_file_fd, file_write_buffer, file_write_index);
print_string("Bytes written: ", 18, 0);
print_number(bytes_written, 18, 15);
vfs_close_file(current_file_fd);
current_file_fd = -1;
} else {
print_string("No data or invalid fd", 18, 0);
if (current_file_fd >= 0) {
vfs_close_file(current_file_fd);
current_file_fd = -1;
}
}
file_write_active = 0;
clear_screen();
clear_shell();
display_shell_prompt();
shell_active = 1;
} else if (scancode == 0x01) { // Esc - Cancel
if (current_file_fd >= 0) {
vfs_close_file(current_file_fd);
current_file_fd = -1;
}
file_write_active = 0;
clear_screen();
clear_shell();
display_shell_prompt();
shell_active = 1;
} else if (c && c >= 32 && c <= 126 && file_write_index < FILE_WRITE_MAX - 1) {
file_write_buffer[file_write_index++] = c;
file_write_buffer[file_write_index] = 0;
redraw_write_buffer(text_row_start, text_col, rect_width, rect_height);
}
asm volatile("mov $0x20, %%al\n\tout %%al, $0x20" : : : "eax");
return;
}