Skip to content

Commit c7c243c

Browse files
Added an considerable multitasking code, fixed some minor bugs, clean up existing code.
1 parent 50b4e00 commit c7c243c

12 files changed

Lines changed: 88 additions & 59 deletions

File tree

disk.img

0 Bytes
Binary file not shown.

source/compile_commands.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,11 @@
269269
"file": "/home/pradosh/osdev/source/kernel/C/shell/commands/lspci.c",
270270
"command": "clang -std=gnu11 -ffreestanding -w -fno-common -fno-stack-protector -fno-stack-check -fno-lto -fno-builtin -fno-omit-frame-pointer -Og -g -fPIE -I includes -I src -m64 -march=x86-64 -mno-80387 -mno-mmx -mno-red-zone -mno-sse -mno-sse2 -MMD -MP -c /home/pradosh/osdev/source/kernel/C/shell/commands/lspci.c -o /dev/null"
271271
},
272+
{
273+
"directory": "/home/pradosh/osdev/source",
274+
"file": "/home/pradosh/osdev/source/kernel/C/shell/commands/tasks.c",
275+
"command": "clang -std=gnu11 -ffreestanding -w -fno-common -fno-stack-protector -fno-stack-check -fno-lto -fno-builtin -fno-omit-frame-pointer -Og -g -fPIE -I includes -I src -m64 -march=x86-64 -mno-80387 -mno-mmx -mno-red-zone -mno-sse -mno-sse2 -MMD -MP -c /home/pradosh/osdev/source/kernel/C/shell/commands/tasks.c -o /dev/null"
276+
},
272277
{
273278
"directory": "/home/pradosh/osdev/source",
274279
"file": "/home/pradosh/osdev/source/kernel/C/shell/commands/shutdown.c",
@@ -339,6 +344,11 @@
339344
"file": "/home/pradosh/osdev/source/kernel/C/algorithms/hashing.c",
340345
"command": "clang -std=gnu11 -ffreestanding -w -fno-common -fno-stack-protector -fno-stack-check -fno-lto -fno-builtin -fno-omit-frame-pointer -Og -g -fPIE -I includes -I src -m64 -march=x86-64 -mno-80387 -mno-mmx -mno-red-zone -mno-sse -mno-sse2 -MMD -MP -c /home/pradosh/osdev/source/kernel/C/algorithms/hashing.c -o /dev/null"
341346
},
347+
{
348+
"directory": "/home/pradosh/osdev/source",
349+
"file": "/home/pradosh/osdev/source/kernel/C/multitasking.c",
350+
"command": "clang -std=gnu11 -ffreestanding -w -fno-common -fno-stack-protector -fno-stack-check -fno-lto -fno-builtin -fno-omit-frame-pointer -Og -g -fPIE -I includes -I src -m64 -march=x86-64 -mno-80387 -mno-mmx -mno-red-zone -mno-sse -mno-sse2 -MMD -MP -c /home/pradosh/osdev/source/kernel/C/multitasking.c -o /dev/null"
351+
},
342352
{
343353
"directory": "/home/pradosh/osdev/source",
344354
"file": "/home/pradosh/osdev/source/kernel/C/debugger.c",

source/kernel/C/filesystems/fat16.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,8 @@ int fat16_read(fat16_file_t* f, uint8_t* out, uint32_t size) {
516516
f->fs->bs.sectors_per_cluster * 512;
517517
uint16_t cluster = f->entry.first_cluster;
518518

519+
printf("READ CALLED: pos=%u size=%u\n", f->pos, size);
520+
519521
if (cluster_size == 0 || cluster < 2)
520522
return 0;
521523

source/kernel/C/filesystems/vfs.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ int vfs_open(const char* path, int flags, vfs_file_t* out)
328328
if (vfs_resolve_mount(norm, &res) != 0)
329329
return -2;
330330

331+
printf("[OPEN] %s", norm);
331332

332333
if (res.mnt->type == FS_PROC) {
333334
strncpy(out->rel_path, res.rel_path, sizeof(out->rel_path));
@@ -378,6 +379,11 @@ int vfs_open(const char* path, int flags, vfs_file_t* out)
378379

379380
out->mnt = res.mnt;
380381
out->flags = flags;
382+
if (flags & VFS_APPEND) {
383+
out->f.fat16.pos = out->f.fat16.entry.filesize;
384+
} else {
385+
out->f.fat16.pos = 0;
386+
}
381387

382388
return 0;
383389
} else if (res.mnt->type == FS_FAT32) {
@@ -411,6 +417,12 @@ int vfs_open(const char* path, int flags, vfs_file_t* out)
411417

412418
out->mnt = res.mnt;
413419
out->flags = flags;
420+
421+
if (flags & VFS_APPEND) {
422+
out->f.fat32.pos = out->f.fat32.entry.file_size;
423+
} else {
424+
out->f.fat32.pos = 0;
425+
}
414426
return 0;
415427
}
416428

source/kernel/C/multitasking.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,11 +273,10 @@ static bool cursor_blink_task(uint32_t pid, uint64_t now_ticks, void* ctx, int*
273273
if (!next_toggle_tick)
274274
return false;
275275

276-
if (now_ticks < *next_toggle_tick)
277-
return false;
278-
279-
ft_ctx->cursor_enabled = !ft_ctx->cursor_enabled;
280-
*next_toggle_tick = now_ticks + 50;
276+
while (now_ticks >= *next_toggle_tick) {
277+
ft_ctx->cursor_enabled = !ft_ctx->cursor_enabled;
278+
*next_toggle_tick += 50;
279+
}
281280
return false;
282281
}
283282

source/kernel/C/pit.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ volatile int pit_ticks = 0;
1818
void process_pit(InterruptFrame* frame) {
1919
(void)frame;
2020
pit_ticks++;
21-
multitasking_on_pit_tick((uint64_t)pit_ticks);
2221
outb(0x20, 0x20); // Notify the PIC that we've handled the interrupt
2322
}
2423

@@ -34,6 +33,6 @@ void pit_sleep(uint32_t milliseconds) {
3433
uint32_t target_ticks = pit_ticks + (milliseconds / (1000 / pit_freq));
3534

3635
while (pit_ticks < target_ticks) {
37-
36+
asm volatile("hlt");
3837
}
3938
}

source/kernel/C/shell/commands/exec.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include <graphics.h>
1616
#include <tty.h>
1717
#include <keyboard.h>
18-
#include <multitasking.h>
1918

2019
int cmd_exec(int argc, char** argv)
2120
{
@@ -25,28 +24,30 @@ int cmd_exec(int argc, char** argv)
2524
return 1;
2625
}
2726

28-
user_task_spec_t spec;
29-
memset(&spec, 0, sizeof(spec));
30-
3127
const char* path = argv[1];
32-
spec.path = path;
3328

29+
const char* user_argv[34];
3430
int user_argc = 0;
35-
for (int i = 2; i < argc && user_argc < 31; ++i)
36-
spec.argv[user_argc++] = argv[i];
3731

38-
spec.argc = user_argc;
39-
spec.argv[user_argc] = NULL;
32+
// argv[0] should be the program name (basename)
33+
const char* basename = vfs_basename(path);
34+
// user_argv[user_argc++] = basename;
35+
36+
// copy actual arguments AFTER the program path
37+
for (int i = 2; i < argc && user_argc < 33; ++i)
38+
user_argv[user_argc++] = argv[i];
39+
40+
user_argv[user_argc] = NULL;
4041

4142
tty_flush_input();
4243
keyboard_flush_buffer();
4344

44-
uint32_t pid = multitasking_spawn_userland(path, &spec);
45-
if (pid == 0) {
46-
eprintf("exec: failed to create task");
45+
if (userland_exec(path, user_argc, user_argv, NULL) != 0) {
46+
keyboard_flush_buffer();
47+
eprintf("exec: failed to load ELF");
4748
return -1;
4849
}
4950

50-
printf("exec: scheduled '%s' as pid %u", path, pid);
51+
keyboard_flush_buffer();
5152
return 0;
5253
}

source/kernel/C/shell/commands/tasks.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33

44
static const char* state_name(task_state_t state) {
55
switch (state) {
6-
case TASK_STATE_READY: return "READY";
7-
case TASK_STATE_RUNNING: return "RUNNING";
8-
case TASK_STATE_EXITED: return "EXITED";
9-
default: return "UNKNOWN";
6+
case TASK_STATE_READY: return "ready";
7+
case TASK_STATE_RUNNING: return "running";
8+
case TASK_STATE_EXITED: return "exited";
9+
default: return "?";
1010
}
1111
}
1212

1313
static const char* type_name(task_type_t type) {
1414
switch (type) {
15-
case TASK_TYPE_KERNEL: return "KERNEL";
16-
case TASK_TYPE_USERLAND: return "USER";
15+
case TASK_TYPE_KERNEL: return "kernel";
16+
case TASK_TYPE_USERLAND: return "userland";
1717
default: return "?";
1818
}
1919
}
@@ -24,7 +24,7 @@ typedef struct {
2424

2525
static bool print_task_row(const task_info_t* info, void* ctx_ptr) {
2626
list_ctx_t* ctx = (list_ctx_t*)ctx_ptr;
27-
printf("%4u %-6s %-8s exit=%d runtime_ticks=%u %s",
27+
printf("%2u %8s %10s exit=%02d runtime_ticks=%02u %s",
2828
info->pid,
2929
type_name(info->type),
3030
state_name(info->state),
@@ -41,10 +41,10 @@ int cmd_tasks(int argc, char** argv) {
4141

4242
list_ctx_t ctx = {0};
4343

44-
printf("PID TYPE STATE DETAILS");
44+
printf("PID TYPE STATE DETAILS");
4545
multitasking_for_each_task(print_task_row, &ctx);
4646

47-
printf("total=%u active=%u",
47+
printf("\ntotal=%02u active=%02u",
4848
multitasking_count_tasks(),
4949
multitasking_count_running());
5050

source/kernel/C/stream.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,6 @@ void stream_init(void)
113113

114114
int stream_set_file(stream_t s, vfs_file_t* file)
115115
{
116-
fd_table_init();
117-
118116
streams[s].file = file;
119117
streams[s].index = s;
120118

@@ -182,7 +180,6 @@ void fd_table_init(void)
182180

183181
bool fd_valid(int fd)
184182
{
185-
fd_table_init();
186183
return fd >= 0 && fd < STREAM_MAX_FDS && fd_table[fd].used;
187184
}
188185

@@ -199,8 +196,6 @@ vfs_file_t* fd_get_file(int fd)
199196

200197
int fd_open(const char* path, int flags)
201198
{
202-
fd_table_init();
203-
204199
int fd = fd_alloc_slot();
205200
if (fd < 0)
206201
return -1;
@@ -224,8 +219,6 @@ int fd_open(const char* path, int flags)
224219

225220
int fd_close(int fd)
226221
{
227-
fd_table_init();
228-
229222
if (!fd_valid(fd))
230223
return -1;
231224

@@ -238,8 +231,6 @@ int fd_close(int fd)
238231

239232
int fd_dup2(int oldfd, int newfd)
240233
{
241-
fd_table_init();
242-
243234
if (!fd_valid(oldfd) || newfd < 0 || newfd >= STREAM_MAX_FDS)
244235
return -1;
245236

@@ -255,8 +246,6 @@ int fd_dup2(int oldfd, int newfd)
255246

256247
int fd_dup(int oldfd)
257248
{
258-
fd_table_init();
259-
260249
if (!fd_valid(oldfd))
261250
return -1;
262251

source/kernel/C/syscalls.c

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -508,8 +508,6 @@ static void fat32_short_name(const fat32_dir_entry_t* e, char* out, size_t out_s
508508
}
509509

510510
static int sys_getdents64(uint64_t fd, char* buf, uint64_t buflen) {
511-
fd_table_init();
512-
513511
if (!buf || buflen < sizeof(linux_dirent64_t))
514512
return -LINUX_EINVAL;
515513
if (!fd_valid((int)fd))
@@ -676,7 +674,6 @@ static int sys_getdents64(uint64_t fd, char* buf, uint64_t buflen) {
676674

677675
static int64 sys_open_common(int dirfd, const char* path, int flags, int mode) {
678676
(void)mode;
679-
fd_table_init();
680677

681678
if (path == NULL)
682679
return -LINUX_EINVAL;
@@ -696,8 +693,6 @@ static int64 sys_open_common(int dirfd, const char* path, int flags, int mode) {
696693
}
697694

698695
static int64 sys_close(uint64_t fd) {
699-
fd_table_init();
700-
701696
if (!fd_valid((int)fd))
702697
return -LINUX_EBADF;
703698

@@ -770,8 +765,6 @@ static int64 sys_statx(int dirfd, const char* path, int flags, unsigned int mask
770765
}
771766

772767
static int64 sys_read(uint64_t fd, char* buf, uint64_t count) {
773-
fd_table_init();
774-
775768
if (buf == NULL || count == 0)
776769
return 0;
777770

@@ -794,8 +787,6 @@ static int64 sys_read(uint64_t fd, char* buf, uint64_t count) {
794787
}
795788

796789
static int64 sys_write(uint64_t fd, const char* buf, uint64_t count) {
797-
fd_table_init();
798-
799790
if (buf == NULL || count == 0)
800791
return 0;
801792

@@ -874,7 +865,6 @@ static int64 sys_ioctl(uint64_t fd, uint64_t req, uint64_t arg) {
874865
}
875866

876867
static int64 sys_fcntl(uint64_t fd, uint64_t cmd, uint64_t arg) {
877-
fd_table_init();
878868
if (!fd_valid((int)fd))
879869
return -LINUX_EBADF;
880870

@@ -912,8 +902,6 @@ static int64 sys_access_common(int dirfd, const char* path, int mode) {
912902
}
913903

914904
static int64 sys_lseek(uint64_t fd, int64_t offset, uint64_t whence) {
915-
fd_table_init();
916-
917905
if (!fd_valid((int)fd))
918906
return -LINUX_EBADF;
919907

@@ -945,8 +933,6 @@ static int64 sys_lseek(uint64_t fd, int64_t offset, uint64_t whence) {
945933
}
946934

947935
static int64 sys_dup2(uint64_t oldfd, uint64_t newfd) {
948-
fd_table_init();
949-
950936
if (!fd_valid((int)oldfd))
951937
return -LINUX_EBADF;
952938

@@ -958,8 +944,6 @@ static int64 sys_dup2(uint64_t oldfd, uint64_t newfd) {
958944
}
959945

960946
static int64 sys_dup(uint64_t oldfd) {
961-
fd_table_init();
962-
963947
if (!fd_valid((int)oldfd))
964948
return -LINUX_EBADF;
965949

@@ -1177,6 +1161,21 @@ static int64 sys_futex(uint32_t* uaddr, int op, uint32_t val,
11771161
}
11781162
}
11791163

1164+
struct passwd {
1165+
char *pw_name;
1166+
int pw_uid;
1167+
};
1168+
1169+
struct passwd fake_root = {
1170+
.pw_name = "root",
1171+
.pw_uid = 0,
1172+
};
1173+
1174+
struct passwd* getpwuid(int uid) {
1175+
if (uid == 0)
1176+
return &fake_root;
1177+
return NULL;
1178+
}
11801179

11811180
// THIS IS FOR INTERRUPT 0X80
11821181
void int80_handler(InterruptFrame* frame)
@@ -1304,6 +1303,9 @@ uint64_t syscall_dispatch (
13041303
case LINUX_SYS_CHDIR:
13051304
return sys_chdir((const char*)arg1);
13061305

1306+
case LINUX_SYS_FORK:
1307+
return 0;
1308+
13071309
case LINUX_SYS_UNAME:
13081310
return sys_uname((linux_utsname_t*)arg1);
13091311

0 commit comments

Comments
 (0)