Assembly coroutine backend and x86 CET support
cover Assembly coroutine backend and x86 CET support
*** BLURB HERE ***
Paolo Bonzini (10):
qemugdb: allow adding support for other coroutine backends
qemugdb: allow adding support for other architectures
coroutine: add host specific coroutine backend for 64-bit x86
coroutine: add host specific coroutine backend for 64-bit ARM
coroutine: add host specific coroutine backend for 64-bit s390
configure: add control-flow protection support
tcg: add tcg_out_start
tcg/i386: add support for IBT
linux-user: add IBT support to x86 safe-syscall.S
coroutine-asm: add x86 CET shadow stack support
Makefile.target | 5 + configure | 62 ++++ include/qemu/cpuid.h | 5 + linux-user/host/i386/safe-syscall.inc.S | 19 ++ linux-user/host/x86_64/safe-syscall.inc.S | 19 ++ scripts/qemugdb/coroutine.py | 107 ++---- scripts/qemugdb/coroutine_asm.py | 24 ++ scripts/qemugdb/coroutine_ucontext.py | 69 ++++ tcg/aarch64/tcg-target.inc.c | 4 + tcg/arm/tcg-target.inc.c | 4 + tcg/i386/tcg-target.inc.c | 23 ++ tcg/mips/tcg-target.inc.c | 4 + tcg/ppc/tcg-target.inc.c | 4 + tcg/riscv/tcg-target.inc.c | 4 + tcg/s390/tcg-target.inc.c | 4 + tcg/sparc/tcg-target.inc.c | 4 + tcg/tcg.c | 2 + tcg/tci/tcg-target.inc.c | 4 + util/Makefile.objs | 10 + util/coroutine-asm.c | 387 ++++++++++++++++++++++ 20 files changed, 689 insertions(+), 75 deletions(-) create mode 100644 scripts/qemugdb/coroutine_asm.py create mode 100644 scripts/qemugdb/coroutine_ucontext.py create mode 100644 util/coroutine-asm.c
Signature · 1 line
2.21.0
PATCH 1/9 qemugdb: allow adding support for other coroutine backends
Split the jmpbuf access to a separate module and dispatch based on which CoroutineXYZ type is defined.
--- scripts/qemugdb/coroutine.py | 106 ++++++++------------------ scripts/qemugdb/coroutine_ucontext.py | 69 +++++++++++++++++ 2 files changed, 100 insertions(+), 75 deletions(-) create mode 100644 scripts/qemugdb/coroutine_ucontext.py
scripts/qemugdb/coroutine.py +31 -75
@@ -1,6 +1,6 @@ #!/usr/bin/python -# GDB debugging support +# GDB debugging support, coroutine dispatch # # Copyright 2012 Red Hat, Inc. and/or its affiliates #
@@ -10,82 +10,25 @@ # This work is licensed under the terms of the GNU GPL, version 2 # or later. See the COPYING file in the top-level directory. +from . import coroutine_ucontext import gdb VOID_PTR = gdb.lookup_type('void').pointer() +UINTPTR_T = gdb.lookup_type('uintptr_t') -def get_fs_base(): - '''Fetch %fs base value using arch_prctl(ARCH_GET_FS). This is - pthread_self().''' - # %rsp - 120 is scratch space according to the SystemV ABI - old = gdb.parse_and_eval('*(uint64_t*)($rsp - 120)') - gdb.execute('call (int)arch_prctl(0x1003, $rsp - 120)', False, True) - fs_base = gdb.parse_and_eval('*(uint64_t*)($rsp - 120)') - gdb.execute('set *(uint64_t*)($rsp - 120) = %s' % old, False, True) - return fs_base - -def pthread_self(): - '''Fetch pthread_self() from the glibc start_thread function.''' - f = gdb.newest_frame() - while f.name() != 'start_thread': - f = f.older() - if f is None: - return get_fs_base() - - try: - return f.read_var("arg") - except ValueError: - return get_fs_base() - -def get_glibc_pointer_guard(): - '''Fetch glibc pointer guard value''' - fs_base = pthread_self() - return gdb.parse_and_eval('*(uint64_t*)((uint64_t)%s + 0x30)' % fs_base) - -def glibc_ptr_demangle(val, pointer_guard): - '''Undo effect of glibc's PTR_MANGLE()''' - return gdb.parse_and_eval('(((uint64_t)%s >> 0x11) | ((uint64_t)%s << (64 - 0x11))) ^ (uint64_t)%s' % (val, val, pointer_guard)) - -def get_jmpbuf_regs(jmpbuf): - JB_RBX = 0 - JB_RBP = 1 - JB_R12 = 2 - JB_R13 = 3 - JB_R14 = 4 - JB_R15 = 5 - JB_RSP = 6 - JB_PC = 7 - - pointer_guard = get_glibc_pointer_guard() - return {'rbx': jmpbuf[JB_RBX], - 'rbp': glibc_ptr_demangle(jmpbuf[JB_RBP], pointer_guard), - 'rsp': glibc_ptr_demangle(jmpbuf[JB_RSP], pointer_guard), - 'r12': jmpbuf[JB_R12], - 'r13': jmpbuf[JB_R13], - 'r14': jmpbuf[JB_R14], - 'r15': jmpbuf[JB_R15], - 'rip': glibc_ptr_demangle(jmpbuf[JB_PC], pointer_guard) } - -def bt_jmpbuf(jmpbuf): - '''Backtrace a jmpbuf''' - regs = get_jmpbuf_regs(jmpbuf) - old = dict() - - for i in regs: - old[i] = gdb.parse_and_eval('(uint64_t)$%s' % i) - - for i in regs: - gdb.execute('set $%s = %s' % (i, regs[i])) - - gdb.execute('bt') - - for i in regs: - gdb.execute('set $%s = %s' % (i, old[i])) - -def coroutine_to_jmpbuf(co): - coroutine_pointer = co.cast(gdb.lookup_type('CoroutineUContext').pointer()) - return coroutine_pointer['env']['__jmpbuf'] +backends = { + 'CoroutineUContext': coroutine_ucontext +} +def coroutine_backend(): + for k, v in backends.items(): + try: + gdb.lookup_type(k) + except: + continue + return v + + raise Exception('could not find coroutine backend') class CoroutineCommand(gdb.Command): '''Display coroutine backtrace'''
@@ -99,18 +42,31 @@ class CoroutineCommand(gdb.Command): gdb.write('usage: qemu coroutine <coroutine-pointer>\n') return - bt_jmpbuf(coroutine_to_jmpbuf(gdb.parse_and_eval(argv[0]))) + addr = gdb.parse_and_eval(argv[0]) + regs = coroutine_backend().get_coroutine_regs(addr) + old = dict() + + for i in regs: + old[i] = gdb.parse_and_eval('(uint64_t)$%s' % i) + + for i in regs: + gdb.execute('set $%s = %s' % (i, regs[i].cast(UINTPTR_T))) + + gdb.execute('bt') + + for i in regs: + gdb.execute('set $%s = %s' % (i, old[i].cast(UINTPTR_T))) class CoroutineSPFunction(gdb.Function): def __init__(self): gdb.Function.__init__(self, 'qemu_coroutine_sp') def invoke(self, addr): - return get_jmpbuf_regs(coroutine_to_jmpbuf(addr))['rsp'].cast(VOID_PTR) + return coroutine_backend().get_coroutine_regs(addr)['rsp'].cast(VOID_PTR) class CoroutinePCFunction(gdb.Function): def __init__(self): gdb.Function.__init__(self, 'qemu_coroutine_pc') def invoke(self, addr): - return get_jmpbuf_regs(coroutine_to_jmpbuf(addr))['rip'].cast(VOID_PTR) + return coroutine_backend().get_coroutine_regs(addr)['rip'].cast(VOID_PTR)
scripts/qemugdb/coroutine_ucontext.py new file +69 -0
69 lines not on this page · raw message
Signature · 1 line
2.21.0
PATCH 2/9 qemugdb: allow adding support for other architectures
In reply to Paolo Bonzini$sp and $pc are standard register names that are available on most machines, use them instead of $rsp and $rip so that other architectures can use qemu_coroutine_sp and qemu_coroutine_pc.
--- scripts/qemugdb/coroutine.py | 4 ++-- scripts/qemugdb/coroutine_ucontext.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)
scripts/qemugdb/coroutine.py +2 -2
@@ -62,11 +62,11 @@ class CoroutineSPFunction(gdb.Function): gdb.Function.__init__(self, 'qemu_coroutine_sp') def invoke(self, addr): - return coroutine_backend().get_coroutine_regs(addr)['rsp'].cast(VOID_PTR) + return coroutine_backend().get_coroutine_regs(addr)['sp'].cast(VOID_PTR) class CoroutinePCFunction(gdb.Function): def __init__(self): gdb.Function.__init__(self, 'qemu_coroutine_pc') def invoke(self, addr): - return coroutine_backend().get_coroutine_regs(addr)['rip'].cast(VOID_PTR) + return coroutine_backend().get_coroutine_regs(addr)['pc'].cast(VOID_PTR)
scripts/qemugdb/coroutine_ucontext.py +2 -2
@@ -57,12 +57,12 @@ def get_jmpbuf_regs(jmpbuf): pointer_guard = get_glibc_pointer_guard() return {'rbx': jmpbuf[JB_RBX], 'rbp': glibc_ptr_demangle(jmpbuf[JB_RBP], pointer_guard), - 'rsp': glibc_ptr_demangle(jmpbuf[JB_RSP], pointer_guard), + 'sp': glibc_ptr_demangle(jmpbuf[JB_RSP], pointer_guard), 'r12': jmpbuf[JB_R12], 'r13': jmpbuf[JB_R13], 'r14': jmpbuf[JB_R14], 'r15': jmpbuf[JB_R15], - 'rip': glibc_ptr_demangle(jmpbuf[JB_PC], pointer_guard) } + 'pc': glibc_ptr_demangle(jmpbuf[JB_PC], pointer_guard) } def get_coroutine_regs(addr): co = addr.cast(gdb.lookup_type('CoroutineUContext').pointer())
Signature · 1 line
2.21.0
PATCH 3/9 coroutine: add host specific coroutine backend for 64-bit x86
In reply to Paolo BonziniThis backend is faster (100ns vs 150ns per switch on my laptop), but especially it will be possible to add CET support to it. Most of the code is actually not architecture specific.
--- configure | 10 ++ scripts/qemugdb/coroutine.py | 5 +- scripts/qemugdb/coroutine_asm.py | 20 +++ util/Makefile.objs | 1 + util/coroutine-asm.c | 230 +++++++++++++++++++++++++++++++ 5 files changed, 264 insertions(+), 2 deletions(-) create mode 100644 scripts/qemugdb/coroutine_asm.py create mode 100644 util/coroutine-asm.c
configure +10 -0
@@ -5200,6 +5200,8 @@ fi if test "$coroutine" = ""; then if test "$mingw32" = "yes"; then coroutine=win32 + elif test "$cpu" = "x86_64"; then + coroutine=asm elif test "$ucontext_works" = "yes"; then coroutine=ucontext else
@@ -5225,6 +5227,14 @@ else error_exit "only the 'windows' coroutine backend is valid for Windows" fi ;; + asm) + if test "$mingw32" = "yes"; then + error_exit "only the 'windows' coroutine backend is valid for Windows" + fi + if test "$cpu" != "x86_64"; then + error_exit "the 'asm' backend is only valid for x86_64 hosts" + fi + ;; *) error_exit "unknown coroutine backend $coroutine" ;;
scripts/qemugdb/coroutine.py +3 -2
@@ -10,14 +10,15 @@ # This work is licensed under the terms of the GNU GPL, version 2 # or later. See the COPYING file in the top-level directory. -from . import coroutine_ucontext +from . import coroutine_ucontext, coroutine_asm import gdb VOID_PTR = gdb.lookup_type('void').pointer() UINTPTR_T = gdb.lookup_type('uintptr_t') backends = { - 'CoroutineUContext': coroutine_ucontext + 'CoroutineUContext': coroutine_ucontext, + 'CoroutineAsm': coroutine_asm } def coroutine_backend():
scripts/qemugdb/coroutine_asm.py new file +20 -0
@@ -0,0 +1,20 @@ +#!/usr/bin/python + +# GDB debugging support +# +# Copyright 2019 Red Hat, Inc. +# +# Authors: +# Paolo Bonzini <[email protected]> +# +# This work is licensed under the terms of the GNU GPL, version 2 or +# later. See the COPYING file in the top-level directory. + +import gdb + +U64_PTR = gdb.lookup_type('uint64_t').pointer() + +def get_coroutine_regs(addr): + addr = addr.cast(gdb.lookup_type('CoroutineAsm').pointer()) + rsp = addr['sp'].cast(U64_PTR) + return {'sp': rsp, 'pc': rsp.dereference()}
util/Makefile.objs +1 -0
@@ -39,6 +39,7 @@ util-obj-$(CONFIG_MEMBARRIER) += sys_membarrier.o util-obj-y += qemu-coroutine.o qemu-coroutine-lock.o qemu-coroutine-io.o util-obj-y += qemu-coroutine-sleep.o util-obj-y += coroutine-$(CONFIG_COROUTINE_BACKEND).o +coroutine-asm.o-cflags := -mno-red-zone util-obj-y += buffer.o util-obj-y += timed-average.o util-obj-y += base64.o
util/coroutine-asm.c new file +230 -0
230 lines not on this page · raw message
Signature · 1 line
2.21.0
Re: [Qemu-devel] [PATCH 3/9] coroutine: add host specific coroutine backend for 64-bit x86
Quoted 5 lines · Paolo Bonzini
This backend is faster (100ns vs 150ns per switch on my laptop), but especially it will be possible to add CET support to it. Most of the code is actually not architecture specific.
Signed-off-by Paolo Bonzini <[email protected]>
Quoted 9 lines
--- configure | 10 ++ scripts/qemugdb/coroutine.py | 5 +- scripts/qemugdb/coroutine_asm.py | 20 +++ util/Makefile.objs | 1 + util/coroutine-asm.c | 230 +++++++++++++++++++++++++++++++ 5 files changed, 264 insertions(+), 2 deletions(-) create mode 100644 scripts/qemugdb/coroutine_asm.py create mode 100644 util/coroutine-asm.c
r~
PATCH 4/9 coroutine: add host specific coroutine backend for 64-bit ARM
In reply to Paolo BonziniThe speedup is similar to x86, 120 ns vs 180 ns on an APM Mustang.
--- configure | 2 +- scripts/qemugdb/coroutine_asm.py | 6 ++++- util/Makefile.objs | 2 ++ util/coroutine-asm.c | 45 ++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 2 deletions(-)
configure +1 -1
@@ -5200,7 +5200,7 @@ fi if test "$coroutine" = ""; then if test "$mingw32" = "yes"; then coroutine=win32 - elif test "$cpu" = "x86_64"; then + elif test "$cpu" = "x86_64" || test "$cpu" = "aarch64"; then coroutine=asm elif test "$ucontext_works" = "yes"; then coroutine=ucontext
scripts/qemugdb/coroutine_asm.py +5 -1
@@ -17,4 +17,8 @@ U64_PTR = gdb.lookup_type('uint64_t').pointer() def get_coroutine_regs(addr): addr = addr.cast(gdb.lookup_type('CoroutineAsm').pointer()) rsp = addr['sp'].cast(U64_PTR) - return {'sp': rsp, 'pc': rsp.dereference()} + arch = gdb.selected_frame().architecture.name().split(':'): + if arch[0] == 'i386' and arch[1] == 'x86-64': + return {'rsp': rsp, 'pc': rsp.dereference()} + else: + return {'sp': rsp, 'pc': addr['scratch'].cast(U64_PTR) }
util/Makefile.objs +2 -0
@@ -39,7 +39,9 @@ util-obj-$(CONFIG_MEMBARRIER) += sys_membarrier.o util-obj-y += qemu-coroutine.o qemu-coroutine-lock.o qemu-coroutine-io.o util-obj-y += qemu-coroutine-sleep.o util-obj-y += coroutine-$(CONFIG_COROUTINE_BACKEND).o +ifeq ($(ARCH),x86_64) coroutine-asm.o-cflags := -mno-red-zone +endif util-obj-y += buffer.o util-obj-y += timed-average.o util-obj-y += base64.o
util/coroutine-asm.c +48 -0
60 lines not on this page · raw message
Signature · 1 line
2.21.0
Re: [Qemu-devel] [PATCH 4/9] coroutine: add host specific coroutine backend for 64-bit ARM
Quoted 9 lines · Paolo Bonzini
The speedup is similar to x86, 120 ns vs 180 ns on an APM Mustang.
Signed-off-by Paolo Bonzini <[email protected]>--- configure | 2 +- scripts/qemugdb/coroutine_asm.py | 6 ++++- util/Makefile.objs | 2 ++ util/coroutine-asm.c | 45 ++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 2 deletions(-)
util/coroutine-asm.c static void start_switch_fiber(void **fake_stack_save, · line 148
Quoted 5 lines
util/coroutine-asm.c +5
-0+"ldr x30, [x1, %[SCRATCH]]\n" /* load destination PC */ \ +"ldr x1, [x1, %[SP]]\n" /* load destination SP */ \ +"mov sp, x1\n" \ +"br x30\n" \ +"2: \n" \
For future reference, "bti j" (aka hint #36) goes here, for the aarch64 branch target identification extension.
r~
Re: [Qemu-devel] [PATCH 4/9] coroutine: add host specific coroutine backend for 64-bit ARM
In reply to Paolo BonziniQuoted 18 lines · Paolo Bonzini
configure +1
-1@@ -5200,7 +5200,7 @@ fi if test "$coroutine" = ""; then if test "$mingw32" = "yes"; then coroutine=win32 - elif test "$cpu" = "x86_64"; then + elif test "$cpu" = "x86_64" || test "$cpu" = "aarch64"; then coroutine=asm elif test "$ucontext_works" = "yes"; then coroutine=ucontextscripts/qemugdb/coroutine_asm.py +0
-0
scripts/qemugdb/coroutine_asm.py U64_PTR = gdb.lookup_type('uint64_t').pointer() · line 22
Quoted 7 lines
scripts/qemugdb/coroutine_asm.py +3
-1@@ -17,4 +17,8 @@ U64_PTR = gdb.lookup_type('uint64_t').pointer() def get_coroutine_regs(addr): addr = addr.cast(gdb.lookup_type('CoroutineAsm').pointer()) rsp = addr['sp'].cast(U64_PTR) - return {'sp': rsp, 'pc': rsp.dereference()} + arch = gdb.selected_frame().architecture.name().split(':'): + if arch[0] == 'i386' and arch[1] == 'x86-64': + return {'rsp': rsp, 'pc': rsp.dereference()}
Before: sp
After: rsp
Is this a typo? I thought we were using sp everywhere now.
util/Makefile.objs util-obj-$(CONFIG_MEMBARRIER) += sys_membarrier.o · line 44
Quoted 13 lines
scripts/qemugdb/coroutine_asm.py +2
-0+ else: + return {'sp': rsp, 'pc': addr['scratch'].cast(U64_PTR) }util/Makefile.objs +2
-0@@ -39,7 +39,9 @@ util-obj-$(CONFIG_MEMBARRIER) += sys_membarrier.o util-obj-y += qemu-coroutine.o qemu-coroutine-lock.o qemu-coroutine-io.o util-obj-y += qemu-coroutine-sleep.o util-obj-y += coroutine-$(CONFIG_COROUTINE_BACKEND).o +ifeq ($(ARCH),x86_64) coroutine-asm.o-cflags := -mno-red-zone +endif
-mno-red-zone was mentioned in the previous patch. Should this hunk be moved there?
PATCH 5/9 coroutine: add host specific coroutine backend for 64-bit s390
In reply to Paolo Bonzini--- util/coroutine-asm.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
util/coroutine-asm.c +35 -1
@@ -41,7 +41,7 @@ typedef struct {void *sp;/* -* aarch64: instruction pointer +* aarch64, s390x: instruction pointer */ void *scratch;
@@ -161,6 +161,40 @@ static void start_switch_fiber(void **fake_stack_save, (to)->scratch = (void *) coroutine_trampoline; \ (void )CO_SWITCH_RET (from, to, (uintptr_t ) to); \ } while(0) + +#elif defined __s390x__ +#define CO_SWITCH_RET (from, to, action) ({ \ + register uintptr_t action_ __asm__("r2") = action; \ + register void *from_ __asm__("r1") = from; \ + register void *to_ __asm__("r3") = to; \ + register void *pc_ __asm__("r4") = to->scratch; \ + void *save_r13; \ + asm volatile( \ + "stg %%r13, %[SAVE_R13]\n" \ + "stg %%r15, %[SP](%%r1)\n"/* save source SP */ \ +"lg %%r15, %[SP](%%r3)\n" /* load destination SP */ \ +"bras %%r3, 1f\n" /* source PC will be after the BR */ \ +"1: aghi %%r3, 12\n" /* 4 */ \ +"stg %%r3, %[SCRATCH](%%r1)\n" /* 6 save switch-back PC */ \ +"br %%r4\n" /* 2 jump to destination PC */ \ +"lg %%r13, %[SAVE_R13]\n" \ + : "+r" (action_), "+r" (from_), "+r" (to_), "+r" (pc_), \ +[SAVE_R13] " +m" (r13) \ +: [SP] " i" (offsetof(CoroutineAsm, sp)), \ +[SCRATCH] " i" (offsetof(CoroutineAsm, scratch)) \ +: " r0", " r5", " r6", " r7", " r8", " r9", " r10", " r11", " r12", " r14", \ +" a2", " a3", " a4", " a5", " a6", " a7", \ +" a8", " a9", " a10", " a11", " a12", " a13", " a14", " a15", \ +" f0", " f1", " f2", " f3", " f4", " f5", " f6", " f7", \ + "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15", "memory"); \ + action_; \ +}) + +#define CO_SWITCH_NEW(from, to) do { \ + (to)->scratch = (void *) coroutine_trampoline; \ + (to)->sp -=160 ; \ + (void )CO_SWITCH_RET (from, to, (uintptr_t ) to); \ +} while(0) #else#error coroutine-asm.c not ported to this architecture.#endif
Signature · 1 line
2.21.0
Re: [Qemu-devel] [PATCH 5/9] coroutine: add host specific coroutine backend for 64-bit s390
util/coroutine-asm.c static void start_switch_fiber(void **fake_stack_save, · line 179
Quoted 4 lines · Paolo Bonzini
util/coroutine-asm.c +4
-0+"bras %%r3, 1f\n" /* source PC will be after the BR */ \ +"1: aghi %%r3, 12\n" /* 4 */ \ +"stg %%r3, %[SCRATCH](%%r1)\n" /* 6 save switch-back PC */ \ +"br %%r4\n" /* 2 jump to destination PC */ \
Better as
larl %r3, 2f stg %r3, SCRATCH(%r1) br %r4
2:
r~
PATCH 6/9 configure: add control-flow protection support
In reply to Paolo BonziniControl-flow protection requires object files to note which features are supported. The linker will merge them to the set of features that are supported by all object files. The compiler creates these notes when the -fcf-protection option is passed, but we have to blacklist some object files that only support a subset of the full control-flow protection feature set.
Even without any further host-specific patches, user-mode emulation binaries can already use shadow stacks, because they don't need coroutines and don't include the problematic util/coroutine-*.o object files. Likewise, system-mode emulation binaries will enable indirect branch tracking if built without TCG support.
The next patches will improve the situation so that QEMU can be built with full protection on x86 hosts.
--- Makefile.target | 3 +++ configure | 29 +++++++++++++++++++++++++++++ util/Makefile.objs | 5 +++++ 3 files changed, 37 insertions(+)
Makefile.target +3 -0
@@ -111,6 +111,9 @@ obj-y += exec.o obj-y += accel/ obj-$(CONFIG_TCG) += tcg/tcg.o tcg/tcg-op.o tcg/tcg-op-vec.o tcg/tcg-op-gvec.o obj-$(CONFIG_TCG) += tcg/tcg-common.o tcg/optimize.o +ifeq ($(CONFIG_CF_PROTECTION),y) +tcg/tcg.o-cflags := -fcf-protection=return +endif obj-$(CONFIG_TCG_INTERPRETER) += tcg/tci.o obj-$(CONFIG_TCG_INTERPRETER) += disas/tci.o obj-$(CONFIG_TCG) += fpu/softfloat.o
configure +29 -0
@@ -449,6 +449,7 @@ win_sdk="no" want_tools="yes" libiscsi="" libnfs="" +cf_protection="no" # leave it disabled until we can test performance coroutine="" coroutine_pool="" debug_stack_usage="no"
@@ -1267,6 +1268,10 @@ for opt do ;; --with-pkgversion=*) pkgversion="$optarg" ;; + --enable-cf-protection) cf_protection="yes" + ;; + --disable-cf-protection) cf_protection="no" + ;; --with-coroutine=*) coroutine="$optarg" ;; --disable-coroutine-pool) coroutine_pool="no"
@@ -1796,6 +1801,7 @@ disabled with --disable-FEATURE, default is enabled if available: lzfse support of lzfse compression library (for reading lzfse-compressed dmg images) seccomp seccomp support + cf-protection Control-flow protection coroutine-pool coroutine freelist (better performance) glusterfs GlusterFS backend tpm TPM support
@@ -5176,6 +5182,25 @@ if have_backend "dtrace"; then fi fi +########################################## +# detect Control-flow protection support in the toolchain + +if test "$cf_protection" != no; then + write_c_skeleton; + if ! compile_prog "-fcf-protection" "" ; then + if test "$cf_protection" = yes; then + feature_not_found "cf_protection" 'Control-flow protection is not supported by your toolchain' + fi + cf_protection=no + fi +fi +if test "$cf_protection" = ""; then + cf_protection=yes +fi +if test "$cf_protection" = "yes"; then + QEMU_CFLAGS="-fcf-protection $QEMU_CFLAGS" +fi + ########################################## # check and set a backend for coroutine
@@ -6361,6 +6386,7 @@ echo "netmap support $netmap" echo "Linux AIO support $linux_aio" echo "ATTR/XATTR support $attr" echo "Install blobs $blobs" +echo "CF protection $cf_protection" echo "KVM support $kvm" echo "HAX support $hax" echo "HVF support $hvf"
@@ -6571,6 +6597,9 @@ fi if test "$profiler" = "yes" ; then echo "CONFIG_PROFILER=y" >> $config_host_mak fi +if test "$cf_protection" = "yes" ; then + echo "CONFIG_CF_PROTECTION=y" >> $config_host_mak +fi if test "$slirp" != "no"; then echo "CONFIG_SLIRP=y" >> $config_host_mak echo "CONFIG_SMBD_COMMAND=\"$smbd\"" >> $config_host_mak
util/Makefile.objs +5 -0
@@ -42,6 +42,11 @@ util-obj-y += coroutine-$(CONFIG_COROUTINE_BACKEND).o ifeq ($(ARCH),x86_64) coroutine-asm.o-cflags := -mno-red-zone endif +ifeq ($(CONFIG_CF_PROTECTION),y) +coroutine-sigaltstack.o-cflags := -fcf-protection=branch +coroutine-ucontext.o-cflags := -fcf-protection=branch +coroutine-asm.o-cflags += -fcf-protection=branch +endif util-obj-y += buffer.o util-obj-y += timed-average.o util-obj-y += base64.o
Signature · 1 line
2.21.0
PATCH 7/9 tcg: add tcg_out_start
In reply to Paolo BonziniThis function is called at the beginning of any translation block. We will use it to emit ENDBR32 or ENDBR64 annotations for x86 CET.
--- tcg/aarch64/tcg-target.inc.c | 4 ++++ tcg/arm/tcg-target.inc.c | 4 ++++ tcg/i386/tcg-target.inc.c | 4 ++++ tcg/mips/tcg-target.inc.c | 4 ++++ tcg/ppc/tcg-target.inc.c | 4 ++++ tcg/riscv/tcg-target.inc.c | 4 ++++ tcg/s390/tcg-target.inc.c | 4 ++++ tcg/sparc/tcg-target.inc.c | 4 ++++ tcg/tcg.c | 2 ++ tcg/tci/tcg-target.inc.c | 4 ++++ 10 files changed, 38 insertions(+)
tcg/aarch64/tcg-target.inc.c +4 -0
@@ -2539,6 +2539,10 @@ QEMU_BUILD_BUG_ON(FRAME_SIZE >= (1 << 14));/* We're expecting to use a single ADDI insn. */ QEMU_BUILD_BUG_ON (FRAME_SIZE -PUSH_SIZE >0xfff ); +static inline void tcg_out_start (TCGContext *s) +{ +} +static void tcg_target_qemu_prologue (TCGContext *s) {TCGReg r;
tcg/arm/tcg-target.inc.c +4 -0
@@ -2284,6 +2284,10 @@ static void tcg_out_nop_fill(tcg_insn_unit *p, int count) } } +static inline void tcg_out_start (TCGContext *s) +{ +} +/* Compute frame size via macros, to share between tcg_target_qemu_prologue and tcg_register_jit. */
tcg/i386/tcg-target.inc.c +4 -0
@@ -3510,6 +3510,10 @@ static const int tcg_target_callee_save_regs[] = { + TCG_TARGET_STACK_ALIGN - 1) \ & ~(TCG_TARGET_STACK_ALIGN - 1)) +static inline void tcg_out_start (TCGContext *s) +{ +} +/* Generate global QEMU prologue and epilogue code */ static void tcg_target_qemu_prologue (TCGContext *s) {
tcg/mips/tcg-target.inc.c +4 -0
@@ -2471,6 +2471,10 @@ static tcg_insn_unit *align_code_ptr(TCGContext *s)return s->code_ptr; } +static inline void tcg_out_start (TCGContext *s) +{ +} +/* Stack frame parameters. */ #define REG_SIZE (TCG_TARGET_REG_BITS / 8)#define SAVE_SIZE ((int)ARRAY_SIZE(tcg_target_callee_save_regs) * REG_SIZE)
tcg/ppc/tcg-target.inc.c +4 -0
@@ -1904,6 +1904,10 @@ static void tcg_out_nop_fill(tcg_insn_unit *p, int count) } } +static inline void tcg_out_start (TCGContext *s) +{ +} +/* Parameters for function call generation, used in tcg.c. */ #define TCG_TARGET_STACK_ALIGN 16#define TCG_TARGET_EXTEND_ARGS 1
tcg/riscv/tcg-target.inc.c +4 -0
@@ -1798,6 +1798,10 @@ static const TCGTargetOpDef *tcg_target_op_def(TCGOpcode op) } } +static inline void tcg_out_start (TCGContext *s) +{ +} +static const int tcg_target_callee_save_regs[] = {TCG_REG_S0 ,/* used for the global env (TCG_AREG0) */ TCG_REG_S1 ,
tcg/s390/tcg-target.inc.c +4 -0
@@ -2499,6 +2499,10 @@ static void query_s390_facilities(void) } } +static inline void tcg_out_start (TCGContext *s) +{ +} +static void tcg_target_init (TCGContext *s) {query_s390_facilities ();
tcg/sparc/tcg-target.inc.c +4 -0
@@ -1004,6 +1004,10 @@ static void build_trampolines(TCGContext *s) }#endif +static inline void tcg_out_start (TCGContext *s) +{ +} +/* Generate global QEMU prologue and epilogue code */ static void tcg_target_qemu_prologue (TCGContext *s) {
tcg/tcg.c +2 -0
@@ -101,6 +101,7 @@ static void tcg_register_jit_int(void *buf, size_t size,/* Forward declarations for functions declared and used in tcg-target.inc.c. */ static const char *target_parse_constraint (TCGArgConstraint *ct,const char *ct_str,TCGType type); +static void tcg_out_start (TCGContext *s);static void tcg_out_ld (TCGContext *s,TCGType type,TCGReg ret,TCGReg arg1,intptr_t arg2);static void tcg_out_mov (TCGContext *s,TCGType type,TCGReg ret,TCGReg arg);
@@ -3925,6 +3926,7 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb)#endif num_insns =-1 ; +tcg_out_start (s); QTAILQ_FOREACH(op, &s->ops, link) {TCGOpcode opc = op->opc;
tcg/tci/tcg-target.inc.c +4 -0
@@ -889,6 +889,10 @@ static void tcg_target_init(TCGContext *s) CPU_TEMP_BUF_NLONGS * sizeof(long)); } +static inline void tcg_out_start (void ) +{ +} +/* Generate global QEMU prologue and epilogue code. */ static inline void tcg_target_qemu_prologue (TCGContext *s) {
Signature · 1 line
2.21.0
PATCH 8/9 tcg/i386: add support for IBT
In reply to Paolo BonziniAdd endbr annotations before indirect branch targets. This lets QEMU enable IBT even for TCG-enabled builds.
--- Makefile.target | 2 ++ configure | 9 +++++++++ include/qemu/cpuid.h | 5 +++++ tcg/i386/tcg-target.inc.c | 19 +++++++++++++++++++ 4 files changed, 35 insertions(+)
Makefile.target +2 -0
@@ -112,8 +112,10 @@ obj-y += accel/ obj-$(CONFIG_TCG) += tcg/tcg.o tcg/tcg-op.o tcg/tcg-op-vec.o tcg/tcg-op-gvec.o obj-$(CONFIG_TCG) += tcg/tcg-common.o tcg/optimize.o ifeq ($(CONFIG_CF_PROTECTION),y) +ifneq ($(CONFIG_CF_PROTECTION_TCG),y) tcg/tcg.o-cflags := -fcf-protection=return endif +endif obj-$(CONFIG_TCG_INTERPRETER) += tcg/tci.o obj-$(CONFIG_TCG_INTERPRETER) += disas/tci.o obj-$(CONFIG_TCG) += fpu/softfloat.o
configure +9 -0
@@ -5200,6 +5200,11 @@ fi if test "$cf_protection" = "yes"; then QEMU_CFLAGS="-fcf-protection $QEMU_CFLAGS" fi +if test "$cpu" = "x86_64"; then + cf_protection_tcg=yes +else + cf_protection_tcg=no +fi ########################################## # check and set a backend for coroutine
@@ -6395,6 +6400,7 @@ echo "TCG support $tcg" if test "$tcg" = "yes" ; then echo "TCG debug enabled $debug_tcg" echo "TCG interpreter $tcg_interpreter" + echo "TCG CF protection $cf_protection_tcg" fi echo "malloc trim support $malloc_trim" echo "RDMA support $rdma"
@@ -6600,6 +6606,9 @@ fi if test "$cf_protection" = "yes" ; then echo "CONFIG_CF_PROTECTION=y" >> $config_host_mak fi +if test "$cf_protection_tcg" = "yes" ; then + echo "CONFIG_CF_PROTECTION_TCG=y" >> $config_host_mak +fi if test "$slirp" != "no"; then echo "CONFIG_SLIRP=y" >> $config_host_mak echo "CONFIG_SMBD_COMMAND=\"$smbd\"" >> $config_host_mak
include/qemu/cpuid.h +5 -0
@@ -49,6 +49,11 @@#define bit_BMI2 (1 << 8)#endif +/* Leaf 7, %edx */ +#ifndef bit_IBT +#define bit_IBT (1 << 20) +#endif +/* Leaf 0x80000001, %ecx */ #ifndef bit_LZCNT#define bit_LZCNT (1 << 5)
tcg/i386/tcg-target.inc.c +19 -0
@@ -159,10 +159,12 @@ bool have_avx2;static bool have_movbe;static bool have_bmi2;static bool have_lzcnt; +static bool have_ibt;#else # define have_movbe 0# define have_bmi2 0# define have_lzcnt 0 +# define have_ibt 1#endif static tcg_insn_unit *tb_ret_addr;
@@ -809,6 +811,19 @@ static inline void tgen_arithr(TCGContext *s, int subop, int dest, int src)tcg_out_modrm (s, OPC_ARITH_GvEv + (subop <<3 ) + ext, dest, src); } +static void tcg_out_endbr (TCGContext *s) +{ +if (have_ibt) { +#if defined __CET__ && (__CET__ &1 ) +#ifdef __x86_64__ +tcg_out32 (s,0xfa1e0ff3 ); +#else +tcg_out32 (s,0xfb1e0ff3 ); +#endif +#endif + } +} +static void tcg_out_mov (TCGContext *s,TCGType type,TCGReg ret,TCGReg arg) {int rexw =0 ;
@@ -3512,6 +3527,7 @@ static const int tcg_target_callee_save_regs[] = {static inline void tcg_out_start (TCGContext *s) { +tcg_out_endbr (s); }/* Generate global QEMU prologue and epilogue code */
@@ -3520,6 +3536,7 @@ static void tcg_target_qemu_prologue(TCGContext *s)int i, stack_addend;/* TB prologue */ +tcg_out_endbr (s);/* Reserve some stack space, also for TCG temps. */ stack_addend =FRAME_SIZE -PUSH_SIZE ;
@@ -3566,6 +3583,7 @@ static void tcg_target_qemu_prologue(TCGContext *s)* and fall through to the rest of the epilogue. */ s->code_gen_epilogue = s->code_ptr; +tcg_out_endbr (s);tcg_out_movi (s,TCG_TYPE_REG ,TCG_REG_EAX ,0 );/* TB epilogue */
@@ -3598,6 +3616,7 @@ static void tcg_target_init(TCGContext *s)__cpuid_count (7 ,0 , a, b7, c, d); have_bmi1 = (b7 & bit_BMI) !=0 ; have_bmi2 = (b7 & bit_BMI2) !=0 ; + have_ibt = (d & bit_IBT) !=0 ; }if (max >=1 ) {
Signature · 1 line
2.21.0
Re: [Qemu-devel] [PATCH 8/9] tcg/i386: add support for IBT
Quoted 10 lines · Paolo Bonzini
Add endbr annotations before indirect branch targets. This lets QEMU enable IBT even for TCG-enabled builds.
Signed-off-by Paolo Bonzini <[email protected]>--- Makefile.target | 2 ++ configure | 9 +++++++++ include/qemu/cpuid.h | 5 +++++ tcg/i386/tcg-target.inc.c | 19 +++++++++++++++++++ 4 files changed, 35 insertions(+)
r~
PATCH 9/9 linux-user: add IBT support to x86 safe-syscall.S
In reply to Paolo BonziniBecause safe-syscall.S does not go through the C compiler, the .note.gnu.property note has to be added manually. Safe syscalls do not involve any indirect branch or stack unwinding, so they are trivially safe for IBT or shadow stacks.
--- linux-user/host/i386/safe-syscall.inc.S | 19 +++++++++++++++++++ linux-user/host/x86_64/safe-syscall.inc.S | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+)
linux-user/host/i386/safe-syscall.inc.S +19 -0
@@ -98,3 +98,22 @@ safe_syscall_end: .cfi_endproc .size safe_syscall_base, .-safe_syscall_base + + .pushsection ".note.gnu.property", "a" + .p2align 2 + .long 1f - 0f /* name length. */ + .long 4f - 1f /* data length. */ + .long 5 /* NT_GNU_PROPERTY_TYPE_0. */ +0: + .asciz "GNU" /* vendor name. */ + .p2align 2 +1: + /* GNU_PROPERTY_X86_FEATURE_1_AND. */ + .long 0xc0000002 /* pr_type. */ + .long 3f - 2f /* pr_datasz. */ +2: + .long 0x3 /* IBT, SHSTK */ +3: + .p2align 2 +4: + .popsection
linux-user/host/x86_64/safe-syscall.inc.S +19 -0
@@ -89,3 +89,22 @@ safe_syscall_end: .cfi_endproc .size safe_syscall_base, .-safe_syscall_base + + .pushsection ".note.gnu.property", "a" + .p2align 3 + .long 1f - 0f /* name length. */ + .long 4f - 1f /* data length. */ + .long 5 /* NT_GNU_PROPERTY_TYPE_0. */ +0: + .asciz "GNU" /* vendor name. */ + .p2align 3 +1: + /* GNU_PROPERTY_X86_FEATURE_1_AND. */ + .long 0xc0000002 /* pr_type. */ + .long 3f - 2f /* pr_datasz. */ +2: + .long 0x3 /* IBT, SHSTK */ +3: + .p2align 3 +4: + .popsection
Signature · 1 line
2.21.0
[Qemu-devel] [PATCH 10/9] coroutine-asm: add x86 CET shadow stack support
In reply to Paolo BonziniNote that the ABI is not yet part of Linux; this patch is not intended to be committed until that is approved.
--- configure | 14 ++++++++ util/Makefile.objs | 2 ++ util/coroutine-asm.c | 82 ++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 96 insertions(+), 2 deletions(-)
configure +14 -0
@@ -5192,6 +5192,20 @@ if test "$cf_protection" != no; then feature_not_found "cf_protection" 'Control-flow protection is not supported by your toolchain' fi cf_protection=no + else + if test $cpu = x86_64; then + # only needed by coroutine-asm.c, however it should be rare to have + # CET support in the compiler but not in binutils + cat > $TMPC << EOF +int main(void) { asm("rdsspq %%rax" : : : "rax"); } +EOF + if ! compile_prog "" "" ; then + if test "$cf_protection" = yes; then + feature_not_found "cf_protection" 'CET is not supported by your toolchain' + fi + cf_protection=no + fi + fi fi fi if test "$cf_protection" = ""; then
util/Makefile.objs +2 -0
@@ -45,8 +45,10 @@ endif ifeq ($(CONFIG_CF_PROTECTION),y) coroutine-sigaltstack.o-cflags := -fcf-protection=branch coroutine-ucontext.o-cflags := -fcf-protection=branch +ifneq ($(ARCH),x86_64) coroutine-asm.o-cflags += -fcf-protection=branch endif +endif util-obj-y += buffer.o util-obj-y += timed-average.o util-obj-y += base64.o
util/coroutine-asm.c +80 -2
136 lines not on this page · raw message
Signature · 1 line
2.21.0
Quoted 1 line · Paolo Bonzini
*** BLURB HERE ***
I assume there was going to be a bit more background here?
--
Alex Bennée
Quoted 7 lines · Alex Bennée
Quoted 1 line · Paolo Bonzini
*** BLURB HERE ***
I assume there was going to be a bit more background here?
Mmm, could we have the rationale, please ?
thanks
-- PMM
Quoted 10 lines · Peter Maydell
Quoted 7 lines · Alex Bennée
Quoted 1 line · Paolo Bonzini
*** BLURB HERE ***
I assume there was going to be a bit more background here?
Mmm, could we have the rationale, please ?
Paolo can add more if necessary, but my understanding is:
1. It's required for Intel Control-flow Enforcement Technology (CET). The existing ucontext backend doesn't work with CET.
2. It's faster than the existing ucontext implementation.
Quoted 11 lines · Stefan Hajnoczi
Quoted 9 lines · Peter Maydell
Quoted 7 lines · Alex Bennée
Quoted 1 line · Paolo Bonzini
*** BLURB HERE ***
I assume there was going to be a bit more background here?
Quoted 7 lines
Quoted 1 line
Mmm, could we have the rationale, please ?
Paolo can add more if necessary, but my understanding is:
1. It's required for Intel Control-flow Enforcement Technology (CET). The existing ucontext backend doesn't work with CET.
2. It's faster than the existing ucontext implementation.
Mmm, I think we've talked about 1 before, but I think it would be useful to clearly state why we need to do things here. It's also useful for identifying whether we need an asm backend for every host, or only some hosts (and if so which).
I'm unconvinced by 2 as a rationale for adding more host asm. Coroutines were already bad enough when they were at least vaguely portable C code.
thanks
-- PMM
Quoted 7 lines · Peter Maydell
Quoted 3 lines · Stefan Hajnoczi
1. It's required for Intel Control-flow Enforcement Technology (CET). The existing ucontext backend doesn't work with CET.
2. It's faster than the existing ucontext implementation.Mmm, I think we've talked about 1 before, but I think it would be useful to clearly state why we need to do things here.
The reason is that, with CET enabled, setjmp and longjmp assume that they are used only to unwind the stack and not to switch to a completely different one. You are supposed to use swapcontext for that, but it doesn't work for QEMU coroutines because it saves/restores the signal mask; that is not only slower, it's incorrect we want the signal mask to be a property of the thread, not the coroutine.
Quoted 2 lines
It's also useful for identifying whether we need an asm backend for every host, or only some hosts (and if so which).
It's not needed for _any_ host (except x86 if you want CET support). I wrote these three backends to ensure that it could be ported without much effort on any host. If you prefer not having an aarch64 backend, for example, I can leave it out.
Paolo
Quoted 30 lines · Paolo Bonzini
*** BLURB HERE ***
Paolo Bonzini (10):
qemugdb: allow adding support for other coroutine backends
qemugdb: allow adding support for other architectures
coroutine: add host specific coroutine backend for 64-bit x86
coroutine: add host specific coroutine backend for 64-bit ARM
coroutine: add host specific coroutine backend for 64-bit s390
configure: add control-flow protection support
tcg: addtcg_out_start
tcg/i386: add support for IBT
linux-user: add IBT support to x86safe-syscall.S
coroutine-asm: add x86 CET shadow stack supportMakefile.target | 5 + configure | 62 ++++ include/qemu/cpuid.h | 5 + linux-user/host/i386/safe-syscall.inc.S | 19 ++ linux-user/host/x86_64/safe-syscall.inc.S | 19 ++ scripts/qemugdb/coroutine.py | 107 ++---- scripts/qemugdb/coroutine_asm.py | 24 ++ scripts/qemugdb/coroutine_ucontext.py | 69 ++++ tcg/aarch64/tcg-target.inc.c | 4 + tcg/arm/tcg-target.inc.c | 4 + tcg/i386/tcg-target.inc.c | 23 ++ tcg/mips/tcg-target.inc.c | 4 + tcg/ppc/tcg-target.inc.c | 4 + tcg/riscv/tcg-target.inc.c | 4 + tcg/s390/tcg-target.inc.c | 4 + tcg/sparc/tcg-target.inc.c | 4 +
Quoted 13 lines
tcg/tcg.c | 2 + tcg/tci/tcg-target.inc.c | 4 + util/Makefile.objs | 10 + util/coroutine-asm.c | 387 ++++++++++++++++++++++ 20 files changed, 689 insertions(+), 75 deletions(-) create mode 100644 scripts/qemugdb/coroutine_asm.py create mode 100644 scripts/qemugdb/coroutine_ucontext.py create mode 100644 util/coroutine-asm.cSignature · 1 line
2.21.0
Aside from the comments I posted: