I've added fork() to a7out after adding some comments to src/cmd/init.s.

There is a test program to demonstrate fork working.
This commit is contained in:
Warren Toomey
2016-03-03 09:23:48 +10:00
parent 644bee06a0
commit 6547aa98be
3 changed files with 105 additions and 62 deletions
+62 -61
View File
@@ -2,46 +2,46 @@
-1 -1
sys intrp sys intrp
jms init1 jms init1 " Fork the first child connected to ttyin/ttyout
jms init2 jms init2 " Fork the second child connected to keyboard/display
1: 1:
sys rmes sys rmes " Wait for a child to exit
sad pid1 sad pid1
jmp 1f jmp 1f " It was child 1, so jump to 1f and restart it
sad pid2 sad pid2
jms init2 jms init2 " It was child 2, so restart it
jmp 1 jmp 1 " and loop back. XXX: weird use of 1: not 1b. I don't like it!
1: 1:
jms init1 jms init1
jmp 1 jmp 1 " Weird use of 1: not 1b. I don't like it!
init1: 0 init1: 0
sys fork sys fork " Fork a child process
jmp 1f jmp 1f
sys open; ttyin; 0 sys open; ttyin; 0 " which opens the ttyin
sys open; ttyout; 1 sys open; ttyout; 1 " and ttyout files, and
jmp login jmp login " waits for a user to log in
1: 1:
dac pid1 dac pid1 " Parent stores childs pid in pid1
jmp init1 i jmp init1 i " and returns
init2: 0 init2: 0
sys fork sys fork " Fork a child process
jmp 1f jmp 1f
sys open; keybd; 0 sys open; keybd; 0 " which opens the keyboard
sys open; displ; 1 sys open; displ; 1 " and display files, and
jmp login jmp login " waits for a user to log in
1: 1:
dac pid2 dac pid2 " Parent stores childs pid in pid2
jmp init2 i jmp init2 i " and returns
login: login:
-1 -1
sys intrp sys intrp
sys open; password; 0 sys open; password; 0 " Open the passwd file
lac d1 lac d1
sys write; m1; m1s sys write; m1; m1s " Write "\nlogin:" on the terminal
jms rline jms rline " and read the user's username
lac ebufp lac ebufp
dac tal dac tal
1: 1:
@@ -55,31 +55,31 @@ login:
sac o12 sac o12
lac o72 lac o72
sad 9 i sad 9 i
skp skp
jmp 1b jmp 1b
sad o72 sad o72
skp skp
jmp 2b jmp 2b
lac 9 i lac 9 i
sad o72 sad o72
jmp 1f jmp 1f
-1 -1
tad 9 tad 9
dac 9 dac 9
lac d1 lac d1
sys write; m3; m3s sys write; m3; m3s " Write "password: " on the terminal
jms rline jms rline " and read the user's password
law ibuf-1 law ibuf-1
dac 8 dac 8
2: 2:
lac 8 i lac 8 i
sad o12 sad o12
lac o72 lac o72
sad 9 i sad 9 i
skp skp
jmp error jmp error
sad o72 sad o72
skp skp
jmp 2b jmp 2b
1: 1:
dzm nchar dzm nchar
@@ -88,11 +88,11 @@ login:
1: 1:
lac 9 i lac 9 i
sad o72 sad o72
jmp 1f jmp 1f
dac char dac char
lac nchar lac nchar
sza sza
jmp 2f jmp 2f
lac char lac char
alss 9 alss 9
xor o40 xor o40
@@ -113,7 +113,7 @@ login:
1: 1:
lac 9 i lac 9 i
sad o12 sad o12
jmp 2f jmp 2f
tad om60 tad om60
lmq lmq
lac nchar lac nchar
@@ -123,42 +123,43 @@ login:
jmp 1b jmp 1b
2: 2:
lac nchar lac nchar
sys setuid sys setuid " Set the user's user-id
sys chdir; dd sys chdir; dd " and change into the "dd" directory
sys chdir; dir sys chdir; dir
lac d2 lac d2 " Close file descriptor 2
sys close sys close
sys open; sh; 0 sys open; sh; 0 " Open the shell executable file (we get fd 2)
sma sma
jmp 1f jmp 1f
sys link; system; sh; sh sys link; system; sh; sh
spa spa
jmp error jmp error
sys open; sh; 0 sys open; sh; 0
spa spa
jmp error jmp error
sys unlink; sh sys unlink; sh
1: 1:
law 017700 law 017700 " Copy the code at the boot label below
dac 9 dac 9 " up to location 017700
law boot-1 law boot-1
dac 8 dac 8
1: 1:
lac 8 i lac 8 i
dac 9 i dac 9 i
sza sza " Stop copying when we hit the 0 marker
jmp 1b jmp 1b
jmp 017701 jmp 017701 " and then jump to the code
boot: boot:
lac d2 lac d2
lmq lmq " Save the fd into MQ
sys read; 4096; 07700 sys read; 4096; 07700 " Read 4,032 words into locations 4096 onwards
lacq " leaving the top 64 words for this boot code.
lacq " Get the fd back and close the file
sys close sys close
jmp 4096 jmp 4096 " and jump to the beginning of that executable
0 0 " 0 marks the end of the code, used by the copy routine above
rline: 0 rline: 0
law ibuf-1 law ibuf-1
@@ -169,17 +170,17 @@ rline: 0
lac char lac char
lrss 9 lrss 9
sad o100 sad o100
jmp rline+1 jmp rline+1
sad o43 sad o43
jmp 2f jmp 2f
dac 8 i dac 8 i
sad o12 sad o12
jmp rline i jmp rline i
jmp 1b jmp 1b
2: 2:
law ibuf-1 law ibuf-1
sad 8 sad 8
jmp 1b jmp 1b
-1 -1
tad 8 tad 8
dac 8 dac 8
@@ -192,17 +193,17 @@ gline: 0
jms gchar jms gchar
dac 8 i dac 8 i
sad o12 sad o12
jmp gline i jmp gline i
jmp 1b jmp 1b
gchar: 0 gchar: 0
lac tal lac tal
sad ebufp sad ebufp
jmp 1f jmp 1f
ral ral
lac tal i lac tal i
snl snl
lrss 9 lrss 9
and o777 and o777
lmq lmq
lac tal lac tal
@@ -210,7 +211,7 @@ gchar: 0
dac tal dac tal
lacq lacq
sna sna
jmp gchar+1 jmp gchar+1
jmp gchar i jmp gchar i
1: 1:
lac bufp lac bufp
@@ -220,14 +221,14 @@ gchar: 0
isz tal isz tal
lac tal lac tal
sad ebufp sad ebufp
skp skp
jmp 1b jmp 1b
lac bufp lac bufp
dac tal dac tal
lac d2 lac d2
sys tead; buf; 64 sys tead; buf; 64
sna sna
jmp error jmp error
jmp gchar+1 jmp gchar+1
error: error:
+20
View File
@@ -0,0 +1,20 @@
" Fork text code
main:
sys fork
jmp parent
child:
lac d1
sys write; childmsg; 3
sys exit
parent:
lac d1
sys write; parentmsg; 4
sys exit
d1: 1 " stdout fd
parentmsg: <pa>; <re>; <nt>; 012000
childmsg: <ch>; <il>; <d 012
+22
View File
@@ -533,9 +533,11 @@ sub cal {
9 => \&sys_close, 9 => \&sys_close,
11 => \&sys_unlink, 11 => \&sys_unlink,
14 => \&sys_exit, 14 => \&sys_exit,
16 => \&sys_intrp,
17 => \&sys_chdir, 17 => \&sys_chdir,
18 => \&sys_chmod, 18 => \&sys_chmod,
19 => \&sys_chown, 19 => \&sys_chown,
29 => \&sys_fork,
); );
# Simulate the syscall. Each syscall updates the $PC # Simulate the syscall. Each syscall updates the $PC
@@ -553,6 +555,26 @@ sub sys_exit {
exit(0); exit(0);
} }
# Intrp system call
sub sys_intrp {
# For now, do nothing
dprint("intrp system call\n");
$PC += 1;
return;
}
# Fork system call
sub sys_fork {
# Fork and get the child's process-id back, or zero if we are the child
my $pid= fork();
$AC= $pid;
# The parent returns back to PC+1, the child returns to PC+2
$PC += ($pid) ? 1 : 2;
return;
}
# Close system call # Close system call
sub sys_close { sub sys_close {