diff --git a/share/man/man9/Makefile b/share/man/man9/Makefile --- a/share/man/man9/Makefile +++ b/share/man/man9/Makefile @@ -150,6 +150,7 @@ EVENTHANDLER.9 \ eventtimers.9 \ extattr.9 \ + exterror.9 \ fail.9 \ fdt_pinctrl.9 \ fetch.9 \ diff --git a/share/man/man9/exterror.9 b/share/man/man9/exterror.9 new file mode 100644 --- /dev/null +++ b/share/man/man9/exterror.9 @@ -0,0 +1,151 @@ +.\" SPDX-License-Identifier: BSD-2-Clause +.\" +.\" Copyright (c) 2025 The FreeBSD Foundation +.\" +.\" This documentation was written by +.\" Mark Johnston under sponsorship +.\" from the FreeBSD Foundation. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.Dd November 5, 2025 +.Dt EXTERROR 9 +.Os +.Sh NAME +.Nm exterror +.Nd provide extended error information to userspace +.Sh SYNOPSIS +.Bd -literal -offset left -compact +#define EXTERR_CATEGORY EXTERR_CAT_MYCATEGORY +.Ed +.In sys/exterrvar.h +.Ft int +.Fn EXTERROR "int error" "const char *msg" ... +.Sh DESCRIPTION +The +.Nm +framework allows kernel to pass additional information on an error +to userspace, besides the standard +.Xr errno 3 +error code, which is too terse and lacking the context. +.Pp +It is especially visible with the commonly overloaded error codes like +.Er EINVAL +or +.Er EIO , +which might occur at many places for the given syscall, or even +outside the context of the current kernel call. +Identifying the specific cause for the returned error only by +.Va errno +value practically requires guessing it. +.Nm +attaches additional data to the error itself, and records meta-data about +the error place in the kernel source code. +Intent is to make it easier for human user to identify the cause of error, +not to augment the existing +.Va errno +mechanism for the programmatical consumption. +.Sh USAGE +Before +.Nm +can be used in the given source .c file, the category of extended errors +should be allocated in the +.In sys/exterr_cat.h +file. +The category is the unique integer, that would, together with the source +line number, uniquely identify the extended error occurence. +Then, the +.Va EXTERR_CATEGORY +symbol should be defined as an alias for the allocated category, as +shown in the summary. +.Pp +Instead of typical code fragment to report an error +.D1 return (EINVAL); +code now can do +.D1 return (EXTERROR(EINVAL, \[dq]Invalid length\[dq])); +The error data and metadata is saved in the current thread storage. +The metadata includes the category and source line number. +.Pp +Arguments to the +.Fn EXTERROR +macro: +.Bl -dash +.It +First argument to +.Fn EXTERROR +is the errno error code. +.It +Second argument is the constant string with the unbound lifetime, +which should tersely provide enough human-readable details about +the error. +.It +The +.Fn EXTERROR +macro might take two optional 64bit integer arguments, +which meaning is transparent for the subsystem. +.El +.Pp +The strings passed as the second argument are only retained +in the kernel text if the +.Cd option EXTERR_STRINGS +was enabled in the kernel config. +.Pp +The +.Fn EXTERROR +macro can be used in any context where current thread is defined. +In other words, only interrupt contexts and context switch code +formally cannot use EXTERROR. For kernel threads, use of +.Fn EXTERROR +does not make sense because there is no userspace to retrive +the extended error data. +.Sh USERSPACE ACCESS TO EXTENDED ERROR DATA +There is no syscall speed overhead for using +.Nm +for non-error case. +In case of error that supplied the extended information, +kernel copies out that information into the userspace +per-thread area that was registered with it, typically on +image activation, or later at the thread startup. +The area is controlled by the +.Xr exterrctl 2 +internal syscall, normally done by the userspace C runtime. +.Pp +Userspace programs do not need to access the extended information +area directly. +There is no field that is stable for the specific error condition. +Instead, the base +.Lb c +functions +.Xr err 3 +and +.Xr warn 3 +were modified to print the extended information alongside with the +.Va errno +decoding, if available. +.Sh SEE ALSO +.Xr errno 3 , +.Xr err 3 +.Sh HISTORY +The +.Nm +facility was introduced in +.Fx 15.0 .