vmci_driver.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * VMware VMCI Driver
  4. *
  5. * Copyright (C) 2012 VMware, Inc. All rights reserved.
  6. */
  7. #include <linux/vmw_vmci_defs.h>
  8. #include <linux/vmw_vmci_api.h>
  9. #include <linux/atomic.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include "vmci_driver.h"
  14. #include "vmci_event.h"
  15. static bool vmci_disable_host;
  16. module_param_named(disable_host, vmci_disable_host, bool, 0);
  17. MODULE_PARM_DESC(disable_host,
  18. "Disable driver host personality (default=enabled)");
  19. static bool vmci_disable_guest;
  20. module_param_named(disable_guest, vmci_disable_guest, bool, 0);
  21. MODULE_PARM_DESC(disable_guest,
  22. "Disable driver guest personality (default=enabled)");
  23. static bool vmci_guest_personality_initialized;
  24. static bool vmci_host_personality_initialized;
  25. static DEFINE_MUTEX(vmci_vsock_mutex); /* protects vmci_vsock_transport_cb */
  26. static vmci_vsock_cb vmci_vsock_transport_cb;
  27. static bool vmci_vsock_cb_host_called;
  28. /*
  29. * vmci_get_context_id() - Gets the current context ID.
  30. *
  31. * Returns the current context ID. Note that since this is accessed only
  32. * from code running in the host, this always returns the host context ID.
  33. */
  34. u32 vmci_get_context_id(void)
  35. {
  36. if (vmci_guest_code_active())
  37. return vmci_get_vm_context_id();
  38. else if (vmci_host_code_active())
  39. return VMCI_HOST_CONTEXT_ID;
  40. return VMCI_INVALID_ID;
  41. }
  42. EXPORT_SYMBOL_GPL(vmci_get_context_id);
  43. /*
  44. * vmci_register_vsock_callback() - Register the VSOCK vmci_transport callback.
  45. *
  46. * The callback will be called when the first host or guest becomes active,
  47. * or if they are already active when this function is called.
  48. * To unregister the callback, call this function with NULL parameter.
  49. *
  50. * Returns 0 on success. -EBUSY if a callback is already registered.
  51. */
  52. int vmci_register_vsock_callback(vmci_vsock_cb callback)
  53. {
  54. int err = 0;
  55. mutex_lock(&vmci_vsock_mutex);
  56. if (vmci_vsock_transport_cb && callback) {
  57. err = -EBUSY;
  58. goto out;
  59. }
  60. vmci_vsock_transport_cb = callback;
  61. if (!vmci_vsock_transport_cb) {
  62. vmci_vsock_cb_host_called = false;
  63. goto out;
  64. }
  65. if (vmci_guest_code_active())
  66. vmci_vsock_transport_cb(false);
  67. if (vmci_host_users() > 0) {
  68. vmci_vsock_cb_host_called = true;
  69. vmci_vsock_transport_cb(true);
  70. }
  71. out:
  72. mutex_unlock(&vmci_vsock_mutex);
  73. return err;
  74. }
  75. EXPORT_SYMBOL_GPL(vmci_register_vsock_callback);
  76. void vmci_call_vsock_callback(bool is_host)
  77. {
  78. mutex_lock(&vmci_vsock_mutex);
  79. if (!vmci_vsock_transport_cb)
  80. goto out;
  81. /* In the host, this function could be called multiple times,
  82. * but we want to register it only once.
  83. */
  84. if (is_host) {
  85. if (vmci_vsock_cb_host_called)
  86. goto out;
  87. vmci_vsock_cb_host_called = true;
  88. }
  89. vmci_vsock_transport_cb(is_host);
  90. out:
  91. mutex_unlock(&vmci_vsock_mutex);
  92. }
  93. static int __init vmci_drv_init(void)
  94. {
  95. int vmci_err;
  96. int error;
  97. vmci_err = vmci_event_init();
  98. if (vmci_err < VMCI_SUCCESS) {
  99. pr_err("Failed to initialize VMCIEvent (result=%d)\n",
  100. vmci_err);
  101. return -EINVAL;
  102. }
  103. if (!vmci_disable_guest) {
  104. error = vmci_guest_init();
  105. if (error) {
  106. pr_warn("Failed to initialize guest personality (err=%d)\n",
  107. error);
  108. } else {
  109. vmci_guest_personality_initialized = true;
  110. pr_info("Guest personality initialized and is %s\n",
  111. vmci_guest_code_active() ?
  112. "active" : "inactive");
  113. }
  114. }
  115. if (!vmci_disable_host) {
  116. error = vmci_host_init();
  117. if (error) {
  118. pr_warn("Unable to initialize host personality (err=%d)\n",
  119. error);
  120. } else {
  121. vmci_host_personality_initialized = true;
  122. pr_info("Initialized host personality\n");
  123. }
  124. }
  125. if (!vmci_guest_personality_initialized &&
  126. !vmci_host_personality_initialized) {
  127. vmci_event_exit();
  128. return -ENODEV;
  129. }
  130. return 0;
  131. }
  132. module_init(vmci_drv_init);
  133. static void __exit vmci_drv_exit(void)
  134. {
  135. if (vmci_guest_personality_initialized)
  136. vmci_guest_exit();
  137. if (vmci_host_personality_initialized)
  138. vmci_host_exit();
  139. vmci_event_exit();
  140. }
  141. module_exit(vmci_drv_exit);
  142. MODULE_AUTHOR("VMware, Inc.");
  143. MODULE_DESCRIPTION("VMware Virtual Machine Communication Interface.");
  144. MODULE_VERSION("1.1.6.0-k");
  145. MODULE_LICENSE("GPL v2");