From nobody Sat Nov 8 05:40:39 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 1545336984128461.07850221797594; Thu, 20 Dec 2018 12:16:24 -0800 (PST) Received: from localhost ([::1]:39707 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ga4k1-0005Dq-Uu for importer@patchew.org; Thu, 20 Dec 2018 15:16:21 -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) 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) 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-Google-Smtp-Source: AFSGD/WKgTPFa1Do5nN9w5rrBCvPtccoo2gZn0jIrW6QrU/ArtXIoSOCDdPu8Xn8hcuK89dS3gCWpA== X-Received: by 2002:a17:902:8d8e:: with SMTP id v14mr25217009plo.133.1545336904508; Thu, 20 Dec 2018 12:15:04 -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+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" Signed-off-by: Richard Henderson Reviewed-by: Alistair Francis --- 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 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