From: "Alex Bennée" <alex.bennee@linaro.org>
To: qemu-devel@nongnu.org, peter.maydell@linaro.org
Cc: qemu-arm@nongnu.org, "Alex Bennée" <alex.bennee@linaro.org>
Subject: [PATCH v5 2/2] tests/tcg: port SYS_HEAPINFO to a system test
Date: Thu, 10 Feb 2022 11:30:21 +0000 [thread overview]
Message-ID: <20220210113021.3799514-3-alex.bennee@linaro.org> (raw)
In-Reply-To: <20220210113021.3799514-1-alex.bennee@linaro.org>
This allows us to check our new SYS_HEAPINFO implementation generates
sane values.
Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
---
v5
- static init of heapinfo structure
- clean-up comment on why we can test stack position
- add memory clobber for semicall
- test we can read/write to a portion of the heap
- fix MAINTAINERS
---
tests/tcg/aarch64/system/semiheap.c | 93 +++++++++++++++++++++++++++++
MAINTAINERS | 1 +
2 files changed, 94 insertions(+)
create mode 100644 tests/tcg/aarch64/system/semiheap.c
diff --git a/tests/tcg/aarch64/system/semiheap.c b/tests/tcg/aarch64/system/semiheap.c
new file mode 100644
index 0000000000..4ed258476d
--- /dev/null
+++ b/tests/tcg/aarch64/system/semiheap.c
@@ -0,0 +1,93 @@
+/*
+ * Semihosting System HEAPINFO Test
+ *
+ * Copyright (c) 2021 Linaro Ltd
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include <inttypes.h>
+#include <stddef.h>
+#include <minilib.h>
+
+#define SYS_HEAPINFO 0x16
+
+uintptr_t __semi_call(uintptr_t type, uintptr_t arg0)
+{
+ register uintptr_t t asm("x0") = type;
+ register uintptr_t a0 asm("x1") = arg0;
+ asm("hlt 0xf000"
+ : "=r" (t)
+ : "r" (t), "r" (a0)
+ : "memory" );
+
+ return t;
+}
+
+int main(int argc, char *argv[argc])
+{
+ struct {
+ void *heap_base;
+ void *heap_limit;
+ void *stack_base;
+ void *stack_limit;
+ } info = { };
+ void *ptr_to_info = (void *) &info;
+ uint32_t *ptr_to_heap;
+ int i;
+
+ ml_printf("Semihosting Heap Info Test\n");
+
+ __semi_call(SYS_HEAPINFO, (uintptr_t) &ptr_to_info);
+
+ if (info.heap_base == NULL || info.heap_limit == NULL) {
+ ml_printf("null heap: %p -> %p\n", info.heap_base, info.heap_limit);
+ return -1;
+ }
+
+ /* Error if heap base is above limit */
+ if ((uintptr_t) info.heap_base >= (uintptr_t) info.heap_limit) {
+ ml_printf("heap base %p >= heap_limit %p\n",
+ info.heap_base, info.heap_limit);
+ return -2;
+ }
+
+ if (info.stack_base == NULL) {
+ ml_printf("null stack: %p -> %p\n", info.stack_base, info.stack_limit);
+ return -3;
+ }
+
+ /*
+ * boot.S put our stack somewhere inside the data segment of the
+ * ELF file, and we know that SYS_HEAPINFO won't pick a range
+ * that overlaps with part of a loaded ELF file. So the info
+ * struct (on the stack) should not be inside the reported heap.
+ */
+ if (ptr_to_info > info.heap_base && ptr_to_info < info.heap_limit) {
+ ml_printf("info appears to be inside the heap: %p in %p:%p\n",
+ ptr_to_info, info.heap_base, info.heap_limit);
+ return -4;
+ }
+
+ ml_printf("heap: %p -> %p\n", info.heap_base, info.heap_limit);
+ ml_printf("stack: %p <- %p\n", info.stack_limit, info.stack_base);
+
+ /* finally can we read/write the heap */
+ ptr_to_heap = (uint32_t *) info.heap_base;
+ for (i = 0; i < 512; i++) {
+ *ptr_to_heap++ = i;
+ }
+ ptr_to_heap = (uint32_t *) info.heap_base;
+ for (i = 0; i < 512; i++) {
+ uint32_t tmp = *ptr_to_heap;
+ if (tmp != i) {
+ ml_printf("unexpected value in heap: %d @ %p", tmp, ptr_to_heap);
+ return -5;
+ }
+ ptr_to_heap++;
+ }
+ ml_printf("r/w to heap upto %p\n", ptr_to_heap);
+
+ ml_printf("Passed HeapInfo checks\n");
+ return 0;
+}
diff --git a/MAINTAINERS b/MAINTAINERS
index b0b845f445..251f96af9e 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -3536,6 +3536,7 @@ S: Maintained
F: semihosting/
F: include/semihosting/
F: tests/tcg/multiarch/arm-compat-semi/
+F: tests/tcg/aarch64/system/semiheap.c
Multi-process QEMU
M: Elena Ufimtseva <elena.ufimtseva@oracle.com>
--
2.30.2
next prev parent reply other threads:[~2022-02-10 11:41 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-10 11:30 [PATCH v5 0/2] semihosting/next (SYS_HEAPINFO) Alex Bennée
2022-02-10 11:30 ` [PATCH v5 1/2] semihosting/arm-compat: replace heuristic for softmmu SYS_HEAPINFO Alex Bennée
2022-02-10 11:48 ` Philippe Mathieu-Daudé via
2022-02-11 11:52 ` Peter Maydell
2022-02-11 13:22 ` Alex Bennée
2022-02-11 16:18 ` Philippe Mathieu-Daudé via
2022-02-12 15:57 ` Peter Maydell
2022-02-15 21:27 ` Peter Maydell
2022-02-21 17:03 ` Alex Bennée
2022-02-21 17:18 ` Peter Maydell
2022-02-21 22:45 ` Alex Bennée
2022-02-10 11:30 ` Alex Bennée [this message]
2022-02-15 21:29 ` [PATCH v5 2/2] tests/tcg: port SYS_HEAPINFO to a system test Peter Maydell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220210113021.3799514-3-alex.bennee@linaro.org \
--to=alex.bennee@linaro.org \
--cc=peter.maydell@linaro.org \
--cc=qemu-arm@nongnu.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).