vmwgfx_msg.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /*
  3. * Copyright 2016 VMware, Inc., Palo Alto, CA., USA
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sub license, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial portions
  15. * of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  20. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  21. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  23. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. */
  26. #include <linux/objtool.h>
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/slab.h>
  30. #include <linux/cc_platform.h>
  31. #include <asm/hypervisor.h>
  32. #include <drm/drm_ioctl.h>
  33. #include "vmwgfx_drv.h"
  34. #include "vmwgfx_msg_x86.h"
  35. #include "vmwgfx_msg_arm64.h"
  36. #include "vmwgfx_mksstat.h"
  37. #define MESSAGE_STATUS_SUCCESS 0x0001
  38. #define MESSAGE_STATUS_DORECV 0x0002
  39. #define MESSAGE_STATUS_CPT 0x0010
  40. #define MESSAGE_STATUS_HB 0x0080
  41. #define RPCI_PROTOCOL_NUM 0x49435052
  42. #define GUESTMSG_FLAG_COOKIE 0x80000000
  43. #define RETRIES 3
  44. #define VMW_PORT_CMD_MSG 30
  45. #define VMW_PORT_CMD_HB_MSG 0
  46. #define VMW_PORT_CMD_OPEN_CHANNEL (MSG_TYPE_OPEN << 16 | VMW_PORT_CMD_MSG)
  47. #define VMW_PORT_CMD_CLOSE_CHANNEL (MSG_TYPE_CLOSE << 16 | VMW_PORT_CMD_MSG)
  48. #define VMW_PORT_CMD_SENDSIZE (MSG_TYPE_SENDSIZE << 16 | VMW_PORT_CMD_MSG)
  49. #define VMW_PORT_CMD_RECVSIZE (MSG_TYPE_RECVSIZE << 16 | VMW_PORT_CMD_MSG)
  50. #define VMW_PORT_CMD_RECVSTATUS (MSG_TYPE_RECVSTATUS << 16 | VMW_PORT_CMD_MSG)
  51. #define VMW_PORT_CMD_MKS_GUEST_STATS 85
  52. #define VMW_PORT_CMD_MKSGS_RESET (0 << 16 | VMW_PORT_CMD_MKS_GUEST_STATS)
  53. #define VMW_PORT_CMD_MKSGS_ADD_PPN (1 << 16 | VMW_PORT_CMD_MKS_GUEST_STATS)
  54. #define VMW_PORT_CMD_MKSGS_REMOVE_PPN (2 << 16 | VMW_PORT_CMD_MKS_GUEST_STATS)
  55. #define HIGH_WORD(X) ((X & 0xFFFF0000) >> 16)
  56. #define MAX_USER_MSG_LENGTH PAGE_SIZE
  57. static u32 vmw_msg_enabled = 1;
  58. enum rpc_msg_type {
  59. MSG_TYPE_OPEN,
  60. MSG_TYPE_SENDSIZE,
  61. MSG_TYPE_SENDPAYLOAD,
  62. MSG_TYPE_RECVSIZE,
  63. MSG_TYPE_RECVPAYLOAD,
  64. MSG_TYPE_RECVSTATUS,
  65. MSG_TYPE_CLOSE,
  66. };
  67. struct rpc_channel {
  68. u16 channel_id;
  69. u32 cookie_high;
  70. u32 cookie_low;
  71. };
  72. #if IS_ENABLED(CONFIG_DRM_VMWGFX_MKSSTATS)
  73. /* Kernel mksGuestStats counter names and desciptions; same order as enum mksstat_kern_stats_t */
  74. static const char* const mksstat_kern_name_desc[MKSSTAT_KERN_COUNT][2] =
  75. {
  76. { "vmw_execbuf_ioctl", "vmw_execbuf_ioctl" },
  77. { "vmw_cotable_resize", "vmw_cotable_resize" },
  78. };
  79. #endif
  80. /**
  81. * vmw_open_channel
  82. *
  83. * @channel: RPC channel
  84. * @protocol:
  85. *
  86. * Returns: 0 on success
  87. */
  88. static int vmw_open_channel(struct rpc_channel *channel, unsigned int protocol)
  89. {
  90. u32 ecx, edx, esi, edi;
  91. vmware_hypercall6(VMW_PORT_CMD_OPEN_CHANNEL,
  92. (protocol | GUESTMSG_FLAG_COOKIE), 0,
  93. &ecx, &edx, &esi, &edi);
  94. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
  95. return -EINVAL;
  96. channel->channel_id = HIGH_WORD(edx);
  97. channel->cookie_high = esi;
  98. channel->cookie_low = edi;
  99. return 0;
  100. }
  101. /**
  102. * vmw_close_channel
  103. *
  104. * @channel: RPC channel
  105. *
  106. * Returns: 0 on success
  107. */
  108. static int vmw_close_channel(struct rpc_channel *channel)
  109. {
  110. u32 ecx;
  111. vmware_hypercall5(VMW_PORT_CMD_CLOSE_CHANNEL,
  112. 0, channel->channel_id << 16,
  113. channel->cookie_high,
  114. channel->cookie_low,
  115. &ecx);
  116. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
  117. return -EINVAL;
  118. return 0;
  119. }
  120. /**
  121. * vmw_port_hb_out - Send the message payload either through the
  122. * high-bandwidth port if available, or through the backdoor otherwise.
  123. * @channel: The rpc channel.
  124. * @msg: NULL-terminated message.
  125. * @hb: Whether the high-bandwidth port is available.
  126. *
  127. * Return: The port status.
  128. */
  129. static unsigned long vmw_port_hb_out(struct rpc_channel *channel,
  130. const char *msg, bool hb)
  131. {
  132. u32 ebx, ecx;
  133. unsigned long msg_len = strlen(msg);
  134. /* HB port can't access encrypted memory. */
  135. if (hb && !cc_platform_has(CC_ATTR_MEM_ENCRYPT)) {
  136. vmware_hypercall_hb_out(
  137. (MESSAGE_STATUS_SUCCESS << 16) | VMW_PORT_CMD_HB_MSG,
  138. msg_len,
  139. channel->channel_id << 16,
  140. (uintptr_t) msg, channel->cookie_low,
  141. channel->cookie_high,
  142. &ebx);
  143. return ebx;
  144. }
  145. /* HB port not available. Send the message 4 bytes at a time. */
  146. ecx = MESSAGE_STATUS_SUCCESS << 16;
  147. while (msg_len && (HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS)) {
  148. unsigned int bytes = min_t(size_t, msg_len, 4);
  149. unsigned long word = 0;
  150. memcpy(&word, msg, bytes);
  151. msg_len -= bytes;
  152. msg += bytes;
  153. vmware_hypercall5(VMW_PORT_CMD_MSG |
  154. (MSG_TYPE_SENDPAYLOAD << 16),
  155. word, channel->channel_id << 16,
  156. channel->cookie_high,
  157. channel->cookie_low,
  158. &ecx);
  159. }
  160. return ecx;
  161. }
  162. /**
  163. * vmw_port_hb_in - Receive the message payload either through the
  164. * high-bandwidth port if available, or through the backdoor otherwise.
  165. * @channel: The rpc channel.
  166. * @reply: Pointer to buffer holding reply.
  167. * @reply_len: Length of the reply.
  168. * @hb: Whether the high-bandwidth port is available.
  169. *
  170. * Return: The port status.
  171. */
  172. static unsigned long vmw_port_hb_in(struct rpc_channel *channel, char *reply,
  173. unsigned long reply_len, bool hb)
  174. {
  175. u32 ebx, ecx, edx;
  176. /* HB port can't access encrypted memory */
  177. if (hb && !cc_platform_has(CC_ATTR_MEM_ENCRYPT)) {
  178. vmware_hypercall_hb_in(
  179. (MESSAGE_STATUS_SUCCESS << 16) | VMW_PORT_CMD_HB_MSG,
  180. reply_len,
  181. channel->channel_id << 16,
  182. channel->cookie_high,
  183. (uintptr_t) reply, channel->cookie_low,
  184. &ebx);
  185. return ebx;
  186. }
  187. /* HB port not available. Retrieve the message 4 bytes at a time. */
  188. ecx = MESSAGE_STATUS_SUCCESS << 16;
  189. while (reply_len) {
  190. unsigned int bytes = min_t(unsigned long, reply_len, 4);
  191. vmware_hypercall7(VMW_PORT_CMD_MSG |
  192. (MSG_TYPE_RECVPAYLOAD << 16),
  193. MESSAGE_STATUS_SUCCESS,
  194. channel->channel_id << 16,
  195. channel->cookie_high,
  196. channel->cookie_low,
  197. &ebx, &ecx, &edx);
  198. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0)
  199. break;
  200. memcpy(reply, &ebx, bytes);
  201. reply_len -= bytes;
  202. reply += bytes;
  203. }
  204. return ecx;
  205. }
  206. /**
  207. * vmw_send_msg: Sends a message to the host
  208. *
  209. * @channel: RPC channel
  210. * @msg: NULL terminated string
  211. *
  212. * Returns: 0 on success
  213. */
  214. static int vmw_send_msg(struct rpc_channel *channel, const char *msg)
  215. {
  216. u32 ebx, ecx;
  217. size_t msg_len = strlen(msg);
  218. int retries = 0;
  219. while (retries < RETRIES) {
  220. retries++;
  221. vmware_hypercall5(VMW_PORT_CMD_SENDSIZE,
  222. msg_len, channel->channel_id << 16,
  223. channel->cookie_high,
  224. channel->cookie_low,
  225. &ecx);
  226. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0) {
  227. /* Expected success. Give up. */
  228. return -EINVAL;
  229. }
  230. /* Send msg */
  231. ebx = vmw_port_hb_out(channel, msg,
  232. !!(HIGH_WORD(ecx) & MESSAGE_STATUS_HB));
  233. if ((HIGH_WORD(ebx) & MESSAGE_STATUS_SUCCESS) != 0) {
  234. return 0;
  235. } else if ((HIGH_WORD(ebx) & MESSAGE_STATUS_CPT) != 0) {
  236. /* A checkpoint occurred. Retry. */
  237. continue;
  238. } else {
  239. break;
  240. }
  241. }
  242. return -EINVAL;
  243. }
  244. STACK_FRAME_NON_STANDARD_FP(vmw_send_msg);
  245. /**
  246. * vmw_recv_msg: Receives a message from the host
  247. *
  248. * Note: It is the caller's responsibility to call kfree() on msg.
  249. *
  250. * @channel: channel opened by vmw_open_channel
  251. * @msg: [OUT] message received from the host
  252. * @msg_len: message length
  253. */
  254. static int vmw_recv_msg(struct rpc_channel *channel, void **msg,
  255. size_t *msg_len)
  256. {
  257. u32 ebx, ecx, edx;
  258. char *reply;
  259. size_t reply_len;
  260. int retries = 0;
  261. *msg_len = 0;
  262. *msg = NULL;
  263. while (retries < RETRIES) {
  264. retries++;
  265. vmware_hypercall7(VMW_PORT_CMD_RECVSIZE,
  266. 0, channel->channel_id << 16,
  267. channel->cookie_high,
  268. channel->cookie_low,
  269. &ebx, &ecx, &edx);
  270. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0) {
  271. DRM_ERROR("Failed to get reply size for host message.\n");
  272. return -EINVAL;
  273. }
  274. /* No reply available. This is okay. */
  275. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_DORECV) == 0)
  276. return 0;
  277. reply_len = ebx;
  278. reply = kzalloc(reply_len + 1, GFP_KERNEL);
  279. if (!reply) {
  280. DRM_ERROR("Cannot allocate memory for host message reply.\n");
  281. return -ENOMEM;
  282. }
  283. /* Receive buffer */
  284. ebx = vmw_port_hb_in(channel, reply, reply_len,
  285. !!(HIGH_WORD(ecx) & MESSAGE_STATUS_HB));
  286. if ((HIGH_WORD(ebx) & MESSAGE_STATUS_SUCCESS) == 0) {
  287. kfree(reply);
  288. reply = NULL;
  289. if ((HIGH_WORD(ebx) & MESSAGE_STATUS_CPT) != 0) {
  290. /* A checkpoint occurred. Retry. */
  291. continue;
  292. }
  293. return -EINVAL;
  294. }
  295. reply[reply_len] = '\0';
  296. vmware_hypercall5(VMW_PORT_CMD_RECVSTATUS,
  297. MESSAGE_STATUS_SUCCESS,
  298. channel->channel_id << 16,
  299. channel->cookie_high,
  300. channel->cookie_low,
  301. &ecx);
  302. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_SUCCESS) == 0) {
  303. kfree(reply);
  304. reply = NULL;
  305. if ((HIGH_WORD(ecx) & MESSAGE_STATUS_CPT) != 0) {
  306. /* A checkpoint occurred. Retry. */
  307. continue;
  308. }
  309. return -EINVAL;
  310. }
  311. break;
  312. }
  313. if (!reply)
  314. return -EINVAL;
  315. *msg_len = reply_len;
  316. *msg = reply;
  317. return 0;
  318. }
  319. STACK_FRAME_NON_STANDARD(vmw_recv_msg);
  320. /**
  321. * vmw_host_get_guestinfo: Gets a GuestInfo parameter
  322. *
  323. * Gets the value of a GuestInfo.* parameter. The value returned will be in
  324. * a string, and it is up to the caller to post-process.
  325. *
  326. * @guest_info_param: Parameter to get, e.g. GuestInfo.svga.gl3
  327. * @buffer: if NULL, *reply_len will contain reply size.
  328. * @length: size of the reply_buf. Set to size of reply upon return
  329. *
  330. * Returns: 0 on success
  331. */
  332. int vmw_host_get_guestinfo(const char *guest_info_param,
  333. char *buffer, size_t *length)
  334. {
  335. struct rpc_channel channel;
  336. char *msg, *reply = NULL;
  337. size_t reply_len = 0;
  338. if (!vmw_msg_enabled)
  339. return -ENODEV;
  340. if (!guest_info_param || !length)
  341. return -EINVAL;
  342. msg = kasprintf(GFP_KERNEL, "info-get %s", guest_info_param);
  343. if (!msg) {
  344. DRM_ERROR("Cannot allocate memory to get guest info \"%s\".",
  345. guest_info_param);
  346. return -ENOMEM;
  347. }
  348. if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM))
  349. goto out_open;
  350. if (vmw_send_msg(&channel, msg) ||
  351. vmw_recv_msg(&channel, (void *) &reply, &reply_len))
  352. goto out_msg;
  353. vmw_close_channel(&channel);
  354. if (buffer && reply && reply_len > 0) {
  355. /* Remove reply code, which are the first 2 characters of
  356. * the reply
  357. */
  358. reply_len = max(reply_len - 2, (size_t) 0);
  359. reply_len = min(reply_len, *length);
  360. if (reply_len > 0)
  361. memcpy(buffer, reply + 2, reply_len);
  362. }
  363. *length = reply_len;
  364. kfree(reply);
  365. kfree(msg);
  366. return 0;
  367. out_msg:
  368. vmw_close_channel(&channel);
  369. kfree(reply);
  370. out_open:
  371. *length = 0;
  372. kfree(msg);
  373. DRM_ERROR("Failed to get guest info \"%s\".", guest_info_param);
  374. return -EINVAL;
  375. }
  376. /**
  377. * vmw_host_printf: Sends a log message to the host
  378. *
  379. * @fmt: Regular printf format string and arguments
  380. *
  381. * Returns: 0 on success
  382. */
  383. __printf(1, 2)
  384. int vmw_host_printf(const char *fmt, ...)
  385. {
  386. va_list ap;
  387. struct rpc_channel channel;
  388. char *msg;
  389. char *log;
  390. int ret = 0;
  391. if (!vmw_msg_enabled)
  392. return -ENODEV;
  393. if (!fmt)
  394. return ret;
  395. va_start(ap, fmt);
  396. log = kvasprintf(GFP_KERNEL, fmt, ap);
  397. va_end(ap);
  398. if (!log) {
  399. DRM_ERROR("Cannot allocate memory for the log message.\n");
  400. return -ENOMEM;
  401. }
  402. msg = kasprintf(GFP_KERNEL, "log %s", log);
  403. if (!msg) {
  404. DRM_ERROR("Cannot allocate memory for host log message.\n");
  405. kfree(log);
  406. return -ENOMEM;
  407. }
  408. if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM))
  409. goto out_open;
  410. if (vmw_send_msg(&channel, msg))
  411. goto out_msg;
  412. vmw_close_channel(&channel);
  413. kfree(msg);
  414. kfree(log);
  415. return 0;
  416. out_msg:
  417. vmw_close_channel(&channel);
  418. out_open:
  419. kfree(msg);
  420. kfree(log);
  421. DRM_ERROR("Failed to send host log message.\n");
  422. return -EINVAL;
  423. }
  424. /**
  425. * vmw_msg_ioctl: Sends and receveives a message to/from host from/to user-space
  426. *
  427. * Sends a message from user-space to host.
  428. * Can also receive a result from host and return that to user-space.
  429. *
  430. * @dev: Identifies the drm device.
  431. * @data: Pointer to the ioctl argument.
  432. * @file_priv: Identifies the caller.
  433. * Return: Zero on success, negative error code on error.
  434. */
  435. int vmw_msg_ioctl(struct drm_device *dev, void *data,
  436. struct drm_file *file_priv)
  437. {
  438. struct drm_vmw_msg_arg *arg =
  439. (struct drm_vmw_msg_arg *)data;
  440. struct rpc_channel channel;
  441. char *msg;
  442. int length;
  443. msg = kmalloc(MAX_USER_MSG_LENGTH, GFP_KERNEL);
  444. if (!msg) {
  445. DRM_ERROR("Cannot allocate memory for log message.\n");
  446. return -ENOMEM;
  447. }
  448. length = strncpy_from_user(msg, (void __user *)((unsigned long)arg->send),
  449. MAX_USER_MSG_LENGTH);
  450. if (length < 0 || length >= MAX_USER_MSG_LENGTH) {
  451. DRM_ERROR("Userspace message access failure.\n");
  452. kfree(msg);
  453. return -EINVAL;
  454. }
  455. if (vmw_open_channel(&channel, RPCI_PROTOCOL_NUM)) {
  456. DRM_ERROR("Failed to open channel.\n");
  457. goto out_open;
  458. }
  459. if (vmw_send_msg(&channel, msg)) {
  460. DRM_ERROR("Failed to send message to host.\n");
  461. goto out_msg;
  462. }
  463. if (!arg->send_only) {
  464. char *reply = NULL;
  465. size_t reply_len = 0;
  466. if (vmw_recv_msg(&channel, (void *) &reply, &reply_len)) {
  467. DRM_ERROR("Failed to receive message from host.\n");
  468. goto out_msg;
  469. }
  470. if (reply && reply_len > 0) {
  471. if (copy_to_user((void __user *)((unsigned long)arg->receive),
  472. reply, reply_len)) {
  473. DRM_ERROR("Failed to copy message to userspace.\n");
  474. kfree(reply);
  475. goto out_msg;
  476. }
  477. arg->receive_len = (__u32)reply_len;
  478. }
  479. kfree(reply);
  480. }
  481. vmw_close_channel(&channel);
  482. kfree(msg);
  483. return 0;
  484. out_msg:
  485. vmw_close_channel(&channel);
  486. out_open:
  487. kfree(msg);
  488. return -EINVAL;
  489. }
  490. /**
  491. * reset_ppn_array: Resets a PPN64 array to INVALID_PPN64 content
  492. *
  493. * @arr: Array to reset.
  494. * @size: Array length.
  495. */
  496. static inline void reset_ppn_array(PPN64 *arr, size_t size)
  497. {
  498. size_t i;
  499. BUG_ON(!arr || size == 0);
  500. for (i = 0; i < size; ++i)
  501. arr[i] = INVALID_PPN64;
  502. }
  503. /**
  504. * hypervisor_ppn_reset_all: Removes all mksGuestStat instance descriptors from
  505. * the hypervisor. All related pages should be subsequently unpinned or freed.
  506. *
  507. */
  508. static inline void hypervisor_ppn_reset_all(void)
  509. {
  510. vmware_hypercall1(VMW_PORT_CMD_MKSGS_RESET, 0);
  511. }
  512. /**
  513. * hypervisor_ppn_add: Adds a single mksGuestStat instance descriptor to the
  514. * hypervisor. Any related userspace pages should be pinned in advance.
  515. *
  516. * @pfn: Physical page number of the instance descriptor
  517. */
  518. static inline void hypervisor_ppn_add(PPN64 pfn)
  519. {
  520. vmware_hypercall1(VMW_PORT_CMD_MKSGS_ADD_PPN, (unsigned long)pfn);
  521. }
  522. /**
  523. * hypervisor_ppn_remove: Removes a single mksGuestStat instance descriptor from
  524. * the hypervisor. All related pages should be subsequently unpinned or freed.
  525. *
  526. * @pfn: Physical page number of the instance descriptor
  527. */
  528. static inline void hypervisor_ppn_remove(PPN64 pfn)
  529. {
  530. vmware_hypercall1(VMW_PORT_CMD_MKSGS_REMOVE_PPN, (unsigned long)pfn);
  531. }
  532. #if IS_ENABLED(CONFIG_DRM_VMWGFX_MKSSTATS)
  533. /* Order of the total number of pages used for kernel-internal mksGuestStat; at least 2 */
  534. #define MKSSTAT_KERNEL_PAGES_ORDER 2
  535. /* Header to the text description of mksGuestStat instance descriptor */
  536. #define MKSSTAT_KERNEL_DESCRIPTION "vmwgfx"
  537. /**
  538. * mksstat_init_record_time: Initializes an MKSGuestStatCounterTime-based record
  539. * for the respective mksGuestStat index.
  540. *
  541. * @stat_idx: Index of the MKSGuestStatCounterTime-based mksGuestStat record.
  542. * @pstat: Pointer to array of MKSGuestStatCounterTime.
  543. * @pinfo: Pointer to array of MKSGuestStatInfoEntry.
  544. * @pstrs: Pointer to current end of the name/description sequence.
  545. * Return: Pointer to the new end of the names/description sequence.
  546. */
  547. static inline char *mksstat_init_record_time(mksstat_kern_stats_t stat_idx,
  548. MKSGuestStatCounterTime *pstat, MKSGuestStatInfoEntry *pinfo, char *pstrs)
  549. {
  550. char *const pstrd = pstrs + strlen(mksstat_kern_name_desc[stat_idx][0]) + 1;
  551. strcpy(pstrs, mksstat_kern_name_desc[stat_idx][0]);
  552. strcpy(pstrd, mksstat_kern_name_desc[stat_idx][1]);
  553. pinfo[stat_idx].name.s = pstrs;
  554. pinfo[stat_idx].description.s = pstrd;
  555. pinfo[stat_idx].flags = MKS_GUEST_STAT_FLAG_TIME;
  556. pinfo[stat_idx].stat.counterTime = &pstat[stat_idx];
  557. return pstrd + strlen(mksstat_kern_name_desc[stat_idx][1]) + 1;
  558. }
  559. /**
  560. * mksstat_init_kern_id: Creates a single mksGuestStat instance descriptor and
  561. * kernel-internal counters. Adds PFN mapping to the hypervisor.
  562. *
  563. * Create a single mksGuestStat instance descriptor and corresponding structures
  564. * for all kernel-internal counters. The corresponding PFNs are mapped with the
  565. * hypervisor.
  566. *
  567. * @ppage: Output pointer to page containing the instance descriptor.
  568. * Return: Zero on success, negative error code on error.
  569. */
  570. static int mksstat_init_kern_id(struct page **ppage)
  571. {
  572. MKSGuestStatInstanceDescriptor *pdesc;
  573. MKSGuestStatCounterTime *pstat;
  574. MKSGuestStatInfoEntry *pinfo;
  575. char *pstrs, *pstrs_acc;
  576. /* Allocate pages for the kernel-internal instance descriptor */
  577. struct page *page = alloc_pages(GFP_KERNEL | __GFP_ZERO, MKSSTAT_KERNEL_PAGES_ORDER);
  578. if (!page)
  579. return -ENOMEM;
  580. pdesc = page_address(page);
  581. pstat = vmw_mksstat_get_kern_pstat(pdesc);
  582. pinfo = vmw_mksstat_get_kern_pinfo(pdesc);
  583. pstrs = vmw_mksstat_get_kern_pstrs(pdesc);
  584. /* Set up all kernel-internal counters and corresponding structures */
  585. pstrs_acc = pstrs;
  586. pstrs_acc = mksstat_init_record_time(MKSSTAT_KERN_EXECBUF, pstat, pinfo, pstrs_acc);
  587. pstrs_acc = mksstat_init_record_time(MKSSTAT_KERN_COTABLE_RESIZE, pstat, pinfo, pstrs_acc);
  588. /* Add new counters above, in their order of appearance in mksstat_kern_stats_t */
  589. BUG_ON(pstrs_acc - pstrs > PAGE_SIZE);
  590. /* Set up the kernel-internal instance descriptor */
  591. pdesc->reservedMBZ = 0;
  592. pdesc->statStartVA = (uintptr_t)pstat;
  593. pdesc->strsStartVA = (uintptr_t)pstrs;
  594. pdesc->statLength = sizeof(*pstat) * MKSSTAT_KERN_COUNT;
  595. pdesc->infoLength = sizeof(*pinfo) * MKSSTAT_KERN_COUNT;
  596. pdesc->strsLength = pstrs_acc - pstrs;
  597. snprintf(pdesc->description, ARRAY_SIZE(pdesc->description) - 1, "%s pid=%d",
  598. MKSSTAT_KERNEL_DESCRIPTION, current->pid);
  599. pdesc->statPPNs[0] = page_to_pfn(virt_to_page(pstat));
  600. reset_ppn_array(pdesc->statPPNs + 1, ARRAY_SIZE(pdesc->statPPNs) - 1);
  601. pdesc->infoPPNs[0] = page_to_pfn(virt_to_page(pinfo));
  602. reset_ppn_array(pdesc->infoPPNs + 1, ARRAY_SIZE(pdesc->infoPPNs) - 1);
  603. pdesc->strsPPNs[0] = page_to_pfn(virt_to_page(pstrs));
  604. reset_ppn_array(pdesc->strsPPNs + 1, ARRAY_SIZE(pdesc->strsPPNs) - 1);
  605. *ppage = page;
  606. hypervisor_ppn_add((PPN64)page_to_pfn(page));
  607. return 0;
  608. }
  609. /**
  610. * vmw_mksstat_get_kern_slot: Acquires a slot for a single kernel-internal
  611. * mksGuestStat instance descriptor.
  612. *
  613. * Find a slot for a single kernel-internal mksGuestStat instance descriptor.
  614. * In case no such was already present, allocate a new one and set up a kernel-
  615. * internal mksGuestStat instance descriptor for the former.
  616. *
  617. * @pid: Process for which a slot is sought.
  618. * @dev_priv: Identifies the drm private device.
  619. * Return: Non-negative slot on success, negative error code on error.
  620. */
  621. int vmw_mksstat_get_kern_slot(pid_t pid, struct vmw_private *dev_priv)
  622. {
  623. const size_t base = (u32)hash_32(pid, MKSSTAT_CAPACITY_LOG2);
  624. size_t i;
  625. for (i = 0; i < ARRAY_SIZE(dev_priv->mksstat_kern_pids); ++i) {
  626. const size_t slot = (i + base) % ARRAY_SIZE(dev_priv->mksstat_kern_pids);
  627. /* Check if an instance descriptor for this pid is already present */
  628. if (pid == (pid_t)atomic_read(&dev_priv->mksstat_kern_pids[slot]))
  629. return (int)slot;
  630. /* Set up a new instance descriptor for this pid */
  631. if (!atomic_cmpxchg(&dev_priv->mksstat_kern_pids[slot], 0, MKSSTAT_PID_RESERVED)) {
  632. const int ret = mksstat_init_kern_id(&dev_priv->mksstat_kern_pages[slot]);
  633. if (!ret) {
  634. /* Reset top-timer tracking for this slot */
  635. dev_priv->mksstat_kern_top_timer[slot] = MKSSTAT_KERN_COUNT;
  636. atomic_set(&dev_priv->mksstat_kern_pids[slot], pid);
  637. return (int)slot;
  638. }
  639. atomic_set(&dev_priv->mksstat_kern_pids[slot], 0);
  640. return ret;
  641. }
  642. }
  643. return -ENOSPC;
  644. }
  645. #endif
  646. /**
  647. * vmw_mksstat_cleanup_descriptor: Frees a single userspace-originating
  648. * mksGuestStat instance-descriptor page and unpins all related user pages.
  649. *
  650. * Unpin all user pages realated to this instance descriptor and free
  651. * the instance-descriptor page itself.
  652. *
  653. * @page: Page of the instance descriptor.
  654. */
  655. static void vmw_mksstat_cleanup_descriptor(struct page *page)
  656. {
  657. MKSGuestStatInstanceDescriptor *pdesc = page_address(page);
  658. size_t i;
  659. for (i = 0; i < ARRAY_SIZE(pdesc->statPPNs) && pdesc->statPPNs[i] != INVALID_PPN64; ++i)
  660. unpin_user_page(pfn_to_page(pdesc->statPPNs[i]));
  661. for (i = 0; i < ARRAY_SIZE(pdesc->infoPPNs) && pdesc->infoPPNs[i] != INVALID_PPN64; ++i)
  662. unpin_user_page(pfn_to_page(pdesc->infoPPNs[i]));
  663. for (i = 0; i < ARRAY_SIZE(pdesc->strsPPNs) && pdesc->strsPPNs[i] != INVALID_PPN64; ++i)
  664. unpin_user_page(pfn_to_page(pdesc->strsPPNs[i]));
  665. __free_page(page);
  666. }
  667. /**
  668. * vmw_mksstat_remove_all: Resets all mksGuestStat instance descriptors
  669. * from the hypervisor.
  670. *
  671. * Discard all hypervisor PFN mappings, containing active mksGuestState instance
  672. * descriptors, unpin the related userspace pages and free the related kernel pages.
  673. *
  674. * @dev_priv: Identifies the drm private device.
  675. * Return: Zero on success, negative error code on error.
  676. */
  677. int vmw_mksstat_remove_all(struct vmw_private *dev_priv)
  678. {
  679. int ret = 0;
  680. size_t i;
  681. /* Discard all PFN mappings with the hypervisor */
  682. hypervisor_ppn_reset_all();
  683. /* Discard all userspace-originating instance descriptors and unpin all related pages */
  684. for (i = 0; i < ARRAY_SIZE(dev_priv->mksstat_user_pids); ++i) {
  685. const pid_t pid0 = (pid_t)atomic_read(&dev_priv->mksstat_user_pids[i]);
  686. if (!pid0)
  687. continue;
  688. if (pid0 != MKSSTAT_PID_RESERVED) {
  689. const pid_t pid1 = atomic_cmpxchg(&dev_priv->mksstat_user_pids[i], pid0, MKSSTAT_PID_RESERVED);
  690. if (!pid1)
  691. continue;
  692. if (pid1 == pid0) {
  693. struct page *const page = dev_priv->mksstat_user_pages[i];
  694. BUG_ON(!page);
  695. dev_priv->mksstat_user_pages[i] = NULL;
  696. atomic_set(&dev_priv->mksstat_user_pids[i], 0);
  697. vmw_mksstat_cleanup_descriptor(page);
  698. continue;
  699. }
  700. }
  701. ret = -EAGAIN;
  702. }
  703. #if IS_ENABLED(CONFIG_DRM_VMWGFX_MKSSTATS)
  704. /* Discard all kernel-internal instance descriptors and free all related pages */
  705. for (i = 0; i < ARRAY_SIZE(dev_priv->mksstat_kern_pids); ++i) {
  706. const pid_t pid0 = (pid_t)atomic_read(&dev_priv->mksstat_kern_pids[i]);
  707. if (!pid0)
  708. continue;
  709. if (pid0 != MKSSTAT_PID_RESERVED) {
  710. const pid_t pid1 = atomic_cmpxchg(&dev_priv->mksstat_kern_pids[i], pid0, MKSSTAT_PID_RESERVED);
  711. if (!pid1)
  712. continue;
  713. if (pid1 == pid0) {
  714. struct page *const page = dev_priv->mksstat_kern_pages[i];
  715. BUG_ON(!page);
  716. dev_priv->mksstat_kern_pages[i] = NULL;
  717. atomic_set(&dev_priv->mksstat_kern_pids[i], 0);
  718. __free_pages(page, MKSSTAT_KERNEL_PAGES_ORDER);
  719. continue;
  720. }
  721. }
  722. ret = -EAGAIN;
  723. }
  724. #endif
  725. return ret;
  726. }
  727. /**
  728. * vmw_mksstat_reset_ioctl: Resets all mksGuestStat instance descriptors
  729. * from the hypervisor.
  730. *
  731. * Discard all hypervisor PFN mappings, containing active mksGuestStat instance
  732. * descriptors, unpin the related userspace pages and free the related kernel pages.
  733. *
  734. * @dev: Identifies the drm device.
  735. * @data: Pointer to the ioctl argument.
  736. * @file_priv: Identifies the caller; unused.
  737. * Return: Zero on success, negative error code on error.
  738. */
  739. int vmw_mksstat_reset_ioctl(struct drm_device *dev, void *data,
  740. struct drm_file *file_priv)
  741. {
  742. struct vmw_private *const dev_priv = vmw_priv(dev);
  743. return vmw_mksstat_remove_all(dev_priv);
  744. }
  745. /**
  746. * vmw_mksstat_add_ioctl: Creates a single userspace-originating mksGuestStat
  747. * instance descriptor and registers that with the hypervisor.
  748. *
  749. * Create a hypervisor PFN mapping, containing a single mksGuestStat instance
  750. * descriptor and pin the corresponding userspace pages.
  751. *
  752. * @dev: Identifies the drm device.
  753. * @data: Pointer to the ioctl argument.
  754. * @file_priv: Identifies the caller; unused.
  755. * Return: Zero on success, negative error code on error.
  756. */
  757. int vmw_mksstat_add_ioctl(struct drm_device *dev, void *data,
  758. struct drm_file *file_priv)
  759. {
  760. struct drm_vmw_mksstat_add_arg *arg =
  761. (struct drm_vmw_mksstat_add_arg *) data;
  762. struct vmw_private *const dev_priv = vmw_priv(dev);
  763. const size_t num_pages_stat = PFN_UP(arg->stat_len);
  764. const size_t num_pages_info = PFN_UP(arg->info_len);
  765. const size_t num_pages_strs = PFN_UP(arg->strs_len);
  766. long desc_len;
  767. long nr_pinned_stat;
  768. long nr_pinned_info;
  769. long nr_pinned_strs;
  770. MKSGuestStatInstanceDescriptor *pdesc;
  771. struct page *page = NULL;
  772. struct page **pages_stat = NULL;
  773. struct page **pages_info = NULL;
  774. struct page **pages_strs = NULL;
  775. size_t i, slot;
  776. int ret_err = -ENOMEM;
  777. arg->id = -1;
  778. if (!arg->stat || !arg->info || !arg->strs)
  779. return -EINVAL;
  780. if (!arg->stat_len || !arg->info_len || !arg->strs_len)
  781. return -EINVAL;
  782. if (!arg->description)
  783. return -EINVAL;
  784. if (num_pages_stat > ARRAY_SIZE(pdesc->statPPNs) ||
  785. num_pages_info > ARRAY_SIZE(pdesc->infoPPNs) ||
  786. num_pages_strs > ARRAY_SIZE(pdesc->strsPPNs))
  787. return -EINVAL;
  788. /* Find an available slot in the mksGuestStats user array and reserve it */
  789. for (slot = 0; slot < ARRAY_SIZE(dev_priv->mksstat_user_pids); ++slot)
  790. if (!atomic_cmpxchg(&dev_priv->mksstat_user_pids[slot], 0, MKSSTAT_PID_RESERVED))
  791. break;
  792. if (slot == ARRAY_SIZE(dev_priv->mksstat_user_pids))
  793. return -ENOSPC;
  794. BUG_ON(dev_priv->mksstat_user_pages[slot]);
  795. /* Allocate statically-sized temp arrays for pages -- too big to keep in frame */
  796. pages_stat = (struct page **) kmalloc_objs(*pages_stat,
  797. ARRAY_SIZE(pdesc->statPPNs) + ARRAY_SIZE(pdesc->infoPPNs) + ARRAY_SIZE(pdesc->strsPPNs));
  798. if (!pages_stat)
  799. goto err_nomem;
  800. pages_info = pages_stat + ARRAY_SIZE(pdesc->statPPNs);
  801. pages_strs = pages_info + ARRAY_SIZE(pdesc->infoPPNs);
  802. /* Allocate a page for the instance descriptor */
  803. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  804. if (!page)
  805. goto err_nomem;
  806. /* Set up the instance descriptor */
  807. pdesc = page_address(page);
  808. pdesc->reservedMBZ = 0;
  809. pdesc->statStartVA = arg->stat;
  810. pdesc->strsStartVA = arg->strs;
  811. pdesc->statLength = arg->stat_len;
  812. pdesc->infoLength = arg->info_len;
  813. pdesc->strsLength = arg->strs_len;
  814. desc_len = strncpy_from_user(pdesc->description, u64_to_user_ptr(arg->description),
  815. ARRAY_SIZE(pdesc->description) - 1);
  816. if (desc_len < 0) {
  817. ret_err = -EFAULT;
  818. goto err_nomem;
  819. }
  820. reset_ppn_array(pdesc->statPPNs, ARRAY_SIZE(pdesc->statPPNs));
  821. reset_ppn_array(pdesc->infoPPNs, ARRAY_SIZE(pdesc->infoPPNs));
  822. reset_ppn_array(pdesc->strsPPNs, ARRAY_SIZE(pdesc->strsPPNs));
  823. /* Pin mksGuestStat user pages and store those in the instance descriptor */
  824. nr_pinned_stat = pin_user_pages_fast(arg->stat, num_pages_stat, FOLL_LONGTERM, pages_stat);
  825. if (num_pages_stat != nr_pinned_stat)
  826. goto err_pin_stat;
  827. for (i = 0; i < num_pages_stat; ++i)
  828. pdesc->statPPNs[i] = page_to_pfn(pages_stat[i]);
  829. nr_pinned_info = pin_user_pages_fast(arg->info, num_pages_info, FOLL_LONGTERM, pages_info);
  830. if (num_pages_info != nr_pinned_info)
  831. goto err_pin_info;
  832. for (i = 0; i < num_pages_info; ++i)
  833. pdesc->infoPPNs[i] = page_to_pfn(pages_info[i]);
  834. nr_pinned_strs = pin_user_pages_fast(arg->strs, num_pages_strs, FOLL_LONGTERM, pages_strs);
  835. if (num_pages_strs != nr_pinned_strs)
  836. goto err_pin_strs;
  837. for (i = 0; i < num_pages_strs; ++i)
  838. pdesc->strsPPNs[i] = page_to_pfn(pages_strs[i]);
  839. /* Send the descriptor to the host via a hypervisor call. The mksGuestStat
  840. pages will remain in use until the user requests a matching remove stats
  841. or a stats reset occurs. */
  842. hypervisor_ppn_add((PPN64)page_to_pfn(page));
  843. dev_priv->mksstat_user_pages[slot] = page;
  844. atomic_set(&dev_priv->mksstat_user_pids[slot], task_pgrp_vnr(current));
  845. arg->id = slot;
  846. DRM_DEV_INFO(dev->dev, "pid=%d arg.description='%.*s' id=%zu\n", current->pid, (int)desc_len, pdesc->description, slot);
  847. kfree(pages_stat);
  848. return 0;
  849. err_pin_strs:
  850. if (nr_pinned_strs > 0)
  851. unpin_user_pages(pages_strs, nr_pinned_strs);
  852. err_pin_info:
  853. if (nr_pinned_info > 0)
  854. unpin_user_pages(pages_info, nr_pinned_info);
  855. err_pin_stat:
  856. if (nr_pinned_stat > 0)
  857. unpin_user_pages(pages_stat, nr_pinned_stat);
  858. err_nomem:
  859. atomic_set(&dev_priv->mksstat_user_pids[slot], 0);
  860. if (page)
  861. __free_page(page);
  862. kfree(pages_stat);
  863. return ret_err;
  864. }
  865. /**
  866. * vmw_mksstat_remove_ioctl: Removes a single userspace-originating mksGuestStat
  867. * instance descriptor from the hypervisor.
  868. *
  869. * Discard a hypervisor PFN mapping, containing a single mksGuestStat instance
  870. * descriptor and unpin the corresponding userspace pages.
  871. *
  872. * @dev: Identifies the drm device.
  873. * @data: Pointer to the ioctl argument.
  874. * @file_priv: Identifies the caller; unused.
  875. * Return: Zero on success, negative error code on error.
  876. */
  877. int vmw_mksstat_remove_ioctl(struct drm_device *dev, void *data,
  878. struct drm_file *file_priv)
  879. {
  880. struct drm_vmw_mksstat_remove_arg *arg =
  881. (struct drm_vmw_mksstat_remove_arg *) data;
  882. struct vmw_private *const dev_priv = vmw_priv(dev);
  883. const size_t slot = arg->id;
  884. pid_t pgid, pid;
  885. if (slot >= ARRAY_SIZE(dev_priv->mksstat_user_pids))
  886. return -EINVAL;
  887. DRM_DEV_INFO(dev->dev, "pid=%d arg.id=%zu\n", current->pid, slot);
  888. pgid = task_pgrp_vnr(current);
  889. pid = atomic_cmpxchg(&dev_priv->mksstat_user_pids[slot], pgid, MKSSTAT_PID_RESERVED);
  890. if (!pid)
  891. return 0;
  892. if (pid == pgid) {
  893. struct page *const page = dev_priv->mksstat_user_pages[slot];
  894. BUG_ON(!page);
  895. dev_priv->mksstat_user_pages[slot] = NULL;
  896. atomic_set(&dev_priv->mksstat_user_pids[slot], 0);
  897. hypervisor_ppn_remove((PPN64)page_to_pfn(page));
  898. vmw_mksstat_cleanup_descriptor(page);
  899. return 0;
  900. }
  901. return -EAGAIN;
  902. }
  903. /**
  904. * vmw_disable_backdoor: Disables all backdoor communication
  905. * with the hypervisor.
  906. */
  907. void vmw_disable_backdoor(void)
  908. {
  909. vmw_msg_enabled = 0;
  910. }