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