task.h 3.9 KB

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