From nobody Thu Oct 2 10:17:54 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1545772219021487.08464776190954; Tue, 25 Dec 2018 13:10:19 -0800 (PST) Received: from localhost ([127.0.0.1]:43131 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gbtxx-0003yL-72 for importer@patchew.org; Tue, 25 Dec 2018 16:10:17 -0500 Received: from eggs.gnu.org ([208.118.235.92]:50413) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gbtmt-0006rI-VA for qemu-devel@nongnu.org; Tue, 25 Dec 2018 15:58:54 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gbtms-0000hI-La for qemu-devel@nongnu.org; Tue, 25 Dec 2018 15:58:51 -0500 Received: from mail-pl1-x642.google.com ([2607:f8b0:4864:20::642]:46866) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gbtms-0000gi-FV for qemu-devel@nongnu.org; Tue, 25 Dec 2018 15:58:50 -0500 Received: by mail-pl1-x642.google.com with SMTP id t13so6794748ply.13 for ; Tue, 25 Dec 2018 12:58:50 -0800 (PST) Received: from cloudburst.home (c211-28-135-144.sunsh3.vic.optusnet.com.au. [211.28.135.144]) by smtp.gmail.com with ESMTPSA id t21sm48501628pgg.24.2018.12.25.12.58.43 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 25 Dec 2018 12:58:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=I0GsNGrZ/H+N2fHj/vHzPDrT4zyvgJaCaJbR3/YtAGE=; b=kRO1/XaEhBuWo9ALCz2Sx+zH0UPYgJnnuD5498WuZbbP0FfsNeDkawx6IGKhbkvCEo vRn1lEkqbf7z3xZsJN9YzSquhFGyaOL9iJ6529lkvn1fslAr8je4R7NluXKRtTRkL9rc 8QGkR6HVNsMjmE3Yg+Pe0sj4zR3u4U4cSYBrs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=I0GsNGrZ/H+N2fHj/vHzPDrT4zyvgJaCaJbR3/YtAGE=; b=INBCUposnhj3DzVhZt0uT195qsJXy9/cjNNHyoWH8oxNxoNxgz8LdbWpNYiyhmulY3 zhlaG3hnh2QLR1Zb1QrC5cTQHXJDPgCqlVnKjRZEoUv4trgKpXigq3FdVf0aw/64fP2S HVPJDQOutwdxZUevAhqReOCpYFnQiMnqmCVmnYsz3l7AGMMvXAsFl9nfNnzRQDZiur5c gMXCyVEEoxxq/WFGwymwVLkpeLk3pOskZo7dxPNsaECk/+qtYx+MJBEHfLJsocKw6pkr iMJddl3nu/2K/YsrumjTG0gGWCznBdvYeoTJMN83QZ8gOm6+OhHCJ9rFsjMdkxHJ9Wtd wFqA== X-Gm-Message-State: AJcUukeMKm0h5AEW/pqcpol9m64Wk1RBA3tvUT5aSKcdPLngAWDK5feh XuD2ZWB6CZhazktcBaX/ulzoM39T6vA= X-Google-Smtp-Source: ALg8bN6RWu5mRtU+oFchBKZnnL3CtSCFHBVcLwAj10rdVnI5UjbX1h7LHPoU+Q1b1VhA8ws9Bm+d8A== X-Received: by 2002:a17:902:6502:: with SMTP id b2mr17221412plk.44.1545771529264; Tue, 25 Dec 2018 12:58:49 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Date: Wed, 26 Dec 2018 07:55:12 +1100 Message-Id: <20181225205529.10874-26-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20181225205529.10874-1-richard.henderson@linaro.org> References: <20181225205529.10874-1-richard.henderson@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::642 Subject: [Qemu-devel] [PULL 25/42] linux-user: Add safe_syscall for riscv64 host X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Reviewed-by: Alistair Francis Signed-off-by: Richard Henderson --- linux-user/host/riscv64/hostdep.h | 23 +++++++ linux-user/host/riscv64/safe-syscall.inc.S | 77 ++++++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 linux-user/host/riscv64/safe-syscall.inc.S diff --git a/linux-user/host/riscv64/hostdep.h b/linux-user/host/riscv64/ho= stdep.h index 28467ba00b..865f0fb9ff 100644 --- a/linux-user/host/riscv64/hostdep.h +++ b/linux-user/host/riscv64/hostdep.h @@ -8,4 +8,27 @@ #ifndef RISCV64_HOSTDEP_H #define RISCV64_HOSTDEP_H =20 +/* We have a safe-syscall.inc.S */ +#define HAVE_SAFE_SYSCALL + +#ifndef __ASSEMBLER__ + +/* These are defined by the safe-syscall.inc.S file */ +extern char safe_syscall_start[]; +extern char safe_syscall_end[]; + +/* Adjust the signal context to rewind out of safe-syscall if we're in it = */ +static inline void rewind_if_in_safe_syscall(void *puc) +{ + ucontext_t *uc =3D puc; + unsigned long *pcreg =3D &uc->uc_mcontext.__gregs[REG_PC]; + + if (*pcreg > (uintptr_t)safe_syscall_start + && *pcreg < (uintptr_t)safe_syscall_end) { + *pcreg =3D (uintptr_t)safe_syscall_start; + } +} + +#endif /* __ASSEMBLER__ */ + #endif diff --git a/linux-user/host/riscv64/safe-syscall.inc.S b/linux-user/host/r= iscv64/safe-syscall.inc.S new file mode 100644 index 0000000000..9ca3fbfd1e --- /dev/null +++ b/linux-user/host/riscv64/safe-syscall.inc.S @@ -0,0 +1,77 @@ +/* + * safe-syscall.inc.S : host-specific assembly fragment + * to handle signals occurring at the same time as system calls. + * This is intended to be included by linux-user/safe-syscall.S + * + * Written by Richard Henderson + * Copyright (C) 2018 Linaro, Inc. + * + * This work is licensed under the terms of the GNU GPL, version 2 or late= r. + * See the COPYING file in the top-level directory. + */ + + .global safe_syscall_base + .global safe_syscall_start + .global safe_syscall_end + .type safe_syscall_base, @function + .type safe_syscall_start, @function + .type safe_syscall_end, @function + + /* + * This is the entry point for making a system call. The calling + * convention here is that of a C varargs function with the + * first argument an 'int *' to the signal_pending flag, the + * second one the system call number (as a 'long'), and all further + * arguments being syscall arguments (also 'long'). + * We return a long which is the syscall's return value, which + * may be negative-errno on failure. Conversion to the + * -1-and-errno-set convention is done by the calling wrapper. + */ +safe_syscall_base: + .cfi_startproc + /* + * The syscall calling convention is nearly the same as C: + * we enter with a0 =3D=3D *signal_pending + * a1 =3D=3D syscall number + * a2 ... a7 =3D=3D syscall arguments + * and return the result in a0 + * and the syscall instruction needs + * a7 =3D=3D syscall number + * a0 ... a5 =3D=3D syscall arguments + * and returns the result in a0 + * Shuffle everything around appropriately. + */ + mv t0, a0 /* signal_pending pointer */ + mv t1, a1 /* syscall number */ + mv a0, a2 /* syscall arguments */ + mv a1, a3 + mv a2, a4 + mv a3, a5 + mv a4, a6 + mv a5, a7 + mv a7, t1 + + /* + * This next sequence of code works in conjunction with the + * rewind_if_safe_syscall_function(). If a signal is taken + * and the interrupted PC is anywhere between 'safe_syscall_start' + * and 'safe_syscall_end' then we rewind it to 'safe_syscall_start'. + * The code sequence must therefore be able to cope with this, and + * the syscall instruction must be the final one in the sequence. + */ +safe_syscall_start: + /* If signal_pending is non-zero, don't do the call */ + lw t1, 0(t0) + bnez t1, 0f + scall +safe_syscall_end: + /* code path for having successfully executed the syscall */ + ret + +0: + /* code path when we didn't execute the syscall */ + li a0, -TARGET_ERESTARTSYS + ret + .cfi_endproc + + .size safe_syscall_base, .-safe_syscall_base --=20 2.17.2