From patchwork Wed Oct 23 04:37:41 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hajime Tazaki X-Patchwork-Id: 1181789 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="jRU0zpxJ"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="gN/UZMiG"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46yd1r29DNz9sPV for ; Wed, 23 Oct 2019 15:39:12 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=h8IdV3L6oX7o2t0DRer7Odeb79trUYkE5QQ7I6P884Y=; b=jRU0zpxJ0od6Zk e1njyXNML/vpZ96U2pUFsm+gjcxyEh9A2GtxnS1i3XoZMVOgP6iBpaqiHP+gwLu41Lpf1NYslu1ai s2NapnDGmsSVnNd9IMSfHjZQfGoKOoKFENbw45z9ST/uA2tBHXYRIasFiZQ+VX5wXsul2hCUGJZek MmZlOECguHc8E+AfAt26FPbwkhBXVulIanMXhFQ/XWR9Tt7uU+uR86DWuOf7ChZTrj5OXo7qenBsp R0cy2sKL0SRYPM4kb5LyFr7FB314naiC4vO7ZN6ymaF+AFYHijFOJdyG0nneC/0tqyg39Hh6cl0BB l+KvtmMx+1KQ2pqKj4Kw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1iN8QK-0001oj-RB; Wed, 23 Oct 2019 04:39:04 +0000 Received: from mail-pf1-x444.google.com ([2607:f8b0:4864:20::444]) by bombadil.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1iN8QG-0001k5-QK for linux-um@lists.infradead.org; Wed, 23 Oct 2019 04:39:03 +0000 Received: by mail-pf1-x444.google.com with SMTP id b4so3160751pfr.12 for ; Tue, 22 Oct 2019 21:39:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=DBRurIYGvEGFFItuD/yBDy2cEi/ZXnIHhq/6bC2lVjE=; b=gN/UZMiG++I5HW4j2IV6802QcE0nqgHLOa2yFHr1XZANyYKHbYhvo6kwBMDgbg+5xy Rk/qGmqh9zbEBMEHBXuXtEgRLhRfJ3v9PPO/oBG/IkvDzMH0wpn/jgOrRxni+FRf+JIc FM3oQ+UnS1a1DsqupWXf91hPaEmeKZFLsOJTDb+R4OfIyk9XerGIzfSZZ/9o1OM011N3 AWyXnLqCy2mccA5Rr0sMwtHqRyiW7epOSUUWbblEjU3YQE5qxc4Pol2TkLvKVeQJ4YEM 6dusOVjZ/+HPUtiYTjnZRJylkBBjAlQzREBGuKK0mxGPAGl0Bj+zxgAavsBnl3y8Z8bL P1hA== 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:mime-version:content-transfer-encoding; bh=DBRurIYGvEGFFItuD/yBDy2cEi/ZXnIHhq/6bC2lVjE=; b=dw/An4ghM35ZYWaigfZYXisU2tKUXlDjqsJZ8fIYBT4D9tpFSDtxwdkDhN+U91sKNq 8Xxpa3iBTVC5q4sxGYa/puznWnuC2ZxHmD++mRpr2kaeuPnq/axCZRMQ98TzX3q61fi4 B0Gvm3150ITPJ/vp0jLyceSF3mm2j7RtugMFSUjWT92J2phdKQXUC7CiK279kSzzyvx+ 2J4mBYfht1fX6toRCtMaRUJX2I6rIkdwkJeXcv6x56oadff53Oife6xD4Zc/0USIbCDe E0d+PrEJBNpONp7Vb/q7gwFR3U0IHu+1pZpVFylWmIoaufO30F//HlNvrQ8AY1jUZgdD km+g== X-Gm-Message-State: APjAAAX49jV71NscyqB3PkcZXKh5lUv9M/c+Qxlhm9VVo+8nXsxOxLSk CLS0PVkNO/IHnyrlopCpNT79M7w429J27A== X-Google-Smtp-Source: APXvYqzG8KNpDnO7JMCemVNUqUb7Ay21vGON3OBAKqP2pIugXIcXJsr+W2T2pW/amJQeo1t7xwbrFQ== X-Received: by 2002:a62:2b94:: with SMTP id r142mr8261440pfr.251.1571805539671; Tue, 22 Oct 2019 21:38:59 -0700 (PDT) Received: from earth-mac.local ([202.214.86.179]) by smtp.gmail.com with ESMTPSA id h68sm23988662pfb.149.2019.10.22.21.38.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 22 Oct 2019 21:38:57 -0700 (PDT) Received: by earth-mac.local (Postfix, from userid 501) id 554D3201995800; Wed, 23 Oct 2019 13:38:53 +0900 (JST) From: Hajime Tazaki To: linux-um@lists.infradead.org Subject: [RFC PATCH 07/47] lkl: interrupt support Date: Wed, 23 Oct 2019 13:37:41 +0900 Message-Id: <75e5d3a80edbe076626381be77d437ee1db8dba5.1571798507.git.thehajime@gmail.com> X-Mailer: git-send-email 2.20.1 (Apple Git-117) In-Reply-To: References: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20191022_213900_866469_628EF424 X-CRM114-Status: GOOD ( 24.32 ) X-Spam-Score: -0.2 (/) X-Spam-Report: SpamAssassin version 3.4.2 on bombadil.infradead.org summary: Content analysis details: (-0.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:444 listed in] [list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (thehajime[at]gmail.com) -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid X-BeenThere: linux-um@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Octavian Purdila , Michael Zimmermann , Hajime Tazaki , Akira Moroo Sender: "linux-um" Errors-To: linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org From: Octavian Purdila Add APIs that allows the host to reserve and free and interrupt number and also to trigger an interrupt. The trigger operation will simply store the interrupt data in queue. The interrupt handler is run later, at the first opportunity it has to avoid races with any kernel threads. Currently, interrupts are run on the first interrupt enable operation if interrupts are disabled and if we are not already in interrupt context. When triggering an interrupt the host can also send a void pointer that is going to be available to the handler routine via get_irq_regs()->irq_data. This allows to easly create host <-> kernel synchronous communication channels and is currently used by the system call interface. Signed-off-by: Hajime Tazaki Signed-off-by: Michael Zimmermann Signed-off-by: Octavian Purdila --- arch/um/lkl/include/asm/irq.h | 15 ++ arch/um/lkl/include/uapi/asm/irq.h | 36 ++++ arch/um/lkl/include/uapi/asm/sigcontext.h | 16 ++ arch/um/lkl/kernel/irq.c | 193 ++++++++++++++++++++++ 4 files changed, 260 insertions(+) create mode 100644 arch/um/lkl/include/asm/irq.h create mode 100644 arch/um/lkl/include/uapi/asm/irq.h create mode 100644 arch/um/lkl/include/uapi/asm/sigcontext.h create mode 100644 arch/um/lkl/kernel/irq.c diff --git a/arch/um/lkl/include/asm/irq.h b/arch/um/lkl/include/asm/irq.h new file mode 100644 index 000000000000..36af9e36be1c --- /dev/null +++ b/arch/um/lkl/include/asm/irq.h @@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_LKL_IRQ_H +#define _ASM_LKL_IRQ_H + +#ifndef __arch_um__ +#define IRQ_STATUS_BITS (sizeof(long) * 8) +#define NR_IRQS ((int)(IRQ_STATUS_BITS * IRQ_STATUS_BITS)) +#endif /* __arch_um__ */ + +void run_irqs(void); +void set_irq_pending(int irq); + +#include + +#endif diff --git a/arch/um/lkl/include/uapi/asm/irq.h b/arch/um/lkl/include/uapi/asm/irq.h new file mode 100644 index 000000000000..666628b233eb --- /dev/null +++ b/arch/um/lkl/include/uapi/asm/irq.h @@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef _ASM_UAPI_LKL_IRQ_H +#define _ASM_UAPI_LKL_IRQ_H + +/** + * lkl_trigger_irq - generate an interrupt + * + * This function is used by the device host side to signal its Linux counterpart + * that some event happened. + * + * @irq - the irq number to signal + */ +int lkl_trigger_irq(int irq); + +/** + * lkl_get_free_irq - find and reserve a free IRQ number + * + * This function is called by the host device code to find an unused IRQ number + * and reserved it for its own use. + * + * @user - a string to identify the user + * @returns - and irq number that can be used by request_irq or an negative + * value in case of an error + */ +int lkl_get_free_irq(const char *user); + +/** + * lkl_put_irq - release an IRQ number previously obtained with lkl_get_free_irq + * + * @irq - irq number to release + * @user - string identifying the user; should be the same as the one passed to + * lkl_get_free_irq when the irq number was obtained + */ +void lkl_put_irq(int irq, const char *name); + +#endif diff --git a/arch/um/lkl/include/uapi/asm/sigcontext.h b/arch/um/lkl/include/uapi/asm/sigcontext.h new file mode 100644 index 000000000000..2f4848843d1d --- /dev/null +++ b/arch/um/lkl/include/uapi/asm/sigcontext.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef _ASM_UAPI_LKL_SIGCONTEXT_H +#define _ASM_UAPI_LKL_SIGCONTEXT_H + +#include + +struct pt_regs { + void *irq_data; +}; + +struct sigcontext { + struct pt_regs regs; + unsigned long oldmask; +}; + +#endif diff --git a/arch/um/lkl/kernel/irq.c b/arch/um/lkl/kernel/irq.c new file mode 100644 index 000000000000..e3b59e46ca50 --- /dev/null +++ b/arch/um/lkl/kernel/irq.c @@ -0,0 +1,193 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * To avoid much overhead we use an indirect approach: the irqs are marked using + * a bitmap (array of longs) and a summary of the modified bits is kept in a + * separate "index" long - one bit for each sizeof(long). Thus we can support + * 4096 irqs on 64bit platforms and 1024 irqs on 32bit platforms. + * + * Whenever an irq is trigger both the array and the index is updated. To find + * which irqs were triggered we first search the index and then the + * corresponding part of the arrary. + */ +static unsigned long irq_status[NR_IRQS / IRQ_STATUS_BITS]; +static unsigned long irq_index_status; + +static inline unsigned long test_and_clear_irq_index_status(void) +{ + if (!irq_index_status) + return 0; + return __sync_fetch_and_and(&irq_index_status, 0); +} + +static inline unsigned long test_and_clear_irq_status(int index) +{ + if (!&irq_status[index]) + return 0; + return __sync_fetch_and_and(&irq_status[index], 0); +} + +void set_irq_pending(int irq) +{ + int index = irq / IRQ_STATUS_BITS; + int bit = irq % IRQ_STATUS_BITS; + + __sync_fetch_and_or(&irq_status[index], BIT(bit)); + __sync_fetch_and_or(&irq_index_status, BIT(index)); +} + +static struct irq_info { + const char *user; +} irqs[NR_IRQS]; + +static bool irqs_enabled; + +static struct pt_regs dummy; + +static void run_irq(int irq) +{ + unsigned long flags; + struct pt_regs *old_regs = set_irq_regs((struct pt_regs *)&dummy); + + /* interrupt handlers need to run with interrupts disabled */ + local_irq_save(flags); + irq_enter(); + generic_handle_irq(irq); + irq_exit(); + set_irq_regs(old_regs); + local_irq_restore(flags); +} + +/** + * This function can be called from arbitrary host threads, so do not + * issue any Linux calls (e.g. prink) if lkl_cpu_get() was not issued + * before. + */ +int lkl_trigger_irq(int irq) +{ + int ret; + + if (!irq || irq > NR_IRQS) + return -EINVAL; + + ret = lkl_cpu_try_run_irq(irq); + if (ret <= 0) + return ret; + + /* + * Since this can be called from Linux context (e.g. lkl_trigger_irq -> + * IRQ -> softirq -> lkl_trigger_irq) make sure we are actually allowed + * to run irqs at this point + */ + if (!irqs_enabled) { + set_irq_pending(irq); + lkl_cpu_put(); + return 0; + } + + run_irq(irq); + + lkl_cpu_put(); + + return 0; +} + +static inline void for_each_bit(unsigned long word, void (*f)(int, int), int j) +{ + int i = 0; + + while (word) { + if (word & 1) + f(i, j); + word >>= 1; + i++; + } +} + +static inline void deliver_irq(int bit, int index) +{ + run_irq(index * IRQ_STATUS_BITS + bit); +} + +static inline void check_irq_status(int i, int unused) +{ + for_each_bit(test_and_clear_irq_status(i), deliver_irq, i); +} + +void run_irqs(void) +{ + for_each_bit(test_and_clear_irq_index_status(), check_irq_status, 0); +} + +int show_interrupts(struct seq_file *p, void *v) +{ + return 0; +} + +int lkl_get_free_irq(const char *user) +{ + int i; + int ret = -EBUSY; + + /* 0 is not a valid IRQ */ + for (i = 1; i < NR_IRQS; i++) { + if (!irqs[i].user) { + irqs[i].user = user; + irq_set_chip_and_handler(i, &dummy_irq_chip, + handle_simple_irq); + ret = i; + break; + } + } + + return ret; +} + +void lkl_put_irq(int i, const char *user) +{ + if (!irqs[i].user || strcmp(irqs[i].user, user) != 0) { + WARN("%s tried to release %s's irq %d", user, irqs[i].user, i); + return; + } + + irqs[i].user = NULL; +} + +unsigned long arch_local_save_flags(void) +{ + return irqs_enabled; +} + +void arch_local_irq_restore(unsigned long flags) +{ + if (flags == ARCH_IRQ_ENABLED && irqs_enabled == ARCH_IRQ_DISABLED && + !in_interrupt()) + run_irqs(); + irqs_enabled = flags; +} + +void init_IRQ(void) +{ + int i; + + for (i = 0; i < NR_IRQS; i++) + irq_set_chip_and_handler(i, &dummy_irq_chip, handle_simple_irq); + + pr_info("lkl: irqs initialized\n"); +} + +void cpu_yield_to_irqs(void) +{ + cpu_relax(); +}