Skip to contentSkip to conversation

Nios II generic board config and semihosting

PATCH In review Sandra Loosemore 2 participants 11 messages Feb 13, 2019 – Mar 9, 2019

lore
Share & tools
lore
#5 lore raw

PATCH v5 2/2 Add Nios II semihosting support.

In reply to Sandra Loosemore

This patch adds support for libgloss semihosting to Nios II bare-metal emulation.

---
 qemu-options.hx            |   8 +-
 target/nios2/Makefile.objs |   2 +-
 target/nios2/cpu.h         |   4 +-
 target/nios2/helper.c      |  11 ++
 target/nios2/nios2-semi.c  | 446 +++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 465 insertions(+), 6 deletions(-)
 create mode 100644 target/nios2/nios2-semi.c
qemu-options.hx +4 -4
index 06ef1a7..5019ede 100644
@@ -3712,21 +3712,21 @@ ETEXI
 DEF("semihosting", 0, QEMU_OPTION_semihosting,
     "-semihosting    semihosting mode\n",
     QEMU_ARCH_ARM | QEMU_ARCH_M68K | QEMU_ARCH_XTENSA | QEMU_ARCH_LM32 |
-    QEMU_ARCH_MIPS)
+    QEMU_ARCH_MIPS | QEMU_ARCH_NIOS2)
 STEXI
 @item -semihosting
 @findex -semihosting
-Enable semihosting mode (ARM, M68K, Xtensa, MIPS only).
+Enable semihosting mode (ARM, M68K, Xtensa, MIPS, Nios II only).
 ETEXI
 DEF("semihosting-config", HAS_ARG, QEMU_OPTION_semihosting_config,
     "-semihosting-config [enable=on|off][,target=native|gdb|auto][,arg=str[,...]]\n" \
     "                semihosting configuration\n",
 QEMU_ARCH_ARM | QEMU_ARCH_M68K | QEMU_ARCH_XTENSA | QEMU_ARCH_LM32 |
-QEMU_ARCH_MIPS)
+QEMU_ARCH_MIPS | QEMU_ARCH_NIOS2)
 STEXI
 @item -semihosting-config [enable=on|off][,target=native|gdb|auto][,arg=str[,...]]
 @findex -semihosting-config
-Enable and configure semihosting (ARM, M68K, Xtensa, MIPS only).
+Enable and configure semihosting (ARM, M68K, Xtensa, MIPS, Nios II only).
 @table @option
 @item target=@code{native|gdb|auto}
 Defines where the semihosting calls will be addressed, to QEMU (@code{native})
target/nios2/Makefile.objs +1 -1
index 2a11c5c..010de0e 100644
@@ -1,4 +1,4 @@
-obj-y += translate.o op_helper.o helper.o cpu.o mmu.o
+obj-y += translate.o op_helper.o helper.o cpu.o mmu.o nios2-semi.o
 obj-$(CONFIG_SOFTMMU) += monitor.o
 
 $(obj)/op_helper.o: QEMU_CFLAGS += $(HELPER_CFLAGS)
