[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <aae854b2de881f9bc1c82ab9dda53b51d7330c9a.1687187451.git.falcon@tinylab.org>
Date: Mon, 19 Jun 2023 23:48:21 +0800
From: Zhangjin Wu <falcon@...ylab.org>
To: w@....eu
Cc: david.laight@...lab.com, arnd@...db.de, falcon@...ylab.org,
linux-kernel@...r.kernel.org, linux-kselftest@...r.kernel.org,
linux-riscv@...ts.infradead.org, thomas@...ch.de,
David Laight <David.Laight@...LAB.COM>
Subject: [PATCH v4 06/10] tools/nolibc: __sysret: support syscalls who return a pointer
To support syscalls (e.g. mmap()) who return a pointer and to allow the
pointer as big as possible, we should convert the negated errno value to
unsigned long (uintptr_t), otherwise, in signed long, a potential big
pointer (whose highest bit is 1) will be treated as a failure.
tools/include/nolibc/errno.h defines the MAX_ERRNO, let's use it
directly. after converting to unsigned long, the negative errno value
from -1 to -MAX_ERRNO becomes something like '~1 + 1' (every bit is 1)
to '~MAX_ERRNO + 1', '~1 + 1' is the biggest, '~MAX_ERRNO + 1' is the
smallest, so, the check becomes:
if (ret <= (unsigned long)-1 && ret >= (unsigned long)-MAX_ERRNO) {
...
}
Since (unsigned long)-1 is the biggest unsigned long value, it is always
true if bigger than (unsigned long)-MAX_ERRNO, so, just reserve the
following check is enough:
if (ret >= (unsigned long)-MAX_ERRNO) {
...
}
Suggested-by: David Laight <David.Laight@...LAB.COM>
Link: https://lore.kernel.org/linux-riscv/94dd5170929f454fbc0a10a2eb3b108d@AcuMS.aculab.com/
Signed-off-by: Zhangjin Wu <falcon@...ylab.org>
---
tools/include/nolibc/sys.h | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h
index 4fbefe5adf93..8a6e16472d54 100644
--- a/tools/include/nolibc/sys.h
+++ b/tools/include/nolibc/sys.h
@@ -28,13 +28,16 @@
#include "errno.h"
#include "types.h"
-/* Syscall return helper, set errno as -ret when ret < 0 */
+
+/* Syscall return helper for library routines
+ * set errno as -ret when ret in [-MAX_ERRNO, -1]
+ */
static __inline__ __attribute__((unused, always_inline))
-long __sysret(long ret)
+long __sysret(unsigned long ret)
{
- if (ret < 0) {
- SET_ERRNO(-ret);
- ret = -1;
+ if (ret >= (unsigned long)-MAX_ERRNO) {
+ SET_ERRNO(-(long)ret);
+ return -1;
}
return ret;
}
--
2.25.1
Powered by blists - more mailing lists