paul@308 | 1 | /* |
paul@308 | 2 | * Support for executing code in new tasks and threads. |
paul@308 | 3 | * |
paul@308 | 4 | * Copyright (C) 2022 Paul Boddie <paul@boddie.org.uk> |
paul@308 | 5 | * |
paul@308 | 6 | * This program is free software; you can redistribute it and/or |
paul@308 | 7 | * modify it under the terms of the GNU General Public License as |
paul@308 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@308 | 9 | * the License, or (at your option) any later version. |
paul@308 | 10 | * |
paul@308 | 11 | * This program is distributed in the hope that it will be useful, |
paul@308 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@308 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@308 | 14 | * GNU General Public License for more details. |
paul@308 | 15 | * |
paul@308 | 16 | * You should have received a copy of the GNU General Public License |
paul@308 | 17 | * along with this program; if not, write to the Free Software |
paul@308 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@308 | 19 | * Boston, MA 02110-1301, USA |
paul@308 | 20 | */ |
paul@308 | 21 | |
paul@308 | 22 | #include <l4/re/env.h> |
paul@308 | 23 | #include <l4/sys/err.h> |
paul@312 | 24 | #include <l4/util/util.h> |
paul@308 | 25 | |
paul@327 | 26 | #include <exec/elf.h> |
paul@370 | 27 | #include <exec/external_pager.h> |
paul@349 | 28 | #include <exec/memory.h> |
paul@324 | 29 | #include <exec/process.h> |
paul@370 | 30 | #include <ipc/cap_alloc.h> |
paul@370 | 31 | #include <ipc/map.h> |
paul@308 | 32 | #include <ipc/server.h> |
paul@312 | 33 | |
paul@308 | 34 | #include <stdio.h> |
paul@308 | 35 | |
paul@308 | 36 | #include <pthread-l4.h> |
paul@308 | 37 | #include <pthread.h> |
paul@308 | 38 | |
paul@469 | 39 | #include "parent_pager_object_server.h" |
paul@308 | 40 | |
paul@308 | 41 | |
paul@308 | 42 | |
paul@431 | 43 | /* External system-level pager for the region mapper in a created task. The |
paul@431 | 44 | allocated regions requested by the region mapper are constrained to an area |
paul@431 | 45 | of memory that must not overlap with the area reserved for the program being |
paul@431 | 46 | run. */ |
paul@376 | 47 | |
paul@431 | 48 | static ExternalPager exec_pager(0, 10 * L4_PAGESIZE); |
paul@308 | 49 | |
paul@369 | 50 | static const offset_t initial_stack_size = 16 * L4_PAGESIZE; |
paul@369 | 51 | |
paul@369 | 52 | |
paul@369 | 53 | |
paul@369 | 54 | /* Start the system pager in a separate thread. */ |
paul@369 | 55 | |
paul@369 | 56 | static long start_pager(ipc_server_config_type &config) |
paul@369 | 57 | { |
paul@369 | 58 | pthread_t pager_thread; |
paul@369 | 59 | pthread_attr_t attr; |
paul@369 | 60 | |
paul@369 | 61 | pthread_attr_init(&attr); |
paul@369 | 62 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
paul@369 | 63 | |
paul@469 | 64 | ipc_server_init_for(&config, ParentPagerObject, &exec_pager); |
paul@369 | 65 | |
paul@369 | 66 | long err = pthread_create(&pager_thread, &attr, ipc_server_start_mainloop, &config); |
paul@369 | 67 | |
paul@369 | 68 | if (err) |
paul@369 | 69 | return err; |
paul@369 | 70 | |
paul@369 | 71 | return ipc_server_start_config_thread(&config, pthread_l4_cap(pager_thread)); |
paul@369 | 72 | } |
paul@369 | 73 | |
paul@308 | 74 | |
paul@308 | 75 | |
paul@308 | 76 | int main(int argc, char *argv[]) |
paul@308 | 77 | { |
paul@308 | 78 | long err; |
paul@308 | 79 | |
paul@370 | 80 | if (argc < 3) |
paul@308 | 81 | { |
paul@370 | 82 | printf("Need a program to run as the region mapper and a main program.\n"); |
paul@370 | 83 | return 1; |
paul@370 | 84 | } |
paul@370 | 85 | |
paul@370 | 86 | /* Define the different payloads. */ |
paul@370 | 87 | |
paul@370 | 88 | char *rm_filename = argv[1]; |
paul@370 | 89 | char *program_filename = argv[2]; |
paul@370 | 90 | |
paul@370 | 91 | /* Initialise the memory segments of the region mapper. These are mapped into |
paul@370 | 92 | this task so that we may access them. */ |
paul@370 | 93 | |
paul@370 | 94 | ExplicitSegment rm_stack(Utcb_area_start - initial_stack_size, initial_stack_size, L4_FPAGE_RW); |
paul@370 | 95 | Payload *rm_payload; |
paul@370 | 96 | |
paul@370 | 97 | if (exec_get_payload(rm_filename, &rm_payload, true)) |
paul@370 | 98 | { |
paul@370 | 99 | printf("Could not initialise region mapper: %s\n", rm_filename); |
paul@370 | 100 | return 1; |
paul@370 | 101 | } |
paul@370 | 102 | |
paul@370 | 103 | if (rm_stack.allocate(true)) |
paul@370 | 104 | { |
paul@370 | 105 | printf("Could not allocate region mapper stack.\n"); |
paul@308 | 106 | return 1; |
paul@308 | 107 | } |
paul@308 | 108 | |
paul@370 | 109 | /* Initialise the memory segments of the actual program. These are not mapped |
paul@370 | 110 | into this task, instead being accessed by the region mapper in the new |
paul@370 | 111 | task. */ |
paul@323 | 112 | |
paul@370 | 113 | ExplicitSegment program_stack(Utcb_area_start - initial_stack_size * 2, initial_stack_size, L4_FPAGE_RW); |
paul@370 | 114 | Payload *program_payload; |
paul@323 | 115 | |
paul@370 | 116 | if (exec_get_payload(program_filename, &program_payload, false)) |
paul@323 | 117 | { |
paul@370 | 118 | printf("Could not initialise program: %s\n", program_filename); |
paul@325 | 119 | return 1; |
paul@325 | 120 | } |
paul@325 | 121 | |
paul@370 | 122 | if (program_stack.allocate(true)) |
paul@316 | 123 | { |
paul@370 | 124 | printf("Could not allocate program stack.\n"); |
paul@349 | 125 | return 1; |
paul@308 | 126 | } |
paul@308 | 127 | |
paul@370 | 128 | /* Initialise pager regions for the region mapper. */ |
paul@312 | 129 | |
paul@370 | 130 | for (unsigned int i = 0; i < rm_payload->segments(); i++) |
paul@325 | 131 | { |
paul@370 | 132 | if (rm_payload->segment(i)->loadable()) |
paul@370 | 133 | exec_pager.add(rm_payload->segment(i)->region()); |
paul@349 | 134 | } |
paul@325 | 135 | |
paul@370 | 136 | exec_pager.add(rm_stack.region()); |
paul@308 | 137 | |
paul@369 | 138 | /* Start the pager in a separate thread. */ |
paul@354 | 139 | |
paul@354 | 140 | ipc_server_config_type config; |
paul@308 | 141 | |
paul@354 | 142 | printf("Starting pager thread...\n"); |
paul@369 | 143 | |
paul@369 | 144 | err = start_pager(config); |
paul@308 | 145 | |
paul@308 | 146 | if (err) |
paul@308 | 147 | { |
paul@308 | 148 | printf("Could not start pager.\n"); |
paul@308 | 149 | return 1; |
paul@308 | 150 | } |
paul@308 | 151 | |
paul@370 | 152 | /* Configure the environment for the task, reserving two threads. */ |
paul@308 | 153 | |
paul@430 | 154 | Process process; |
paul@321 | 155 | |
paul@365 | 156 | err = process.configure_task(); |
paul@308 | 157 | |
paul@308 | 158 | if (err) |
paul@308 | 159 | { |
paul@321 | 160 | printf("Could not configure task.\n"); |
paul@308 | 161 | return 1; |
paul@308 | 162 | } |
paul@308 | 163 | |
paul@370 | 164 | /* Configure the environment for the thread, specifying the pager (and |
paul@370 | 165 | exception handler plus region mapper). */ |
paul@370 | 166 | |
paul@365 | 167 | err = process.configure_thread(config.server); |
paul@365 | 168 | |
paul@365 | 169 | if (err) |
paul@365 | 170 | { |
paul@365 | 171 | printf("Could not configure thread.\n"); |
paul@365 | 172 | return 1; |
paul@365 | 173 | } |
paul@365 | 174 | |
paul@469 | 175 | err = process.set_parent(config.server); |
paul@469 | 176 | |
paul@469 | 177 | if (err) |
paul@469 | 178 | { |
paul@469 | 179 | printf("Could not map parent to task for internal pager.\n"); |
paul@469 | 180 | return 1; |
paul@469 | 181 | } |
paul@469 | 182 | |
paul@370 | 183 | /* Create an unbound IPC gate for the region mapper. */ |
paul@370 | 184 | |
paul@370 | 185 | l4_cap_idx_t ipc_gate = ipc_cap_alloc(); |
paul@370 | 186 | |
paul@370 | 187 | if (l4_is_invalid_cap(ipc_gate)) |
paul@370 | 188 | { |
paul@370 | 189 | printf("Could not allocate IPC gate capability.\n"); |
paul@370 | 190 | return 1; |
paul@370 | 191 | } |
paul@370 | 192 | |
paul@370 | 193 | err = l4_error(l4_factory_create_gate(l4re_env()->factory, ipc_gate, L4_INVALID_CAP, 0)); |
paul@370 | 194 | |
paul@370 | 195 | if (err) |
paul@370 | 196 | { |
paul@370 | 197 | printf("Could not create IPC gate.\n"); |
paul@370 | 198 | return 1; |
paul@370 | 199 | } |
paul@370 | 200 | |
paul@370 | 201 | /* Define regions employing dataspaces to provide program segments. |
paul@370 | 202 | |
paul@370 | 203 | Define capabilities for mapping, including region dataspace capabilities, |
paul@370 | 204 | the stack dataspace capability, and the server capability. |
paul@370 | 205 | |
paul@370 | 206 | Here, the arrays are sized for the maximum number of regions and |
paul@370 | 207 | capabilities, but in practice only the loadable segments are used, leaving |
paul@370 | 208 | fewer elements utilised. A terminating entry is employed to indicate the |
paul@370 | 209 | limit of utilised elements. */ |
paul@370 | 210 | |
paul@370 | 211 | struct exec_region rm_regions[rm_payload->segments() + 2]; |
paul@370 | 212 | struct ipc_mapped_cap rm_mapped_caps[rm_payload->segments() + 3]; |
paul@376 | 213 | l4_cap_idx_t mapped_cap; |
paul@370 | 214 | unsigned int rm_index = 0; |
paul@370 | 215 | |
paul@376 | 216 | for (unsigned int i = 0; i < program_payload->segments(); i++) |
paul@370 | 217 | { |
paul@376 | 218 | Segment *s = program_payload->segment(i); |
paul@321 | 219 | |
paul@370 | 220 | if (s->loadable()) |
paul@370 | 221 | { |
paul@376 | 222 | mapped_cap = process.allocate_cap(); |
paul@370 | 223 | rm_regions[rm_index] = s->exec_region(); |
paul@376 | 224 | rm_mapped_caps[rm_index] = (struct ipc_mapped_cap) {mapped_cap, rm_regions[rm_index].ds, L4_CAP_FPAGE_RWS, 0}; |
paul@376 | 225 | rm_regions[rm_index].ds = mapped_cap; |
paul@370 | 226 | rm_index++; |
paul@370 | 227 | } |
paul@370 | 228 | } |
paul@370 | 229 | |
paul@370 | 230 | /* Introduce the stack region and capability. */ |
paul@370 | 231 | |
paul@376 | 232 | mapped_cap = process.allocate_cap(); |
paul@370 | 233 | rm_regions[rm_index] = program_stack.exec_region(); |
paul@376 | 234 | rm_mapped_caps[rm_index] = (struct ipc_mapped_cap) {mapped_cap, program_stack.exec_region().ds, L4_CAP_FPAGE_RWS, 0}; |
paul@376 | 235 | rm_regions[rm_index].ds = mapped_cap; |
paul@370 | 236 | rm_index++; |
paul@370 | 237 | |
paul@370 | 238 | /* Terminate the region array. */ |
paul@370 | 239 | |
paul@370 | 240 | rm_regions[rm_index] = (struct exec_region) {0, 0, 0, L4_INVALID_CAP}; |
paul@370 | 241 | |
paul@370 | 242 | /* Introduce the server capability. */ |
paul@370 | 243 | |
paul@370 | 244 | l4_cap_idx_t ipc_gate_cap = process.allocate_cap(); |
paul@370 | 245 | |
paul@376 | 246 | printf("Mapping %lx to %lx in task.\n", ipc_gate, ipc_gate_cap); |
paul@376 | 247 | |
paul@376 | 248 | rm_mapped_caps[rm_index] = (struct ipc_mapped_cap) {ipc_gate_cap, ipc_gate, L4_CAP_FPAGE_RWS, L4_FPAGE_C_OBJ_RIGHTS}; |
paul@370 | 249 | rm_index++; |
paul@370 | 250 | |
paul@370 | 251 | /* Terminate the capability array. */ |
paul@370 | 252 | |
paul@376 | 253 | rm_mapped_caps[rm_index] = (struct ipc_mapped_cap) {0, L4_INVALID_CAP, 0, 0}; |
paul@370 | 254 | |
paul@370 | 255 | /* Map these additional capabilities. */ |
paul@370 | 256 | |
paul@370 | 257 | printf("Map additional capabilities...\n"); |
paul@370 | 258 | |
paul@376 | 259 | process.map_capabilities(rm_mapped_caps, false); |
paul@370 | 260 | |
paul@370 | 261 | /* Define the IPC gate as an initial capability to be acquired by the region |
paul@370 | 262 | mapper via the l4re_env API. The capability index is assigned above when |
paul@370 | 263 | mapping the capability and encoded in the entry below. */ |
paul@370 | 264 | |
paul@370 | 265 | l4re_env_cap_entry_t rm_init_caps[] = { |
paul@370 | 266 | l4re_env_cap_entry_t("server", ipc_gate_cap, L4_CAP_FPAGE_RWS), |
paul@370 | 267 | l4re_env_cap_entry_t() |
paul@370 | 268 | }; |
paul@321 | 269 | |
paul@321 | 270 | /* NOTE: Environment vector is currently not defined. */ |
paul@321 | 271 | |
paul@321 | 272 | char *envp[] = {NULL}; |
paul@321 | 273 | |
paul@370 | 274 | /* Populate a thread stack with argument and environment details for the |
paul@370 | 275 | region mapper, plus the initial server capability and region details. */ |
paul@321 | 276 | |
paul@370 | 277 | printf("Populating region mapper stack...\n"); |
paul@370 | 278 | |
paul@370 | 279 | Stack rm_st(rm_stack); |
paul@308 | 280 | |
paul@370 | 281 | rm_st.set_init_caps(rm_init_caps); |
paul@370 | 282 | rm_st.set_regions(rm_regions); |
paul@370 | 283 | rm_st.populate(1, argv + 1, envp); |
paul@308 | 284 | |
paul@370 | 285 | /* Start the region mapper thread in the appropriate stack. */ |
paul@370 | 286 | |
paul@370 | 287 | printf("Run region mapper thread...\n"); |
paul@370 | 288 | |
paul@370 | 289 | err = process.thread_start(rm_payload->entry_point(), rm_st); |
paul@308 | 290 | |
paul@308 | 291 | if (err) |
paul@308 | 292 | { |
paul@370 | 293 | printf("Could not run thread for region mapper.\n"); |
paul@370 | 294 | return 1; |
paul@370 | 295 | } |
paul@370 | 296 | |
paul@370 | 297 | /* Configure the environment for the thread, specifying the pager (and |
paul@370 | 298 | exception handler plus region mapper). */ |
paul@370 | 299 | |
paul@376 | 300 | err = process.configure_thread(ipc_gate, ipc_gate_cap); |
paul@370 | 301 | |
paul@370 | 302 | if (err) |
paul@370 | 303 | { |
paul@370 | 304 | printf("Could not configure task.\n"); |
paul@370 | 305 | return 1; |
paul@370 | 306 | } |
paul@370 | 307 | |
paul@469 | 308 | err = process.set_parent(config.server); |
paul@469 | 309 | |
paul@469 | 310 | if (err) |
paul@469 | 311 | { |
paul@469 | 312 | printf("Could not map parent to task for payload.\n"); |
paul@469 | 313 | return 1; |
paul@469 | 314 | } |
paul@469 | 315 | |
paul@370 | 316 | /* Populate a thread stack with argument and environment details for the |
paul@370 | 317 | actual program. The server capability should be assigned to the region |
paul@370 | 318 | mapper capability slot already. */ |
paul@370 | 319 | |
paul@370 | 320 | printf("Populating program stack...\n"); |
paul@370 | 321 | |
paul@370 | 322 | Stack program_st(program_stack); |
paul@370 | 323 | |
paul@370 | 324 | program_st.populate(argc - 2, argv + 2, envp); |
paul@370 | 325 | |
paul@370 | 326 | /* Start the program thread in the appropriate stack. */ |
paul@370 | 327 | |
paul@370 | 328 | printf("Run program thread...\n"); |
paul@370 | 329 | |
paul@370 | 330 | err = process.thread_start(program_payload->entry_point(), program_st); |
paul@370 | 331 | |
paul@370 | 332 | if (err) |
paul@370 | 333 | { |
paul@454 | 334 | printf("Could not run thread for program: %s\n", l4sys_errtostr(err)); |
paul@308 | 335 | return 1; |
paul@308 | 336 | } |
paul@308 | 337 | |
paul@402 | 338 | printf("Finished program initiation.\n"); |
paul@402 | 339 | printf("End of test.\n"); |
paul@402 | 340 | |
paul@402 | 341 | /* NOTE: Should be able to obtain a notification for when the program |
paul@402 | 342 | finishes, which might be done using a capability deletion IRQ. |
paul@402 | 343 | Eventually, this program will operate as a server, invoking new |
paul@402 | 344 | programs and handling termination. This test would then merely |
paul@402 | 345 | involve the invocation of a utility function. */ |
paul@376 | 346 | |
paul@369 | 347 | while (1) |
paul@369 | 348 | l4_sleep_forever(); |
paul@308 | 349 | |
paul@308 | 350 | return 0; |
paul@308 | 351 | } |
paul@308 | 352 | |
paul@308 | 353 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@308 | 354 | */ |