4 ms·
Without them it'll segfault on QEMU, IIRC OpenBSD libc uses them invariably as well. I don't know how it fares on real hardware
by oguz-ismail 2y ago
Without them it'll segfault on QEMU, IIRC OpenBSD libc uses them invariably as well. I don't know how it fares on real hardware
- johnisgood 2y agoYeah, probably (although I am not sure) because QEMU doesn't fully emulate hardware behavior, but on real hardware some CPUs may internally handle these state transitions better. I suppose its inclusion is recommended to ensure correctness across all CPUs. 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.
- pm215 2y agoThat doesn't sound right -- QEMU always works one instruction at a time, so we never start executing a following insn until the previous one is completed.
- oguz-ismail 2y agoYeah the segfault doesn't happen on other OSes, I believe it's an OpenBSD thing
- johnisgood 2y agoSo what causes QEMU to segfault without the synchronization? I have not tested it myself, however, so I cannot guarantee that it indeed does segfault.
- jmillikin 2y agoThe "dsb nsh; isb" sequence after "svc 0" is part of OpenBSD's mitigations for Spectre. https://github.com/openbsd/src/commit/bbeaada4689520859307d5cf69224e7b786b0148 https://github.com/openbsd/src/commit/bbeaada4689520859307d5... https://github.com/openbsd/src/commit/0c401ffc2a2550c32105ce180746b0a95071123b https://github.com/openbsd/src/commit/0c401ffc2a2550c32105ce... https://github.com/openbsd/src/commit/5ecc9681133f1894e81c385eddd09981f78f1193 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.