]>
Commit | Line | Data |
---|---|---|
a360d965 AG |
1 | /* |
2 | * QEMU/moxiesim emulation | |
3 | * | |
805a2505 | 4 | * Emulates a very simple machine model similar to the one used by the |
a360d965 AG |
5 | * GDB moxie simulator. |
6 | * | |
7 | * Copyright (c) 2008, 2009, 2010, 2013 Anthony Green | |
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. | |
26 | */ | |
d24688fb | 27 | #include "qemu/osdep.h" |
7ce32f30 | 28 | #include "qemu/error-report.h" |
da34e65c | 29 | #include "qapi/error.h" |
4771d756 PB |
30 | #include "qemu-common.h" |
31 | #include "cpu.h" | |
a360d965 AG |
32 | #include "hw/sysbus.h" |
33 | #include "hw/hw.h" | |
a360d965 AG |
34 | #include "net/net.h" |
35 | #include "sysemu/sysemu.h" | |
36 | #include "hw/boards.h" | |
37 | #include "hw/loader.h" | |
0d09e41a | 38 | #include "hw/char/serial.h" |
a360d965 | 39 | #include "exec/address-spaces.h" |
98dbe5ac | 40 | #include "elf.h" |
a360d965 AG |
41 | |
42 | #define PHYS_MEM_BASE 0x80000000 | |
7ce32f30 TH |
43 | #define FIRMWARE_BASE 0x1000 |
44 | #define FIRMWARE_SIZE (128 * 0x1000) | |
a360d965 AG |
45 | |
46 | typedef struct { | |
47 | uint64_t ram_size; | |
48 | const char *kernel_filename; | |
49 | const char *kernel_cmdline; | |
50 | const char *initrd_filename; | |
51 | } LoaderParams; | |
52 | ||
53 | static void load_kernel(MoxieCPU *cpu, LoaderParams *loader_params) | |
54 | { | |
55 | uint64_t entry, kernel_low, kernel_high; | |
f3839fda | 56 | int64_t initrd_size; |
a360d965 | 57 | long kernel_size; |
a360d965 AG |
58 | ram_addr_t initrd_offset; |
59 | ||
4366e1db | 60 | kernel_size = load_elf(loader_params->kernel_filename, NULL, NULL, NULL, |
7ef295ea PC |
61 | &entry, &kernel_low, &kernel_high, 1, EM_MOXIE, |
62 | 0, 0); | |
a360d965 | 63 | |
6a2331d1 | 64 | if (kernel_size <= 0) { |
2ecdc2c3 AF |
65 | error_report("could not load kernel '%s'", |
66 | loader_params->kernel_filename); | |
a360d965 AG |
67 | exit(1); |
68 | } | |
69 | ||
70 | /* load initrd */ | |
71 | initrd_size = 0; | |
72 | initrd_offset = 0; | |
73 | if (loader_params->initrd_filename) { | |
74 | initrd_size = get_image_size(loader_params->initrd_filename); | |
75 | if (initrd_size > 0) { | |
76 | initrd_offset = (kernel_high + ~TARGET_PAGE_MASK) | |
77 | & TARGET_PAGE_MASK; | |
78 | if (initrd_offset + initrd_size > loader_params->ram_size) { | |
2ecdc2c3 AF |
79 | error_report("memory too small for initial ram disk '%s'", |
80 | loader_params->initrd_filename); | |
a360d965 AG |
81 | exit(1); |
82 | } | |
83 | initrd_size = load_image_targphys(loader_params->initrd_filename, | |
84 | initrd_offset, | |
85 | ram_size); | |
86 | } | |
87 | if (initrd_size == (target_ulong)-1) { | |
2ecdc2c3 AF |
88 | error_report("could not load initial ram disk '%s'", |
89 | loader_params->initrd_filename); | |
a360d965 AG |
90 | exit(1); |
91 | } | |
92 | } | |
93 | } | |
94 | ||
95 | static void main_cpu_reset(void *opaque) | |
96 | { | |
97 | MoxieCPU *cpu = opaque; | |
98 | ||
99 | cpu_reset(CPU(cpu)); | |
100 | } | |
a360d965 | 101 | |
3ef96221 | 102 | static void moxiesim_init(MachineState *machine) |
a360d965 AG |
103 | { |
104 | MoxieCPU *cpu = NULL; | |
3ef96221 | 105 | ram_addr_t ram_size = machine->ram_size; |
3ef96221 MA |
106 | const char *kernel_filename = machine->kernel_filename; |
107 | const char *kernel_cmdline = machine->kernel_cmdline; | |
108 | const char *initrd_filename = machine->initrd_filename; | |
a360d965 AG |
109 | CPUMoxieState *env; |
110 | MemoryRegion *address_space_mem = get_system_memory(); | |
111 | MemoryRegion *ram = g_new(MemoryRegion, 1); | |
112 | MemoryRegion *rom = g_new(MemoryRegion, 1); | |
113 | hwaddr ram_base = 0x200000; | |
114 | LoaderParams loader_params; | |
115 | ||
116 | /* Init CPUs. */ | |
b2c22357 | 117 | cpu = MOXIE_CPU(cpu_create(machine->cpu_type)); |
a360d965 AG |
118 | env = &cpu->env; |
119 | ||
120 | qemu_register_reset(main_cpu_reset, cpu); | |
121 | ||
122 | /* Allocate RAM. */ | |
98a99ce0 | 123 | memory_region_init_ram(ram, NULL, "moxiesim.ram", ram_size, &error_fatal); |
a360d965 AG |
124 | memory_region_add_subregion(address_space_mem, ram_base, ram); |
125 | ||
7ce32f30 TH |
126 | memory_region_init_ram(rom, NULL, "moxie.rom", FIRMWARE_SIZE, &error_fatal); |
127 | memory_region_add_subregion(get_system_memory(), FIRMWARE_BASE, rom); | |
a360d965 AG |
128 | |
129 | if (kernel_filename) { | |
130 | loader_params.ram_size = ram_size; | |
131 | loader_params.kernel_filename = kernel_filename; | |
132 | loader_params.kernel_cmdline = kernel_cmdline; | |
133 | loader_params.initrd_filename = initrd_filename; | |
134 | load_kernel(cpu, &loader_params); | |
135 | } | |
7ce32f30 TH |
136 | if (bios_name) { |
137 | if (load_image_targphys(bios_name, FIRMWARE_BASE, FIRMWARE_SIZE) < 0) { | |
138 | error_report("Failed to load firmware '%s'", bios_name); | |
139 | } | |
140 | } | |
a360d965 AG |
141 | |
142 | /* A single 16450 sits at offset 0x3f8. */ | |
9bca0edb | 143 | if (serial_hd(0)) { |
a360d965 | 144 | serial_mm_init(address_space_mem, 0x3f8, 0, env->irq[4], |
9bca0edb | 145 | 8000000/16, serial_hd(0), DEVICE_LITTLE_ENDIAN); |
a360d965 AG |
146 | } |
147 | } | |
148 | ||
e264d29d | 149 | static void moxiesim_machine_init(MachineClass *mc) |
a360d965 | 150 | { |
e264d29d EH |
151 | mc->desc = "Moxie simulator platform"; |
152 | mc->init = moxiesim_init; | |
153 | mc->is_default = 1; | |
b2c22357 | 154 | mc->default_cpu_type = MOXIE_CPU_TYPE_NAME("MoxieLite"); |
a360d965 AG |
155 | } |
156 | ||
e264d29d | 157 | DEFINE_MACHINE("moxiesim", moxiesim_machine_init) |