Skip to content

Commit

Permalink
Modified basic-syscall and syscall-wrappers infrastructure
Browse files Browse the repository at this point in the history
Added checkers and modified files

Signed-off-by: Sorin Birchi <[email protected]>
  • Loading branch information
Sorin Birchi committed Oct 6, 2024
1 parent eb0056b commit 4cd60c1
Show file tree
Hide file tree
Showing 34 changed files with 455 additions and 146 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
support/
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
PYTHON = python3
SCRIPT = generate_skels.py

skels:
mkdir -p support/src/arm
$(PYTHON) $(SCRIPT) --input ./solution/src/arm --output ./support/src/arm
$(PYTHON) $(SCRIPT) --input ./solution/src/ --output ./support/src/
$(PYTHON) $(SCRIPT) --input ./solution/tests --output ./support/src/tests

clean:
rm -rf support/
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ For debugging, use `strace` to trace the system calls from your program and make

[Quiz 3](../drills/questions/syscall-numbers.md)

1. Update the `hello.asm` and / or `hello.s` files to pause the execution of the program before the `exit` system call.
1. Update the `hello.asm` and / or `hello.s` files to sleep before the `exit` system call.

You need to make the `sys_pause()` system call, with no arguments.
You need to make the `sys_nanosleep()` system call, with the `timespec` structure.
Find its ID [here](https://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64/).

1. Update the `hello.asm` and / or `hello.s` files to read a message from standard input and print it to standard output.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#!/usr/bin/python3 -u
# SPDX-License-Identifier: BSD-3-Clause

import sys
import argparse
import os.path
import re


def process_file(src, dst, pattern, replace, remove, replace_pairs, end_string=None):
fin = open(src, "r")
fout = open(dst, "w")
remove_lines = 0
skip_lines = 0
uncomment_lines = 0
end_found = True

for l in fin.readlines():
# Skip generation of file.
if "SKIP_GENERATE" in l:
fout.close()
os.remove(dst)
return

if end_string and end_found == False:
fout.write(l)
if end_string in l:
end_found = True
continue

if remove_lines > 0:
remove_lines -= 1
continue

if skip_lines > 0:
skip_lines -= 1
m = re.search(pattern, l)
if m:
l = "%s%s\n" % (m.group(1), m.group(3))
fout.write(l)
continue

if uncomment_lines > 0:
uncomment_lines -= 1
for fro, to in replace_pairs:
l = re.sub(fro, to, l)
fout.write(l)
continue

m = re.search(pattern, l)
if m:
if m.group(2):
skip_lines = int(m.group(2))
else:
skip_lines = 1

if end_string and end_string not in l:
end_found = False

l = "%s%s\n" % (m.group(1), m.group(3))

m = re.search(replace, l)
if m:
if m.group(2):
uncomment_lines = int(m.group(2))
else:
uncomment_lines = 1
continue

m = re.search(remove, l)
if m:
if m.group(2):
remove_lines = int(m.group(2))
else:
remove_lines = 1
continue

fout.write(l)

fout.close()


def main():
parser = argparse.ArgumentParser(
description="Generate skeletons sources from reference solution sources"
)
parser.add_argument(
"--input", help="input directory to process files", required=True
)
parser.add_argument(
"--output", help="output directory to copy processed files", required=True
)
args = parser.parse_args()

for root, dirs, files in os.walk(args.input):
new_root = os.path.join(args.output, root.replace(args.input, ""))
for d in dirs:
os.makedirs(os.path.join(new_root, d), exist_ok=True)

for src in files:
if (
re.match("Makefile.*$", src)
or re.match(".*\.sh$", src)
or re.match(".*\.[sS]$", src)
):
pattern = "(^\s*#\s*TODO)( [0-9]*)(:.*)"
replace = "(^\s*#\s*REPLACE)( [0-9]*)"
remove = "(^\s*#\s*REMOVE)( [0-9]*)"
replace_pairs = [("# ", "")]
end_string = None
elif re.match(".*\.asm$", src):
pattern = "(^\s*;\s*TODO)( [0-9]*)(:.*)"
replace = "(^\s*;\s*REPLACE)( [0-9]*)"
remove = "(^\s*;\s*REMOVE)( [0-9]*)"
replace_pairs = [("; ", "")]
end_string = None
elif (
re.match(".*\.[ch]$", src)
or re.match(".*\.cpp$", src)
or re.match(".*\.hpp$", src)
):
pattern = "(.*/\*\s*TODO)([ 0-9]*)(:.*)"
replace = "(.*/\*\s*REPLACE)( [0-9]*)"
remove = "(.*/\*\s*REMOVE)( [0-9]*)"
replace_pairs = [("/\* ", ""), (" \*/", "")]
end_string = "*/"
elif re.match(".*\.d$", src):
pattern = "(.*//\s*TODO)([ 0-9]*)(:.*)"
replace = "(.*//\s*REPLACE)( [0-9]*)"
else:
continue

dst = os.path.join(new_root, src)
src = os.path.join(root, src)
print(dst)
process_file(src, dst, pattern, replace, remove, replace_pairs, end_string)


if __name__ == "__main__":
sys.exit(main())

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ bye:
.global main

main:

# TODO 11: print "Hello, world!"
/* Call write(1, "Hello, world!\n", 14). */
/* rax <- __NR_write (index of write syscall: 1) */
/* rdi <- first syscall argument (fd: 1) */
Expand All @@ -37,11 +39,11 @@ main:
mov x8, #64
svc #0


# TODO 7: exit #
/* Call exit(0). */
/* rax <- __NR_exit (index of exit syscall: 60) */
/* rdi <- first syscall argument (error_code: 0) */
mov x0, #0
mov x8, #93
svc #0

ret
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ section .bss
buffer resb 128
len equ 128

section .data

; Structure to hold 2 seconds and 0 nanoseconds
timespec:
dq 2 ; 2 seconds
dq 0 ; 0 nanoseconds

section .rodata

hello db "Hello, world!", 10, 0
Expand All @@ -18,6 +25,7 @@ main:
push rbp
mov rbp, rsp

; TODO 11: print "Hello, world!"
; Call write(1, "Hello, world!\n", 14).
; rax <- __NR_write (index of write syscall: 1)
; rdi <- first syscall argument (fd: 1)
Expand All @@ -29,6 +37,8 @@ main:
mov rax, 1
syscall


; TODO 11: print "Bye, world!"
; Call write(1, "Bye, world!\n", 12).
; rax <- __NR_write (index of write syscall: 1)
; rdi <- first syscall argument (fd: 1)
Expand All @@ -40,6 +50,8 @@ main:
mov rax, 1
syscall


; TODO 11: read from buffer
; Call read(0, buffer, 128).
; rax <- __NR_read (index of read syscall: 0)
; rdi <- first syscall argument (fd: 0)
Expand All @@ -51,6 +63,8 @@ main:
mov rax, 0
syscall


; TODO 11: print the content of buffer
; Call write(1, buffer, size).
; rax <- __NR_write (index of write syscall: 1)
; rdi <- first syscall argument (fd: 1)
Expand All @@ -62,11 +76,22 @@ main:
mov rax, 1
syscall

; Call pause().
; rax <- __NR_pause (index of pause syscall: 34)
mov rax, 34

; TODO 5: sleep for 2 seconds using nanosleep
lea rdi, [timespec]
xor rsi, rsi
mov rax, 35
syscall

; TODO : exit
; Call exit(0).
; rax <- __NR_exit (index of exit syscall: 60)
; rdi <- first syscall argument (status: 0)
mov rdi, 0
mov rax, 60
syscall


xor rax, rax
leave
ret
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* SPDX-License-Identifier: BSD-3-Clause */

.section .bss

.lcomm buffer, 128

.equ len, 128
Expand All @@ -14,6 +13,13 @@ hello:
bye:
.ascii "Bye, world!\n\0"

.section .data

# Structure to hold 2 seconds and 0 nanoseconds
timespec:
.quad 2 # 2 seconds
.quad 0 # 0 nanoseconds

.section .text

.global main
Expand All @@ -22,62 +28,68 @@ main:
push %rbp
mov %rsp, %rbp


# TODO 10: print "Hello, world!"
# Call write(1, "Hello, world!\n", 14).
# rax <- __NR_write (index of write syscall: 1)
# rdi <- first syscall argument (fd: 1)
# rsi <- second syscall argument (buffer: hello)
# rdx <- third syscall argument (length: 14)
mov $1, %rdi
lea hello, %rsi
lea hello(%rip), %rsi
mov $14, %rdx
mov $1, %rax
syscall

# TODO 10: print "Bye, world!"
# Call write(1, "Bye, world!\n", 12).
# rax <- __NR_write (index of write syscall: 1)
# rdi <- first syscall argument (fd: 1)
# rsi <- second syscall argument (buffer: hello)
# rsi <- second syscall argument (buffer: bye)
# rdx <- third syscall argument (length: 12)
mov $1, %rdi
lea bye, %rsi
lea bye(%rip), %rsi
mov $12, %rdx
mov $1, %rax
syscall

# TODO 10: read from stdin
# Call read(0, buffer, 128).
# rax <- __NR_read (index of read syscall: 0)
# rdi <- first syscall argument (fd: 0)
# rsi <- second syscall argument (buffer: buffer)
# rdx <- third syscall argument (length: len)
mov $0, %rdi
lea buffer, %rsi
lea len, %rdx
lea buffer(%rip), %rsi
mov $len, %rdx
mov $0, %rax
syscall

# TODO 10: print the content of buffer
# Call write(1, buffer, size).
# rax <- __NR_write (index of write syscall: 1)
# rdi <- first syscall argument (fd: 1)
# rsi <- second syscall argument (buffer: buffer)
# rdx <- third syscall argument (length: size - rax)
# rdx <- third syscall argument (length: size from rax)
mov $1, %rdi
lea buffer, %rsi
lea buffer(%rip), %rsi
mov %rax, %rdx
mov $1, %rax
syscall

# Call pause().
# rax <- __NR_pause (index of pause syscall: 34)
mov $34, %rax
# TODO 4: sleep for 2 seconds using nanosleep
lea timespec(%rip), %rdi # Pointer to timespec structure
xor %rsi, %rsi # NULL for remaining time
mov $35, %rax # Syscall number for nanosleep (35)
syscall

# TODO 6: exit
# Call exit(0).
# rax <- __NR_exit (index of exit syscall: 60)
# rdi <- first syscall argument (error_code: 0)
mov $0, %rdi
mov $60, %rax
syscall

xor %rax, %rax
leave
ret
Loading

0 comments on commit 4cd60c1

Please sign in to comment.