blob: 07fc4c2300d58cfe3f7742549d48a45792467794 [file] [log] [blame]
thsf0fc6f82007-10-17 13:39:42 +00001/*
2 * QEMU/mipssim emulation
3 *
Stefan Weilb5e49462011-11-13 22:24:26 +01004 * Emulates a very simple machine model similar to the one used by the
thsf0fc6f82007-10-17 13:39:42 +00005 * proprietary MIPS emulator.
thsa79ee212007-10-31 17:14:08 +00006 *
7 * Copyright (c) 2007 Thiemo Seufer
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
thsf0fc6f82007-10-17 13:39:42 +000026 */
Peter Maydellc6848222016-01-18 17:35:00 +000027#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010028#include "qapi/error.h"
Paolo Bonzini4771d752016-01-19 21:51:44 +010029#include "qemu-common.h"
30#include "cpu.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010031#include "hw/hw.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010032#include "hw/mips/mips.h"
33#include "hw/mips/cpudevs.h"
34#include "hw/char/serial.h"
35#include "hw/isa/isa.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020036#include "net/net.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010037#include "sysemu/sysemu.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010038#include "hw/boards.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010039#include "hw/mips/bios.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010040#include "hw/loader.h"
Blue Swirlca20cf32009-09-20 14:58:02 +000041#include "elf.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010042#include "hw/sysbus.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010043#include "exec/address-spaces.h"
Aurelien Jarno2e985fe2013-08-03 16:03:18 +020044#include "qemu/error-report.h"
Andreas Färber22d55232013-07-29 17:01:37 +020045#include "sysemu/qtest.h"
thsf0fc6f82007-10-17 13:39:42 +000046
ths7df526e2007-11-09 17:52:11 +000047static struct _loaderparams {
48 int ram_size;
49 const char *kernel_filename;
50 const char *kernel_cmdline;
51 const char *initrd_filename;
52} loaderparams;
53
Aurelien Jarnoe16ad5b2009-11-14 01:04:29 +010054typedef struct ResetData {
Andreas Färber2d44fc82012-05-05 14:19:45 +020055 MIPSCPU *cpu;
Aurelien Jarnoe16ad5b2009-11-14 01:04:29 +010056 uint64_t vector;
57} ResetData;
58
59static int64_t load_kernel(void)
thsf0fc6f82007-10-17 13:39:42 +000060{
Aurelien Jarno409dbce2010-03-14 21:20:59 +010061 int64_t entry, kernel_high;
thsf0fc6f82007-10-17 13:39:42 +000062 long kernel_size;
63 long initrd_size;
Anthony Liguoric227f092009-10-01 16:12:16 -050064 ram_addr_t initrd_offset;
Blue Swirlca20cf32009-09-20 14:58:02 +000065 int big_endian;
66
67#ifdef TARGET_WORDS_BIGENDIAN
68 big_endian = 1;
69#else
70 big_endian = 0;
71#endif
thsf0fc6f82007-10-17 13:39:42 +000072
Aurelien Jarno409dbce2010-03-14 21:20:59 +010073 kernel_size = load_elf(loaderparams.kernel_filename, cpu_mips_kseg0_to_phys,
74 NULL, (uint64_t *)&entry, NULL,
75 (uint64_t *)&kernel_high, big_endian,
Peter Crosthwaite7ef295e2016-03-04 11:30:21 +000076 EM_MIPS, 1, 0);
thsf0fc6f82007-10-17 13:39:42 +000077 if (kernel_size >= 0) {
78 if ((entry & ~0x7fffffffULL) == 0x80000000)
79 entry = (int32_t)entry;
thsf0fc6f82007-10-17 13:39:42 +000080 } else {
Aurelien Jarno3ee31222017-07-27 01:56:13 +020081 error_report("qemu: could not load kernel '%s': %s",
82 loaderparams.kernel_filename,
83 load_elf_strerror(kernel_size));
thsf0fc6f82007-10-17 13:39:42 +000084 exit(1);
85 }
86
87 /* load initrd */
88 initrd_size = 0;
89 initrd_offset = 0;
ths7df526e2007-11-09 17:52:11 +000090 if (loaderparams.initrd_filename) {
91 initrd_size = get_image_size (loaderparams.initrd_filename);
thsf0fc6f82007-10-17 13:39:42 +000092 if (initrd_size > 0) {
James Hogan05b32742013-06-27 08:35:27 +010093 initrd_offset = (kernel_high + ~INITRD_PAGE_MASK) & INITRD_PAGE_MASK;
ths7df526e2007-11-09 17:52:11 +000094 if (initrd_offset + initrd_size > loaderparams.ram_size) {
thsf0fc6f82007-10-17 13:39:42 +000095 fprintf(stderr,
96 "qemu: memory too small for initial ram disk '%s'\n",
ths7df526e2007-11-09 17:52:11 +000097 loaderparams.initrd_filename);
thsf0fc6f82007-10-17 13:39:42 +000098 exit(1);
99 }
pbrookdcac9672009-04-09 20:05:49 +0000100 initrd_size = load_image_targphys(loaderparams.initrd_filename,
101 initrd_offset, loaderparams.ram_size - initrd_offset);
thsf0fc6f82007-10-17 13:39:42 +0000102 }
103 if (initrd_size == (target_ulong) -1) {
104 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
ths7df526e2007-11-09 17:52:11 +0000105 loaderparams.initrd_filename);
thsf0fc6f82007-10-17 13:39:42 +0000106 exit(1);
107 }
108 }
Aurelien Jarnoe16ad5b2009-11-14 01:04:29 +0100109 return entry;
thsf0fc6f82007-10-17 13:39:42 +0000110}
111
112static void main_cpu_reset(void *opaque)
113{
Aurelien Jarnoe16ad5b2009-11-14 01:04:29 +0100114 ResetData *s = (ResetData *)opaque;
Andreas Färber2d44fc82012-05-05 14:19:45 +0200115 CPUMIPSState *env = &s->cpu->env;
thsf0fc6f82007-10-17 13:39:42 +0000116
Andreas Färber2d44fc82012-05-05 14:19:45 +0200117 cpu_reset(CPU(s->cpu));
Nathan Froydaecf1372010-06-08 13:30:03 -0700118 env->active_tc.PC = s->vector & ~(target_ulong)1;
119 if (s->vector & 1) {
120 env->hflags |= MIPS_HFLAG_M16;
121 }
thsf0fc6f82007-10-17 13:39:42 +0000122}
123
Hervé Poussineaud118d642011-09-04 22:29:26 +0200124static void mipsnet_init(int base, qemu_irq irq, NICInfo *nd)
125{
126 DeviceState *dev;
127 SysBusDevice *s;
128
129 dev = qdev_create(NULL, "mipsnet");
130 qdev_set_nic_properties(dev, nd);
131 qdev_init_nofail(dev);
132
Andreas Färber1356b982013-01-20 02:47:33 +0100133 s = SYS_BUS_DEVICE(dev);
Hervé Poussineaud118d642011-09-04 22:29:26 +0200134 sysbus_connect_irq(s, 0, irq);
135 memory_region_add_subregion(get_system_io(),
136 base,
137 sysbus_mmio_get_region(s, 0));
138}
139
thsf0fc6f82007-10-17 13:39:42 +0000140static void
Marcel Apfelbaum3ef96222014-05-07 17:42:57 +0300141mips_mipssim_init(MachineState *machine)
thsf0fc6f82007-10-17 13:39:42 +0000142{
Marcel Apfelbaum3ef96222014-05-07 17:42:57 +0300143 ram_addr_t ram_size = machine->ram_size;
144 const char *cpu_model = machine->cpu_model;
145 const char *kernel_filename = machine->kernel_filename;
146 const char *kernel_cmdline = machine->kernel_cmdline;
147 const char *initrd_filename = machine->initrd_filename;
Paul Brook5cea8592009-05-30 00:52:44 +0100148 char *filename;
Avi Kivity23ebf232011-08-08 22:17:28 +0300149 MemoryRegion *address_space_mem = get_system_memory();
Paolo Bonzinibdb75c72013-07-22 15:54:20 +0200150 MemoryRegion *isa = g_new(MemoryRegion, 1);
Avi Kivity23ebf232011-08-08 22:17:28 +0300151 MemoryRegion *ram = g_new(MemoryRegion, 1);
152 MemoryRegion *bios = g_new(MemoryRegion, 1);
Andreas Färber7ee274c2012-05-05 14:17:49 +0200153 MIPSCPU *cpu;
Andreas Färber61c56c82012-03-14 01:38:23 +0100154 CPUMIPSState *env;
Aurelien Jarnoe16ad5b2009-11-14 01:04:29 +0100155 ResetData *reset_info;
thsb5334152007-10-18 15:05:11 +0000156 int bios_size;
thsf0fc6f82007-10-17 13:39:42 +0000157
158 /* Init CPUs. */
159 if (cpu_model == NULL) {
160#ifdef TARGET_MIPS64
161 cpu_model = "5Kf";
162#else
163 cpu_model = "24Kf";
164#endif
165 }
Andreas Färber7ee274c2012-05-05 14:17:49 +0200166 cpu = cpu_mips_init(cpu_model);
167 if (cpu == NULL) {
bellardaaed9092007-11-10 15:15:54 +0000168 fprintf(stderr, "Unable to find CPU definition\n");
169 exit(1);
170 }
Andreas Färber7ee274c2012-05-05 14:17:49 +0200171 env = &cpu->env;
172
Anthony Liguori7267c092011-08-20 22:09:37 -0500173 reset_info = g_malloc0(sizeof(ResetData));
Andreas Färber2d44fc82012-05-05 14:19:45 +0200174 reset_info->cpu = cpu;
Aurelien Jarnoe16ad5b2009-11-14 01:04:29 +0100175 reset_info->vector = env->active_tc.PC;
176 qemu_register_reset(main_cpu_reset, reset_info);
thsf0fc6f82007-10-17 13:39:42 +0000177
178 /* Allocate RAM. */
Dirk Müller6a926fb2015-03-24 22:28:15 +0100179 memory_region_allocate_system_memory(ram, NULL, "mips_mipssim.ram",
180 ram_size);
Peter Maydell98a99ce2017-07-07 15:42:53 +0100181 memory_region_init_ram(bios, NULL, "mips_mipssim.bios", BIOS_SIZE,
Markus Armbrusterf8ed85a2015-09-11 16:51:43 +0200182 &error_fatal);
Avi Kivity23ebf232011-08-08 22:17:28 +0300183 memory_region_set_readonly(bios, true);
thsf0fc6f82007-10-17 13:39:42 +0000184
Avi Kivity23ebf232011-08-08 22:17:28 +0300185 memory_region_add_subregion(address_space_mem, 0, ram);
pbrookdcac9672009-04-09 20:05:49 +0000186
187 /* Map the BIOS / boot exception handler. */
Avi Kivity23ebf232011-08-08 22:17:28 +0300188 memory_region_add_subregion(address_space_mem, 0x1fc00000LL, bios);
thsf0fc6f82007-10-17 13:39:42 +0000189 /* Load a BIOS / boot exception handler image. */
190 if (bios_name == NULL)
191 bios_name = BIOS_FILENAME;
Paul Brook5cea8592009-05-30 00:52:44 +0100192 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
193 if (filename) {
194 bios_size = load_image_targphys(filename, 0x1fc00000LL, BIOS_SIZE);
Anthony Liguori7267c092011-08-20 22:09:37 -0500195 g_free(filename);
Paul Brook5cea8592009-05-30 00:52:44 +0100196 } else {
197 bios_size = -1;
198 }
Andreas Färber22d55232013-07-29 17:01:37 +0200199 if ((bios_size < 0 || bios_size > BIOS_SIZE) &&
200 !kernel_filename && !qtest_enabled()) {
thsf0fc6f82007-10-17 13:39:42 +0000201 /* Bail out if we have neither a kernel image nor boot vector code. */
Aurelien Jarno2e985fe2013-08-03 16:03:18 +0200202 error_report("Could not load MIPS bios '%s', and no "
Gonglei77e205a2014-11-15 18:06:41 +0800203 "-kernel argument was specified", bios_name);
Aurelien Jarno2e985fe2013-08-03 16:03:18 +0200204 exit(1);
thsf0fc6f82007-10-17 13:39:42 +0000205 } else {
thsb5334152007-10-18 15:05:11 +0000206 /* We have a boot vector start address. */
thsb5dc7732008-06-27 10:02:35 +0000207 env->active_tc.PC = (target_long)(int32_t)0xbfc00000;
thsf0fc6f82007-10-17 13:39:42 +0000208 }
209
210 if (kernel_filename) {
ths7df526e2007-11-09 17:52:11 +0000211 loaderparams.ram_size = ram_size;
212 loaderparams.kernel_filename = kernel_filename;
213 loaderparams.kernel_cmdline = kernel_cmdline;
214 loaderparams.initrd_filename = initrd_filename;
Aurelien Jarnoe16ad5b2009-11-14 01:04:29 +0100215 reset_info->vector = load_kernel();
thsf0fc6f82007-10-17 13:39:42 +0000216 }
217
218 /* Init CPU internal devices. */
Paolo Bonzini5a975d42016-03-15 14:32:19 +0100219 cpu_mips_irq_init_cpu(cpu);
220 cpu_mips_clock_init(cpu);
thsf0fc6f82007-10-17 13:39:42 +0000221
222 /* Register 64 KB of ISA IO space at 0x1fd00000. */
Paolo Bonzinibdb75c72013-07-22 15:54:20 +0200223 memory_region_init_alias(isa, NULL, "isa_mmio",
224 get_system_io(), 0, 0x00010000);
225 memory_region_add_subregion(get_system_memory(), 0x1fd00000, isa);
thsf0fc6f82007-10-17 13:39:42 +0000226
227 /* A single 16450 sits at offset 0x3f8. It is attached to
228 MIPS CPU INT2, which is interrupt 4. */
229 if (serial_hds[0])
Julien Grall568fd152012-09-19 12:50:07 +0100230 serial_init(0x3f8, env->irq[4], 115200, serial_hds[0],
231 get_system_io());
thsf0fc6f82007-10-17 13:39:42 +0000232
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100233 if (nd_table[0].used)
aliguori0ae18ce2009-01-13 19:39:36 +0000234 /* MIPSnet uses the MIPS CPU INT0, which is interrupt 2. */
235 mipsnet_init(0x4200, env->irq[2], &nd_table[0]);
thsf0fc6f82007-10-17 13:39:42 +0000236}
237
Eduardo Habkoste264d292015-09-04 15:37:08 -0300238static void mips_mipssim_machine_init(MachineClass *mc)
Anthony Liguorif80f9ec2009-05-20 18:38:09 -0500239{
Eduardo Habkoste264d292015-09-04 15:37:08 -0300240 mc->desc = "MIPS MIPSsim platform";
241 mc->init = mips_mipssim_init;
Anthony Liguorif80f9ec2009-05-20 18:38:09 -0500242}
243
Eduardo Habkoste264d292015-09-04 15:37:08 -0300244DEFINE_MACHINE("mipssim", mips_mipssim_machine_init)