qemu/hw/moxie/moxiesim.c
<<
>>
Prefs
   1/*
   2 * QEMU/moxiesim emulation
   3 *
   4 * Emulates a very simple machine model similar to the one used by the
   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 */
  27#include "qemu/osdep.h"
  28#include "qemu/error-report.h"
  29#include "qapi/error.h"
  30#include "cpu.h"
  31#include "hw/sysbus.h"
  32#include "hw/hw.h"
  33#include "net/net.h"
  34#include "sysemu/sysemu.h"
  35#include "hw/boards.h"
  36#include "hw/loader.h"
  37#include "hw/char/serial.h"
  38#include "exec/address-spaces.h"
  39#include "elf.h"
  40
  41#define PHYS_MEM_BASE 0x80000000
  42#define FIRMWARE_BASE 0x1000
  43#define FIRMWARE_SIZE (128 * 0x1000)
  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    int64_t initrd_size;
  56    long kernel_size;
  57    ram_addr_t initrd_offset;
  58
  59    kernel_size = load_elf(loader_params->kernel_filename,  NULL, NULL, NULL,
  60                           &entry, &kernel_low, &kernel_high, 1, EM_MOXIE,
  61                           0, 0);
  62
  63    if (kernel_size <= 0) {
  64        error_report("could not load kernel '%s'",
  65                     loader_params->kernel_filename);
  66        exit(1);
  67    }
  68
  69    /* load initrd */
  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                error_report("memory too small for initial ram disk '%s'",
  79                             loader_params->initrd_filename);
  80                exit(1);
  81            }
  82            initrd_size = load_image_targphys(loader_params->initrd_filename,
  83                                              initrd_offset,
  84                                              ram_size);
  85        }
  86        if (initrd_size == (target_ulong)-1) {
  87            error_report("could not load initial ram disk '%s'",
  88                         loader_params->initrd_filename);
  89            exit(1);
  90        }
  91    }
  92}
  93
  94static void main_cpu_reset(void *opaque)
  95{
  96    MoxieCPU *cpu = opaque;
  97
  98    cpu_reset(CPU(cpu));
  99}
 100
 101static void moxiesim_init(MachineState *machine)
 102{
 103    MoxieCPU *cpu = NULL;
 104    ram_addr_t ram_size = machine->ram_size;
 105    const char *kernel_filename = machine->kernel_filename;
 106    const char *kernel_cmdline = machine->kernel_cmdline;
 107    const char *initrd_filename = machine->initrd_filename;
 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    cpu = MOXIE_CPU(cpu_create(machine->cpu_type));
 117    env = &cpu->env;
 118
 119    qemu_register_reset(main_cpu_reset, cpu);
 120
 121    /* Allocate RAM. */
 122    memory_region_init_ram(ram, NULL, "moxiesim.ram", ram_size, &error_fatal);
 123    memory_region_add_subregion(address_space_mem, ram_base, ram);
 124
 125    memory_region_init_ram(rom, NULL, "moxie.rom", FIRMWARE_SIZE, &error_fatal);
 126    memory_region_add_subregion(get_system_memory(), FIRMWARE_BASE, rom);
 127
 128    if (kernel_filename) {
 129        loader_params.ram_size = ram_size;
 130        loader_params.kernel_filename = kernel_filename;
 131        loader_params.kernel_cmdline = kernel_cmdline;
 132        loader_params.initrd_filename = initrd_filename;
 133        load_kernel(cpu, &loader_params);
 134    }
 135    if (bios_name) {
 136        if (load_image_targphys(bios_name, FIRMWARE_BASE, FIRMWARE_SIZE) < 0) {
 137            error_report("Failed to load firmware '%s'", bios_name);
 138        }
 139    }
 140
 141    /* A single 16450 sits at offset 0x3f8.  */
 142    if (serial_hd(0)) {
 143        serial_mm_init(address_space_mem, 0x3f8, 0, env->irq[4],
 144                       8000000/16, serial_hd(0), DEVICE_LITTLE_ENDIAN);
 145    }
 146}
 147
 148static void moxiesim_machine_init(MachineClass *mc)
 149{
 150    mc->desc = "Moxie simulator platform";
 151    mc->init = moxiesim_init;
 152    mc->is_default = 1;
 153    mc->default_cpu_type = MOXIE_CPU_TYPE_NAME("MoxieLite");
 154}
 155
 156DEFINE_MACHINE("moxiesim", moxiesim_machine_init)
 157