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@477 | 45 | } |
paul@477 | 46 | |
paul@477 | 47 | /* Initialise the memory segments of the region mapper. These are mapped into |
paul@477 | 48 | this task so that we may access them, allowing the external pager in this |
paul@477 | 49 | task to use them. */ |
paul@477 | 50 | |
paul@483 | 51 | long ProcessCreating::init_region_mapper() |
paul@477 | 52 | { |
paul@477 | 53 | long err = exec_get_payload(_rm_filename, &_rm_payload, true); |
paul@477 | 54 | |
paul@477 | 55 | if (err) |
paul@477 | 56 | return err; |
paul@477 | 57 | |
paul@504 | 58 | _rm_stack = new ExplicitSegment(Utcb_area_start - initial_stack_size, initial_stack_size, L4_FPAGE_RW); |
paul@495 | 59 | return _rm_stack->allocate(true); |
paul@477 | 60 | } |
paul@477 | 61 | |
paul@477 | 62 | /* Initialise the memory segments of the actual program. These are not mapped |
paul@477 | 63 | into this task, instead being accessed by the region mapper in the new |
paul@477 | 64 | task. */ |
paul@477 | 65 | |
paul@483 | 66 | long ProcessCreating::init_program(file_t *file) |
paul@477 | 67 | { |
paul@477 | 68 | long err = exec_get_payload_file(file, &_program_payload, false); |
paul@477 | 69 | |
paul@477 | 70 | if (err) |
paul@477 | 71 | return err; |
paul@477 | 72 | |
paul@504 | 73 | _program_stack = new ExplicitSegment(Utcb_area_start - initial_stack_size * 2, initial_stack_size, L4_FPAGE_RW); |
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@504 | 97 | _exec_pager->set_payload(_rm_payload); |
paul@504 | 98 | _exec_pager->set_stack(_rm_stack); |
paul@477 | 99 | |
paul@477 | 100 | /* Start the pager in a separate thread. */ |
paul@477 | 101 | |
paul@492 | 102 | long err = ResourceServer(_exec_pager).start_thread(pager); |
paul@492 | 103 | |
paul@492 | 104 | if (err) |
paul@492 | 105 | return err; |
paul@492 | 106 | |
paul@492 | 107 | return L4_EOK; |
paul@477 | 108 | } |
paul@477 | 109 | |
paul@477 | 110 | /* Configure the environment for the task. */ |
paul@477 | 111 | |
paul@489 | 112 | long ProcessCreating::configure_task(l4_cap_idx_t pager) |
paul@477 | 113 | { |
paul@503 | 114 | l4_cap_idx_t task, mapped_task; |
paul@503 | 115 | long err = _process.configure_task(&task, &mapped_task); |
paul@477 | 116 | |
paul@477 | 117 | if (err) |
paul@477 | 118 | return err; |
paul@477 | 119 | |
paul@503 | 120 | /* Record the task details in the pager for eventual resource deallocation. */ |
paul@503 | 121 | |
paul@503 | 122 | _exec_pager->set_task(task, mapped_task); |
paul@501 | 123 | |
paul@503 | 124 | /* Note the pager as the parent of the new task, recording its capability |
paul@503 | 125 | details in the new task. */ |
paul@501 | 126 | |
paul@503 | 127 | l4_cap_idx_t mapped_parent; |
paul@503 | 128 | err = _process.set_parent(pager, &mapped_parent); |
paul@499 | 129 | |
paul@499 | 130 | if (err) |
paul@499 | 131 | return err; |
paul@499 | 132 | |
paul@503 | 133 | _exec_pager->set_parent(pager, mapped_parent); |
paul@499 | 134 | return L4_EOK; |
paul@477 | 135 | } |
paul@477 | 136 | |
paul@477 | 137 | /* Create an unbound IPC gate for the region mapper and allocate it in the |
paul@477 | 138 | created process. */ |
paul@477 | 139 | |
paul@483 | 140 | long ProcessCreating::create_ipc_gate() |
paul@477 | 141 | { |
paul@477 | 142 | _ipc_gate_cap = _process.allocate_cap(); |
paul@477 | 143 | _ipc_gate = ipc_cap_alloc(); |
paul@477 | 144 | |
paul@477 | 145 | if (l4_is_invalid_cap(_ipc_gate)) |
paul@477 | 146 | return -L4_ENOMEM; |
paul@477 | 147 | |
paul@503 | 148 | long err = l4_error(l4_factory_create_gate(l4re_env()->factory, _ipc_gate, L4_INVALID_CAP, 0)); |
paul@503 | 149 | |
paul@503 | 150 | if (err) |
paul@503 | 151 | return err; |
paul@503 | 152 | |
paul@503 | 153 | /* The gate is retained because even after being mapped to the new task, |
paul@503 | 154 | releasing it will cause it to be deallocated. */ |
paul@503 | 155 | |
paul@501 | 156 | _exec_pager->set_gate(_ipc_gate); |
paul@503 | 157 | return L4_EOK; |
paul@477 | 158 | } |
paul@477 | 159 | |
paul@477 | 160 | /* Initialise and assign a region in a list to the created process. */ |
paul@477 | 161 | |
paul@483 | 162 | void ProcessCreating::init_region(struct exec_region *regions, |
paul@477 | 163 | struct ipc_mapped_cap *mapped_caps, |
paul@477 | 164 | struct exec_region &r, unsigned int &index) |
paul@477 | 165 | { |
paul@477 | 166 | l4_cap_idx_t mapped_cap = _process.allocate_cap(); |
paul@477 | 167 | |
paul@477 | 168 | mapped_caps[index] = (struct ipc_mapped_cap) {mapped_cap, r.ds, L4_CAP_FPAGE_RWS, 0}; |
paul@477 | 169 | |
paul@477 | 170 | /* Change the region definition to use the allocated capability in the created |
paul@477 | 171 | process. */ |
paul@477 | 172 | |
paul@477 | 173 | regions[index] = r; |
paul@477 | 174 | regions[index].ds = mapped_cap; |
paul@477 | 175 | index++; |
paul@477 | 176 | } |
paul@477 | 177 | |
paul@477 | 178 | /* Initialise the region mapper with details of the payload program regions |
paul@482 | 179 | and of the associated capabilities, configure the region mapper thread, |
paul@482 | 180 | populate its stack, and start the thread. */ |
paul@477 | 181 | |
paul@489 | 182 | long ProcessCreating::start_region_mapper(l4_cap_idx_t pager) |
paul@477 | 183 | { |
paul@477 | 184 | /* Define regions employing dataspaces to provide program segments. */ |
paul@477 | 185 | |
paul@496 | 186 | struct exec_region rm_regions[_program_payload->segments() + 2]; |
paul@477 | 187 | |
paul@477 | 188 | /* Define capabilities for mapping, including region dataspace capabilities, |
paul@477 | 189 | the stack dataspace capability, and the server capability. */ |
paul@477 | 190 | |
paul@496 | 191 | struct ipc_mapped_cap rm_mapped_caps[_program_payload->segments() + 3]; |
paul@477 | 192 | |
paul@477 | 193 | /* Here, the arrays are sized for the maximum number of regions and |
paul@477 | 194 | capabilities, but in practice only the loadable segments are used, leaving |
paul@477 | 195 | fewer elements utilised. A terminating entry is employed to indicate the |
paul@477 | 196 | limit of utilised elements. */ |
paul@477 | 197 | |
paul@477 | 198 | unsigned int rm_index = 0; |
paul@477 | 199 | |
paul@477 | 200 | for (unsigned int i = 0; i < _program_payload->segments(); i++) |
paul@477 | 201 | { |
paul@477 | 202 | Segment *s = _program_payload->segment(i); |
paul@477 | 203 | |
paul@477 | 204 | if (s->loadable()) |
paul@477 | 205 | init_region(rm_regions, rm_mapped_caps, s->exec_region(), rm_index); |
paul@477 | 206 | } |
paul@477 | 207 | |
paul@477 | 208 | /* Introduce the stack region and capability. */ |
paul@477 | 209 | |
paul@495 | 210 | init_region(rm_regions, rm_mapped_caps, _program_stack->exec_region(), rm_index); |
paul@477 | 211 | |
paul@477 | 212 | /* Terminate the region array. */ |
paul@477 | 213 | |
paul@477 | 214 | rm_regions[rm_index] = (struct exec_region) {0, 0, 0, L4_INVALID_CAP}; |
paul@477 | 215 | |
paul@477 | 216 | /* Introduce the server capability and terminate the capability array. */ |
paul@477 | 217 | |
paul@477 | 218 | 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 | 219 | rm_mapped_caps[rm_index] = (struct ipc_mapped_cap) {0, L4_INVALID_CAP, 0, 0}; |
paul@477 | 220 | |
paul@477 | 221 | /* Map these additional capabilities. */ |
paul@477 | 222 | |
paul@477 | 223 | _process.map_capabilities(rm_mapped_caps, false); |
paul@477 | 224 | |
paul@477 | 225 | /* Define the IPC gate as an initial capability to be acquired by the region |
paul@477 | 226 | mapper via the l4re_env API. The capability index is assigned above when |
paul@477 | 227 | mapping the capability and encoded in the entry below. */ |
paul@477 | 228 | |
paul@477 | 229 | l4re_env_cap_entry_t rm_init_caps[] = { |
paul@477 | 230 | l4re_env_cap_entry_t("server", _ipc_gate_cap, L4_CAP_FPAGE_RWS), |
paul@477 | 231 | l4re_env_cap_entry_t() |
paul@477 | 232 | }; |
paul@477 | 233 | |
paul@477 | 234 | /* NOTE: Environment vector is currently not defined. */ |
paul@477 | 235 | |
paul@477 | 236 | const char *envp[] = {NULL}; |
paul@477 | 237 | |
paul@477 | 238 | /* Configure the environment for the thread, specifying the pager (and |
paul@477 | 239 | exception handler plus region mapper). */ |
paul@477 | 240 | |
paul@503 | 241 | l4_cap_idx_t mapped_pager = L4_INVALID_CAP; |
paul@503 | 242 | long err = _process.configure_thread(pager, &mapped_pager); |
paul@477 | 243 | |
paul@477 | 244 | if (err) |
paul@477 | 245 | return err; |
paul@477 | 246 | |
paul@503 | 247 | _exec_pager->set_pager(pager, mapped_pager); |
paul@499 | 248 | |
paul@477 | 249 | /* Populate a thread stack with argument and environment details for the |
paul@477 | 250 | region mapper, plus the initial server capability and region details. */ |
paul@477 | 251 | |
paul@477 | 252 | const char *argv[] = {_rm_filename}; |
paul@495 | 253 | Stack rm_st(*_rm_stack); |
paul@477 | 254 | |
paul@477 | 255 | rm_st.set_init_caps(rm_init_caps); |
paul@477 | 256 | rm_st.set_regions(rm_regions); |
paul@477 | 257 | rm_st.populate(1, argv, envp); |
paul@477 | 258 | |
paul@477 | 259 | /* Start the region mapper thread in the appropriate stack. */ |
paul@477 | 260 | |
paul@503 | 261 | l4_cap_idx_t thread, mapped_thread; |
paul@503 | 262 | err = _process.thread_start(_rm_payload->entry_point(), rm_st, &thread, &mapped_thread); |
paul@492 | 263 | |
paul@492 | 264 | if (err) |
paul@492 | 265 | return err; |
paul@492 | 266 | |
paul@503 | 267 | _exec_pager->add_thread(thread, mapped_thread); |
paul@492 | 268 | return L4_EOK; |
paul@477 | 269 | } |
paul@477 | 270 | |
paul@482 | 271 | /* Configure a thread for a program, populate its stack, and start the |
paul@482 | 272 | thread. */ |
paul@482 | 273 | |
paul@483 | 274 | long ProcessCreating::start_program(int argc, const char *argv[]) |
paul@477 | 275 | { |
paul@477 | 276 | /* NOTE: Environment vector is currently not defined. */ |
paul@477 | 277 | |
paul@477 | 278 | const char *envp[] = {NULL}; |
paul@477 | 279 | |
paul@477 | 280 | /* Configure the environment for the thread, specifying the pager (and |
paul@477 | 281 | exception handler plus region mapper). */ |
paul@477 | 282 | |
paul@503 | 283 | l4_cap_idx_t mapped_pager = _ipc_gate_cap; |
paul@503 | 284 | long err = _process.configure_thread(_ipc_gate, &mapped_pager); |
paul@477 | 285 | |
paul@477 | 286 | if (err) |
paul@477 | 287 | return err; |
paul@477 | 288 | |
paul@477 | 289 | /* Populate a thread stack with argument and environment details for the |
paul@477 | 290 | actual program. The server capability should be assigned to the region |
paul@477 | 291 | mapper capability slot already. */ |
paul@477 | 292 | |
paul@495 | 293 | Stack program_st(*_program_stack); |
paul@477 | 294 | |
paul@477 | 295 | program_st.populate(argc, argv, envp); |
paul@477 | 296 | |
paul@477 | 297 | /* Start the program thread in the appropriate stack. */ |
paul@477 | 298 | |
paul@503 | 299 | l4_cap_idx_t thread, mapped_thread; |
paul@503 | 300 | err = _process.thread_start(_program_payload->entry_point(), program_st, &thread, &mapped_thread); |
paul@492 | 301 | |
paul@492 | 302 | if (err) |
paul@492 | 303 | return err; |
paul@492 | 304 | |
paul@503 | 305 | _exec_pager->add_thread(thread, mapped_thread); |
paul@492 | 306 | return L4_EOK; |
paul@477 | 307 | } |
paul@477 | 308 | |
paul@482 | 309 | /* Start a new process for the given payload, providing the indicated program |
paul@487 | 310 | arguments, returning a reference to the pager. */ |
paul@482 | 311 | |
paul@487 | 312 | long ProcessCreating::start(file_t *file, int argc, const char *argv[], |
paul@487 | 313 | l4_cap_idx_t *process) |
paul@477 | 314 | { |
paul@477 | 315 | long err; |
paul@477 | 316 | |
paul@477 | 317 | err = init_region_mapper(); |
paul@477 | 318 | if (err) |
paul@477 | 319 | return err; |
paul@477 | 320 | |
paul@477 | 321 | err = init_program(file); |
paul@477 | 322 | if (err) |
paul@477 | 323 | return err; |
paul@477 | 324 | |
paul@489 | 325 | err = init_external_pager(process); |
paul@477 | 326 | if (err) |
paul@477 | 327 | return err; |
paul@477 | 328 | |
paul@489 | 329 | err = configure_task(*process); |
paul@477 | 330 | if (err) |
paul@477 | 331 | return err; |
paul@477 | 332 | |
paul@477 | 333 | err = create_ipc_gate(); |
paul@477 | 334 | if (err) |
paul@477 | 335 | return err; |
paul@477 | 336 | |
paul@489 | 337 | err = start_region_mapper(*process); |
paul@477 | 338 | if (err) |
paul@477 | 339 | return err; |
paul@477 | 340 | |
paul@487 | 341 | err = start_program(argc, argv); |
paul@487 | 342 | if (err) |
paul@487 | 343 | return err; |
paul@487 | 344 | |
paul@495 | 345 | /* Discard instances created to initialise the process. The region mapper |
paul@495 | 346 | relies on resources associated with its payload and stack and so these |
paul@495 | 347 | cannot be deleted immediately. |
paul@495 | 348 | |
paul@487 | 349 | NOTE: The region mapper payload could be retained instead of being |
paul@487 | 350 | reconstructed each time. */ |
paul@482 | 351 | |
paul@487 | 352 | delete _program_payload; |
paul@495 | 353 | delete _program_stack; |
paul@487 | 354 | |
paul@487 | 355 | return L4_EOK; |
paul@480 | 356 | } |
paul@480 | 357 | |
paul@477 | 358 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@477 | 359 | */ |