L4Re/departure

Annotated libexec/lib/src/process.cc

524:86d76cd9a8d3
19 months ago Paul Boddie Made the process creation activity sequential and introduced a missing reset operation so that more than one process can be successfully created.
paul@324 1
/*
paul@324 2
 * Support for initialising programs in new tasks and threads.
paul@324 3
 *
paul@492 4
 * Copyright (C) 2022, 2023 Paul Boddie <paul@boddie.org.uk>
paul@324 5
 *
paul@324 6
 * This program is free software; you can redistribute it and/or
paul@324 7
 * modify it under the terms of the GNU General Public License as
paul@324 8
 * published by the Free Software Foundation; either version 2 of
paul@324 9
 * the License, or (at your option) any later version.
paul@324 10
 *
paul@324 11
 * This program is distributed in the hope that it will be useful,
paul@324 12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
paul@324 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
paul@324 14
 * GNU General Public License for more details.
paul@324 15
 *
paul@324 16
 * You should have received a copy of the GNU General Public License
paul@324 17
 * along with this program; if not, write to the Free Software
paul@324 18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
paul@324 19
 * Boston, MA  02110-1301, USA
paul@324 20
 */
paul@324 21
paul@324 22
#include <l4/sys/err.h>
paul@324 23
#include <l4/sys/scheduler.h>
paul@324 24
paul@324 25
#include <exec/process.h>
paul@324 26
#include <ipc/cap_alloc.h>
paul@324 27
#include <ipc/map.h>
paul@324 28
#include <mem/memory_utils.h>
paul@324 29
paul@324 30
paul@324 31
paul@324 32
/* Obtain a flexpage defining the UTCB area location and size in a new task. */
paul@324 33
paul@429 34
static l4_fpage_t get_utcb_fpage(unsigned int page, unsigned int pages)
paul@324 35
{
paul@324 36
  /* UTCB location and size. */
paul@324 37
paul@453 38
  int utcb_log2size = page_order(Default_max_threads * L4_UTCB_OFFSET);
paul@324 39
paul@324 40
  /* Round up to at least one page. */
paul@324 41
paul@324 42
  if (utcb_log2size < L4_PAGESHIFT)
paul@324 43
    utcb_log2size = L4_PAGESHIFT;
paul@324 44
paul@453 45
  utcb_log2size = page_order(pages * (1UL << utcb_log2size));
paul@453 46
paul@429 47
  return l4_fpage(Utcb_area_start + page * (1UL << utcb_log2size), utcb_log2size, 0);
paul@324 48
}
paul@324 49
paul@324 50
paul@324 51
paul@324 52
/* Initialise a new process, this being an abstraction for a new task with some
paul@324 53
   threads. */
