Skip to content

Commit 2afec56

Browse files
committed
POC fork()
1 parent 549cf6b commit 2afec56

File tree

9 files changed

+33
-29
lines changed

9 files changed

+33
-29
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ SOURCE_SNAPSHOT="\"$$(git rev-parse --short HEAD)$$(git diff --quiet || echo '_u
6161
## Integer is 4 bytes
6262

6363
# Debugging controller
64-
DEBUG?=
65-
ifdef DEBUG
64+
NODEBUG?=
65+
ifndef NODEBUG
6666
CC_DEBUG=-g
6767
NASM_DEBUG=-g
6868
LD_DEBUG=
@@ -102,7 +102,7 @@ binaries: $(bt_stage1) $(bt_stage2) $(kernel_core) $(rm_static)
102102
SECTOR_COUNT_BT_STAGE1 = 1
103103
SECTOR_COUNT_SHARED_LIBRARY = 1
104104
SECTOR_COUNT_BT_STAGE2 = 12
105-
SECTOR_COUNT_KERNEL = 54
105+
SECTOR_COUNT_KERNEL = 55
106106

107107
SECTOR_START_BT_STAGE1 = 0
108108
SECTOR_START_SHARED_LIBRARY = $(shell expr $(SECTOR_START_BT_STAGE1) + $(SECTOR_COUNT_BT_STAGE1) )

