rx-gdbsim.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * RX QEMU GDB simulator
  3. *
  4. * Copyright (c) 2019 Yoshinori Sato
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2 or later, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "qemu/osdep.h"
  19. #include "qemu/cutils.h"
  20. #include "qemu/error-report.h"
  21. #include "qapi/error.h"
  22. #include "hw/loader.h"
  23. #include "hw/rx/rx62n.h"
  24. #include "sysemu/qtest.h"
  25. #include "sysemu/device_tree.h"
  26. #include "hw/boards.h"
  27. #include "qom/object.h"
  28. /* Same address of GDB integrated simulator */
  29. #define SDRAM_BASE EXT_CS_BASE
  30. struct RxGdbSimMachineClass {
  31. /*< private >*/
  32. MachineClass parent_class;
  33. /*< public >*/
  34. const char *mcu_name;
  35. uint32_t xtal_freq_hz;
  36. };
  37. typedef struct RxGdbSimMachineClass RxGdbSimMachineClass;
  38. struct RxGdbSimMachineState {
  39. /*< private >*/
  40. MachineState parent_obj;
  41. /*< public >*/
  42. RX62NState mcu;
  43. };
  44. typedef struct RxGdbSimMachineState RxGdbSimMachineState;
  45. #define TYPE_RX_GDBSIM_MACHINE MACHINE_TYPE_NAME("rx62n-common")
  46. DECLARE_OBJ_CHECKERS(RxGdbSimMachineState, RxGdbSimMachineClass,
  47. RX_GDBSIM_MACHINE, TYPE_RX_GDBSIM_MACHINE)
  48. static void rx_load_image(RXCPU *cpu, const char *filename,
  49. uint32_t start, uint32_t size)
  50. {
  51. static uint32_t extable[32];
  52. long kernel_size;
  53. int i;
  54. kernel_size = load_image_targphys(filename, start, size);
  55. if (kernel_size < 0) {
  56. fprintf(stderr, "qemu: could not load kernel '%s'\n", filename);
  57. exit(1);
  58. }
  59. cpu->env.pc = start;
  60. /* setup exception trap trampoline */
  61. /* linux kernel only works little-endian mode */
  62. for (i = 0; i < ARRAY_SIZE(extable); i++) {
  63. extable[i] = cpu_to_le32(0x10 + i * 4);
  64. }
  65. rom_add_blob_fixed("extable", extable, sizeof(extable), VECTOR_TABLE_BASE);
  66. }
  67. static void rx_gdbsim_init(MachineState *machine)
  68. {
  69. MachineClass *mc = MACHINE_GET_CLASS(machine);
  70. RxGdbSimMachineState *s = RX_GDBSIM_MACHINE(machine);
  71. RxGdbSimMachineClass *rxc = RX_GDBSIM_MACHINE_GET_CLASS(machine);
  72. MemoryRegion *sysmem = get_system_memory();
  73. const char *kernel_filename = machine->kernel_filename;
  74. const char *dtb_filename = machine->dtb;
  75. if (machine->ram_size < mc->default_ram_size) {
  76. char *sz = size_to_str(mc->default_ram_size);
  77. error_report("Invalid RAM size, should be more than %s", sz);
  78. g_free(sz);
  79. exit(1);
  80. }
  81. /* Allocate memory space */
  82. memory_region_add_subregion(sysmem, SDRAM_BASE, machine->ram);
  83. /* Initialize MCU */
  84. object_initialize_child(OBJECT(machine), "mcu", &s->mcu, rxc->mcu_name);
  85. object_property_set_link(OBJECT(&s->mcu), "main-bus", OBJECT(sysmem),
  86. &error_abort);
  87. object_property_set_uint(OBJECT(&s->mcu), "xtal-frequency-hz",
  88. rxc->xtal_freq_hz, &error_abort);
  89. object_property_set_bool(OBJECT(&s->mcu), "load-kernel",
  90. kernel_filename != NULL, &error_abort);
  91. if (!kernel_filename) {
  92. if (machine->firmware) {
  93. rom_add_file_fixed(machine->firmware, RX62N_CFLASH_BASE, 0);
  94. } else if (!qtest_enabled()) {
  95. error_report("No bios or kernel specified");
  96. exit(1);
  97. }
  98. }
  99. qdev_realize(DEVICE(&s->mcu), NULL, &error_abort);
  100. /* Load kernel and dtb */
  101. if (kernel_filename) {
  102. ram_addr_t kernel_offset;
  103. /*
  104. * The kernel image is loaded into
  105. * the latter half of the SDRAM space.
  106. */
  107. kernel_offset = machine->ram_size / 2;
  108. rx_load_image(RX_CPU(first_cpu), kernel_filename,
  109. SDRAM_BASE + kernel_offset, kernel_offset);
  110. if (dtb_filename) {
  111. ram_addr_t dtb_offset;
  112. int dtb_size;
  113. g_autofree void *dtb = load_device_tree(dtb_filename, &dtb_size);
  114. if (dtb == NULL) {
  115. error_report("Couldn't open dtb file %s", dtb_filename);
  116. exit(1);
  117. }
  118. if (machine->kernel_cmdline &&
  119. qemu_fdt_setprop_string(dtb, "/chosen", "bootargs",
  120. machine->kernel_cmdline) < 0) {
  121. error_report("Couldn't set /chosen/bootargs");
  122. exit(1);
  123. }
  124. /* DTB is located at the end of SDRAM space. */
  125. dtb_offset = machine->ram_size - dtb_size;
  126. rom_add_blob_fixed("dtb", dtb, dtb_size,
  127. SDRAM_BASE + dtb_offset);
  128. /* Set dtb address to R1 */
  129. RX_CPU(first_cpu)->env.regs[1] = SDRAM_BASE + dtb_offset;
  130. }
  131. }
  132. }
  133. static void rx_gdbsim_class_init(ObjectClass *oc, void *data)
  134. {
  135. MachineClass *mc = MACHINE_CLASS(oc);
  136. mc->init = rx_gdbsim_init;
  137. mc->default_cpu_type = TYPE_RX62N_CPU;
  138. mc->default_ram_size = 16 * MiB;
  139. mc->default_ram_id = "ext-sdram";
  140. }
  141. static void rx62n7_class_init(ObjectClass *oc, void *data)
  142. {
  143. RxGdbSimMachineClass *rxc = RX_GDBSIM_MACHINE_CLASS(oc);
  144. MachineClass *mc = MACHINE_CLASS(oc);
  145. rxc->mcu_name = TYPE_R5F562N7_MCU;
  146. rxc->xtal_freq_hz = 12 * 1000 * 1000;
  147. mc->desc = "gdb simulator (R5F562N7 MCU and external RAM)";
  148. };
  149. static void rx62n8_class_init(ObjectClass *oc, void *data)
  150. {
  151. RxGdbSimMachineClass *rxc = RX_GDBSIM_MACHINE_CLASS(oc);
  152. MachineClass *mc = MACHINE_CLASS(oc);
  153. rxc->mcu_name = TYPE_R5F562N8_MCU;
  154. rxc->xtal_freq_hz = 12 * 1000 * 1000;
  155. mc->desc = "gdb simulator (R5F562N8 MCU and external RAM)";
  156. };
  157. static const TypeInfo rx_gdbsim_types[] = {
  158. {
  159. .name = MACHINE_TYPE_NAME("gdbsim-r5f562n7"),
  160. .parent = TYPE_RX_GDBSIM_MACHINE,
  161. .class_init = rx62n7_class_init,
  162. }, {
  163. .name = MACHINE_TYPE_NAME("gdbsim-r5f562n8"),
  164. .parent = TYPE_RX_GDBSIM_MACHINE,
  165. .class_init = rx62n8_class_init,
  166. }, {
  167. .name = TYPE_RX_GDBSIM_MACHINE,
  168. .parent = TYPE_MACHINE,
  169. .instance_size = sizeof(RxGdbSimMachineState),
  170. .class_size = sizeof(RxGdbSimMachineClass),
  171. .class_init = rx_gdbsim_class_init,
  172. .abstract = true,
  173. }
  174. };
  175. DEFINE_TYPES(rx_gdbsim_types)