paul@543 | 1 | /* |
paul@543 | 2 | * A process monitor abstraction. |
paul@543 | 3 | * |
paul@543 | 4 | * Copyright (C) 2022, 2023 Paul Boddie <paul@boddie.org.uk> |
paul@543 | 5 | * |
paul@543 | 6 | * This program is free software; you can redistribute it and/or |
paul@543 | 7 | * modify it under the terms of the GNU General Public License as |
paul@543 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@543 | 9 | * the License, or (at your option) any later version. |
paul@543 | 10 | * |
paul@543 | 11 | * This program is distributed in the hope that it will be useful, |
paul@543 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@543 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@543 | 14 | * GNU General Public License for more details. |
paul@543 | 15 | * |
paul@543 | 16 | * You should have received a copy of the GNU General Public License |
paul@543 | 17 | * along with this program; if not, write to the Free Software |
paul@543 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@543 | 19 | * Boston, MA 02110-1301, USA |
paul@543 | 20 | */ |
paul@543 | 21 | |
paul@581 | 22 | #include <l4/sys/thread.h> |
paul@581 | 23 | |
paul@543 | 24 | #include <ipc/cap_alloc.h> |
paul@543 | 25 | #include <ipc/map.h> |
paul@543 | 26 | #include <systypes/base.h> |
paul@543 | 27 | |
paul@543 | 28 | #include <stdio.h> |
paul@543 | 29 | |
paul@543 | 30 | #include "process_monitor.h" |
paul@543 | 31 | #include "parent_notification_object_server.h" |
paul@543 | 32 | |
paul@543 | 33 | |
paul@543 | 34 | |
paul@543 | 35 | /* A process monitor receiving signals from a task. */ |
paul@543 | 36 | |
paul@543 | 37 | ProcessMonitor::ProcessMonitor() |
paul@543 | 38 | : NotificationSupport() |
paul@543 | 39 | { |
paul@543 | 40 | } |
paul@543 | 41 | |
paul@543 | 42 | ipc_server_default_config_type ProcessMonitor::config() |
paul@543 | 43 | { |
paul@543 | 44 | return config_ParentNotificationObject; |
paul@543 | 45 | } |
paul@543 | 46 | |
paul@543 | 47 | |
paul@543 | 48 | |
paul@543 | 49 | /* Close the process monitor. */ |
paul@543 | 50 | |
paul@543 | 51 | void ProcessMonitor::close() |
paul@543 | 52 | { |
paul@543 | 53 | printf("Process monitor closing...\n"); |
paul@543 | 54 | |
paul@543 | 55 | if (l4_is_valid_cap(_task)) |
paul@543 | 56 | { |
paul@543 | 57 | ipc_unmap_capability(_task, _mapped_task); |
paul@543 | 58 | ipc_cap_free_um(_task); |
paul@543 | 59 | _task = L4_INVALID_CAP; |
paul@543 | 60 | } |
paul@543 | 61 | } |
paul@543 | 62 | |
paul@543 | 63 | |
paul@543 | 64 | |
paul@543 | 65 | /* Capability management. */ |
paul@543 | 66 | |
paul@543 | 67 | void ProcessMonitor::set_pager(l4_cap_idx_t cap, l4_cap_idx_t mapped_cap) |
paul@543 | 68 | { |
paul@543 | 69 | _pager = cap; |
paul@543 | 70 | _mapped_pager = mapped_cap; |
paul@543 | 71 | } |
paul@543 | 72 | |
paul@543 | 73 | void ProcessMonitor::set_parent(l4_cap_idx_t cap, l4_cap_idx_t mapped_cap) |
paul@543 | 74 | { |
paul@543 | 75 | _parent = cap; |
paul@543 | 76 | _mapped_parent = mapped_cap; |
paul@543 | 77 | } |
paul@543 | 78 | |
paul@543 | 79 | void ProcessMonitor::set_task(l4_cap_idx_t cap, l4_cap_idx_t mapped_cap) |
paul@543 | 80 | { |
paul@543 | 81 | _task = cap; |
paul@543 | 82 | _mapped_task = mapped_cap; |
paul@543 | 83 | } |
paul@543 | 84 | |
paul@543 | 85 | void ProcessMonitor::set_thread(l4_cap_idx_t cap, l4_cap_idx_t mapped_cap) |
paul@543 | 86 | { |
paul@543 | 87 | _thread = cap; |
paul@543 | 88 | _mapped_thread = mapped_cap; |
paul@543 | 89 | } |
paul@543 | 90 | |
paul@543 | 91 | |
paul@543 | 92 | |
paul@543 | 93 | /* Lifecycle management. */ |
paul@543 | 94 | |
paul@543 | 95 | void ProcessMonitor::pager_ended() |
paul@543 | 96 | { |
paul@543 | 97 | /* Release the program thread and its reference to this object. This will |
paul@543 | 98 | cause this object to be closed. */ |
paul@543 | 99 | |
paul@543 | 100 | if (l4_is_valid_cap(_thread)) |
paul@543 | 101 | { |
paul@543 | 102 | ipc_unmap_capability(_task, _mapped_thread); |
paul@543 | 103 | ipc_cap_free_um(_thread); |
paul@543 | 104 | _thread = L4_INVALID_CAP; |
paul@543 | 105 | |
paul@543 | 106 | ipc_unmap_capability(_task, _mapped_parent); |
paul@543 | 107 | } |
paul@543 | 108 | } |
paul@543 | 109 | |
paul@543 | 110 | |
paul@543 | 111 | |
paul@543 | 112 | /* Receive signals from a task. */ |
paul@543 | 113 | |
paul@543 | 114 | long ProcessMonitor::signal(unsigned long sig, unsigned long val) |
paul@543 | 115 | { |
paul@543 | 116 | notify_all(NOTIFY_TASK_SIGNAL, (notify_values_t) {sig, val}); |
paul@543 | 117 | |
paul@543 | 118 | /* Handle the termination event. */ |
paul@543 | 119 | |
paul@543 | 120 | if (sig == 0) |
paul@543 | 121 | { |
paul@543 | 122 | printf("Signal from task.\n"); |
paul@543 | 123 | |
paul@581 | 124 | /* Cancel any IPC to avoid spurious pager warnings. */ |
paul@581 | 125 | |
paul@581 | 126 | l4_thread_ex_regs(_pager, ~0UL, ~0UL, L4_THREAD_EX_REGS_CANCEL); |
paul@581 | 127 | |
paul@543 | 128 | /* Once the program exits, the IPC gate connecting the program with its |
paul@543 | 129 | internal pager can be released. This will cause the internal pager to |
paul@543 | 130 | finish, which is then handled by the external pager. */ |
paul@543 | 131 | |
paul@543 | 132 | if (l4_is_valid_cap(_pager)) |
paul@543 | 133 | { |
paul@543 | 134 | ipc_cap_free_um(_pager); |
paul@543 | 135 | _pager = L4_INVALID_CAP; |
paul@543 | 136 | } |
paul@543 | 137 | |
paul@543 | 138 | /* The thread is not released here since the initiator of the process |
paul@543 | 139 | needs to be able to receive a reference to this object, but if the |
paul@543 | 140 | process terminates and the thread is released, this object may be |
paul@543 | 141 | released before the initiator obtains a reference to it. */ |
paul@543 | 142 | } |
paul@543 | 143 | |
paul@543 | 144 | return L4_EOK; |
paul@543 | 145 | } |
paul@543 | 146 | |
paul@543 | 147 | |
paul@543 | 148 | |
paul@543 | 149 | /* Subscribe to notifications. */ |
paul@543 | 150 | |
paul@543 | 151 | long ProcessMonitor::subscribe(l4_cap_idx_t notifier, notify_flags_t flags) |
paul@543 | 152 | { |
paul@548 | 153 | unsubscribe(); |
paul@548 | 154 | |
paul@548 | 155 | _notifier = notifier; |
paul@548 | 156 | return NotificationSupport::subscribe(_notifier, flags); |
paul@543 | 157 | } |
paul@543 | 158 | |
paul@543 | 159 | /* Unsubscribe from notifications. */ |
paul@543 | 160 | |
paul@548 | 161 | long ProcessMonitor::unsubscribe() |
paul@543 | 162 | { |
paul@548 | 163 | if (l4_is_valid_cap(_notifier)) |
paul@548 | 164 | { |
paul@548 | 165 | long err = NotificationSupport::unsubscribe(_notifier); |
paul@548 | 166 | _notifier = L4_INVALID_CAP; |
paul@548 | 167 | return err; |
paul@548 | 168 | } |
paul@548 | 169 | else |
paul@548 | 170 | return L4_EOK; |
paul@543 | 171 | } |
paul@543 | 172 | |
paul@543 | 173 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@543 | 174 | */ |