cspace.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (c) 2023 Agustina Arzille.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  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. * Capability spaces.
  18. */
  19. #ifndef KERN_CAP_SPACE_H
  20. #define KERN_CAP_SPACE_H
  21. #include <errno.h>
  22. #include <kern/capability.h>
  23. #include <kern/cspace_types.h>
  24. #include <kern/rcu.h>
  25. static inline void
  26. cspace_init (struct cspace *sp)
  27. {
  28. rdxtree_init (&sp->tree, RDXTREE_KEY_ALLOC);
  29. adaptive_lock_init (&sp->lock);
  30. }
  31. static inline struct cap_base*
  32. cspace_get (struct cspace *sp, int cap_idx)
  33. {
  34. if (cap_idx < 0)
  35. return (NULL);
  36. CPU_INTR_GUARD ();
  37. RCU_GUARD ();
  38. struct cap_base *cap = rdxtree_lookup (&sp->tree, cap_idx);
  39. if (cap)
  40. cap_base_acq (cap);
  41. return (cap);
  42. }
  43. static inline int
  44. cspace_add_free_locked (struct cspace *sp, struct cap_base *cap,
  45. int flags __unused)
  46. {
  47. rdxtree_key_t cap_idx;
  48. int rv = rdxtree_insert_alloc (&sp->tree, cap, &cap_idx);
  49. if (rv < 0)
  50. return (-ENOMEM);
  51. cap_base_acq (cap);
  52. return ((int)cap_idx);
  53. }
  54. static inline int
  55. cspace_add_free (struct cspace *sp, struct cap_base *cap,
  56. int flags __unused)
  57. {
  58. ADAPTIVE_LOCK_GUARD (&sp->lock);
  59. return (cspace_add_free_locked (sp, cap, flags));
  60. }
  61. static inline int
  62. cspace_rem_locked (struct cspace *sp, int cap_idx)
  63. {
  64. if (cap_idx < 0)
  65. return (EBADF);
  66. struct cap_base *cap = rdxtree_remove (&sp->tree, cap_idx);
  67. if (! cap)
  68. return (EINVAL);
  69. CPU_INTR_GUARD ();
  70. cap_base_rel (cap);
  71. return (0);
  72. }
  73. static inline int
  74. cspace_rem (struct cspace *sp, int cap_idx)
  75. {
  76. ADAPTIVE_LOCK_GUARD (&sp->lock);
  77. return (cspace_rem_locked (sp, cap_idx));
  78. }
  79. static inline int
  80. cspace_dup (struct cspace *sp, int cap_idx)
  81. {
  82. _Auto cap = cspace_get (sp, cap_idx);
  83. if (! cap)
  84. return (-EBADF);
  85. int new_idx = cspace_add_free (sp, cap, 0);
  86. if (new_idx < 0)
  87. cap_base_rel (cap);
  88. return (new_idx);
  89. }
  90. static inline int
  91. cspace_dup3 (struct cspace *sp, int cap_idx, int new_idx,
  92. int flags __unused)
  93. {
  94. if (cap_idx < 0)
  95. return (EBADF);
  96. ADAPTIVE_LOCK_GUARD (&sp->lock);
  97. struct cap_base *cap = rdxtree_lookup (&sp->tree, cap_idx);
  98. if (! cap)
  99. return (EBADF);
  100. void **slot;
  101. int rv = rdxtree_insert_slot (&sp->tree, new_idx, cap, &slot);
  102. if (rv == EBUSY)
  103. // Release the older capability.
  104. cap_base_rel (rdxtree_replace_slot (slot, cap));
  105. else if (rv)
  106. return (ENOMEM);
  107. cap_base_acq (cap);
  108. return (0);
  109. }
  110. static inline void
  111. cspace_destroy (struct cspace *sp)
  112. {
  113. struct rdxtree_iter iter;
  114. struct cap_base *cap;
  115. rdxtree_for_each (&sp->tree, &iter, cap)
  116. cap_base_rel (cap);
  117. rdxtree_remove_all (&sp->tree);
  118. }
  119. #define cspace_self() ((struct cspace *)&thread_self()->xtask->caps)
  120. #endif