paul@324 54
paul@430 55
Process::Process()
paul@324 56
{
paul@524 57
  reset();
paul@524 58
}
paul@524 59
paul@524 60
void Process::reset()
paul@524 61
{
paul@429 62
  _thread_number = 0;
paul@324 63
paul@324 64
  /* Populate the common initial environment for the threads. */
paul@324 65
paul@324 66
  _env.factory          = L4_BASE_FACTORY_CAP;
paul@324 67
  _env.log              = L4_BASE_LOG_CAP;
paul@324 68
  _env.scheduler        = L4_BASE_SCHEDULER_CAP;
paul@324 69
  _env.mem_alloc        = L4_EXEC_MA_CAP;
paul@469 70
  _env.parent           = L4_EXEC_PARENT_CAP;
paul@324 71
paul@469 72
  /* Capability details that are updated for each thread. Note that the region
paul@469 73
     mapper is redefined, but it would traditionally employ the given index. */
paul@370 74
paul@370 75
  _env.main_thread      = L4_BASE_THREAD_CAP;
paul@370 76
  _env.rm               = L4_EXEC_RM_CAP;
paul@370 77
  _env.first_free_cap   = L4_EXEC_FIRST_FREE_CAP_INDEX;
paul@370 78
paul@324 79
  /* Populate auxiliary information. */
paul@324 80
paul@324 81
  _aux.kip_ds           = L4_EXEC_KIP_CAP;
paul@324 82
  _aux.dbg_lvl          = 0;
paul@324 83
  _aux.ldr_flags        = 0;
paul@324 84
}
paul@324 85
paul@370 86
/* Capability index allocation. */
paul@370 87
paul@370 88
l4_cap_idx_t Process::allocate_cap()
paul@370 89
{
paul@370 90
  return (_env.first_free_cap++ << L4_CAP_SHIFT);
paul@370 91
}
paul@370 92
paul@324 93
/* Task and thread initialisation. */
paul@324 94
paul@474 95
long Process::create_task(unsigned int threads)
paul@324 96
{
paul@524 97
  /* Reset the process if it has already been used. */
paul@524 98
paul@524 99
  if (_thread_number)
paul@524 100
    reset();
paul@524 101
paul@324 102
  _task = ipc_cap_alloc();
paul@324 103
paul@324 104
  if (l4_is_invalid_cap(_task))
paul@324 105
    return -L4_ENOMEM;
paul@324 106
paul@474 107
  return l4_error(l4_factory_create_task(l4re_env()->factory, _task, get_utcb_fpage(0, threads)));
paul@324 108
}
paul@324 109
paul@324 110
long Process::create_thread(l4_cap_idx_t *thread)
paul@324 111
{
paul@324 112
  *thread = ipc_cap_alloc();
paul@324 113
paul@324 114
  if (l4_is_invalid_cap(*thread))
paul@324 115
    return -L4_ENOMEM;
paul@324 116
paul@324 117
  return l4_error(l4_factory_create_thread(l4re_env()->factory, *thread));
paul@324 118
}
paul@324 119
paul@324 120
/* Configure the task environment. */
paul@324 121
paul@503 122
long Process::configure_task(l4_cap_idx_t *task, l4_cap_idx_t *mapped_task, unsigned int threads)
paul@324 123
{
paul@474 124
  long err = create_task(threads);
paul@324 125
paul@324 126
  if (err)
paul@324 127
    return err;
paul@324 128
paul@324 129
  /* Map the KIP into the task. */
paul@324 130
paul@324 131
  l4_addr_t kip_start = (l4_addr_t) l4re_kip();
paul@324 132
paul@324 133
  err = l4_error(l4_task_map(_task, L4RE_THIS_TASK_CAP,
paul@324 134
                             l4_fpage(kip_start, L4_PAGESHIFT, L4_FPAGE_RX),
paul@324 135
                             kip_start));
paul@324 136
paul@324 137
  if (err)
paul@324 138
    return err;
paul@324 139
paul@324 140
  /* Define capability mappings for the new task. */
paul@324 141
paul@324 142
  struct ipc_mapped_cap mapped_caps[] = {
paul@376 143
    {L4_BASE_TASK_CAP,  _task,                  L4_CAP_FPAGE_RWS, 0},
paul@376 144
    {_env.factory,      l4re_env()->factory,    L4_CAP_FPAGE_RWS, 0},
paul@376 145
    {_env.log,          l4re_env()->log,        L4_CAP_FPAGE_RWS, 0},
paul@376 146
    {_env.scheduler,    l4re_env()->scheduler,  L4_CAP_FPAGE_RWS, 0},
paul@376 147
    {_env.mem_alloc,    l4re_env()->mem_alloc,  L4_CAP_FPAGE_RWS, 0},
paul@376 148
    {0,                 L4_INVALID_CAP,         0,                0},
paul@324 149
    };
paul@324 150
paul@501 151
  /* Return the capability details for the task. */
paul@501 152
paul@501 153
  *task = _task;
paul@503 154
  *mapped_task = L4_BASE_TASK_CAP;
paul@501 155
paul@365 156
  return map_capabilities(mapped_caps, false);
paul@365 157
}
paul@365 158
paul@503 159
/* Configure the thread environment, employing the given capability for the
paul@503 160
   region mapper, returning its capability details in the new task. */
paul@365 161
paul@503 162
long Process::configure_thread(l4_cap_idx_t rm, l4_cap_idx_t *mapped_rm)
paul@365 163
{
paul@370 164
  /* Employ a distinct region mapper for each thread's environment, this acting
paul@370 165
     as pager. */
paul@370 166
paul@503 167
  if ((mapped_rm != NULL) && l4_is_valid_cap(*mapped_rm))
paul@376 168
  {
paul@503 169
    _env.rm = *mapped_rm;
paul@376 170
    return L4_EOK;
paul@376 171
  }
paul@376 172
  else
paul@376 173
  {
paul@376 174
    _env.rm = allocate_cap();
paul@503 175
    *mapped_rm = _env.rm;
paul@503 176
    return ipc_map_capability(_task, (struct ipc_mapped_cap) {_env.rm, rm, L4_CAP_FPAGE_RWS, 0});
paul@376 177
  }
paul@365 178
}
paul@365 179
paul@469 180
/* Set the parent of the new thread. */
paul@469 181
paul@503 182
long Process::set_parent(l4_cap_idx_t parent, l4_cap_idx_t *mapped_parent)
paul@469 183
{
paul@503 184
  *mapped_parent = _env.parent;
paul@499 185
  return ipc_map_capability(_task, (struct ipc_mapped_cap) {_env.parent, parent, L4_CAP_FPAGE_RWS, 0});
paul@469 186
}
paul@469 187
paul@510 188
/* Map capabilities into the task, counting them if indicated. If capability
paul@510 189
   indexes are obtained using the allocate_cap method, then they do not need to
paul@510 190
   be counted again. */
