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