1// SPDX-License-Identifier: GPL-3.0-or-later
2// Special processes: (pid 0: idle, pid 1: init, pid 2: kthreadd)
3
4#include "mos/filesystem/sysfs/sysfs.hpp"
5#include "mos/filesystem/sysfs/sysfs_autoinit.hpp"
6#include "mos/syslog/printk.hpp"
7#include "mos/tasks/schedule.hpp"
8
9#include <mos/lib/structures/hashmap.hpp>
10#include <mos/lib/structures/hashmap_common.hpp>
11#include <mos/misc/panic.hpp>
12#include <mos/platform/platform.hpp>
13#include <mos/tasks/process.hpp>
14#include <mos/tasks/task_types.hpp>
15#include <mos/tasks/thread.hpp>
16#include <mos_stdlib.hpp>
17
18#define PROCESS_HASHTABLE_SIZE 512
19#define THREAD_HASHTABLE_SIZE 512
20
21static void dump_process(void)
22{
23 if (current_thread)
24 {
25 auto proc = current_process;
26 pr_info("process %pp ", proc);
27 if (proc->parent)
28 pr_info2("parent %pp ", proc->parent);
29 else
30 pr_info2("parent <none> ");
31 process_dump_mmaps(process: proc);
32 }
33 else
34 {
35 pr_warn("no current thread");
36 }
37}
38
39MOS_PANIC_HOOK(dump_process, "Dump current process");
40
41// ! sysfs support
42
43static bool tasks_sysfs_process_list(sysfs_file_t *f)
44{
45 for (const auto &[pid, proc] : ProcessTable)
46 sysfs_printf(file: f, fmt: "%pp, parent=%pp, main_thread=%pt, exit_status=%d\n", proc, proc->parent, proc->main_thread, proc->exit_status);
47
48 return true;
49}
50
51static bool tasks_sysfs_thread_list(sysfs_file_t *f)
52{
53 for (const auto &[tid, thread] : thread_table)
54 sysfs_printf(file: f, fmt: "%pt, state=%c, mode=%s, owner=%pp, stack=" PTR_FMT " (%zu bytes)\n", thread, thread_state_str(state: thread->state),
55 thread->mode == THREAD_MODE_KERNEL ? "kernel" : "user", thread->owner, thread->u_stack.top, thread->u_stack.capacity);
56 return true;
57}
58
59static sysfs_item_t task_sysfs_items[] = {
60 SYSFS_RO_ITEM("processes", tasks_sysfs_process_list),
61 SYSFS_RO_ITEM("threads", tasks_sysfs_thread_list),
62};
63
64SYSFS_AUTOREGISTER(tasks, task_sysfs_items);
65