Re: [Qemu-devel] [PATCH V8 2/7] nios2: Add architecture emulation support

2017-01-17 Thread Marek Vasut
On 01/17/2017 09:16 AM, Alexander Graf wrote:
> 
> 
>> Am 17.01.2017 um 01:18 schrieb Marek Vasut :
>>
>>> On 01/16/2017 11:21 PM, Alexander Graf wrote:
>>>
>>>
 On 31/12/2016 14:22, Marek Vasut wrote:
 From: Chris Wulff 

 Add support for emulating Altera NiosII R1 architecture into qemu.
 This patch is based on previous work by Chris Wulff from 2012 and
 updated to latest mainline QEMU.

 Signed-off-by: Marek Vasut 
 Cc: Chris Wulff 
 Cc: Jeff Da Silva 
 Cc: Ley Foon Tan 
 Cc: Sandra Loosemore 
 Cc: Yves Vandervennet 
 ---
 V3: Thorough cleanup, deal with the review comments all over the place
 V4: - Use extract32()
- Fix gen_goto_tb() , suppress tcg_gen_goto_tb()
- Clean up gen_check_supervisor() helper
- Use TCGMemOp type for flags
- Drop jump labels from wrctl/rdctl
- More TCG cleanup
 V5: - Simplify load/store handling
- Handle loads into R_ZERO from protected page, add comment
 V6: - Fix division opcode handling
- Add missing disas handling
- V5 review comments cleanup
 V7: - Drop newline at the end of file
 V8: - Rebase on top of qemu/master
