paul@324 | 1 | /* |
paul@324 | 2 | * Support for initialising programs in new tasks and threads. |
paul@324 | 3 | * |
paul@324 | 4 | * Copyright (C) 2022 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@324 | 65 | |
paul@370 | 66 | /* Capability details that are updated for each thread. */ |
paul@370 | 67 | |
paul@370 | 68 | _env.main_thread = L4_BASE_THREAD_CAP; |
paul@370 | 69 | _env.rm = L4_EXEC_RM_CAP; |
paul@370 | 70 | _env.first_free_cap = L4_EXEC_FIRST_FREE_CAP_INDEX; |
paul@370 | 71 | |
paul@324 | 72 | /* Populate auxiliary information. */ |
paul@324 | 73 | |
paul@324 | 74 | _aux.kip_ds = L4_EXEC_KIP_CAP; |
paul@324 | 75 | _aux.dbg_lvl = 0; |
paul@324 | 76 | _aux.ldr_flags = 0; |
paul@324 | 77 | } |
paul@324 | 78 | |
paul@370 | 79 | /* Capability index allocation. */ |
paul@370 | 80 | |
paul@370 | 81 | l4_cap_idx_t Process::allocate_cap() |
paul@370 | 82 | { |
paul@370 | 83 | return (_env.first_free_cap++ << L4_CAP_SHIFT); |
paul@370 | 84 | } |
paul@370 | 85 | |
paul@324 | 86 | /* Task and thread initialisation. */ |
paul@324 | 87 | |
paul@324 | 88 | long Process::create_task() |
paul@324 | 89 | { |
paul@324 | 90 | _task = ipc_cap_alloc(); |
paul@324 | 91 | |
paul@324 | 92 | if (l4_is_invalid_cap(_task)) |
paul@324 | 93 | return -L4_ENOMEM; |
paul@324 | 94 | |
paul@429 | 95 | return l4_error(l4_factory_create_task(l4re_env()->factory, _task, get_utcb_fpage(0, 2))); |
paul@324 | 96 | } |
paul@324 | 97 | |
paul@324 | 98 | long Process::create_thread(l4_cap_idx_t *thread) |
paul@324 | 99 | { |
paul@324 | 100 | *thread = ipc_cap_alloc(); |
paul@324 | 101 | |
paul@324 | 102 | if (l4_is_invalid_cap(*thread)) |
paul@324 | 103 | return -L4_ENOMEM; |
paul@324 | 104 | |
paul@324 | 105 | return l4_error(l4_factory_create_thread(l4re_env()->factory, *thread)); |
paul@324 | 106 | } |
paul@324 | 107 | |
paul@324 | 108 | /* Configure the task environment. */ |
paul@324 | 109 | |
paul@365 | 110 | long Process::configure_task() |
paul@324 | 111 | { |
paul@324 | 112 | long err = create_task(); |
paul@324 | 113 | |
paul@324 | 114 | if (err) |
paul@324 | 115 | return err; |
paul@324 | 116 | |
paul@324 | 117 | /* Map the KIP into the task. */ |
paul@324 | 118 | |
paul@324 | 119 | l4_addr_t kip_start = (l4_addr_t) l4re_kip(); |
paul@324 | 120 | |
paul@324 | 121 | err = l4_error(l4_task_map(_task, L4RE_THIS_TASK_CAP, |
paul@324 | 122 | l4_fpage(kip_start, L4_PAGESHIFT, L4_FPAGE_RX), |
paul@324 | 123 | kip_start)); |
paul@324 | 124 | |
paul@324 | 125 | if (err) |
paul@324 | 126 | return err; |
paul@324 | 127 | |
paul@324 | 128 | /* Define capability mappings for the new task. */ |
paul@324 | 129 | |
paul@324 | 130 | struct ipc_mapped_cap mapped_caps[] = { |
paul@376 | 131 | {L4_BASE_TASK_CAP, _task, L4_CAP_FPAGE_RWS, 0}, |
paul@376 | 132 | {_env.factory, l4re_env()->factory, L4_CAP_FPAGE_RWS, 0}, |
paul@376 | 133 | {_env.log, l4re_env()->log, L4_CAP_FPAGE_RWS, 0}, |
paul@376 | 134 | {_env.scheduler, l4re_env()->scheduler, L4_CAP_FPAGE_RWS, 0}, |
paul@376 | 135 | {_env.mem_alloc, l4re_env()->mem_alloc, L4_CAP_FPAGE_RWS, 0}, |
paul@376 | 136 | {0, L4_INVALID_CAP, 0, 0}, |
paul@324 | 137 | }; |
paul@324 | 138 | |
paul@365 | 139 | return map_capabilities(mapped_caps, false); |
paul@365 | 140 | } |
paul@365 | 141 | |
paul@365 | 142 | /* Configure the thread environment. */ |
paul@365 | 143 | |
paul@376 | 144 | long Process::configure_thread(l4_cap_idx_t server, l4_cap_idx_t mapped_cap) |
paul@365 | 145 | { |
paul@370 | 146 | /* Employ a distinct region mapper for each thread's environment, this acting |
paul@370 | 147 | as pager. */ |
paul@370 | 148 | |
paul@376 | 149 | if (l4_is_valid_cap(mapped_cap)) |
paul@376 | 150 | { |
paul@376 | 151 | _env.rm = mapped_cap; |
paul@376 | 152 | return L4_EOK; |
paul@376 | 153 | } |
paul@376 | 154 | else |
paul@376 | 155 | { |
paul@376 | 156 | _env.rm = allocate_cap(); |
paul@376 | 157 | return ipc_map_capability(_task, (struct ipc_mapped_cap) {_env.rm, server, L4_CAP_FPAGE_RWS, 0}); |
paul@376 | 158 | } |
paul@365 | 159 | } |
paul@365 | 160 | |
paul@365 | 161 | /* Map capabilities into the task, counting them if indicated. */ |
paul@365 | 162 | |
paul@365 | 163 | long Process::map_capabilities(struct ipc_mapped_cap mapped_caps[], |
paul@365 | 164 | bool to_count) |
paul@365 | 165 | { |
paul@370 | 166 | unsigned int num_mapped_caps; |
paul@370 | 167 | long err = ipc_map_capabilities(_task, mapped_caps, to_count ? &num_mapped_caps : NULL); |
paul@370 | 168 | |
paul@370 | 169 | if (to_count) |
paul@370 | 170 | _env.first_free_cap += num_mapped_caps; |
paul@370 | 171 | |
paul@370 | 172 | return err; |
paul@324 | 173 | } |
paul@324 | 174 | |
paul@324 | 175 | /* Create, initialise and start a thread. */ |
paul@324 | 176 | |
paul@324 | 177 | long Process::thread_start(l4_addr_t program_start, Stack &st) |
paul@324 | 178 | { |
paul@324 | 179 | l4_cap_idx_t thread; |
paul@324 | 180 | long err; |
paul@324 | 181 | |
paul@324 | 182 | err = create_thread(&thread); |
paul@324 | 183 | |
paul@324 | 184 | if (err) |
paul@324 | 185 | return err; |
paul@324 | 186 | |
paul@429 | 187 | /* Obtain UTCB area details for the thread. */ |
paul@429 | 188 | |
paul@429 | 189 | l4_fpage_t utcb_fpage = get_utcb_fpage(_thread_number, 1); |
paul@429 | 190 | |
paul@429 | 191 | _env.utcb_area = utcb_fpage; |
paul@429 | 192 | _env.first_free_utcb = l4_fpage_memaddr(utcb_fpage); |
paul@429 | 193 | |
paul@324 | 194 | /* Initialise the thread with pager, UTCB and task details. */ |
paul@324 | 195 | |
paul@324 | 196 | l4_thread_control_start(); |
paul@370 | 197 | l4_thread_control_pager(_env.rm); |
paul@370 | 198 | l4_thread_control_exc_handler(_env.rm); |
paul@429 | 199 | l4_thread_control_bind((l4_utcb_t *) l4_fpage_memaddr(_env.utcb_area), _task); |
paul@324 | 200 | |
paul@324 | 201 | err = l4_error(l4_thread_control_commit(thread)); |
paul@324 | 202 | |
paul@324 | 203 | if (err) |
paul@324 | 204 | { |
paul@324 | 205 | ipc_cap_free(thread); |
paul@324 | 206 | return err; |
paul@324 | 207 | } |
paul@324 | 208 | |
paul@370 | 209 | /* Map the thread capability to the task using a distinct capability index. */ |
paul@370 | 210 | |
paul@370 | 211 | _env.main_thread = allocate_cap(); |
paul@324 | 212 | |
paul@376 | 213 | ipc_map_capability(_task, (struct ipc_mapped_cap) {_env.main_thread, thread, L4_CAP_FPAGE_RWS, 0}); |
paul@324 | 214 | |
paul@324 | 215 | /* Populate the initial environment in the thread. */ |
paul@324 | 216 | |
paul@324 | 217 | st.set_l4re_aux(&_aux); |
paul@324 | 218 | st.set_l4re_env(&_env); |
paul@324 | 219 | |
paul@376 | 220 | /* Reserve some extra space for capabilities used by this thread. |
paul@376 | 221 | NOTE: Surely the capability allocator should be able to avoid conflicts, |
paul@376 | 222 | but concurrency issues have been observed before, leading to various |
paul@376 | 223 | measures in libipc. */ |
paul@376 | 224 | |
paul@376 | 225 | _env.first_free_cap += 0x20; |
paul@376 | 226 | |
paul@324 | 227 | /* Set the start details. */ |
paul@324 | 228 | |
paul@324 | 229 | err = l4_error(l4_thread_ex_regs(thread, program_start, st.start_address(), 0)); |
paul@324 | 230 | |
paul@324 | 231 | if (err) |
paul@324 | 232 | return err; |
paul@324 | 233 | |
paul@429 | 234 | /* Select a new thread. */ |
paul@324 | 235 | |
paul@429 | 236 | _thread_number++; |
paul@324 | 237 | |
paul@324 | 238 | /* Start the thread. */ |
paul@324 | 239 | |
paul@324 | 240 | l4_sched_param_t sp = l4_sched_param(L4RE_MAIN_THREAD_PRIO); |
paul@324 | 241 | |
paul@324 | 242 | return l4_error(l4_scheduler_run_thread(l4re_env()->scheduler, thread, &sp)); |
paul@324 | 243 | } |
paul@324 | 244 | |
paul@324 | 245 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@324 | 246 | */ |