2
0

mips_mipssim.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * QEMU/mipssim emulation
  3. *
  4. * Emulates a very simple machine model similar to the one used by the
  5. * proprietary MIPS emulator.
  6. *
  7. * Copyright (c) 2007 Thiemo Seufer
  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 "hw/hw.h"
  28. #include "hw/mips/mips.h"
  29. #include "hw/mips/cpudevs.h"
  30. #include "hw/char/serial.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/mips/bios.h"
  36. #include "hw/loader.h"
  37. #include "elf.h"
  38. #include "hw/sysbus.h"
  39. #include "exec/address-spaces.h"
  40. #include "qemu/error-report.h"
  41. #include "sysemu/qtest.h"
  42. static struct _loaderparams {
  43. int ram_size;
  44. const char *kernel_filename;
  45. const char *kernel_cmdline;
  46. const char *initrd_filename;
  47. } loaderparams;
  48. typedef struct ResetData {
  49. MIPSCPU *cpu;
  50. uint64_t vector;
  51. } ResetData;
  52. static int64_t load_kernel(void)
  53. {
  54. int64_t entry, kernel_high;
  55. long kernel_size;
  56. long initrd_size;
  57. ram_addr_t initrd_offset;
  58. int big_endian;
  59. #ifdef TARGET_WORDS_BIGENDIAN
  60. big_endian = 1;
  61. #else
  62. big_endian = 0;
  63. #endif
  64. kernel_size = load_elf(loaderparams.kernel_filename, cpu_mips_kseg0_to_phys,
  65. NULL, (uint64_t *)&entry, NULL,
  66. (uint64_t *)&kernel_high, big_endian,
  67. ELF_MACHINE, 1);
  68. if (kernel_size >= 0) {
  69. if ((entry & ~0x7fffffffULL) == 0x80000000)
  70. entry = (int32_t)entry;
  71. } else {
  72. fprintf(stderr, "qemu: could not load kernel '%s'\n",
  73. loaderparams.kernel_filename);
  74. exit(1);
  75. }
  76. /* load initrd */
  77. initrd_size = 0;
  78. initrd_offset = 0;
  79. if (loaderparams.initrd_filename) {
  80. initrd_size = get_image_size (loaderparams.initrd_filename);
  81. if (initrd_size > 0) {
  82. initrd_offset = (kernel_high + ~INITRD_PAGE_MASK) & INITRD_PAGE_MASK;
  83. if (initrd_offset + initrd_size > loaderparams.ram_size) {
  84. fprintf(stderr,
  85. "qemu: memory too small for initial ram disk '%s'\n",
  86. loaderparams.initrd_filename);
  87. exit(1);
  88. }
  89. initrd_size = load_image_targphys(loaderparams.initrd_filename,
  90. initrd_offset, loaderparams.ram_size - initrd_offset);
  91. }
  92. if (initrd_size == (target_ulong) -1) {
  93. fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
  94. loaderparams.initrd_filename);
  95. exit(1);
  96. }
  97. }
  98. return entry;
  99. }
  100. static void main_cpu_reset(void *opaque)
  101. {
  102. ResetData *s = (ResetData *)opaque;
  103. CPUMIPSState *env = &s->cpu->env;
  104. cpu_reset(CPU(s->cpu));
  105. env->active_tc.PC = s->vector & ~(target_ulong)1;
  106. if (s->vector & 1) {
  107. env->hflags |= MIPS_HFLAG_M16;
  108. }
  109. }
  110. static void mipsnet_init(int base, qemu_irq irq, NICInfo *nd)
  111. {
  112. DeviceState *dev;
  113. SysBusDevice *s;
  114. dev = qdev_create(NULL, "mipsnet");
  115. qdev_set_nic_properties(dev, nd);
  116. qdev_init_nofail(dev);
  117. s = SYS_BUS_DEVICE(dev);
  118. sysbus_connect_irq(s, 0, irq);
  119. memory_region_add_subregion(get_system_io(),
  120. base,
  121. sysbus_mmio_get_region(s, 0));
  122. }
  123. static void
  124. mips_mipssim_init(MachineState *machine)
  125. {
  126. ram_addr_t ram_size = machine->ram_size;
  127. const char *cpu_model = machine->cpu_model;
  128. const char *kernel_filename = machine->kernel_filename;
  129. const char *kernel_cmdline = machine->kernel_cmdline;
  130. const char *initrd_filename = machine->initrd_filename;
  131. char *filename;
  132. MemoryRegion *address_space_mem = get_system_memory();
  133. MemoryRegion *isa = g_new(MemoryRegion, 1);
  134. MemoryRegion *ram = g_new(MemoryRegion, 1);
  135. MemoryRegion *bios = g_new(MemoryRegion, 1);
  136. MIPSCPU *cpu;
  137. CPUMIPSState *env;
  138. ResetData *reset_info;
  139. int bios_size;
  140. /* Init CPUs. */
  141. if (cpu_model == NULL) {
  142. #ifdef TARGET_MIPS64
  143. cpu_model = "5Kf";
  144. #else
  145. cpu_model = "24Kf";
  146. #endif
  147. }
  148. cpu = cpu_mips_init(cpu_model);
  149. if (cpu == NULL) {
  150. fprintf(stderr, "Unable to find CPU definition\n");
  151. exit(1);
  152. }
  153. env = &cpu->env;
  154. reset_info = g_malloc0(sizeof(ResetData));
  155. reset_info->cpu = cpu;
  156. reset_info->vector = env->active_tc.PC;
  157. qemu_register_reset(main_cpu_reset, reset_info);
  158. /* Allocate RAM. */
  159. memory_region_init_ram(ram, NULL, "mips_mipssim.ram", ram_size);
  160. vmstate_register_ram_global(ram);
  161. memory_region_init_ram(bios, NULL, "mips_mipssim.bios", BIOS_SIZE);
  162. vmstate_register_ram_global(bios);
  163. memory_region_set_readonly(bios, true);
  164. memory_region_add_subregion(address_space_mem, 0, ram);
  165. /* Map the BIOS / boot exception handler. */
  166. memory_region_add_subregion(address_space_mem, 0x1fc00000LL, bios);
  167. /* Load a BIOS / boot exception handler image. */
  168. if (bios_name == NULL)
  169. bios_name = BIOS_FILENAME;
  170. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  171. if (filename) {
  172. bios_size = load_image_targphys(filename, 0x1fc00000LL, BIOS_SIZE);
  173. g_free(filename);
  174. } else {
  175. bios_size = -1;
  176. }
  177. if ((bios_size < 0 || bios_size > BIOS_SIZE) &&
  178. !kernel_filename && !qtest_enabled()) {
  179. /* Bail out if we have neither a kernel image nor boot vector code. */
  180. error_report("Could not load MIPS bios '%s', and no "
  181. "-kernel argument was specified", filename);
  182. exit(1);
  183. } else {
  184. /* We have a boot vector start address. */
  185. env->active_tc.PC = (target_long)(int32_t)0xbfc00000;
  186. }
  187. if (kernel_filename) {
  188. loaderparams.ram_size = ram_size;
  189. loaderparams.kernel_filename = kernel_filename;
  190. loaderparams.kernel_cmdline = kernel_cmdline;
  191. loaderparams.initrd_filename = initrd_filename;
  192. reset_info->vector = load_kernel();
  193. }
  194. /* Init CPU internal devices. */
  195. cpu_mips_irq_init_cpu(env);
  196. cpu_mips_clock_init(env);
  197. /* Register 64 KB of ISA IO space at 0x1fd00000. */
  198. memory_region_init_alias(isa, NULL, "isa_mmio",
  199. get_system_io(), 0, 0x00010000);
  200. memory_region_add_subregion(get_system_memory(), 0x1fd00000, isa);
  201. /* A single 16450 sits at offset 0x3f8. It is attached to
  202. MIPS CPU INT2, which is interrupt 4. */
  203. if (serial_hds[0])
  204. serial_init(0x3f8, env->irq[4], 115200, serial_hds[0],
  205. get_system_io());
  206. if (nd_table[0].used)
  207. /* MIPSnet uses the MIPS CPU INT0, which is interrupt 2. */
  208. mipsnet_init(0x4200, env->irq[2], &nd_table[0]);
  209. }
  210. static QEMUMachine mips_mipssim_machine = {
  211. .name = "mipssim",
  212. .desc = "MIPS MIPSsim platform",
  213. .init = mips_mipssim_init,
  214. };
  215. static void mips_mipssim_machine_init(void)
  216. {
  217. qemu_register_machine(&mips_mipssim_machine);
  218. }
  219. machine_init(mips_mipssim_machine_init);