virtio-serial-bus.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  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. port->bh = qemu_bh_new(flush_queued_data_bh, port);
  796. assert(vsc->have_data);
  797. /*
  798. * Is the first console port we're seeing? If so, put it up at
  799. * location 0. This is done for backward compatibility (old
  800. * kernel, new qemu).
  801. */
  802. plugging_port0 = vsc->is_console && !find_port_by_id(port->vser, 0);
  803. if (find_port_by_id(port->vser, port->id)) {
  804. error_setg(errp, "virtio-serial-bus: A port already exists at id %u",
  805. port->id);
  806. return;
  807. }
  808. if (port->name != NULL && find_port_by_name(port->name)) {
  809. error_setg(errp, "virtio-serial-bus: A port already exists by name %s",
  810. port->name);
  811. return;
  812. }
  813. if (port->id == VIRTIO_CONSOLE_BAD_ID) {
  814. if (plugging_port0) {
  815. port->id = 0;
  816. } else {
  817. port->id = find_free_port_id(port->vser);
  818. if (port->id == VIRTIO_CONSOLE_BAD_ID) {
  819. error_setg(errp, "virtio-serial-bus: Maximum port limit for "
  820. "this device reached");
  821. return;
  822. }
  823. }
  824. }
  825. max_nr_ports = port->vser->serial.max_virtserial_ports;
  826. if (port->id >= max_nr_ports) {
  827. error_setg(errp, "virtio-serial-bus: Out-of-range port id specified, "
  828. "max. allowed: %u", max_nr_ports - 1);
  829. return;
  830. }
  831. vsc->realize(dev, &err);
  832. if (err != NULL) {
  833. error_propagate(errp, err);
  834. return;
  835. }
  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, Error **errp)
  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, errp);
  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), errp);
  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. k->props = 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, Error **errp)
  941. {
  942. VirtIODevice *vdev = VIRTIO_DEVICE(dev);
  943. VirtIOSerial *vser = VIRTIO_SERIAL(dev);
  944. QLIST_REMOVE(vser, next);
  945. g_free(vser->ivqs);
  946. g_free(vser->ovqs);
  947. g_free(vser->ports_map);
  948. if (vser->post_load) {
  949. g_free(vser->post_load->connected);
  950. timer_del(vser->post_load->timer);
  951. timer_free(vser->post_load->timer);
  952. g_free(vser->post_load);
  953. }
  954. qbus_set_hotplug_handler(BUS(&vser->bus), NULL, errp);
  955. virtio_cleanup(vdev);
  956. }
  957. /* Note: 'console' is used for backwards compatibility */
  958. static const VMStateDescription vmstate_virtio_console = {
  959. .name = "virtio-console",
  960. .minimum_version_id = 3,
  961. .version_id = 3,
  962. .fields = (VMStateField[]) {
  963. VMSTATE_VIRTIO_DEVICE,
  964. VMSTATE_END_OF_LIST()
  965. },
  966. };
  967. static Property virtio_serial_properties[] = {
  968. DEFINE_PROP_UINT32("max_ports", VirtIOSerial, serial.max_virtserial_ports,
  969. 31),
  970. DEFINE_PROP_BIT64("emergency-write", VirtIOSerial, host_features,
  971. VIRTIO_CONSOLE_F_EMERG_WRITE, true),
  972. DEFINE_PROP_END_OF_LIST(),
  973. };
  974. static void virtio_serial_class_init(ObjectClass *klass, void *data)
  975. {
  976. DeviceClass *dc = DEVICE_CLASS(klass);
  977. VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
  978. HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass);
  979. QLIST_INIT(&vserdevices.devices);
  980. dc->props = virtio_serial_properties;
  981. dc->vmsd = &vmstate_virtio_console;
  982. set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
  983. vdc->realize = virtio_serial_device_realize;
  984. vdc->unrealize = virtio_serial_device_unrealize;
  985. vdc->get_features = get_features;
  986. vdc->get_config = get_config;
  987. vdc->set_config = set_config;
  988. vdc->set_status = set_status;
  989. vdc->reset = vser_reset;
  990. vdc->save = virtio_serial_save_device;
  991. vdc->load = virtio_serial_load_device;
  992. hc->plug = virtser_port_device_plug;
  993. hc->unplug = qdev_simple_device_unplug_cb;
  994. }
  995. static const TypeInfo virtio_device_info = {
  996. .name = TYPE_VIRTIO_SERIAL,
  997. .parent = TYPE_VIRTIO_DEVICE,
  998. .instance_size = sizeof(VirtIOSerial),
  999. .class_init = virtio_serial_class_init,
  1000. .interfaces = (InterfaceInfo[]) {
  1001. { TYPE_HOTPLUG_HANDLER },
  1002. { }
  1003. }
  1004. };
  1005. static void virtio_serial_register_types(void)
  1006. {
  1007. type_register_static(&virtser_bus_info);
  1008. type_register_static(&virtio_serial_port_type_info);
  1009. type_register_static(&virtio_device_info);
  1010. }
  1011. type_init(virtio_serial_register_types)