From patchwork Thu Sep 24 07:12:58 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hajime Tazaki X-Patchwork-Id: 1370383 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=2001:8b0:10b:1231::1; helo=merlin.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; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=merlin.20170209 header.b=abtUqnQE; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=p2UFUQc7; dkim-atps=neutral Received: from merlin.infradead.org (merlin.infradead.org [IPv6:2001:8b0:10b:1231::1]) (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 4BxmYl0fxFz9sSC for ; Thu, 24 Sep 2020 17:16:27 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.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=7cHrtNpaFWobObkyrRVJFfvYzBFiTznYFrzZdn0nFGw=; b=abtUqnQEb0hemk7YEFAvTsMqi Kdoyq6cvD/v0SCFn5qMBhlrEGuNu6vyEFYAZQvAygad0CfoNUI0GQwBDLpoCQXCFscyHWf2aQsNmS 6vczzhrQJcBJj67J3m4f+qzb0lgJVDD8il+0EXBwZQNEZt8U8ZS//R8DHzeze2RzZ6y40BqwMNKC1 TfifARsYrZnO4UsHTSu3i85SLb0CyA0ViceGPjeH9HVAaT8OAt91549hExzYNRiDW5yR47G1YMSq8 PDRskckLqXRzq4WcjY9Kz/reC9hG0UlyCK9BEunjASrLtvPKa28L0xQ1AuQyCW+qd6nm3IXM5J5e3 rrFN2Gizw==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kLLUN-0003ro-LG; Thu, 24 Sep 2020 07:16:23 +0000 Received: from mail-pg1-x52e.google.com ([2607:f8b0:4864:20::52e]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kLLUH-0003p2-Vx for linux-um@lists.infradead.org; Thu, 24 Sep 2020 07:16:22 +0000 Received: by mail-pg1-x52e.google.com with SMTP id y14so1331511pgf.12 for ; Thu, 24 Sep 2020 00:16:17 -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=f8X530ewYdklTpRShu04QgcFqIloEAYvsR2T5wI/7yQ=; b=p2UFUQc7xZvO0JVqEnhRfp+Mn7w7n+RjKhglUpdVcTm36RmXPrIGfX5o+h36chiVW0 5HbnnlQ8n3n31U2wzMQWnD9mZq0Mzhv/57KNbMVnyPmBfjAcrkeVRUSfxB2joLzOfbd1 4Tg/s+QoD+Q+z/W3ANj0rINyTVlwLYktLPVO85VIk79BuXBBiuxRetaaj7o0GjvhDJq5 ze4Jk4/zdj8ylO8iXPThxvz0k9xh6Agl71fisfklIAOC7PZ7oXFc8bc+SDo09kqEeaKW VkdPs9AyxSGU3YEcK+zuRCvCTxu0VLkE0PI4OYHYtuTQmNzqr0LoIhuLzu7yBeFL6FxV NewA== 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=f8X530ewYdklTpRShu04QgcFqIloEAYvsR2T5wI/7yQ=; b=tDYE1JZ1Ihc03hKcXeK1/3VzwmD1LiEFoOxm2Ns85jyU4ol1DCd1zx5l+7BpgYBJHu JTpXVfBX2Q5I17Vm3TORa0ggkNvVeBGZvpyQvKnxx1Xur1cLMyb7rlFg5i3nApOWDaOu XpIY7XCTUMksCWbT6P5BVI5jGmVAVkVbQ5HN5zoLYFJEdiNu0up0R0fORe0Y0EynMiGn figRI2KNlIpTX8lTeNjWIeZTF3vsez+zM6aHpsFP0NdUEMxiASUIU9Esn+xwHrLO+gH+ iRIywgXksnLNQJHggd6Cn0ZUzQ+u+PhDnZVN7abwH1lbXp1XM8+eidbxpqeUpdD4DvEO IWgw== X-Gm-Message-State: AOAM530X5zIzlJ7EMIRoKWNE6ZX+oU9Q3JaFxPG/ej2bTbsnWk+YDRYU qu7VOSOTi7AacaJnxuw1138= X-Google-Smtp-Source: ABdhPJxfifKa0OZyq8V4uwu3hypYKMx7Q9NBWA07LeiSLhHRAmaFGtH6rPXcFFgUMcW149QsTlSu9w== X-Received: by 2002:aa7:96af:0:b029:142:6a8f:c04b with SMTP id g15-20020aa796af0000b02901426a8fc04bmr3396020pfk.30.1600931775668; Thu, 24 Sep 2020 00:16:15 -0700 (PDT) Received: from earth-mac.local (219x123x138x129.ap219.ftth.ucom.ne.jp. [219.123.138.129]) by smtp.gmail.com with ESMTPSA id x4sm1811050pfm.86.2020.09.24.00.16.14 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 24 Sep 2020 00:16:15 -0700 (PDT) Received: by earth-mac.local (Postfix, from userid 501) id 192FE2037C20F0; Thu, 24 Sep 2020 16:16:05 +0900 (JST) From: Hajime Tazaki To: linux-um@lists.infradead.org, jdike@addtoit.com, richard@nod.at, anton.ivanov@cambridgegreys.com Subject: [RFC v6 18/21] um: host: add utilities functions Date: Thu, 24 Sep 2020 16:12:58 +0900 Message-Id: <598d65c578c9cc5a61509241cf9a63234be1cfeb.1600922529.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-20200924_031618_112257_43D82A44 X-CRM114-Status: GOOD ( 24.32 ) X-Spam-Score: -0.2 (/) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.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:52e listed in] [list.dnswl.org] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [thehajime[at]gmail.com] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record -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: tavi.purdila@gmail.com, linux-kernel-library@freelists.org, linux-arch@vger.kernel.org, Hajime Tazaki , retrage01@gmail.com Sender: "linux-um" Errors-To: linux-um-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Add basic utility functions for getting a string from a kernel error code and a fprintf like function that uses the host print operation. The latter is useful for informing the user about errors that occur in the host library. Signed-off-by: Hajime Tazaki --- tools/um/lib/Build | 4 + tools/um/lib/jmp_buf.c | 14 +++ tools/um/lib/jmp_buf.h | 8 ++ tools/um/lib/utils.c | 210 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 236 insertions(+) create mode 100644 tools/um/lib/Build create mode 100644 tools/um/lib/jmp_buf.c create mode 100644 tools/um/lib/jmp_buf.h create mode 100644 tools/um/lib/utils.c diff --git a/tools/um/lib/Build b/tools/um/lib/Build new file mode 100644 index 000000000000..fc967af4104a --- /dev/null +++ b/tools/um/lib/Build @@ -0,0 +1,4 @@ +include $(objtree)/include/config/auto.conf + +liblinux-$(CONFIG_UMMODE_LIB) += utils.o +liblinux-$(CONFIG_UMMODE_LIB) += jmp_buf.o diff --git a/tools/um/lib/jmp_buf.c b/tools/um/lib/jmp_buf.c new file mode 100644 index 000000000000..f6bdd7e4bd83 --- /dev/null +++ b/tools/um/lib/jmp_buf.c @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include + +void jmp_buf_set(struct lkl_jmp_buf *jmpb, void (*f)(void)) +{ + if (!setjmp(*((jmp_buf *)jmpb->buf))) + f(); +} + +void jmp_buf_longjmp(struct lkl_jmp_buf *jmpb, int val) +{ + longjmp(*((jmp_buf *)jmpb->buf), val); +} diff --git a/tools/um/lib/jmp_buf.h b/tools/um/lib/jmp_buf.h new file mode 100644 index 000000000000..8782cbaaf51f --- /dev/null +++ b/tools/um/lib/jmp_buf.h @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LKL_LIB_JMP_BUF_H +#define _LKL_LIB_JMP_BUF_H + +void jmp_buf_set(struct lkl_jmp_buf *jmpb, void (*f)(void)); +void jmp_buf_longjmp(struct lkl_jmp_buf *jmpb, int val); + +#endif diff --git a/tools/um/lib/utils.c b/tools/um/lib/utils.c new file mode 100644 index 000000000000..4930479a8a35 --- /dev/null +++ b/tools/um/lib/utils.c @@ -0,0 +1,210 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include + +static const char * const lkl_err_strings[] = { + "Success", + "Operation not permitted", + "No such file or directory", + "No such process", + "Interrupted system call", + "I/O error", + "No such device or address", + "Argument list too long", + "Exec format error", + "Bad file number", + "No child processes", + "Try again", + "Out of memory", + "Permission denied", + "Bad address", + "Block device required", + "Device or resource busy", + "File exists", + "Cross-device link", + "No such device", + "Not a directory", + "Is a directory", + "Invalid argument", + "File table overflow", + "Too many open files", + "Not a typewriter", + "Text file busy", + "File too large", + "No space left on device", + "Illegal seek", + "Read-only file system", + "Too many links", + "Broken pipe", + "Math argument out of domain of func", + "Math result not representable", + "Resource deadlock would occur", + "File name too long", + "No record locks available", + "Invalid system call number", + "Directory not empty", + "Too many symbolic links encountered", + "Bad error code", /* EWOULDBLOCK is EAGAIN */ + "No message of desired type", + "Identifier removed", + "Channel number out of range", + "Level 2 not synchronized", + "Level 3 halted", + "Level 3 reset", + "Link number out of range", + "Protocol driver not attached", + "No CSI structure available", + "Level 2 halted", + "Invalid exchange", + "Invalid request descriptor", + "Exchange full", + "No anode", + "Invalid request code", + "Invalid slot", + "Bad error code", /* EDEADLOCK is EDEADLK */ + "Bad font file format", + "Device not a stream", + "No data available", + "Timer expired", + "Out of streams resources", + "Machine is not on the network", + "Package not installed", + "Object is remote", + "Link has been severed", + "Advertise error", + "Srmount error", + "Communication error on send", + "Protocol error", + "Multihop attempted", + "RFS specific error", + "Not a data message", + "Value too large for defined data type", + "Name not unique on network", + "File descriptor in bad state", + "Remote address changed", + "Can not access a needed shared library", + "Accessing a corrupted shared library", + ".lib section in a.out corrupted", + "Attempting to link in too many shared libraries", + "Cannot exec a shared library directly", + "Illegal byte sequence", + "Interrupted system call should be restarted", + "Streams pipe error", + "Too many users", + "Socket operation on non-socket", + "Destination address required", + "Message too long", + "Protocol wrong type for socket", + "Protocol not available", + "Protocol not supported", + "Socket type not supported", + "Operation not supported on transport endpoint", + "Protocol family not supported", + "Address family not supported by protocol", + "Address already in use", + "Cannot assign requested address", + "Network is down", + "Network is unreachable", + "Network dropped connection because of reset", + "Software caused connection abort", + "Connection reset by peer", + "No buffer space available", + "Transport endpoint is already connected", + "Transport endpoint is not connected", + "Cannot send after transport endpoint shutdown", + "Too many references: cannot splice", + "Connection timed out", + "Connection refused", + "Host is down", + "No route to host", + "Operation already in progress", + "Operation now in progress", + "Stale file handle", + "Structure needs cleaning", + "Not a XENIX named type file", + "No XENIX semaphores available", + "Is a named type file", + "Remote I/O error", + "Quota exceeded", + "No medium found", + "Wrong medium type", + "Operation Canceled", + "Required key not available", + "Key has expired", + "Key has been revoked", + "Key was rejected by service", + "Owner died", + "State not recoverable", + "Operation not possible due to RF-kill", + "Memory page has hardware error", +}; + +const char *lkl_strerror(int err) +{ + if (err < 0) + err = -err; + + if ((size_t)err >= sizeof(lkl_err_strings) / sizeof(const char *)) + return "Bad error code"; + + return lkl_err_strings[err]; +} + +void lkl_perror(char *msg, int err) +{ + const char *err_msg = lkl_strerror(err); + /* We need to use 'real' printf because lkl_host_ops.print can + * be turned off when debugging is off. + */ + lkl_printf("%s: %s\n", msg, err_msg); +} + +static int lkl_vprintf(const char *fmt, va_list args) +{ + int n; + char *buffer; + va_list copy; + + if (!lkl_host_ops.print) + return 0; + + va_copy(copy, args); + n = vsnprintf(NULL, 0, fmt, copy); + va_end(copy); + + buffer = lkl_host_ops.mem_alloc(n + 1); + if (!buffer) + return (-1); + + vsnprintf(buffer, n + 1, fmt, args); + + lkl_host_ops.print(buffer, n); + lkl_host_ops.mem_free(buffer); + + return n; +} + +int lkl_printf(const char *fmt, ...) +{ + int n; + va_list args; + + va_start(args, fmt); + n = lkl_vprintf(fmt, args); + va_end(args); + + return n; +} + +void lkl_bug(const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + lkl_vprintf(fmt, args); + va_end(args); + + lkl_host_ops.panic(); +}