From bb4c13143c69c46b1f255437d0338cdf98a3afb8 Mon Sep 17 00:00:00 2001 From: Alex Richardson Date: Tue, 30 Apr 2024 16:00:03 -0700 Subject: [PATCH 1/2] arch-generic: Fix reading from special :semihosting-features file The implementation of SYS_FLEN was missing, which caused picolibc to treat this file as not implemented. Additionally, there was a bug in the SYS_READ call that was comparing the wrong variable against the passed buffer length. It was comparing the current file position against the buffer length instead of the number of written bytes. Finally, pos was unititialized which could result in spurious errors. Change-Id: I8b487a79df5970a5001d3fef08d5579bb4aa0dd0 --- src/arch/generic/semihosting.cc | 14 +++++++++++--- src/arch/generic/semihosting.hh | 5 +++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/arch/generic/semihosting.cc b/src/arch/generic/semihosting.cc index 4da54c4b4b..d8eabedbae 100644 --- a/src/arch/generic/semihosting.cc +++ b/src/arch/generic/semihosting.cc @@ -94,7 +94,7 @@ const std::map BaseSemihosting::exitCodes{ {0x20029, "semi:ADP_Stopped_DivisionByZero"}, }; -const std::vector BaseSemihosting::features{ +const std::array BaseSemihosting::features{ 0x53, 0x48, 0x46, 0x42, // Magic 0x3, // EXT_EXIT_EXTENDED, EXT_STDOUT_STDERR }; @@ -649,18 +649,26 @@ BaseSemihosting::FileBase::flen() return -EINVAL; } +static const std::vector features; + BaseSemihosting::FileFeatures:: FileFeatures(BaseSemihosting &_parent, const char *_name, const char *_mode) : FileBase(_parent, _name, _mode) {} +int64_t +BaseSemihosting::FileFeatures::flen() +{ + return features.size(); +} + int64_t BaseSemihosting::FileFeatures::read(uint8_t *buffer, uint64_t size) { int64_t len = 0; - for (; pos < size && pos < BaseSemihosting::features.size(); pos++) - buffer[len++] = BaseSemihosting::features[pos]; + for (; len < size && pos < features.size(); pos++) + buffer[len++] = features[pos]; return len; } diff --git a/src/arch/generic/semihosting.hh b/src/arch/generic/semihosting.hh index 975cac8b78..8725e25389 100644 --- a/src/arch/generic/semihosting.hh +++ b/src/arch/generic/semihosting.hh @@ -323,9 +323,10 @@ class BaseSemihosting : public SimObject int64_t read(uint8_t *buffer, uint64_t size) override; int64_t seek(uint64_t pos) override; + int64_t flen() override; protected: - size_t pos; + size_t pos = 0; }; class File : public FileBase @@ -540,7 +541,7 @@ class BaseSemihosting : public SimObject static const std::vector fmodes; static const std::map exitCodes; - static const std::vector features; + static const std::array features; static const std::map stdioMap; // used in callTmpNam() to deterministically generate a temp filename From aa2fade12ed55b413f23fd61dc9820a25588a09b Mon Sep 17 00:00:00 2001 From: Alexander Richardson Date: Wed, 1 May 2024 18:00:09 +0100 Subject: [PATCH 2/2] Drop unrelated change --- src/arch/generic/semihosting.cc | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/arch/generic/semihosting.cc b/src/arch/generic/semihosting.cc index d8eabedbae..7366c831cd 100644 --- a/src/arch/generic/semihosting.cc +++ b/src/arch/generic/semihosting.cc @@ -649,8 +649,6 @@ BaseSemihosting::FileBase::flen() return -EINVAL; } -static const std::vector features; - BaseSemihosting::FileFeatures:: FileFeatures(BaseSemihosting &_parent, const char *_name, const char *_mode) : FileBase(_parent, _name, _mode)