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@324 | 34 | static l4_fpage_t get_utcb_fpage() |
paul@324 | 35 | { |
paul@324 | 36 | /* UTCB location and size. */ |
paul@324 | 37 | |
paul@324 | 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@324 | 45 | return l4_fpage(Utcb_area_start, utcb_log2size, 0); |
paul@324 | 46 | } |
paul@324 | 47 | |
paul@324 | 48 | |
paul@324 | 49 | |
paul@324 | 50 | /* Initialise a new process, this being an abstraction for a new task with some |
paul@324 | 51 | threads. */ |
paul@324 | 52 | |
paul@365 | 53 | Process::Process(int reserved_threads) |
paul@324 | 54 | { |
paul@324 | 55 | /* Obtain UTCB area details for the task. */ |
paul@324 | 56 | |
paul@324 | 57 | l4_fpage_t utcb_fpage = get_utcb_fpage(); |
paul@324 | 58 | |
paul@324 | 59 | _utcb_start = l4_fpage_memaddr(utcb_fpage); |
paul@324 | 60 | |
paul@324 | 61 | /* Populate the common initial environment for the threads. */ |
paul@324 | 62 | |
paul@324 | 63 | _env.factory = L4_BASE_FACTORY_CAP; |
paul@324 | 64 | _env.log = L4_BASE_LOG_CAP; |
paul@324 | 65 | _env.scheduler = L4_BASE_SCHEDULER_CAP; |
paul@324 | 66 | _env.mem_alloc = L4_EXEC_MA_CAP; |
paul@324 | 67 | _env.utcb_area = utcb_fpage; |
paul@365 | 68 | _env.first_free_utcb = l4_fpage_memaddr(utcb_fpage) + reserved_threads * L4_UTCB_OFFSET; |
paul@324 | 69 | |
paul@370 | 70 | /* Capability details that are updated for each thread. */ |
paul@370 | 71 | |
paul@370 | 72 | _env.main_thread = L4_BASE_THREAD_CAP; |
paul@370 | 73 | _env.rm = L4_EXEC_RM_CAP; |
paul@370 | 74 | _env.first_free_cap = L4_EXEC_FIRST_FREE_CAP_INDEX; |
paul@370 | 75 | |
paul@324 | 76 | /* Populate auxiliary information. */ |
paul@324 | 77 | |
paul@324 | 78 | _aux.kip_ds = L4_EXEC_KIP_CAP; |
paul@324 | 79 | _aux.dbg_lvl = 0; |
paul@324 | 80 | _aux.ldr_flags = 0; |
paul@324 | 81 | } |
paul@324 | 82 | |
paul@370 | 83 | /* Capability index allocation. */ |
paul@370 | 84 | |
paul@370 | 85 | l4_cap_idx_t Process::allocate_cap() |
paul@370 | 86 | { |
paul@370 | 87 | return (_env.first_free_cap++ << L4_CAP_SHIFT); |
paul@370 | 88 | } |
paul@370 | 89 | |
paul@324 | 90 | /* Task and thread initialisation. */ |
paul@324 | 91 | |
paul@324 | 92 | long Process::create_task() |
paul@324 | 93 | { |
paul@324 | 94 | _task = ipc_cap_alloc(); |
paul@324 | 95 | |
paul@324 | 96 | if (l4_is_invalid_cap(_task)) |
paul@324 | 97 | return -L4_ENOMEM; |
paul@324 | 98 | |
paul@324 | 99 | return l4_error(l4_factory_create_task(l4re_env()->factory, _task, _env.utcb_area)); |
paul@324 | 100 | } |
paul@324 | 101 | |
paul@324 | 102 | long Process::create_thread(l4_cap_idx_t *thread) |
paul@324 | 103 | { |
paul@324 | 104 | *thread = ipc_cap_alloc(); |
paul@324 | 105 | |
paul@324 | 106 | if (l4_is_invalid_cap(*thread)) |
paul@324 | 107 | return -L4_ENOMEM; |
paul@324 | 108 | |
paul@324 | 109 | return l4_error(l4_factory_create_thread(l4re_env()->factory, *thread)); |
paul@324 | 110 | } |
paul@324 | 111 | |
paul@324 | 112 | /* Configure the task environment. */ |
paul@324 | 113 | |
paul@365 | 114 | long Process::configure_task() |
paul@324 | 115 | { |
paul@324 | 116 | long err = create_task(); |
paul@324 | 117 | |
paul@324 | 118 | if (err) |
paul@324 | 119 | return err; |
paul@324 | 120 | |
paul@324 | 121 | /* Map the KIP into the task. */ |
paul@324 | 122 | |
paul@324 | 123 | l4_addr_t kip_start = (l4_addr_t) l4re_kip(); |
paul@324 | 124 | |
paul@324 | 125 | err = l4_error(l4_task_map(_task, L4RE_THIS_TASK_CAP, |
paul@324 | 126 | l4_fpage(kip_start, L4_PAGESHIFT, L4_FPAGE_RX), |
paul@324 | 127 | kip_start)); |
paul@324 | 128 | |
paul@324 | 129 | if (err) |
paul@324 | 130 | return err; |
paul@324 | 131 | |
paul@324 | 132 | /* Define capability mappings for the new task. */ |
paul@324 | 133 | |
paul@324 | 134 | struct ipc_mapped_cap mapped_caps[] = { |
paul@324 | 135 | {L4_BASE_TASK_CAP, _task, L4_CAP_FPAGE_RWS}, |
paul@324 | 136 | {_env.factory, l4re_env()->factory, L4_CAP_FPAGE_RWS}, |
paul@324 | 137 | {_env.log, l4re_env()->log, L4_CAP_FPAGE_RWS}, |
paul@324 | 138 | {_env.scheduler, l4re_env()->scheduler, L4_CAP_FPAGE_RWS}, |
paul@324 | 139 | {_env.mem_alloc, l4re_env()->mem_alloc, L4_CAP_FPAGE_RWS}, |
paul@324 | 140 | {0, L4_INVALID_CAP, 0}, |
paul@324 | 141 | }; |
paul@324 | 142 | |
paul@365 | 143 | return map_capabilities(mapped_caps, false); |
paul@365 | 144 | } |
paul@365 | 145 | |
paul@365 | 146 | /* Configure the thread environment. */ |
paul@365 | 147 | |
paul@365 | 148 | long Process::configure_thread(l4_cap_idx_t server) |
paul@365 | 149 | { |
paul@370 | 150 | /* Employ a distinct region mapper for each thread's environment, this acting |
paul@370 | 151 | as pager. */ |
paul@370 | 152 | |
paul@370 | 153 | _env.rm = allocate_cap(); |
paul@370 | 154 | |
paul@365 | 155 | struct ipc_mapped_cap mapped_caps[] = { |
paul@365 | 156 | {_env.rm, server, L4_CAP_FPAGE_RWS}, |
paul@365 | 157 | {0, L4_INVALID_CAP, 0}, |
paul@365 | 158 | }; |
paul@365 | 159 | |
paul@365 | 160 | return map_capabilities(mapped_caps, false); |
paul@365 | 161 | } |
paul@365 | 162 | |
paul@365 | 163 | /* Map capabilities into the task, counting them if indicated. */ |
paul@365 | 164 | |
paul@365 | 165 | long Process::map_capabilities(struct ipc_mapped_cap mapped_caps[], |
paul@365 | 166 | bool to_count) |
paul@365 | 167 | { |
paul@370 | 168 | unsigned int num_mapped_caps; |
paul@370 | 169 | long err = ipc_map_capabilities(_task, mapped_caps, to_count ? &num_mapped_caps : NULL); |
paul@370 | 170 | |
paul@370 | 171 | if (to_count) |
paul@370 | 172 | _env.first_free_cap += num_mapped_caps; |
paul@370 | 173 | |
paul@370 | 174 | return err; |
paul@324 | 175 | } |
paul@324 | 176 | |
paul@324 | 177 | /* Create, initialise and start a thread. */ |
paul@324 | 178 | |
paul@324 | 179 | long Process::thread_start(l4_addr_t program_start, Stack &st) |
paul@324 | 180 | { |
paul@324 | 181 | l4_cap_idx_t thread; |
paul@324 | 182 | long err; |
paul@324 | 183 | |
paul@324 | 184 | err = create_thread(&thread); |
paul@324 | 185 | |
paul@324 | 186 | if (err) |
paul@324 | 187 | return err; |
paul@324 | 188 | |
paul@324 | 189 | /* Initialise the thread with pager, UTCB and task details. */ |
paul@324 | 190 | |
paul@324 | 191 | l4_thread_control_start(); |
paul@370 | 192 | l4_thread_control_pager(_env.rm); |
paul@370 | 193 | l4_thread_control_exc_handler(_env.rm); |
paul@324 | 194 | l4_thread_control_bind((l4_utcb_t *) _utcb_start, _task); |
paul@324 | 195 | |
paul@324 | 196 | err = l4_error(l4_thread_control_commit(thread)); |
paul@324 | 197 | |
paul@324 | 198 | if (err) |
paul@324 | 199 | { |
paul@324 | 200 | ipc_cap_free(thread); |
paul@324 | 201 | return err; |
paul@324 | 202 | } |
paul@324 | 203 | |
paul@370 | 204 | /* Map the thread capability to the task using a distinct capability index. */ |
paul@370 | 205 | |
paul@370 | 206 | _env.main_thread = allocate_cap(); |
paul@324 | 207 | |
paul@324 | 208 | ipc_map_capability(_task, (struct ipc_mapped_cap) {_env.main_thread, thread, L4_CAP_FPAGE_RWS}); |
paul@324 | 209 | |
paul@324 | 210 | /* Populate the initial environment in the thread. */ |
paul@324 | 211 | |
paul@324 | 212 | st.set_l4re_aux(&_aux); |
paul@324 | 213 | st.set_l4re_env(&_env); |
paul@324 | 214 | |
paul@324 | 215 | /* Set the start details. */ |
paul@324 | 216 | |
paul@324 | 217 | err = l4_error(l4_thread_ex_regs(thread, program_start, st.start_address(), 0)); |
paul@324 | 218 | |
paul@324 | 219 | if (err) |
paul@324 | 220 | return err; |
paul@324 | 221 | |
paul@324 | 222 | /* Select a new address for the next thread. */ |
paul@324 | 223 | |
paul@324 | 224 | _utcb_start += L4_UTCB_OFFSET; |
paul@324 | 225 | |
paul@324 | 226 | /* Start the thread. */ |
paul@324 | 227 | |
paul@324 | 228 | l4_sched_param_t sp = l4_sched_param(L4RE_MAIN_THREAD_PRIO); |
paul@324 | 229 | |
paul@324 | 230 | return l4_error(l4_scheduler_run_thread(l4re_env()->scheduler, thread, &sp)); |
paul@324 | 231 | } |
paul@324 | 232 | |
paul@324 | 233 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@324 | 234 | */ |