From patchwork Thu Dec 20 20:15:02 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 154366 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp95246ljp; Thu, 20 Dec 2018 12:15:32 -0800 (PST) X-Google-Smtp-Source: AFSGD/UBI26nqC8ij+K4LF+Em5f+BaRQu3kvicJf8edyOV+ovmo1644ZZa2pGTYw9LdUbpZcx8sB X-Received: by 2002:ae9:ef14:: with SMTP id d20mr26468700qkg.147.1545336932059; Thu, 20 Dec 2018 12:15:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545336932; cv=none; d=google.com; s=arc-20160816; b=TT7dNtn1q3avaj7/OSzyrp9v4wpqgH6c+EjP1e676j5vw2Jva42OpE+cL6UoxC4RQI VdXSLeE9cdCAQugktXu51HkAVBM9Wn9FqDTM2b2nq/ZTbf8xJ0GuQT4h8g57ydsPCul6 +E1QWAKooIf6YeA8DWANJRdhYKXWO+Etq9Q5eBj64uI6r7S367L3ivqL/vpG4WkkW4+v lPX/Skrx1dnyoN4Xvt57XX7filr1Pk1RIum1BpAgGLDF2xwCCTZYiMVhtDG15q/HLJSf RvL8q6PAVWKPtbLQVg3K3pSciA5p03x8HpA4GRe5LGaSufSRPHb22+lkGqZJr1/+jeql iKsA== 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:message-id:date:to:from :dkim-signature; bh=x7ZkkSKSVr4HyIt0BhAOIX/c8oxP6mRSBclCC7pVpBk=; b=tqn5V5k4yMhQByk3O1LY/qaGQk6Hr+0CLxScTCHTwHyuGxZpFid40wIX/TplXfRO7T bdg7FctaGM/YOyf5KizOLlxD0kTnZTtXnRTn6Nm/XuLmsA5oe5XCzHaayzyBNZR7L9Uw wi6gvJNhMYRi0jjcq86pKF5znYz5rWkdGlXnCIvdfbpmxZZk+ttuyvQgosxlEMYFnBul JZtadNfuAHcEGYIxw4Q9vSV38V+OC6X8lNl9VzB4hlxtnM5vbrE9wPOT6gUNXT7ys9qc gKg7fxFtMh3y1TarTb+wP6wK+8I1c5etwganJ921j/LPeB3N53aA4JG5n3h6aAc2QxHq F3+w== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b="CVy2bI/h"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 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. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id e29si2636060qtg.311.2018.12.20.12.15.31 for (version=TLS1 cipher=AES128-SHA bits=128/128); Thu, 20 Dec 2018 12:15:32 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b="CVy2bI/h"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 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 ([::1]:39702 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ga4jD-0003mg-Hz for patch@linaro.org; Thu, 20 Dec 2018 15:15:31 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59818) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ga4it-0003ku-Bp for qemu-devel@nongnu.org; Thu, 20 Dec 2018 15:15:12 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ga4io-0001XE-DN for qemu-devel@nongnu.org; Thu, 20 Dec 2018 15:15:11 -0500 Received: from mail-pl1-x644.google.com ([2607:f8b0:4864:20::644]:38443) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ga4io-0001Tv-61 for qemu-devel@nongnu.org; Thu, 20 Dec 2018 15:15:06 -0500 Received: by mail-pl1-x644.google.com with SMTP id e5so1378306plb.5 for ; Thu, 20 Dec 2018 12:15:05 -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; bh=x7ZkkSKSVr4HyIt0BhAOIX/c8oxP6mRSBclCC7pVpBk=; b=CVy2bI/hGO2VbyxQo4nLydwyoOejQBvw5r+znk2Aukwca78tTDPKuh/ymqx7dH7aLc 7FS92qBw7gKLb3J16CQ8NrTZ27OQaUwDj+gwLmeVTJQP/m00g43FhRBQ0pFel5gfsI4J KOYeWh7bW4Sge6KSB51ISz12ND1UQ1L8x8Jf0= 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; bh=x7ZkkSKSVr4HyIt0BhAOIX/c8oxP6mRSBclCC7pVpBk=; b=UBCu8xjIOylKWIIovRXwo5yp1V9dHYE2U/JFpoFEWx7d5Is2S/fIwECtIYBpP/mO53 BQcBHmi0RU3nxFeffyTuVALIL0j6llbsKrps8h0lkFFHCXacgJbrT2a1kVDPH9jJlIbd ifaGENkowmcdDw7BPy6bpEogg3O6jSLEQa1w5DMiNAe6pwUvztUW9cikTeCUZCAilXSU nGJlFMiTh/a39kRT1zQjwhA4cogTuEG1y3L2vIYzr3F5OUPZBO/BcvB6penA5F1BoB+V gkySK3YQuqMslgX87URWH6psIdPqyZzsvXfGQaY4DHTJeMvtm3NGuPU5NJtJ2w3/4j+u lqPA== X-Gm-Message-State: AA+aEWaX7XUvNqhkNp/KBAVsbn9Ry4NGjusSj2aaFp8Qr8ww0CRkGDif Khi0tIekpsxu0GWJR505neyZMqy7mjU= X-Received: by 2002:a17:902:8d8e:: with SMTP id v14mr25217009plo.133.1545336904508; Thu, 20 Dec 2018 12:15:04 -0800 (PST) Received: from cloudburst.twiddle.net (97-126-115-157.tukw.qwest.net. [97.126.115.157]) by smtp.gmail.com with ESMTPSA id p67sm37351426pfg.44.2018.12.20.12.15.03 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 20 Dec 2018 12:15:03 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Date: Thu, 20 Dec 2018 12:15:02 -0800 Message-Id: <20181220201502.12285-1-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.2 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::644 Subject: [Qemu-devel] [PATCH] 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: palmer@sifive.com, Alistair.Francis@wdc.com, laurent@vivier.eu Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" Signed-off-by: Richard Henderson --- At some point we should make this routine be non-optional for porting to a new host. r~ --- 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 Reviewed-by: Alistair Francis 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