-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsyscall.c
More file actions
1178 lines (972 loc) · 34.6 KB
/
Copy pathsyscall.c
File metadata and controls
1178 lines (972 loc) · 34.6 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
#include <stdint.h>
#include <stddef.h>
#include "kernel/isr.h"
#include "drivers/terminal.h"
#include "kernel/process.h"
#include "kernel/scheduler.h"
#include "drivers/timer.h"
#include "drivers/keyboard.h"
#include "mm/kmalloc.h"
#include "mm/vmm.h"
#include "lib/string.h"
#include "mm/pmm.h"
#include "fs/fs.h"
#include "ipc/pipe.h"
#include "ipc/signal.h"
#include "lib/elf.h"
// System call numbers
#define SYS_EXIT 1
#define SYS_WRITE 2
#define SYS_READ 3
#define SYS_GETPID 4
#define SYS_SLEEP 5
#define SYS_SBRK 6
#define SYS_FORK 7
#define SYS_WAIT 8
#define SYS_EXECVE 9
#define SYS_PS 10
#define SYS_OPEN 11
#define SYS_CLOSE 12
#define SYS_STAT 13
#define SYS_MKDIR 14
#define SYS_READDIR 15
#define SYS_KILL 16
#define SYS_PIPE 17
#define SYS_DUP2 18
#define SYS_SIGNAL 19
#define SYS_SIGRETURN 20
#define SYS_SETFG 21
#define SYS_CHDIR 22
#define SYS_GETCWD 23
// File descriptors
#define STDIN 0
#define STDOUT 1
#define STDERR 2
#define O_CREAT 0x1
#define O_APPEND 0x2
// Maximum number of system calls
#define MAX_SYSCALLS 64
// System call function type (32-bit)
typedef uint32_t (*syscall_func_t)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t);
// System call table
static syscall_func_t syscall_table[MAX_SYSCALLS];
// File descriptor table (per-process)
#define MAX_FDS 16
typedef struct {
fs_node_t* node;
pipe_t* pipe;
uint32_t offset;
int flags;
int is_pipe;
} fd_entry_t;
// Forward declarations
static fd_entry_t* get_fd_table(void);
static void string_concat(char* dest, const char* src);
static void int_to_string(uint32_t num, char* buf);
extern void interrupt_return_trampoline(void);
static uint32_t sys_fork_with_regs(registers_t* regs);
static uint32_t sys_execve_with_regs(uint32_t path_ptr, uint32_t argv_ptr, uint32_t envp_ptr,
uint32_t arg4, uint32_t arg5, registers_t* regs);
static int is_user_process(process_t* process) {
return process && process->kind == PROCESS_KIND_USER;
}
static int validate_user_buffer(process_t* process, uint32_t addr, size_t len, bool write) {
if (!is_user_process(process) || len == 0) {
return 0;
}
if (addr < USER_VADDR_MIN || addr >= KERNEL_BASE) {
return -1;
}
if (addr + len < addr || addr + len > KERNEL_BASE) {
return -1;
}
uint32_t start = addr & ~(PAGE_SIZE - 1);
uint32_t end = (addr + len - 1) & ~(PAGE_SIZE - 1);
for (uint32_t page = start; page <= end; page += PAGE_SIZE) {
uint32_t flags = vmm_get_page_flags(process->page_directory, page);
if (!(flags & PAGE_PRESENT) || !(flags & PAGE_USER)) {
return -1;
}
if (write && !(flags & PAGE_WRITABLE)) {
return -1;
}
}
return 0;
}
static int copy_from_user(void* dst, uint32_t src, size_t len) {
process_t* current = process_get_current();
if (validate_user_buffer(current, src, len, false) < 0) {
return -1;
}
memcpy(dst, (const void*)src, len);
return 0;
}
static int copy_to_user(uint32_t dst, const void* src, size_t len) {
process_t* current = process_get_current();
if (validate_user_buffer(current, dst, len, true) < 0) {
return -1;
}
memcpy((void*)dst, src, len);
return 0;
}
static int copy_string_from_user(char* dst, size_t dst_len, uint32_t src) {
process_t* current = process_get_current();
size_t i;
if (dst_len == 0) {
return -1;
}
if (!is_user_process(current)) {
strncpy(dst, (const char*)src, dst_len - 1);
dst[dst_len - 1] = '\0';
return 0;
}
for (i = 0; i < dst_len - 1; i++) {
if (validate_user_buffer(current, src + i, 1, false) < 0) {
return -1;
}
dst[i] = ((const char*)src)[i];
if (dst[i] == '\0') {
return 0;
}
}
dst[dst_len - 1] = '\0';
return -1;
}
// sys_exit: Terminate current process
static uint32_t sys_exit(uint32_t status, uint32_t arg2, uint32_t arg3, uint32_t arg4, uint32_t arg5) {
(void)arg2; (void)arg3; (void)arg4; (void)arg5;
process_t* current = process_get_current();
if (!current) {
return 0;
}
process_exit((int)status);
return (uint32_t)-1;
}
// sys_write: Write to file descriptor
static uint32_t sys_write(uint32_t fd, uint32_t buf_ptr, uint32_t count, uint32_t arg4, uint32_t arg5) {
(void)arg4; (void)arg5;
process_t* current = process_get_current();
if (buf_ptr == 0 || count == 0) {
return 0;
}
if (validate_user_buffer(current, buf_ptr, count, false) < 0) {
return (uint32_t)-1;
}
const char* buf = (const char*)buf_ptr;
if (fd == STDOUT || fd == STDERR) {
for (size_t i = 0; i < count; i++) {
terminal_putchar(buf[i]);
}
return count;
}
fd_entry_t* fd_table = get_fd_table();
if (fd_table && fd < MAX_FDS) {
if (fd_table[fd].is_pipe && fd_table[fd].pipe) {
return pipe_write(fd_table[fd].pipe, buf, count);
} else if (fd_table[fd].node) {
fs_node_t* node = fd_table[fd].node;
int written = fs_write(node, fd_table[fd].offset, count, (uint8_t*)buf);
if (written > 0) {
fd_table[fd].offset += written;
}
return written;
}
}
return (uint32_t)-1;
}
// sys_read: Read from file descriptor
static uint32_t sys_read(uint32_t fd, uint32_t buf_ptr, uint32_t count, uint32_t arg4, uint32_t arg5) {
(void)arg4; (void)arg5;
process_t* current = process_get_current();
if (buf_ptr == 0 || count == 0) {
return 0;
}
if (validate_user_buffer(current, buf_ptr, count, true) < 0) {
return (uint32_t)-1;
}
char* buf = (char*)buf_ptr;
if (fd == STDIN) {
size_t read = 0;
while (read < count) {
if (keyboard_has_char()) {
char c = keyboard_getchar();
buf[read++] = c;
if (c == '\n') {
break;
}
} else {
process_yield();
}
}
return read;
}
fd_entry_t* fd_table = get_fd_table();
if (fd_table && fd < MAX_FDS) {
if (fd_table[fd].is_pipe && fd_table[fd].pipe) {
return pipe_read(fd_table[fd].pipe, buf, count);
} else if (fd_table[fd].node) {
fs_node_t* node = fd_table[fd].node;
int bytes_read = fs_read(node, fd_table[fd].offset, count, (uint8_t*)buf);
if (bytes_read > 0) {
fd_table[fd].offset += bytes_read;
}
return bytes_read;
}
}
return (uint32_t)-1;
}
// sys_getpid: Get current process ID
static uint32_t sys_getpid(uint32_t arg1, uint32_t arg2, uint32_t arg3, uint32_t arg4, uint32_t arg5) {
(void)arg1; (void)arg2; (void)arg3; (void)arg4; (void)arg5;
return process_get_pid();
}
// sys_sleep: Sleep for milliseconds
static uint32_t sys_sleep(uint32_t ms, uint32_t arg2, uint32_t arg3, uint32_t arg4, uint32_t arg5) {
(void)arg2; (void)arg3; (void)arg4; (void)arg5;
sleep_ms(ms);
return 0;
}
// sys_sbrk: Extend data segment
static uint32_t sys_sbrk(uint32_t increment, uint32_t arg2, uint32_t arg3, uint32_t arg4, uint32_t arg5) {
(void)arg2; (void)arg3; (void)arg4; (void)arg5;
process_t* current = process_get_current();
if (!current) {
return (uint32_t)-1;
}
if (!is_user_process(current)) {
return (uint32_t)-1;
}
uint32_t old_heap = current->heap_current;
if (increment == 0) {
return old_heap;
}
int32_t signed_inc = (int32_t)increment;
uint32_t new_heap;
// Check for overflow/underflow before computing
if (signed_inc > 0) {
if (old_heap > (uint32_t)0xFFFFFFFF - (uint32_t)signed_inc) {
return (uint32_t)-1;
}
new_heap = old_heap + (uint32_t)signed_inc;
} else {
uint32_t abs_dec = (uint32_t)(-signed_inc);
if (abs_dec > old_heap) {
return (uint32_t)-1;
}
new_heap = old_heap - abs_dec;
}
if (new_heap > current->heap_max) {
return (uint32_t)-1;
}
if (new_heap < current->heap_start) {
return (uint32_t)-1;
}
if ((int32_t)increment > 0) {
uint32_t old_page = current->heap_current & ~(PAGE_SIZE - 1);
uint32_t new_page = (new_heap + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
for (uint32_t page = old_page; page < new_page; page += PAGE_SIZE) {
if (page >= current->heap_current) {
if (vmm_alloc_user_pages(current, page, 1) < 0) {
// Roll back the pages already mapped in this call so a
// partial failure does not leak frames or leave the heap
// half-grown. Free exactly what we allocated above.
for (uint32_t p = old_page; p < page; p += PAGE_SIZE) {
if (p >= current->heap_current) {
uint32_t phys = vmm_get_physical(current->page_directory, p) & ~0xFFF;
vmm_unmap_page(current->page_directory, p);
if (phys) {
pmm_free_page((void*)phys);
}
if (current->pages_allocated > 0) {
current->pages_allocated--;
}
}
}
return (uint32_t)-1;
}
}
}
}
current->heap_current = new_heap;
return old_heap;
}
// sys_fork: Create a child process
static uint32_t sys_fork(uint32_t arg1, uint32_t arg2, uint32_t arg3, uint32_t arg4, uint32_t arg5) {
(void)arg1; (void)arg2; (void)arg3; (void)arg4; (void)arg5;
return (uint32_t)-1;
}
static uint32_t sys_fork_with_regs(registers_t* regs) {
process_t* parent = process_get_current();
if (!parent) {
return (uint32_t)-1;
}
if (!regs || !is_user_process(parent) || (regs->cs & 0x3) != 0x3) {
return (uint32_t)-1;
}
process_t* child = allocate_process_struct();
if (!child) {
return (uint32_t)-1;
}
strncpy(child->name, parent->name, 24);
string_concat(child->name, "[child]");
child->page_directory = vmm_clone_address_space(parent->page_directory);
if (!child->page_directory) {
free_process_struct(child);
return (uint32_t)-1;
}
child->context = parent->context;
child->kind = parent->kind;
child->parent_pid = parent->pid;
child->state = PROCESS_STATE_READY;
child->priority = parent->priority;
child->ticks_remaining = DEFAULT_QUANTUM;
child->ticks_total = 0;
child->heap_start = parent->heap_start;
child->heap_current = parent->heap_current;
child->heap_max = parent->heap_max;
child->stack_bottom = parent->stack_bottom;
child->stack_top = parent->stack_top;
child->pages_allocated = parent->pages_allocated;
child->page_faults = 0;
child->pending_signals = 0;
child->signal_mask = parent->signal_mask;
for (int i = 0; i < 32; i++) {
child->signal_handlers[i] = parent->signal_handlers[i];
}
memcpy(child->cwd, parent->cwd, sizeof(child->cwd)); // inherit working dir
if (parent->fd_table && child->fd_table) {
fd_entry_t* parent_fds = (fd_entry_t*)parent->fd_table;
fd_entry_t* child_fds = (fd_entry_t*)child->fd_table;
for (int i = 0; i < MAX_FDS; i++) {
child_fds[i] = parent_fds[i];
if (child_fds[i].is_pipe && child_fds[i].pipe) {
pipe_add_ref(child_fds[i].pipe, child_fds[i].flags);
}
}
}
child->user_entry = parent->user_entry;
registers_t* child_regs =
(registers_t*)((uint8_t*)child->kernel_stack + KERNEL_STACK_SIZE - sizeof(registers_t));
*child_regs = *regs;
child_regs->eax = 0;
child->context.edi = 0;
child->context.esi = 0;
child->context.ebx = 0;
child->context.ebp = 0;
child->context.esp = (uint32_t)child_regs;
child->context.eip = (uint32_t)interrupt_return_trampoline;
child->context.eflags = regs->eflags;
ready_queue_push(child);
return child->pid;
}
// sys_wait: Wait for child process to exit
static uint32_t sys_wait(uint32_t status_ptr, uint32_t arg2, uint32_t arg3, uint32_t arg4, uint32_t arg5) {
(void)arg2; (void)arg3; (void)arg4; (void)arg5;
process_t* parent = process_get_current();
if (!parent) {
return (uint32_t)-1;
}
while (1) {
process_t* child = find_zombie_child(parent->pid);
if (child) {
if (status_ptr) {
int status = child->exit_status;
if (copy_to_user(status_ptr, &status, sizeof(status)) < 0) {
if (!is_user_process(parent)) {
*(int*)status_ptr = status;
} else {
return (uint32_t)-1;
}
}
}
uint32_t child_pid = child->pid;
free_process_struct(child);
return child_pid;
}
// No zombie yet. If we have no children at all, there is nothing to
// wait for: return ECHILD (-1) instead of blocking forever.
if (!process_has_children(parent->pid)) {
return (uint32_t)-1;
}
parent->state = PROCESS_STATE_WAITING;
schedule();
}
}
static uint32_t sys_execve(uint32_t path_ptr, uint32_t argv_ptr, uint32_t envp_ptr, uint32_t arg4, uint32_t arg5) {
(void)path_ptr; (void)argv_ptr; (void)envp_ptr; (void)arg4; (void)arg5;
return (uint32_t)-1;
}
// Maximum argv entries and total string data for execve
#define EXEC_MAX_ARGC 16
#define EXEC_MAX_STRINGS 2048
static uint32_t sys_execve_with_regs(uint32_t path_ptr, uint32_t argv_ptr, uint32_t envp_ptr,
uint32_t arg4, uint32_t arg5, registers_t* regs) {
(void)envp_ptr; (void)arg4; (void)arg5;
process_t* current = process_get_current();
char path[128];
fs_node_t* node;
uint8_t* image = NULL;
process_t new_image;
uint32_t* old_pd;
// Kernel buffer for argv strings (copied before address space switch)
char argv_buf[EXEC_MAX_STRINGS];
int argv_offsets[EXEC_MAX_ARGC]; // offset into argv_buf for each arg
int argv_lens[EXEC_MAX_ARGC]; // length including null terminator
int exec_argc = 0;
int argv_total = 0;
if (!current || !regs || !is_user_process(current) || path_ptr == 0) {
return (uint32_t)-1;
}
if (copy_string_from_user(path, sizeof(path), path_ptr) < 0) {
return (uint32_t)-1;
}
// Copy argv strings from caller's address space into kernel buffer
if (argv_ptr) {
for (int i = 0; i < EXEC_MAX_ARGC; i++) {
uint32_t str_ptr;
if (copy_from_user(&str_ptr, argv_ptr + i * sizeof(uint32_t), sizeof(uint32_t)) < 0) {
break;
}
if (str_ptr == 0) break; // NULL terminator
char arg[256];
if (copy_string_from_user(arg, sizeof(arg), str_ptr) < 0) {
break;
}
int len = 0;
while (arg[len]) len++;
len++; // include null terminator
if (argv_total + len > EXEC_MAX_STRINGS) break;
argv_offsets[exec_argc] = argv_total;
argv_lens[exec_argc] = len;
memcpy(argv_buf + argv_total, arg, len);
argv_total += len;
exec_argc++;
}
}
terminal_writestring("[EXEC] Executing: ");
terminal_writestring(path);
terminal_writestring("\n");
node = fs_resolve_path(path);
if (!node || node->type != FS_TYPE_FILE || node->size == 0) {
terminal_writestring("[EXEC] Program not found\n");
return (uint32_t)-1;
}
image = (uint8_t*)kmalloc(node->size);
if (!image) {
return (uint32_t)-1;
}
if (fs_read(node, 0, node->size, image) != (int)node->size) {
kfree(image);
return (uint32_t)-1;
}
memset(&new_image, 0, sizeof(new_image));
new_image.kind = PROCESS_KIND_USER;
new_image.page_directory = vmm_create_address_space();
if (!new_image.page_directory) {
kfree(image);
return (uint32_t)-1;
}
if (vmm_setup_user_stack(&new_image) < 0 ||
vmm_setup_user_heap(&new_image) < 0 ||
elf_load(&new_image, image, node->size) < 0) {
vmm_destroy_address_space(new_image.page_directory);
kfree(image);
return (uint32_t)-1;
}
old_pd = current->page_directory;
current->page_directory = new_image.page_directory;
current->heap_start = new_image.heap_start;
current->heap_current = new_image.heap_current;
current->heap_max = new_image.heap_max;
current->stack_bottom = new_image.stack_bottom;
current->stack_top = new_image.stack_top;
current->pages_allocated = new_image.pages_allocated;
current->page_faults = 0;
current->user_entry = new_image.user_entry;
vmm_switch_address_space(current->page_directory);
vmm_destroy_address_space(old_pd);
kfree(image);
// Set up user stack with argc/argv
// Layout (growing downward):
// [string data] <- arg strings stored here
// argv[argc] = NULL
// argv[1] pointer
// argv[0] pointer
// argc <- ESP points here
uint32_t sp = current->stack_top;
// Pre-check: ensure argv data fits within the user stack
// Need: argv_total bytes for strings + (exec_argc+1)*4 for pointers + 8 for argc/argv_base
uint32_t needed = (uint32_t)argv_total + ((uint32_t)exec_argc + 1) * 4 + 8 + 4; // +4 for alignment slack
if (needed > sp - current->stack_bottom) {
// Not enough stack space for argv — fail the exec
return (uint32_t)-1;
}
// Copy arg strings onto user stack and record their user-space addresses
uint32_t str_addrs[EXEC_MAX_ARGC];
for (int i = exec_argc - 1; i >= 0; i--) {
sp -= argv_lens[i];
memcpy((void*)sp, argv_buf + argv_offsets[i], argv_lens[i]);
str_addrs[i] = sp;
}
// Align to 4 bytes
sp &= ~3;
// Push NULL terminator for argv
sp -= 4;
*(uint32_t*)sp = 0;
// Push argv pointers (reverse order so argv[0] is at lowest address)
for (int i = exec_argc - 1; i >= 0; i--) {
sp -= 4;
*(uint32_t*)sp = str_addrs[i];
}
// Save argv pointer (points to argv[0])
uint32_t argv_base = sp;
// Push argv pointer
sp -= 4;
*(uint32_t*)sp = argv_base;
// Push argc
sp -= 4;
*(uint32_t*)sp = (uint32_t)exec_argc;
regs->eip = current->user_entry;
regs->esp = sp;
regs->eax = 0;
return 0;
}
// sys_ps: List processes
static uint32_t sys_ps(uint32_t arg1, uint32_t arg2, uint32_t arg3, uint32_t arg4, uint32_t arg5) {
(void)arg1; (void)arg2; (void)arg3; (void)arg4; (void)arg5;
terminal_writestring("PID PPID STATE NAME\n");
terminal_writestring("--- ---- -------- ----------\n");
extern process_t* process_table[];
for (int i = 0; i < MAX_PROCESSES; i++) {
process_t* p = process_table[i];
if (p) {
const char* state_str = "UNKNOWN";
switch (p->state) {
case PROCESS_STATE_READY: state_str = "READY"; break;
case PROCESS_STATE_RUNNING: state_str = "RUN"; break;
case PROCESS_STATE_BLOCKED: state_str = "BLOCK"; break;
case PROCESS_STATE_WAITING: state_str = "WAIT"; break;
case PROCESS_STATE_ZOMBIE: state_str = "ZOMBIE"; break;
case PROCESS_STATE_TERMINATED: state_str = "TERM"; break;
}
char pid_str[16];
int_to_string(p->pid, pid_str);
terminal_writestring(pid_str);
terminal_writestring(" ");
char ppid_str[16];
int_to_string(p->parent_pid, ppid_str);
terminal_writestring(ppid_str);
terminal_writestring(" ");
terminal_writestring(state_str);
int state_len = 0;
while (state_str[state_len]) state_len++;
for (int j = state_len; j < 8; j++) {
terminal_writestring(" ");
}
terminal_writestring(" ");
terminal_writestring(p->name);
terminal_writestring("\n");
}
}
return 0;
}
static void int_to_string(uint32_t num, char* buf) {
if (num == 0) {
buf[0] = '0';
buf[1] = '\0';
return;
}
uint32_t temp = num;
int digits = 0;
while (temp > 0) {
digits++;
temp /= 10;
}
buf[digits] = '\0';
for (int j = digits - 1; j >= 0; j--) {
buf[j] = '0' + (num % 10);
num /= 10;
}
}
static void string_concat(char* dest, const char* src) {
while (*dest) dest++;
while (*src) {
*dest++ = *src++;
}
*dest = '\0';
}
// Get current process's fd table
static fd_entry_t* get_fd_table(void) {
process_t* current = process_get_current();
if (!current || !current->fd_table) {
return NULL;
}
return (fd_entry_t*)current->fd_table;
}
// Initialize fd table for a process
void init_process_fd_table(process_t* proc) {
if (!proc) return;
proc->fd_table = kmalloc(sizeof(fd_entry_t) * MAX_FDS);
if (!proc->fd_table) return;
fd_entry_t* fds = (fd_entry_t*)proc->fd_table;
for (int i = 0; i < MAX_FDS; i++) {
fds[i].node = NULL;
fds[i].pipe = NULL;
fds[i].offset = 0;
fds[i].flags = 0;
fds[i].is_pipe = 0;
}
}
// Close a single fd entry, releasing its resources
static void close_fd_entry(fd_entry_t* entry) {
if (entry->is_pipe && entry->pipe) {
// flags encodes the pipe end: 0 = read, 1 = write
pipe_close_ref(entry->pipe, entry->flags);
} else if (entry->node) {
fs_close(entry->node);
}
entry->node = NULL;
entry->pipe = NULL;
entry->offset = 0;
entry->flags = 0;
entry->is_pipe = 0;
}
// Close all open file descriptors for a process (called during destroy/free)
void cleanup_process_fds(process_t* proc) {
if (!proc || !proc->fd_table) return;
fd_entry_t* fds = (fd_entry_t*)proc->fd_table;
for (int i = 0; i < MAX_FDS; i++) {
if (fds[i].node || fds[i].pipe) {
close_fd_entry(&fds[i]);
}
}
}
// Resolve a (possibly relative) path against a working directory, normalizing
// "." and ".." components. Absolute inputs pass through unchanged except for
// normalization, so existing absolute-path behavior is preserved.
static void resolve_against_cwd(const char* cwd, const char* in, char* out, int outsz) {
char raw[256];
int r = 0;
if (in[0] == '/') {
for (int i = 0; in[i] && r < (int)sizeof(raw) - 1; i++) raw[r++] = in[i];
} else {
for (int i = 0; cwd[i] && r < (int)sizeof(raw) - 1; i++) raw[r++] = cwd[i];
if (r == 0 || raw[r - 1] != '/') {
if (r < (int)sizeof(raw) - 1) raw[r++] = '/';
}
for (int i = 0; in[i] && r < (int)sizeof(raw) - 1; i++) raw[r++] = in[i];
}
raw[r] = '\0';
int op = 0;
int i = 0;
while (raw[i]) {
while (raw[i] == '/') i++;
if (!raw[i]) break;
char comp[64];
int c = 0;
while (raw[i] && raw[i] != '/' && c < (int)sizeof(comp) - 1) comp[c++] = raw[i++];
comp[c] = '\0';
if (comp[0] == '.' && comp[1] == '\0') {
continue; // current dir
} else if (comp[0] == '.' && comp[1] == '.' && comp[2] == '\0') {
while (op > 0 && out[op - 1] != '/') op--; // drop last component
if (op > 0) op--; // and its leading slash
} else {
if (op < outsz - 1) out[op++] = '/';
for (int k = 0; comp[k] && op < outsz - 1; k++) out[op++] = comp[k];
}
}
if (op == 0) out[op++] = '/';
out[op] = '\0';
}
// sys_chdir: change the calling process's working directory
static uint32_t sys_chdir(uint32_t path_ptr, uint32_t arg2, uint32_t arg3, uint32_t arg4, uint32_t arg5) {
(void)arg2; (void)arg3; (void)arg4; (void)arg5;
process_t* cur = process_get_current();
if (!cur || !path_ptr) return (uint32_t)-1;
char path[128];
if (copy_string_from_user(path, sizeof(path), path_ptr) < 0) return (uint32_t)-1;
char resolved[128];
resolve_against_cwd(cur->cwd, path, resolved, sizeof(resolved));
fs_node_t* node = fs_resolve_path(resolved);
if (!node || node->type != FS_TYPE_DIR) return (uint32_t)-1;
int i = 0;
for (; resolved[i] && i < (int)sizeof(cur->cwd) - 1; i++) cur->cwd[i] = resolved[i];
cur->cwd[i] = '\0';
return 0;
}
// sys_getcwd: copy the calling process's working directory into a user buffer
static uint32_t sys_getcwd(uint32_t buf_ptr, uint32_t size, uint32_t arg3, uint32_t arg4, uint32_t arg5) {
(void)arg3; (void)arg4; (void)arg5;
process_t* cur = process_get_current();
if (!cur || !buf_ptr) return (uint32_t)-1;
int len = 0;
while (cur->cwd[len]) len++;
if ((uint32_t)(len + 1) > size) return (uint32_t)-1;
if (copy_to_user(buf_ptr, cur->cwd, len + 1) < 0) {
if (!is_user_process(cur)) {
memcpy((void*)buf_ptr, cur->cwd, len + 1);
} else {
return (uint32_t)-1;
}
}
return (uint32_t)len;
}
// sys_open: Open a file
static uint32_t sys_open(uint32_t path_ptr, uint32_t flags, uint32_t mode, uint32_t arg4, uint32_t arg5) {
(void)mode; (void)arg4; (void)arg5;
char rawpath[128];
char path[128];
if (!path_ptr) return (uint32_t)-1;
if (copy_string_from_user(rawpath, sizeof(rawpath), path_ptr) < 0) return (uint32_t)-1;
process_t* cur = process_get_current();
resolve_against_cwd(cur ? cur->cwd : "/", rawpath, path, sizeof(path));
fd_entry_t* fd_table = get_fd_table();
if (!fd_table) return (uint32_t)-1;
int fd = -1;
for (int i = 3; i < MAX_FDS; i++) {
if (!fd_table[i].node && !fd_table[i].pipe) {
fd = i;
break;
}
}
if (fd == -1) return (uint32_t)-1;
fs_node_t* node = fs_resolve_path(path);
if (!node && (flags & O_CREAT)) {
node = ramfs_create_file_path(path);
if (!node) return (uint32_t)-1;
}
if (!node) return (uint32_t)-1;
fd_table[fd].node = node;
// O_APPEND starts the write cursor at end-of-file; a non-zero offset also
// tells ramfs_write not to truncate, so appends accumulate.
fd_table[fd].offset = (flags & O_APPEND) ? node->size : 0;
fd_table[fd].flags = flags;
fd_table[fd].is_pipe = 0;
return fd;
}
// sys_close: Close a file descriptor
static uint32_t sys_close(uint32_t fd, uint32_t arg2, uint32_t arg3, uint32_t arg4, uint32_t arg5) {
(void)arg2; (void)arg3; (void)arg4; (void)arg5;
fd_entry_t* fd_table = get_fd_table();
if (!fd_table || fd >= MAX_FDS) {
return (uint32_t)-1;
}
if (!fd_table[fd].node && !fd_table[fd].pipe) {
return (uint32_t)-1;
}
close_fd_entry(&fd_table[fd]);
return 0;
}
// sys_stat: Get file information
static uint32_t sys_stat(uint32_t path_ptr, uint32_t stat_ptr, uint32_t arg3, uint32_t arg4, uint32_t arg5) {
(void)arg3; (void)arg4; (void)arg5;
char path[128];
if (!path_ptr || !stat_ptr) return (uint32_t)-1;
if (copy_string_from_user(path, sizeof(path), path_ptr) < 0) return (uint32_t)-1;
struct stat {
uint32_t size;
uint32_t type;
} st;
fs_node_t* node = fs_resolve_path(path);
if (!node) return (uint32_t)-1;
st.size = node->size;
st.type = node->type;
if (copy_to_user(stat_ptr, &st, sizeof(st)) < 0) {
if (!is_user_process(process_get_current())) {
memcpy((void*)stat_ptr, &st, sizeof(st));
} else {
return (uint32_t)-1;
}
}
return 0;
}
// sys_mkdir: Create directory
static uint32_t sys_mkdir(uint32_t path_ptr, uint32_t mode, uint32_t arg3, uint32_t arg4, uint32_t arg5) {
(void)mode; (void)arg3; (void)arg4; (void)arg5;
char path[128];
if (!path_ptr) return (uint32_t)-1;
if (copy_string_from_user(path, sizeof(path), path_ptr) < 0) return (uint32_t)-1;
fs_node_t* dir = ramfs_create_dir_path(path);
if (!dir) return (uint32_t)-1;
return 0;
}
// sys_readdir: Read directory entries
static uint32_t sys_readdir(uint32_t fd, uint32_t dirent_ptr, uint32_t arg3, uint32_t arg4, uint32_t arg5) {
(void)arg3; (void)arg4; (void)arg5;
if (!dirent_ptr) return (uint32_t)-1;
fd_entry_t* fd_table = get_fd_table();
if (!fd_table || fd >= MAX_FDS || !fd_table[fd].node) {
return (uint32_t)-1;
}
fs_node_t* node = fd_table[fd].node;
if (node->type != FS_TYPE_DIR) {
return (uint32_t)-1;
}
struct dirent {
char name[32];
uint32_t type;
} de;
fs_dirent_t* entry = fs_readdir(node, fd_table[fd].offset);
if (!entry) return 0;
strncpy(de.name, entry->name, 31);
de.name[31] = '\0';
de.type = FS_TYPE_FILE;
if (copy_to_user(dirent_ptr, &de, sizeof(de)) < 0) {
if (!is_user_process(process_get_current())) {
memcpy((void*)dirent_ptr, &de, sizeof(de));
} else {
return (uint32_t)-1;
}
}
fd_table[fd].offset++;
return 1;
}
// syscall_kill: Send signal to process
static uint32_t syscall_kill(uint32_t pid, uint32_t sig, uint32_t arg3, uint32_t arg4, uint32_t arg5) {
(void)arg3; (void)arg4; (void)arg5;
extern void signal_send(int pid, int sig);