task.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. vm_map_destroy (task->map);
  146. kuid_remove (&task->kuid, KUID_TASK);
  147. kmem_cache_free (&task_cache, task);
  148. }
  149. struct task*
  150. task_lookup (const char *name)
  151. {
  152. SPINLOCK_GUARD (&task_list_lock);
  153. struct task *task;
  154. list_for_each_entry (&task_list, task, node)
  155. {
  156. SPINLOCK_GUARD (&task->lock);
  157. if (strcmp (task->name, name) == 0)
  158. {
  159. task_ref (task);
  160. return (task);
  161. }
  162. }
  163. return (NULL);
  164. }
  165. void
  166. task_add_thread (struct task *task, struct thread *thread)
  167. {
  168. SPINLOCK_GUARD (&task->lock);
  169. list_insert_tail (&task->threads, &thread->task_node);
  170. }
  171. void
  172. task_remove_thread (struct task *task, struct thread *thread)
  173. {
  174. spinlock_lock (&task->lock);
  175. list_remove (&thread->task_node);
  176. bool last = list_empty (&task->threads);
  177. spinlock_unlock (&task->lock);
  178. if (last)
  179. { // Destroy the cspace early to avoid circular references.
  180. cspace_destroy (&task->caps);
  181. cap_notify_dead (&task->dead_subs);
  182. task_unref (task);
  183. }
  184. }
  185. struct thread*
  186. task_lookup_thread (struct task *task, const char *name)
  187. {
  188. SPINLOCK_GUARD (&task->lock);
  189. struct thread *thread;
  190. list_for_each_entry (&task->threads, thread, task_node)
  191. if (strcmp (thread_name (thread), name) == 0)
  192. {
  193. thread_ref (thread);
  194. return (thread);
  195. }
  196. return (NULL);
  197. }
  198. void
  199. task_info (struct task *task, struct stream *stream)
  200. {
  201. if (! task)
  202. {
  203. SPINLOCK_GUARD (&task_list_lock);
  204. list_for_each_entry (&task_list, task, node)
  205. task_info (task, stream);
  206. return;
  207. }
  208. SPINLOCK_GUARD (&task->lock);
  209. fmt_xprintf (stream, "task: name: %s, threads:\n", task->name);
  210. if (list_empty (&task->threads))
  211. {
  212. stream_puts (stream, "(empty)\n");
  213. return;
  214. }
  215. /*
  216. * Don't grab any lock when accessing threads, so that the function
  217. * can be used to debug in the middle of most critical sections.
  218. * Threads are only destroyed after being removed from their task
  219. * so holding the task lock is enough to guarantee existence.
  220. */
  221. struct thread *thread;
  222. list_for_each_entry (&task->threads, thread, task_node)
  223. fmt_xprintf (stream, TASK_INFO_ADDR_FMT " %c %8s:" TASK_INFO_ADDR_FMT
  224. " %.2s:%02hu %02u %s\n",
  225. (uintptr_t)thread,
  226. thread_state_to_chr (thread_state (thread)),
  227. thread_wchan_desc (thread),
  228. (uintptr_t)thread_wchan_addr (thread),
  229. thread_sched_class_to_str (thread_user_sched_class (thread)),
  230. thread_user_priority (thread),
  231. thread_real_global_priority (thread),
  232. thread_name (thread));
  233. }
  234. #define TASK_IPC_NEEDS_COPY \
  235. ((1u << TASK_IPC_GET_NAME) | (1u << TASK_IPC_GET_ID))
  236. static ssize_t
  237. task_name_impl (struct task *task, char *name, bool set)
  238. {
  239. SPINLOCK_GUARD (&task->lock);
  240. if (set)
  241. memcpy (task->name, name, sizeof (task->name));
  242. else
  243. memcpy (name, task->name, sizeof (task->name));
  244. return (0);
  245. }
  246. ssize_t
  247. task_handle_msg (struct task *task, struct cap_iters *src,
  248. struct cap_iters *dst, struct ipc_msg_data *data)
  249. {
  250. struct task_ipc_msg tmsg;
  251. struct ipc_iov_iter k_it;
  252. ipc_iov_iter_init_buf (&k_it, &tmsg, sizeof (tmsg));
  253. ssize_t rv = user_copyv_from (&k_it, &src->iov);
  254. if (rv < 0)
  255. return (rv);
  256. switch (tmsg.op)
  257. {
  258. case TASK_IPC_GET_NAME:
  259. case TASK_IPC_SET_NAME:
  260. rv = task_name_impl (task, tmsg.name, tmsg.op == TASK_IPC_SET_NAME);
  261. break;
  262. case TASK_IPC_GET_ID:
  263. tmsg.id = task_id (task);
  264. break;
  265. default:
  266. return (-EINVAL);
  267. }
  268. if (rv >= 0 && ((1u << tmsg.op) & TASK_IPC_NEEDS_COPY))
  269. {
  270. ipc_iov_iter_init_buf (&k_it, &tmsg, sizeof (tmsg));
  271. rv = user_copyv_to (&dst->iov, &k_it);
  272. }
  273. (void)data;
  274. return (rv < 0 ? rv : 0);
  275. }