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