1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include "qemu/osdep.h"
28#include "qapi/error.h"
29#include "qemu-common.h"
30#include "cpu.h"
31#include "hw/sysbus.h"
32#include "hw/hw.h"
33#include "hw/i386/pc.h"
34#include "hw/isa/isa.h"
35#include "net/net.h"
36#include "sysemu/sysemu.h"
37#include "hw/boards.h"
38#include "hw/loader.h"
39#include "hw/char/serial.h"
40#include "exec/address-spaces.h"
41#include "elf.h"
42
43#define PHYS_MEM_BASE 0x80000000
44
45typedef struct {
46 uint64_t ram_size;
47 const char *kernel_filename;
48 const char *kernel_cmdline;
49 const char *initrd_filename;
50} LoaderParams;
51
52static void load_kernel(MoxieCPU *cpu, LoaderParams *loader_params)
53{
54 uint64_t entry, kernel_low, kernel_high;
55 long kernel_size;
56 long initrd_size;
57 ram_addr_t initrd_offset;
58
59 kernel_size = load_elf(loader_params->kernel_filename, NULL, NULL,
60 &entry, &kernel_low, &kernel_high, 1, EM_MOXIE,
61 0, 0);
62
63 if (kernel_size <= 0) {
64 fprintf(stderr, "qemu: could not load kernel '%s'\n",
65 loader_params->kernel_filename);
66 exit(1);
67 }
68
69
70 initrd_size = 0;
71 initrd_offset = 0;
72 if (loader_params->initrd_filename) {
73 initrd_size = get_image_size(loader_params->initrd_filename);
74 if (initrd_size > 0) {
75 initrd_offset = (kernel_high + ~TARGET_PAGE_MASK)
76 & TARGET_PAGE_MASK;
77 if (initrd_offset + initrd_size > loader_params->ram_size) {
78 fprintf(stderr,
79 "qemu: memory too small for initial ram disk '%s'\n",
80 loader_params->initrd_filename);
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) {
88 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
89 loader_params->initrd_filename);
90 exit(1);
91 }
92 }
93}
94
95static void main_cpu_reset(void *opaque)
96{
97 MoxieCPU *cpu = opaque;
98
99 cpu_reset(CPU(cpu));
100}
101
102static void moxiesim_init(MachineState *machine)
103{
104 MoxieCPU *cpu = NULL;
105 ram_addr_t ram_size = machine->ram_size;
106 const char *cpu_model = machine->cpu_model;
107 const char *kernel_filename = machine->kernel_filename;
108 const char *kernel_cmdline = machine->kernel_cmdline;
109 const char *initrd_filename = machine->initrd_filename;
110 CPUMoxieState *env;
111 MemoryRegion *address_space_mem = get_system_memory();
112 MemoryRegion *ram = g_new(MemoryRegion, 1);
113 MemoryRegion *rom = g_new(MemoryRegion, 1);
114 hwaddr ram_base = 0x200000;
115 LoaderParams loader_params;
116
117
118 if (cpu_model == NULL) {
119 cpu_model = "MoxieLite-moxie-cpu";
120 }
121 cpu = cpu_moxie_init(cpu_model);
122 if (!cpu) {
123 fprintf(stderr, "Unable to find CPU definition\n");
124 exit(1);
125 }
126 env = &cpu->env;
127
128 qemu_register_reset(main_cpu_reset, cpu);
129
130
131 memory_region_init_ram(ram, NULL, "moxiesim.ram", ram_size, &error_fatal);
132 memory_region_add_subregion(address_space_mem, ram_base, ram);
133
134 memory_region_init_ram(rom, NULL, "moxie.rom", 128 * 0x1000, &error_fatal);
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
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
152static void moxiesim_machine_init(MachineClass *mc)
153{
154 mc->desc = "Moxie simulator platform";
155 mc->init = moxiesim_init;
156 mc->is_default = 1;
157}
158
159DEFINE_MACHINE("moxiesim", moxiesim_machine_init)
160