blob: b6ccb3cd4ae9fa40d03a934d62b02089095d5ca9 [file] [log] [blame]
Max Filippov47d05a82011-09-06 03:55:55 +04001/*
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 Maydell09aae232016-01-26 18:17:21 +000028#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010029#include "qapi/error.h"
Paolo Bonzini4771d752016-01-19 21:51:44 +010030#include "qemu-common.h"
31#include "cpu.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010032#include "sysemu/sysemu.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010033#include "hw/boards.h"
34#include "hw/loader.h"
Max Filippov47d05a82011-09-06 03:55:55 +040035#include "elf.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010036#include "exec/memory.h"
37#include "exec/address-spaces.h"
Max Filippov8488ab02014-06-21 16:14:56 +040038#include "qemu/error-report.h"
Max Filippove53fa622017-12-22 13:53:36 -080039#include "xtensa_memory.h"
Max Filippovb68755c2017-02-22 18:59:32 -080040
Andreas Färber00b941e2013-06-29 18:55:54 +020041static uint64_t translate_phys_addr(void *opaque, uint64_t addr)
Max Filippov47d05a82011-09-06 03:55:55 +040042{
Andreas Färber00b941e2013-06-29 18:55:54 +020043 XtensaCPU *cpu = opaque;
44
45 return cpu_get_phys_page_debug(CPU(cpu), addr);
Max Filippov47d05a82011-09-06 03:55:55 +040046}
47
Andreas Färber11e7bfd2012-05-04 19:28:19 +020048static void sim_reset(void *opaque)
Max Filippov47d05a82011-09-06 03:55:55 +040049{
Andreas Färber11e7bfd2012-05-04 19:28:19 +020050 XtensaCPU *cpu = opaque;
51
52 cpu_reset(CPU(cpu));
Max Filippov47d05a82011-09-06 03:55:55 +040053}
54
Marcel Apfelbaum3ef96222014-05-07 17:42:57 +030055static void xtensa_sim_init(MachineState *machine)
Max Filippov47d05a82011-09-06 03:55:55 +040056{
Andreas Färber06d26272012-05-04 19:26:57 +020057 XtensaCPU *cpu = NULL;
Andreas Färber5bfcb362012-03-14 01:38:24 +010058 CPUXtensaState *env = NULL;
Marcel Apfelbaum3ef96222014-05-07 17:42:57 +030059 ram_addr_t ram_size = machine->ram_size;
Marcel Apfelbaum3ef96222014-05-07 17:42:57 +030060 const char *kernel_filename = machine->kernel_filename;
Max Filippov47d05a82011-09-06 03:55:55 +040061 int n;
62
63 for (n = 0; n < smp_cpus; n++) {
Igor Mammedovd58eeae2017-10-05 15:50:59 +020064 cpu = XTENSA_CPU(cpu_create(machine->cpu_type));
Andreas Färber06d26272012-05-04 19:26:57 +020065 env = &cpu->env;
66
Max Filippov47d05a82011-09-06 03:55:55 +040067 env->sregs[PRID] = n;
Andreas Färber11e7bfd2012-05-04 19:28:19 +020068 qemu_register_reset(sim_reset, cpu);
Max Filippov47d05a82011-09-06 03:55:55 +040069 /* Need MMU initialized prior to ELF loading,
70 * so that ELF gets loaded into virtual addresses
71 */
Andreas Färber11e7bfd2012-05-04 19:28:19 +020072 sim_reset(cpu);
Max Filippov47d05a82011-09-06 03:55:55 +040073 }
74
Max Filippovb68755c2017-02-22 18:59:32 -080075 if (env) {
76 XtensaMemory sysram = env->config->sysram;
Max Filippov47d05a82011-09-06 03:55:55 +040077
Max Filippovb68755c2017-02-22 18:59:32 -080078 sysram.location[0].size = ram_size;
Max Filippove53fa622017-12-22 13:53:36 -080079 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 Filippovb68755c2017-02-22 18:59:32 -080091 }
Max Filippov47d05a82011-09-06 03:55:55 +040092
Peter Maydell9bca0ed2018-04-20 15:52:43 +010093 if (serial_hd(0)) {
94 xtensa_sim_open_console(serial_hd(0));
Max Filippov8128b3e2017-05-12 11:09:14 -070095 }
Max Filippov47d05a82011-09-06 03:55:55 +040096 if (kernel_filename) {
97 uint64_t elf_entry;
98 uint64_t elf_lowaddr;
99#ifdef TARGET_WORDS_BIGENDIAN
Andreas Färber00b941e2013-06-29 18:55:54 +0200100 int success = load_elf(kernel_filename, translate_phys_addr, cpu,
Peter Crosthwaite7ef295e2016-03-04 11:30:21 +0000101 &elf_entry, &elf_lowaddr, NULL, 1, EM_XTENSA, 0, 0);
Max Filippov47d05a82011-09-06 03:55:55 +0400102#else
Andreas Färber00b941e2013-06-29 18:55:54 +0200103 int success = load_elf(kernel_filename, translate_phys_addr, cpu,
Peter Crosthwaite7ef295e2016-03-04 11:30:21 +0000104 &elf_entry, &elf_lowaddr, NULL, 0, EM_XTENSA, 0, 0);
Max Filippov47d05a82011-09-06 03:55:55 +0400105#endif
106 if (success > 0) {
107 env->pc = elf_entry;
108 }
109 }
110}
111
Eduardo Habkoste264d292015-09-04 15:37:08 -0300112static void xtensa_sim_machine_init(MachineClass *mc)
Max Filippov47d05a82011-09-06 03:55:55 +0400113{
Eduardo Habkoste264d292015-09-04 15:37:08 -0300114 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 Filippov8128b3e2017-05-12 11:09:14 -0700118 mc->no_serial = 1;
Igor Mammedovd58eeae2017-10-05 15:50:59 +0200119 mc->default_cpu_type = XTENSA_DEFAULT_CPU_TYPE;
Max Filippov47d05a82011-09-06 03:55:55 +0400120}
121
Eduardo Habkoste264d292015-09-04 15:37:08 -0300122DEFINE_MACHINE("sim", xtensa_sim_machine_init)