2
0

spapr_vty.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #include "qemu/osdep.h"
  2. #include "qemu/error-report.h"
  3. #include "qemu/module.h"
  4. #include "qapi/error.h"
  5. #include "migration/vmstate.h"
  6. #include "chardev/char-fe.h"
  7. #include "hw/ppc/spapr.h"
  8. #include "hw/ppc/spapr_vio.h"
  9. #include "hw/qdev-properties.h"
  10. #include "hw/qdev-properties-system.h"
  11. #include "qom/object.h"
  12. #define VTERM_BUFSIZE 16
  13. struct SpaprVioVty {
  14. SpaprVioDevice sdev;
  15. CharBackend chardev;
  16. uint32_t in, out;
  17. uint8_t buf[VTERM_BUFSIZE];
  18. };
  19. #define TYPE_VIO_SPAPR_VTY_DEVICE "spapr-vty"
  20. OBJECT_DECLARE_SIMPLE_TYPE(SpaprVioVty, VIO_SPAPR_VTY_DEVICE)
  21. static int vty_can_receive(void *opaque)
  22. {
  23. SpaprVioVty *dev = VIO_SPAPR_VTY_DEVICE(opaque);
  24. return VTERM_BUFSIZE - (dev->in - dev->out);
  25. }
  26. static void vty_receive(void *opaque, const uint8_t *buf, int size)
  27. {
  28. SpaprVioVty *dev = VIO_SPAPR_VTY_DEVICE(opaque);
  29. int i;
  30. if ((dev->in == dev->out) && size) {
  31. /* toggle line to simulate edge interrupt */
  32. spapr_vio_irq_pulse(&dev->sdev);
  33. }
  34. for (i = 0; i < size; i++) {
  35. if (dev->in - dev->out >= VTERM_BUFSIZE) {
  36. static bool reported;
  37. if (!reported) {
  38. error_report("VTY input buffer exhausted - characters dropped."
  39. " (input size = %i)", size);
  40. reported = true;
  41. }
  42. break;
  43. }
  44. dev->buf[dev->in++ % VTERM_BUFSIZE] = buf[i];
  45. }
  46. }
  47. static int vty_getchars(SpaprVioDevice *sdev, uint8_t *buf, int max)
  48. {
  49. SpaprVioVty *dev = VIO_SPAPR_VTY_DEVICE(sdev);
  50. int n = 0;
  51. while ((n < max) && (dev->out != dev->in)) {
  52. /*
  53. * Long ago, PowerVM's vty implementation had a bug where it
  54. * inserted a \0 after every \r going to the guest. Existing
  55. * guests have a workaround for this which removes every \0
  56. * immediately following a \r. To avoid triggering this
  57. * workaround, we stop before inserting a \0 if the preceding
  58. * character in the output buffer is a \r.
  59. */
  60. if (n > 0 && (buf[n - 1] == '\r') &&
  61. (dev->buf[dev->out % VTERM_BUFSIZE] == '\0')) {
  62. break;
  63. }
  64. buf[n++] = dev->buf[dev->out++ % VTERM_BUFSIZE];
  65. }
  66. qemu_chr_fe_accept_input(&dev->chardev);
  67. return n;
  68. }
  69. void vty_putchars(SpaprVioDevice *sdev, uint8_t *buf, int len)
  70. {
  71. SpaprVioVty *dev = VIO_SPAPR_VTY_DEVICE(sdev);
  72. /* XXX this blocks entire thread. Rewrite to use
  73. * qemu_chr_fe_write and background I/O callbacks */
  74. qemu_chr_fe_write_all(&dev->chardev, buf, len);
  75. }
  76. static void spapr_vty_realize(SpaprVioDevice *sdev, Error **errp)
  77. {
  78. SpaprVioVty *dev = VIO_SPAPR_VTY_DEVICE(sdev);
  79. if (!qemu_chr_fe_backend_connected(&dev->chardev)) {
  80. error_setg(errp, "chardev property not set");
  81. return;
  82. }
  83. qemu_chr_fe_set_handlers(&dev->chardev, vty_can_receive,
  84. vty_receive, NULL, NULL, dev, NULL, true);
  85. }
  86. /* Forward declaration */
  87. static target_ulong h_put_term_char(PowerPCCPU *cpu, SpaprMachineState *spapr,
  88. target_ulong opcode, target_ulong *args)
  89. {
  90. target_ulong reg = args[0];
  91. target_ulong len = args[1];
  92. target_ulong char0_7 = args[2];
  93. target_ulong char8_15 = args[3];
  94. SpaprVioDevice *sdev;
  95. uint8_t buf[16];
  96. sdev = vty_lookup(spapr, reg);
  97. if (!sdev) {
  98. return H_PARAMETER;
  99. }
  100. if (len > 16) {
  101. return H_PARAMETER;
  102. }
  103. *((uint64_t *)buf) = cpu_to_be64(char0_7);
  104. *((uint64_t *)buf + 1) = cpu_to_be64(char8_15);
  105. vty_putchars(sdev, buf, len);
  106. return H_SUCCESS;
  107. }
  108. static target_ulong h_get_term_char(PowerPCCPU *cpu, SpaprMachineState *spapr,
  109. target_ulong opcode, target_ulong *args)
  110. {
  111. target_ulong reg = args[0];
  112. target_ulong *len = args + 0;
  113. target_ulong *char0_7 = args + 1;
  114. target_ulong *char8_15 = args + 2;
  115. SpaprVioDevice *sdev;
  116. uint8_t buf[16];
  117. sdev = vty_lookup(spapr, reg);
  118. if (!sdev) {
  119. return H_PARAMETER;
  120. }
  121. *len = vty_getchars(sdev, buf, sizeof(buf));
  122. if (*len < 16) {
  123. memset(buf + *len, 0, 16 - *len);
  124. }
  125. *char0_7 = be64_to_cpu(*((uint64_t *)buf));
  126. *char8_15 = be64_to_cpu(*((uint64_t *)buf + 1));
  127. return H_SUCCESS;
  128. }
  129. void spapr_vty_create(SpaprVioBus *bus, Chardev *chardev)
  130. {
  131. DeviceState *dev;
  132. dev = qdev_new("spapr-vty");
  133. qdev_prop_set_chr(dev, "chardev", chardev);
  134. qdev_realize_and_unref(dev, &bus->bus, &error_fatal);
  135. }
  136. static const Property spapr_vty_properties[] = {
  137. DEFINE_SPAPR_PROPERTIES(SpaprVioVty, sdev),
  138. DEFINE_PROP_CHR("chardev", SpaprVioVty, chardev),
  139. };
  140. static const VMStateDescription vmstate_spapr_vty = {
  141. .name = "spapr_vty",
  142. .version_id = 1,
  143. .minimum_version_id = 1,
  144. .fields = (const VMStateField[]) {
  145. VMSTATE_SPAPR_VIO(sdev, SpaprVioVty),
  146. VMSTATE_UINT32(in, SpaprVioVty),
  147. VMSTATE_UINT32(out, SpaprVioVty),
  148. VMSTATE_BUFFER(buf, SpaprVioVty),
  149. VMSTATE_END_OF_LIST()
  150. },
  151. };
  152. static void spapr_vty_class_init(ObjectClass *klass, void *data)
  153. {
  154. DeviceClass *dc = DEVICE_CLASS(klass);
  155. SpaprVioDeviceClass *k = VIO_SPAPR_DEVICE_CLASS(klass);
  156. k->realize = spapr_vty_realize;
  157. k->dt_name = "vty";
  158. k->dt_type = "serial";
  159. k->dt_compatible = "hvterm1";
  160. set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
  161. device_class_set_props(dc, spapr_vty_properties);
  162. dc->vmsd = &vmstate_spapr_vty;
  163. }
  164. static const TypeInfo spapr_vty_info = {
  165. .name = TYPE_VIO_SPAPR_VTY_DEVICE,
  166. .parent = TYPE_VIO_SPAPR_DEVICE,
  167. .instance_size = sizeof(SpaprVioVty),
  168. .class_init = spapr_vty_class_init,
  169. };
  170. SpaprVioDevice *spapr_vty_get_default(SpaprVioBus *bus)
  171. {
  172. SpaprVioDevice *sdev, *selected;
  173. BusChild *kid;
  174. /*
  175. * To avoid the console bouncing around we want one VTY to be
  176. * the "default". We haven't really got anything to go on, so
  177. * arbitrarily choose the one with the lowest reg value.
  178. */
  179. selected = NULL;
  180. QTAILQ_FOREACH(kid, &bus->bus.children, sibling) {
  181. DeviceState *iter = kid->child;
  182. /* Only look at VTY devices */
  183. if (!object_dynamic_cast(OBJECT(iter), TYPE_VIO_SPAPR_VTY_DEVICE)) {
  184. continue;
  185. }
  186. sdev = VIO_SPAPR_DEVICE(iter);
  187. /* First VTY we've found, so it is selected for now */
  188. if (!selected) {
  189. selected = sdev;
  190. continue;
  191. }
  192. /* Choose VTY with lowest reg value */
  193. if (sdev->reg < selected->reg) {
  194. selected = sdev;
  195. }
  196. }
  197. return selected;
  198. }
  199. SpaprVioDevice *vty_lookup(SpaprMachineState *spapr, target_ulong reg)
  200. {
  201. SpaprVioDevice *sdev;
  202. sdev = spapr_vio_find_by_reg(spapr->vio_bus, reg);
  203. if (!sdev && reg == 0) {
  204. /* Hack for kernel early debug, which always specifies reg==0.
  205. * We search all VIO devices, and grab the vty with the lowest
  206. * reg. This attempts to mimic existing PowerVM behaviour
  207. * (early debug does work there, despite having no vty with
  208. * reg==0. */
  209. return spapr_vty_get_default(spapr->vio_bus);
  210. }
  211. if (!object_dynamic_cast(OBJECT(sdev), TYPE_VIO_SPAPR_VTY_DEVICE)) {
  212. return NULL;
  213. }
  214. return sdev;
  215. }
  216. static void spapr_vty_register_types(void)
  217. {
  218. spapr_register_hypercall(H_PUT_TERM_CHAR, h_put_term_char);
  219. spapr_register_hypercall(H_GET_TERM_CHAR, h_get_term_char);
  220. type_register_static(&spapr_vty_info);
  221. }
  222. type_init(spapr_vty_register_types)