virtio_uml.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Virtio vhost-user driver
  4. *
  5. * Copyright(c) 2019 Intel Corporation
  6. *
  7. * This driver allows virtio devices to be used over a vhost-user socket.
  8. *
  9. * Guest devices can be instantiated by kernel module or command line
  10. * parameters. One device will be created for each parameter. Syntax:
  11. *
  12. * virtio_uml.device=<socket>:<virtio_id>[:<platform_id>]
  13. * where:
  14. * <socket> := vhost-user socket path to connect
  15. * <virtio_id> := virtio device id (as in virtio_ids.h)
  16. * <platform_id> := (optional) platform device id
  17. *
  18. * example:
  19. * virtio_uml.device=/var/uml.socket:1
  20. *
  21. * Based on Virtio MMIO driver by Pawel Moll, copyright 2011-2014, ARM Ltd.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/of.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/slab.h>
  27. #include <linux/string_choices.h>
  28. #include <linux/virtio.h>
  29. #include <linux/virtio_config.h>
  30. #include <linux/virtio_ring.h>
  31. #include <linux/time-internal.h>
  32. #include <linux/virtio-uml.h>
  33. #include <shared/as-layout.h>
  34. #include <irq_kern.h>
  35. #include <init.h>
  36. #include <os.h>
  37. #include "mconsole_kern.h"
  38. #include "vhost_user.h"
  39. #define MAX_SUPPORTED_QUEUE_SIZE 256
  40. #define to_virtio_uml_device(_vdev) \
  41. container_of(_vdev, struct virtio_uml_device, vdev)
  42. struct virtio_uml_platform_data {
  43. u32 virtio_device_id;
  44. const char *socket_path;
  45. struct work_struct conn_broken_wk;
  46. struct platform_device *pdev;
  47. };
  48. struct virtio_uml_device {
  49. struct virtio_device vdev;
  50. struct platform_device *pdev;
  51. struct virtio_uml_platform_data *pdata;
  52. raw_spinlock_t sock_lock;
  53. int sock, req_fd, irq;
  54. u64 features;
  55. u64 protocol_features;
  56. u64 max_vqs;
  57. u8 status;
  58. u8 registered:1;
  59. u8 suspended:1;
  60. u8 no_vq_suspend:1;
  61. u8 config_changed_irq:1;
  62. uint64_t vq_irq_vq_map;
  63. int recv_rc;
  64. };
  65. struct virtio_uml_vq_info {
  66. int kick_fd, call_fd;
  67. char name[32];
  68. bool suspended;
  69. };
  70. #define vu_err(vu_dev, ...) dev_err(&(vu_dev)->pdev->dev, ##__VA_ARGS__)
  71. /* Vhost-user protocol */
  72. static int full_sendmsg_fds(int fd, const void *buf, unsigned int len,
  73. const int *fds, unsigned int fds_num)
  74. {
  75. int rc;
  76. do {
  77. rc = os_sendmsg_fds(fd, buf, len, fds, fds_num);
  78. if (rc > 0) {
  79. buf += rc;
  80. len -= rc;
  81. fds = NULL;
  82. fds_num = 0;
  83. }
  84. } while (len && (rc >= 0 || rc == -EINTR));
  85. if (rc < 0)
  86. return rc;
  87. return 0;
  88. }
  89. static int full_read(int fd, void *buf, int len, bool abortable)
  90. {
  91. int rc;
  92. if (!len)
  93. return 0;
  94. do {
  95. rc = os_read_file(fd, buf, len);
  96. if (rc > 0) {
  97. buf += rc;
  98. len -= rc;
  99. }
  100. } while (len && (rc > 0 || rc == -EINTR || (!abortable && rc == -EAGAIN)));
  101. if (rc < 0)
  102. return rc;
  103. if (rc == 0)
  104. return -ECONNRESET;
  105. return 0;
  106. }
  107. static int vhost_user_recv_header(int fd, struct vhost_user_msg *msg)
  108. {
  109. return full_read(fd, msg, sizeof(msg->header), true);
  110. }
  111. static int vhost_user_recv(struct virtio_uml_device *vu_dev,
  112. int fd, struct vhost_user_msg *msg,
  113. size_t max_payload_size, bool wait)
  114. {
  115. size_t size;
  116. int rc;
  117. /*
  118. * In virtio time-travel mode, we're handling all the vhost-user
  119. * FDs by polling them whenever appropriate. However, we may get
  120. * into a situation where we're sending out an interrupt message
  121. * to a device (e.g. a net device) and need to handle a simulation
  122. * time message while doing so, e.g. one that tells us to update
  123. * our idea of how long we can run without scheduling.
  124. *
  125. * Thus, we need to not just read() from the given fd, but need
  126. * to also handle messages for the simulation time - this function
  127. * does that for us while waiting for the given fd to be readable.
  128. */
  129. if (wait)
  130. time_travel_wait_readable(fd);
  131. rc = vhost_user_recv_header(fd, msg);
  132. if (rc)
  133. return rc;
  134. size = msg->header.size;
  135. if (size > max_payload_size)
  136. return -EPROTO;
  137. return full_read(fd, &msg->payload, size, false);
  138. }
  139. static void vhost_user_check_reset(struct virtio_uml_device *vu_dev,
  140. int rc)
  141. {
  142. struct virtio_uml_platform_data *pdata = vu_dev->pdata;
  143. if (rc != -ECONNRESET)
  144. return;
  145. if (!vu_dev->registered)
  146. return;
  147. vu_dev->registered = 0;
  148. schedule_work(&pdata->conn_broken_wk);
  149. }
  150. static int vhost_user_recv_resp(struct virtio_uml_device *vu_dev,
  151. struct vhost_user_msg *msg,
  152. size_t max_payload_size)
  153. {
  154. int rc = vhost_user_recv(vu_dev, vu_dev->sock, msg,
  155. max_payload_size, true);
  156. if (rc) {
  157. vhost_user_check_reset(vu_dev, rc);
  158. return rc;
  159. }
  160. if (msg->header.flags != (VHOST_USER_FLAG_REPLY | VHOST_USER_VERSION))
  161. return -EPROTO;
  162. return 0;
  163. }
  164. static int vhost_user_recv_u64(struct virtio_uml_device *vu_dev,
  165. u64 *value)
  166. {
  167. struct vhost_user_msg msg;
  168. int rc = vhost_user_recv_resp(vu_dev, &msg,
  169. sizeof(msg.payload.integer));
  170. if (rc)
  171. return rc;
  172. if (msg.header.size != sizeof(msg.payload.integer))
  173. return -EPROTO;
  174. *value = msg.payload.integer;
  175. return 0;
  176. }
  177. static int vhost_user_recv_req(struct virtio_uml_device *vu_dev,
  178. struct vhost_user_msg *msg,
  179. size_t max_payload_size)
  180. {
  181. int rc = vhost_user_recv(vu_dev, vu_dev->req_fd, msg,
  182. max_payload_size, false);
  183. if (rc)
  184. return rc;
  185. if ((msg->header.flags & ~VHOST_USER_FLAG_NEED_REPLY) !=
  186. VHOST_USER_VERSION)
  187. return -EPROTO;
  188. return 0;
  189. }
  190. static int vhost_user_send(struct virtio_uml_device *vu_dev,
  191. bool need_response, struct vhost_user_msg *msg,
  192. int *fds, size_t num_fds)
  193. {
  194. size_t size = sizeof(msg->header) + msg->header.size;
  195. unsigned long flags;
  196. bool request_ack;
  197. int rc;
  198. msg->header.flags |= VHOST_USER_VERSION;
  199. /*
  200. * The need_response flag indicates that we already need a response,
  201. * e.g. to read the features. In these cases, don't request an ACK as
  202. * it is meaningless. Also request an ACK only if supported.
  203. */
  204. request_ack = !need_response;
  205. if (!(vu_dev->protocol_features &
  206. BIT_ULL(VHOST_USER_PROTOCOL_F_REPLY_ACK)))
  207. request_ack = false;
  208. if (request_ack)
  209. msg->header.flags |= VHOST_USER_FLAG_NEED_REPLY;
  210. raw_spin_lock_irqsave(&vu_dev->sock_lock, flags);
  211. rc = full_sendmsg_fds(vu_dev->sock, msg, size, fds, num_fds);
  212. if (rc < 0)
  213. goto out;
  214. if (request_ack) {
  215. uint64_t status;
  216. rc = vhost_user_recv_u64(vu_dev, &status);
  217. if (rc)
  218. goto out;
  219. if (status) {
  220. vu_err(vu_dev, "slave reports error: %llu\n", status);
  221. rc = -EIO;
  222. goto out;
  223. }
  224. }
  225. out:
  226. raw_spin_unlock_irqrestore(&vu_dev->sock_lock, flags);
  227. return rc;
  228. }
  229. static int vhost_user_send_no_payload(struct virtio_uml_device *vu_dev,
  230. bool need_response, u32 request)
  231. {
  232. struct vhost_user_msg msg = {
  233. .header.request = request,
  234. };
  235. return vhost_user_send(vu_dev, need_response, &msg, NULL, 0);
  236. }
  237. static int vhost_user_send_no_payload_fd(struct virtio_uml_device *vu_dev,
  238. u32 request, int fd)
  239. {
  240. struct vhost_user_msg msg = {
  241. .header.request = request,
  242. };
  243. return vhost_user_send(vu_dev, false, &msg, &fd, 1);
  244. }
  245. static int vhost_user_send_u64(struct virtio_uml_device *vu_dev,
  246. u32 request, u64 value)
  247. {
  248. struct vhost_user_msg msg = {
  249. .header.request = request,
  250. .header.size = sizeof(msg.payload.integer),
  251. .payload.integer = value,
  252. };
  253. return vhost_user_send(vu_dev, false, &msg, NULL, 0);
  254. }
  255. static int vhost_user_set_owner(struct virtio_uml_device *vu_dev)
  256. {
  257. return vhost_user_send_no_payload(vu_dev, false, VHOST_USER_SET_OWNER);
  258. }
  259. static int vhost_user_get_features(struct virtio_uml_device *vu_dev,
  260. u64 *features)
  261. {
  262. int rc = vhost_user_send_no_payload(vu_dev, true,
  263. VHOST_USER_GET_FEATURES);
  264. if (rc)
  265. return rc;
  266. return vhost_user_recv_u64(vu_dev, features);
  267. }
  268. static int vhost_user_set_features(struct virtio_uml_device *vu_dev,
  269. u64 features)
  270. {
  271. return vhost_user_send_u64(vu_dev, VHOST_USER_SET_FEATURES, features);
  272. }
  273. static int vhost_user_get_protocol_features(struct virtio_uml_device *vu_dev,
  274. u64 *protocol_features)
  275. {
  276. int rc = vhost_user_send_no_payload(vu_dev, true,
  277. VHOST_USER_GET_PROTOCOL_FEATURES);
  278. if (rc)
  279. return rc;
  280. return vhost_user_recv_u64(vu_dev, protocol_features);
  281. }
  282. static int vhost_user_set_protocol_features(struct virtio_uml_device *vu_dev,
  283. u64 protocol_features)
  284. {
  285. return vhost_user_send_u64(vu_dev, VHOST_USER_SET_PROTOCOL_FEATURES,
  286. protocol_features);
  287. }
  288. static int vhost_user_get_queue_num(struct virtio_uml_device *vu_dev,
  289. u64 *queue_num)
  290. {
  291. int rc = vhost_user_send_no_payload(vu_dev, true,
  292. VHOST_USER_GET_QUEUE_NUM);
  293. if (rc)
  294. return rc;
  295. return vhost_user_recv_u64(vu_dev, queue_num);
  296. }
  297. static void vhost_user_reply(struct virtio_uml_device *vu_dev,
  298. struct vhost_user_msg *msg, int response)
  299. {
  300. struct vhost_user_msg reply = {
  301. .payload.integer = response,
  302. };
  303. size_t size = sizeof(reply.header) + sizeof(reply.payload.integer);
  304. int rc;
  305. reply.header = msg->header;
  306. reply.header.flags &= ~VHOST_USER_FLAG_NEED_REPLY;
  307. reply.header.flags |= VHOST_USER_FLAG_REPLY;
  308. reply.header.size = sizeof(reply.payload.integer);
  309. rc = full_sendmsg_fds(vu_dev->req_fd, &reply, size, NULL, 0);
  310. if (rc)
  311. vu_err(vu_dev,
  312. "sending reply to slave request failed: %d (size %zu)\n",
  313. rc, size);
  314. }
  315. static irqreturn_t vu_req_read_message(struct virtio_uml_device *vu_dev,
  316. struct time_travel_event *ev)
  317. {
  318. struct virtqueue *vq;
  319. int response = 1;
  320. struct {
  321. struct vhost_user_msg msg;
  322. u8 extra_payload[512];
  323. } msg;
  324. int rc;
  325. irqreturn_t irq_rc = IRQ_NONE;
  326. while (1) {
  327. rc = vhost_user_recv_req(vu_dev, &msg.msg,
  328. sizeof(msg.msg.payload) +
  329. sizeof(msg.extra_payload));
  330. if (rc)
  331. break;
  332. switch (msg.msg.header.request) {
  333. case VHOST_USER_SLAVE_CONFIG_CHANGE_MSG:
  334. vu_dev->config_changed_irq = true;
  335. response = 0;
  336. break;
  337. case VHOST_USER_SLAVE_VRING_CALL:
  338. virtio_device_for_each_vq((&vu_dev->vdev), vq) {
  339. if (vq->index == msg.msg.payload.vring_state.index) {
  340. response = 0;
  341. vu_dev->vq_irq_vq_map |= BIT_ULL(vq->index);
  342. break;
  343. }
  344. }
  345. break;
  346. case VHOST_USER_SLAVE_IOTLB_MSG:
  347. /* not supported - VIRTIO_F_ACCESS_PLATFORM */
  348. case VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG:
  349. /* not supported - VHOST_USER_PROTOCOL_F_HOST_NOTIFIER */
  350. default:
  351. vu_err(vu_dev, "unexpected slave request %d\n",
  352. msg.msg.header.request);
  353. }
  354. if (ev && !vu_dev->suspended)
  355. time_travel_add_irq_event(ev);
  356. if (msg.msg.header.flags & VHOST_USER_FLAG_NEED_REPLY)
  357. vhost_user_reply(vu_dev, &msg.msg, response);
  358. irq_rc = IRQ_HANDLED;
  359. }
  360. /* mask EAGAIN as we try non-blocking read until socket is empty */
  361. vu_dev->recv_rc = (rc == -EAGAIN) ? 0 : rc;
  362. return irq_rc;
  363. }
  364. static irqreturn_t vu_req_interrupt(int irq, void *data)
  365. {
  366. struct virtio_uml_device *vu_dev = data;
  367. irqreturn_t ret = IRQ_HANDLED;
  368. if (!um_irq_timetravel_handler_used())
  369. ret = vu_req_read_message(vu_dev, NULL);
  370. if (vu_dev->recv_rc) {
  371. vhost_user_check_reset(vu_dev, vu_dev->recv_rc);
  372. } else if (vu_dev->vq_irq_vq_map) {
  373. struct virtqueue *vq;
  374. virtio_device_for_each_vq((&vu_dev->vdev), vq) {
  375. if (vu_dev->vq_irq_vq_map & BIT_ULL(vq->index))
  376. vring_interrupt(0 /* ignored */, vq);
  377. }
  378. vu_dev->vq_irq_vq_map = 0;
  379. } else if (vu_dev->config_changed_irq) {
  380. virtio_config_changed(&vu_dev->vdev);
  381. vu_dev->config_changed_irq = false;
  382. }
  383. return ret;
  384. }
  385. static void vu_req_interrupt_comm_handler(int irq, int fd, void *data,
  386. struct time_travel_event *ev)
  387. {
  388. vu_req_read_message(data, ev);
  389. }
  390. static int vhost_user_init_slave_req(struct virtio_uml_device *vu_dev)
  391. {
  392. int rc, req_fds[2];
  393. /* Use a pipe for slave req fd, SIGIO is not supported for eventfd */
  394. rc = os_pipe(req_fds, true, true);
  395. if (rc < 0)
  396. return rc;
  397. vu_dev->req_fd = req_fds[0];
  398. rc = um_request_irq_tt(UM_IRQ_ALLOC, vu_dev->req_fd, IRQ_READ,
  399. vu_req_interrupt, IRQF_SHARED,
  400. vu_dev->pdev->name, vu_dev,
  401. vu_req_interrupt_comm_handler);
  402. if (rc < 0)
  403. goto err_close;
  404. vu_dev->irq = rc;
  405. rc = vhost_user_send_no_payload_fd(vu_dev, VHOST_USER_SET_SLAVE_REQ_FD,
  406. req_fds[1]);
  407. if (rc)
  408. goto err_free_irq;
  409. goto out;
  410. err_free_irq:
  411. um_free_irq(vu_dev->irq, vu_dev);
  412. err_close:
  413. os_close_file(req_fds[0]);
  414. out:
  415. /* Close unused write end of request fds */
  416. os_close_file(req_fds[1]);
  417. return rc;
  418. }
  419. static int vhost_user_init(struct virtio_uml_device *vu_dev)
  420. {
  421. int rc = vhost_user_set_owner(vu_dev);
  422. if (rc)
  423. return rc;
  424. rc = vhost_user_get_features(vu_dev, &vu_dev->features);
  425. if (rc)
  426. return rc;
  427. if (vu_dev->features & BIT_ULL(VHOST_USER_F_PROTOCOL_FEATURES)) {
  428. rc = vhost_user_get_protocol_features(vu_dev,
  429. &vu_dev->protocol_features);
  430. if (rc)
  431. return rc;
  432. vu_dev->protocol_features &= VHOST_USER_SUPPORTED_PROTOCOL_F;
  433. rc = vhost_user_set_protocol_features(vu_dev,
  434. vu_dev->protocol_features);
  435. if (rc)
  436. return rc;
  437. }
  438. if (vu_dev->protocol_features &
  439. BIT_ULL(VHOST_USER_PROTOCOL_F_SLAVE_REQ)) {
  440. rc = vhost_user_init_slave_req(vu_dev);
  441. if (rc)
  442. return rc;
  443. }
  444. if (vu_dev->protocol_features &
  445. BIT_ULL(VHOST_USER_PROTOCOL_F_MQ)) {
  446. rc = vhost_user_get_queue_num(vu_dev, &vu_dev->max_vqs);
  447. if (rc)
  448. return rc;
  449. } else {
  450. vu_dev->max_vqs = U64_MAX;
  451. }
  452. return 0;
  453. }
  454. static void vhost_user_get_config(struct virtio_uml_device *vu_dev,
  455. u32 offset, void *buf, u32 len)
  456. {
  457. u32 cfg_size = offset + len;
  458. struct vhost_user_msg *msg;
  459. size_t payload_size = sizeof(msg->payload.config) + cfg_size;
  460. size_t msg_size = sizeof(msg->header) + payload_size;
  461. int rc;
  462. if (!(vu_dev->protocol_features &
  463. BIT_ULL(VHOST_USER_PROTOCOL_F_CONFIG)))
  464. return;
  465. msg = kzalloc(msg_size, GFP_KERNEL);
  466. if (!msg)
  467. return;
  468. msg->header.request = VHOST_USER_GET_CONFIG;
  469. msg->header.size = payload_size;
  470. msg->payload.config.offset = 0;
  471. msg->payload.config.size = cfg_size;
  472. rc = vhost_user_send(vu_dev, true, msg, NULL, 0);
  473. if (rc) {
  474. vu_err(vu_dev, "sending VHOST_USER_GET_CONFIG failed: %d\n",
  475. rc);
  476. goto free;
  477. }
  478. rc = vhost_user_recv_resp(vu_dev, msg, msg_size);
  479. if (rc) {
  480. vu_err(vu_dev,
  481. "receiving VHOST_USER_GET_CONFIG response failed: %d\n",
  482. rc);
  483. goto free;
  484. }
  485. if (msg->header.size != payload_size ||
  486. msg->payload.config.size != cfg_size) {
  487. rc = -EPROTO;
  488. vu_err(vu_dev,
  489. "Invalid VHOST_USER_GET_CONFIG sizes (payload %d expected %zu, config %u expected %u)\n",
  490. msg->header.size, payload_size,
  491. msg->payload.config.size, cfg_size);
  492. goto free;
  493. }
  494. memcpy(buf, msg->payload.config.payload + offset, len);
  495. free:
  496. kfree(msg);
  497. }
  498. static void vhost_user_set_config(struct virtio_uml_device *vu_dev,
  499. u32 offset, const void *buf, u32 len)
  500. {
  501. struct vhost_user_msg *msg;
  502. size_t payload_size = sizeof(msg->payload.config) + len;
  503. size_t msg_size = sizeof(msg->header) + payload_size;
  504. int rc;
  505. if (!(vu_dev->protocol_features &
  506. BIT_ULL(VHOST_USER_PROTOCOL_F_CONFIG)))
  507. return;
  508. msg = kzalloc(msg_size, GFP_KERNEL);
  509. if (!msg)
  510. return;
  511. msg->header.request = VHOST_USER_SET_CONFIG;
  512. msg->header.size = payload_size;
  513. msg->payload.config.offset = offset;
  514. msg->payload.config.size = len;
  515. memcpy(msg->payload.config.payload, buf, len);
  516. rc = vhost_user_send(vu_dev, false, msg, NULL, 0);
  517. if (rc)
  518. vu_err(vu_dev, "sending VHOST_USER_SET_CONFIG failed: %d\n",
  519. rc);
  520. kfree(msg);
  521. }
  522. static int vhost_user_init_mem_region(u64 addr, u64 size, int *fd_out,
  523. struct vhost_user_mem_region *region_out)
  524. {
  525. unsigned long long mem_offset;
  526. int rc = phys_mapping(addr, &mem_offset);
  527. if (WARN(rc < 0, "phys_mapping of 0x%llx returned %d\n", addr, rc))
  528. return -EFAULT;
  529. *fd_out = rc;
  530. region_out->guest_addr = addr;
  531. region_out->user_addr = addr;
  532. region_out->size = size;
  533. region_out->mmap_offset = mem_offset;
  534. /* Ensure mapping is valid for the entire region */
  535. rc = phys_mapping(addr + size - 1, &mem_offset);
  536. if (WARN(rc != *fd_out, "phys_mapping of 0x%llx failed: %d != %d\n",
  537. addr + size - 1, rc, *fd_out))
  538. return -EFAULT;
  539. return 0;
  540. }
  541. static int vhost_user_set_mem_table(struct virtio_uml_device *vu_dev)
  542. {
  543. struct vhost_user_msg msg = {
  544. .header.request = VHOST_USER_SET_MEM_TABLE,
  545. .header.size = offsetof(typeof(msg.payload.mem_regions), regions[1]),
  546. .payload.mem_regions.num = 1,
  547. };
  548. unsigned long reserved = uml_reserved - uml_physmem;
  549. int fds[2];
  550. int rc;
  551. /*
  552. * This is a bit tricky, see also the comment with setup_physmem().
  553. *
  554. * Essentially, setup_physmem() uses a file to mmap() our physmem,
  555. * but the code and data we *already* have is omitted. To us, this
  556. * is no difference, since they both become part of our address
  557. * space and memory consumption. To somebody looking in from the
  558. * outside, however, it is different because the part of our memory
  559. * consumption that's already part of the binary (code/data) is not
  560. * mapped from the file, so it's not visible to another mmap from
  561. * the file descriptor.
  562. *
  563. * Thus, don't advertise this space to the vhost-user slave. This
  564. * means that the slave will likely abort or similar when we give
  565. * it an address from the hidden range, since it's not marked as
  566. * a valid address, but at least that way we detect the issue and
  567. * don't just have the slave read an all-zeroes buffer from the
  568. * shared memory file, or write something there that we can never
  569. * see (depending on the direction of the virtqueue traffic.)
  570. *
  571. * Since we usually don't want to use .text for virtio buffers,
  572. * this effectively means that you cannot use
  573. * 1) global variables, which are in the .bss and not in the shm
  574. * file-backed memory
  575. * 2) the stack in some processes, depending on where they have
  576. * their stack (or maybe only no interrupt stack?)
  577. *
  578. * The stack is already not typically valid for DMA, so this isn't
  579. * much of a restriction, but global variables might be encountered.
  580. *
  581. * It might be possible to fix it by copying around the data that's
  582. * between bss_start and where we map the file now, but it's not
  583. * something that you typically encounter with virtio drivers, so
  584. * it didn't seem worthwhile.
  585. */
  586. rc = vhost_user_init_mem_region(reserved, physmem_size - reserved,
  587. &fds[0],
  588. &msg.payload.mem_regions.regions[0]);
  589. if (rc < 0)
  590. return rc;
  591. return vhost_user_send(vu_dev, false, &msg, fds,
  592. msg.payload.mem_regions.num);
  593. }
  594. static int vhost_user_set_vring_state(struct virtio_uml_device *vu_dev,
  595. u32 request, u32 index, u32 num)
  596. {
  597. struct vhost_user_msg msg = {
  598. .header.request = request,
  599. .header.size = sizeof(msg.payload.vring_state),
  600. .payload.vring_state.index = index,
  601. .payload.vring_state.num = num,
  602. };
  603. return vhost_user_send(vu_dev, false, &msg, NULL, 0);
  604. }
  605. static int vhost_user_set_vring_num(struct virtio_uml_device *vu_dev,
  606. u32 index, u32 num)
  607. {
  608. return vhost_user_set_vring_state(vu_dev, VHOST_USER_SET_VRING_NUM,
  609. index, num);
  610. }
  611. static int vhost_user_set_vring_base(struct virtio_uml_device *vu_dev,
  612. u32 index, u32 offset)
  613. {
  614. return vhost_user_set_vring_state(vu_dev, VHOST_USER_SET_VRING_BASE,
  615. index, offset);
  616. }
  617. static int vhost_user_set_vring_addr(struct virtio_uml_device *vu_dev,
  618. u32 index, u64 desc, u64 used, u64 avail,
  619. u64 log)
  620. {
  621. struct vhost_user_msg msg = {
  622. .header.request = VHOST_USER_SET_VRING_ADDR,
  623. .header.size = sizeof(msg.payload.vring_addr),
  624. .payload.vring_addr.index = index,
  625. .payload.vring_addr.desc = desc,
  626. .payload.vring_addr.used = used,
  627. .payload.vring_addr.avail = avail,
  628. .payload.vring_addr.log = log,
  629. };
  630. return vhost_user_send(vu_dev, false, &msg, NULL, 0);
  631. }
  632. static int vhost_user_set_vring_fd(struct virtio_uml_device *vu_dev,
  633. u32 request, int index, int fd)
  634. {
  635. struct vhost_user_msg msg = {
  636. .header.request = request,
  637. .header.size = sizeof(msg.payload.integer),
  638. .payload.integer = index,
  639. };
  640. if (index & ~VHOST_USER_VRING_INDEX_MASK)
  641. return -EINVAL;
  642. if (fd < 0) {
  643. msg.payload.integer |= VHOST_USER_VRING_POLL_MASK;
  644. return vhost_user_send(vu_dev, false, &msg, NULL, 0);
  645. }
  646. return vhost_user_send(vu_dev, false, &msg, &fd, 1);
  647. }
  648. static int vhost_user_set_vring_call(struct virtio_uml_device *vu_dev,
  649. int index, int fd)
  650. {
  651. return vhost_user_set_vring_fd(vu_dev, VHOST_USER_SET_VRING_CALL,
  652. index, fd);
  653. }
  654. static int vhost_user_set_vring_kick(struct virtio_uml_device *vu_dev,
  655. int index, int fd)
  656. {
  657. return vhost_user_set_vring_fd(vu_dev, VHOST_USER_SET_VRING_KICK,
  658. index, fd);
  659. }
  660. static int vhost_user_set_vring_enable(struct virtio_uml_device *vu_dev,
  661. u32 index, bool enable)
  662. {
  663. if (!(vu_dev->features & BIT_ULL(VHOST_USER_F_PROTOCOL_FEATURES)))
  664. return 0;
  665. return vhost_user_set_vring_state(vu_dev, VHOST_USER_SET_VRING_ENABLE,
  666. index, enable);
  667. }
  668. /* Virtio interface */
  669. static bool vu_notify(struct virtqueue *vq)
  670. {
  671. struct virtio_uml_vq_info *info = vq->priv;
  672. const uint64_t n = 1;
  673. int rc;
  674. if (info->suspended)
  675. return true;
  676. time_travel_propagate_time();
  677. if (info->kick_fd < 0) {
  678. struct virtio_uml_device *vu_dev;
  679. vu_dev = to_virtio_uml_device(vq->vdev);
  680. return vhost_user_set_vring_state(vu_dev, VHOST_USER_VRING_KICK,
  681. vq->index, 0) == 0;
  682. }
  683. do {
  684. rc = os_write_file(info->kick_fd, &n, sizeof(n));
  685. } while (rc == -EINTR);
  686. return !WARN(rc != sizeof(n), "write returned %d\n", rc);
  687. }
  688. static irqreturn_t vu_interrupt(int irq, void *opaque)
  689. {
  690. struct virtqueue *vq = opaque;
  691. struct virtio_uml_vq_info *info = vq->priv;
  692. uint64_t n;
  693. int rc;
  694. irqreturn_t ret = IRQ_NONE;
  695. do {
  696. rc = os_read_file(info->call_fd, &n, sizeof(n));
  697. if (rc == sizeof(n))
  698. ret |= vring_interrupt(irq, vq);
  699. } while (rc == sizeof(n) || rc == -EINTR);
  700. WARN(rc != -EAGAIN, "read returned %d\n", rc);
  701. return ret;
  702. }
  703. static void vu_get(struct virtio_device *vdev, unsigned offset,
  704. void *buf, unsigned len)
  705. {
  706. struct virtio_uml_device *vu_dev = to_virtio_uml_device(vdev);
  707. vhost_user_get_config(vu_dev, offset, buf, len);
  708. }
  709. static void vu_set(struct virtio_device *vdev, unsigned offset,
  710. const void *buf, unsigned len)
  711. {
  712. struct virtio_uml_device *vu_dev = to_virtio_uml_device(vdev);
  713. vhost_user_set_config(vu_dev, offset, buf, len);
  714. }
  715. static u8 vu_get_status(struct virtio_device *vdev)
  716. {
  717. struct virtio_uml_device *vu_dev = to_virtio_uml_device(vdev);
  718. return vu_dev->status;
  719. }
  720. static void vu_set_status(struct virtio_device *vdev, u8 status)
  721. {
  722. struct virtio_uml_device *vu_dev = to_virtio_uml_device(vdev);
  723. vu_dev->status = status;
  724. }
  725. static void vu_reset(struct virtio_device *vdev)
  726. {
  727. struct virtio_uml_device *vu_dev = to_virtio_uml_device(vdev);
  728. vu_dev->status = 0;
  729. }
  730. static void vu_del_vq(struct virtqueue *vq)
  731. {
  732. struct virtio_uml_vq_info *info = vq->priv;
  733. if (info->call_fd >= 0) {
  734. struct virtio_uml_device *vu_dev;
  735. vu_dev = to_virtio_uml_device(vq->vdev);
  736. um_free_irq(vu_dev->irq, vq);
  737. os_close_file(info->call_fd);
  738. }
  739. if (info->kick_fd >= 0)
  740. os_close_file(info->kick_fd);
  741. vring_del_virtqueue(vq);
  742. kfree(info);
  743. }
  744. static void vu_del_vqs(struct virtio_device *vdev)
  745. {
  746. struct virtio_uml_device *vu_dev = to_virtio_uml_device(vdev);
  747. struct virtqueue *vq, *n;
  748. u64 features;
  749. /* Note: reverse order as a workaround to a decoding bug in snabb */
  750. list_for_each_entry_reverse(vq, &vdev->vqs, list)
  751. WARN_ON(vhost_user_set_vring_enable(vu_dev, vq->index, false));
  752. /* Ensure previous messages have been processed */
  753. WARN_ON(vhost_user_get_features(vu_dev, &features));
  754. list_for_each_entry_safe(vq, n, &vdev->vqs, list)
  755. vu_del_vq(vq);
  756. }
  757. static int vu_setup_vq_call_fd(struct virtio_uml_device *vu_dev,
  758. struct virtqueue *vq)
  759. {
  760. struct virtio_uml_vq_info *info = vq->priv;
  761. int call_fds[2];
  762. int rc, irq;
  763. /* no call FD needed/desired in this case */
  764. if (vu_dev->protocol_features &
  765. BIT_ULL(VHOST_USER_PROTOCOL_F_INBAND_NOTIFICATIONS) &&
  766. vu_dev->protocol_features &
  767. BIT_ULL(VHOST_USER_PROTOCOL_F_SLAVE_REQ)) {
  768. info->call_fd = -1;
  769. return 0;
  770. }
  771. /* Use a pipe for call fd, since SIGIO is not supported for eventfd */
  772. rc = os_pipe(call_fds, true, true);
  773. if (rc < 0)
  774. return rc;
  775. info->call_fd = call_fds[0];
  776. irq = um_request_irq(vu_dev->irq, info->call_fd, IRQ_READ,
  777. vu_interrupt, IRQF_SHARED, info->name, vq);
  778. if (irq < 0) {
  779. rc = irq;
  780. goto close_both;
  781. }
  782. rc = vhost_user_set_vring_call(vu_dev, vq->index, call_fds[1]);
  783. if (rc)
  784. goto release_irq;
  785. vu_dev->irq = irq;
  786. goto out;
  787. release_irq:
  788. um_free_irq(irq, vq);
  789. close_both:
  790. os_close_file(call_fds[0]);
  791. out:
  792. /* Close (unused) write end of call fds */
  793. os_close_file(call_fds[1]);
  794. return rc;
  795. }
  796. static struct virtqueue *vu_setup_vq(struct virtio_device *vdev,
  797. unsigned index, vq_callback_t *callback,
  798. const char *name, bool ctx)
  799. {
  800. struct virtio_uml_device *vu_dev = to_virtio_uml_device(vdev);
  801. struct platform_device *pdev = vu_dev->pdev;
  802. struct virtio_uml_vq_info *info;
  803. struct virtqueue *vq;
  804. int num = MAX_SUPPORTED_QUEUE_SIZE;
  805. int rc;
  806. info = kzalloc_obj(*info);
  807. if (!info) {
  808. rc = -ENOMEM;
  809. goto error_kzalloc;
  810. }
  811. snprintf(info->name, sizeof(info->name), "%s.%d-%s", pdev->name,
  812. pdev->id, name);
  813. vq = vring_create_virtqueue(index, num, PAGE_SIZE, vdev, true, true,
  814. ctx, vu_notify, callback, info->name);
  815. if (!vq) {
  816. rc = -ENOMEM;
  817. goto error_create;
  818. }
  819. vq->priv = info;
  820. vq->num_max = num;
  821. num = virtqueue_get_vring_size(vq);
  822. if (vu_dev->protocol_features &
  823. BIT_ULL(VHOST_USER_PROTOCOL_F_INBAND_NOTIFICATIONS)) {
  824. info->kick_fd = -1;
  825. } else {
  826. rc = os_eventfd(0, 0);
  827. if (rc < 0)
  828. goto error_kick;
  829. info->kick_fd = rc;
  830. }
  831. rc = vu_setup_vq_call_fd(vu_dev, vq);
  832. if (rc)
  833. goto error_call;
  834. rc = vhost_user_set_vring_num(vu_dev, index, num);
  835. if (rc)
  836. goto error_setup;
  837. rc = vhost_user_set_vring_base(vu_dev, index, 0);
  838. if (rc)
  839. goto error_setup;
  840. rc = vhost_user_set_vring_addr(vu_dev, index,
  841. virtqueue_get_desc_addr(vq),
  842. virtqueue_get_used_addr(vq),
  843. virtqueue_get_avail_addr(vq),
  844. (u64) -1);
  845. if (rc)
  846. goto error_setup;
  847. return vq;
  848. error_setup:
  849. if (info->call_fd >= 0) {
  850. um_free_irq(vu_dev->irq, vq);
  851. os_close_file(info->call_fd);
  852. }
  853. error_call:
  854. if (info->kick_fd >= 0)
  855. os_close_file(info->kick_fd);
  856. error_kick:
  857. vring_del_virtqueue(vq);
  858. error_create:
  859. kfree(info);
  860. error_kzalloc:
  861. return ERR_PTR(rc);
  862. }
  863. static int vu_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  864. struct virtqueue *vqs[],
  865. struct virtqueue_info vqs_info[],
  866. struct irq_affinity *desc)
  867. {
  868. struct virtio_uml_device *vu_dev = to_virtio_uml_device(vdev);
  869. int i, queue_idx = 0, rc;
  870. struct virtqueue *vq;
  871. /* not supported for now */
  872. if (WARN(nvqs > 64 || nvqs > vu_dev->max_vqs,
  873. "%d VQs requested, only up to 64 or %lld supported\n",
  874. nvqs, vu_dev->max_vqs))
  875. return -EINVAL;
  876. rc = vhost_user_set_mem_table(vu_dev);
  877. if (rc)
  878. return rc;
  879. for (i = 0; i < nvqs; ++i) {
  880. struct virtqueue_info *vqi = &vqs_info[i];
  881. if (!vqi->name) {
  882. vqs[i] = NULL;
  883. continue;
  884. }
  885. vqs[i] = vu_setup_vq(vdev, queue_idx++, vqi->callback,
  886. vqi->name, vqi->ctx);
  887. if (IS_ERR(vqs[i])) {
  888. rc = PTR_ERR(vqs[i]);
  889. goto error_setup;
  890. }
  891. }
  892. list_for_each_entry(vq, &vdev->vqs, list) {
  893. struct virtio_uml_vq_info *info = vq->priv;
  894. if (info->kick_fd >= 0) {
  895. rc = vhost_user_set_vring_kick(vu_dev, vq->index,
  896. info->kick_fd);
  897. if (rc)
  898. goto error_setup;
  899. }
  900. rc = vhost_user_set_vring_enable(vu_dev, vq->index, true);
  901. if (rc)
  902. goto error_setup;
  903. }
  904. return 0;
  905. error_setup:
  906. vu_del_vqs(vdev);
  907. return rc;
  908. }
  909. static u64 vu_get_features(struct virtio_device *vdev)
  910. {
  911. struct virtio_uml_device *vu_dev = to_virtio_uml_device(vdev);
  912. return vu_dev->features;
  913. }
  914. static int vu_finalize_features(struct virtio_device *vdev)
  915. {
  916. struct virtio_uml_device *vu_dev = to_virtio_uml_device(vdev);
  917. u64 supported = vdev->features & VHOST_USER_SUPPORTED_F;
  918. vring_transport_features(vdev);
  919. vu_dev->features = vdev->features | supported;
  920. return vhost_user_set_features(vu_dev, vu_dev->features);
  921. }
  922. static const char *vu_bus_name(struct virtio_device *vdev)
  923. {
  924. struct virtio_uml_device *vu_dev = to_virtio_uml_device(vdev);
  925. return vu_dev->pdev->name;
  926. }
  927. static const struct virtio_config_ops virtio_uml_config_ops = {
  928. .get = vu_get,
  929. .set = vu_set,
  930. .get_status = vu_get_status,
  931. .set_status = vu_set_status,
  932. .reset = vu_reset,
  933. .find_vqs = vu_find_vqs,
  934. .del_vqs = vu_del_vqs,
  935. .get_features = vu_get_features,
  936. .finalize_features = vu_finalize_features,
  937. .bus_name = vu_bus_name,
  938. };
  939. static void virtio_uml_release_dev(struct device *d)
  940. {
  941. struct virtio_device *vdev =
  942. container_of(d, struct virtio_device, dev);
  943. struct virtio_uml_device *vu_dev = to_virtio_uml_device(vdev);
  944. time_travel_propagate_time();
  945. /* might not have been opened due to not negotiating the feature */
  946. if (vu_dev->req_fd >= 0) {
  947. um_free_irq(vu_dev->irq, vu_dev);
  948. os_close_file(vu_dev->req_fd);
  949. }
  950. os_close_file(vu_dev->sock);
  951. kfree(vu_dev);
  952. }
  953. void virtio_uml_set_no_vq_suspend(struct virtio_device *vdev,
  954. bool no_vq_suspend)
  955. {
  956. struct virtio_uml_device *vu_dev = to_virtio_uml_device(vdev);
  957. if (WARN_ON(vdev->config != &virtio_uml_config_ops))
  958. return;
  959. vu_dev->no_vq_suspend = no_vq_suspend;
  960. dev_info(&vdev->dev, "%s VQ suspend\n", str_disabled_enabled(no_vq_suspend));
  961. }
  962. static void vu_of_conn_broken(struct work_struct *wk)
  963. {
  964. struct virtio_uml_platform_data *pdata;
  965. struct virtio_uml_device *vu_dev;
  966. pdata = container_of(wk, struct virtio_uml_platform_data, conn_broken_wk);
  967. vu_dev = platform_get_drvdata(pdata->pdev);
  968. virtio_break_device(&vu_dev->vdev);
  969. /*
  970. * We can't remove the device from the devicetree so the only thing we
  971. * can do is warn.
  972. */
  973. WARN_ON(1);
  974. }
  975. /* Platform device */
  976. static struct virtio_uml_platform_data *
  977. virtio_uml_create_pdata(struct platform_device *pdev)
  978. {
  979. struct device_node *np = pdev->dev.of_node;
  980. struct virtio_uml_platform_data *pdata;
  981. int ret;
  982. if (!np)
  983. return ERR_PTR(-EINVAL);
  984. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  985. if (!pdata)
  986. return ERR_PTR(-ENOMEM);
  987. INIT_WORK(&pdata->conn_broken_wk, vu_of_conn_broken);
  988. pdata->pdev = pdev;
  989. ret = of_property_read_string(np, "socket-path", &pdata->socket_path);
  990. if (ret)
  991. return ERR_PTR(ret);
  992. ret = of_property_read_u32(np, "virtio-device-id",
  993. &pdata->virtio_device_id);
  994. if (ret)
  995. return ERR_PTR(ret);
  996. return pdata;
  997. }
  998. static int virtio_uml_probe(struct platform_device *pdev)
  999. {
  1000. struct virtio_uml_platform_data *pdata = pdev->dev.platform_data;
  1001. struct virtio_uml_device *vu_dev;
  1002. int rc;
  1003. if (!pdata) {
  1004. pdata = virtio_uml_create_pdata(pdev);
  1005. if (IS_ERR(pdata))
  1006. return PTR_ERR(pdata);
  1007. }
  1008. vu_dev = kzalloc_obj(*vu_dev);
  1009. if (!vu_dev)
  1010. return -ENOMEM;
  1011. vu_dev->pdata = pdata;
  1012. vu_dev->vdev.dev.parent = &pdev->dev;
  1013. vu_dev->vdev.dev.release = virtio_uml_release_dev;
  1014. vu_dev->vdev.config = &virtio_uml_config_ops;
  1015. vu_dev->vdev.id.device = pdata->virtio_device_id;
  1016. vu_dev->vdev.id.vendor = VIRTIO_DEV_ANY_ID;
  1017. vu_dev->pdev = pdev;
  1018. vu_dev->req_fd = -1;
  1019. vu_dev->irq = UM_IRQ_ALLOC;
  1020. time_travel_propagate_time();
  1021. do {
  1022. rc = os_connect_socket(pdata->socket_path);
  1023. } while (rc == -EINTR);
  1024. if (rc < 0)
  1025. goto error_free;
  1026. vu_dev->sock = rc;
  1027. raw_spin_lock_init(&vu_dev->sock_lock);
  1028. rc = vhost_user_init(vu_dev);
  1029. if (rc)
  1030. goto error_init;
  1031. platform_set_drvdata(pdev, vu_dev);
  1032. device_set_wakeup_capable(&vu_dev->vdev.dev, true);
  1033. rc = register_virtio_device(&vu_dev->vdev);
  1034. if (rc) {
  1035. put_device(&vu_dev->vdev.dev);
  1036. return rc;
  1037. }
  1038. vu_dev->registered = 1;
  1039. return 0;
  1040. error_init:
  1041. os_close_file(vu_dev->sock);
  1042. error_free:
  1043. kfree(vu_dev);
  1044. return rc;
  1045. }
  1046. static void virtio_uml_remove(struct platform_device *pdev)
  1047. {
  1048. struct virtio_uml_device *vu_dev = platform_get_drvdata(pdev);
  1049. unregister_virtio_device(&vu_dev->vdev);
  1050. }
  1051. /* Command line device list */
  1052. static void vu_cmdline_release_dev(struct device *d)
  1053. {
  1054. }
  1055. static struct device vu_cmdline_parent = {
  1056. .init_name = "virtio-uml-cmdline",
  1057. .release = vu_cmdline_release_dev,
  1058. };
  1059. static DEFINE_MUTEX(vu_cmdline_lock);
  1060. static bool vu_cmdline_parent_registered;
  1061. static int vu_cmdline_id;
  1062. static int vu_unregister_cmdline_device(struct device *dev, void *data)
  1063. {
  1064. struct platform_device *pdev = to_platform_device(dev);
  1065. struct virtio_uml_platform_data *pdata = pdev->dev.platform_data;
  1066. kfree(pdata->socket_path);
  1067. platform_device_unregister(pdev);
  1068. return 0;
  1069. }
  1070. static void vu_conn_broken(struct work_struct *wk)
  1071. {
  1072. struct virtio_uml_platform_data *pdata;
  1073. struct virtio_uml_device *vu_dev;
  1074. pdata = container_of(wk, struct virtio_uml_platform_data, conn_broken_wk);
  1075. vu_dev = platform_get_drvdata(pdata->pdev);
  1076. virtio_break_device(&vu_dev->vdev);
  1077. vu_unregister_cmdline_device(&pdata->pdev->dev, NULL);
  1078. }
  1079. static int vu_cmdline_set_device(const char *device)
  1080. {
  1081. const char *ids = strchr(device, ':');
  1082. unsigned int virtio_device_id;
  1083. int processed, consumed, err;
  1084. char *socket_path;
  1085. struct virtio_uml_platform_data pdata, *ppdata;
  1086. struct platform_device *pdev;
  1087. if (!ids || ids == device)
  1088. return -EINVAL;
  1089. guard(mutex)(&vu_cmdline_lock);
  1090. processed = sscanf(ids, ":%u%n:%d%n",
  1091. &virtio_device_id, &consumed,
  1092. &vu_cmdline_id, &consumed);
  1093. if (processed < 1 || ids[consumed])
  1094. return -EINVAL;
  1095. if (!vu_cmdline_parent_registered) {
  1096. err = device_register(&vu_cmdline_parent);
  1097. if (err) {
  1098. pr_err("Failed to register parent device!\n");
  1099. put_device(&vu_cmdline_parent);
  1100. return err;
  1101. }
  1102. vu_cmdline_parent_registered = true;
  1103. }
  1104. socket_path = kmemdup_nul(device, ids - device, GFP_KERNEL);
  1105. if (!socket_path)
  1106. return -ENOMEM;
  1107. pdata.virtio_device_id = (u32) virtio_device_id;
  1108. pdata.socket_path = socket_path;
  1109. pr_info("Registering device virtio-uml.%d id=%d at %s\n",
  1110. vu_cmdline_id, virtio_device_id, socket_path);
  1111. pdev = platform_device_register_data(&vu_cmdline_parent, "virtio-uml",
  1112. vu_cmdline_id++, &pdata,
  1113. sizeof(pdata));
  1114. err = PTR_ERR_OR_ZERO(pdev);
  1115. if (err)
  1116. goto free;
  1117. ppdata = pdev->dev.platform_data;
  1118. ppdata->pdev = pdev;
  1119. INIT_WORK(&ppdata->conn_broken_wk, vu_conn_broken);
  1120. return 0;
  1121. free:
  1122. kfree(socket_path);
  1123. return err;
  1124. }
  1125. static int vu_cmdline_set(const char *device, const struct kernel_param *kp)
  1126. {
  1127. return vu_cmdline_set_device(device);
  1128. }
  1129. static int vu_cmdline_get_device(struct device *dev, void *data)
  1130. {
  1131. struct platform_device *pdev = to_platform_device(dev);
  1132. struct virtio_uml_platform_data *pdata = pdev->dev.platform_data;
  1133. char *buffer = data;
  1134. unsigned int len = strlen(buffer);
  1135. snprintf(buffer + len, PAGE_SIZE - len, "%s:%d:%d\n",
  1136. pdata->socket_path, pdata->virtio_device_id, pdev->id);
  1137. return 0;
  1138. }
  1139. static int vu_cmdline_get(char *buffer, const struct kernel_param *kp)
  1140. {
  1141. guard(mutex)(&vu_cmdline_lock);
  1142. buffer[0] = '\0';
  1143. if (vu_cmdline_parent_registered)
  1144. device_for_each_child(&vu_cmdline_parent, buffer,
  1145. vu_cmdline_get_device);
  1146. return strlen(buffer) + 1;
  1147. }
  1148. static const struct kernel_param_ops vu_cmdline_param_ops = {
  1149. .set = vu_cmdline_set,
  1150. .get = vu_cmdline_get,
  1151. };
  1152. device_param_cb(device, &vu_cmdline_param_ops, NULL, S_IRUSR);
  1153. __uml_help(vu_cmdline_param_ops,
  1154. "virtio_uml.device=<socket>:<virtio_id>[:<platform_id>]\n"
  1155. " Configure a virtio device over a vhost-user socket.\n"
  1156. " See virtio_ids.h for a list of possible virtio device id values.\n"
  1157. " Optionally use a specific platform_device id.\n\n"
  1158. );
  1159. static void vu_unregister_cmdline_devices(void)
  1160. {
  1161. guard(mutex)(&vu_cmdline_lock);
  1162. if (vu_cmdline_parent_registered) {
  1163. device_for_each_child(&vu_cmdline_parent, NULL,
  1164. vu_unregister_cmdline_device);
  1165. device_unregister(&vu_cmdline_parent);
  1166. vu_cmdline_parent_registered = false;
  1167. }
  1168. }
  1169. static int vu_mc_config(char *str, char **error_out)
  1170. {
  1171. if (*str != '=') {
  1172. *error_out = "Invalid config";
  1173. return -EINVAL;
  1174. }
  1175. str += 1;
  1176. return vu_cmdline_set_device(str);
  1177. }
  1178. static int vu_mc_id(char **str, int *start_out, int *end_out)
  1179. {
  1180. return -EOPNOTSUPP;
  1181. }
  1182. static int vu_mc_remove(int n, char **error_out)
  1183. {
  1184. return -EOPNOTSUPP;
  1185. }
  1186. static struct mc_device virtio_uml_mc = {
  1187. .list = LIST_HEAD_INIT(virtio_uml_mc.list),
  1188. .name = "virtio_uml.device",
  1189. .config = vu_mc_config,
  1190. .get_config = NULL,
  1191. .id = vu_mc_id,
  1192. .remove = vu_mc_remove,
  1193. };
  1194. static int __init virtio_uml_mc_init(void)
  1195. {
  1196. mconsole_register_dev(&virtio_uml_mc);
  1197. return 0;
  1198. }
  1199. late_initcall(virtio_uml_mc_init);
  1200. /* Platform driver */
  1201. static const struct of_device_id virtio_uml_match[] = {
  1202. { .compatible = "virtio,uml", },
  1203. { }
  1204. };
  1205. MODULE_DEVICE_TABLE(of, virtio_uml_match);
  1206. static int virtio_uml_suspend(struct platform_device *pdev, pm_message_t state)
  1207. {
  1208. struct virtio_uml_device *vu_dev = platform_get_drvdata(pdev);
  1209. if (!vu_dev->no_vq_suspend) {
  1210. struct virtqueue *vq;
  1211. virtio_device_for_each_vq((&vu_dev->vdev), vq) {
  1212. struct virtio_uml_vq_info *info = vq->priv;
  1213. info->suspended = true;
  1214. vhost_user_set_vring_enable(vu_dev, vq->index, false);
  1215. }
  1216. }
  1217. if (!device_may_wakeup(&vu_dev->vdev.dev)) {
  1218. vu_dev->suspended = true;
  1219. return 0;
  1220. }
  1221. return irq_set_irq_wake(vu_dev->irq, 1);
  1222. }
  1223. static int virtio_uml_resume(struct platform_device *pdev)
  1224. {
  1225. struct virtio_uml_device *vu_dev = platform_get_drvdata(pdev);
  1226. if (!vu_dev->no_vq_suspend) {
  1227. struct virtqueue *vq;
  1228. virtio_device_for_each_vq((&vu_dev->vdev), vq) {
  1229. struct virtio_uml_vq_info *info = vq->priv;
  1230. info->suspended = false;
  1231. vhost_user_set_vring_enable(vu_dev, vq->index, true);
  1232. }
  1233. }
  1234. vu_dev->suspended = false;
  1235. if (!device_may_wakeup(&vu_dev->vdev.dev))
  1236. return 0;
  1237. return irq_set_irq_wake(vu_dev->irq, 0);
  1238. }
  1239. static struct platform_driver virtio_uml_driver = {
  1240. .probe = virtio_uml_probe,
  1241. .remove = virtio_uml_remove,
  1242. .driver = {
  1243. .name = "virtio-uml",
  1244. .of_match_table = virtio_uml_match,
  1245. },
  1246. .suspend = virtio_uml_suspend,
  1247. .resume = virtio_uml_resume,
  1248. };
  1249. static int __init virtio_uml_init(void)
  1250. {
  1251. return platform_driver_register(&virtio_uml_driver);
  1252. }
  1253. static void __exit virtio_uml_exit(void)
  1254. {
  1255. platform_driver_unregister(&virtio_uml_driver);
  1256. vu_unregister_cmdline_devices();
  1257. }
  1258. module_init(virtio_uml_init);
  1259. module_exit(virtio_uml_exit);
  1260. __uml_exitcall(virtio_uml_exit);
  1261. MODULE_DESCRIPTION("UML driver for vhost-user virtio devices");
  1262. MODULE_LICENSE("GPL");