diff mbox series

[v2,68/74] hw/rx: Handle a kernel file that is ELF

Message ID 20220503194843.1379101-69-richard.henderson@linaro.org
State New
Headers show
Series semihosting cleanup | expand

Commit Message

Richard Henderson May 3, 2022, 7:48 p.m. UTC
Attempt to load the kernel with load_elf.  If this fails with
ELF_LOAD_NOT_ELF, continue to treat the kernel as a raw image.

This will be handy for running semihosting programs.

Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
 hw/rx/rx-gdbsim.c | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)

Comments

Peter Maydell May 13, 2022, 3:44 p.m. UTC | #1
On Tue, 3 May 2022 at 21:52, Richard Henderson
<richard.henderson@linaro.org> wrote:
>
> Attempt to load the kernel with load_elf.  If this fails with
> ELF_LOAD_NOT_ELF, continue to treat the kernel as a raw image.
>
> This will be handy for running semihosting programs.
>
> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>

I know nothing of the specifics of the rx target, but I'm
always a bit dubious about adding more behaviour to the
-kernel option, which is (a) already a morass of undocumented
target specific behaviours (b) nominally supposed to be
"load a Linux kernel", not "load any random thing".
Can you do what you need with the generic-loader device instead?
That has the benefit of both being documented and also doing
the same thing on all target architectures.


thanks
-- PMM
Richard Henderson May 13, 2022, 3:48 p.m. UTC | #2
On 5/13/22 08:44, Peter Maydell wrote:
> On Tue, 3 May 2022 at 21:52, Richard Henderson
> <richard.henderson@linaro.org> wrote:
>>
>> Attempt to load the kernel with load_elf.  If this fails with
>> ELF_LOAD_NOT_ELF, continue to treat the kernel as a raw image.
>>
>> This will be handy for running semihosting programs.
>>
>> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
> 
> I know nothing of the specifics of the rx target, but I'm
> always a bit dubious about adding more behaviour to the
> -kernel option, which is (a) already a morass of undocumented
> target specific behaviours (b) nominally supposed to be
> "load a Linux kernel", not "load any random thing".
> Can you do what you need with the generic-loader device instead?
> That has the benefit of both being documented and also doing
> the same thing on all target architectures.

I'll give that a try.


r~
Richard Henderson May 13, 2022, 4:40 p.m. UTC | #3
On 5/13/22 08:48, Richard Henderson wrote:
> On 5/13/22 08:44, Peter Maydell wrote:
>> On Tue, 3 May 2022 at 21:52, Richard Henderson
>> <richard.henderson@linaro.org> wrote:
>>>
>>> Attempt to load the kernel with load_elf.  If this fails with
>>> ELF_LOAD_NOT_ELF, continue to treat the kernel as a raw image.
>>>
>>> This will be handy for running semihosting programs.
>>>
>>> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
>>
>> I know nothing of the specifics of the rx target, but I'm
>> always a bit dubious about adding more behaviour to the
>> -kernel option, which is (a) already a morass of undocumented
>> target specific behaviours (b) nominally supposed to be
>> "load a Linux kernel", not "load any random thing".
>> Can you do what you need with the generic-loader device instead?
>> That has the benefit of both being documented and also doing
>> the same thing on all target architectures.
> 
> I'll give that a try.

It works, editing the board not to reject lack of -bios and lack of -kernel.  And running 
by hand because the syntax of the test harness does not allow the test file to be joined 
with $(QEMU_OPTS), as in

   -device loader,cpu-num=0,file=testcase

I'll work with Alex to figure out how best to restructure the test harness.

In the meantime I certainly don't mind putting the rx part on the back burner if it'll 
help get the previous 67 patches reviewed...


r~
diff mbox series

Patch

diff --git a/hw/rx/rx-gdbsim.c b/hw/rx/rx-gdbsim.c
index be147b4bd9..64b533181d 100644
--- a/hw/rx/rx-gdbsim.c
+++ b/hw/rx/rx-gdbsim.c
@@ -26,6 +26,7 @@ 
 #include "sysemu/device_tree.h"
 #include "hw/boards.h"
 #include "qom/object.h"
+#include "elf.h"
 
 /* Same address of GDB integrated simulator */
 #define SDRAM_BASE  EXT_CS_BASE
@@ -57,15 +58,32 @@  static void rx_load_image(RXCPU *cpu, const char *filename,
                           uint32_t start, uint32_t size)
 {
     static uint32_t extable[32];
-    long kernel_size;
+    ssize_t kernel_size;
+    uint64_t kernel_entry;
     int i;
 
+    /* Try an ELF image first. */
+
+    kernel_size = load_elf(filename, NULL, NULL, NULL, &kernel_entry,
+                           NULL, NULL, NULL, false, EM_RX, false, false);
+    if (kernel_size >= 0) {
+        cpu_set_pc(CPU(cpu), kernel_entry);
+        return;
+    }
+    if (kernel_size != ELF_LOAD_NOT_ELF) {
+        error_report("could not load kernel '%s': %s",
+                     filename, load_elf_strerror(kernel_size));
+        exit(1);
+    }
+
+    /* Not ELF: load a raw image, e.g. zImage. */
+
     kernel_size = load_image_targphys(filename, start, size);
     if (kernel_size < 0) {
-        fprintf(stderr, "qemu: could not load kernel '%s'\n", filename);
+        error_report("could not load kernel '%s'", filename);
         exit(1);
     }
-    cpu->env.pc = start;
+    cpu_set_pc(CPU(cpu), start);
 
     /* setup exception trap trampoline */
     /* linux kernel only works little-endian mode */