asterinas/test/apps/fork/fork.S

85 lines
2.5 KiB
ArmAsm
Raw Normal View History

2024-01-03 03:22:36 +00:00
# SPDX-License-Identifier: MPL-2.0
2024-02-05 07:38:39 +00:00
# FIXME: WNOHANG option currently does not work properly without preemption, so we have temporarily
# removed it. Once preemption is supported, the following macro can be uncommented to add the WNOHANG
# option back.
# #define PREEMPTION_ENABLE
.global _start
.section .text
_start:
call print_hello_world
mov $57, %rax # syscall number of fork
syscall
cmp $0, %rax
je _child # child process
jmp _parent # parent process
2022-09-08 07:37:34 +00:00
_parent:
2022-09-27 11:51:18 +00:00
call wait_child
2022-09-08 07:37:34 +00:00
call get_pid
call print_parent_message
call exit
_child:
2022-09-08 07:37:34 +00:00
call get_pid
call print_child_message
call exit
2022-09-27 11:51:18 +00:00
wait_child:
mov %rax, %rdi # child process id
2024-02-05 07:38:39 +00:00
#ifdef PREEMPTION_ENABLE
2022-09-27 11:51:18 +00:00
_loop:
mov $61, %rax # syscall number of wait4
mov $0, %rsi # exit status address
2024-02-05 07:38:39 +00:00
mov $1, %rdx # wait option: WNOHANG
2022-09-27 11:51:18 +00:00
syscall
cmp %rdi, %rax # The return value is the pid of child
jne _loop
ret
2024-02-05 07:38:39 +00:00
#else
mov $61, %rax # syscall number of wait4
mov $0, %rsi # exit status address
mov $0, %rdx # wait option
syscall
ret
#endif
exit:
mov $60, %rax # syscall number of exit
mov $0, %rdi # exit code
2022-09-08 07:37:34 +00:00
syscall
get_pid:
mov $39, %rax
syscall
ret
print_hello_world:
mov $message, %rsi # address of message
mov $message_end, %rdx
sub %rsi, %rdx # calculate message len
jmp _print_message
print_parent_message:
mov $message_parent, %rsi # address of message
mov $message_parent_end, %rdx
sub %rsi, %rdx # calculate message len
jmp _print_message
print_child_message:
mov $message_child, %rsi # address of message
mov $message_child_end, %rdx
sub %rsi, %rdx # calculate message len
jmp _print_message
# never directly call _print_message
_print_message:
mov $1, %rax # syscall number of write
mov $1, %rdi # stdout
syscall
ret
2022-09-08 07:37:34 +00:00
.section .rodata
message:
2022-09-08 07:37:34 +00:00
.ascii "Hello, world in fork\n"
message_end:
message_parent:
.ascii "Hello world from parent\n"
message_parent_end:
message_child:
.ascii "Hello world from child\n"
message_child_end: