ram: Split host_from_stream_offset() into two helper functions
[qemu/ar7.git] / hw / moxie / moxiesim.c
blob9191ae9603b5a879c585de6e01f99aab0a1128dc
1 /*
2 * QEMU/moxiesim emulation
4 * Emulates a very simple machine model similar to the one used by the
5 * GDB moxie simulator.
7 * Copyright (c) 2008, 2009, 2010, 2013 Anthony Green
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:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
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.
27 #include "qemu/osdep.h"
28 #include "hw/sysbus.h"
29 #include "hw/hw.h"
30 #include "hw/i386/pc.h"
31 #include "hw/isa/isa.h"
32 #include "net/net.h"
33 #include "sysemu/sysemu.h"
34 #include "hw/boards.h"
35 #include "hw/loader.h"
36 #include "hw/char/serial.h"
37 #include "exec/address-spaces.h"
38 #include "elf.h"
40 #define PHYS_MEM_BASE 0x80000000
42 typedef struct {
43 uint64_t ram_size;
44 const char *kernel_filename;
45 const char *kernel_cmdline;
46 const char *initrd_filename;
47 } LoaderParams;
49 static void load_kernel(MoxieCPU *cpu, LoaderParams *loader_params)
51 uint64_t entry, kernel_low, kernel_high;
52 long kernel_size;
53 long initrd_size;
54 ram_addr_t initrd_offset;
56 kernel_size = load_elf(loader_params->kernel_filename, NULL, NULL,
57 &entry, &kernel_low, &kernel_high, 1, EM_MOXIE, 0);
59 if (kernel_size <= 0) {
60 fprintf(stderr, "qemu: could not load kernel '%s'\n",
61 loader_params->kernel_filename);
62 exit(1);
65 /* load initrd */
66 initrd_size = 0;
67 initrd_offset = 0;
68 if (loader_params->initrd_filename) {
69 initrd_size = get_image_size(loader_params->initrd_filename);
70 if (initrd_size > 0) {
71 initrd_offset = (kernel_high + ~TARGET_PAGE_MASK)
72 & TARGET_PAGE_MASK;
73 if (initrd_offset + initrd_size > loader_params->ram_size) {
74 fprintf(stderr,
75 "qemu: memory too small for initial ram disk '%s'\n",
76 loader_params->initrd_filename);
77 exit(1);
79 initrd_size = load_image_targphys(loader_params->initrd_filename,
80 initrd_offset,
81 ram_size);
83 if (initrd_size == (target_ulong)-1) {
84 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
85 loader_params->initrd_filename);
86 exit(1);
91 static void main_cpu_reset(void *opaque)
93 MoxieCPU *cpu = opaque;
95 cpu_reset(CPU(cpu));
98 static void moxiesim_init(MachineState *machine)
100 MoxieCPU *cpu = NULL;
101 ram_addr_t ram_size = machine->ram_size;
102 const char *cpu_model = machine->cpu_model;
103 const char *kernel_filename = machine->kernel_filename;
104 const char *kernel_cmdline = machine->kernel_cmdline;
105 const char *initrd_filename = machine->initrd_filename;
106 CPUMoxieState *env;
107 MemoryRegion *address_space_mem = get_system_memory();
108 MemoryRegion *ram = g_new(MemoryRegion, 1);
109 MemoryRegion *rom = g_new(MemoryRegion, 1);
110 hwaddr ram_base = 0x200000;
111 LoaderParams loader_params;
113 /* Init CPUs. */
114 if (cpu_model == NULL) {
115 cpu_model = "MoxieLite-moxie-cpu";
117 cpu = cpu_moxie_init(cpu_model);
118 if (!cpu) {
119 fprintf(stderr, "Unable to find CPU definition\n");
120 exit(1);
122 env = &cpu->env;
124 qemu_register_reset(main_cpu_reset, cpu);
126 /* Allocate RAM. */
127 memory_region_init_ram(ram, NULL, "moxiesim.ram", ram_size, &error_fatal);
128 vmstate_register_ram_global(ram);
129 memory_region_add_subregion(address_space_mem, ram_base, ram);
131 memory_region_init_ram(rom, NULL, "moxie.rom", 128*0x1000, &error_fatal);
132 vmstate_register_ram_global(rom);
133 memory_region_add_subregion(get_system_memory(), 0x1000, rom);
135 if (kernel_filename) {
136 loader_params.ram_size = ram_size;
137 loader_params.kernel_filename = kernel_filename;
138 loader_params.kernel_cmdline = kernel_cmdline;
139 loader_params.initrd_filename = initrd_filename;
140 load_kernel(cpu, &loader_params);
143 /* A single 16450 sits at offset 0x3f8. */
144 if (serial_hds[0]) {
145 serial_mm_init(address_space_mem, 0x3f8, 0, env->irq[4],
146 8000000/16, serial_hds[0], DEVICE_LITTLE_ENDIAN);
150 static void moxiesim_machine_init(MachineClass *mc)
152 mc->desc = "Moxie simulator platform";
153 mc->init = moxiesim_init;
154 mc->is_default = 1;
157 DEFINE_MACHINE("moxiesim", moxiesim_machine_init)