From patchwork Tue Dec 25 20:55:12 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 154486 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp5216286ljp; Tue, 25 Dec 2018 13:08:45 -0800 (PST) X-Google-Smtp-Source: ALg8bN6Hp/gKUE/PcTJPXfM7l7PXW85izzv4APikHOdwXm5R8frtM6bfueB9oYlrNi60hyyVC/+9 X-Received: by 2002:a37:a28d:: with SMTP id l135mr16159728qke.226.1545772125622; Tue, 25 Dec 2018 13:08:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545772125; cv=none; d=google.com; s=arc-20160816; b=XldJiAMj/Bcr1Z0JgV15sdTSyecf9VCim+yXMpVzSa11mc5G07zMdIJ4vHi+fbuPfZ b07OL0oYiaYkc+LF2E3/cuQKtxiYpXirPxu7j8WG10CCdyNr0zdTl3GZIL1cNDQNomN9 oUxYyNPvuZEncvzoFxUG8NVqbIEnCG1rKrqajyh8tF0siE8DWueZ241WVkinUNk0fGW/ bFQgR+QDNM+E4vHkk9q9IPuOrwlxu4BhAis82VZNa5rTeaU7Az9Aaqp8jN/JXRxriGRb gbj00TO/aAF96bw7xZQN9TrS0lGXJ7V9l8+2xja/thn+hjBJJsw7q04Y/eSgeWqzkTlI QuWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:references:in-reply-to :message-id:date:to:from:dkim-signature; bh=I0GsNGrZ/H+N2fHj/vHzPDrT4zyvgJaCaJbR3/YtAGE=; b=AthMDYzLUedc+iNU43eLcVKETMGwzODvjAUfvdVDYyqH+abkjVKRbPzq2p0PSSgvcu FDYrMBNKhQdxY5hTGQq9CQ7fjM/RSj6+feVfx0LTDe8m3vfaV1t1Kz9LQe0AHmZVzwsz S32p0JueGZgELl7Qila9xswIBUs74BAuvvEtgQmK8bjDFmtdLPzeIcix2k6fFf5lus8Q y18PPaufEFTxYn8syh1bgSLtzrbvZAcg5RCn5A16Df4RkPkvQReUtd8Fi/VpBAmiN0iN TKIa21mBOrfQseMBorTKdqDNHR/G63ZGwwQ+m8toAiwnc8ZTU3ZlAD/3J0rgCb4Lmfg7 zmvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b="kRO1/XaE"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [208.118.235.17]) by mx.google.com with ESMTPS id b16si8127290qtp.115.2018.12.25.13.08.45 for (version=TLS1 cipher=AES128-SHA bits=128/128); Tue, 25 Dec 2018 13:08:45 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b="kRO1/XaE"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([127.0.0.1]:43121 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gbtwT-0002Ap-1P for patch@linaro.org; Tue, 25 Dec 2018 16:08:45 -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) 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-Received: by 2002:a17:902:6502:: with SMTP id b2mr17221412plk.44.1545771529264; Tue, 25 Dec 2018 12:58:49 -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) 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+patch=linaro.org@nongnu.org Sender: "Qemu-devel" 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 -- 2.17.2 diff --git a/linux-user/host/riscv64/hostdep.h b/linux-user/host/riscv64/hostdep.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 +/* 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 = puc; + unsigned long *pcreg = &uc->uc_mcontext.__gregs[REG_PC]; + + if (*pcreg > (uintptr_t)safe_syscall_start + && *pcreg < (uintptr_t)safe_syscall_end) { + *pcreg = (uintptr_t)safe_syscall_start; + } +} + +#endif /* __ASSEMBLER__ */ + #endif diff --git a/linux-user/host/riscv64/safe-syscall.inc.S b/linux-user/host/riscv64/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 later. + * 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 == *signal_pending + * a1 == syscall number + * a2 ... a7 == syscall arguments + * and return the result in a0 + * and the syscall instruction needs + * a7 == syscall number + * a0 ... a5 == 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