Max Filippov | 47d05a8 | 2011-09-06 03:55:55 +0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011, Max Filippov, Open Source and Linux Lab. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * * Neither the name of the Open Source and Linux Lab nor the |
| 13 | * names of its contributors may be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
Peter Maydell | 09aae23 | 2016-01-26 18:17:21 +0000 | [diff] [blame] | 28 | #include "qemu/osdep.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 29 | #include "qapi/error.h" |
Paolo Bonzini | 4771d75 | 2016-01-19 21:51:44 +0100 | [diff] [blame] | 30 | #include "qemu-common.h" |
| 31 | #include "cpu.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 32 | #include "sysemu/sysemu.h" |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 33 | #include "hw/boards.h" |
| 34 | #include "hw/loader.h" |
Max Filippov | 47d05a8 | 2011-09-06 03:55:55 +0400 | [diff] [blame] | 35 | #include "elf.h" |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 36 | #include "exec/memory.h" |
| 37 | #include "exec/address-spaces.h" |
Max Filippov | 8488ab0 | 2014-06-21 16:14:56 +0400 | [diff] [blame] | 38 | #include "qemu/error-report.h" |
Max Filippov | e53fa62 | 2017-12-22 13:53:36 -0800 | [diff] [blame] | 39 | #include "xtensa_memory.h" |
Max Filippov | b68755c | 2017-02-22 18:59:32 -0800 | [diff] [blame] | 40 | |
Andreas Färber | 00b941e | 2013-06-29 18:55:54 +0200 | [diff] [blame] | 41 | static uint64_t translate_phys_addr(void *opaque, uint64_t addr) |
Max Filippov | 47d05a8 | 2011-09-06 03:55:55 +0400 | [diff] [blame] | 42 | { |
Andreas Färber | 00b941e | 2013-06-29 18:55:54 +0200 | [diff] [blame] | 43 | XtensaCPU *cpu = opaque; |
| 44 | |
| 45 | return cpu_get_phys_page_debug(CPU(cpu), addr); |
Max Filippov | 47d05a8 | 2011-09-06 03:55:55 +0400 | [diff] [blame] | 46 | } |
| 47 | |
Andreas Färber | 11e7bfd | 2012-05-04 19:28:19 +0200 | [diff] [blame] | 48 | static void sim_reset(void *opaque) |
Max Filippov | 47d05a8 | 2011-09-06 03:55:55 +0400 | [diff] [blame] | 49 | { |
Andreas Färber | 11e7bfd | 2012-05-04 19:28:19 +0200 | [diff] [blame] | 50 | XtensaCPU *cpu = opaque; |
| 51 | |
| 52 | cpu_reset(CPU(cpu)); |
Max Filippov | 47d05a8 | 2011-09-06 03:55:55 +0400 | [diff] [blame] | 53 | } |
| 54 | |
Marcel Apfelbaum | 3ef9622 | 2014-05-07 17:42:57 +0300 | [diff] [blame] | 55 | static void xtensa_sim_init(MachineState *machine) |
Max Filippov | 47d05a8 | 2011-09-06 03:55:55 +0400 | [diff] [blame] | 56 | { |
Andreas Färber | 06d2627 | 2012-05-04 19:26:57 +0200 | [diff] [blame] | 57 | XtensaCPU *cpu = NULL; |
Andreas Färber | 5bfcb36 | 2012-03-14 01:38:24 +0100 | [diff] [blame] | 58 | CPUXtensaState *env = NULL; |
Marcel Apfelbaum | 3ef9622 | 2014-05-07 17:42:57 +0300 | [diff] [blame] | 59 | ram_addr_t ram_size = machine->ram_size; |
Marcel Apfelbaum | 3ef9622 | 2014-05-07 17:42:57 +0300 | [diff] [blame] | 60 | const char *kernel_filename = machine->kernel_filename; |
Max Filippov | 47d05a8 | 2011-09-06 03:55:55 +0400 | [diff] [blame] | 61 | int n; |
| 62 | |
| 63 | for (n = 0; n < smp_cpus; n++) { |
Igor Mammedov | d58eeae | 2017-10-05 15:50:59 +0200 | [diff] [blame] | 64 | cpu = XTENSA_CPU(cpu_create(machine->cpu_type)); |
Andreas Färber | 06d2627 | 2012-05-04 19:26:57 +0200 | [diff] [blame] | 65 | env = &cpu->env; |
| 66 | |
Max Filippov | 47d05a8 | 2011-09-06 03:55:55 +0400 | [diff] [blame] | 67 | env->sregs[PRID] = n; |
Andreas Färber | 11e7bfd | 2012-05-04 19:28:19 +0200 | [diff] [blame] | 68 | qemu_register_reset(sim_reset, cpu); |
Max Filippov | 47d05a8 | 2011-09-06 03:55:55 +0400 | [diff] [blame] | 69 | /* Need MMU initialized prior to ELF loading, |
| 70 | * so that ELF gets loaded into virtual addresses |
| 71 | */ |
Andreas Färber | 11e7bfd | 2012-05-04 19:28:19 +0200 | [diff] [blame] | 72 | sim_reset(cpu); |
Max Filippov | 47d05a8 | 2011-09-06 03:55:55 +0400 | [diff] [blame] | 73 | } |
| 74 | |
Max Filippov | b68755c | 2017-02-22 18:59:32 -0800 | [diff] [blame] | 75 | if (env) { |
| 76 | XtensaMemory sysram = env->config->sysram; |
Max Filippov | 47d05a8 | 2011-09-06 03:55:55 +0400 | [diff] [blame] | 77 | |
Max Filippov | b68755c | 2017-02-22 18:59:32 -0800 | [diff] [blame] | 78 | sysram.location[0].size = ram_size; |
Max Filippov | e53fa62 | 2017-12-22 13:53:36 -0800 | [diff] [blame] | 79 | xtensa_create_memory_regions(&env->config->instrom, "xtensa.instrom", |
| 80 | get_system_memory()); |
| 81 | xtensa_create_memory_regions(&env->config->instram, "xtensa.instram", |
| 82 | get_system_memory()); |
| 83 | xtensa_create_memory_regions(&env->config->datarom, "xtensa.datarom", |
| 84 | get_system_memory()); |
| 85 | xtensa_create_memory_regions(&env->config->dataram, "xtensa.dataram", |
| 86 | get_system_memory()); |
| 87 | xtensa_create_memory_regions(&env->config->sysrom, "xtensa.sysrom", |
| 88 | get_system_memory()); |
| 89 | xtensa_create_memory_regions(&sysram, "xtensa.sysram", |
| 90 | get_system_memory()); |
Max Filippov | b68755c | 2017-02-22 18:59:32 -0800 | [diff] [blame] | 91 | } |
Max Filippov | 47d05a8 | 2011-09-06 03:55:55 +0400 | [diff] [blame] | 92 | |
Peter Maydell | 9bca0ed | 2018-04-20 15:52:43 +0100 | [diff] [blame] | 93 | if (serial_hd(0)) { |
| 94 | xtensa_sim_open_console(serial_hd(0)); |
Max Filippov | 8128b3e | 2017-05-12 11:09:14 -0700 | [diff] [blame] | 95 | } |
Max Filippov | 47d05a8 | 2011-09-06 03:55:55 +0400 | [diff] [blame] | 96 | if (kernel_filename) { |
| 97 | uint64_t elf_entry; |
| 98 | uint64_t elf_lowaddr; |
| 99 | #ifdef TARGET_WORDS_BIGENDIAN |
Andreas Färber | 00b941e | 2013-06-29 18:55:54 +0200 | [diff] [blame] | 100 | int success = load_elf(kernel_filename, translate_phys_addr, cpu, |
Peter Crosthwaite | 7ef295e | 2016-03-04 11:30:21 +0000 | [diff] [blame] | 101 | &elf_entry, &elf_lowaddr, NULL, 1, EM_XTENSA, 0, 0); |
Max Filippov | 47d05a8 | 2011-09-06 03:55:55 +0400 | [diff] [blame] | 102 | #else |
Andreas Färber | 00b941e | 2013-06-29 18:55:54 +0200 | [diff] [blame] | 103 | int success = load_elf(kernel_filename, translate_phys_addr, cpu, |
Peter Crosthwaite | 7ef295e | 2016-03-04 11:30:21 +0000 | [diff] [blame] | 104 | &elf_entry, &elf_lowaddr, NULL, 0, EM_XTENSA, 0, 0); |
Max Filippov | 47d05a8 | 2011-09-06 03:55:55 +0400 | [diff] [blame] | 105 | #endif |
| 106 | if (success > 0) { |
| 107 | env->pc = elf_entry; |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
Eduardo Habkost | e264d29 | 2015-09-04 15:37:08 -0300 | [diff] [blame] | 112 | static void xtensa_sim_machine_init(MachineClass *mc) |
Max Filippov | 47d05a8 | 2011-09-06 03:55:55 +0400 | [diff] [blame] | 113 | { |
Eduardo Habkost | e264d29 | 2015-09-04 15:37:08 -0300 | [diff] [blame] | 114 | mc->desc = "sim machine (" XTENSA_DEFAULT_CPU_MODEL ")"; |
| 115 | mc->is_default = true; |
| 116 | mc->init = xtensa_sim_init; |
| 117 | mc->max_cpus = 4; |
Max Filippov | 8128b3e | 2017-05-12 11:09:14 -0700 | [diff] [blame] | 118 | mc->no_serial = 1; |
Igor Mammedov | d58eeae | 2017-10-05 15:50:59 +0200 | [diff] [blame] | 119 | mc->default_cpu_type = XTENSA_DEFAULT_CPU_TYPE; |
Max Filippov | 47d05a8 | 2011-09-06 03:55:55 +0400 | [diff] [blame] | 120 | } |
| 121 | |
Eduardo Habkost | e264d29 | 2015-09-04 15:37:08 -0300 | [diff] [blame] | 122 | DEFINE_MACHINE("sim", xtensa_sim_machine_init) |