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