task.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (c) 2012-2019 Richard Braun.
  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. #ifndef KERN_TASK_H
  18. #define KERN_TASK_H
  19. #include <stdint.h>
  20. #include <kern/atomic.h>
  21. #include <kern/bulletin.h>
  22. #include <kern/cspace_types.h>
  23. #include <kern/init.h>
  24. #include <kern/kuid.h>
  25. #include <kern/list.h>
  26. #include <kern/log.h>
  27. #include <kern/spinlock.h>
  28. #include <kern/stream.h>
  29. #include <kern/thread.h>
  30. #include <kern/work.h>
  31. #include <vm/map.h>
  32. // Task name buffer size.
  33. #define TASK_NAME_SIZE 32
  34. // Task structure.
  35. struct task
  36. {
  37. struct kuid_head kuid;
  38. struct spinlock lock;
  39. struct list node;
  40. struct list threads;
  41. struct vm_map *map;
  42. struct cspace caps;
  43. union
  44. {
  45. struct bulletin dead_subs;
  46. struct work work; // For deferred deallocation.
  47. };
  48. char name[TASK_NAME_SIZE];
  49. };
  50. // Task IPC message (TODO: Move to a specific header).
  51. struct task_ipc_msg
  52. {
  53. uint32_t size;
  54. int op;
  55. union
  56. {
  57. char name[TASK_NAME_SIZE];
  58. int id;
  59. };
  60. };
  61. // Task IPC operations.
  62. enum
  63. {
  64. TASK_IPC_GET_NAME,
  65. TASK_IPC_SET_NAME,
  66. TASK_IPC_GET_ID,
  67. };
  68. static inline struct task*
  69. task_get_kernel_task (void)
  70. {
  71. extern struct task task_kernel_task;
  72. return (&task_kernel_task);
  73. }
  74. static inline uint32_t
  75. task_get_kuid (const struct task *task)
  76. {
  77. return (task->kuid.id);
  78. }
  79. static inline void
  80. task_ref (struct task *task)
  81. {
  82. size_t nr_refs = atomic_add_rlx (&task->kuid.nr_refs, 1);
  83. assert (nr_refs != (size_t)-1);
  84. }
  85. void task_destroy (struct task *task);
  86. static inline void
  87. task_unref (struct task *task)
  88. {
  89. size_t nr_refs = atomic_sub_acq_rel (&task->kuid.nr_refs, 1);
  90. assert (nr_refs);
  91. if (nr_refs == 1)
  92. task_destroy (task);
  93. }
  94. static inline struct vm_map*
  95. task_get_vm_map (const struct task *task)
  96. {
  97. return (task->map);
  98. }
  99. static inline struct task*
  100. task_self (void)
  101. {
  102. return (thread_self()->xtask);
  103. }
  104. static inline int
  105. task_id (const struct task *task)
  106. {
  107. return ((int)task->kuid.id);
  108. }
  109. // Create a task.
  110. int task_create (struct task **taskp, const char *name);
  111. // Same as above, only this function allows the caller to specify the VM map.
  112. int task_create2 (struct task **taskp, const char *name, struct vm_map *map);
  113. /*
  114. * Look up a task from its name.
  115. *
  116. * If a task is found, it gains a reference. Otherwise, NULL is returned.
  117. *
  118. * This function is meant for debugging only.
  119. */
  120. struct task* task_lookup (const char *name);
  121. // Add a thread to a task.
  122. void task_add_thread (struct task *task, struct thread *thread);
  123. // Remove a thread from a task.
  124. void task_remove_thread (struct task *task, struct thread *thread);
  125. /*
  126. * Look up a thread in a task from its name.
  127. *
  128. * If a thread is found, it gains a reference, Otherwise, NULL is returned.
  129. *
  130. * This function is meant for debugging only.
  131. */
  132. struct thread* task_lookup_thread (struct task *task, const char *name);
  133. // Look up a task by its KUID.
  134. static inline struct task*
  135. task_by_kuid (uint32_t kuid)
  136. {
  137. return (kuid_find_type (kuid, struct task, kuid, KUID_TASK));
  138. }
  139. /*
  140. * Display task information.
  141. *
  142. * If task is NULL, this function displays all tasks.
  143. */
  144. void task_info (struct task *task, struct stream *stream);
  145. // Handle an IPC message on a task capability.
  146. struct cap_iters;
  147. struct ipc_msg_data;
  148. ssize_t task_handle_msg (struct task *task, struct cap_iters *src,
  149. struct cap_iters *dst, struct ipc_msg_data *data);
  150. /*
  151. * This init operation provides :
  152. * - task creation
  153. * - module fully initialized
  154. */
  155. INIT_OP_DECLARE (task_setup);
  156. #endif