task.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. #include <errno.h>
  18. #include <stddef.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <kern/capability.h>
  22. #include <kern/cspace.h>
  23. #include <kern/init.h>
  24. #include <kern/kmem.h>
  25. #include <kern/list.h>
  26. #include <kern/log.h>
  27. #include <kern/macros.h>
  28. #include <kern/shell.h>
  29. #include <kern/spinlock.h>
  30. #include <kern/task.h>
  31. #include <kern/thread.h>
  32. #include <kern/user.h>
  33. #include <vm/map.h>
  34. #ifdef __LP64__
  35. #define TASK_INFO_ADDR_FMT "%016lx"
  36. #else
  37. #define TASK_INFO_ADDR_FMT "%08lx"
  38. #endif
  39. struct task task_kernel_task;
  40. // Cache for allocated tasks.
  41. static struct kmem_cache task_cache;
  42. // Global list of tasks.
  43. static struct list task_list;
  44. static struct spinlock task_list_lock;
  45. static void
  46. task_init (struct task *task, const char *name, struct vm_map *map)
  47. {
  48. kuid_head_init (&task->kuid);
  49. spinlock_init (&task->lock);
  50. list_init (&task->threads);
  51. task->map = map;
  52. strlcpy (task->name, name, sizeof (task->name));
  53. bulletin_init (&task->dead_subs);
  54. }
  55. #ifdef CONFIG_SHELL
  56. static void
  57. task_shell_info (struct shell *shell, int argc, char **argv)
  58. {
  59. if (argc == 1)
  60. {
  61. task_info (NULL, shell->stream);
  62. return;
  63. }
  64. struct task *task = task_lookup (argv[1]);
  65. if (! task)
  66. {
  67. fmt_xprintf (shell->stream, "task not found: %s\n", argv[1]);
  68. return;
  69. }
  70. task_info (task, shell->stream);
  71. task_unref (task);
  72. }
  73. static struct shell_cmd task_shell_cmds[] =
  74. {
  75. SHELL_CMD_INITIALIZER ("task_info", task_shell_info,
  76. "task_info [<task_name>]",
  77. "display tasks and threads"),
  78. };
  79. static int __init
  80. task_setup_shell (void)
  81. {
  82. SHELL_REGISTER_CMDS (task_shell_cmds, shell_get_main_cmd_set ());
  83. return (0);
  84. }
  85. INIT_OP_DEFINE (task_setup_shell,
  86. INIT_OP_DEP (printf_setup, true),
  87. INIT_OP_DEP (shell_setup, true),
  88. INIT_OP_DEP (task_setup, true),
  89. INIT_OP_DEP (thread_setup, true));
  90. #endif
  91. static int __init
  92. task_setup (void)
  93. {
  94. struct task *kernel_task = task_get_kernel_task ();
  95. kmem_cache_init (&task_cache, "task", sizeof (struct task), 0, NULL, 0);
  96. list_init (&task_list);
  97. spinlock_init (&task_list_lock);
  98. task_init (kernel_task, "x15", vm_map_get_kernel_map ());
  99. list_insert_head (&task_list, &kernel_task->node);
  100. return (0);
  101. }
  102. INIT_OP_DEFINE (task_setup,
  103. INIT_OP_DEP (kmem_setup, true),
  104. INIT_OP_DEP (spinlock_setup, true),
  105. INIT_OP_DEP (vm_map_setup, true));
  106. int
  107. task_create (struct task **taskp, const char *name)
  108. {
  109. struct vm_map *map;
  110. int error = vm_map_create (&map);
  111. if (error)
  112. return (error);
  113. error = task_create2 (taskp, name, map);
  114. if (error)
  115. vm_map_destroy (map);
  116. return (error);
  117. }
  118. int
  119. task_create2 (struct task **taskp, const char *name, struct vm_map *map)
  120. {
  121. struct task *task = kmem_cache_alloc (&task_cache);
  122. if (! task)
  123. return (ENOMEM);
  124. task_init (task, name, map);
  125. int error = kuid_alloc (&task->kuid, KUID_TASK);
  126. if (error)
  127. {
  128. kmem_cache_free (&task_cache, task);
  129. return (error);
  130. }
  131. cspace_init (&task->caps);
  132. spinlock_lock (&task_list_lock);
  133. list_insert_tail (&task_list, &task->node);
  134. spinlock_unlock (&task_list_lock);
  135. *taskp = task;
  136. return (0);
  137. }
  138. void
  139. task_destroy (struct task *task)
  140. {
  141. spinlock_lock (&task_list_lock);
  142. list_remove (&task->node);
  143. spinlock_unlock (&task_list_lock);
  144. cspace_destroy (&task->caps);
  145. kuid_remove (&task->kuid, KUID_TASK);
  146. if (task->map)
  147. vm_map_destroy (task->map);
  148. kmem_cache_free (&task_cache, task);
  149. }
  150. struct task*
  151. task_lookup (const char *name)
  152. {
  153. SPINLOCK_GUARD (&task_list_lock);
  154. struct task *task;
  155. list_for_each_entry (&task_list, task, node)
  156. {
  157. SPINLOCK_GUARD (&task->lock);
  158. if (strcmp (task->name, name) == 0)
  159. {
  160. task_ref (task);
  161. return (task);
  162. }
  163. }
  164. return (NULL);
  165. }
  166. void
  167. task_add_thread (struct task *task, struct thread *thread)
  168. {
  169. SPINLOCK_GUARD (&task->lock);
  170. list_insert_tail (&task->threads, &thread->task_node);
  171. }
  172. void
  173. task_remove_thread (struct task *task, struct thread *thread)
  174. {
  175. spinlock_lock (&task->lock);
  176. list_remove (&thread->task_node);
  177. bool last = list_empty (&task->threads);
  178. spinlock_unlock (&task->lock);
  179. if (last)
  180. { // The VM map and cspace must be destroyed early to avoid circularities.
  181. cspace_destroy (&task->caps);
  182. vm_map_destroy (task->map);
  183. task->map = NULL;
  184. // A task is considered dead when no more of its threads are running.
  185. cap_notify_dead (&task->dead_subs);
  186. task_unref (task);
  187. }
  188. }
  189. struct thread*
  190. task_lookup_thread (struct task *task, const char *name)
  191. {
  192. SPINLOCK_GUARD (&task->lock);
  193. struct thread *thread;
  194. list_for_each_entry (&task->threads, thread, task_node)
  195. if (strcmp (thread_name (thread), name) == 0)
  196. {
  197. thread_ref (thread);
  198. return (thread);
  199. }
  200. return (NULL);
  201. }
  202. void
  203. task_info (struct task *task, struct stream *stream)
  204. {
  205. if (! task)
  206. {
  207. SPINLOCK_GUARD (&task_list_lock);
  208. list_for_each_entry (&task_list, task, node)
  209. task_info (task, stream);
  210. return;
  211. }
  212. SPINLOCK_GUARD (&task->lock);
  213. fmt_xprintf (stream, "task: name: %s, threads:\n", task->name);
  214. if (list_empty (&task->threads))
  215. {
  216. stream_puts (stream, "(empty)\n");
  217. return;
  218. }
  219. /*
  220. * Don't grab any lock when accessing threads, so that the function
  221. * can be used to debug in the middle of most critical sections.
  222. * Threads are only destroyed after being removed from their task
  223. * so holding the task lock is enough to guarantee existence.
  224. */
  225. struct thread *thread;
  226. list_for_each_entry (&task->threads, thread, task_node)
  227. fmt_xprintf (stream, TASK_INFO_ADDR_FMT " %c %8s:" TASK_INFO_ADDR_FMT
  228. " %.2s:%02hu %02u %s\n",
  229. (uintptr_t)thread,
  230. thread_state_to_chr (thread_state (thread)),
  231. thread_wchan_desc (thread),
  232. (uintptr_t)thread_wchan_addr (thread),
  233. thread_sched_class_to_str (thread_user_sched_class (thread)),
  234. thread_user_priority (thread),
  235. thread_real_global_priority (thread),
  236. thread_name (thread));
  237. }
  238. #define TASK_IPC_NEEDS_COPY \
  239. ((1u << TASK_IPC_GET_NAME) | (1u << TASK_IPC_GET_ID))
  240. static ssize_t
  241. task_name_impl (struct task *task, char *name, bool set)
  242. {
  243. SPINLOCK_GUARD (&task->lock);
  244. if (set)
  245. memcpy (task->name, name, sizeof (task->name));
  246. else
  247. memcpy (name, task->name, sizeof (task->name));
  248. return (0);
  249. }
  250. ssize_t
  251. task_handle_msg (struct task *task, struct cap_iters *src,
  252. struct cap_iters *dst, struct ipc_msg_data *data)
  253. {
  254. struct task_ipc_msg tmsg;
  255. struct ipc_iov_iter k_it;
  256. ipc_iov_iter_init_buf (&k_it, &tmsg, sizeof (tmsg));
  257. ssize_t rv = user_copyv_from (&k_it, &src->iov);
  258. if (rv < 0)
  259. return (rv);
  260. switch (tmsg.op)
  261. {
  262. case TASK_IPC_GET_NAME:
  263. case TASK_IPC_SET_NAME:
  264. rv = task_name_impl (task, tmsg.name, tmsg.op == TASK_IPC_SET_NAME);
  265. break;
  266. case TASK_IPC_GET_ID:
  267. tmsg.id = task_id (task);
  268. break;
  269. default:
  270. return (-EINVAL);
  271. }
  272. if (rv >= 0 && ((1u << tmsg.op) & TASK_IPC_NEEDS_COPY))
  273. {
  274. ipc_iov_iter_init_buf (&k_it, &tmsg, sizeof (tmsg));
  275. rv = user_copyv_to (&dst->iov, &k_it);
  276. }
  277. (void)data;
  278. return (rv < 0 ? rv : 0);
  279. }