mips_mipssim.c 6.1 KB

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