]>
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" |
da34e65c | 28 | #include "qapi/error.h" |
a360d965 AG |
29 | #include "hw/sysbus.h" |
30 | #include "hw/hw.h" | |
0d09e41a PB |
31 | #include "hw/i386/pc.h" |
32 | #include "hw/isa/isa.h" | |
a360d965 AG |
33 | #include "net/net.h" |
34 | #include "sysemu/sysemu.h" | |
35 | #include "hw/boards.h" | |
36 | #include "hw/loader.h" | |
0d09e41a | 37 | #include "hw/char/serial.h" |
a360d965 | 38 | #include "exec/address-spaces.h" |
98dbe5ac | 39 | #include "elf.h" |
a360d965 AG |
40 | |
41 | #define PHYS_MEM_BASE 0x80000000 | |
42 | ||
43 | typedef struct { | |
44 | uint64_t ram_size; | |
45 | const char *kernel_filename; | |
46 | const char *kernel_cmdline; | |
47 | const char *initrd_filename; | |
48 | } LoaderParams; | |
49 | ||
50 | static void load_kernel(MoxieCPU *cpu, LoaderParams *loader_params) | |
51 | { | |
52 | uint64_t entry, kernel_low, kernel_high; | |
53 | long kernel_size; | |
54 | long initrd_size; | |
55 | ram_addr_t initrd_offset; | |
56 | ||
57 | kernel_size = load_elf(loader_params->kernel_filename, NULL, NULL, | |
7ef295ea PC |
58 | &entry, &kernel_low, &kernel_high, 1, EM_MOXIE, |
59 | 0, 0); | |
a360d965 | 60 | |
6a2331d1 | 61 | if (kernel_size <= 0) { |
a360d965 AG |
62 | fprintf(stderr, "qemu: could not load kernel '%s'\n", |
63 | loader_params->kernel_filename); | |
64 | exit(1); | |
65 | } | |
66 | ||
67 | /* load initrd */ | |
68 | initrd_size = 0; | |
69 | initrd_offset = 0; | |
70 | if (loader_params->initrd_filename) { | |
71 | initrd_size = get_image_size(loader_params->initrd_filename); | |
72 | if (initrd_size > 0) { | |
73 | initrd_offset = (kernel_high + ~TARGET_PAGE_MASK) | |
74 | & TARGET_PAGE_MASK; | |
75 | if (initrd_offset + initrd_size > loader_params->ram_size) { | |
76 | fprintf(stderr, | |
77 | "qemu: memory too small for initial ram disk '%s'\n", | |
78 | loader_params->initrd_filename); | |
79 | exit(1); | |
80 | } | |
81 | initrd_size = load_image_targphys(loader_params->initrd_filename, | |
82 | initrd_offset, | |
83 | ram_size); | |
84 | } | |
85 | if (initrd_size == (target_ulong)-1) { | |
86 | fprintf(stderr, "qemu: could not load initial ram disk '%s'\n", | |
87 | loader_params->initrd_filename); | |
88 | exit(1); | |
89 | } | |
90 | } | |
91 | } | |
92 | ||
93 | static void main_cpu_reset(void *opaque) | |
94 | { | |
95 | MoxieCPU *cpu = opaque; | |
96 | ||
97 | cpu_reset(CPU(cpu)); | |
98 | } | |
a360d965 | 99 | |
3ef96221 | 100 | static void moxiesim_init(MachineState *machine) |
a360d965 AG |
101 | { |
102 | MoxieCPU *cpu = NULL; | |
3ef96221 MA |
103 | ram_addr_t ram_size = machine->ram_size; |
104 | const char *cpu_model = machine->cpu_model; | |
105 | const char *kernel_filename = machine->kernel_filename; | |
106 | const char *kernel_cmdline = machine->kernel_cmdline; | |
107 | const char *initrd_filename = machine->initrd_filename; | |
a360d965 AG |
108 | CPUMoxieState *env; |
109 | MemoryRegion *address_space_mem = get_system_memory(); | |
110 | MemoryRegion *ram = g_new(MemoryRegion, 1); | |
111 | MemoryRegion *rom = g_new(MemoryRegion, 1); | |
112 | hwaddr ram_base = 0x200000; | |
113 | LoaderParams loader_params; | |
114 | ||
115 | /* Init CPUs. */ | |
116 | if (cpu_model == NULL) { | |
117 | cpu_model = "MoxieLite-moxie-cpu"; | |
118 | } | |
119 | cpu = cpu_moxie_init(cpu_model); | |
120 | if (!cpu) { | |
121 | fprintf(stderr, "Unable to find CPU definition\n"); | |
122 | exit(1); | |
123 | } | |
124 | env = &cpu->env; | |
125 | ||
126 | qemu_register_reset(main_cpu_reset, cpu); | |
127 | ||
128 | /* Allocate RAM. */ | |
f8ed85ac | 129 | memory_region_init_ram(ram, NULL, "moxiesim.ram", ram_size, &error_fatal); |
a360d965 AG |
130 | vmstate_register_ram_global(ram); |
131 | memory_region_add_subregion(address_space_mem, ram_base, ram); | |
132 | ||
f8ed85ac | 133 | memory_region_init_ram(rom, NULL, "moxie.rom", 128*0x1000, &error_fatal); |
a360d965 AG |
134 | vmstate_register_ram_global(rom); |
135 | memory_region_add_subregion(get_system_memory(), 0x1000, rom); | |
136 | ||
137 | if (kernel_filename) { | |
138 | loader_params.ram_size = ram_size; | |
139 | loader_params.kernel_filename = kernel_filename; | |
140 | loader_params.kernel_cmdline = kernel_cmdline; | |
141 | loader_params.initrd_filename = initrd_filename; | |
142 | load_kernel(cpu, &loader_params); | |
143 | } | |
144 | ||
145 | /* A single 16450 sits at offset 0x3f8. */ | |
146 | if (serial_hds[0]) { | |
147 | serial_mm_init(address_space_mem, 0x3f8, 0, env->irq[4], | |
148 | 8000000/16, serial_hds[0], DEVICE_LITTLE_ENDIAN); | |
149 | } | |
150 | } | |
151 | ||
e264d29d | 152 | static void moxiesim_machine_init(MachineClass *mc) |
a360d965 | 153 | { |
e264d29d EH |
154 | mc->desc = "Moxie simulator platform"; |
155 | mc->init = moxiesim_init; | |
156 | mc->is_default = 1; | |
a360d965 AG |
157 | } |
158 | ||
e264d29d | 159 | DEFINE_MACHINE("moxiesim", moxiesim_machine_init) |