paul@324 | 1 | /* |
paul@324 | 2 | * Support for initialising programs in new tasks and threads. |
paul@324 | 3 | * |
paul@492 | 4 | * Copyright (C) 2022, 2023 Paul Boddie <paul@boddie.org.uk> |
paul@324 | 5 | * |
paul@324 | 6 | * This program is free software; you can redistribute it and/or |
paul@324 | 7 | * modify it under the terms of the GNU General Public License as |
paul@324 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@324 | 9 | * the License, or (at your option) any later version. |
paul@324 | 10 | * |
paul@324 | 11 | * This program is distributed in the hope that it will be useful, |
paul@324 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@324 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@324 | 14 | * GNU General Public License for more details. |
paul@324 | 15 | * |
paul@324 | 16 | * You should have received a copy of the GNU General Public License |
paul@324 | 17 | * along with this program; if not, write to the Free Software |
paul@324 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@324 | 19 | * Boston, MA 02110-1301, USA |
paul@324 | 20 | */ |
paul@324 | 21 | |
paul@324 | 22 | #include <l4/sys/err.h> |
paul@324 | 23 | #include <l4/sys/scheduler.h> |
paul@324 | 24 | |
paul@324 | 25 | #include <exec/process.h> |
paul@324 | 26 | #include <ipc/cap_alloc.h> |
paul@324 | 27 | #include <ipc/map.h> |
paul@324 | 28 | #include <mem/memory_utils.h> |
paul@324 | 29 | |
paul@324 | 30 | |
paul@324 | 31 | |
paul@324 | 32 | /* Obtain a flexpage defining the UTCB area location and size in a new task. */ |
paul@324 | 33 | |
paul@429 | 34 | static l4_fpage_t get_utcb_fpage(unsigned int page, unsigned int pages) |
paul@324 | 35 | { |
paul@324 | 36 | /* UTCB location and size. */ |
paul@324 | 37 | |
paul@453 | 38 | int utcb_log2size = page_order(Default_max_threads * L4_UTCB_OFFSET); |
paul@324 | 39 | |
paul@324 | 40 | /* Round up to at least one page. */ |
paul@324 | 41 | |
paul@324 | 42 | if (utcb_log2size < L4_PAGESHIFT) |
paul@324 | 43 | utcb_log2size = L4_PAGESHIFT; |
paul@324 | 44 | |
paul@453 | 45 | utcb_log2size = page_order(pages * (1UL << utcb_log2size)); |
paul@453 | 46 | |
paul@429 | 47 | return l4_fpage(Utcb_area_start + page * (1UL << utcb_log2size), utcb_log2size, 0); |
paul@324 | 48 | } |
paul@324 | 49 | |
paul@324 | 50 | |
paul@324 | 51 | |
paul@324 | 52 | /* Initialise a new process, this being an abstraction for a new task with some |
paul@324 | 53 | threads. */ |
paul@324 | 54 | |
paul@430 | 55 | Process::Process() |
paul@324 | 56 | { |
paul@429 | 57 | _thread_number = 0; |
paul@324 | 58 | |
paul@324 | 59 | /* Populate the common initial environment for the threads. */ |
paul@324 | 60 | |
paul@324 | 61 | _env.factory = L4_BASE_FACTORY_CAP; |
paul@324 | 62 | _env.log = L4_BASE_LOG_CAP; |
paul@324 | 63 | _env.scheduler = L4_BASE_SCHEDULER_CAP; |
paul@324 | 64 | _env.mem_alloc = L4_EXEC_MA_CAP; |
paul@469 | 65 | _env.parent = L4_EXEC_PARENT_CAP; |
paul@324 | 66 | |
paul@469 | 67 | /* Capability details that are updated for each thread. Note that the region |
paul@469 | 68 | mapper is redefined, but it would traditionally employ the given index. */ |
paul@370 | 69 | |
paul@370 | 70 | _env.main_thread = L4_BASE_THREAD_CAP; |
paul@370 | 71 | _env.rm = L4_EXEC_RM_CAP; |
paul@370 | 72 | _env.first_free_cap = L4_EXEC_FIRST_FREE_CAP_INDEX; |
paul@370 | 73 | |
paul@324 | 74 | /* Populate auxiliary information. */ |
paul@324 | 75 | |
paul@324 | 76 | _aux.kip_ds = L4_EXEC_KIP_CAP; |
paul@324 | 77 | _aux.dbg_lvl = 0; |
paul@324 | 78 | _aux.ldr_flags = 0; |
paul@324 | 79 | } |
paul@324 | 80 | |
paul@370 | 81 | /* Capability index allocation. */ |
paul@370 | 82 | |
paul@370 | 83 | l4_cap_idx_t Process::allocate_cap() |
paul@370 | 84 | { |
paul@370 | 85 | return (_env.first_free_cap++ << L4_CAP_SHIFT); |
paul@370 | 86 | } |
paul@370 | 87 | |
paul@324 | 88 | /* Task and thread initialisation. */ |
paul@324 | 89 | |
paul@474 | 90 | long Process::create_task(unsigned int threads) |
paul@324 | 91 | { |
paul@324 | 92 | _task = ipc_cap_alloc(); |
paul@324 | 93 | |
paul@324 | 94 | if (l4_is_invalid_cap(_task)) |
paul@324 | 95 | return -L4_ENOMEM; |
paul@324 | 96 | |
paul@474 | 97 | return l4_error(l4_factory_create_task(l4re_env()->factory, _task, get_utcb_fpage(0, threads))); |
paul@324 | 98 | } |
paul@324 | 99 | |
paul@324 | 100 | long Process::create_thread(l4_cap_idx_t *thread) |
paul@324 | 101 | { |
paul@324 | 102 | *thread = ipc_cap_alloc(); |
paul@324 | 103 | |
paul@324 | 104 | if (l4_is_invalid_cap(*thread)) |
paul@324 | 105 | return -L4_ENOMEM; |
paul@324 | 106 | |
paul@324 | 107 | return l4_error(l4_factory_create_thread(l4re_env()->factory, *thread)); |
paul@324 | 108 | } |
paul@324 | 109 | |
paul@492 | 110 | /* Task access. */ |
paul@492 | 111 | |
paul@492 | 112 | l4_cap_idx_t Process::get_task() |
paul@492 | 113 | { |
paul@492 | 114 | return _task; |
paul@492 | 115 | } |
paul@492 | 116 | |
paul@324 | 117 | /* Configure the task environment. */ |
paul@324 | 118 | |
paul@474 | 119 | long Process::configure_task(unsigned int threads) |
paul@324 | 120 | { |
paul@474 | 121 | long err = create_task(threads); |
paul@324 | 122 | |
paul@324 | 123 | if (err) |
paul@324 | 124 | return err; |
paul@324 | 125 | |
paul@324 | 126 | /* Map the KIP into the task. */ |
paul@324 | 127 | |
paul@324 | 128 | l4_addr_t kip_start = (l4_addr_t) l4re_kip(); |
paul@324 | 129 | |
paul@324 | 130 | err = l4_error(l4_task_map(_task, L4RE_THIS_TASK_CAP, |
paul@324 | 131 | l4_fpage(kip_start, L4_PAGESHIFT, L4_FPAGE_RX), |
paul@324 | 132 | kip_start)); |
paul@324 | 133 | |
paul@324 | 134 | if (err) |
paul@324 | 135 | return err; |
paul@324 | 136 | |
paul@324 | 137 | /* Define capability mappings for the new task. */ |
paul@324 | 138 | |
paul@324 | 139 | struct ipc_mapped_cap mapped_caps[] = { |
paul@376 | 140 | {L4_BASE_TASK_CAP, _task, L4_CAP_FPAGE_RWS, 0}, |
paul@376 | 141 | {_env.factory, l4re_env()->factory, L4_CAP_FPAGE_RWS, 0}, |
paul@376 | 142 | {_env.log, l4re_env()->log, L4_CAP_FPAGE_RWS, 0}, |
paul@376 | 143 | {_env.scheduler, l4re_env()->scheduler, L4_CAP_FPAGE_RWS, 0}, |
paul@376 | 144 | {_env.mem_alloc, l4re_env()->mem_alloc, L4_CAP_FPAGE_RWS, 0}, |
paul@376 | 145 | {0, L4_INVALID_CAP, 0, 0}, |
paul@324 | 146 | }; |
paul@324 | 147 | |
paul@365 | 148 | return map_capabilities(mapped_caps, false); |
paul@365 | 149 | } |
paul@365 | 150 | |
paul@469 | 151 | /* Configure the thread environment, employing the given server as the region |
paul@499 | 152 | mapper. Return the capability index for the server in the new task. */ |
paul@365 | 153 | |
paul@499 | 154 | long Process::configure_thread(l4_cap_idx_t server, l4_cap_idx_t *mapped_cap) |
paul@365 | 155 | { |
paul@370 | 156 | /* Employ a distinct region mapper for each thread's environment, this acting |
paul@370 | 157 | as pager. */ |
paul@370 | 158 | |
paul@499 | 159 | if ((mapped_cap != NULL) && l4_is_valid_cap(*mapped_cap)) |
paul@376 | 160 | { |
paul@499 | 161 | _env.rm = *mapped_cap; |
paul@376 | 162 | return L4_EOK; |
paul@376 | 163 | } |
paul@376 | 164 | else |
paul@376 | 165 | { |
paul@376 | 166 | _env.rm = allocate_cap(); |
paul@499 | 167 | *mapped_cap = _env.rm; |
paul@376 | 168 | return ipc_map_capability(_task, (struct ipc_mapped_cap) {_env.rm, server, L4_CAP_FPAGE_RWS, 0}); |
paul@376 | 169 | } |
paul@365 | 170 | } |
paul@365 | 171 | |
paul@469 | 172 | /* Set the parent of the new thread. */ |
paul@469 | 173 | |
paul@499 | 174 | long Process::set_parent(l4_cap_idx_t parent, l4_cap_idx_t *rparent) |
paul@469 | 175 | { |
paul@499 | 176 | *rparent = _env.parent; |
paul@499 | 177 | return ipc_map_capability(_task, (struct ipc_mapped_cap) {_env.parent, parent, L4_CAP_FPAGE_RWS, 0}); |
paul@469 | 178 | } |
paul@469 | 179 | |
paul@365 | 180 | /* Map capabilities into the task, counting them if indicated. */ |
paul@365 | 181 | |
paul@365 | 182 | long Process::map_capabilities(struct ipc_mapped_cap mapped_caps[], |
paul@365 | 183 | bool to_count) |
paul@365 | 184 | { |
paul@370 | 185 | unsigned int num_mapped_caps; |
paul@370 | 186 | long err = ipc_map_capabilities(_task, mapped_caps, to_count ? &num_mapped_caps : NULL); |
paul@370 | 187 | |
paul@370 | 188 | if (to_count) |
paul@370 | 189 | _env.first_free_cap += num_mapped_caps; |
paul@370 | 190 | |
paul@370 | 191 | return err; |
paul@324 | 192 | } |
paul@324 | 193 | |
paul@324 | 194 | /* Create, initialise and start a thread. */ |
paul@324 | 195 | |
paul@499 | 196 | long Process::thread_start(l4_addr_t program_start, Stack &st, l4_cap_idx_t *thread, l4_cap_idx_t *rthread) |
paul@324 | 197 | { |
paul@324 | 198 | long err; |
paul@324 | 199 | |
paul@492 | 200 | err = create_thread(thread); |
paul@324 | 201 | |
paul@324 | 202 | if (err) |
paul@324 | 203 | return err; |
paul@324 | 204 | |
paul@429 | 205 | /* Obtain UTCB area details for the thread. */ |
paul@429 | 206 | |
paul@429 | 207 | l4_fpage_t utcb_fpage = get_utcb_fpage(_thread_number, 1); |
paul@429 | 208 | |
paul@429 | 209 | _env.utcb_area = utcb_fpage; |
paul@429 | 210 | _env.first_free_utcb = l4_fpage_memaddr(utcb_fpage); |
paul@429 | 211 | |
paul@324 | 212 | /* Initialise the thread with pager, UTCB and task details. */ |
paul@324 | 213 | |
paul@324 | 214 | l4_thread_control_start(); |
paul@370 | 215 | l4_thread_control_pager(_env.rm); |
paul@370 | 216 | l4_thread_control_exc_handler(_env.rm); |
paul@429 | 217 | l4_thread_control_bind((l4_utcb_t *) l4_fpage_memaddr(_env.utcb_area), _task); |
paul@324 | 218 | |
paul@492 | 219 | err = l4_error(l4_thread_control_commit(*thread)); |
paul@324 | 220 | |
paul@324 | 221 | if (err) |
paul@324 | 222 | { |
paul@492 | 223 | ipc_cap_free(*thread); |
paul@324 | 224 | return err; |
paul@324 | 225 | } |
paul@324 | 226 | |
paul@370 | 227 | /* Map the thread capability to the task using a distinct capability index. */ |
paul@370 | 228 | |
paul@370 | 229 | _env.main_thread = allocate_cap(); |
paul@324 | 230 | |
paul@492 | 231 | ipc_map_capability(_task, (struct ipc_mapped_cap) {_env.main_thread, *thread, L4_CAP_FPAGE_RWS, 0}); |
paul@324 | 232 | |
paul@324 | 233 | /* Populate the initial environment in the thread. */ |
paul@324 | 234 | |
paul@324 | 235 | st.set_l4re_aux(&_aux); |
paul@324 | 236 | st.set_l4re_env(&_env); |
paul@324 | 237 | |
paul@376 | 238 | /* Reserve some extra space for capabilities used by this thread. |
paul@376 | 239 | NOTE: Surely the capability allocator should be able to avoid conflicts, |
paul@376 | 240 | but concurrency issues have been observed before, leading to various |
paul@376 | 241 | measures in libipc. */ |
paul@376 | 242 | |
paul@376 | 243 | _env.first_free_cap += 0x20; |
paul@376 | 244 | |
paul@324 | 245 | /* Set the start details. */ |
paul@324 | 246 | |
paul@492 | 247 | err = l4_error(l4_thread_ex_regs(*thread, program_start, st.start_address(), 0)); |
paul@324 | 248 | |
paul@324 | 249 | if (err) |
paul@324 | 250 | return err; |
paul@324 | 251 | |
paul@429 | 252 | /* Select a new thread. */ |
paul@324 | 253 | |
paul@429 | 254 | _thread_number++; |
paul@324 | 255 | |
paul@324 | 256 | /* Start the thread. */ |
paul@324 | 257 | |
paul@324 | 258 | l4_sched_param_t sp = l4_sched_param(L4RE_MAIN_THREAD_PRIO); |
paul@324 | 259 | |
paul@499 | 260 | *rthread = _env.main_thread; |
paul@492 | 261 | return l4_error(l4_scheduler_run_thread(l4re_env()->scheduler, *thread, &sp)); |
paul@324 | 262 | } |
paul@324 | 263 | |
paul@324 | 264 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@324 | 265 | */ |