- Move the target-nios2 to target/nios2
 ---
 target/nios2/Makefile.objs |   4 +
 target/nios2/cpu.c | 232 +++
 target/nios2/cpu.h | 269 +
 target/nios2/helper.c  | 313 +++
 target/nios2/helper.h  |  27 ++
 target/nios2/mmu.c | 292 ++
 target/nios2/mmu.h |  54 +++
 target/nios2/monitor.c |  35 ++
 target/nios2/op_helper.c   |  47 +++
 target/nios2/translate.c   | 953
 +
 10 files changed, 2226 insertions(+)
 create mode 100644 target/nios2/Makefile.objs
 create mode 100644 target/nios2/cpu.c
 create mode 100644 target/nios2/cpu.h
 create mode 100644 target/nios2/helper.c
 create mode 100644 target/nios2/helper.h
 create mode 100644 target/nios2/mmu.c
 create mode 100644 target/nios2/mmu.h
 create mode 100644 target/nios2/monitor.c
 create mode 100644 target/nios2/op_helper.c
 create mode 100644 target/nios2/translate.c

 diff --git a/target/nios2/Makefile.objs b/target/nios2/Makefile.objs
 new file mode 100644
 index 000..2a11c5c
 --- /dev/null
 +++ b/target/nios2/Makefile.objs
 @@ -0,0 +1,4 @@
 +obj-y += translate.o op_helper.o helper.o cpu.o mmu.o
 +obj-$(CONFIG_SOFTMMU) += monitor.o
 +
 +$(obj)/op_helper.o: QEMU_CFLAGS += $(HELPER_CFLAGS)
 diff --git a/target/nios2/cpu.c b/target/nios2/cpu.c
 new file mode 100644
 index 000..658d684
 --- /dev/null
 +++ b/target/nios2/cpu.c
 @@ -0,0 +1,232 @@
 +/*
 + * QEMU Nios II CPU
 + *
 + * Copyright (c) 2012 Chris Wulff 
 + *
 + * This library is free software; you can redistribute it and/or
 + * modify it under the terms of the GNU Lesser General Public
 + * License as published by the Free Software Foundation; either
 + * version 2.1 of the License, or (at your option) any later version.
 + *
 + * This library is distributed in the hope that it will be useful,
 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 + * Lesser General Public License for more details.
 + *
 + * You should have received a copy of the GNU Lesser General Public
 + * License along with this library; if not, see
 + * 
 + */
 +
 +#include "qemu/osdep.h"
 +#include "qemu-common.h"
 +#include "qapi/error.h"
 +#include "cpu.h"
 +#include "exec/log.h"
 +#include "exec/gdbstub.h"
 +#include "hw/qdev-properties.h"
 +
 +static void nios2_cpu_set_pc(CPUState *cs, vaddr value)
 +{
 +Nios2CPU *cpu = NIOS2_CPU(cs);
 +CPUNios2State *env = >env;
 +
 +env->regs[R_PC] = value;
 +}
 +
 +static bool nios2_cpu_has_work(CPUState *cs)
 +{
 +return cs->interrupt_request & (CPU_INTERRUPT_HARD |
 CPU_INTERRUPT_NMI);
 +}
 +
 +/* CPUClass::reset() */
 +static void nios2_cpu_reset(CPUState *cs)
 +{
 +Nios2CPU *cpu = NIOS2_CPU(cs);
 +Nios2CPUClass *ncc = NIOS2_CPU_GET_CLASS(cpu);
 +CPUNios2State *env = >env;
 +
 +if (qemu_loglevel_mask(CPU_LOG_RESET)) {
 +qemu_log("CPU Reset (CPU %d)\n", cs->cpu_index);
 +log_cpu_state(cs, 0);
 +}
 +
 +ncc->parent_reset(cs);
 +
 +tlb_flush(cs, 1);
 +
 +memset(env->regs, 0, sizeof(uint32_t) * 

Re: [Qemu-devel] [PATCH V8 2/7] nios2: Add architecture emulation support

2017-01-17 Thread Alexander Graf


> Am 17.01.2017 um 01:18 schrieb Marek Vasut :
> 
>> On 01/16/2017 11:21 PM, Alexander Graf wrote:
>> 
>> 
>>> On 31/12/2016 14:22, Marek Vasut wrote:
>>> From: Chris Wulff 
>>> 
>>> Add support for emulating Altera NiosII R1 architecture into qemu.
>>> This patch is based on previous work by Chris Wulff from 2012 and
>>> updated to latest mainline QEMU.
>>> 
>>> Signed-off-by: Marek Vasut 
>>> Cc: Chris Wulff 
>>> Cc: Jeff Da Silva 
>>> Cc: Ley Foon Tan 
>>> Cc: Sandra Loosemore 
>>> Cc: Yves Vandervennet 
>>> ---
>>> V3: Thorough cleanup, deal with the review comments all over the place
>>> V4: - Use extract32()
>>>- Fix gen_goto_tb() , suppress tcg_gen_goto_tb()
>>>- Clean up gen_check_supervisor() helper
>>>- Use TCGMemOp type for flags
>>>- Drop jump labels from wrctl/rdctl
>>>- More TCG cleanup
>>> V5: - Simplify load/store handling
>>>- Handle loads into R_ZERO from protected page, add comment
>>> V6: - Fix division opcode handling
>>>- Add missing disas handling
>>>- V5 review comments cleanup
>>> V7: - Drop newline at the end of file
>>> V8: - Rebase on top of qemu/master
>>>- Move the target-nios2 to target/nios2
>>> ---
>>> target/nios2/Makefile.objs |   4 +
>>> target/nios2/cpu.c | 232 +++
>>> target/nios2/cpu.h | 269 +
>>> target/nios2/helper.c  | 313 +++
>>> target/nios2/helper.h  |  27 ++
>>> target/nios2/mmu.c | 292 ++
>>> target/nios2/mmu.h |  54 +++
>>> target/nios2/monitor.c |  35 ++
>>> target/nios2/op_helper.c   |  47 +++
>>> target/nios2/translate.c   | 953
>>> +
>>> 10 files changed, 2226 insertions(+)
>>> create mode 100644 target/nios2/Makefile.objs
>>> create mode 100644 target/nios2/cpu.c
>>> create mode 100644 target/nios2/cpu.h
>>> create mode 100644 target/nios2/helper.c
>>> create mode 100644 target/nios2/helper.h
>>> create mode 100644 target/nios2/mmu.c
>>> create mode 100644 target/nios2/mmu.h
>>> create mode 100644 target/nios2/monitor.c
>>> create mode 100644 target/nios2/op_helper.c
>>> create mode 100644 target/nios2/translate.c
>>> 
>>> diff --git a/target/nios2/Makefile.objs b/target/nios2/Makefile.objs
>>> new file mode 100644
>>> index 000..2a11c5c
>>> --- /dev/null
>>> +++ b/target/nios2/Makefile.objs
>>> @@ -0,0 +1,4 @@
>>> +obj-y += translate.o op_helper.o helper.o cpu.o mmu.o
>>> +obj-$(CONFIG_SOFTMMU) += monitor.o
>>> +
>>> +$(obj)/op_helper.o: QEMU_CFLAGS += $(HELPER_CFLAGS)
>>> diff --git a/target/nios2/cpu.c b/target/nios2/cpu.c
>>> new file mode 100644
>>> index 000..658d684
>>> --- /dev/null
>>> +++ b/target/nios2/cpu.c
>>> @@ -0,0 +1,232 @@
>>> +/*
>>> + * QEMU Nios II CPU
>>> + *
>>> + * Copyright (c) 2012 Chris Wulff 
>>> + *
>>> + * This library is free software; you can redistribute it and/or
>>> + * modify it under the terms of the GNU Lesser General Public
>>> + * License as published by the Free Software Foundation; either
>>> + * version 2.1 of the License, or (at your option) any later version.
>>> + *
>>> + * This library is distributed in the hope that it will be useful,
>>> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
>>> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
>>> + * Lesser General Public License for more details.
>>> + *
>>> + * You should have received a copy of the GNU Lesser General Public
>>> + * License along with this library; if not, see
>>> + * 
>>> + */
>>> +
>>> +#include "qemu/osdep.h"
>>> +#include "qemu-common.h"
>>> +#include "qapi/error.h"
>>> +#include "cpu.h"
>>> +#include "exec/log.h"
>>> +#include "exec/gdbstub.h"
>>> +#include "hw/qdev-properties.h"
>>> +
>>> +static void nios2_cpu_set_pc(CPUState *cs, vaddr value)
>>> +{
>>> +Nios2CPU *cpu = NIOS2_CPU(cs);
>>> +CPUNios2State *env = >env;
>>> +
>>> +env->regs[R_PC] = value;
>>> +}
>>> +
>>> +static bool nios2_cpu_has_work(CPUState *cs)
>>> +{
>>> +return cs->interrupt_request & (CPU_INTERRUPT_HARD |
>>> CPU_INTERRUPT_NMI);
>>> +}
>>> +
>>> +/* CPUClass::reset() */
>>> +static void nios2_cpu_reset(CPUState *cs)
>>> +{
>>> +Nios2CPU *cpu = NIOS2_CPU(cs);
>>> +Nios2CPUClass *ncc = NIOS2_CPU_GET_CLASS(cpu);
>>> +CPUNios2State *env = >env;
>>> +
>>> +if (qemu_loglevel_mask(CPU_LOG_RESET)) {
>>> +qemu_log("CPU Reset (CPU %d)\n", cs->cpu_index);
>>> +log_cpu_state(cs, 0);
>>> +}
>>> +
>>> +ncc->parent_reset(cs);
>>> +
>>> +tlb_flush(cs, 1);
>>> +
>>> +memset(env->regs, 0, sizeof(uint32_t) * NUM_CORE_REGS);
>>> +env->regs[R_PC] = cpu->reset_addr;
>>> +
>>> +#if defined(CONFIG_USER_ONLY)
>>> +/* Start in user mode with interrupts enabled. */
>>> +env->regs[CR_STATUS] 

Re: [Qemu-devel] [PATCH V8 2/7] nios2: Add architecture emulation support

2017-01-16 Thread Marek Vasut
On 01/16/2017 11:21 PM, Alexander Graf wrote:
> 
> 
> On 31/12/2016 14:22, Marek Vasut wrote:
>> From: Chris Wulff 
>>
>> Add support for emulating Altera NiosII R1 architecture into qemu.
>> This patch is based on previous work by Chris Wulff from 2012 and
>> updated to latest mainline QEMU.
>>
>> Signed-off-by: Marek Vasut 
>> Cc: Chris Wulff 
>> Cc: Jeff Da Silva 
>> Cc: Ley Foon Tan 
>> Cc: Sandra Loosemore 
>> Cc: Yves Vandervennet 
>> ---
>> V3: Thorough cleanup, deal with the review comments all over the place
>> V4: - Use extract32()
>> - Fix gen_goto_tb() , suppress tcg_gen_goto_tb()
>> - Clean up gen_check_supervisor() helper
>> - Use TCGMemOp type for flags
>> - Drop jump labels from wrctl/rdctl
>> - More TCG cleanup
>> V5: - Simplify load/store handling
>> - Handle loads into R_ZERO from protected page, add comment
>> V6: - Fix division opcode handling
>> - Add missing disas handling
>> - V5 review comments cleanup
>> V7: - Drop newline at the end of file
>> V8: - Rebase on top of qemu/master
>> - Move the target-nios2 to target/nios2
>> ---
>>  target/nios2/Makefile.objs |   4 +
>>  target/nios2/cpu.c | 232 +++
>>  target/nios2/cpu.h | 269 +
>>  target/nios2/helper.c  | 313 +++
>>  target/nios2/helper.h  |  27 ++
>>  target/nios2/mmu.c | 292 ++
>>  target/nios2/mmu.h |  54 +++
>>  target/nios2/monitor.c |  35 ++
>>  target/nios2/op_helper.c   |  47 +++
>>  target/nios2/translate.c   | 953
>> +
>>  10 files changed, 2226 insertions(+)
>>  create mode 100644 target/nios2/Makefile.objs
>>  create mode 100644 target/nios2/cpu.c
>>  create mode 100644 target/nios2/cpu.h
>>  create mode 100644 target/nios2/helper.c
>>  create mode 100644 target/nios2/helper.h
>>  create mode 100644 target/nios2/mmu.c
>>  create mode 100644 target/nios2/mmu.h
>>  create mode 100644 target/nios2/monitor.c
>>  create mode 100644 target/nios2/op_helper.c
>>  create mode 100644 target/nios2/translate.c
>>
>> diff --git a/target/nios2/Makefile.objs b/target/nios2/Makefile.objs
>> new file mode 100644
>> index 000..2a11c5c
>> --- /dev/null
>> +++ b/target/nios2/Makefile.objs
>> @@ -0,0 +1,4 @@
>> +obj-y += translate.o op_helper.o helper.o cpu.o mmu.o
>> +obj-$(CONFIG_SOFTMMU) += monitor.o
>> +
>> +$(obj)/op_helper.o: QEMU_CFLAGS += $(HELPER_CFLAGS)
>> diff --git a/target/nios2/cpu.c b/target/nios2/cpu.c
>> new file mode 100644
>> index 000..658d684
>> --- /dev/null
>> +++ b/target/nios2/cpu.c
>> @@ -0,0 +1,232 @@
>> +/*
>> + * QEMU Nios II CPU
>> + *
>> + * Copyright (c) 2012 Chris Wulff 
>> + *
>> + * This library is free software; you can redistribute it and/or
>> + * modify it under the terms of the GNU Lesser General Public
>> + * License as published by the Free Software Foundation; either
>> + * version 2.1 of the License, or (at your option) any later version.
>> + *
>> + * This library is distributed in the hope that it will be useful,
>> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
>> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
>> + * Lesser General Public License for more details.
>> + *
>> + * You should have received a copy of the GNU Lesser General Public
>> + * License along with this library; if not, see
>> + * 
>> + */
>> +
>> +#include "qemu/osdep.h"
>> +#include "qemu-common.h"
>> +#include "qapi/error.h"
>> +#include "cpu.h"
>> +#include "exec/log.h"
>> +#include "exec/gdbstub.h"
>> +#include "hw/qdev-properties.h"
>> +
>> +static void nios2_cpu_set_pc(CPUState *cs, vaddr value)
>> +{
>> +Nios2CPU *cpu = NIOS2_CPU(cs);
>> +CPUNios2State *env = >env;
>> +
>> +env->regs[R_PC] = value;
>> +}
>> +
>> +static bool nios2_cpu_has_work(CPUState *cs)
>> +{
>> +return cs->interrupt_request & (CPU_INTERRUPT_HARD |
>> CPU_INTERRUPT_NMI);
>> +}
>> +
>> +/* CPUClass::reset() */
>> +static void nios2_cpu_reset(CPUState *cs)
>> +{
>> +Nios2CPU *cpu = NIOS2_CPU(cs);
>> +Nios2CPUClass *ncc = NIOS2_CPU_GET_CLASS(cpu);
>> +CPUNios2State *env = >env;
>> +
>> +if (qemu_loglevel_mask(CPU_LOG_RESET)) {
>> +qemu_log("CPU Reset (CPU %d)\n", cs->cpu_index);
>> +log_cpu_state(cs, 0);
>> +}
>> +
>> +ncc->parent_reset(cs);
>> +
>> +tlb_flush(cs, 1);
>> +
>> +memset(env->regs, 0, sizeof(uint32_t) * NUM_CORE_REGS);
>> +env->regs[R_PC] = cpu->reset_addr;
>> +
>> +#if defined(CONFIG_USER_ONLY)
>> +/* Start in user mode with interrupts enabled. */
>> +env->regs[CR_STATUS] = CR_STATUS_U | CR_STATUS_PIE;
> 
> So what is the value of CR_STATUS after reset in softmmu land then?
> Random value from before reset? Probably not what you want :).

Dropped, 

Re: [Qemu-devel] [PATCH V8 2/7] nios2: Add architecture emulation support

2017-01-16 Thread Sandra Loosemore

On 01/16/2017 03:21 PM, Alexander Graf wrote:


+static void nios2_cpu_disas_set_info(CPUState *cpu, disassemble_info
*info)
+{
+/* NOTE: NiosII R2 is not supported yet. */
+info->mach = bfd_arch_nios2;
+#ifdef TARGET_WORDS_BIGENDIAN
+info->print_insn = print_insn_big_nios2;
+#else
+info->print_insn = print_insn_little_nios2;
+#endif


I take it there is no runtime switch for endianness? Most architectures
eventually got one and moved to a single default endianness for softmmu
with swizzling for the "other" one (LE for ARM, BE for ppc).


Maybe QEMU should just error out if configured for big-endianness on 
this target.  Per the published Nios II Processor Reference Handbook, 
"The Nios II architecture uses little-endian byte ordering."  When I was 
working on preparing the nios2 binutils patches for submission, Altera 
asked me to retain the big-endian hooks because they didn't want to rule 
out officially supporting that feature.  I had no way to test anything 
big-endian, of course.


-Sandra




Re: [Qemu-devel] [PATCH V8 2/7] nios2: Add architecture emulation support

2017-01-16 Thread Alexander Graf



On 31/12/2016 14:22, Marek Vasut wrote:

From: Chris Wulff 

Add support for emulating Altera NiosII R1 architecture into qemu.
This patch is based on previous work by Chris Wulff from 2012 and
updated to latest mainline QEMU.

Signed-off-by: Marek Vasut 
Cc: Chris Wulff 
Cc: Jeff Da Silva 
Cc: Ley Foon Tan 
Cc: Sandra Loosemore 
Cc: Yves Vandervennet 
---
V3: Thorough cleanup, deal with the review comments all over the place
V4: - Use extract32()
- Fix gen_goto_tb() , suppress tcg_gen_goto_tb()
- Clean up gen_check_supervisor() helper
- Use TCGMemOp type for flags
- Drop jump labels from wrctl/rdctl
- More TCG cleanup
V5: - Simplify load/store handling
- Handle loads into R_ZERO from protected page, add comment
V6: - Fix division opcode handling
- Add missing disas handling
- V5 review comments cleanup
V7: - Drop newline at the end of file
V8: - Rebase on top of qemu/master
- Move the target-nios2 to target/nios2
---
 target/nios2/Makefile.objs |   4 +
 target/nios2/cpu.c | 232 +++
 target/nios2/cpu.h | 269 +
 target/nios2/helper.c  | 313 +++
 target/nios2/helper.h  |  27 ++
 target/nios2/mmu.c | 292 ++
 target/nios2/mmu.h |  54 +++
 target/nios2/monitor.c |  35 ++
 target/nios2/op_helper.c   |  47 +++
 target/nios2/translate.c   | 953 +
 10 files changed, 2226 insertions(+)
 create mode 100644 target/nios2/Makefile.objs
 create mode 100644 target/nios2/cpu.c
 create mode 100644 target/nios2/cpu.h
 create mode 100644 target/nios2/helper.c
 create mode 100644 target/nios2/helper.h
 create mode 100644 target/nios2/mmu.c
 create mode 100644 target/nios2/mmu.h
 create mode 100644 target/nios2/monitor.c
 create mode 100644 target/nios2/op_helper.c
 create mode 100644 target/nios2/translate.c

diff --git a/target/nios2/Makefile.objs b/target/nios2/Makefile.objs
new file mode 100644
index 000..2a11c5c
--- /dev/null
+++ b/target/nios2/Makefile.objs
@@ -0,0 +1,4 @@
+obj-y += translate.o op_helper.o helper.o cpu.o mmu.o
+obj-$(CONFIG_SOFTMMU) += monitor.o
+
+$(obj)/op_helper.o: QEMU_CFLAGS += $(HELPER_CFLAGS)
diff --git a/target/nios2/cpu.c b/target/nios2/cpu.c
new file mode 100644
index 000..658d684
--- /dev/null
+++ b/target/nios2/cpu.c
@@ -0,0 +1,232 @@
+/*
+ * QEMU Nios II CPU
+ *
+ * Copyright (c) 2012 Chris Wulff 
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see
+ * 
+ */
+
+#include "qemu/osdep.h"
+#include "qemu-common.h"
+#include "qapi/error.h"
+#include "cpu.h"
+#include "exec/log.h"
+#include "exec/gdbstub.h"
+#include "hw/qdev-properties.h"
+
+static void nios2_cpu_set_pc(CPUState *cs, vaddr value)
+{
+Nios2CPU *cpu = NIOS2_CPU(cs);
+CPUNios2State *env = >env;
+
+env->regs[R_PC] = value;
+}
+
+static bool nios2_cpu_has_work(CPUState *cs)
+{
+return cs->interrupt_request & (CPU_INTERRUPT_HARD | CPU_INTERRUPT_NMI);
+}
+
+/* CPUClass::reset() */
+static void nios2_cpu_reset(CPUState *cs)
+{
+Nios2CPU *cpu = NIOS2_CPU(cs);
+Nios2CPUClass *ncc = NIOS2_CPU_GET_CLASS(cpu);
+CPUNios2State *env = >env;
+
+if (qemu_loglevel_mask(CPU_LOG_RESET)) {
+qemu_log("CPU Reset (CPU %d)\n", cs->cpu_index);
+log_cpu_state(cs, 0);
+}
+
+ncc->parent_reset(cs);
+
+tlb_flush(cs, 1);
+
+memset(env->regs, 0, sizeof(uint32_t) * NUM_CORE_REGS);
+env->regs[R_PC] = cpu->reset_addr;
+
+#if defined(CONFIG_USER_ONLY)
+/* Start in user mode with interrupts enabled. */
+env->regs[CR_STATUS] = CR_STATUS_U | CR_STATUS_PIE;


So what is the value of CR_STATUS after reset in softmmu land then? 
Random value from before reset? Probably not what you want :).



+#endif
+}
+
+static void nios2_cpu_initfn(Object *obj)
+{
+CPUState *cs = CPU(obj);
+Nios2CPU *cpu = NIOS2_CPU(obj);
+CPUNios2State *env = >env;
+static bool tcg_initialized;
+
+cpu->mmu_present = true;
+cs->env_ptr = env;
+
+#if !defined(CONFIG_USER_ONLY)
+mmu_init(>mmu);
+#endif
+
+if (tcg_enabled() && !tcg_initialized) {
+tcg_initialized = true;
+nios2_tcg_init();
+}
+}
+
+Nios2CPU 

[Qemu-devel] [PATCH V8 2/7] nios2: Add architecture emulation support

2016-12-31 Thread Marek Vasut
From: Chris Wulff 

Add support for emulating Altera NiosII R1 architecture into qemu.
This patch is based on previous work by Chris Wulff from 2012 and
updated to latest mainline QEMU.

Signed-off-by: Marek Vasut 
Cc: Chris Wulff 
Cc: Jeff Da Silva 
Cc: Ley Foon Tan 
Cc: Sandra Loosemore 
Cc: Yves Vandervennet 
---
V3: Thorough cleanup, deal with the review comments all over the place
V4: - Use extract32()
- Fix gen_goto_tb() , suppress tcg_gen_goto_tb()
- Clean up gen_check_supervisor() helper
- Use TCGMemOp type for flags
- Drop jump labels from wrctl/rdctl
- More TCG cleanup
V5: - Simplify load/store handling
- Handle loads into R_ZERO from protected page, add comment
V6: - Fix division opcode handling
- Add missing disas handling
- V5 review comments cleanup
V7: - Drop newline at the end of file
V8: - Rebase on top of qemu/master
- Move the target-nios2 to target/nios2
---
 target/nios2/Makefile.objs |   4 +
 target/nios2/cpu.c | 232 +++
 target/nios2/cpu.h | 269 +
 target/nios2/helper.c  | 313 +++
 target/nios2/helper.h  |  27 ++
 target/nios2/mmu.c | 292 ++
 target/nios2/mmu.h |  54 +++
 target/nios2/monitor.c |  35 ++
 target/nios2/op_helper.c   |  47 +++
 target/nios2/translate.c   | 953 +
 10 files changed, 2226 insertions(+)
 create mode 100644 target/nios2/Makefile.objs
 create mode 100644 target/nios2/cpu.c
 create mode 100644 target/nios2/cpu.h
 create mode 100644 target/nios2/helper.c
 create mode 100644 target/nios2/helper.h
 create mode 100644 target/nios2/mmu.c
 create mode 100644 target/nios2/mmu.h
 create mode 100644 target/nios2/monitor.c
 create mode 100644 target/nios2/op_helper.c
 create mode 100644 target/nios2/translate.c

diff --git a/target/nios2/Makefile.objs b/target/nios2/Makefile.objs
new file mode 100644
index 000..2a11c5c
--- /dev/null
+++ b/target/nios2/Makefile.objs
@@ -0,0 +1,4 @@
+obj-y += translate.o op_helper.o helper.o cpu.o mmu.o
+obj-$(CONFIG_SOFTMMU) += monitor.o
+
+$(obj)/op_helper.o: QEMU_CFLAGS += $(HELPER_CFLAGS)
diff --git a/target/nios2/cpu.c b/target/nios2/cpu.c
new file mode 100644
index 000..658d684
--- /dev/null
+++ b/target/nios2/cpu.c
@@ -0,0 +1,232 @@
+/*
+ * QEMU Nios II CPU
+ *
+ * Copyright (c) 2012 Chris Wulff 
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see
+ * 
+ */
+
+#include "qemu/osdep.h"
+#include "qemu-common.h"
+#include "qapi/error.h"
+#include "cpu.h"
+#include "exec/log.h"
+#include "exec/gdbstub.h"
+#include "hw/qdev-properties.h"
+
+static void nios2_cpu_set_pc(CPUState *cs, vaddr value)
+{
+Nios2CPU *cpu = NIOS2_CPU(cs);
+CPUNios2State *env = >env;
+
+env->regs[R_PC] = value;
+}
+
+static bool nios2_cpu_has_work(CPUState *cs)
+{
+return cs->interrupt_request & (CPU_INTERRUPT_HARD | CPU_INTERRUPT_NMI);
+}
+
+/* CPUClass::reset() */
+static void nios2_cpu_reset(CPUState *cs)
+{
+Nios2CPU *cpu = NIOS2_CPU(cs);
+Nios2CPUClass *ncc = NIOS2_CPU_GET_CLASS(cpu);
+CPUNios2State *env = >env;
+
+if (qemu_loglevel_mask(CPU_LOG_RESET)) {
+qemu_log("CPU Reset (CPU %d)\n", cs->cpu_index);
+log_cpu_state(cs, 0);
+}
+
+ncc->parent_reset(cs);
+
+tlb_flush(cs, 1);
+
+memset(env->regs, 0, sizeof(uint32_t) * NUM_CORE_REGS);
+env->regs[R_PC] = cpu->reset_addr;
+
+#if defined(CONFIG_USER_ONLY)
+/* Start in user mode with interrupts enabled. */
+env->regs[CR_STATUS] = CR_STATUS_U | CR_STATUS_PIE;
+#endif
+}
+
+static void nios2_cpu_initfn(Object *obj)
+{
+CPUState *cs = CPU(obj);
+Nios2CPU *cpu = NIOS2_CPU(obj);
+CPUNios2State *env = >env;
+static bool tcg_initialized;
+
+cpu->mmu_present = true;
+cs->env_ptr = env;
+
+#if !defined(CONFIG_USER_ONLY)
+mmu_init(>mmu);
+#endif
+
+if (tcg_enabled() && !tcg_initialized) {
+tcg_initialized = true;
+nios2_tcg_init();
+}
+}
+
+Nios2CPU *cpu_nios2_init(const char *cpu_model)
+{
+Nios2CPU *cpu = NIOS2_CPU(object_new(TYPE_NIOS2_CPU));
+
+object_property_set_bool(OBJECT(cpu), true, "realized", NULL);
+
+return cpu;
+}