mips_mipssim.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * QEMU/mipssim emulation
  3. *
  4. * Emulates a very simple machine model similiar to the one use 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.h"
  28. #include "mips.h"
  29. #include "mips_cpudevs.h"
  30. #include "pc.h"
  31. #include "isa.h"
  32. #include "net.h"
  33. #include "sysemu.h"
  34. #include "boards.h"
  35. #include "mips-bios.h"
  36. #include "loader.h"
  37. #include "elf.h"
  38. static struct _loaderparams {
  39. int ram_size;
  40. const char *kernel_filename;
  41. const char *kernel_cmdline;
  42. const char *initrd_filename;
  43. } loaderparams;
  44. typedef struct ResetData {
  45. CPUState *env;
  46. uint64_t vector;
  47. } ResetData;
  48. static int64_t load_kernel(void)
  49. {
  50. int64_t entry, kernel_high;
  51. long kernel_size;
  52. long initrd_size;
  53. ram_addr_t initrd_offset;
  54. int big_endian;
  55. #ifdef TARGET_WORDS_BIGENDIAN
  56. big_endian = 1;
  57. #else
  58. big_endian = 0;
  59. #endif
  60. kernel_size = load_elf(loaderparams.kernel_filename, cpu_mips_kseg0_to_phys,
  61. NULL, (uint64_t *)&entry, NULL,
  62. (uint64_t *)&kernel_high, big_endian,
  63. ELF_MACHINE, 1);
  64. if (kernel_size >= 0) {
  65. if ((entry & ~0x7fffffffULL) == 0x80000000)
  66. entry = (int32_t)entry;
  67. } else {
  68. fprintf(stderr, "qemu: could not load kernel '%s'\n",
  69. loaderparams.kernel_filename);
  70. exit(1);
  71. }
  72. /* load initrd */
  73. initrd_size = 0;
  74. initrd_offset = 0;
  75. if (loaderparams.initrd_filename) {
  76. initrd_size = get_image_size (loaderparams.initrd_filename);
  77. if (initrd_size > 0) {
  78. initrd_offset = (kernel_high + ~TARGET_PAGE_MASK) & TARGET_PAGE_MASK;
  79. if (initrd_offset + initrd_size > loaderparams.ram_size) {
  80. fprintf(stderr,
  81. "qemu: memory too small for initial ram disk '%s'\n",
  82. loaderparams.initrd_filename);
  83. exit(1);
  84. }
  85. initrd_size = load_image_targphys(loaderparams.initrd_filename,
  86. initrd_offset, loaderparams.ram_size - initrd_offset);
  87. }
  88. if (initrd_size == (target_ulong) -1) {
  89. fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
  90. loaderparams.initrd_filename);
  91. exit(1);
  92. }
  93. }
  94. return entry;
  95. }
  96. static void main_cpu_reset(void *opaque)
  97. {
  98. ResetData *s = (ResetData *)opaque;
  99. CPUState *env = s->env;
  100. cpu_reset(env);
  101. env->active_tc.PC = s->vector & ~(target_ulong)1;
  102. if (s->vector & 1) {
  103. env->hflags |= MIPS_HFLAG_M16;
  104. }
  105. }
  106. static void
  107. mips_mipssim_init (ram_addr_t ram_size,
  108. const char *boot_device,
  109. const char *kernel_filename, const char *kernel_cmdline,
  110. const char *initrd_filename, const char *cpu_model)
  111. {
  112. char *filename;
  113. ram_addr_t ram_offset;
  114. ram_addr_t bios_offset;
  115. CPUState *env;
  116. ResetData *reset_info;
  117. int bios_size;
  118. /* Init CPUs. */
  119. if (cpu_model == NULL) {
  120. #ifdef TARGET_MIPS64
  121. cpu_model = "5Kf";
  122. #else
  123. cpu_model = "24Kf";
  124. #endif
  125. }
  126. env = cpu_init(cpu_model);
  127. if (!env) {
  128. fprintf(stderr, "Unable to find CPU definition\n");
  129. exit(1);
  130. }
  131. reset_info = qemu_mallocz(sizeof(ResetData));
  132. reset_info->env = env;
  133. reset_info->vector = env->active_tc.PC;
  134. qemu_register_reset(main_cpu_reset, reset_info);
  135. /* Allocate RAM. */
  136. ram_offset = qemu_ram_alloc(NULL, "mips_mipssim.ram", ram_size);
  137. bios_offset = qemu_ram_alloc(NULL, "mips_mipssim.bios", BIOS_SIZE);
  138. cpu_register_physical_memory(0, ram_size, ram_offset | IO_MEM_RAM);
  139. /* Map the BIOS / boot exception handler. */
  140. cpu_register_physical_memory(0x1fc00000LL,
  141. BIOS_SIZE, bios_offset | IO_MEM_ROM);
  142. /* Load a BIOS / boot exception handler image. */
  143. if (bios_name == NULL)
  144. bios_name = BIOS_FILENAME;
  145. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  146. if (filename) {
  147. bios_size = load_image_targphys(filename, 0x1fc00000LL, BIOS_SIZE);
  148. qemu_free(filename);
  149. } else {
  150. bios_size = -1;
  151. }
  152. if ((bios_size < 0 || bios_size > BIOS_SIZE) && !kernel_filename) {
  153. /* Bail out if we have neither a kernel image nor boot vector code. */
  154. fprintf(stderr,
  155. "qemu: Could not load MIPS bios '%s', and no -kernel argument was specified\n",
  156. filename);
  157. exit(1);
  158. } else {
  159. /* We have a boot vector start address. */
  160. env->active_tc.PC = (target_long)(int32_t)0xbfc00000;
  161. }
  162. if (kernel_filename) {
  163. loaderparams.ram_size = ram_size;
  164. loaderparams.kernel_filename = kernel_filename;
  165. loaderparams.kernel_cmdline = kernel_cmdline;
  166. loaderparams.initrd_filename = initrd_filename;
  167. reset_info->vector = load_kernel();
  168. }
  169. /* Init CPU internal devices. */
  170. cpu_mips_irq_init_cpu(env);
  171. cpu_mips_clock_init(env);
  172. /* Register 64 KB of ISA IO space at 0x1fd00000. */
  173. isa_mmio_init(0x1fd00000, 0x00010000);
  174. /* A single 16450 sits at offset 0x3f8. It is attached to
  175. MIPS CPU INT2, which is interrupt 4. */
  176. if (serial_hds[0])
  177. serial_init(0x3f8, env->irq[4], 115200, serial_hds[0]);
  178. if (nd_table[0].vlan)
  179. /* MIPSnet uses the MIPS CPU INT0, which is interrupt 2. */
  180. mipsnet_init(0x4200, env->irq[2], &nd_table[0]);
  181. }
  182. static QEMUMachine mips_mipssim_machine = {
  183. .name = "mipssim",
  184. .desc = "MIPS MIPSsim platform",
  185. .init = mips_mipssim_init,
  186. };
  187. static void mips_mipssim_machine_init(void)
  188. {
  189. qemu_register_machine(&mips_mipssim_machine);
  190. }
  191. machine_init(mips_mipssim_machine_init);