1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/libexec/lib/src/process.cc Tue May 10 22:39:15 2022 +0200
1.3 @@ -0,0 +1,193 @@
1.4 +/*
1.5 + * Support for initialising programs in new tasks and threads.
1.6 + *
1.7 + * Copyright (C) 2022 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#include <l4/sys/err.h>
1.26 +#include <l4/sys/scheduler.h>
1.27 +
1.28 +#include <exec/process.h>
1.29 +#include <ipc/cap_alloc.h>
1.30 +#include <ipc/map.h>
1.31 +#include <mem/memory_utils.h>
1.32 +
1.33 +
1.34 +
1.35 +/* Obtain a flexpage defining the UTCB area location and size in a new task. */
1.36 +
1.37 +static l4_fpage_t get_utcb_fpage()
1.38 +{
1.39 + /* UTCB location and size. */
1.40 +
1.41 + int utcb_log2size = page_order(Default_max_threads * L4_UTCB_OFFSET);
1.42 +
1.43 + /* Round up to at least one page. */
1.44 +
1.45 + if (utcb_log2size < L4_PAGESHIFT)
1.46 + utcb_log2size = L4_PAGESHIFT;
1.47 +
1.48 + return l4_fpage(Utcb_area_start, utcb_log2size, 0);
1.49 +}
1.50 +
1.51 +
1.52 +
1.53 +/* Initialise a new process, this being an abstraction for a new task with some
1.54 + threads. */
1.55 +
1.56 +Process::Process()
1.57 +{
1.58 + /* Obtain UTCB area details for the task. */
1.59 +
1.60 + l4_fpage_t utcb_fpage = get_utcb_fpage();
1.61 +
1.62 + _utcb_start = l4_fpage_memaddr(utcb_fpage);
1.63 +
1.64 + /* Populate the common initial environment for the threads. */
1.65 +
1.66 + _env.factory = L4_BASE_FACTORY_CAP;
1.67 + _env.main_thread = L4_BASE_THREAD_CAP;
1.68 + _env.log = L4_BASE_LOG_CAP;
1.69 + _env.scheduler = L4_BASE_SCHEDULER_CAP;
1.70 + _env.rm = L4_EXEC_RM_CAP;
1.71 + _env.mem_alloc = L4_EXEC_MA_CAP;
1.72 + _env.first_free_cap = L4_EXEC_FIRST_FREE_CAP_INDEX;
1.73 + _env.utcb_area = utcb_fpage;
1.74 + _env.first_free_utcb = l4_fpage_memaddr(utcb_fpage) + L4_UTCB_OFFSET;
1.75 +
1.76 + /* Populate auxiliary information. */
1.77 +
1.78 + _aux.kip_ds = L4_EXEC_KIP_CAP;
1.79 + _aux.dbg_lvl = 0;
1.80 + _aux.ldr_flags = 0;
1.81 +}
1.82 +
1.83 +/* Task and thread initialisation. */
1.84 +
1.85 +long Process::create_task()
1.86 +{
1.87 + _task = ipc_cap_alloc();
1.88 +
1.89 + if (l4_is_invalid_cap(_task))
1.90 + return -L4_ENOMEM;
1.91 +
1.92 + return l4_error(l4_factory_create_task(l4re_env()->factory, _task, _env.utcb_area));
1.93 +}
1.94 +
1.95 +long Process::create_thread(l4_cap_idx_t *thread)
1.96 +{
1.97 + *thread = ipc_cap_alloc();
1.98 +
1.99 + if (l4_is_invalid_cap(*thread))
1.100 + return -L4_ENOMEM;
1.101 +
1.102 + return l4_error(l4_factory_create_thread(l4re_env()->factory, *thread));
1.103 +}
1.104 +
1.105 +/* Configure the task environment. */
1.106 +
1.107 +long Process::configure(l4_cap_idx_t server)
1.108 +{
1.109 + long err = create_task();
1.110 +
1.111 + if (err)
1.112 + return err;
1.113 +
1.114 + /* Map the KIP into the task. */
1.115 +
1.116 + l4_addr_t kip_start = (l4_addr_t) l4re_kip();
1.117 +
1.118 + err = l4_error(l4_task_map(_task, L4RE_THIS_TASK_CAP,
1.119 + l4_fpage(kip_start, L4_PAGESHIFT, L4_FPAGE_RX),
1.120 + kip_start));
1.121 +
1.122 + if (err)
1.123 + return err;
1.124 +
1.125 + /* Define capability mappings for the new task. */
1.126 +
1.127 + struct ipc_mapped_cap mapped_caps[] = {
1.128 + {L4_BASE_TASK_CAP, _task, L4_CAP_FPAGE_RWS},
1.129 + {L4_EXEC_PAGER_CAP, server, L4_CAP_FPAGE_RWS},
1.130 + {_env.rm, server, L4_CAP_FPAGE_RWS},
1.131 + {_env.factory, l4re_env()->factory, L4_CAP_FPAGE_RWS},
1.132 + {_env.log, l4re_env()->log, L4_CAP_FPAGE_RWS},
1.133 + {_env.scheduler, l4re_env()->scheduler, L4_CAP_FPAGE_RWS},
1.134 + {_env.mem_alloc, l4re_env()->mem_alloc, L4_CAP_FPAGE_RWS},
1.135 + {0, L4_INVALID_CAP, 0},
1.136 + };
1.137 +
1.138 + return ipc_map_capabilities(_task, mapped_caps);
1.139 +}
1.140 +
1.141 +/* Create, initialise and start a thread. */
1.142 +
1.143 +long Process::thread_start(l4_addr_t program_start, Stack &st)
1.144 +{
1.145 + l4_cap_idx_t thread;
1.146 + long err;
1.147 +
1.148 + err = create_thread(&thread);
1.149 +
1.150 + if (err)
1.151 + return err;
1.152 +
1.153 + /* Initialise the thread with pager, UTCB and task details. */
1.154 +
1.155 + l4_thread_control_start();
1.156 + l4_thread_control_pager(L4_EXEC_PAGER_CAP);
1.157 + l4_thread_control_exc_handler(L4_EXEC_PAGER_CAP);
1.158 + l4_thread_control_bind((l4_utcb_t *) _utcb_start, _task);
1.159 +
1.160 + err = l4_error(l4_thread_control_commit(thread));
1.161 +
1.162 + if (err)
1.163 + {
1.164 + ipc_cap_free(thread);
1.165 + return err;
1.166 + }
1.167 +
1.168 + /* Map the thread capability to the task. */
1.169 +
1.170 + ipc_map_capability(_task, (struct ipc_mapped_cap) {_env.main_thread, thread, L4_CAP_FPAGE_RWS});
1.171 +
1.172 + /* Populate the initial environment in the thread. */
1.173 +
1.174 + st.set_l4re_aux(&_aux);
1.175 + st.set_l4re_env(&_env);
1.176 +
1.177 + /* Set the start details. */
1.178 +
1.179 + err = l4_error(l4_thread_ex_regs(thread, program_start, st.start_address(), 0));
1.180 +
1.181 + if (err)
1.182 + return err;
1.183 +
1.184 + /* Select a new address for the next thread. */
1.185 +
1.186 + _utcb_start += L4_UTCB_OFFSET;
1.187 +
1.188 + /* Start the thread. */
1.189 +
1.190 + l4_sched_param_t sp = l4_sched_param(L4RE_MAIN_THREAD_PRIO);
1.191 +
1.192 + return l4_error(l4_scheduler_run_thread(l4re_env()->scheduler, thread, &sp));
1.193 +}
1.194 +
1.195 +/* vim: tabstop=2 expandtab shiftwidth=2
1.196 +*/