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.main_thread = L4_BASE_THREAD_CAP; |
paul@324 | 65 | _env.log = L4_BASE_LOG_CAP; |
paul@324 | 66 | _env.scheduler = L4_BASE_SCHEDULER_CAP; |
paul@324 | 67 | _env.rm = L4_EXEC_RM_CAP; |
paul@324 | 68 | _env.mem_alloc = L4_EXEC_MA_CAP; |
paul@324 | 69 | _env.first_free_cap = L4_EXEC_FIRST_FREE_CAP_INDEX; |
paul@324 | 70 | _env.utcb_area = utcb_fpage; |
paul@365 | 71 | _env.first_free_utcb = l4_fpage_memaddr(utcb_fpage) + reserved_threads * L4_UTCB_OFFSET; |
paul@324 | 72 | |
paul@324 | 73 | /* Populate auxiliary information. */ |
paul@324 | 74 | |
paul@324 | 75 | _aux.kip_ds = L4_EXEC_KIP_CAP; |
paul@324 | 76 | _aux.dbg_lvl = 0; |
paul@324 | 77 | _aux.ldr_flags = 0; |
paul@324 | 78 | } |
paul@324 | 79 | |
paul@324 | 80 | /* Task and thread initialisation. */ |
paul@324 | 81 | |
paul@324 | 82 | long Process::create_task() |
paul@324 | 83 | { |
paul@324 | 84 | _task = ipc_cap_alloc(); |
paul@324 | 85 | |
paul@324 | 86 | if (l4_is_invalid_cap(_task)) |
paul@324 | 87 | return -L4_ENOMEM; |
paul@324 | 88 | |
paul@324 | 89 | return l4_error(l4_factory_create_task(l4re_env()->factory, _task, _env.utcb_area)); |
paul@324 | 90 | } |
paul@324 | 91 | |
paul@324 | 92 | long Process::create_thread(l4_cap_idx_t *thread) |
paul@324 | 93 | { |
paul@324 | 94 | *thread = ipc_cap_alloc(); |
paul@324 | 95 | |
paul@324 | 96 | if (l4_is_invalid_cap(*thread)) |
paul@324 | 97 | return -L4_ENOMEM; |
paul@324 | 98 | |
paul@324 | 99 | return l4_error(l4_factory_create_thread(l4re_env()->factory, *thread)); |
paul@324 | 100 | } |
paul@324 | 101 | |
paul@324 | 102 | /* Configure the task environment. */ |
paul@324 | 103 | |
paul@365 | 104 | long Process::configure_task() |
paul@324 | 105 | { |
paul@324 | 106 | long err = create_task(); |
paul@324 | 107 | |
paul@324 | 108 | if (err) |
paul@324 | 109 | return err; |
paul@324 | 110 | |
paul@324 | 111 | /* Map the KIP into the task. */ |
paul@324 | 112 | |
paul@324 | 113 | l4_addr_t kip_start = (l4_addr_t) l4re_kip(); |
paul@324 | 114 | |
paul@324 | 115 | err = l4_error(l4_task_map(_task, L4RE_THIS_TASK_CAP, |
paul@324 | 116 | l4_fpage(kip_start, L4_PAGESHIFT, L4_FPAGE_RX), |
paul@324 | 117 | kip_start)); |
paul@324 | 118 | |
paul@324 | 119 | if (err) |
paul@324 | 120 | return err; |
paul@324 | 121 | |
paul@324 | 122 | /* Define capability mappings for the new task. */ |
paul@324 | 123 | |
paul@324 | 124 | struct ipc_mapped_cap mapped_caps[] = { |
paul@324 | 125 | {L4_BASE_TASK_CAP, _task, L4_CAP_FPAGE_RWS}, |
paul@324 | 126 | {_env.factory, l4re_env()->factory, L4_CAP_FPAGE_RWS}, |
paul@324 | 127 | {_env.log, l4re_env()->log, L4_CAP_FPAGE_RWS}, |
paul@324 | 128 | {_env.scheduler, l4re_env()->scheduler, L4_CAP_FPAGE_RWS}, |
paul@324 | 129 | {_env.mem_alloc, l4re_env()->mem_alloc, L4_CAP_FPAGE_RWS}, |
paul@324 | 130 | {0, L4_INVALID_CAP, 0}, |
paul@324 | 131 | }; |
paul@324 | 132 | |
paul@365 | 133 | return map_capabilities(mapped_caps, false); |
paul@365 | 134 | } |
paul@365 | 135 | |
paul@365 | 136 | /* Configure the thread environment. */ |
paul@365 | 137 | |
paul@365 | 138 | long Process::configure_thread(l4_cap_idx_t server) |
paul@365 | 139 | { |
paul@365 | 140 | struct ipc_mapped_cap mapped_caps[] = { |
paul@365 | 141 | {L4_EXEC_PAGER_CAP, server, L4_CAP_FPAGE_RWS}, |
paul@365 | 142 | {_env.rm, server, L4_CAP_FPAGE_RWS}, |
paul@365 | 143 | {0, L4_INVALID_CAP, 0}, |
paul@365 | 144 | }; |
paul@365 | 145 | |
paul@365 | 146 | return map_capabilities(mapped_caps, false); |
paul@365 | 147 | } |
paul@365 | 148 | |
paul@365 | 149 | /* Map capabilities into the task, counting them if indicated. */ |
paul@365 | 150 | |
paul@365 | 151 | long Process::map_capabilities(struct ipc_mapped_cap mapped_caps[], |
paul@365 | 152 | bool to_count) |
paul@365 | 153 | { |
paul@365 | 154 | return ipc_map_capabilities(_task, mapped_caps, to_count ? &_num_mapped_caps : NULL); |
paul@324 | 155 | } |
paul@324 | 156 | |
paul@324 | 157 | /* Create, initialise and start a thread. */ |
paul@324 | 158 | |
paul@324 | 159 | long Process::thread_start(l4_addr_t program_start, Stack &st) |
paul@324 | 160 | { |
paul@324 | 161 | l4_cap_idx_t thread; |
paul@324 | 162 | long err; |
paul@324 | 163 | |
paul@324 | 164 | err = create_thread(&thread); |
paul@324 | 165 | |
paul@324 | 166 | if (err) |
paul@324 | 167 | return err; |
paul@324 | 168 | |
paul@324 | 169 | /* Initialise the thread with pager, UTCB and task details. */ |
paul@324 | 170 | |
paul@324 | 171 | l4_thread_control_start(); |
paul@324 | 172 | l4_thread_control_pager(L4_EXEC_PAGER_CAP); |
paul@324 | 173 | l4_thread_control_exc_handler(L4_EXEC_PAGER_CAP); |
paul@324 | 174 | l4_thread_control_bind((l4_utcb_t *) _utcb_start, _task); |
paul@324 | 175 | |
paul@324 | 176 | err = l4_error(l4_thread_control_commit(thread)); |
paul@324 | 177 | |
paul@324 | 178 | if (err) |
paul@324 | 179 | { |
paul@324 | 180 | ipc_cap_free(thread); |
paul@324 | 181 | return err; |
paul@324 | 182 | } |
paul@324 | 183 | |
paul@324 | 184 | /* Map the thread capability to the task. */ |
paul@324 | 185 | |
paul@324 | 186 | ipc_map_capability(_task, (struct ipc_mapped_cap) {_env.main_thread, thread, L4_CAP_FPAGE_RWS}); |
paul@324 | 187 | |
paul@365 | 188 | /* Update the environment for any mapped capabilities. */ |
paul@365 | 189 | |
paul@365 | 190 | _env.first_free_cap = L4_EXEC_FIRST_FREE_CAP_INDEX + _num_mapped_caps; |
paul@365 | 191 | |
paul@324 | 192 | /* Populate the initial environment in the thread. */ |
paul@324 | 193 | |
paul@324 | 194 | st.set_l4re_aux(&_aux); |
paul@324 | 195 | st.set_l4re_env(&_env); |
paul@324 | 196 | |
paul@324 | 197 | /* Set the start details. */ |
paul@324 | 198 | |
paul@324 | 199 | err = l4_error(l4_thread_ex_regs(thread, program_start, st.start_address(), 0)); |
paul@324 | 200 | |
paul@324 | 201 | if (err) |
paul@324 | 202 | return err; |
paul@324 | 203 | |
paul@324 | 204 | /* Select a new address for the next thread. */ |
paul@324 | 205 | |
paul@324 | 206 | _utcb_start += L4_UTCB_OFFSET; |
paul@324 | 207 | |
paul@324 | 208 | /* Start the thread. */ |
paul@324 | 209 | |
paul@324 | 210 | l4_sched_param_t sp = l4_sched_param(L4RE_MAIN_THREAD_PRIO); |
paul@324 | 211 | |
paul@324 | 212 | return l4_error(l4_scheduler_run_thread(l4re_env()->scheduler, thread, &sp)); |
paul@324 | 213 | } |
paul@324 | 214 | |
paul@324 | 215 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@324 | 216 | */ |