target/nios2/cpu.h +3 -1
index 047f376..afd30d5 100644
@@ -141,7 +141,7 @@ typedef struct Nios2CPUClass {
 #define R_PC         64
 
 /* Exceptions */
-#define EXCP_BREAK    -1
+#define EXCP_BREAK    0x1000
 #define EXCP_RESET    0
 #define EXCP_PRESET   1
 #define EXCP_IRQ      2
@@ -223,6 +223,8 @@ void nios2_cpu_do_unaligned_access(CPUState *cpu, vaddr addr,
 qemu_irq *nios2_cpu_pic_init(Nios2CPU *cpu);
 void nios2_check_interrupts(CPUNios2State *env);
 
+void do_nios2_semihosting(CPUNios2State *env);
+
 #define TARGET_PHYS_ADDR_SPACE_BITS 32
 #ifdef CONFIG_USER_ONLY
 # define TARGET_VIRT_ADDR_SPACE_BITS 31
target/nios2/helper.c +11 -0
index a8b8ec6..ca3b087 100644
@@ -25,6 +25,7 @@
 #include "exec/exec-all.h"
 #include "exec/log.h"
 #include "exec/helper-proto.h"
+#include "exec/semihost.h"
 
 #if defined(CONFIG_USER_ONLY)
 
@@ -169,6 +170,16 @@ void nios2_cpu_do_interrupt(CPUState *cs)
         break;
 
     case EXCP_BREAK:
+        qemu_log_mask(CPU_LOG_INT, "BREAK exception at pc=%x\n",
+                      env->regs[R_PC]);
+
+        if (semihosting_enabled()) {
+            qemu_log_mask(CPU_LOG_INT, "Entering semihosting\n");
+            env->regs[R_PC] += 4;
+            do_nios2_semihosting(env);
+            break;
+        }
+
         if ((env->regs[CR_STATUS] & CR_STATUS_EH) == 0) {
             env->regs[CR_BSTATUS] = env->regs[CR_STATUS];
             env->regs[R_BA] = env->regs[R_PC] + 4;
target/nios2/nios2-semi.c new file +446 -0

446 lines not on this page · raw message

Signature · 1 line
2.8.1
#0 lore raw

v5 cover Nios II generic board config and semihosting

This is the fifth version of the patch series last posted here:

http://lists.nongnu.org/archive/html/qemu-devel/2018-08/msg01987.html

Since the previous version, I've updated the copyrights on the new files, refreshed the patches against current trunk, and fixed bugs in the implementations of lseek() and gettimeofday().

The original version of these patches was rejected because there was no corresponding open-source BSP or I/O library support, making it difficult to test the code. I contributed those pieces to libgloss last summer (commit fddc74d12bf7f765c04c3182a7237ecf23893d27), so that should no longer be a blocking issue.

Sandra Loosemore (2):
Add generic Nios II board.
Add Nios II semihosting support.

 default-configs/nios2-softmmu.mak |   1 +
 hw/nios2/Makefile.objs            |   1 +
 hw/nios2/boot.c                   |   5 +-
 hw/nios2/generic_nommu.c          | 130 +++++++++++
 qemu-options.hx                   |   8 +-
 target/nios2/Makefile.objs        |   2 +-
 target/nios2/cpu.h                |   4 +-
 target/nios2/helper.c             |  11 +
 target/nios2/nios2-semi.c         | 446 ++++++++++++++++++++++++++++++++++++++
 9 files changed, 601 insertions(+), 7 deletions(-)
 create mode 100644 hw/nios2/generic_nommu.c
 create mode 100644 target/nios2/nios2-semi.c
Signature · 1 line
2.8.1
#1 lore raw

PATCH v5 1/2 Add generic Nios II board.

This patch adds support for a generic MMU-less Nios II board that can be used e.g. for bare-metal compiler testing. Nios II booting is also tweaked so that bare-metal binaries start executing in RAM starting at 0x00000000, rather than an alias at 0xc0000000, which allows features such as unwinding to work when binaries are linked to start at the beginning of the address space.

The generic_nommu.c parts are by Andrew Jenner, based on code by Marek Vasut.

Originally by Marek Vasut and Andrew Jenner.

---
 default-configs/nios2-softmmu.mak |   1 +
 hw/nios2/Makefile.objs            |   1 +
 hw/nios2/boot.c                   |   5 +-
 hw/nios2/generic_nommu.c          | 130 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 136 insertions(+), 1 deletion(-)
 create mode 100644 hw/nios2/generic_nommu.c
default-configs/nios2-softmmu.mak +1 -0
index ab42d0f..95ed1c2 100644
@@ -5,3 +5,4 @@ CONFIG_SERIAL=y
 CONFIG_PTIMER=y
 CONFIG_ALTERA_TIMER=y
 CONFIG_NIOS2_10M50=y
+CONFIG_NIOS2_GENERIC_NOMMU=y
hw/nios2/Makefile.objs +1 -0
index 89a419a..3e01798 100644
@@ -1,2 +1,3 @@
 obj-y = boot.o cpu_pic.o
 obj-$(CONFIG_NIOS2_10M50) += 10m50_devboard.o
+obj-$(CONFIG_NIOS2_GENERIC_NOMMU) += generic_nommu.o
hw/nios2/boot.c +4 -1
index 5f0ab2f..c697047 100644
@@ -140,6 +140,7 @@ void nios2_load_kernel(Nios2CPU *cpu, hwaddr ddr_base,
         uint64_t entry, low, high;
         uint32_t base32;
         int big_endian = 0;
+        int kernel_space = 0;
 
 #ifdef TARGET_WORDS_BIGENDIAN
         big_endian = 1;
@@ -155,10 +156,12 @@ void nios2_load_kernel(Nios2CPU *cpu, hwaddr ddr_base,
                                    translate_kernel_address, NULL,
                                    &entry, NULL, NULL,
                                    big_endian, EM_ALTERA_NIOS2, 0, 0);
+            kernel_space = 1;
         }
 
         /* Always boot into physical ram. */
-        boot_info.bootstrap_pc = ddr_base + 0xc0000000 + (entry & 0x07ffffff);
+        boot_info.bootstrap_pc = ddr_base + (kernel_space ? 0xc0000000 : 0)
+                                 + (entry & 0x07ffffff);
 
         /* If it wasn't an ELF image, try an u-boot image. */
         if (kernel_size < 0) {
hw/nios2/generic_nommu.c new file +130 -0

130 lines not on this page · raw message

Signature · 1 line
2.8.1
#2 lore raw

Re: [Qemu-devel] [PATCH v5 1/2] Add generic Nios II board.

Quoted 7 lines · Sandra Loosemore

This patch adds support for a generic MMU-less Nios II board that can be used e.g. for bare-metal compiler testing. Nios II booting is also tweaked so that bare-metal binaries start executing in RAM starting at 0x00000000, rather than an alias at 0xc0000000, which allows features such as unwinding to work when binaries are linked to start at the beginning of the address space.

I should start this review by saying that I know nothing at all about the Nios II architecture, so I'm just going on general principles. Some of my comments might be confused or wrong as a result...

Quoted 47 lines

The generic_nommu.c parts are by Andrew Jenner, based on code by Marek Vasut.

Originally by Marek Vasut and Andrew Jenner.

---
 default-configs/nios2-softmmu.mak |   1 +
 hw/nios2/Makefile.objs            |   1 +
 hw/nios2/boot.c                   |   5 +-
 hw/nios2/generic_nommu.c          | 130 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 136 insertions(+), 1 deletion(-)
 create mode 100644 hw/nios2/generic_nommu.c
default-configs/nios2-softmmu.mak +1 -0
index ab42d0f..95ed1c2 100644
@@ -5,3 +5,4 @@ CONFIG_SERIAL=y
 CONFIG_PTIMER=y
 CONFIG_ALTERA_TIMER=y
 CONFIG_NIOS2_10M50=y
+CONFIG_NIOS2_GENERIC_NOMMU=y
hw/nios2/Makefile.objs +1 -0
index 89a419a..3e01798 100644
@@ -1,2 +1,3 @@
 obj-y = boot.o cpu_pic.o
 obj-$(CONFIG_NIOS2_10M50) += 10m50_devboard.o
+obj-$(CONFIG_NIOS2_GENERIC_NOMMU) += generic_nommu.o
hw/nios2/boot.c +1 -0
index 5f0ab2f..c697047 100644
@@ -140,6 +140,7 @@ void nios2_load_kernel(Nios2CPU *cpu, hwaddr ddr_base,
         uint64_t entry, low, high;
         uint32_t base32;
         int big_endian = 0;
+        int kernel_space = 0;
 
 #ifdef TARGET_WORDS_BIGENDIAN
         big_endian = 1;

hw/nios2/boot.c void nios2_load_kernel(Nios2CPU *cpu, hwaddr ddr_base, · line 164

Quoted 10 lines
hw/nios2/boot.c +3 -1
@@ -155,10 +156,12 @@ void nios2_load_kernel(Nios2CPU *cpu, hwaddr ddr_base,
                                    translate_kernel_address, NULL,
                                    &entry, NULL, NULL,
                                    big_endian, EM_ALTERA_NIOS2, 0, 0);
+            kernel_space = 1;
         }
 
         /* Always boot into physical ram. */
-        boot_info.bootstrap_pc = ddr_base + 0xc0000000 + (entry & 0x07ffffff);
+        boot_info.bootstrap_pc = ddr_base + (kernel_space ? 0xc0000000 : 0)
+                                 + (entry & 0x07ffffff);

It's not clear to me what's going on here, or why an entry address of 0xc000_0000 is special, but I don't know anything about NiosII -- maybe it's clear if you do?

Why isn't the bootstrap_pc just always the entry address ? Some comments on what is being done here and the use cases being addressed might assist. I wasn't able to work out what the remarks in the commit message meant, I'm afraid.

Looking at the code, I don't think that the second call to load_elf() will return a different entry address to the first one (ie translate_kernel_address() is not applied to &entry). That means that kernel_space is only true if entry == 0xc0000000, and
(kernel_space ? 0xc0000000 : 0) + (entry & 0x07ffffff);
is almost the same thing as just 'entry'.

Quoted 5 lines
hw/nios2/boot.c +5 -0
+static void nios2_generic_nommu_init(MachineState *machine)
+{
+    Nios2CPU *cpu;
+    DeviceState *dev;
+    MemoryRegion *address_space_mem = get_system_memory();

hw/nios2/boot.c void nios2_load_kernel(Nios2CPU *cpu, hwaddr ddr_base,

Quoted 10 lines
hw/nios2/boot.c +10 -0
+    MemoryRegion *phys_tcm = g_new(MemoryRegion, 1);
+    MemoryRegion *phys_tcm_alias = g_new(MemoryRegion, 1);
+    MemoryRegion *phys_ram = g_new(MemoryRegion, 1);
+    MemoryRegion *phys_ram_alias = g_new(MemoryRegion, 1);
+    ram_addr_t tcm_base = 0x0;
+    ram_addr_t tcm_size = 0x1000;    /* 1 kiB, but QEMU limit is 4 kiB */
+    ram_addr_t ram_base = 0x10000000;
+    ram_addr_t ram_size = 0x08000000;
+    qemu_irq *cpu_irq, irq[32];
+    int i;

The description says this is "generic", but it appears to be almost identical to the existing 10M50 board model, including having exactly the same devices at the same apparently arbitrary addresses.

Could we instead add a machine parameter to the existing board so you could say "-machine 10m50-ghrd,mmu=no" (and drop the other changes -- it's not clear what they're needed for) ?

If it really ought to be a separate board model, perhaps it should be in the same source file and share the common code.

thanks
-- PMM

#7 lore raw

Re: [Qemu-devel] [PATCH v5 2/2] Add Nios II semihosting support.

Quoted 10 lines · Peter Maydell
Quoted 6 lines · Sandra Loosemore

This patch adds support for libgloss semihosting to Nios II bare-metal emulation.

Do you have a link to the spec that defines this semihosting ABI, please ?

Well, I just wrote some derived from the comments in the libgloss sources; see the attachment. :-)

FWIW this is pretty much a direct copy of the m68k semihosting protocol, which CodeSourcery contributed ages ago to both libgloss and qemu.

-Sandra

Nios II Semihosting Protocol
----------------------------

The runtime (libgloss) indicates a semihosting request to the debug agent by issuing a "break 1" instruction. r4 and r5 are used to pass parameters per the normal C ABI on nios2.

r4 contains a request code. r5 is typically a pointer to a 4-word parameter block, except for the exit operation where it is an immediate integer value.

The result of the operation is returned in the first word of the parameter block. The second word is used to return an errno value, encoded per the "Errno Values" section of the RSP documentation in the GDB User Manual.

The supported r4 request codes are:

#define HOSTED_EXIT 0

Terminate program execution; send a 'W' stop reply to GDB.

r5 contains the exit code, as an immediate integer rather than indirectly in a parameter block. This semihosting request isn't expected to return.

#define HOSTED_INIT_SIM 1

Reserved/unimplemented.

#define HOSTED_OPEN 2

Open file; 'Fopen' GDB fileio request.

r5 points to a parameter block containing:
[0] pointer to filename
[1] filename length
[2] open flags, encoded per the GDB RSP documentation
[3] mode, encoded per the GDB RSP documentation

Return values in parameter block:
[0] file descriptor or -1 on error
[1] errno, encoded per the GDB RSP documentation

#define HOSTED_CLOSE 3

Close file; 'Fclose' GDB fileio request.

r5 points to a parameter block containing:
[0] file descriptor

Return values in parameter block:
[0] return status
[1] errno, encoded per the GDB RSP documentation

#define HOSTED_READ 4

Read from file; 'Fread' GDB fileio request.

r5 points to a parameter block containing:
[0] file descriptor
[1] pointer to buffer
[2] buffer size

Return values in parameter block:
[0] number of bytes read
[1] errno, encoded per the GDB RSP documentation

#define HOSTED_WRITE 5

Write to file; 'Fwrite' GDB fileio request.

r5 points to a parameter block containing:
[0] file descriptor
[1] pointer to buffer
[2] byte count

Return values in parameter block:
[0] number of bytes written
[1] errno, encoded per the GDB RSP documentation

#define HOSTED_LSEEK 6

File seek; 'Flseek' GDB fileio request.

r5 points to a parameter block containing:
[0] file descriptor
[1] high word of 64-bit offset
[2] low word of 64-bit offset
[3] seek flag, encoded per the GDB RSP documentation

Return values in parameter block:
[0] high word of 64-bit result
[1] low word of 64-bit result
[2] errno, encoded per the GDB RSP documentation

#define HOSTED_RENAME 7

File rename; 'Frename' GDB fileio request.

r5 points to a parameter block containing:
[0] oldname pointer
[1] oldname length
[2] newname pointer
[3] newname length

Return values in parameter block:
[0] return status
[1] errno, encoded per the GDB RSP documentation

#define HOSTED_UNLINK 8

File unlink/delete; 'Funlink' GDB fileio request.

r5 points to a parameter block containing:
[0] filename pointer
[1] filename length

Return values in parameter block:
[0] return status
[1] errno, encoded per the GDB RSP documentation

#define HOSTED_STAT 9

File information; 'Fstat' GDB fileio request.

r5 points to a parameter block containing:
[0] filename pointer
[1] filename length
[2] pointer to stat buf, using the structure definition in the GDB RSP documentation

Return values in parameter block:
[0] return status
[1] errno, encoded per the GDB RSP documentation

#define HOSTED_FSTAT 10

File information; 'Ffstat' GDB fileio request.

r5 points to a parameter block containing:
[0] file descriptor
[1] pointer to stat buf, using the structure definition in the GDB RSP documentation

Return values in parameter block:
[0] return status
[1] errno, encoded per the GDB RSP documentation

#define HOSTED_GETTIMEOFDAY 11

Get current time; 'Fgettimeofday' GDB fileio request.

r5 points to a parameter block containing:
[0] timeval pointer, using the structure definition in the GDB RSP documentation

Return values in parameter block:
[0] return status
[1] errno, encoded per the GDB RSP documentation

#define HOSTED_ISATTY 12

Return true if the file descriptor is the GDB console; 'Fisatty' GDB fileio request.

r5 points to a parameter block containing:
[0] file descriptor

Return values in parameter block:
[0] return status
[1] errno, encoded per the GDB RSP documentation

#define HOSTED_SYSTEM 13

System call; 'Fsystem' GDB fileio request.

r5 points to a parameter block containing:
[0] command pointer
[1] command length

Return values in parameter block:
[0] return status
[1] errno, encoded per the GDB RSP documentation

#8 lore raw

Re: [Qemu-devel] [PATCH v5 2/2] Add Nios II semihosting support.

Quoted 7 lines · Sandra Loosemore
Quoted 5 lines · Peter Maydell
Quoted 4 lines · Sandra Loosemore

This patch adds support for libgloss semihosting to Nios II bare-metal emulation.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Do you have a link to the spec that defines this semihosting ABI, please ?

Well, I just wrote some derived from the comments in the libgloss sources; see the attachment. :-)

Thanks. So who "owns" this ABI (ie has the authority to change it and should be the end documenting it)? How many projects or bits of software are implementing either end of it?

If we decide that QEMU owns the spec we can put the documentation into docs/specs/.

Quoted 2 lines

FWIW this is pretty much a direct copy of the m68k semihosting protocol, which CodeSourcery contributed ages ago to both libgloss and qemu.

Yeah, and that seems to be undocumented too :-( I'm not a huge fan of "this is just some random thing that's private to QEMU and libgloss".

thanks
-- PMM

#9 lore raw

Re: [Qemu-devel] [PATCH v5 2/2] Add Nios II semihosting support.

Quoted 14 lines · Peter Maydell
Quoted 13 lines · Sandra Loosemore
Quoted 10 lines · Peter Maydell
Quoted 6 lines · Sandra Loosemore

This patch adds support for libgloss semihosting to Nios II bare-metal emulation.

Do you have a link to the spec that defines this semihosting ABI, please ?

Quoted 6 lines
Quoted 2 lines

Well, I just wrote some derived from the comments in the libgloss sources; see the attachment. :-)

Thanks. So who "owns" this ABI (ie has the authority to change it and should be the end documenting it)? How many projects or bits of software are implementing either end of it?

Going back in ancient history, I implemented the m68k version in libgloss in 2006 to support a hardware debug stub that CodeSourcery was also providing at that time. We later moved the runtime side of it into target-agnostic code in an internal library, so when it came time to do a similar JTAG debug stub for bare-metal Nios II hardware testing in 2012, we re-used our existing code for both library and debug stub. Later Altera implemented the same protocol in some proprietary simulators they provided to us, and more recently we wrote these patches to add it to QEMU. We've shifted away from hardware testing and no longer use the original debug stub now.

Quoted 2 lines

If we decide that QEMU owns the spec we can put the documentation into docs/specs/.

Making QEMU the "owner" of the ABI seems a little peculiar to me since it is only one client among several, and is a latecomer too. I think libgloss would make a little more sense. OTOH, I have no problem with making the documentation part of QEMU.

-Sandra

#10 lore raw

Re: [Qemu-devel] [PATCH v5 2/2] Add Nios II semihosting support.

Quoted 17 lines · Sandra Loosemore
Quoted 3 lines · Peter Maydell

Thanks. So who "owns" this ABI (ie has the authority to change it and should be the end documenting it)? How many projects or bits of software are implementing either end of it?

Going back in ancient history, I implemented the m68k version in libgloss in 2006 to support a hardware debug stub that CodeSourcery was also providing at that time. We later moved the runtime side of it into target-agnostic code in an internal library, so when it came time to do a similar JTAG debug stub for bare-metal Nios II hardware testing in 2012, we re-used our existing code for both library and debug stub. Later Altera implemented the same protocol in some proprietary simulators they provided to us, and more recently we wrote these patches to add it to QEMU. We've shifted away from hardware testing and no longer use the original debug stub now.

Quoted 7 lines
Quoted 2 lines

If we decide that QEMU owns the spec we can put the documentation into docs/specs/.

Making QEMU the "owner" of the ABI seems a little peculiar to me since it is only one client among several, and is a latecomer too. I think libgloss would make a little more sense. OTOH, I have no problem with making the documentation part of QEMU.

Thanks for the backstory. I agree that if QEMU is just one of multiple implementations then it's not a great place to hold the specification. Either libgloss, or I suppose in theory Altera as the owner of the architecture could bless the specification and host it...

-- PMM

#3 lore raw

Re: [Qemu-devel] [PATCH v5 1/2] Add generic Nios II board.

In reply to Peter Maydell
Quoted 19 lines · Peter Maydell
Quoted 19 lines
hw/nios2/boot.c +2 -0
index 5f0ab2f..c697047 100644
@@ -140,6 +140,7 @@ void nios2_load_kernel(Nios2CPU *cpu, hwaddr ddr_base,
          uint64_t entry, low, high;
          uint32_t base32;
          int big_endian = 0;
+        int kernel_space = 0;
 
  #ifdef TARGET_WORDS_BIGENDIAN
          big_endian = 1;
@@ -155,10 +156,12 @@ void nios2_load_kernel(Nios2CPU *cpu, hwaddr ddr_base,
                                     translate_kernel_address, NULL,
                                     &entry, NULL, NULL,
                                     big_endian, EM_ALTERA_NIOS2, 0, 0);
+            kernel_space = 1;
          }
 
Quoted 8 lines

hw/nios2/boot.c void nios2_load_kernel(Nios2CPU *cpu, hwaddr ddr_base, · line 164

Quoted 4 lines
hw/nios2/boot.c +2 -1
          /* Always boot into physical ram. */
-        boot_info.bootstrap_pc = ddr_base + 0xc0000000 + (entry & 0x07ffffff);
+        boot_info.bootstrap_pc = ddr_base + (kernel_space ? 0xc0000000 : 0)
+                                 + (entry & 0x07ffffff);

It's not clear to me what's going on here, or why an entry address of 0xc000_0000 is special, but I don't know anything about NiosII -- maybe it's clear if you do?

The processor reference guide documents that the kernel is placed at virtual memory address 0xc0000000.

https://www.intel.com/content/www/us/en/programmable/documentation/iga1409256728501.html#iga1409332620358

The problem the patch to boot.c is trying to solve is that we found things like unwind info were screwed up when using -kernel to load executables with an entry address other than 0xc0000000.

Quoted 5 lines

Why isn't the bootstrap_pc just always the entry address ? Some comments on what is being done here and the use cases being addressed might assist. I wasn't able to work out what the remarks in the commit message meant, I'm afraid.

Quoted 7 lines

Looking at the code, I don't think that the second call to load_elf() will return a different entry address to the first one (ie translate_kernel_address() is not applied to &entry). That means that kernel_space is only true if entry == 0xc0000000, and
(kernel_space ? 0xc0000000 : 0) + (entry & 0x07ffffff);
is almost the same thing as just 'entry'.

It seems like these remarks are directed more at the existing code than the patch.... :-S TBH, I don't know why it was done that way originally.

Quoted 22 lines
Quoted 6 lines
hw/nios2/boot.c +5 -0
+static void nios2_generic_nommu_init(MachineState *machine)
+{
+    Nios2CPU *cpu;
+    DeviceState *dev;
+    MemoryRegion *address_space_mem = get_system_memory();

hw/nios2/boot.c void nios2_load_kernel(Nios2CPU *cpu, hwaddr ddr_base,

Quoted 10 lines
hw/nios2/boot.c +10 -0
+    MemoryRegion *phys_tcm = g_new(MemoryRegion, 1);
+    MemoryRegion *phys_tcm_alias = g_new(MemoryRegion, 1);
+    MemoryRegion *phys_ram = g_new(MemoryRegion, 1);
+    MemoryRegion *phys_ram_alias = g_new(MemoryRegion, 1);
+    ram_addr_t tcm_base = 0x0;
+    ram_addr_t tcm_size = 0x1000;    /* 1 kiB, but QEMU limit is 4 kiB */
+    ram_addr_t ram_base = 0x10000000;
+    ram_addr_t ram_size = 0x08000000;
+    qemu_irq *cpu_irq, irq[32];
+    int i;

The description says this is "generic", but it appears to be almost identical to the existing 10M50 board model, including having exactly the same devices at the same apparently arbitrary addresses.

Quoted 8 lines

Could we instead add a machine parameter to the existing board so you could say "-machine 10m50-ghrd,mmu=no" (and drop the other changes -- it's not clear what they're needed for) ?

If it really ought to be a separate board model, perhaps it should be in the same source file and share the common code.

I didn't write this code, but the intent was actually to allow executables linked for the 3c120 devboards we'd been using for hardware testing to run in this emulation; not to emulate a mmu-less 10m50 board.
The BSP that we contributed to libgloss locates the reset vector, etc
appropriately for this emulation.

I can't comment on whether the peripherals copied from the 10m50 emulation are actually necessary or useful for anything; we certainly don't intend to support manipulating them from the program being loaded, but maybe other parts of QEMU expect certain devices to be present (I've seen that on other targets like ARM). Andrew, do you have any state on this left?

-Sandra

#4 lore raw

Re: [Qemu-devel] [PATCH v5 1/2] Add generic Nios II board.

Quoted 30 lines · Sandra Loosemore
Quoted 19 lines · Peter Maydell
Quoted 19 lines
hw/nios2/boot.c +2 -0
index 5f0ab2f..c697047 100644
@@ -140,6 +140,7 @@ void nios2_load_kernel(Nios2CPU *cpu, hwaddr ddr_base,
          uint64_t entry, low, high;
          uint32_t base32;
          int big_endian = 0;
+        int kernel_space = 0;
 
  #ifdef TARGET_WORDS_BIGENDIAN
          big_endian = 1;
@@ -155,10 +156,12 @@ void nios2_load_kernel(Nios2CPU *cpu, hwaddr ddr_base,
                                     translate_kernel_address, NULL,
                                     &entry, NULL, NULL,
                                     big_endian, EM_ALTERA_NIOS2, 0, 0);
+            kernel_space = 1;
          }
 
Quoted 8 lines

hw/nios2/boot.c void nios2_load_kernel(Nios2CPU *cpu, hwaddr ddr_base, · line 164

Quoted 4 lines
hw/nios2/boot.c +2 -1
          /* Always boot into physical ram. */
-        boot_info.bootstrap_pc = ddr_base + 0xc0000000 + (entry & 0x07ffffff);
+        boot_info.bootstrap_pc = ddr_base + (kernel_space ? 0xc0000000 : 0)
+                                 + (entry & 0x07ffffff);

It's not clear to me what's going on here, or why an entry address of 0xc000_0000 is special, but I don't know anything about NiosII -- maybe it's clear if you do?

Quoted 8 lines

The processor reference guide documents that the kernel is placed at virtual memory address 0xc0000000.

https://www.intel.com/content/www/us/en/programmable/documentation/iga1409256728501.html#iga1409332620358

The problem the patch to boot.c is trying to solve is that we found things like unwind info were screwed up when using -kernel to load executables with an entry address other than 0xc0000000.

Ah, so this is one of those architectures like MIPS that has multiple aliases of the same lump of physical RAM with different properties. So an ELF file could be linked to load in one of multiple aliases.

Quoted 13 lines
Quoted 5 lines

Why isn't the bootstrap_pc just always the entry address ? Some comments on what is being done here and the use cases being addressed might assist. I wasn't able to work out what the remarks in the commit message meant, I'm afraid.

Quoted 7 lines

Looking at the code, I don't think that the second call to load_elf() will return a different entry address to the first one (ie translate_kernel_address() is not applied to &entry). That means that kernel_space is only true if entry == 0xc0000000, and
(kernel_space ? 0xc0000000 : 0) + (entry & 0x07ffffff);
is almost the same thing as just 'entry'.

Quoted 2 lines

It seems like these remarks are directed more at the existing code than the patch.... :-S TBH, I don't know why it was done that way originally.

Yeah, some of this is the fault of the original code, which is doing weird things and not documenting why. But you're changing this code, so you're currently the best expert we have at what the architecture should do. So I'm trying to figure out by asking you questions what the architecture does and what the code is intended to do.

Right now my impression from reading the code and this patch is that the existing code is doing something odd and possibly wrong but which happens to work for whatever set of executables it was being tested with, and then this patch is layering on a workaround that fixes some other set of use cases -- but it would be better if we could figure out what the real hardware does and do that, which should then work in all the cases we're trying to support (or we can say "no, these binaries are built wrongly and they wouldn't load on the real hardware either"). If the real hardware is doing something odd that's OK, but then we should have a nice clear comment describing what the hardware does.

Quoted 10 lines
Quoted 10 lines

The description says this is "generic", but it appears to be almost identical to the existing 10M50 board model, including having exactly the same devices at the same apparently arbitrary addresses.

Could we instead add a machine parameter to the existing board so you could say "-machine 10m50-ghrd,mmu=no" (and drop the other changes -- it's not clear what they're needed for) ?

Quoted 7 lines
Quoted 3 lines

If it really ought to be a separate board model, perhaps it should be in the same source file and share the common code.

I didn't write this code, but the intent was actually to allow executables linked for the 3c120 devboards we'd been using for hardware testing to run in this emulation; not to emulate a mmu-less 10m50 board.

That sounds like the right answer is "provide a model of a 3c120 board", not "provide something that is labelled generic but is actually a 10m50 with no MMU" ?

thanks
-- PMM