git: 649cc3598192 - stable/14 - kboot: Create function for error checking.

From: Warner Losh <imp_at_FreeBSD.org>
Date: Tue, 16 Apr 2024 20:13:31 UTC
The branch stable/14 has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=649cc3598192b6c03a5165010a97a3778dd3f0e1

commit 649cc3598192b6c03a5165010a97a3778dd3f0e1
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2024-03-11 20:15:03 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2024-04-16 19:54:29 +0000

    kboot: Create function for error checking.
    
    Linux has the convention of returning -ERRNO to flag errors from its
    system calls. Sometimes other negative values are returned that are
    success...  However, only values -1 to -4096 (inclusive) are really
    errors. The rest are either truncated values that only look negative (so
    use long instead of int), or are things like addresses or legal unsigned
    file offsets or similar that are successful returns. Filter out the
    latter.
    
    Sponsored by:           Netflix
    
    (cherry picked from commit 3ae18fdfbcaad827defdc217386e73c993beeba0)
---
 stand/kboot/include/host_syscall.h | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/stand/kboot/include/host_syscall.h b/stand/kboot/include/host_syscall.h
index fd3b7a0f362b..68106093ce1f 100644
--- a/stand/kboot/include/host_syscall.h
+++ b/stand/kboot/include/host_syscall.h
@@ -190,20 +190,36 @@ ssize_t host_write(int fd, const void *buf, size_t nbyte);
 	host_mmap(0, size, HOST_PROT_READ | HOST_PROT_WRITE, \
 	    HOST_MAP_PRIVATE | HOST_MAP_ANONYMOUS, -1, 0);
 
+/*
+ * Since we have to interface with the 'raw' system call, we have to cope with
+ * Linux's conventions. To run on the most architectures possible, they don't
+ * return errors through some CPU flag, but instead, return a negative value for
+ * an error, and a positive one for success. However, there's some issues since
+ * addresses have to be returned, some of which are also negative, so Linus
+ * declared that no successful result could be -4096 to 0. This implements
+ * that quirk so we can check return values easily.
+ */
+static __inline bool
+is_linux_error(long e)
+{
+	return (e < 0 && e >= -4096);
+}
+
 /*
  * Translate Linux errno to FreeBSD errno. The two system have idenitcal errors
  * for 1-34. After that, they differ. Linux also has errno that don't map
  * exactly to FreeBSD's errno, plus the Linux errno are arch dependent >
  * 34. Since we just need to do this for simple cases, use the simple mapping
  * function where -1 to -34 are translated to 1 to 34 and all others are EINVAL.
- * Pass the linux return value, which will be the -errno.
+ * Pass the linux return value, which will be the -errno. Linux returns these
+ * values as a 'long' which has to align to CPU register size, so accept that
+ * size as the error so the assert can catch more values.
  */
 static __inline int
-host_to_stand_errno(int e)
+host_to_stand_errno(long e)
 {
-	assert(e < 0);
+	assert(is_linux_error(e));
 
 	return((-e) > 34 ? EINVAL : (-e));
 }
-
 #endif