Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp9247507imu; Sat, 29 Dec 2018 14:08:59 -0800 (PST) X-Google-Smtp-Source: ALg8bN4MoLOjl5fKXp3PyOXaOTVUiZau/lhj0UdlPCOBCm7UoKXy5GJYG5RDI9j+EGhs5yBkuwkV X-Received: by 2002:a63:1412:: with SMTP id u18mr2810195pgl.247.1546121339122; Sat, 29 Dec 2018 14:08:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546121339; cv=none; d=google.com; s=arc-20160816; b=OKMJ/yk4x7w7SD4tuPErJyfpRfXUs5I13pJIxpwXGozdsxb8q61yi2H0npQOqASFlZ cehdAxqHVox8GY86gbPJU+akpiGPiCJD30gTutDLSMS/QQc7pbwlsEHurvlte9noHknl iQUTxCcKfDJXKBBtniDirT2GR47VNxjRHIWQ7EZacmuJjwKu3IZpMjCDG4mMIdXTdKng ymg6Boh/AmU66XwgMB8JodDVhTp2W8IhNnp8Tuq6ogNMjDsxxmL7Z5Tb1SW0ijdwN4DM QhyVx6YbJreHrfkRWlKXJKVpWgrYG7ZAa48aH5viDI6PMzwzkieL6yzNPXAAFJF1/qqu Ugcg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=o+a++APjmK0BeTsYMVnRciy20mU5G5R3rk1Lem+TJ58=; b=k3Qd71FLQyAowLSn3ni4Zq7uOh2h3dlgST1V37wZJHnYqkii6sPMnjyY+9HqJJg8/8 8K6kjs1ptMKIaH9yWFYz0KN7B+7aAoQL5ouB4R4JPqfJcCvY5xpqeNWzlq8FTL8WyrBP 8B9gw+Fo3VPGqyn0iOeNk8XrMnb2PLGvwVrx5kn0WamqrX1x5+mpsNA1bFZe0GSjGNF+ mR0fFsOL9yYaZ1tm/tGbppE5nGBU6JXAJ+zFdJIE4V1PrG03YzoPzAeU5aJgqD6/9ywn s7l5ge1lnZODgrp8Ndmvf/nPYY4ypojZInu1WBmwa/4QeYvYpjeYcXyfKuDFN0+CNJyq CuqA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m3si39982361plt.394.2018.12.29.14.08.44; Sat, 29 Dec 2018 14:08:59 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727879AbeL2SDA (ORCPT + 99 others); Sat, 29 Dec 2018 13:03:00 -0500 Received: from wtarreau.pck.nerim.net ([62.212.114.60]:33032 "EHLO 1wt.eu" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727794AbeL2SC7 (ORCPT ); Sat, 29 Dec 2018 13:02:59 -0500 Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id wBTI2OmS015844; Sat, 29 Dec 2018 19:02:24 +0100 From: Willy Tarreau To: mingo@kernel.org, paulmck@linux.vnet.ibm.com Cc: rdunlap@infradead.org, linux-kernel@vger.kernel.org, Willy Tarreau Subject: [PATCH 3/4] rcutorture/nolibc: add a bit of documentation to explain how to use nolibc Date: Sat, 29 Dec 2018 19:02:18 +0100 Message-Id: <20181229180218.15798-4-w@1wt.eu> X-Mailer: git-send-email 2.9.0 In-Reply-To: <20181229180218.15798-1-w@1wt.eu> References: <20181229180218.15798-1-w@1wt.eu> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Ingo rightfully asked for a bit more documentation in the nolibc header, so this patch adds some explanation about its purpose, how it's made, and how to use it. Cc: Ingo Molnar Cc: Paul E. McKenney Cc: Randy Dunlap Signed-off-by: Willy Tarreau --- tools/testing/selftests/rcutorture/bin/nolibc.h | 90 +++++++++++++++++++++---- 1 file changed, 78 insertions(+), 12 deletions(-) diff --git a/tools/testing/selftests/rcutorture/bin/nolibc.h b/tools/testing/selftests/rcutorture/bin/nolibc.h index 985364c..6643ba9 100644 --- a/tools/testing/selftests/rcutorture/bin/nolibc.h +++ b/tools/testing/selftests/rcutorture/bin/nolibc.h @@ -3,6 +3,84 @@ * Copyright (C) 2017-2018 Willy Tarreau */ +/* + * This file is designed to be used as a libc alternative for minimal programs + * with very limited requirements. It consists of a small number of syscall and + * type definitions, and the minimal startup code needed to call main(). + * All syscalls are declared as static functions so that they can be optimized + * away by the compiler when not used. + * + * Syscalls are split into 3 levels: + * - the lower level is the arch-specific syscall() definition, consisting in + * assembly code in compound expressions. These are called my_syscall0() to + * my_syscall6() depending on the number of arguments. The MIPS + * implementation is limited to 5 arguments. All input arguments are cast + * to a long stored in a register. These expressions always return the + * syscall's return value as a signed long value which is often either a + * pointer or the negated errno value. + * + * - the second level is mostly architecture-independent. It is made of + * static functions called sys_() which rely on my_syscallN() + * depending on the syscall definition. These functions are responsible + * for exposing the appropriate types for the syscall arguments (int, + * pointers, etc) and for setting the appropriate return type (often int). + * A few of them are architecture-specific because the syscalls are not all + * mapped exactly the same among architectures. For example, some archs do + * not implement select() and need pselect6() instead, so the sys_select() + * function will have to abstract this. + * + * - the third level is the libc call definition. It exposes the lower raw + * sys_() calls in a way that looks like what a libc usually does, + * takes care of specific input values, and of setting errno upon error. + * There can be minor variations compared to standard libc calls. For + * example the open() call always takes 3 args here. + * + * The errno variable is declared static and unused. This way it can be + * optimized away if not used. However this means that a program made of + * multiple C files may observe different errno values (one per C file). For + * the type of programs this project targets it usually is not a problem. The + * resulting program may even be reduced by defining the NOLIBC_IGNORE_ERRNO + * macro, in which case the errno value will never be assigned. + * + * Some stdint-like integer types are defined. These are valid on all currently + * supported architectures, because signs are enforced, ints are assumed to be + * 32 bits, longs the size of a pointer and long long 64 bits. If more + * architectures have to be supported, this may need to be adapted. + * + * Some macro definitions like the O_* values passed to open(), and some + * structures like the sys_stat struct depend on the architecture. + * + * The definitions start with the architecture-specific parts, which are picked + * based on what the compiler knows about the target architecture, and are + * completed with the generic code. Since it is the compiler which sets the + * target architecture, cross-compiling normally works out of the box without + * having to specify anything. + * + * Finally some very common libc-level functions are provided. It is the case + * for a few functions usually found in string.h, ctype.h, or stdlib.h. Nothing + * is currently provided regarding stdio emulation. + * + * The macro NOLIBC is always defined, so that it is possible for a program to + * check this macro to know if it is being built against and decide to disable + * some features or simply not to include some standard libc files. + * + * Ideally this file should be split in multiple files for easier long term + * maintenance, but provided as a single file as it is now, it's quite + * convenient to use. Maybe some variations involving a set of includes at the + * top could work. + * + * A simple static executable may be built this way : + * $ gcc -fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib \ + * -static -include nolibc.h -lgcc -o hello hello.c + * + * A very useful calling convention table may be found here : + * http://man7.org/linux/man-pages/man2/syscall.2.html + * + * This doc is quite convenient though not necessarily up to date : + * https://w3challs.com/syscalls/ + * + */ + /* some archs (at least aarch64) don't expose the regular syscalls anymore by * default, either because they have an "_at" replacement, or because there are * more modern alternatives. For now we'd rather still use them. @@ -19,18 +97,6 @@ #define NOLIBC -/* Build a static executable this way : - * $ gcc -fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib \ - * -static -include nolibc.h -lgcc -o hello hello.c - * - * Useful calling convention table found here : - * http://man7.org/linux/man-pages/man2/syscall.2.html - * - * This doc is even better : - * https://w3challs.com/syscalls/ - */ - - /* this way it will be removed if unused */ static int errno; -- 2.9.0