riscv_htif.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * QEMU RISC-V Host Target Interface (HTIF) Emulation
  3. *
  4. * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu
  5. * Copyright (c) 2017-2018 SiFive, Inc.
  6. *
  7. * This provides HTIF device emulation for QEMU. At the moment this allows
  8. * for identical copies of bbl/linux to run on both spike and QEMU.
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms and conditions of the GNU General Public License,
  12. * version 2 or later, as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  17. * more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along with
  20. * this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include "qemu/osdep.h"
  23. #include "qapi/error.h"
  24. #include "qemu/log.h"
  25. #include "hw/sysbus.h"
  26. #include "hw/char/serial.h"
  27. #include "chardev/char.h"
  28. #include "chardev/char-fe.h"
  29. #include "hw/riscv/riscv_htif.h"
  30. #include "qemu/timer.h"
  31. #include "qemu/error-report.h"
  32. #define RISCV_DEBUG_HTIF 0
  33. #define HTIF_DEBUG(fmt, ...) \
  34. do { \
  35. if (RISCV_DEBUG_HTIF) { \
  36. qemu_log_mask(LOG_TRACE, "%s: " fmt "\n", __func__, ##__VA_ARGS__);\
  37. } \
  38. } while (0)
  39. static uint64_t fromhost_addr, tohost_addr;
  40. static int address_symbol_set;
  41. void htif_symbol_callback(const char *st_name, int st_info, uint64_t st_value,
  42. uint64_t st_size)
  43. {
  44. if (strcmp("fromhost", st_name) == 0) {
  45. address_symbol_set |= 1;
  46. fromhost_addr = st_value;
  47. if (st_size != 8) {
  48. error_report("HTIF fromhost must be 8 bytes");
  49. exit(1);
  50. }
  51. } else if (strcmp("tohost", st_name) == 0) {
  52. address_symbol_set |= 2;
  53. tohost_addr = st_value;
  54. if (st_size != 8) {
  55. error_report("HTIF tohost must be 8 bytes");
  56. exit(1);
  57. }
  58. }
  59. }
  60. /*
  61. * Called by the char dev to see if HTIF is ready to accept input.
  62. */
  63. static int htif_can_recv(void *opaque)
  64. {
  65. return 1;
  66. }
  67. /*
  68. * Called by the char dev to supply input to HTIF console.
  69. * We assume that we will receive one character at a time.
  70. */
  71. static void htif_recv(void *opaque, const uint8_t *buf, int size)
  72. {
  73. HTIFState *htifstate = opaque;
  74. if (size != 1) {
  75. return;
  76. }
  77. /* TODO - we need to check whether mfromhost is zero which indicates
  78. the device is ready to receive. The current implementation
  79. will drop characters */
  80. uint64_t val_written = htifstate->pending_read;
  81. uint64_t resp = 0x100 | *buf;
  82. htifstate->env->mfromhost = (val_written >> 48 << 48) | (resp << 16 >> 16);
  83. }
  84. /*
  85. * Called by the char dev to supply special events to the HTIF console.
  86. * Not used for HTIF.
  87. */
  88. static void htif_event(void *opaque, int event)
  89. {
  90. }
  91. static int htif_be_change(void *opaque)
  92. {
  93. HTIFState *s = opaque;
  94. qemu_chr_fe_set_handlers(&s->chr, htif_can_recv, htif_recv, htif_event,
  95. htif_be_change, s, NULL, true);
  96. return 0;
  97. }
  98. static void htif_handle_tohost_write(HTIFState *htifstate, uint64_t val_written)
  99. {
  100. uint8_t device = val_written >> 56;
  101. uint8_t cmd = val_written >> 48;
  102. uint64_t payload = val_written & 0xFFFFFFFFFFFFULL;
  103. int resp = 0;
  104. HTIF_DEBUG("mtohost write: device: %d cmd: %d what: %02" PRIx64
  105. " -payload: %016" PRIx64 "\n", device, cmd, payload & 0xFF, payload);
  106. /*
  107. * Currently, there is a fixed mapping of devices:
  108. * 0: riscv-tests Pass/Fail Reporting Only (no syscall proxy)
  109. * 1: Console
  110. */
  111. if (unlikely(device == 0x0)) {
  112. /* frontend syscall handler, shutdown and exit code support */
  113. if (cmd == 0x0) {
  114. if (payload & 0x1) {
  115. /* exit code */
  116. int exit_code = payload >> 1;
  117. exit(exit_code);
  118. } else {
  119. qemu_log_mask(LOG_UNIMP, "pk syscall proxy not supported\n");
  120. }
  121. } else {
  122. qemu_log("HTIF device %d: unknown command\n", device);
  123. }
  124. } else if (likely(device == 0x1)) {
  125. /* HTIF Console */
  126. if (cmd == 0x0) {
  127. /* this should be a queue, but not yet implemented as such */
  128. htifstate->pending_read = val_written;
  129. htifstate->env->mtohost = 0; /* clear to indicate we read */
  130. return;
  131. } else if (cmd == 0x1) {
  132. qemu_chr_fe_write(&htifstate->chr, (uint8_t *)&payload, 1);
  133. resp = 0x100 | (uint8_t)payload;
  134. } else {
  135. qemu_log("HTIF device %d: unknown command\n", device);
  136. }
  137. } else {
  138. qemu_log("HTIF unknown device or command\n");
  139. HTIF_DEBUG("device: %d cmd: %d what: %02" PRIx64
  140. " payload: %016" PRIx64, device, cmd, payload & 0xFF, payload);
  141. }
  142. /*
  143. * - latest bbl does not set fromhost to 0 if there is a value in tohost
  144. * - with this code enabled, qemu hangs waiting for fromhost to go to 0
  145. * - with this code disabled, qemu works with bbl priv v1.9.1 and v1.10
  146. * - HTIF needs protocol documentation and a more complete state machine
  147. while (!htifstate->fromhost_inprogress &&
  148. htifstate->env->mfromhost != 0x0) {
  149. }
  150. */
  151. htifstate->env->mfromhost = (val_written >> 48 << 48) | (resp << 16 >> 16);
  152. htifstate->env->mtohost = 0; /* clear to indicate we read */
  153. }
  154. #define TOHOST_OFFSET1 (htifstate->tohost_offset)
  155. #define TOHOST_OFFSET2 (htifstate->tohost_offset + 4)
  156. #define FROMHOST_OFFSET1 (htifstate->fromhost_offset)
  157. #define FROMHOST_OFFSET2 (htifstate->fromhost_offset + 4)
  158. /* CPU wants to read an HTIF register */
  159. static uint64_t htif_mm_read(void *opaque, hwaddr addr, unsigned size)
  160. {
  161. HTIFState *htifstate = opaque;
  162. if (addr == TOHOST_OFFSET1) {
  163. return htifstate->env->mtohost & 0xFFFFFFFF;
  164. } else if (addr == TOHOST_OFFSET2) {
  165. return (htifstate->env->mtohost >> 32) & 0xFFFFFFFF;
  166. } else if (addr == FROMHOST_OFFSET1) {
  167. return htifstate->env->mfromhost & 0xFFFFFFFF;
  168. } else if (addr == FROMHOST_OFFSET2) {
  169. return (htifstate->env->mfromhost >> 32) & 0xFFFFFFFF;
  170. } else {
  171. qemu_log("Invalid htif read: address %016" PRIx64 "\n",
  172. (uint64_t)addr);
  173. return 0;
  174. }
  175. }
  176. /* CPU wrote to an HTIF register */
  177. static void htif_mm_write(void *opaque, hwaddr addr,
  178. uint64_t value, unsigned size)
  179. {
  180. HTIFState *htifstate = opaque;
  181. if (addr == TOHOST_OFFSET1) {
  182. if (htifstate->env->mtohost == 0x0) {
  183. htifstate->allow_tohost = 1;
  184. htifstate->env->mtohost = value & 0xFFFFFFFF;
  185. } else {
  186. htifstate->allow_tohost = 0;
  187. }
  188. } else if (addr == TOHOST_OFFSET2) {
  189. if (htifstate->allow_tohost) {
  190. htifstate->env->mtohost |= value << 32;
  191. htif_handle_tohost_write(htifstate, htifstate->env->mtohost);
  192. }
  193. } else if (addr == FROMHOST_OFFSET1) {
  194. htifstate->fromhost_inprogress = 1;
  195. htifstate->env->mfromhost = value & 0xFFFFFFFF;
  196. } else if (addr == FROMHOST_OFFSET2) {
  197. htifstate->env->mfromhost |= value << 32;
  198. htifstate->fromhost_inprogress = 0;
  199. } else {
  200. qemu_log("Invalid htif write: address %016" PRIx64 "\n",
  201. (uint64_t)addr);
  202. }
  203. }
  204. static const MemoryRegionOps htif_mm_ops = {
  205. .read = htif_mm_read,
  206. .write = htif_mm_write,
  207. };
  208. HTIFState *htif_mm_init(MemoryRegion *address_space, MemoryRegion *main_mem,
  209. CPURISCVState *env, Chardev *chr)
  210. {
  211. uint64_t base = MIN(tohost_addr, fromhost_addr);
  212. uint64_t size = MAX(tohost_addr + 8, fromhost_addr + 8) - base;
  213. uint64_t tohost_offset = tohost_addr - base;
  214. uint64_t fromhost_offset = fromhost_addr - base;
  215. HTIFState *s = g_malloc0(sizeof(HTIFState));
  216. s->address_space = address_space;
  217. s->main_mem = main_mem;
  218. s->main_mem_ram_ptr = memory_region_get_ram_ptr(main_mem);
  219. s->env = env;
  220. s->tohost_offset = tohost_offset;
  221. s->fromhost_offset = fromhost_offset;
  222. s->pending_read = 0;
  223. s->allow_tohost = 0;
  224. s->fromhost_inprogress = 0;
  225. qemu_chr_fe_init(&s->chr, chr, &error_abort);
  226. qemu_chr_fe_set_handlers(&s->chr, htif_can_recv, htif_recv, htif_event,
  227. htif_be_change, s, NULL, true);
  228. if (address_symbol_set == 3) {
  229. memory_region_init_io(&s->mmio, NULL, &htif_mm_ops, s,
  230. TYPE_HTIF_UART, size);
  231. memory_region_add_subregion_overlap(address_space, base,
  232. &s->mmio, 1);
  233. }
  234. return s;
  235. }