scripts/build_image.sh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ function verify_subimage_and_push() {
2525
echo "The ${subimage} size isn't divisible by 512" >&2
2626
exit 2
2727
fi
28-
if [ "$(( $fsize / 512 ))" -lt ${min_sectors:?} ]; then
29-
echo "The ${subimage} uses less sectors than ${min_sectors:?}" >&2
30-
exit 2
31-
fi
3228
if [ "$(( $fsize / 512 ))" -gt ${max_sectors:?} ]; then
3329
echo "The ${subimage} uses more sectors than ${max_sectors:?}" >&2
3430
exit 2

src/kernel/interrupts/exceptions.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ extern void _interrupt_handler_0x1F_exception();
3939

4040
void interrupt_handler_0x00_0x1F_exception(int id, int err_code, int ds, int ss, int ip, int cs, int eflag) {
4141
panic_screen_init();
42-
unsigned int abs_address =
4342
print_log("Hardware exception %d (0x%x) triggered", id, id);
44-
print_log(" Error Code: %x or %x", err_code);
43+
print_log(" Error Code: %x", err_code);
4544
print_log(" CS : %x (GDT entry)", cs);
4645
print_log(" DS : %x (GDT entry)", ds);
4746
print_log(" SS : %x (GDT entry)", ss);

src/kernel/interrupts/timer.asm

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,16 @@ global create_infant_process_irq0_stack
6363
mov ss, edx
6464
mov esp, esi
6565

66-
pop gs
67-
pop fs
68-
pop es
69-
pop ds
70-
; add esp, 16
71-
; mov eax, cs
72-
; add eax, 8
73-
; mov gs, eax
74-
; mov fs, eax
75-
; mov es, eax
76-
; mov ds, eax
66+
; pop gs
67+
; pop fs
68+
; pop es
69+
; pop ds
70+
add esp, 16
71+
mov eax, ss ; ss and ds should be same for MOST purpose. This will break something :(
72+
mov gs, eax
73+
mov fs, eax
74+
mov es, eax
75+
mov ds, eax
7776

7877

7978
mov [esp+36], ecx ; cs

src/kernel/process/allocation.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ int process_fork(unsigned int ppid) {
218218
nprocess->sp = process->sp;
219219
nprocess->ip = process->ip;
220220

221-
process->state = STATE_EXIT;
221+
// HERE
222+
// process->state = STATE_EXIT;
222223

223224
size_t size = min(
224225
memmgr_app_size(ppid),
@@ -234,7 +235,7 @@ int process_fork(unsigned int ppid) {
234235
0,
235236
size
236237
);
237-
print_log("copy success");
238+
// print_log("copy success");
238239
// PANIC(0, "A");
239240
return npid;
240241
}

src/kernel/process/process.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ int syscall_1_process_wait(int pid, int blocked_on_pid) {
4747
int syscall_1_process_fork(int user_pid, int op) {
4848
switch (op) {
4949
case SYSCALL_PROCESS_SUB_FORK_MARK_READY:
50+
// print_log("FORK REQUESTED:: %d", user_pid);
5051
return process_fork_mark_ready(user_pid);
5152
case SYSCALL_PROCESS_SUB_FORK_CHECK_READY:
5253
return process_fork_check_ready(user_pid);

src/kernel/process/scheduler.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ void process_scheduler_stash_state() {
3636
if(process->state == STATE_EXIT) {
3737
// process unallocate ready to be killed
3838
process->state = STATE_COLD;
39+
// print_log("process_killed: %d", id);
3940
} else if(process->state == STATE_RUNNING) {
4041
// process move running process to ready
4142
process->state = STATE_READY;
@@ -49,7 +50,11 @@ int process_scheduler_get_next_pid(int lastpid) {
4950
}
5051

5152
static void handle_fork(unsigned int ppid, struct Process *process) {
53+
// if(process->flagirq0_fork_ready>0 && ppid!=2) {
54+
// print_log("[ignore] handle fork for %d", ppid);
55+
// }
5256
if(process->flagirq0_fork_ready>0) {
57+
// print_log("handle fork for %d", ppid);
5358
int npid = process_fork(ppid);
5459
if(npid<0) {
5560
process->flagirq0_fork_ready = -1; // request failed;
@@ -76,15 +81,16 @@ void process_scheduler(int *_e_ip, int *_e_cs, int *_e_sp, int *_e_ss) {
7681

7782
struct Process *process = get_process(pid);
7883
process_scheduler_stash_state();
79-
handle_fork(pid, process);
8084

8185
if(process->state != STATE_COLD) {
8286
// if last process is still alive
8387
process->cs = e_cs;
8488
process->ip = e_ip;
8589
process->ss = e_ss;
8690
process->sp = e_sp;
91+
handle_fork(pid, process);
8792
}
93+
8894
// last process can be
8995
// - RUNNING
9096
// - BLOCK # TODO: implement
@@ -96,7 +102,8 @@ void process_scheduler(int *_e_ip, int *_e_cs, int *_e_sp, int *_e_ss) {
96102
}
97103

98104
if(pid != npid) {
99-
print_log("[process_scheduler] pid: %d -> %d", pid, npid);
105+
// print_log("[process_scheduler] pid: %d -> %d", pid, npid);
106+
// print_log("[process_scheduler] pid: %d -> %d, at eip: %x", pid, npid, e_ip);
100107
}
101108

102109
struct Process *nprocess = get_process(npid);

src/usr/lib/process.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,15 @@ int fork() {
7575
}
7676
// fork requested.
7777
// let's process scheduler fork the job.
78-
printf("calling yield\n");
78+
// printf("calling yield\n");
7979
yield();
80-
while(1);
81-
printf("yield over\n");
80+
// while(1);
81+
// exit(0);
82+
// while(1);
83+
// printf("yield over\n");
8284
// fork request should be complete by now.
8385
int status = SYSCALL_A2(SYSCALL_PROCESS, SYSCALL_PROCESS_SUB_FORK, SYSCALL_PROCESS_SUB_FORK_CHECK_READY);
84-
printf("fork_check_ready: %d\n", status);
86+
// printf("fork_check_ready: %d\n", status);
8587
if(status < 0) {
8688
// request either failed
8789
// or still waiting: it's a bad state, but we are going to let that slide.

src/usr/local/src/ls.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ void process() {
1414
}
1515

1616
int main(int argc,char *argv[]) {
17-
__asm__("CLI");
1817

1918
int p = fork();
2019
printf("fork() = %d\n", p);

0 commit comments

Comments
 (0)