Skip to contentSkip to conversation

Nios II generic board config and semihosting support

PATCH Superseded Julian Brown 2 participants 4 messages May 17, 2018 – May 18, 2018

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover Nios II generic board config and semihosting support

These patches add support for a generic MMU-less Nios II board and add semihosting support for Nios II, the latter based on the m68k semihosting implementation. These are useful for testing "bare metal" Nios II toolchains, given appropriate startup code. I've tested our internal builds successfully with these patches.

OK, or any comments?

Thanks,

Julian

Julian Brown (2):
Add generic Nios II board.
Add Nios II semihosting support.

 hw/nios2/Makefile.objs     |   2 +-
 hw/nios2/boot.c            |   5 +-
 hw/nios2/generic_nommu.c   | 128 ++++++++++++++
 qemu-options.hx            |   8 +-
 target/nios2/Makefile.objs |   2 +-
 target/nios2/cpu.h         |   4 +-
 target/nios2/helper.c      |  11 ++
 target/nios2/nios2-semi.c  | 429 +++++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 581 insertions(+), 8 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 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.

---
 hw/nios2/Makefile.objs   |   2 +-
 hw/nios2/boot.c          |   5 +-
 hw/nios2/generic_nommu.c | 128 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 133 insertions(+), 2 deletions(-)
 create mode 100644 hw/nios2/generic_nommu.c
hw/nios2/Makefile.objs +1 -1
index 6b5c421..680caaa 100644
@@ -1 +1 @@
-obj-y = boot.o cpu_pic.o 10m50_devboard.o
+obj-y = boot.o cpu_pic.o 10m50_devboard.o generic_nommu.o
hw/nios2/boot.c +4 -1
index 94f436e..8f2887a 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;
@@ -154,10 +155,12 @@ void nios2_load_kernel(Nios2CPU *cpu, hwaddr ddr_base,
             kernel_size = load_elf(kernel_filename, 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 +128 -0

128 lines not on this page · raw message

#3 lore raw

PATCH 2/2 Add Nios II semihosting support.

In reply to Julian Brown

This patch (by Sandra Loosemore, mildly rebased) adds support for semihosting for 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  | 429 +++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 448 insertions(+), 6 deletions(-)
 create mode 100644 target/nios2/nios2-semi.c
qemu-options.hx +4 -4
index abbfa6a..626a99e 100644
@@ -3780,21 +3780,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 145796e..a7efb8a 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 +429 -0

429 lines not on this page · raw message

#2 lore raw

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

In reply to Julian Brown

Hi Julian,

You should Cc the NiosII maintainers for faster review (done).

(From
https://wiki.qemu.org/Contribute/SubmitAPatch#CC_the_relevant_maintainer:)

$ ./scripts/get_maintainer.pl -f hw/nios2/
Chris Wulff <[email protected]> (maintainer:NiosII)
Marek Vasut <[email protected]> (maintainer:NiosII)
Quoted 10 lines · Julian Brown

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.

Can you get/add their S-o-b?

Quoted 7 lines
---
 hw/nios2/Makefile.objs   |   2 +-
 hw/nios2/boot.c          |   5 +-
 hw/nios2/generic_nommu.c | 128 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 133 insertions(+), 2 deletions(-)
 create mode 100644 hw/nios2/generic_nommu.c

It seems your email client messed your patch up, see
https://wiki.qemu.org/Contribute/SubmitAPatch#Do_not_send_as_an_attachment

Can you resend?

Regards,

Phil.