paul@365 191
paul@365 192
long Process::map_capabilities(struct ipc_mapped_cap mapped_caps[],
paul@365 193
                               bool to_count)
paul@365 194
{
paul@370 195
  unsigned int num_mapped_caps;
paul@370 196
  long err = ipc_map_capabilities(_task, mapped_caps, to_count ? &num_mapped_caps : NULL);
paul@370 197
paul@370 198
  if (to_count)
paul@370 199
    _env.first_free_cap += num_mapped_caps;
paul@370 200
paul@370 201
  return err;
paul@324 202
}
paul@324 203
paul@324 204
/* Create, initialise and start a thread. */
paul@324 205
paul@503 206
long Process::thread_start(l4_addr_t program_start, Stack &st,
paul@503 207
                           l4_cap_idx_t *thread, l4_cap_idx_t *mapped_thread)
paul@324 208
{
paul@324 209
  long err;
paul@324 210
paul@492 211
  err = create_thread(thread);
paul@324 212
paul@324 213
  if (err)
paul@324 214
    return err;
paul@324 215
paul@429 216
  /* Obtain UTCB area details for the thread. */
paul@429 217
paul@429 218
  l4_fpage_t utcb_fpage = get_utcb_fpage(_thread_number, 1);
paul@429 219
paul@429 220
  _env.utcb_area       = utcb_fpage;
paul@514 221
  _env.first_free_utcb = l4_fpage_memaddr(utcb_fpage) + L4_UTCB_OFFSET;
paul@429 222
paul@324 223
  /* Initialise the thread with pager, UTCB and task details. */
paul@324 224
paul@324 225
  l4_thread_control_start();
paul@370 226
  l4_thread_control_pager(_env.rm);
paul@370 227
  l4_thread_control_exc_handler(_env.rm);
paul@429 228
  l4_thread_control_bind((l4_utcb_t *) l4_fpage_memaddr(_env.utcb_area), _task);
paul@324 229
paul@492 230
  err = l4_error(l4_thread_control_commit(*thread));
paul@324 231
paul@324 232
  if (err)
paul@324 233
  {
paul@492 234
    ipc_cap_free(*thread);
paul@324 235
    return err;
paul@324 236
  }
paul@324 237
paul@370 238
  /* Map the thread capability to the task using a distinct capability index. */
paul@370 239
paul@370 240
  _env.main_thread = allocate_cap();
paul@324 241
paul@492 242
  ipc_map_capability(_task, (struct ipc_mapped_cap) {_env.main_thread, *thread, L4_CAP_FPAGE_RWS, 0});
paul@324 243
paul@324 244
  /* Populate the initial environment in the thread. */
paul@324 245
paul@324 246
  st.set_l4re_aux(&_aux);
paul@324 247
  st.set_l4re_env(&_env);
paul@324 248
paul@376 249
  /* Reserve some extra space for capabilities used by this thread.
paul@376 250
     NOTE: Surely the capability allocator should be able to avoid conflicts,
paul@376 251
           but concurrency issues have been observed before, leading to various
paul@376 252
           measures in libipc. */
paul@376 253
paul@376 254
  _env.first_free_cap += 0x20;
paul@376 255
paul@324 256
  /* Set the start details. */
paul@324 257
paul@492 258
  err = l4_error(l4_thread_ex_regs(*thread, program_start, st.start_address(), 0));
paul@324 259
paul@324 260
  if (err)
paul@324 261
    return err;
paul@324 262
paul@429 263
  /* Select a new thread. */
paul@324 264
paul@429 265
  _thread_number++;
paul@324 266
paul@324 267
  /* Start the thread. */
paul@324 268
paul@324 269
  l4_sched_param_t sp = l4_sched_param(L4RE_MAIN_THREAD_PRIO);
paul@324 270
paul@503 271
  *mapped_thread = _env.main_thread;
paul@492 272
  return l4_error(l4_scheduler_run_thread(l4re_env()->scheduler, *thread, &sp));
paul@324 273
}
paul@324 274
paul@324 275
/* vim: tabstop=2 expandtab shiftwidth=2
paul@324 276
*/