by oguz-ismail · 1y ago · 0 comments
.global _start
.data
what: .string "hello\n"
.set len, .-what - 1
.text
_start:
mov w0, 1
adr x1, what
mov w2, len
mov w8, 4
99: svc 0
dsb nsh
isb
mov w8, 1
98: svc 0
.section .openbsd.syscalls, ""
.long 99b, 4
.long 98b, 1
.section .note.openbsd.ident, "a"
.long 8, 4, 1
.string "OpenBSD"
.long 0Mixing C is helpful because most code doesn't need to be written in raw assembly.
https://github.com/openbsd/src/commit/bbeaada4689520859307d5...
https://github.com/openbsd/src/commit/0c401ffc2a2550c32105ce...
https://github.com/openbsd/src/commit/5ecc9681133f1894e81c38...
If I'm reading the commits correctly, the OpenBSD kernel will skip two instructions after a "svc 0" when returning to userspace, on the assumption that any syscall comes from libc and therefore has "dsb nsh; isb" after it.
It may happen because without "dsb nsh" (Data Synchronization Barrier) and "isb" (Instruction Synchronization Barrier), QEMU may continue execution before the syscall fully completes, which causes a segfault or UB, but I am not entirely sure.
"dsb nsh" and "isb" ensures correct execution order which prevents speculative execution issues.
I can grasp the example, but back in my days, if we wanting something faster than interpreted BASIC, Assembly was the way.
Folks nowadays start with Python and JavaScript.