paul@477 | 1 | /* |
paul@477 | 2 | * Support for executing code in new tasks and threads. |
paul@477 | 3 | * |
paul@610 | 4 | * Copyright (C) 2022, 2023, 2024 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@506 | 24 | #include <exec/common.h> |
paul@505 | 25 | #include <fsclient/client.h> |
paul@477 | 26 | #include <ipc/cap_alloc.h> |
paul@477 | 27 | #include <ipc/map.h> |
paul@535 | 28 | #include <resource/resource_server.h> |
paul@511 | 29 | #include <systypes/env.h> |
paul@505 | 30 | #include <systypes/fcntl.h> |
paul@477 | 31 | |
paul@477 | 32 | #include <stdio.h> |
paul@477 | 33 | |
paul@483 | 34 | #include "process_creating.h" |
paul@477 | 35 | |
paul@477 | 36 | |
paul@477 | 37 | |
paul@477 | 38 | /* Process stack configuration. */ |
paul@477 | 39 | |
paul@477 | 40 | static const offset_t initial_stack_size = 16 * L4_PAGESIZE; |
paul@477 | 41 | |
paul@477 | 42 | |
paul@477 | 43 | |
paul@477 | 44 | /* Initialise the process creator with the details of a region mapper. */ |
paul@477 | 45 | |
paul@527 | 46 | ProcessCreating::ProcessCreating(const char *rm_filename, file_t *rm_file) |
paul@527 | 47 | : _rm_filename(rm_filename), _rm_file(rm_file) |
paul@477 | 48 | { |
paul@477 | 49 | } |
paul@477 | 50 | |
paul@477 | 51 | /* Initialise the memory segments of the region mapper. These are mapped into |
paul@477 | 52 | this task so that we may access them, allowing the external pager in this |
paul@477 | 53 | task to use them. */ |
paul@477 | 54 | |
paul@483 | 55 | long ProcessCreating::init_region_mapper() |
paul@477 | 56 | { |
paul@527 | 57 | long err = exec_get_payload_file(_rm_file, &_rm_payload, true); |
paul@477 | 58 | |
paul@477 | 59 | if (err) |
paul@477 | 60 | return err; |
paul@477 | 61 | |
paul@625 | 62 | /* Introduce a page as a barrier between the UTCB and the stack. */ |
paul@625 | 63 | |
paul@625 | 64 | _rm_stack = new ExplicitSegment(Utcb_area_start - L4_PAGESIZE - |
paul@625 | 65 | initial_stack_size, initial_stack_size, L4_FPAGE_RW); |
paul@625 | 66 | |
paul@495 | 67 | return _rm_stack->allocate(true); |
paul@477 | 68 | } |
paul@477 | 69 | |
paul@477 | 70 | /* Initialise the memory segments of the actual program. These are not mapped |
paul@477 | 71 | into this task, instead being accessed by the region mapper in the new |
paul@477 | 72 | task. */ |
paul@477 | 73 | |
paul@483 | 74 | long ProcessCreating::init_program(file_t *file) |
paul@477 | 75 | { |
paul@477 | 76 | long err = exec_get_payload_file(file, &_program_payload, false); |
paul@477 | 77 | |
paul@477 | 78 | if (err) |
paul@477 | 79 | return err; |
paul@477 | 80 | |
paul@625 | 81 | /* Introduce a page as a barrier between the program stack and the region |
paul@625 | 82 | mapper stack. */ |
paul@625 | 83 | |
paul@625 | 84 | _program_stack = new ExplicitSegment(Utcb_area_start - L4_PAGESIZE * 2 - |
paul@625 | 85 | initial_stack_size * 2, initial_stack_size, L4_FPAGE_RW); |
paul@625 | 86 | |
paul@495 | 87 | return _program_stack->allocate(true); |
paul@477 | 88 | } |
paul@477 | 89 | |
paul@477 | 90 | /* Initialise an external system-level pager serving the region mapper in a |
paul@477 | 91 | created task. The allocated regions requested by the region mapper are |
paul@477 | 92 | constrained to an area of memory that must not overlap with the area reserved |
paul@477 | 93 | for the program being run. */ |
paul@477 | 94 | |
paul@489 | 95 | long ProcessCreating::init_external_pager(l4_cap_idx_t *pager) |
paul@477 | 96 | { |
paul@595 | 97 | _exec_pager = new ExternalPager(0, MEM_INTERNAL_PAGER_LIMIT); |
paul@559 | 98 | _exec_pager->set_monitor(_monitor); |
paul@487 | 99 | |
paul@477 | 100 | /* Initialise pager regions for the region mapper. */ |
paul@477 | 101 | |
paul@477 | 102 | for (unsigned int i = 0; i < _rm_payload->segments(); i++) |
paul@477 | 103 | { |
paul@477 | 104 | if (_rm_payload->segment(i)->loadable()) |
paul@487 | 105 | _exec_pager->add(_rm_payload->segment(i)->region()); |
paul@477 | 106 | } |
paul@477 | 107 | |
paul@477 | 108 | /* Include the region mapper's stack region. */ |
paul@477 | 109 | |
paul@495 | 110 | _exec_pager->add(_rm_stack->region()); |
paul@504 | 111 | _exec_pager->set_payload(_rm_payload); |
paul@504 | 112 | _exec_pager->set_stack(_rm_stack); |
paul@477 | 113 | |
paul@477 | 114 | /* Start the pager in a separate thread. */ |
paul@477 | 115 | |
paul@492 | 116 | long err = ResourceServer(_exec_pager).start_thread(pager); |
paul@492 | 117 | |
paul@492 | 118 | if (err) |
paul@492 | 119 | return err; |
paul@492 | 120 | |
paul@492 | 121 | return L4_EOK; |
paul@477 | 122 | } |
paul@477 | 123 | |
paul@543 | 124 | /* Initialise a resource to receive signals from the process. */ |
paul@543 | 125 | |
paul@543 | 126 | long ProcessCreating::init_process_monitor(l4_cap_idx_t *monitor) |
paul@543 | 127 | { |
paul@543 | 128 | _monitor = new ProcessMonitor; |
paul@543 | 129 | |
paul@543 | 130 | /* Start the monitor in a separate thread. */ |
paul@543 | 131 | |
paul@559 | 132 | return ResourceServer(_monitor).start_thread(monitor); |
paul@543 | 133 | } |
paul@543 | 134 | |
paul@477 | 135 | /* Configure the environment for the task. */ |
paul@477 | 136 | |
paul@543 | 137 | long ProcessCreating::configure_task() |
paul@477 | 138 | { |
paul@503 | 139 | l4_cap_idx_t task, mapped_task; |
paul@503 | 140 | long err = _process.configure_task(&task, &mapped_task); |
paul@477 | 141 | |
paul@477 | 142 | if (err) |
paul@477 | 143 | return err; |
paul@477 | 144 | |
paul@543 | 145 | /* Record the task details elsewhere for eventual resource deallocation. */ |
paul@503 | 146 | |
paul@503 | 147 | _exec_pager->set_task(task, mapped_task); |
paul@543 | 148 | _monitor->set_task(task, mapped_task); |
paul@499 | 149 | return L4_EOK; |
paul@477 | 150 | } |
paul@477 | 151 | |
paul@543 | 152 | /* Create an unbound IPC gate for the internal pager/region mapper and allocate |
paul@543 | 153 | it in the created process. */ |
paul@477 | 154 | |
paul@543 | 155 | long ProcessCreating::allocate_internal_pager() |
paul@477 | 156 | { |
paul@543 | 157 | _mapped_internal_pager = _process.allocate_cap(); |
paul@543 | 158 | _internal_pager = ipc_cap_alloc(); |
paul@477 | 159 | |
paul@543 | 160 | if (l4_is_invalid_cap(_internal_pager)) |
paul@477 | 161 | return -L4_ENOMEM; |
paul@477 | 162 | |
paul@543 | 163 | return l4_error(l4_factory_create_gate(l4re_env()->factory, _internal_pager, L4_INVALID_CAP, 0)); |
paul@477 | 164 | } |
paul@477 | 165 | |
paul@477 | 166 | /* Initialise and assign a region in a list to the created process. */ |
paul@477 | 167 | |
paul@483 | 168 | void ProcessCreating::init_region(struct exec_region *regions, |
paul@543 | 169 | struct ipc_mapped_cap *mapped_caps, |
paul@543 | 170 | struct exec_region &r, unsigned int &index) |
paul@477 | 171 | { |
paul@477 | 172 | l4_cap_idx_t mapped_cap = _process.allocate_cap(); |
paul@477 | 173 | |
paul@477 | 174 | mapped_caps[index] = (struct ipc_mapped_cap) {mapped_cap, r.ds, L4_CAP_FPAGE_RWS, 0}; |
paul@477 | 175 | |
paul@477 | 176 | /* Change the region definition to use the allocated capability in the created |
paul@477 | 177 | process. */ |
paul@477 | 178 | |
paul@477 | 179 | regions[index] = r; |
paul@477 | 180 | regions[index].ds = mapped_cap; |
paul@477 | 181 | index++; |
paul@477 | 182 | } |
paul@477 | 183 | |
paul@477 | 184 | /* Initialise the region mapper with details of the payload program regions |
paul@482 | 185 | and of the associated capabilities, configure the region mapper thread, |
paul@482 | 186 | populate its stack, and start the thread. */ |
paul@477 | 187 | |
paul@489 | 188 | long ProcessCreating::start_region_mapper(l4_cap_idx_t pager) |
paul@477 | 189 | { |
paul@477 | 190 | /* Define regions employing dataspaces to provide program segments. */ |
paul@477 | 191 | |
paul@496 | 192 | struct exec_region rm_regions[_program_payload->segments() + 2]; |
paul@477 | 193 | |
paul@477 | 194 | /* Define capabilities for mapping, including region dataspace capabilities, |
paul@511 | 195 | the stack dataspace capability, plus the pager capability. */ |
paul@477 | 196 | |
paul@496 | 197 | struct ipc_mapped_cap rm_mapped_caps[_program_payload->segments() + 3]; |
paul@477 | 198 | |
paul@477 | 199 | /* Here, the arrays are sized for the maximum number of regions and |
paul@477 | 200 | capabilities, but in practice only the loadable segments are used, leaving |
paul@477 | 201 | fewer elements utilised. A terminating entry is employed to indicate the |
paul@477 | 202 | limit of utilised elements. */ |
paul@477 | 203 | |
paul@477 | 204 | unsigned int rm_index = 0; |
paul@477 | 205 | |
paul@477 | 206 | for (unsigned int i = 0; i < _program_payload->segments(); i++) |
paul@477 | 207 | { |
paul@477 | 208 | Segment *s = _program_payload->segment(i); |
paul@477 | 209 | |
paul@477 | 210 | if (s->loadable()) |
paul@477 | 211 | init_region(rm_regions, rm_mapped_caps, s->exec_region(), rm_index); |
paul@477 | 212 | } |
paul@477 | 213 | |
paul@477 | 214 | /* Introduce the stack region and capability. */ |
paul@477 | 215 | |
paul@495 | 216 | init_region(rm_regions, rm_mapped_caps, _program_stack->exec_region(), rm_index); |
paul@477 | 217 | |
paul@477 | 218 | /* Terminate the region array. */ |
paul@477 | 219 | |
paul@477 | 220 | rm_regions[rm_index] = (struct exec_region) {0, 0, 0, L4_INVALID_CAP}; |
paul@477 | 221 | |
paul@543 | 222 | /* Introduce the internal pager capability and terminate the capability array. */ |
paul@477 | 223 | |
paul@543 | 224 | rm_mapped_caps[rm_index++] = (struct ipc_mapped_cap) {_mapped_internal_pager, |
paul@543 | 225 | _internal_pager, L4_CAP_FPAGE_RWS, |
paul@543 | 226 | L4_FPAGE_C_OBJ_RIGHTS}; |
paul@610 | 227 | rm_mapped_caps[rm_index] = (struct ipc_mapped_cap) {L4_INVALID_CAP, L4_INVALID_CAP, 0, 0}; |
paul@477 | 228 | |
paul@477 | 229 | /* Map these additional capabilities. */ |
paul@477 | 230 | |
paul@511 | 231 | long err = _process.map_capabilities(rm_mapped_caps, false); |
paul@511 | 232 | |
paul@511 | 233 | if (err) |
paul@511 | 234 | return err; |
paul@477 | 235 | |
paul@477 | 236 | /* Define the IPC gate as an initial capability to be acquired by the region |
paul@477 | 237 | mapper via the l4re_env API. The capability index is assigned above when |
paul@477 | 238 | mapping the capability and encoded in the entry below. */ |
paul@477 | 239 | |
paul@477 | 240 | l4re_env_cap_entry_t rm_init_caps[] = { |
paul@543 | 241 | l4re_env_cap_entry_t(ENV_INTERNAL_PAGER_NAME, _mapped_internal_pager, L4_CAP_FPAGE_RWS), |
paul@477 | 242 | l4re_env_cap_entry_t() |
paul@477 | 243 | }; |
paul@477 | 244 | |
paul@477 | 245 | /* NOTE: Environment vector is currently not defined. */ |
paul@477 | 246 | |
paul@477 | 247 | const char *envp[] = {NULL}; |
paul@477 | 248 | |
paul@477 | 249 | /* Configure the environment for the thread, specifying the pager (and |
paul@477 | 250 | exception handler plus region mapper). */ |
paul@477 | 251 | |
paul@503 | 252 | l4_cap_idx_t mapped_pager = L4_INVALID_CAP; |
paul@543 | 253 | err = _process.set_pager(pager, &mapped_pager); |
paul@477 | 254 | |
paul@477 | 255 | if (err) |
paul@477 | 256 | return err; |
paul@477 | 257 | |
paul@503 | 258 | _exec_pager->set_pager(pager, mapped_pager); |
paul@499 | 259 | |
paul@543 | 260 | /* Note the pager as the parent of the new task, recording its capability |
paul@543 | 261 | details in the new task. */ |
paul@543 | 262 | |
paul@543 | 263 | err = _process.set_parent(pager, &mapped_pager); |
paul@543 | 264 | |
paul@543 | 265 | if (err) |
paul@543 | 266 | return err; |
paul@543 | 267 | |
paul@543 | 268 | _exec_pager->set_parent(pager, mapped_pager); |
paul@543 | 269 | |
paul@477 | 270 | /* Populate a thread stack with argument and environment details for the |
paul@477 | 271 | region mapper, plus the initial server capability and region details. */ |
paul@477 | 272 | |
paul@477 | 273 | const char *argv[] = {_rm_filename}; |
paul@495 | 274 | Stack rm_st(*_rm_stack); |
paul@477 | 275 | |
paul@477 | 276 | rm_st.set_init_caps(rm_init_caps); |
paul@477 | 277 | rm_st.set_regions(rm_regions); |
paul@477 | 278 | rm_st.populate(1, argv, envp); |
paul@477 | 279 | |
paul@477 | 280 | /* Start the region mapper thread in the appropriate stack. */ |
paul@477 | 281 | |
paul@503 | 282 | l4_cap_idx_t thread, mapped_thread; |
paul@503 | 283 | err = _process.thread_start(_rm_payload->entry_point(), rm_st, &thread, &mapped_thread); |
paul@492 | 284 | |
paul@492 | 285 | if (err) |
paul@492 | 286 | return err; |
paul@492 | 287 | |
paul@543 | 288 | _exec_pager->set_thread(thread, mapped_thread); |
paul@492 | 289 | return L4_EOK; |
paul@477 | 290 | } |
paul@477 | 291 | |
paul@482 | 292 | /* Configure a thread for a program, populate its stack, and start the |
paul@482 | 293 | thread. */ |
paul@482 | 294 | |
paul@584 | 295 | long ProcessCreating::start_program(l4_cap_idx_t monitor, int argc, |
paul@615 | 296 | const char *argv[], l4_cap_idx_t reader, |
paul@615 | 297 | l4_cap_idx_t writer) |
paul@477 | 298 | { |
paul@477 | 299 | /* NOTE: Environment vector is currently not defined. */ |
paul@477 | 300 | |
paul@477 | 301 | const char *envp[] = {NULL}; |
paul@477 | 302 | |
paul@477 | 303 | /* Configure the environment for the thread, specifying the pager (and |
paul@477 | 304 | exception handler plus region mapper). */ |
paul@477 | 305 | |
paul@543 | 306 | l4_cap_idx_t mapped_pager = _mapped_internal_pager; |
paul@543 | 307 | long err = _process.set_pager(_internal_pager, &mapped_pager); |
paul@477 | 308 | |
paul@477 | 309 | if (err) |
paul@477 | 310 | return err; |
paul@477 | 311 | |
paul@543 | 312 | _monitor->set_pager(_internal_pager, _mapped_internal_pager); |
paul@543 | 313 | |
paul@543 | 314 | /* Note the monitor as the parent of the new task, recording its capability |
paul@543 | 315 | details in the new task. */ |
paul@543 | 316 | |
paul@543 | 317 | l4_cap_idx_t mapped_parent = L4_INVALID_CAP; |
paul@543 | 318 | err = _process.set_parent(monitor, &mapped_parent); |
paul@543 | 319 | |
paul@543 | 320 | if (err) |
paul@543 | 321 | return err; |
paul@543 | 322 | |
paul@543 | 323 | _monitor->set_parent(monitor, mapped_parent); |
paul@543 | 324 | |
paul@511 | 325 | /* Obtain the filesystem capability for exporting to the task. */ |
paul@511 | 326 | |
paul@511 | 327 | l4_cap_idx_t fsserver_cap = _process.allocate_cap(); |
paul@511 | 328 | l4_cap_idx_t fsserver = l4re_env_get_cap(ENV_FILESYSTEM_SERVER_NAME); |
paul@511 | 329 | |
paul@662 | 330 | /* Obtain the process server capability. */ |
paul@662 | 331 | |
paul@662 | 332 | l4_cap_idx_t prserver_cap = _process.allocate_cap(); |
paul@662 | 333 | l4_cap_idx_t prserver = l4re_env_get_cap(ENV_PROCESS_SERVER_NAME); |
paul@662 | 334 | |
paul@615 | 335 | /* Also reserve capabilities for the reader and writer. If the reader or |
paul@615 | 336 | writer are invalid capabilities, these will not actually be transferred. */ |
paul@584 | 337 | |
paul@615 | 338 | l4_cap_idx_t reader_cap = _process.allocate_cap(); |
paul@584 | 339 | l4_cap_idx_t writer_cap = _process.allocate_cap(); |
paul@584 | 340 | |
paul@511 | 341 | /* Define the capabilities to be mapped for the filesystem. */ |
paul@511 | 342 | |
paul@511 | 343 | struct ipc_mapped_cap program_mapped_caps[] = { |
paul@511 | 344 | {fsserver_cap, fsserver, L4_CAP_FPAGE_RWS, L4_FPAGE_C_OBJ_RIGHTS}, |
paul@662 | 345 | {prserver_cap, prserver, L4_CAP_FPAGE_RWS, L4_FPAGE_C_OBJ_RIGHTS}, |
paul@615 | 346 | {reader_cap, reader, L4_CAP_FPAGE_RWS, L4_FPAGE_C_OBJ_RIGHTS}, |
paul@584 | 347 | {writer_cap, writer, L4_CAP_FPAGE_RWS, L4_FPAGE_C_OBJ_RIGHTS}, |
paul@610 | 348 | {L4_INVALID_CAP, L4_INVALID_CAP, 0, 0}, |
paul@511 | 349 | }; |
paul@511 | 350 | |
paul@511 | 351 | /* Map these additional capabilities. */ |
paul@511 | 352 | |
paul@511 | 353 | err = _process.map_capabilities(program_mapped_caps, false); |
paul@511 | 354 | |
paul@511 | 355 | if (err) |
paul@511 | 356 | return err; |
paul@511 | 357 | |
paul@511 | 358 | /* Define initial capabilities to be acquired by the region mapper via the |
paul@511 | 359 | l4re_env API. Each capability index is assigned above when mapping the |
paul@511 | 360 | capability and encoded in the entry below. */ |
paul@511 | 361 | |
paul@615 | 362 | if (l4_is_invalid_cap(reader)) |
paul@615 | 363 | reader_cap = L4_INVALID_CAP; |
paul@615 | 364 | |
paul@615 | 365 | if (l4_is_invalid_cap(writer)) |
paul@615 | 366 | writer_cap = L4_INVALID_CAP; |
paul@615 | 367 | |
paul@511 | 368 | l4re_env_cap_entry_t program_init_caps[] = { |
paul@511 | 369 | l4re_env_cap_entry_t(ENV_FILESYSTEM_SERVER_NAME, fsserver_cap, L4_CAP_FPAGE_RWS), |
paul@662 | 370 | l4re_env_cap_entry_t(ENV_PROCESS_SERVER_NAME, prserver_cap, L4_CAP_FPAGE_RWS), |
paul@615 | 371 | l4re_env_cap_entry_t(ENV_INPUT_STREAM_NAME, reader_cap, L4_CAP_FPAGE_R), |
paul@584 | 372 | l4re_env_cap_entry_t(ENV_OUTPUT_STREAM_NAME, writer_cap, L4_CAP_FPAGE_W), |
paul@511 | 373 | l4re_env_cap_entry_t() |
paul@511 | 374 | }; |
paul@511 | 375 | |
paul@477 | 376 | /* Populate a thread stack with argument and environment details for the |
paul@477 | 377 | actual program. The server capability should be assigned to the region |
paul@477 | 378 | mapper capability slot already. */ |
paul@477 | 379 | |
paul@495 | 380 | Stack program_st(*_program_stack); |
paul@477 | 381 | |
paul@511 | 382 | program_st.set_init_caps(program_init_caps); |
paul@477 | 383 | program_st.populate(argc, argv, envp); |
paul@477 | 384 | |
paul@477 | 385 | /* Start the program thread in the appropriate stack. */ |
paul@477 | 386 | |
paul@503 | 387 | l4_cap_idx_t thread, mapped_thread; |
paul@503 | 388 | err = _process.thread_start(_program_payload->entry_point(), program_st, &thread, &mapped_thread); |
paul@492 | 389 | |
paul@492 | 390 | if (err) |
paul@492 | 391 | return err; |
paul@492 | 392 | |
paul@543 | 393 | _monitor->set_thread(thread, mapped_thread); |
paul@492 | 394 | return L4_EOK; |
paul@477 | 395 | } |
paul@477 | 396 | |
paul@505 | 397 | /* Start a new process for the payload indicated by the first of the given |
paul@615 | 398 | program arguments, employing the given reader and writer pipes, and returning |
paul@615 | 399 | a reference to the process monitor as an object for interacting with the |
paul@584 | 400 | process. */ |
paul@505 | 401 | |
paul@615 | 402 | long ProcessCreating::_start(int argc, const char *argv[], l4_cap_idx_t reader, |
paul@615 | 403 | l4_cap_idx_t writer, l4_cap_idx_t process) |
paul@505 | 404 | { |
paul@505 | 405 | /* Open the program file, handling any error conditions. If successfully |
paul@505 | 406 | opened, it will be closed when the process terminates. */ |
paul@482 | 407 | |
paul@524 | 408 | file_t *file = client_open(argv[0], O_RDONLY); |
paul@524 | 409 | long err; |
paul@524 | 410 | |
paul@505 | 411 | if (file == NULL) |
paul@505 | 412 | return -L4_EIO; |
paul@505 | 413 | |
paul@505 | 414 | if (!client_opened(file)) |
paul@505 | 415 | { |
paul@505 | 416 | err = file->error; |
paul@505 | 417 | client_close(file); |
paul@505 | 418 | return err; |
paul@505 | 419 | } |
paul@505 | 420 | |
paul@505 | 421 | /* Initialise the different elements of the process. */ |
paul@477 | 422 | |
paul@543 | 423 | l4_cap_idx_t pager; |
paul@543 | 424 | |
paul@477 | 425 | err = init_region_mapper(); |
paul@477 | 426 | if (err) |
paul@477 | 427 | return err; |
paul@477 | 428 | |
paul@477 | 429 | err = init_program(file); |
paul@477 | 430 | if (err) |
paul@477 | 431 | return err; |
paul@477 | 432 | |
paul@543 | 433 | err = init_external_pager(&pager); |
paul@477 | 434 | if (err) |
paul@477 | 435 | return err; |
paul@477 | 436 | |
paul@543 | 437 | err = configure_task(); |
paul@477 | 438 | if (err) |
paul@477 | 439 | return err; |
paul@477 | 440 | |
paul@543 | 441 | err = allocate_internal_pager(); |
paul@477 | 442 | if (err) |
paul@477 | 443 | return err; |
paul@477 | 444 | |
paul@543 | 445 | err = start_region_mapper(pager); |
paul@543 | 446 | if (err) |
paul@543 | 447 | return err; |
paul@543 | 448 | |
paul@615 | 449 | err = start_program(process, argc, argv, reader, writer); |
paul@487 | 450 | if (err) |
paul@487 | 451 | return err; |
paul@487 | 452 | |
paul@495 | 453 | /* Discard instances created to initialise the process. The region mapper |
paul@495 | 454 | relies on resources associated with its payload and stack and so these |
paul@505 | 455 | cannot be deleted immediately. Instead, they are released when the pager is |
paul@505 | 456 | deallocated. |
paul@495 | 457 | |
paul@487 | 458 | NOTE: The region mapper payload could be retained instead of being |
paul@487 | 459 | reconstructed each time. */ |
paul@482 | 460 | |
paul@487 | 461 | delete _program_payload; |
paul@495 | 462 | delete _program_stack; |
paul@487 | 463 | |
paul@526 | 464 | /* Close the file given that it was used to construct the payload and |
paul@526 | 465 | separate file references are now used by the created process. */ |
paul@526 | 466 | |
paul@526 | 467 | client_close(file); |
paul@526 | 468 | |
paul@487 | 469 | return L4_EOK; |
paul@480 | 470 | } |
paul@480 | 471 | |
paul@559 | 472 | /* Start the given program, notifying the process monitor upon any error. */ |
paul@559 | 473 | |
paul@615 | 474 | long ProcessCreating::start(int argc, const char *argv[], l4_cap_idx_t reader, |
paul@615 | 475 | l4_cap_idx_t writer, l4_cap_idx_t process) |
paul@559 | 476 | { |
paul@559 | 477 | std::lock_guard<std::mutex> guard(_lock); |
paul@559 | 478 | |
paul@615 | 479 | long err = _start(argc, argv, reader, writer, process); |
paul@559 | 480 | |
paul@615 | 481 | /* Discard the reader and writer since they will not be used in this task. */ |
paul@585 | 482 | |
paul@615 | 483 | ipc_cap_free_um(reader); |
paul@585 | 484 | ipc_cap_free_um(writer); |
paul@585 | 485 | |
paul@559 | 486 | /* Communicate the error using the signal value. */ |
paul@559 | 487 | |
paul@559 | 488 | if (err) |
paul@559 | 489 | _monitor->notify_all(NOTIFY_TASK_ERROR, (notify_values_t) {0, err}); |
paul@559 | 490 | |
paul@559 | 491 | return err; |
paul@559 | 492 | } |
paul@559 | 493 | |
paul@477 | 494 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@477 | 495 | */ |