vgic-debug.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Copyright (C) 2016 Linaro
  3. * Author: Christoffer Dall <christoffer.dall@linaro.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/cpu.h>
  18. #include <linux/debugfs.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/kvm_host.h>
  21. #include <linux/seq_file.h>
  22. #include <kvm/arm_vgic.h>
  23. #include <asm/kvm_mmu.h>
  24. #include "vgic.h"
  25. /*
  26. * Structure to control looping through the entire vgic state. We start at
  27. * zero for each field and move upwards. So, if dist_id is 0 we print the
  28. * distributor info. When dist_id is 1, we have already printed it and move
  29. * on.
  30. *
  31. * When vcpu_id < nr_cpus we print the vcpu info until vcpu_id == nr_cpus and
  32. * so on.
  33. */
  34. struct vgic_state_iter {
  35. int nr_cpus;
  36. int nr_spis;
  37. int dist_id;
  38. int vcpu_id;
  39. int intid;
  40. };
  41. static void iter_next(struct vgic_state_iter *iter)
  42. {
  43. if (iter->dist_id == 0) {
  44. iter->dist_id++;
  45. return;
  46. }
  47. iter->intid++;
  48. if (iter->intid == VGIC_NR_PRIVATE_IRQS &&
  49. ++iter->vcpu_id < iter->nr_cpus)
  50. iter->intid = 0;
  51. }
  52. static void iter_init(struct kvm *kvm, struct vgic_state_iter *iter,
  53. loff_t pos)
  54. {
  55. int nr_cpus = atomic_read(&kvm->online_vcpus);
  56. memset(iter, 0, sizeof(*iter));
  57. iter->nr_cpus = nr_cpus;
  58. iter->nr_spis = kvm->arch.vgic.nr_spis;
  59. /* Fast forward to the right position if needed */
  60. while (pos--)
  61. iter_next(iter);
  62. }
  63. static bool end_of_vgic(struct vgic_state_iter *iter)
  64. {
  65. return iter->dist_id > 0 &&
  66. iter->vcpu_id == iter->nr_cpus &&
  67. (iter->intid - VGIC_NR_PRIVATE_IRQS) == iter->nr_spis;
  68. }
  69. static void *vgic_debug_start(struct seq_file *s, loff_t *pos)
  70. {
  71. struct kvm *kvm = (struct kvm *)s->private;
  72. struct vgic_state_iter *iter;
  73. mutex_lock(&kvm->lock);
  74. iter = kvm->arch.vgic.iter;
  75. if (iter) {
  76. iter = ERR_PTR(-EBUSY);
  77. goto out;
  78. }
  79. iter = kmalloc(sizeof(*iter), GFP_KERNEL);
  80. if (!iter) {
  81. iter = ERR_PTR(-ENOMEM);
  82. goto out;
  83. }
  84. iter_init(kvm, iter, *pos);
  85. kvm->arch.vgic.iter = iter;
  86. if (end_of_vgic(iter))
  87. iter = NULL;
  88. out:
  89. mutex_unlock(&kvm->lock);
  90. return iter;
  91. }
  92. static void *vgic_debug_next(struct seq_file *s, void *v, loff_t *pos)
  93. {
  94. struct kvm *kvm = (struct kvm *)s->private;
  95. struct vgic_state_iter *iter = kvm->arch.vgic.iter;
  96. ++*pos;
  97. iter_next(iter);
  98. if (end_of_vgic(iter))
  99. iter = NULL;
  100. return iter;
  101. }
  102. static void vgic_debug_stop(struct seq_file *s, void *v)
  103. {
  104. struct kvm *kvm = (struct kvm *)s->private;
  105. struct vgic_state_iter *iter;
  106. /*
  107. * If the seq file wasn't properly opened, there's nothing to clearn
  108. * up.
  109. */
  110. if (IS_ERR(v))
  111. return;
  112. mutex_lock(&kvm->lock);
  113. iter = kvm->arch.vgic.iter;
  114. kfree(iter);
  115. kvm->arch.vgic.iter = NULL;
  116. mutex_unlock(&kvm->lock);
  117. }
  118. static void print_dist_state(struct seq_file *s, struct vgic_dist *dist)
  119. {
  120. seq_printf(s, "Distributor\n");
  121. seq_printf(s, "===========\n");
  122. seq_printf(s, "vgic_model:\t%s\n",
  123. (dist->vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) ?
  124. "GICv3" : "GICv2");
  125. seq_printf(s, "nr_spis:\t%d\n", dist->nr_spis);
  126. seq_printf(s, "enabled:\t%d\n", dist->enabled);
  127. seq_printf(s, "\n");
  128. seq_printf(s, "P=pending_latch, L=line_level, A=active\n");
  129. seq_printf(s, "E=enabled, H=hw, C=config (level=1, edge=0)\n");
  130. }
  131. static void print_header(struct seq_file *s, struct vgic_irq *irq,
  132. struct kvm_vcpu *vcpu)
  133. {
  134. int id = 0;
  135. char *hdr = "SPI ";
  136. if (vcpu) {
  137. hdr = "VCPU";
  138. id = vcpu->vcpu_id;
  139. }
  140. seq_printf(s, "\n");
  141. seq_printf(s, "%s%2d TYP ID TGT_ID PLAEHC HWID TARGET SRC PRI VCPU_ID\n", hdr, id);
  142. seq_printf(s, "---------------------------------------------------------------\n");
  143. }
  144. static void print_irq_state(struct seq_file *s, struct vgic_irq *irq,
  145. struct kvm_vcpu *vcpu)
  146. {
  147. char *type;
  148. if (irq->intid < VGIC_NR_SGIS)
  149. type = "SGI";
  150. else if (irq->intid < VGIC_NR_PRIVATE_IRQS)
  151. type = "PPI";
  152. else
  153. type = "SPI";
  154. if (irq->intid ==0 || irq->intid == VGIC_NR_PRIVATE_IRQS)
  155. print_header(s, irq, vcpu);
  156. seq_printf(s, " %s %4d "
  157. " %2d "
  158. "%d%d%d%d%d%d "
  159. "%8d "
  160. "%8x "
  161. " %2x "
  162. "%3d "
  163. " %2d "
  164. "\n",
  165. type, irq->intid,
  166. (irq->target_vcpu) ? irq->target_vcpu->vcpu_id : -1,
  167. irq->pending_latch,
  168. irq->line_level,
  169. irq->active,
  170. irq->enabled,
  171. irq->hw,
  172. irq->config == VGIC_CONFIG_LEVEL,
  173. irq->hwintid,
  174. irq->mpidr,
  175. irq->source,
  176. irq->priority,
  177. (irq->vcpu) ? irq->vcpu->vcpu_id : -1);
  178. }
  179. static int vgic_debug_show(struct seq_file *s, void *v)
  180. {
  181. struct kvm *kvm = (struct kvm *)s->private;
  182. struct vgic_state_iter *iter = (struct vgic_state_iter *)v;
  183. struct vgic_irq *irq;
  184. struct kvm_vcpu *vcpu = NULL;
  185. if (iter->dist_id == 0) {
  186. print_dist_state(s, &kvm->arch.vgic);
  187. return 0;
  188. }
  189. if (!kvm->arch.vgic.initialized)
  190. return 0;
  191. if (iter->vcpu_id < iter->nr_cpus) {
  192. vcpu = kvm_get_vcpu(kvm, iter->vcpu_id);
  193. irq = &vcpu->arch.vgic_cpu.private_irqs[iter->intid];
  194. } else {
  195. irq = &kvm->arch.vgic.spis[iter->intid - VGIC_NR_PRIVATE_IRQS];
  196. }
  197. spin_lock(&irq->irq_lock);
  198. print_irq_state(s, irq, vcpu);
  199. spin_unlock(&irq->irq_lock);
  200. return 0;
  201. }
  202. static const struct seq_operations vgic_debug_seq_ops = {
  203. .start = vgic_debug_start,
  204. .next = vgic_debug_next,
  205. .stop = vgic_debug_stop,
  206. .show = vgic_debug_show
  207. };
  208. static int debug_open(struct inode *inode, struct file *file)
  209. {
  210. int ret;
  211. ret = seq_open(file, &vgic_debug_seq_ops);
  212. if (!ret) {
  213. struct seq_file *seq;
  214. /* seq_open will have modified file->private_data */
  215. seq = file->private_data;
  216. seq->private = inode->i_private;
  217. }
  218. return ret;
  219. };
  220. static const struct file_operations vgic_debug_fops = {
  221. .owner = THIS_MODULE,
  222. .open = debug_open,
  223. .read = seq_read,
  224. .llseek = seq_lseek,
  225. .release = seq_release
  226. };
  227. int vgic_debug_init(struct kvm *kvm)
  228. {
  229. if (!kvm->debugfs_dentry)
  230. return -ENOENT;
  231. if (!debugfs_create_file("vgic-state", 0444,
  232. kvm->debugfs_dentry,
  233. kvm,
  234. &vgic_debug_fops))
  235. return -ENOMEM;
  236. return 0;
  237. }
  238. int vgic_debug_destroy(struct kvm *kvm)
  239. {
  240. return 0;
  241. }