virtio-serial-bus.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  1. /*
  2. * A bus for connecting virtio serial and console ports
  3. *
  4. * Copyright (C) 2009, 2010 Red Hat, Inc.
  5. *
  6. * Author(s):
  7. * Amit Shah <amit.shah@redhat.com>
  8. *
  9. * Some earlier parts are:
  10. * Copyright IBM, Corp. 2008
  11. * authored by
  12. * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
  13. *
  14. * This work is licensed under the terms of the GNU GPL, version 2. See
  15. * the COPYING file in the top-level directory.
  16. *
  17. * Contributions after 2012-01-13 are licensed under the terms of the
  18. * GNU GPL, version 2 or (at your option) any later version.
  19. */
  20. #include "qemu/osdep.h"
  21. #include "qapi/error.h"
  22. #include "qemu/iov.h"
  23. #include "qemu/main-loop.h"
  24. #include "qemu/module.h"
  25. #include "migration/qemu-file-types.h"
  26. #include "monitor/monitor.h"
  27. #include "qemu/error-report.h"
  28. #include "qemu/queue.h"
  29. #include "hw/qdev-properties.h"
  30. #include "hw/sysbus.h"
  31. #include "trace.h"
  32. #include "hw/virtio/virtio-serial.h"
  33. #include "hw/virtio/virtio-access.h"
  34. static struct VirtIOSerialDevices {
  35. QLIST_HEAD(, VirtIOSerial) devices;
  36. } vserdevices;
  37. static VirtIOSerialPort *find_port_by_id(VirtIOSerial *vser, uint32_t id)
  38. {
  39. VirtIOSerialPort *port;
  40. if (id == VIRTIO_CONSOLE_BAD_ID) {
  41. return NULL;
  42. }
  43. QTAILQ_FOREACH(port, &vser->ports, next) {
  44. if (port->id == id)
  45. return port;
  46. }
  47. return NULL;
  48. }
  49. static VirtIOSerialPort *find_port_by_vq(VirtIOSerial *vser, VirtQueue *vq)
  50. {
  51. VirtIOSerialPort *port;
  52. QTAILQ_FOREACH(port, &vser->ports, next) {
  53. if (port->ivq == vq || port->ovq == vq)
  54. return port;
  55. }
  56. return NULL;
  57. }
  58. static VirtIOSerialPort *find_port_by_name(char *name)
  59. {
  60. VirtIOSerial *vser;
  61. QLIST_FOREACH(vser, &vserdevices.devices, next) {
  62. VirtIOSerialPort *port;
  63. QTAILQ_FOREACH(port, &vser->ports, next) {
  64. if (port->name && !strcmp(port->name, name)) {
  65. return port;
  66. }
  67. }
  68. }
  69. return NULL;
  70. }
  71. static VirtIOSerialPort *find_first_connected_console(VirtIOSerial *vser)
  72. {
  73. VirtIOSerialPort *port;
  74. QTAILQ_FOREACH(port, &vser->ports, next) {
  75. VirtIOSerialPortClass const *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
  76. if (vsc->is_console && port->host_connected) {
  77. return port;
  78. }
  79. }
  80. return NULL;
  81. }
  82. static bool use_multiport(VirtIOSerial *vser)
  83. {
  84. VirtIODevice *vdev = VIRTIO_DEVICE(vser);
  85. return virtio_vdev_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT);
  86. }
  87. static size_t write_to_port(VirtIOSerialPort *port,
  88. const uint8_t *buf, size_t size)
  89. {
  90. VirtQueueElement *elem;
  91. VirtQueue *vq;
  92. size_t offset;
  93. vq = port->ivq;
  94. if (!virtio_queue_ready(vq)) {
  95. return 0;
  96. }
  97. offset = 0;
  98. while (offset < size) {
  99. size_t len;
  100. elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
  101. if (!elem) {
  102. break;
  103. }
  104. len = iov_from_buf(elem->in_sg, elem->in_num, 0,
  105. buf + offset, size - offset);
  106. offset += len;
  107. virtqueue_push(vq, elem, len);
  108. g_free(elem);
  109. }
  110. virtio_notify(VIRTIO_DEVICE(port->vser), vq);
  111. return offset;
  112. }
  113. static void discard_vq_data(VirtQueue *vq, VirtIODevice *vdev)
  114. {
  115. VirtQueueElement *elem;
  116. if (!virtio_queue_ready(vq)) {
  117. return;
  118. }
  119. for (;;) {
  120. elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
  121. if (!elem) {
  122. break;
  123. }
  124. virtqueue_push(vq, elem, 0);
  125. g_free(elem);
  126. }
  127. virtio_notify(vdev, vq);
  128. }
  129. static void discard_throttle_data(VirtIOSerialPort *port)
  130. {
  131. if (port->elem) {
  132. virtqueue_detach_element(port->ovq, port->elem, 0);
  133. g_free(port->elem);
  134. port->elem = NULL;
  135. }
  136. }
  137. static void do_flush_queued_data(VirtIOSerialPort *port, VirtQueue *vq,
  138. VirtIODevice *vdev)
  139. {
  140. VirtIOSerialPortClass *vsc;
  141. assert(port);
  142. assert(virtio_queue_ready(vq));
  143. vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
  144. while (!port->throttled) {
  145. unsigned int i;
  146. /* Pop an elem only if we haven't left off a previous one mid-way */
  147. if (!port->elem) {
  148. port->elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
  149. if (!port->elem) {
  150. break;
  151. }
  152. port->iov_idx = 0;
  153. port->iov_offset = 0;
  154. }
  155. for (i = port->iov_idx; i < port->elem->out_num; i++) {
  156. size_t buf_size;
  157. ssize_t ret;
  158. buf_size = port->elem->out_sg[i].iov_len - port->iov_offset;
  159. ret = vsc->have_data(port,
  160. port->elem->out_sg[i].iov_base
  161. + port->iov_offset,
  162. buf_size);
  163. if (!port->elem) { /* bail if we got disconnected */
  164. return;
  165. }
  166. if (port->throttled) {
  167. port->iov_idx = i;
  168. if (ret > 0) {
  169. port->iov_offset += ret;
  170. }
  171. break;
  172. }
  173. port->iov_offset = 0;
  174. }
  175. if (port->throttled) {
  176. break;
  177. }
  178. virtqueue_push(vq, port->elem, 0);
  179. g_free(port->elem);
  180. port->elem = NULL;
  181. }
  182. virtio_notify(vdev, vq);
  183. }
  184. static void flush_queued_data(VirtIOSerialPort *port)
  185. {
  186. assert(port);
  187. if (!virtio_queue_ready(port->ovq)) {
  188. return;
  189. }
  190. do_flush_queued_data(port, port->ovq, VIRTIO_DEVICE(port->vser));
  191. }
  192. static size_t send_control_msg(VirtIOSerial *vser, void *buf, size_t len)
  193. {
  194. VirtQueueElement *elem;
  195. VirtQueue *vq;
  196. vq = vser->c_ivq;
  197. if (!virtio_queue_ready(vq)) {
  198. return 0;
  199. }
  200. elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
  201. if (!elem) {
  202. return 0;
  203. }
  204. /* TODO: detect a buffer that's too short, set NEEDS_RESET */
  205. iov_from_buf(elem->in_sg, elem->in_num, 0, buf, len);
  206. virtqueue_push(vq, elem, len);
  207. virtio_notify(VIRTIO_DEVICE(vser), vq);
  208. g_free(elem);
  209. return len;
  210. }
  211. static size_t send_control_event(VirtIOSerial *vser, uint32_t port_id,
  212. uint16_t event, uint16_t value)
  213. {
  214. VirtIODevice *vdev = VIRTIO_DEVICE(vser);
  215. struct virtio_console_control cpkt;
  216. virtio_stl_p(vdev, &cpkt.id, port_id);
  217. virtio_stw_p(vdev, &cpkt.event, event);
  218. virtio_stw_p(vdev, &cpkt.value, value);
  219. trace_virtio_serial_send_control_event(port_id, event, value);
  220. return send_control_msg(vser, &cpkt, sizeof(cpkt));
  221. }
  222. /* Functions for use inside qemu to open and read from/write to ports */
  223. int virtio_serial_open(VirtIOSerialPort *port)
  224. {
  225. /* Don't allow opening an already-open port */
  226. if (port->host_connected) {
  227. return 0;
  228. }
  229. /* Send port open notification to the guest */
  230. port->host_connected = true;
  231. send_control_event(port->vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 1);
  232. return 0;
  233. }
  234. int virtio_serial_close(VirtIOSerialPort *port)
  235. {
  236. port->host_connected = false;
  237. /*
  238. * If there's any data the guest sent which the app didn't
  239. * consume, reset the throttling flag and discard the data.
  240. */
  241. port->throttled = false;
  242. discard_throttle_data(port);
  243. discard_vq_data(port->ovq, VIRTIO_DEVICE(port->vser));
  244. send_control_event(port->vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 0);
  245. return 0;
  246. }
  247. /* Individual ports/apps call this function to write to the guest. */
  248. ssize_t virtio_serial_write(VirtIOSerialPort *port, const uint8_t *buf,
  249. size_t size)
  250. {
  251. if (!port || !port->host_connected || !port->guest_connected) {
  252. return 0;
  253. }
  254. return write_to_port(port, buf, size);
  255. }
  256. /*
  257. * Readiness of the guest to accept data on a port.
  258. * Returns max. data the guest can receive
  259. */
  260. size_t virtio_serial_guest_ready(VirtIOSerialPort *port)
  261. {
  262. VirtIODevice *vdev = VIRTIO_DEVICE(port->vser);
  263. VirtQueue *vq = port->ivq;
  264. unsigned int bytes;
  265. if (!virtio_queue_ready(vq) ||
  266. !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) ||
  267. virtio_queue_empty(vq)) {
  268. return 0;
  269. }
  270. if (use_multiport(port->vser) && !port->guest_connected) {
  271. return 0;
  272. }
  273. virtqueue_get_avail_bytes(vq, &bytes, NULL, 4096, 0);
  274. return bytes;
  275. }
  276. static void flush_queued_data_bh(void *opaque)
  277. {
  278. VirtIOSerialPort *port = opaque;
  279. flush_queued_data(port);
  280. }
  281. void virtio_serial_throttle_port(VirtIOSerialPort *port, bool throttle)
  282. {
  283. if (!port) {
  284. return;
  285. }
  286. trace_virtio_serial_throttle_port(port->id, throttle);
  287. port->throttled = throttle;
  288. if (throttle) {
  289. return;
  290. }
  291. qemu_bh_schedule(port->bh);
  292. }
  293. /* Guest wants to notify us of some event */
  294. static void handle_control_message(VirtIOSerial *vser, void *buf, size_t len)
  295. {
  296. VirtIODevice *vdev = VIRTIO_DEVICE(vser);
  297. struct VirtIOSerialPort *port;
  298. VirtIOSerialPortClass *vsc;
  299. struct virtio_console_control cpkt, *gcpkt;
  300. uint8_t *buffer;
  301. size_t buffer_len;
  302. gcpkt = buf;
  303. if (len < sizeof(cpkt)) {
  304. /* The guest sent an invalid control packet */
  305. return;
  306. }
  307. cpkt.event = virtio_lduw_p(vdev, &gcpkt->event);
  308. cpkt.value = virtio_lduw_p(vdev, &gcpkt->value);
  309. trace_virtio_serial_handle_control_message(cpkt.event, cpkt.value);
  310. if (cpkt.event == VIRTIO_CONSOLE_DEVICE_READY) {
  311. if (!cpkt.value) {
  312. error_report("virtio-serial-bus: Guest failure in adding device %s",
  313. vser->bus.qbus.name);
  314. return;
  315. }
  316. /*
  317. * The device is up, we can now tell the device about all the
  318. * ports we have here.
  319. */
  320. QTAILQ_FOREACH(port, &vser->ports, next) {
  321. send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_ADD, 1);
  322. }
  323. return;
  324. }
  325. port = find_port_by_id(vser, virtio_ldl_p(vdev, &gcpkt->id));
  326. if (!port) {
  327. error_report("virtio-serial-bus: Unexpected port id %u for device %s",
  328. virtio_ldl_p(vdev, &gcpkt->id), vser->bus.qbus.name);
  329. return;
  330. }
  331. trace_virtio_serial_handle_control_message_port(port->id);
  332. vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
  333. switch(cpkt.event) {
  334. case VIRTIO_CONSOLE_PORT_READY:
  335. if (!cpkt.value) {
  336. error_report("virtio-serial-bus: Guest failure in adding port %u for device %s",
  337. port->id, vser->bus.qbus.name);
  338. break;
  339. }
  340. /*
  341. * Now that we know the guest asked for the port name, we're
  342. * sure the guest has initialised whatever state is necessary
  343. * for this port. Now's a good time to let the guest know if
  344. * this port is a console port so that the guest can hook it
  345. * up to hvc.
  346. */
  347. if (vsc->is_console) {
  348. send_control_event(vser, port->id, VIRTIO_CONSOLE_CONSOLE_PORT, 1);
  349. }
  350. if (port->name) {
  351. virtio_stl_p(vdev, &cpkt.id, port->id);
  352. virtio_stw_p(vdev, &cpkt.event, VIRTIO_CONSOLE_PORT_NAME);
  353. virtio_stw_p(vdev, &cpkt.value, 1);
  354. buffer_len = sizeof(cpkt) + strlen(port->name) + 1;
  355. buffer = g_malloc(buffer_len);
  356. memcpy(buffer, &cpkt, sizeof(cpkt));
  357. memcpy(buffer + sizeof(cpkt), port->name, strlen(port->name));
  358. buffer[buffer_len - 1] = 0;
  359. send_control_msg(vser, buffer, buffer_len);
  360. g_free(buffer);
  361. }
  362. if (port->host_connected) {
  363. send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_OPEN, 1);
  364. }
  365. /*
  366. * When the guest has asked us for this information it means
  367. * the guest is all setup and has its virtqueues
  368. * initialised. If some app is interested in knowing about
  369. * this event, let it know.
  370. */
  371. if (vsc->guest_ready) {
  372. vsc->guest_ready(port);
  373. }
  374. break;
  375. case VIRTIO_CONSOLE_PORT_OPEN:
  376. port->guest_connected = cpkt.value;
  377. if (vsc->set_guest_connected) {
  378. /* Send the guest opened notification if an app is interested */
  379. vsc->set_guest_connected(port, cpkt.value);
  380. }
  381. break;
  382. }
  383. }
  384. static void control_in(VirtIODevice *vdev, VirtQueue *vq)
  385. {
  386. }
  387. static void control_out(VirtIODevice *vdev, VirtQueue *vq)
  388. {
  389. VirtQueueElement *elem;
  390. VirtIOSerial *vser;
  391. uint8_t *buf;
  392. size_t len;
  393. vser = VIRTIO_SERIAL(vdev);
  394. len = 0;
  395. buf = NULL;
  396. for (;;) {
  397. size_t cur_len;
  398. elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
  399. if (!elem) {
  400. break;
  401. }
  402. cur_len = iov_size(elem->out_sg, elem->out_num);
  403. /*
  404. * Allocate a new buf only if we didn't have one previously or
  405. * if the size of the buf differs
  406. */
  407. if (cur_len > len) {
  408. g_free(buf);
  409. buf = g_malloc(cur_len);
  410. len = cur_len;
  411. }
  412. iov_to_buf(elem->out_sg, elem->out_num, 0, buf, cur_len);
  413. handle_control_message(vser, buf, cur_len);
  414. virtqueue_push(vq, elem, 0);
  415. g_free(elem);
  416. }
  417. g_free(buf);
  418. virtio_notify(vdev, vq);
  419. }
  420. /* Guest wrote something to some port. */
  421. static void handle_output(VirtIODevice *vdev, VirtQueue *vq)
  422. {
  423. VirtIOSerial *vser;
  424. VirtIOSerialPort *port;
  425. vser = VIRTIO_SERIAL(vdev);
  426. port = find_port_by_vq(vser, vq);
  427. if (!port || !port->host_connected) {
  428. discard_vq_data(vq, vdev);
  429. return;
  430. }
  431. if (!port->throttled) {
  432. do_flush_queued_data(port, vq, vdev);
  433. return;
  434. }
  435. }
  436. static void handle_input(VirtIODevice *vdev, VirtQueue *vq)
  437. {
  438. /*
  439. * Users of virtio-serial would like to know when guest becomes
  440. * writable again -- i.e. if a vq had stuff queued up and the
  441. * guest wasn't reading at all, the host would not be able to
  442. * write to the vq anymore. Once the guest reads off something,
  443. * we can start queueing things up again. However, this call is
  444. * made for each buffer addition by the guest -- even though free
  445. * buffers existed prior to the current buffer addition. This is
  446. * done so as not to maintain previous state, which will need
  447. * additional live-migration-related changes.
  448. */
  449. VirtIOSerial *vser;
  450. VirtIOSerialPort *port;
  451. VirtIOSerialPortClass *vsc;
  452. vser = VIRTIO_SERIAL(vdev);
  453. port = find_port_by_vq(vser, vq);
  454. if (!port) {
  455. return;
  456. }
  457. vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
  458. /*
  459. * If guest_connected is false, this call is being made by the
  460. * early-boot queueing up of descriptors, which is just noise for
  461. * the host apps -- don't disturb them in that case.
  462. */
  463. if (port->guest_connected && port->host_connected && vsc->guest_writable) {
  464. vsc->guest_writable(port);
  465. }
  466. }
  467. static uint64_t get_features(VirtIODevice *vdev, uint64_t features,
  468. Error **errp)
  469. {
  470. VirtIOSerial *vser;
  471. vser = VIRTIO_SERIAL(vdev);
  472. features |= vser->host_features;
  473. if (vser->bus.max_nr_ports > 1) {
  474. virtio_add_feature(&features, VIRTIO_CONSOLE_F_MULTIPORT);
  475. }
  476. return features;
  477. }
  478. /* Guest requested config info */
  479. static void get_config(VirtIODevice *vdev, uint8_t *config_data)
  480. {
  481. VirtIOSerial *vser = VIRTIO_SERIAL(vdev);
  482. struct virtio_console_config *config =
  483. (struct virtio_console_config *)config_data;
  484. config->cols = 0;
  485. config->rows = 0;
  486. config->max_nr_ports = virtio_tswap32(vdev,
  487. vser->serial.max_virtserial_ports);
  488. }
  489. /* Guest sent new config info */
  490. static void set_config(VirtIODevice *vdev, const uint8_t *config_data)
  491. {
  492. VirtIOSerial *vser = VIRTIO_SERIAL(vdev);
  493. struct virtio_console_config *config =
  494. (struct virtio_console_config *)config_data;
  495. VirtIOSerialPort *port = find_first_connected_console(vser);
  496. VirtIOSerialPortClass *vsc;
  497. uint8_t emerg_wr_lo;
  498. if (!virtio_has_feature(vser->host_features,
  499. VIRTIO_CONSOLE_F_EMERG_WRITE) || !config->emerg_wr) {
  500. return;
  501. }
  502. emerg_wr_lo = le32_to_cpu(config->emerg_wr);
  503. /* Make sure we don't misdetect an emergency write when the guest
  504. * does a short config write after an emergency write. */
  505. config->emerg_wr = 0;
  506. if (!port) {
  507. return;
  508. }
  509. vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
  510. (void)vsc->have_data(port, &emerg_wr_lo, 1);
  511. }
  512. static void guest_reset(VirtIOSerial *vser)
  513. {
  514. VirtIOSerialPort *port;
  515. VirtIOSerialPortClass *vsc;
  516. QTAILQ_FOREACH(port, &vser->ports, next) {
  517. vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
  518. discard_throttle_data(port);
  519. if (port->guest_connected) {
  520. port->guest_connected = false;
  521. if (vsc->set_guest_connected) {
  522. vsc->set_guest_connected(port, false);
  523. }
  524. }
  525. }
  526. }
  527. static void set_status(VirtIODevice *vdev, uint8_t status)
  528. {
  529. VirtIOSerial *vser;
  530. VirtIOSerialPort *port;
  531. vser = VIRTIO_SERIAL(vdev);
  532. port = find_port_by_id(vser, 0);
  533. if (port && !use_multiport(port->vser)
  534. && (status & VIRTIO_CONFIG_S_DRIVER_OK)) {
  535. /*
  536. * Non-multiport guests won't be able to tell us guest
  537. * open/close status. Such guests can only have a port at id
  538. * 0, so set guest_connected for such ports as soon as guest
  539. * is up.
  540. */
  541. port->guest_connected = true;
  542. }
  543. if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
  544. guest_reset(vser);
  545. }
  546. QTAILQ_FOREACH(port, &vser->ports, next) {
  547. VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
  548. if (vsc->enable_backend) {
  549. vsc->enable_backend(port, vdev->vm_running);
  550. }
  551. }
  552. }
  553. static void vser_reset(VirtIODevice *vdev)
  554. {
  555. VirtIOSerial *vser;
  556. vser = VIRTIO_SERIAL(vdev);
  557. guest_reset(vser);
  558. }
  559. static void virtio_serial_save_device(VirtIODevice *vdev, QEMUFile *f)
  560. {
  561. VirtIOSerial *s = VIRTIO_SERIAL(vdev);
  562. VirtIOSerialPort *port;
  563. uint32_t nr_active_ports;
  564. unsigned int i, max_nr_ports;
  565. struct virtio_console_config config;
  566. /* The config space (ignored on the far end in current versions) */
  567. get_config(vdev, (uint8_t *)&config);
  568. qemu_put_be16(f, config.cols);
  569. qemu_put_be16(f, config.rows);
  570. qemu_put_be32(f, config.max_nr_ports);
  571. /* The ports map */
  572. max_nr_ports = s->serial.max_virtserial_ports;
  573. for (i = 0; i < DIV_ROUND_UP(max_nr_ports, 32); i++) {
  574. qemu_put_be32s(f, &s->ports_map[i]);
  575. }
  576. /* Ports */
  577. nr_active_ports = 0;
  578. QTAILQ_FOREACH(port, &s->ports, next) {
  579. nr_active_ports++;
  580. }
  581. qemu_put_be32s(f, &nr_active_ports);
  582. /*
  583. * Items in struct VirtIOSerialPort.
  584. */
  585. QTAILQ_FOREACH(port, &s->ports, next) {
  586. uint32_t elem_popped;
  587. qemu_put_be32s(f, &port->id);
  588. qemu_put_byte(f, port->guest_connected);
  589. qemu_put_byte(f, port->host_connected);
  590. elem_popped = 0;
  591. if (port->elem) {
  592. elem_popped = 1;
  593. }
  594. qemu_put_be32s(f, &elem_popped);
  595. if (elem_popped) {
  596. qemu_put_be32s(f, &port->iov_idx);
  597. qemu_put_be64s(f, &port->iov_offset);
  598. qemu_put_virtqueue_element(vdev, f, port->elem);
  599. }
  600. }
  601. }
  602. static void virtio_serial_post_load_timer_cb(void *opaque)
  603. {
  604. uint32_t i;
  605. VirtIOSerial *s = VIRTIO_SERIAL(opaque);
  606. VirtIOSerialPort *port;
  607. uint8_t host_connected;
  608. VirtIOSerialPortClass *vsc;
  609. if (!s->post_load) {
  610. return;
  611. }
  612. for (i = 0 ; i < s->post_load->nr_active_ports; ++i) {
  613. port = s->post_load->connected[i].port;
  614. host_connected = s->post_load->connected[i].host_connected;
  615. if (host_connected != port->host_connected) {
  616. /*
  617. * We have to let the guest know of the host connection
  618. * status change
  619. */
  620. send_control_event(s, port->id, VIRTIO_CONSOLE_PORT_OPEN,
  621. port->host_connected);
  622. }
  623. vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
  624. if (vsc->set_guest_connected) {
  625. vsc->set_guest_connected(port, port->guest_connected);
  626. }
  627. }
  628. g_free(s->post_load->connected);
  629. timer_del(s->post_load->timer);
  630. timer_free(s->post_load->timer);
  631. g_free(s->post_load);
  632. s->post_load = NULL;
  633. }
  634. static int fetch_active_ports_list(QEMUFile *f,
  635. VirtIOSerial *s, uint32_t nr_active_ports)
  636. {
  637. VirtIODevice *vdev = VIRTIO_DEVICE(s);
  638. uint32_t i;
  639. s->post_load = g_malloc0(sizeof(*s->post_load));
  640. s->post_load->nr_active_ports = nr_active_ports;
  641. s->post_load->connected =
  642. g_malloc0(sizeof(*s->post_load->connected) * nr_active_ports);
  643. s->post_load->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
  644. virtio_serial_post_load_timer_cb,
  645. s);
  646. /* Items in struct VirtIOSerialPort */
  647. for (i = 0; i < nr_active_ports; i++) {
  648. VirtIOSerialPort *port;
  649. uint32_t elem_popped;
  650. uint32_t id;
  651. id = qemu_get_be32(f);
  652. port = find_port_by_id(s, id);
  653. if (!port) {
  654. return -EINVAL;
  655. }
  656. port->guest_connected = qemu_get_byte(f);
  657. s->post_load->connected[i].port = port;
  658. s->post_load->connected[i].host_connected = qemu_get_byte(f);
  659. qemu_get_be32s(f, &elem_popped);
  660. if (elem_popped) {
  661. qemu_get_be32s(f, &port->iov_idx);
  662. qemu_get_be64s(f, &port->iov_offset);
  663. port->elem =
  664. qemu_get_virtqueue_element(vdev, f, sizeof(VirtQueueElement));
  665. /*
  666. * Port was throttled on source machine. Let's
  667. * unthrottle it here so data starts flowing again.
  668. */
  669. virtio_serial_throttle_port(port, false);
  670. }
  671. }
  672. timer_mod(s->post_load->timer, 1);
  673. return 0;
  674. }
  675. static int virtio_serial_load_device(VirtIODevice *vdev, QEMUFile *f,
  676. int version_id)
  677. {
  678. VirtIOSerial *s = VIRTIO_SERIAL(vdev);
  679. uint32_t max_nr_ports, nr_active_ports, ports_map;
  680. unsigned int i;
  681. int ret;
  682. uint32_t tmp;
  683. /* Unused */
  684. qemu_get_be16s(f, (uint16_t *) &tmp);
  685. qemu_get_be16s(f, (uint16_t *) &tmp);
  686. qemu_get_be32s(f, &tmp);
  687. max_nr_ports = s->serial.max_virtserial_ports;
  688. for (i = 0; i < DIV_ROUND_UP(max_nr_ports, 32); i++) {
  689. qemu_get_be32s(f, &ports_map);
  690. if (ports_map != s->ports_map[i]) {
  691. /*
  692. * Ports active on source and destination don't
  693. * match. Fail migration.
  694. */
  695. return -EINVAL;
  696. }
  697. }
  698. qemu_get_be32s(f, &nr_active_ports);
  699. if (nr_active_ports) {
  700. ret = fetch_active_ports_list(f, s, nr_active_ports);
  701. if (ret) {
  702. return ret;
  703. }
  704. }
  705. return 0;
  706. }
  707. static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
  708. static Property virtser_props[] = {
  709. DEFINE_PROP_UINT32("nr", VirtIOSerialPort, id, VIRTIO_CONSOLE_BAD_ID),
  710. DEFINE_PROP_STRING("name", VirtIOSerialPort, name),
  711. DEFINE_PROP_END_OF_LIST()
  712. };
  713. #define TYPE_VIRTIO_SERIAL_BUS "virtio-serial-bus"
  714. #define VIRTIO_SERIAL_BUS(obj) \
  715. OBJECT_CHECK(VirtIOSerialBus, (obj), TYPE_VIRTIO_SERIAL_BUS)
  716. static void virtser_bus_class_init(ObjectClass *klass, void *data)
  717. {
  718. BusClass *k = BUS_CLASS(klass);
  719. k->print_dev = virtser_bus_dev_print;
  720. }
  721. static const TypeInfo virtser_bus_info = {
  722. .name = TYPE_VIRTIO_SERIAL_BUS,
  723. .parent = TYPE_BUS,
  724. .instance_size = sizeof(VirtIOSerialBus),
  725. .class_init = virtser_bus_class_init,
  726. };
  727. static void virtser_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
  728. {
  729. VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(qdev);
  730. monitor_printf(mon, "%*sport %d, guest %s, host %s, throttle %s\n",
  731. indent, "", port->id,
  732. port->guest_connected ? "on" : "off",
  733. port->host_connected ? "on" : "off",
  734. port->throttled ? "on" : "off");
  735. }
  736. /* This function is only used if a port id is not provided by the user */
  737. static uint32_t find_free_port_id(VirtIOSerial *vser)
  738. {
  739. unsigned int i, max_nr_ports;
  740. max_nr_ports = vser->serial.max_virtserial_ports;
  741. for (i = 0; i < DIV_ROUND_UP(max_nr_ports, 32); i++) {
  742. uint32_t map, zeroes;
  743. map = vser->ports_map[i];
  744. zeroes = ctz32(~map);
  745. if (zeroes != 32) {
  746. return zeroes + i * 32;
  747. }
  748. }
  749. return VIRTIO_CONSOLE_BAD_ID;
  750. }
  751. static void mark_port_added(VirtIOSerial *vser, uint32_t port_id)
  752. {
  753. unsigned int i;
  754. i = port_id / 32;
  755. vser->ports_map[i] |= 1U << (port_id % 32);
  756. }
  757. static void add_port(VirtIOSerial *vser, uint32_t port_id)
  758. {
  759. mark_port_added(vser, port_id);
  760. send_control_event(vser, port_id, VIRTIO_CONSOLE_PORT_ADD, 1);
  761. }
  762. static void remove_port(VirtIOSerial *vser, uint32_t port_id)
  763. {
  764. VirtIOSerialPort *port;
  765. /*
  766. * Don't mark port 0 removed -- we explicitly reserve it for
  767. * backward compat with older guests, ensure a virtconsole device
  768. * unplug retains the reservation.
  769. */
  770. if (port_id) {
  771. unsigned int i;
  772. i = port_id / 32;
  773. vser->ports_map[i] &= ~(1U << (port_id % 32));
  774. }
  775. port = find_port_by_id(vser, port_id);
  776. /*
  777. * This function is only called from qdev's unplug callback; if we
  778. * get a NULL port here, we're in trouble.
  779. */
  780. assert(port);
  781. /* Flush out any unconsumed buffers first */
  782. discard_throttle_data(port);
  783. discard_vq_data(port->ovq, VIRTIO_DEVICE(port->vser));
  784. send_control_event(vser, port->id, VIRTIO_CONSOLE_PORT_REMOVE, 1);
  785. }
  786. static void virtser_port_device_realize(DeviceState *dev, Error **errp)
  787. {
  788. VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
  789. VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(port);
  790. VirtIOSerialBus *bus = VIRTIO_SERIAL_BUS(qdev_get_parent_bus(dev));
  791. int max_nr_ports;
  792. bool plugging_port0;
  793. Error *err = NULL;
  794. port->vser = bus->vser;
  795. assert(vsc->have_data);
  796. /*
  797. * Is the first console port we're seeing? If so, put it up at
  798. * location 0. This is done for backward compatibility (old
  799. * kernel, new qemu).
  800. */
  801. plugging_port0 = vsc->is_console && !find_port_by_id(port->vser, 0);
  802. if (find_port_by_id(port->vser, port->id)) {
  803. error_setg(errp, "virtio-serial-bus: A port already exists at id %u",
  804. port->id);
  805. return;
  806. }
  807. if (port->name != NULL && find_port_by_name(port->name)) {
  808. error_setg(errp, "virtio-serial-bus: A port already exists by name %s",
  809. port->name);
  810. return;
  811. }
  812. if (port->id == VIRTIO_CONSOLE_BAD_ID) {
  813. if (plugging_port0) {
  814. port->id = 0;
  815. } else {
  816. port->id = find_free_port_id(port->vser);
  817. if (port->id == VIRTIO_CONSOLE_BAD_ID) {
  818. error_setg(errp, "virtio-serial-bus: Maximum port limit for "
  819. "this device reached");
  820. return;
  821. }
  822. }
  823. }
  824. max_nr_ports = port->vser->serial.max_virtserial_ports;
  825. if (port->id >= max_nr_ports) {
  826. error_setg(errp, "virtio-serial-bus: Out-of-range port id specified, "
  827. "max. allowed: %u", max_nr_ports - 1);
  828. return;
  829. }
  830. vsc->realize(dev, &err);
  831. if (err != NULL) {
  832. error_propagate(errp, err);
  833. return;
  834. }
  835. port->bh = qemu_bh_new(flush_queued_data_bh, port);
  836. port->elem = NULL;
  837. }
  838. static void virtser_port_device_plug(HotplugHandler *hotplug_dev,
  839. DeviceState *dev, Error **errp)
  840. {
  841. VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
  842. QTAILQ_INSERT_TAIL(&port->vser->ports, port, next);
  843. port->ivq = port->vser->ivqs[port->id];
  844. port->ovq = port->vser->ovqs[port->id];
  845. add_port(port->vser, port->id);
  846. /* Send an update to the guest about this new port added */
  847. virtio_notify_config(VIRTIO_DEVICE(hotplug_dev));
  848. }
  849. static void virtser_port_device_unrealize(DeviceState *dev)
  850. {
  851. VirtIOSerialPort *port = VIRTIO_SERIAL_PORT(dev);
  852. VirtIOSerialPortClass *vsc = VIRTIO_SERIAL_PORT_GET_CLASS(dev);
  853. VirtIOSerial *vser = port->vser;
  854. qemu_bh_delete(port->bh);
  855. remove_port(port->vser, port->id);
  856. QTAILQ_REMOVE(&vser->ports, port, next);
  857. if (vsc->unrealize) {
  858. vsc->unrealize(dev);
  859. }
  860. }
  861. static void virtio_serial_device_realize(DeviceState *dev, Error **errp)
  862. {
  863. VirtIODevice *vdev = VIRTIO_DEVICE(dev);
  864. VirtIOSerial *vser = VIRTIO_SERIAL(dev);
  865. uint32_t i, max_supported_ports;
  866. size_t config_size = sizeof(struct virtio_console_config);
  867. if (!vser->serial.max_virtserial_ports) {
  868. error_setg(errp, "Maximum number of serial ports not specified");
  869. return;
  870. }
  871. /* Each port takes 2 queues, and one pair is for the control queue */
  872. max_supported_ports = VIRTIO_QUEUE_MAX / 2 - 1;
  873. if (vser->serial.max_virtserial_ports > max_supported_ports) {
  874. error_setg(errp, "maximum ports supported: %u", max_supported_ports);
  875. return;
  876. }
  877. if (!virtio_has_feature(vser->host_features,
  878. VIRTIO_CONSOLE_F_EMERG_WRITE)) {
  879. config_size = offsetof(struct virtio_console_config, emerg_wr);
  880. }
  881. virtio_init(vdev, "virtio-serial", VIRTIO_ID_CONSOLE,
  882. config_size);
  883. /* Spawn a new virtio-serial bus on which the ports will ride as devices */
  884. qbus_create_inplace(&vser->bus, sizeof(vser->bus), TYPE_VIRTIO_SERIAL_BUS,
  885. dev, vdev->bus_name);
  886. qbus_set_hotplug_handler(BUS(&vser->bus), OBJECT(vser));
  887. vser->bus.vser = vser;
  888. QTAILQ_INIT(&vser->ports);
  889. vser->bus.max_nr_ports = vser->serial.max_virtserial_ports;
  890. vser->ivqs = g_malloc(vser->serial.max_virtserial_ports
  891. * sizeof(VirtQueue *));
  892. vser->ovqs = g_malloc(vser->serial.max_virtserial_ports
  893. * sizeof(VirtQueue *));
  894. /* Add a queue for host to guest transfers for port 0 (backward compat) */
  895. vser->ivqs[0] = virtio_add_queue(vdev, 128, handle_input);
  896. /* Add a queue for guest to host transfers for port 0 (backward compat) */
  897. vser->ovqs[0] = virtio_add_queue(vdev, 128, handle_output);
  898. /* TODO: host to guest notifications can get dropped
  899. * if the queue fills up. Implement queueing in host,
  900. * this might also make it possible to reduce the control
  901. * queue size: as guest preposts buffers there,
  902. * this will save 4Kbyte of guest memory per entry. */
  903. /* control queue: host to guest */
  904. vser->c_ivq = virtio_add_queue(vdev, 32, control_in);
  905. /* control queue: guest to host */
  906. vser->c_ovq = virtio_add_queue(vdev, 32, control_out);
  907. for (i = 1; i < vser->bus.max_nr_ports; i++) {
  908. /* Add a per-port queue for host to guest transfers */
  909. vser->ivqs[i] = virtio_add_queue(vdev, 128, handle_input);
  910. /* Add a per-per queue for guest to host transfers */
  911. vser->ovqs[i] = virtio_add_queue(vdev, 128, handle_output);
  912. }
  913. vser->ports_map = g_malloc0((DIV_ROUND_UP(vser->serial.max_virtserial_ports, 32))
  914. * sizeof(vser->ports_map[0]));
  915. /*
  916. * Reserve location 0 for a console port for backward compat
  917. * (old kernel, new qemu)
  918. */
  919. mark_port_added(vser, 0);
  920. vser->post_load = NULL;
  921. QLIST_INSERT_HEAD(&vserdevices.devices, vser, next);
  922. }
  923. static void virtio_serial_port_class_init(ObjectClass *klass, void *data)
  924. {
  925. DeviceClass *k = DEVICE_CLASS(klass);
  926. set_bit(DEVICE_CATEGORY_INPUT, k->categories);
  927. k->bus_type = TYPE_VIRTIO_SERIAL_BUS;
  928. k->realize = virtser_port_device_realize;
  929. k->unrealize = virtser_port_device_unrealize;
  930. device_class_set_props(k, virtser_props);
  931. }
  932. static const TypeInfo virtio_serial_port_type_info = {
  933. .name = TYPE_VIRTIO_SERIAL_PORT,
  934. .parent = TYPE_DEVICE,
  935. .instance_size = sizeof(VirtIOSerialPort),
  936. .abstract = true,
  937. .class_size = sizeof(VirtIOSerialPortClass),
  938. .class_init = virtio_serial_port_class_init,
  939. };
  940. static void virtio_serial_device_unrealize(DeviceState *dev)
  941. {
  942. VirtIODevice *vdev = VIRTIO_DEVICE(dev);
  943. VirtIOSerial *vser = VIRTIO_SERIAL(dev);
  944. int i;
  945. QLIST_REMOVE(vser, next);
  946. virtio_delete_queue(vser->c_ivq);
  947. virtio_delete_queue(vser->c_ovq);
  948. for (i = 0; i < vser->bus.max_nr_ports; i++) {
  949. virtio_delete_queue(vser->ivqs[i]);
  950. virtio_delete_queue(vser->ovqs[i]);
  951. }
  952. g_free(vser->ivqs);
  953. g_free(vser->ovqs);
  954. g_free(vser->ports_map);
  955. if (vser->post_load) {
  956. g_free(vser->post_load->connected);
  957. timer_del(vser->post_load->timer);
  958. timer_free(vser->post_load->timer);
  959. g_free(vser->post_load);
  960. }
  961. qbus_set_hotplug_handler(BUS(&vser->bus), NULL);
  962. virtio_cleanup(vdev);
  963. }
  964. /* Note: 'console' is used for backwards compatibility */
  965. static const VMStateDescription vmstate_virtio_console = {
  966. .name = "virtio-console",
  967. .minimum_version_id = 3,
  968. .version_id = 3,
  969. .fields = (VMStateField[]) {
  970. VMSTATE_VIRTIO_DEVICE,
  971. VMSTATE_END_OF_LIST()
  972. },
  973. };
  974. static Property virtio_serial_properties[] = {
  975. DEFINE_PROP_UINT32("max_ports", VirtIOSerial, serial.max_virtserial_ports,
  976. 31),
  977. DEFINE_PROP_BIT64("emergency-write", VirtIOSerial, host_features,
  978. VIRTIO_CONSOLE_F_EMERG_WRITE, true),
  979. DEFINE_PROP_END_OF_LIST(),
  980. };
  981. static void virtio_serial_class_init(ObjectClass *klass, void *data)
  982. {
  983. DeviceClass *dc = DEVICE_CLASS(klass);
  984. VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
  985. HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
  986. QLIST_INIT(&vserdevices.devices);
  987. device_class_set_props(dc, virtio_serial_properties);
  988. dc->vmsd = &vmstate_virtio_console;
  989. set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
  990. vdc->realize = virtio_serial_device_realize;
  991. vdc->unrealize = virtio_serial_device_unrealize;
  992. vdc->get_features = get_features;
  993. vdc->get_config = get_config;
  994. vdc->set_config = set_config;
  995. vdc->set_status = set_status;
  996. vdc->reset = vser_reset;
  997. vdc->save = virtio_serial_save_device;
  998. vdc->load = virtio_serial_load_device;
  999. hc->plug = virtser_port_device_plug;
  1000. hc->unplug = qdev_simple_device_unplug_cb;
  1001. }
  1002. static const TypeInfo virtio_device_info = {
  1003. .name = TYPE_VIRTIO_SERIAL,
  1004. .parent = TYPE_VIRTIO_DEVICE,
  1005. .instance_size = sizeof(VirtIOSerial),
  1006. .class_init = virtio_serial_class_init,
  1007. .interfaces = (InterfaceInfo[]) {
  1008. { TYPE_HOTPLUG_HANDLER },
  1009. { }
  1010. }
  1011. };
  1012. static void virtio_serial_register_types(void)
  1013. {
  1014. type_register_static(&virtser_bus_info);
  1015. type_register_static(&virtio_serial_port_type_info);
  1016. type_register_static(&virtio_device_info);
  1017. }
  1018. type_init(virtio_serial_register_types)