paul@477 | 1 | /* |
paul@477 | 2 | * Support for executing code in new tasks and threads. |
paul@477 | 3 | * |
paul@477 | 4 | * Copyright (C) 2022, 2023 Paul Boddie <paul@boddie.org.uk> |
paul@477 | 5 | * |
paul@477 | 6 | * This program is free software; you can redistribute it and/or |
paul@477 | 7 | * modify it under the terms of the GNU General Public License as |
paul@477 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@477 | 9 | * the License, or (at your option) any later version. |
paul@477 | 10 | * |
paul@477 | 11 | * This program is distributed in the hope that it will be useful, |
paul@477 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@477 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@477 | 14 | * GNU General Public License for more details. |
paul@477 | 15 | * |
paul@477 | 16 | * You should have received a copy of the GNU General Public License |
paul@477 | 17 | * along with this program; if not, write to the Free Software |
paul@477 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@477 | 19 | * Boston, MA 02110-1301, USA |
paul@477 | 20 | */ |
paul@477 | 21 | |
paul@477 | 22 | #include <l4/re/env.h> |
paul@477 | 23 | |
paul@489 | 24 | #include <fsserver/resource_server.h> |
paul@477 | 25 | #include <ipc/cap_alloc.h> |
paul@477 | 26 | #include <ipc/map.h> |
paul@477 | 27 | |
paul@477 | 28 | #include <stdio.h> |
paul@477 | 29 | |
paul@483 | 30 | #include "process_creating.h" |
paul@477 | 31 | |
paul@477 | 32 | |
paul@477 | 33 | |
paul@477 | 34 | /* Process stack configuration. */ |
paul@477 | 35 | |
paul@477 | 36 | static const offset_t initial_stack_size = 16 * L4_PAGESIZE; |
paul@477 | 37 | |
paul@477 | 38 | |
paul@477 | 39 | |
paul@477 | 40 | /* Initialise the process creator with the details of a region mapper. */ |
paul@477 | 41 | |
paul@483 | 42 | ProcessCreating::ProcessCreating(const char *rm_filename) |
paul@495 | 43 | : _rm_filename(rm_filename) |
paul@477 | 44 | { |
paul@495 | 45 | _rm_stack = new ExplicitSegment(Utcb_area_start - initial_stack_size, initial_stack_size, L4_FPAGE_RW); |
paul@495 | 46 | _program_stack = new ExplicitSegment(Utcb_area_start - initial_stack_size * 2, initial_stack_size, L4_FPAGE_RW); |
paul@477 | 47 | } |
paul@477 | 48 | |
paul@477 | 49 | /* Initialise the memory segments of the region mapper. These are mapped into |
paul@477 | 50 | this task so that we may access them, allowing the external pager in this |
paul@477 | 51 | task to use them. */ |
paul@477 | 52 | |
paul@483 | 53 | long ProcessCreating::init_region_mapper() |
paul@477 | 54 | { |
paul@477 | 55 | long err = exec_get_payload(_rm_filename, &_rm_payload, true); |
paul@477 | 56 | |
paul@477 | 57 | if (err) |
paul@477 | 58 | return err; |
paul@477 | 59 | |
paul@495 | 60 | return _rm_stack->allocate(true); |
paul@477 | 61 | } |
paul@477 | 62 | |
paul@477 | 63 | /* Initialise the memory segments of the actual program. These are not mapped |
paul@477 | 64 | into this task, instead being accessed by the region mapper in the new |
paul@477 | 65 | task. */ |
paul@477 | 66 | |
paul@483 | 67 | long ProcessCreating::init_program(file_t *file) |
paul@477 | 68 | { |
paul@477 | 69 | long err = exec_get_payload_file(file, &_program_payload, false); |
paul@477 | 70 | |
paul@477 | 71 | if (err) |
paul@477 | 72 | return err; |
paul@477 | 73 | |
paul@495 | 74 | return _program_stack->allocate(true); |
paul@477 | 75 | } |
paul@477 | 76 | |
paul@477 | 77 | /* Initialise an external system-level pager serving the region mapper in a |
paul@477 | 78 | created task. The allocated regions requested by the region mapper are |
paul@477 | 79 | constrained to an area of memory that must not overlap with the area reserved |
paul@477 | 80 | for the program being run. */ |
paul@477 | 81 | |
paul@489 | 82 | long ProcessCreating::init_external_pager(l4_cap_idx_t *pager) |
paul@477 | 83 | { |
paul@487 | 84 | _exec_pager = new ExternalPager(0, 10 * L4_PAGESIZE); |
paul@487 | 85 | |
paul@477 | 86 | /* Initialise pager regions for the region mapper. */ |
paul@477 | 87 | |
paul@477 | 88 | for (unsigned int i = 0; i < _rm_payload->segments(); i++) |
paul@477 | 89 | { |
paul@477 | 90 | if (_rm_payload->segment(i)->loadable()) |
paul@487 | 91 | _exec_pager->add(_rm_payload->segment(i)->region()); |
paul@477 | 92 | } |
paul@477 | 93 | |
paul@477 | 94 | /* Include the region mapper's stack region. */ |
paul@477 | 95 | |
paul@495 | 96 | _exec_pager->add(_rm_stack->region()); |
paul@477 | 97 | |
paul@477 | 98 | /* Start the pager in a separate thread. */ |
paul@477 | 99 | |
paul@492 | 100 | long err = ResourceServer(_exec_pager).start_thread(pager); |
paul@492 | 101 | |
paul@492 | 102 | if (err) |
paul@492 | 103 | return err; |
paul@492 | 104 | |
paul@492 | 105 | return L4_EOK; |
paul@477 | 106 | } |
paul@477 | 107 | |
paul@477 | 108 | /* Configure the environment for the task. */ |
paul@477 | 109 | |
paul@489 | 110 | long ProcessCreating::configure_task(l4_cap_idx_t pager) |
paul@477 | 111 | { |
paul@477 | 112 | long err = _process.configure_task(); |
paul@477 | 113 | |
paul@477 | 114 | if (err) |
paul@477 | 115 | return err; |
paul@477 | 116 | |
paul@489 | 117 | return _process.set_parent(pager); |
paul@477 | 118 | } |
paul@477 | 119 | |
paul@477 | 120 | /* Create an unbound IPC gate for the region mapper and allocate it in the |
paul@477 | 121 | created process. */ |
paul@477 | 122 | |
paul@483 | 123 | long ProcessCreating::create_ipc_gate() |
paul@477 | 124 | { |
paul@477 | 125 | _ipc_gate_cap = _process.allocate_cap(); |
paul@477 | 126 | _ipc_gate = ipc_cap_alloc(); |
paul@477 | 127 | |
paul@477 | 128 | if (l4_is_invalid_cap(_ipc_gate)) |
paul@477 | 129 | return -L4_ENOMEM; |
paul@477 | 130 | |
paul@477 | 131 | return l4_error(l4_factory_create_gate(l4re_env()->factory, _ipc_gate, L4_INVALID_CAP, 0)); |
paul@477 | 132 | } |
paul@477 | 133 | |
paul@477 | 134 | /* Initialise and assign a region in a list to the created process. */ |
paul@477 | 135 | |
paul@483 | 136 | void ProcessCreating::init_region(struct exec_region *regions, |
paul@477 | 137 | struct ipc_mapped_cap *mapped_caps, |
paul@477 | 138 | struct exec_region &r, unsigned int &index) |
paul@477 | 139 | { |
paul@477 | 140 | l4_cap_idx_t mapped_cap = _process.allocate_cap(); |
paul@477 | 141 | |
paul@477 | 142 | mapped_caps[index] = (struct ipc_mapped_cap) {mapped_cap, r.ds, L4_CAP_FPAGE_RWS, 0}; |
paul@477 | 143 | |
paul@477 | 144 | /* Change the region definition to use the allocated capability in the created |
paul@477 | 145 | process. */ |
paul@477 | 146 | |
paul@477 | 147 | regions[index] = r; |
paul@477 | 148 | regions[index].ds = mapped_cap; |
paul@477 | 149 | index++; |
paul@477 | 150 | } |
paul@477 | 151 | |
paul@477 | 152 | /* Initialise the region mapper with details of the payload program regions |
paul@482 | 153 | and of the associated capabilities, configure the region mapper thread, |
paul@482 | 154 | populate its stack, and start the thread. */ |
paul@477 | 155 | |
paul@489 | 156 | long ProcessCreating::start_region_mapper(l4_cap_idx_t pager) |
paul@477 | 157 | { |
paul@477 | 158 | /* Define regions employing dataspaces to provide program segments. */ |
paul@477 | 159 | |
paul@496 | 160 | struct exec_region rm_regions[_program_payload->segments() + 2]; |
paul@477 | 161 | |
paul@477 | 162 | /* Define capabilities for mapping, including region dataspace capabilities, |
paul@477 | 163 | the stack dataspace capability, and the server capability. */ |
paul@477 | 164 | |
paul@496 | 165 | struct ipc_mapped_cap rm_mapped_caps[_program_payload->segments() + 3]; |
paul@477 | 166 | |
paul@477 | 167 | /* Here, the arrays are sized for the maximum number of regions and |
paul@477 | 168 | capabilities, but in practice only the loadable segments are used, leaving |
paul@477 | 169 | fewer elements utilised. A terminating entry is employed to indicate the |
paul@477 | 170 | limit of utilised elements. */ |
paul@477 | 171 | |
paul@477 | 172 | unsigned int rm_index = 0; |
paul@477 | 173 | |
paul@477 | 174 | for (unsigned int i = 0; i < _program_payload->segments(); i++) |
paul@477 | 175 | { |
paul@477 | 176 | Segment *s = _program_payload->segment(i); |
paul@477 | 177 | |
paul@477 | 178 | if (s->loadable()) |
paul@477 | 179 | init_region(rm_regions, rm_mapped_caps, s->exec_region(), rm_index); |
paul@477 | 180 | } |
paul@477 | 181 | |
paul@477 | 182 | /* Introduce the stack region and capability. */ |
paul@477 | 183 | |
paul@495 | 184 | init_region(rm_regions, rm_mapped_caps, _program_stack->exec_region(), rm_index); |
paul@477 | 185 | |
paul@477 | 186 | /* Terminate the region array. */ |
paul@477 | 187 | |
paul@477 | 188 | rm_regions[rm_index] = (struct exec_region) {0, 0, 0, L4_INVALID_CAP}; |
paul@477 | 189 | |
paul@477 | 190 | /* Introduce the server capability and terminate the capability array. */ |
paul@477 | 191 | |
paul@477 | 192 | rm_mapped_caps[rm_index++] = (struct ipc_mapped_cap) {_ipc_gate_cap, _ipc_gate, L4_CAP_FPAGE_RWS, L4_FPAGE_C_OBJ_RIGHTS}; |
paul@477 | 193 | rm_mapped_caps[rm_index] = (struct ipc_mapped_cap) {0, L4_INVALID_CAP, 0, 0}; |
paul@477 | 194 | |
paul@477 | 195 | /* Map these additional capabilities. */ |
paul@477 | 196 | |
paul@477 | 197 | _process.map_capabilities(rm_mapped_caps, false); |
paul@477 | 198 | |
paul@477 | 199 | /* Define the IPC gate as an initial capability to be acquired by the region |
paul@477 | 200 | mapper via the l4re_env API. The capability index is assigned above when |
paul@477 | 201 | mapping the capability and encoded in the entry below. */ |
paul@477 | 202 | |
paul@477 | 203 | l4re_env_cap_entry_t rm_init_caps[] = { |
paul@477 | 204 | l4re_env_cap_entry_t("server", _ipc_gate_cap, L4_CAP_FPAGE_RWS), |
paul@477 | 205 | l4re_env_cap_entry_t() |
paul@477 | 206 | }; |
paul@477 | 207 | |
paul@477 | 208 | /* NOTE: Environment vector is currently not defined. */ |
paul@477 | 209 | |
paul@477 | 210 | const char *envp[] = {NULL}; |
paul@477 | 211 | |
paul@477 | 212 | /* Configure the environment for the thread, specifying the pager (and |
paul@477 | 213 | exception handler plus region mapper). */ |
paul@477 | 214 | |
paul@489 | 215 | long err = _process.configure_thread(pager); |
paul@477 | 216 | |
paul@477 | 217 | if (err) |
paul@477 | 218 | return err; |
paul@477 | 219 | |
paul@477 | 220 | /* Populate a thread stack with argument and environment details for the |
paul@477 | 221 | region mapper, plus the initial server capability and region details. */ |
paul@477 | 222 | |
paul@477 | 223 | const char *argv[] = {_rm_filename}; |
paul@495 | 224 | Stack rm_st(*_rm_stack); |
paul@477 | 225 | |
paul@477 | 226 | rm_st.set_init_caps(rm_init_caps); |
paul@477 | 227 | rm_st.set_regions(rm_regions); |
paul@477 | 228 | rm_st.populate(1, argv, envp); |
paul@477 | 229 | |
paul@477 | 230 | /* Start the region mapper thread in the appropriate stack. */ |
paul@477 | 231 | |
paul@492 | 232 | l4_cap_idx_t thread; |
paul@492 | 233 | err = _process.thread_start(_rm_payload->entry_point(), rm_st, &thread); |
paul@492 | 234 | |
paul@492 | 235 | if (err) |
paul@492 | 236 | return err; |
paul@492 | 237 | |
paul@492 | 238 | ipc_cap_free_um(thread); |
paul@492 | 239 | return L4_EOK; |
paul@477 | 240 | } |
paul@477 | 241 | |
paul@482 | 242 | /* Configure a thread for a program, populate its stack, and start the |
paul@482 | 243 | thread. */ |
paul@482 | 244 | |
paul@483 | 245 | long ProcessCreating::start_program(int argc, const char *argv[]) |
paul@477 | 246 | { |
paul@477 | 247 | /* NOTE: Environment vector is currently not defined. */ |
paul@477 | 248 | |
paul@477 | 249 | const char *envp[] = {NULL}; |
paul@477 | 250 | |
paul@477 | 251 | /* Configure the environment for the thread, specifying the pager (and |
paul@477 | 252 | exception handler plus region mapper). */ |
paul@477 | 253 | |
paul@477 | 254 | long err = _process.configure_thread(_ipc_gate, _ipc_gate_cap); |
paul@477 | 255 | |
paul@477 | 256 | if (err) |
paul@477 | 257 | return err; |
paul@477 | 258 | |
paul@477 | 259 | /* Populate a thread stack with argument and environment details for the |
paul@477 | 260 | actual program. The server capability should be assigned to the region |
paul@477 | 261 | mapper capability slot already. */ |
paul@477 | 262 | |
paul@495 | 263 | Stack program_st(*_program_stack); |
paul@477 | 264 | |
paul@477 | 265 | program_st.populate(argc, argv, envp); |
paul@477 | 266 | |
paul@477 | 267 | /* Start the program thread in the appropriate stack. */ |
paul@477 | 268 | |
paul@492 | 269 | l4_cap_idx_t thread; |
paul@492 | 270 | err = _process.thread_start(_program_payload->entry_point(), program_st, &thread); |
paul@492 | 271 | |
paul@492 | 272 | if (err) |
paul@492 | 273 | return err; |
paul@492 | 274 | |
paul@492 | 275 | ipc_cap_free_um(thread); |
paul@492 | 276 | return L4_EOK; |
paul@477 | 277 | } |
paul@477 | 278 | |
paul@482 | 279 | /* Start a new process for the given payload, providing the indicated program |
paul@487 | 280 | arguments, returning a reference to the pager. */ |
paul@482 | 281 | |
paul@487 | 282 | long ProcessCreating::start(file_t *file, int argc, const char *argv[], |
paul@487 | 283 | l4_cap_idx_t *process) |
paul@477 | 284 | { |
paul@477 | 285 | long err; |
paul@477 | 286 | |
paul@477 | 287 | err = init_region_mapper(); |
paul@477 | 288 | if (err) |
paul@477 | 289 | return err; |
paul@477 | 290 | |
paul@477 | 291 | err = init_program(file); |
paul@477 | 292 | if (err) |
paul@477 | 293 | return err; |
paul@477 | 294 | |
paul@489 | 295 | err = init_external_pager(process); |
paul@477 | 296 | if (err) |
paul@477 | 297 | return err; |
paul@477 | 298 | |
paul@489 | 299 | err = configure_task(*process); |
paul@477 | 300 | if (err) |
paul@477 | 301 | return err; |
paul@477 | 302 | |
paul@477 | 303 | err = create_ipc_gate(); |
paul@477 | 304 | if (err) |
paul@477 | 305 | return err; |
paul@477 | 306 | |
paul@489 | 307 | err = start_region_mapper(*process); |
paul@477 | 308 | if (err) |
paul@477 | 309 | return err; |
paul@477 | 310 | |
paul@487 | 311 | err = start_program(argc, argv); |
paul@487 | 312 | if (err) |
paul@487 | 313 | return err; |
paul@487 | 314 | |
paul@492 | 315 | /* Note the task and IPC gate on the pager object. */ |
paul@492 | 316 | |
paul@492 | 317 | _exec_pager->set_server(*process); |
paul@492 | 318 | _exec_pager->set_task(_process.get_task()); |
paul@492 | 319 | _exec_pager->set_gate(_ipc_gate); |
paul@492 | 320 | |
paul@495 | 321 | /* Discard instances created to initialise the process. The region mapper |
paul@495 | 322 | relies on resources associated with its payload and stack and so these |
paul@495 | 323 | cannot be deleted immediately. |
paul@495 | 324 | |
paul@487 | 325 | NOTE: The region mapper payload could be retained instead of being |
paul@487 | 326 | reconstructed each time. */ |
paul@482 | 327 | |
paul@487 | 328 | delete _program_payload; |
paul@495 | 329 | delete _program_stack; |
paul@487 | 330 | |
paul@487 | 331 | return L4_EOK; |
paul@480 | 332 | } |
paul@480 | 333 | |
paul@477 | 334 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@477 | 335 | */ |