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@308 | 24 | #include <l4/sys/factory.h> |
paul@308 | 25 | #include <l4/sys/task.h> |
paul@308 | 26 | #include <l4/sys/thread.h> |
paul@312 | 27 | #include <l4/util/elf.h> |
paul@312 | 28 | #include <l4/util/util.h> |
paul@308 | 29 | |
paul@315 | 30 | #include <l4/libloader/adjust_stack> |
paul@315 | 31 | |
paul@308 | 32 | #include <fsclient/client.h> |
paul@308 | 33 | #include <ipc/cap_alloc.h> |
paul@308 | 34 | #include <ipc/mem_ipc.h> |
paul@308 | 35 | #include <ipc/server.h> |
paul@311 | 36 | #include <mem/memory_utils.h> |
paul@308 | 37 | #include <systypes/fcntl.h> |
paul@308 | 38 | |
paul@312 | 39 | #include <map> |
paul@312 | 40 | |
paul@308 | 41 | #include <stdio.h> |
paul@308 | 42 | #include <stdlib.h> |
paul@308 | 43 | #include <string.h> |
paul@308 | 44 | |
paul@308 | 45 | #include <pthread-l4.h> |
paul@308 | 46 | #include <pthread.h> |
paul@308 | 47 | |
paul@312 | 48 | #include "pager_object_interface.h" |
paul@312 | 49 | #include "pager_object_server.h" |
paul@308 | 50 | |
paul@308 | 51 | |
paul@308 | 52 | |
paul@312 | 53 | /* Region data structures. */ |
paul@308 | 54 | |
paul@312 | 55 | class Region |
paul@308 | 56 | { |
paul@308 | 57 | public: |
paul@312 | 58 | l4_addr_t start; |
paul@312 | 59 | unsigned int log2size; |
paul@312 | 60 | l4_umword_t flags; |
paul@312 | 61 | l4_addr_t map_start; |
paul@312 | 62 | |
paul@312 | 63 | explicit Region() |
paul@312 | 64 | : start(0), log2size(0), flags(0), map_start(0) |
paul@312 | 65 | { |
paul@312 | 66 | } |
paul@312 | 67 | |
paul@312 | 68 | explicit Region(l4_addr_t start, unsigned int log2size, l4_umword_t flags, |
paul@312 | 69 | l4_addr_t map_start) |
paul@312 | 70 | : start(start), log2size(log2size), flags(flags), map_start(map_start) |
paul@312 | 71 | { |
paul@312 | 72 | } |
paul@312 | 73 | }; |
paul@312 | 74 | |
paul@312 | 75 | typedef std::map<l4_addr_t, Region> Regions; |
paul@312 | 76 | |
paul@312 | 77 | |
paul@312 | 78 | |
paul@312 | 79 | /* A simple system pager also acting as a region mapper. */ |
paul@312 | 80 | |
paul@312 | 81 | class ExecPager : public PagerObject |
paul@312 | 82 | { |
paul@312 | 83 | protected: |
paul@312 | 84 | Regions _regions; |
paul@312 | 85 | |
paul@312 | 86 | public: |
paul@312 | 87 | virtual void add(Region region) |
paul@312 | 88 | { |
paul@312 | 89 | _regions[region.map_start] = region; |
paul@312 | 90 | } |
paul@312 | 91 | |
paul@312 | 92 | /* Notification methods. */ |
paul@308 | 93 | |
paul@308 | 94 | virtual long exception(l4_exc_regs_t regs, |
paul@308 | 95 | l4_snd_fpage_t *region); |
paul@308 | 96 | |
paul@308 | 97 | virtual long page_fault(l4_umword_t pfa, l4_umword_t pc, |
paul@308 | 98 | l4_snd_fpage_t *region); |
paul@312 | 99 | |
paul@312 | 100 | /* Region manager/mapper methods. */ |
paul@312 | 101 | |
paul@312 | 102 | virtual long attach(address_t *start, offset_t size, map_flags_t flags, |
paul@312 | 103 | l4_cap_idx_t ds, address_t offset, unsigned char align); |
paul@312 | 104 | |
paul@308 | 105 | }; |
paul@308 | 106 | |
paul@312 | 107 | /* Handle a general exception. */ |
paul@312 | 108 | |
paul@308 | 109 | long ExecPager::exception(l4_exc_regs_t regs, l4_snd_fpage_t *region) |
paul@308 | 110 | { |
paul@308 | 111 | (void) region; |
paul@308 | 112 | |
paul@308 | 113 | printf("exception(...) -> pfa = %lx, pc = %lx\n", l4_utcb_exc_pfa(®s), l4_utcb_exc_pc(®s)); |
paul@312 | 114 | |
paul@312 | 115 | printf("r15 = %lx\n", regs.r15); |
paul@312 | 116 | printf("r14 = %lx\n", regs.r14); |
paul@312 | 117 | printf("r13 = %lx\n", regs.r13); |
paul@312 | 118 | printf("r12 = %lx\n", regs.r12); |
paul@312 | 119 | printf("r11 = %lx\n", regs.r11); |
paul@312 | 120 | printf("r10 = %lx\n", regs.r10); |
paul@312 | 121 | printf("r9 = %lx\n", regs.r9); |
paul@312 | 122 | printf("r8 = %lx\n", regs.r8); |
paul@312 | 123 | printf("rdi = %lx\n", regs.rdi); |
paul@312 | 124 | printf("rsi = %lx\n", regs.rsi); |
paul@312 | 125 | printf("rbp = %lx\n", regs.rbp); |
paul@312 | 126 | printf("pfa = %lx\n", regs.pfa); |
paul@312 | 127 | printf("rbx = %lx\n", regs.rbx); |
paul@312 | 128 | printf("rdx = %lx\n", regs.rdx); |
paul@312 | 129 | printf("rcx = %lx\n", regs.rcx); |
paul@312 | 130 | printf("rax = %lx\n", regs.rax); |
paul@312 | 131 | printf("trapno = %lx\n", regs.trapno); |
paul@312 | 132 | printf("err = %lx\n", regs.err); |
paul@312 | 133 | printf("ip = %lx\n", regs.ip); |
paul@312 | 134 | printf("flags = %lx\n", regs.flags); |
paul@312 | 135 | printf("sp = %lx\n", regs.sp); |
paul@312 | 136 | printf("ss = %lx\n", regs.ss); |
paul@312 | 137 | printf("fs_base = %lx\n", regs.fs_base); |
paul@312 | 138 | printf("gs_base = %lx\n", regs.gs_base); |
paul@312 | 139 | |
paul@308 | 140 | return L4_EOK; |
paul@308 | 141 | } |
paul@308 | 142 | |
paul@315 | 143 | #define DEBUG 0 |
paul@315 | 144 | |
paul@312 | 145 | /* Handle a page fault using any configured regions. */ |
paul@312 | 146 | |
paul@308 | 147 | long ExecPager::page_fault(l4_umword_t pfa, l4_umword_t pc, l4_snd_fpage_t *region) |
paul@308 | 148 | { |
paul@308 | 149 | l4_umword_t addr = pfa & ~7UL, flags = pfa & 7; |
paul@308 | 150 | |
paul@315 | 151 | #if DEBUG |
paul@312 | 152 | printf("page_fault(%lx, %lx) -> %lx (%lx) -> ", pfa, pc, addr, flags); |
paul@312 | 153 | #endif |
paul@312 | 154 | |
paul@312 | 155 | Regions::iterator it = _regions.upper_bound(addr); |
paul@312 | 156 | |
paul@312 | 157 | if (it != _regions.begin()) |
paul@312 | 158 | it--; |
paul@312 | 159 | else |
paul@312 | 160 | { |
paul@312 | 161 | printf("not mapped!\n"); |
paul@312 | 162 | return -L4_ENOMEM; |
paul@312 | 163 | } |
paul@312 | 164 | |
paul@312 | 165 | Region &r = it->second; |
paul@308 | 166 | |
paul@312 | 167 | if ((addr >= r.map_start) && (addr < r.map_start + (1UL << r.log2size))) |
paul@308 | 168 | { |
paul@312 | 169 | l4_addr_t page_addr = trunc(addr, L4_PAGESIZE); |
paul@312 | 170 | |
paul@312 | 171 | region->fpage = l4_fpage(r.start + (page_addr - r.map_start), L4_PAGESHIFT, r.flags); |
paul@312 | 172 | region->snd_base = page_addr; |
paul@312 | 173 | |
paul@315 | 174 | #if DEBUG |
paul@312 | 175 | printf("%lx...%lx from %lx...%lx size %d rights %x\n", |
paul@312 | 176 | r.map_start, region->snd_base, |
paul@312 | 177 | r.start, l4_fpage_memaddr(region->fpage), |
paul@312 | 178 | l4_fpage_size(region->fpage), |
paul@312 | 179 | l4_fpage_rights(region->fpage)); |
paul@312 | 180 | printf("%lx -> ", addr); |
paul@312 | 181 | |
paul@312 | 182 | for (unsigned int i = 0; i < sizeof(l4_umword_t); i++) |
paul@312 | 183 | printf("%02x", *((unsigned char *)(r.start + (addr - r.map_start) + i))); |
paul@312 | 184 | |
paul@312 | 185 | printf("\n"); |
paul@312 | 186 | #endif |
paul@308 | 187 | |
paul@308 | 188 | return L4_EOK; |
paul@308 | 189 | } |
paul@308 | 190 | |
paul@315 | 191 | #if DEBUG |
paul@312 | 192 | printf("not mapped!\n"); |
paul@312 | 193 | #endif |
paul@312 | 194 | |
paul@308 | 195 | return -L4_ENOMEM; |
paul@308 | 196 | } |
paul@308 | 197 | |
paul@312 | 198 | /* Attach a region for provision when page faults occur. This is required in |
paul@312 | 199 | the initialisation of a program by the C library which requires a region |
paul@312 | 200 | mapper. */ |
paul@312 | 201 | |
paul@312 | 202 | long ExecPager::attach(address_t *start, offset_t size, map_flags_t flags, |
paul@312 | 203 | l4_cap_idx_t ds, address_t offset, unsigned char align) |
paul@312 | 204 | { |
paul@315 | 205 | #if DEBUG |
paul@312 | 206 | printf("attach(%lx, %ld, %lx, ..., %lx, %d)\n", *start, size, flags, offset, align); |
paul@312 | 207 | #endif |
paul@312 | 208 | |
paul@312 | 209 | if (align < L4_PAGESHIFT) |
paul@312 | 210 | align = L4_PAGESHIFT; |
paul@312 | 211 | |
paul@312 | 212 | offset_t increment = 1UL << align; |
paul@312 | 213 | offset_t region_size = round(size, increment); |
paul@312 | 214 | |
paul@312 | 215 | /* Either attempt to find an address for the specified region, starting from |
paul@312 | 216 | any indicated address. */ |
paul@312 | 217 | |
paul@312 | 218 | if (flags & L4RE_RM_F_SEARCH_ADDR) |
paul@312 | 219 | { |
paul@312 | 220 | address_t region_start = trunc(*start, increment); |
paul@312 | 221 | Regions::iterator it = _regions.upper_bound(*start); |
paul@312 | 222 | |
paul@312 | 223 | if (!region_start) |
paul@312 | 224 | region_start += increment; |
paul@312 | 225 | |
paul@315 | 226 | #if DEBUG |
paul@312 | 227 | printf("-> search from %lx -> %lx...\n", *start, region_start); |
paul@312 | 228 | #endif |
paul@312 | 229 | |
paul@312 | 230 | /* Before last known region. */ |
paul@312 | 231 | |
paul@312 | 232 | while (it != _regions.end()) |
paul@312 | 233 | { |
paul@312 | 234 | Regions::iterator next = it; |
paul@312 | 235 | Region &r = it->second; |
paul@312 | 236 | address_t start_limit; |
paul@312 | 237 | address_t end_limit = r.map_start; |
paul@312 | 238 | |
paul@312 | 239 | /* Consider any preceding region. If no such region exists, choose an |
paul@312 | 240 | address at the start of memory. */ |
paul@312 | 241 | |
paul@312 | 242 | if (it == _regions.begin()) |
paul@312 | 243 | start_limit = L4_PAGESIZE; |
paul@312 | 244 | else |
paul@312 | 245 | { |
paul@312 | 246 | it--; |
paul@312 | 247 | Region &pr = it->second; |
paul@312 | 248 | start_limit = pr.map_start + (1UL << pr.log2size); |
paul@312 | 249 | it = next; |
paul@312 | 250 | } |
paul@312 | 251 | |
paul@312 | 252 | /* Test against the limits. */ |
paul@312 | 253 | |
paul@312 | 254 | if (region_start < start_limit) |
paul@312 | 255 | region_start = round(start_limit, increment); |
paul@312 | 256 | |
paul@312 | 257 | /* Investigate subsequent regions if not enough space exists between the |
paul@312 | 258 | preceding region (or start of memory) and the current region. */ |
paul@312 | 259 | |
paul@312 | 260 | if ((region_start + region_size) > end_limit) |
paul@312 | 261 | { |
paul@312 | 262 | it++; |
paul@312 | 263 | if (it == _regions.end()) |
paul@312 | 264 | return -L4_ENOMEM; |
paul@312 | 265 | } |
paul@312 | 266 | else |
paul@312 | 267 | break; |
paul@312 | 268 | } |
paul@312 | 269 | |
paul@312 | 270 | /* Attach the provided dataspace. |
paul@312 | 271 | NOTE: This is only done in this implementation to support the paging |
paul@312 | 272 | mechanism. In a region mapper residing within the actual task, the |
paul@312 | 273 | dataspace's map operation would be invoked to obtain mappings. */ |
paul@312 | 274 | |
paul@312 | 275 | l4_addr_t ds_start; |
paul@312 | 276 | |
paul@312 | 277 | long err = ipc_attach_dataspace(ds, size, (void **) &ds_start); |
paul@312 | 278 | |
paul@312 | 279 | if (err) |
paul@312 | 280 | return err; |
paul@312 | 281 | |
paul@312 | 282 | l4_touch_rw((const void *) ds_start, size); |
paul@312 | 283 | |
paul@315 | 284 | #if DEBUG |
paul@312 | 285 | printf("-> added region for %lx size %ld (%d)\n", region_start, region_size, page_order(region_size)); |
paul@312 | 286 | #endif |
paul@312 | 287 | |
paul@312 | 288 | add(Region(ds_start, page_order(region_size), flags & L4RE_DS_F_RIGHTS_MASK, region_start)); |
paul@312 | 289 | |
paul@312 | 290 | *start = region_start; |
paul@312 | 291 | return L4_EOK; |
paul@312 | 292 | } |
paul@312 | 293 | |
paul@312 | 294 | /* Or attempt to add the specified region at a specific address. */ |
paul@312 | 295 | |
paul@312 | 296 | else |
paul@312 | 297 | { |
paul@312 | 298 | // NOTE: To be implemented. |
paul@312 | 299 | |
paul@312 | 300 | return -L4_ENOMEM; |
paul@312 | 301 | } |
paul@312 | 302 | } |
paul@312 | 303 | |
paul@308 | 304 | |
paul@308 | 305 | |
paul@308 | 306 | static ExecPager exec_pager; |
paul@308 | 307 | |
paul@312 | 308 | static void init_pager(ipc_server_config_type *config) |
paul@308 | 309 | { |
paul@308 | 310 | ipc_server_init_config(config); |
paul@308 | 311 | |
paul@312 | 312 | config->expected_items = PagerObject_expected_items; |
paul@312 | 313 | config->handler = (ipc_server_handler_type) handle_PagerObject; |
paul@312 | 314 | config->handler_obj = static_cast<PagerObject *>(&exec_pager); |
paul@308 | 315 | } |
paul@308 | 316 | |
paul@308 | 317 | static long start_pager(ipc_server_config_type *config, pthread_t thread) |
paul@308 | 318 | { |
paul@308 | 319 | config->config_thread = 1; |
paul@308 | 320 | config->thread = pthread_l4_cap(thread); |
paul@308 | 321 | |
paul@308 | 322 | printf("Starting pager thread...\n"); |
paul@308 | 323 | return ipc_server_start_config(config); |
paul@308 | 324 | } |
paul@308 | 325 | |
paul@308 | 326 | |
paul@308 | 327 | |
paul@308 | 328 | /* UTCB properties. |
paul@308 | 329 | See: moe/server/src/loader.cc */ |
paul@308 | 330 | |
paul@308 | 331 | enum ipc_exec_utcb |
paul@308 | 332 | { |
paul@308 | 333 | Default_max_threads = 16, |
paul@308 | 334 | #ifdef ARCH_mips |
paul@308 | 335 | Utcb_area_start = 0x73000000, |
paul@308 | 336 | #else |
paul@308 | 337 | Utcb_area_start = 0xb3000000, |
paul@308 | 338 | #endif |
paul@308 | 339 | }; |
paul@308 | 340 | |
paul@308 | 341 | |
paul@308 | 342 | |
paul@311 | 343 | /* Capability mapping definitions for the new task. */ |
paul@311 | 344 | |
paul@311 | 345 | struct mapped_cap |
paul@311 | 346 | { |
paul@311 | 347 | l4_cap_idx_t cap; |
paul@311 | 348 | unsigned char rights; |
paul@311 | 349 | l4_umword_t spot; |
paul@311 | 350 | }; |
paul@311 | 351 | |
paul@311 | 352 | static long map_capabilities(l4_cap_idx_t task, struct mapped_cap mapped_caps[]) |
paul@308 | 353 | { |
paul@311 | 354 | long err = L4_EOK; |
paul@311 | 355 | int i = 0; |
paul@311 | 356 | |
paul@311 | 357 | while (l4_is_valid_cap(mapped_caps[i].cap) && !err) |
paul@311 | 358 | { |
paul@311 | 359 | err = l4_error(l4_task_map(task, L4RE_THIS_TASK_CAP, |
paul@311 | 360 | l4_obj_fpage(mapped_caps[i].cap, 0, mapped_caps[i].rights), |
paul@311 | 361 | l4_map_obj_control(mapped_caps[i].spot, L4_MAP_ITEM_MAP))); |
paul@311 | 362 | i++; |
paul@311 | 363 | } |
paul@311 | 364 | |
paul@311 | 365 | return err; |
paul@308 | 366 | } |
paul@308 | 367 | |
paul@311 | 368 | |
paul@311 | 369 | |
paul@308 | 370 | int main(int argc, char *argv[]) |
paul@308 | 371 | { |
paul@308 | 372 | long err; |
paul@308 | 373 | |
paul@308 | 374 | if (argc < 2) |
paul@308 | 375 | { |
paul@308 | 376 | printf("Need a program to run.\n"); |
paul@308 | 377 | return 1; |
paul@308 | 378 | } |
paul@308 | 379 | |
paul@308 | 380 | /* Allocate capabilities for the task and thread. */ |
paul@308 | 381 | |
paul@311 | 382 | l4_cap_idx_t caps[2]; |
paul@308 | 383 | |
paul@311 | 384 | err = ipc_cap_alloc_many(caps, 2); |
paul@308 | 385 | |
paul@311 | 386 | if (err) |
paul@308 | 387 | { |
paul@311 | 388 | printf("Could not allocate capabilities.\n"); |
paul@308 | 389 | return 1; |
paul@308 | 390 | } |
paul@308 | 391 | |
paul@311 | 392 | l4_cap_idx_t &task = caps[0]; |
paul@311 | 393 | l4_cap_idx_t &thread = caps[1]; |
paul@308 | 394 | |
paul@308 | 395 | /* Obtain the payload as a dataspace. */ |
paul@308 | 396 | |
paul@308 | 397 | file_t *file = client_open(argv[1], O_RDONLY); |
paul@308 | 398 | |
paul@308 | 399 | if (file == NULL) |
paul@308 | 400 | { |
paul@308 | 401 | printf("Could not read file: %s\n", argv[1]); |
paul@308 | 402 | return 1; |
paul@308 | 403 | } |
paul@308 | 404 | |
paul@312 | 405 | /* Copy the payload regions to new dataspaces. |
paul@312 | 406 | NOTE: This should be directed by the ELF metadata. */ |
paul@308 | 407 | |
paul@315 | 408 | address_t program_region_base = 0x1000000; |
paul@315 | 409 | address_t program_start = 0x1000af3; |
paul@315 | 410 | |
paul@312 | 411 | char *program_buf; |
paul@308 | 412 | offset_t nread; |
paul@315 | 413 | offset_t program_region_contents = 0x282ae; |
paul@312 | 414 | offset_t program_region_size = round(program_region_contents, L4_PAGESIZE); |
paul@312 | 415 | l4re_ds_t program_region_ds; |
paul@308 | 416 | |
paul@312 | 417 | err = ipc_allocate_align(program_region_size, L4RE_RM_F_SEARCH_ADDR | L4RE_RM_F_RWX, |
paul@312 | 418 | L4_PAGESHIFT, (void **) &program_buf, &program_region_ds); |
paul@308 | 419 | |
paul@308 | 420 | if (err) |
paul@308 | 421 | { |
paul@312 | 422 | printf("Could not reserve program memory.\n"); |
paul@308 | 423 | return 1; |
paul@308 | 424 | } |
paul@308 | 425 | |
paul@312 | 426 | nread = client_read(file, program_buf, program_region_contents); |
paul@308 | 427 | |
paul@312 | 428 | printf("Read %ld from file into %p.\n", nread, program_buf); |
paul@308 | 429 | |
paul@315 | 430 | if (memcmp(program_buf + program_start - program_region_base, "\x31\xed", 2)) |
paul@308 | 431 | { |
paul@308 | 432 | printf("Did not find expected instructions at start.\n"); |
paul@308 | 433 | return 1; |
paul@308 | 434 | } |
paul@308 | 435 | |
paul@315 | 436 | offset_t data_region_start = 0x1029360; |
paul@312 | 437 | offset_t data_region_size = round(0x8068, L4_PAGESIZE); |
paul@312 | 438 | offset_t data_region_base = trunc(data_region_start, L4_PAGESIZE); |
paul@312 | 439 | offset_t data_region_offset = data_region_start - data_region_base; |
paul@312 | 440 | |
paul@312 | 441 | char *data_buf; |
paul@315 | 442 | offset_t data_file_offset = 0x28360; |
paul@312 | 443 | offset_t data_region_contents = 0x2058; |
paul@312 | 444 | l4re_ds_t data_region_ds; |
paul@312 | 445 | |
paul@312 | 446 | err = ipc_allocate_align(data_region_size, L4RE_RM_F_SEARCH_ADDR | L4RE_RM_F_RW, |
paul@312 | 447 | L4_PAGESHIFT, (void **) &data_buf, &data_region_ds); |
paul@312 | 448 | |
paul@312 | 449 | if (err) |
paul@312 | 450 | { |
paul@312 | 451 | printf("Could not reserve data memory.\n"); |
paul@312 | 452 | return 1; |
paul@312 | 453 | } |
paul@312 | 454 | |
paul@312 | 455 | memset(data_buf, 0, data_region_size); |
paul@312 | 456 | |
paul@315 | 457 | client_seek(file, data_file_offset, SEEK_SET); |
paul@312 | 458 | nread = client_read(file, data_buf + data_region_offset, data_region_contents); |
paul@312 | 459 | |
paul@312 | 460 | printf("Read %ld from file into %p in region %p with size %ld for %lx.\n", |
paul@312 | 461 | nread, data_buf + data_region_offset, data_buf, data_region_size, data_region_base); |
paul@312 | 462 | |
paul@308 | 463 | /* UTCB location and size. */ |
paul@308 | 464 | |
paul@308 | 465 | l4_addr_t utcb_start = Utcb_area_start; |
paul@311 | 466 | int utcb_log2size = page_order(Default_max_threads * L4_UTCB_OFFSET); |
paul@308 | 467 | |
paul@308 | 468 | /* Round up to at least one page. */ |
paul@308 | 469 | |
paul@308 | 470 | if (utcb_log2size < L4_PAGESHIFT) |
paul@308 | 471 | utcb_log2size = L4_PAGESHIFT; |
paul@308 | 472 | |
paul@312 | 473 | l4_fpage_t utcb_fpage = l4_fpage(utcb_start, utcb_log2size, 0); |
paul@312 | 474 | |
paul@308 | 475 | /* KIP allocation. */ |
paul@308 | 476 | |
paul@308 | 477 | l4_addr_t kip_start = (l4_addr_t) l4re_kip(); |
paul@308 | 478 | |
paul@308 | 479 | printf("KIP at %lx.\n", kip_start); |
paul@308 | 480 | |
paul@312 | 481 | /* Stack allocation. */ |
paul@312 | 482 | |
paul@312 | 483 | l4_addr_t stack_buf; |
paul@312 | 484 | offset_t stack_size = 16 * L4_PAGESIZE; |
paul@312 | 485 | l4_addr_t stack_region_base = 0x8000000 - stack_size; |
paul@312 | 486 | l4re_ds_t stack_ds; |
paul@312 | 487 | |
paul@312 | 488 | err = ipc_allocate_align(stack_size, L4RE_RM_F_SEARCH_ADDR | L4RE_RM_F_RW, |
paul@312 | 489 | L4_PAGESHIFT, (void **) &stack_buf, &stack_ds); |
paul@312 | 490 | |
paul@312 | 491 | if (err) |
paul@312 | 492 | { |
paul@312 | 493 | printf("Could not reserve stack.\n"); |
paul@312 | 494 | return 1; |
paul@312 | 495 | } |
paul@312 | 496 | |
paul@312 | 497 | /* Populate stack with additional capabilities. */ |
paul@312 | 498 | |
paul@312 | 499 | l4re_env_cap_entry_t *stack_env_cap = (l4re_env_cap_entry_t *) (stack_buf + stack_size); |
paul@312 | 500 | |
paul@312 | 501 | /* Special invalid/terminating environment capability entry. */ |
paul@312 | 502 | |
paul@312 | 503 | *(--stack_env_cap) = l4re_env_cap_entry_t(); |
paul@312 | 504 | |
paul@312 | 505 | printf("Stack region end: %p\n", stack_env_cap); |
paul@312 | 506 | |
paul@312 | 507 | l4_addr_t caps_start = (l4_addr_t) stack_env_cap; |
paul@312 | 508 | l4_umword_t *stack_element = (l4_umword_t *) stack_env_cap; |
paul@312 | 509 | |
paul@315 | 510 | /* Populate stack with argument values. */ |
paul@315 | 511 | |
paul@315 | 512 | char *stack_arg = (char *) stack_element; |
paul@315 | 513 | |
paul@315 | 514 | stack_arg = (char *) trunc((offset_t) stack_arg - strlen(argv[1]) - 1, sizeof(l4_umword_t)); |
paul@315 | 515 | |
paul@315 | 516 | memset(stack_arg, 0, (char *) stack_element - stack_arg); |
paul@315 | 517 | memcpy(stack_arg, argv[1], strlen(argv[1])); |
paul@315 | 518 | |
paul@315 | 519 | printf("Stack L4 program argument: %p / %lx\n", stack_arg, ((l4_addr_t) stack_arg - stack_buf) + stack_region_base); |
paul@315 | 520 | |
paul@315 | 521 | stack_element = (l4_umword_t *) stack_arg; |
paul@315 | 522 | |
paul@312 | 523 | /* Loader flags, debugging flags, and the KIP capability index. |
paul@312 | 524 | See: generate_l4aux in Remote_app_model */ |
paul@312 | 525 | |
paul@312 | 526 | *(--stack_element) = 0; |
paul@312 | 527 | *(--stack_element) = 0; |
paul@312 | 528 | *(--stack_element) = 0x14 << L4_CAP_SHIFT; |
paul@312 | 529 | |
paul@312 | 530 | printf("Stack L4 aux elements: %p / %lx\n", stack_element, ((l4_addr_t) stack_element - stack_buf) + stack_region_base); |
paul@312 | 531 | |
paul@312 | 532 | /* Populate stack with standard capabilities. */ |
paul@312 | 533 | |
paul@312 | 534 | l4re_env_t *env = (l4re_env_t *) stack_element; |
paul@312 | 535 | |
paul@312 | 536 | env--; |
paul@312 | 537 | env->factory = L4_BASE_FACTORY_CAP; |
paul@312 | 538 | env->main_thread = L4_BASE_THREAD_CAP; |
paul@312 | 539 | env->log = L4_BASE_LOG_CAP; |
paul@312 | 540 | env->scheduler = L4_BASE_SCHEDULER_CAP; |
paul@312 | 541 | env->rm = 0x11 << L4_CAP_SHIFT; |
paul@312 | 542 | env->mem_alloc = 0x12 << L4_CAP_SHIFT; |
paul@312 | 543 | env->first_free_cap = 0x15; |
paul@312 | 544 | env->caps = (l4re_env_cap_entry_t *) (caps_start - stack_buf) + stack_region_base; |
paul@312 | 545 | env->utcb_area = utcb_fpage; |
paul@312 | 546 | env->first_free_utcb = utcb_start + L4_UTCB_OFFSET; |
paul@312 | 547 | |
paul@312 | 548 | stack_element = (l4_umword_t *) env; |
paul@312 | 549 | |
paul@315 | 550 | /* Populate stack with AUXV. */ |
paul@308 | 551 | |
paul@312 | 552 | /* AUXV NULL. */ |
paul@312 | 553 | |
paul@312 | 554 | *(--stack_element) = 0; |
paul@312 | 555 | *(--stack_element) = 0; |
paul@312 | 556 | |
paul@312 | 557 | /* L4Re global environment pointer. */ |
paul@312 | 558 | |
paul@312 | 559 | *(--stack_element) = ((l4_addr_t) env - stack_buf) + stack_region_base; |
paul@312 | 560 | *(--stack_element) = 0xf1; |
paul@312 | 561 | |
paul@312 | 562 | /* Apparently required entries. */ |
paul@312 | 563 | |
paul@312 | 564 | *(--stack_element) = L4_PAGESIZE; |
paul@312 | 565 | *(--stack_element) = AT_PAGESZ; |
paul@312 | 566 | |
paul@312 | 567 | *(--stack_element) = 0; |
paul@312 | 568 | *(--stack_element) = AT_UID; |
paul@312 | 569 | |
paul@312 | 570 | *(--stack_element) = 0; |
paul@312 | 571 | *(--stack_element) = AT_EUID; |
paul@312 | 572 | |
paul@312 | 573 | *(--stack_element) = 0; |
paul@312 | 574 | *(--stack_element) = AT_GID; |
paul@312 | 575 | |
paul@312 | 576 | *(--stack_element) = 0; |
paul@312 | 577 | *(--stack_element) = AT_EGID; |
paul@312 | 578 | |
paul@315 | 579 | l4_addr_t stack_data_end = (l4_addr_t) stack_element; |
paul@312 | 580 | |
paul@315 | 581 | printf("Stack L4 data: %lx / %lx\n", stack_data_end, (stack_data_end - stack_buf) + stack_region_base); |
paul@312 | 582 | |
paul@315 | 583 | /* No environment pointers. */ |
paul@312 | 584 | |
paul@315 | 585 | *(--stack_element) = 0; |
paul@312 | 586 | |
paul@312 | 587 | /* Populate stack with argument pointers and count. */ |
paul@312 | 588 | /* NOTE: Just one argument currently. */ |
paul@312 | 589 | |
paul@315 | 590 | *(--stack_element) = 0; |
paul@312 | 591 | *(--stack_element) = (l4_umword_t) ((l4_addr_t) stack_arg - stack_buf) + stack_region_base; |
paul@312 | 592 | *(--stack_element) = 1; |
paul@312 | 593 | |
paul@315 | 594 | char *stack_adjusted = Ldr::adjust_sp((char *) stack_element, NULL); |
paul@315 | 595 | |
paul@315 | 596 | /* Adjust the stack alignment. */ |
paul@315 | 597 | |
paul@315 | 598 | if (stack_adjusted != (char *) stack_element) |
paul@315 | 599 | memmove(stack_adjusted, (const void *) stack_element, stack_data_end - (l4_addr_t) stack_element); |
paul@312 | 600 | |
paul@315 | 601 | l4_umword_t *stack_adjusted_element = (l4_umword_t *) stack_adjusted; |
paul@315 | 602 | |
paul@315 | 603 | printf("%ld %lx %lx\n", stack_adjusted_element[0], stack_adjusted_element[1], stack_adjusted_element[2]); |
paul@315 | 604 | |
paul@315 | 605 | l4_addr_t stack_start = ((l4_addr_t) stack_adjusted - stack_buf) + stack_region_base; |
paul@315 | 606 | |
paul@315 | 607 | printf("Stack L4 start: %p / %lx\n", stack_adjusted, ((l4_addr_t) stack_adjusted - stack_buf) + stack_region_base); |
paul@312 | 608 | |
paul@312 | 609 | /* Create a new task and thread. */ |
paul@308 | 610 | |
paul@308 | 611 | err = l4_error(l4_factory_create_task(l4re_env()->factory, task, utcb_fpage)); |
paul@308 | 612 | |
paul@308 | 613 | if (err) |
paul@308 | 614 | { |
paul@308 | 615 | printf("Could not create task.\n"); |
paul@308 | 616 | return 1; |
paul@308 | 617 | } |
paul@308 | 618 | |
paul@308 | 619 | err = l4_error(l4_factory_create_thread(l4re_env()->factory, thread)); |
paul@308 | 620 | |
paul@308 | 621 | if (err) |
paul@308 | 622 | { |
paul@308 | 623 | printf("Could not create thread.\n"); |
paul@308 | 624 | return 1; |
paul@308 | 625 | } |
paul@308 | 626 | |
paul@308 | 627 | /* Start the pager. */ |
paul@308 | 628 | |
paul@308 | 629 | ipc_server_config_type config; |
paul@308 | 630 | pthread_t pager_thread; |
paul@308 | 631 | pthread_attr_t attr; |
paul@308 | 632 | |
paul@308 | 633 | pthread_attr_init(&attr); |
paul@308 | 634 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
paul@308 | 635 | |
paul@312 | 636 | init_pager(&config); |
paul@312 | 637 | |
paul@315 | 638 | exec_pager.add(Region((l4_addr_t) program_buf, page_order(program_region_size), L4_FPAGE_RX, program_region_base)); |
paul@312 | 639 | exec_pager.add(Region((l4_addr_t) data_buf, page_order(data_region_size), L4_FPAGE_RW, data_region_base)); |
paul@312 | 640 | exec_pager.add(Region((l4_addr_t) stack_buf, page_order(stack_size), L4_FPAGE_RW, stack_region_base)); |
paul@308 | 641 | |
paul@308 | 642 | err = pthread_create(&pager_thread, &attr, ipc_server_start_mainloop, &config); |
paul@308 | 643 | |
paul@308 | 644 | if (err) |
paul@308 | 645 | { |
paul@308 | 646 | printf("Could not start pager thread.\n"); |
paul@308 | 647 | return 1; |
paul@308 | 648 | } |
paul@308 | 649 | |
paul@308 | 650 | err = start_pager(&config, pager_thread); |
paul@308 | 651 | |
paul@308 | 652 | if (err) |
paul@308 | 653 | { |
paul@308 | 654 | printf("Could not start pager.\n"); |
paul@308 | 655 | return 1; |
paul@308 | 656 | } |
paul@308 | 657 | |
paul@312 | 658 | /* Define capability mappings for the new task. */ |
paul@308 | 659 | |
paul@312 | 660 | struct mapped_cap mapped_caps[] = { |
paul@312 | 661 | {config.server, L4_CAP_FPAGE_RWS, 0x10 << L4_CAP_SHIFT}, |
paul@312 | 662 | {config.server, L4_CAP_FPAGE_RWS, 0x11 << L4_CAP_SHIFT}, |
paul@312 | 663 | {task, L4_CAP_FPAGE_RWS, L4_BASE_TASK_CAP}, |
paul@312 | 664 | {thread, L4_CAP_FPAGE_RWS, L4_BASE_THREAD_CAP}, |
paul@312 | 665 | {l4re_env()->factory, L4_CAP_FPAGE_RWS, L4_BASE_FACTORY_CAP}, |
paul@312 | 666 | {l4re_env()->log, L4_CAP_FPAGE_RWS, L4_BASE_LOG_CAP}, |
paul@312 | 667 | {l4re_env()->scheduler, L4_CAP_FPAGE_RWS, L4_BASE_SCHEDULER_CAP}, |
paul@312 | 668 | {l4re_env()->mem_alloc, L4_CAP_FPAGE_RWS, 0x12 << L4_CAP_SHIFT}, |
paul@312 | 669 | {L4_INVALID_CAP, 0, 0}, |
paul@312 | 670 | }; |
paul@312 | 671 | |
paul@312 | 672 | err = map_capabilities(task, mapped_caps); |
paul@308 | 673 | |
paul@308 | 674 | if (err) |
paul@308 | 675 | { |
paul@312 | 676 | printf("Could not capabilities into task.\n"); |
paul@308 | 677 | return 1; |
paul@308 | 678 | } |
paul@308 | 679 | |
paul@308 | 680 | /* Map the KIP into the task. */ |
paul@308 | 681 | |
paul@308 | 682 | err = l4_error(l4_task_map(task, L4RE_THIS_TASK_CAP, |
paul@308 | 683 | l4_fpage(kip_start, L4_PAGESHIFT, L4_FPAGE_RX), |
paul@308 | 684 | kip_start)); |
paul@308 | 685 | |
paul@308 | 686 | if (err) |
paul@308 | 687 | { |
paul@308 | 688 | printf("Could not map KIP into task.\n"); |
paul@308 | 689 | return 1; |
paul@308 | 690 | } |
paul@308 | 691 | |
paul@308 | 692 | /* Configure the thread with the region manager acting as pager and exception |
paul@308 | 693 | handler. The UTCB will be situated at an address supported by a dataspace |
paul@308 | 694 | attached to the new task. */ |
paul@308 | 695 | |
paul@308 | 696 | printf("Configure thread...\n"); |
paul@308 | 697 | |
paul@308 | 698 | l4_thread_control_start(); |
paul@308 | 699 | l4_thread_control_pager(0x10 << L4_CAP_SHIFT); |
paul@308 | 700 | l4_thread_control_exc_handler(0x10 << L4_CAP_SHIFT); |
paul@308 | 701 | l4_thread_control_bind((l4_utcb_t *) utcb_start, task); |
paul@308 | 702 | err = l4_error(l4_thread_control_commit(thread)); |
paul@308 | 703 | |
paul@308 | 704 | if (err) |
paul@308 | 705 | { |
paul@308 | 706 | printf("Could not configure thread.\n"); |
paul@308 | 707 | return 1; |
paul@308 | 708 | } |
paul@308 | 709 | |
paul@308 | 710 | /* Start the new thread. */ |
paul@308 | 711 | |
paul@308 | 712 | printf("Schedule thread...\n"); |
paul@308 | 713 | |
paul@312 | 714 | printf("Stack at 0x%lx mapped to region at 0x%lx.\n", stack_start, stack_region_base); |
paul@312 | 715 | |
paul@315 | 716 | err = l4_error(l4_thread_ex_regs(thread, program_start, stack_start, 0)); |
paul@308 | 717 | |
paul@308 | 718 | if (err) |
paul@308 | 719 | { |
paul@308 | 720 | printf("Could not set thread registers.\n"); |
paul@308 | 721 | return 1; |
paul@308 | 722 | } |
paul@308 | 723 | |
paul@308 | 724 | printf("Run thread...\n"); |
paul@308 | 725 | |
paul@308 | 726 | l4_sched_param_t sp = l4_sched_param(L4RE_MAIN_THREAD_PRIO); |
paul@308 | 727 | |
paul@308 | 728 | err = l4_error(l4_scheduler_run_thread(l4re_env()->scheduler, thread, &sp)); |
paul@308 | 729 | |
paul@308 | 730 | if (err) |
paul@308 | 731 | { |
paul@308 | 732 | printf("Could not run thread.\n"); |
paul@308 | 733 | return 1; |
paul@308 | 734 | } |
paul@308 | 735 | |
paul@308 | 736 | printf("Finished.\n"); |
paul@308 | 737 | while (1); |
paul@308 | 738 | |
paul@308 | 739 | return 0; |
paul@308 | 740 | } |
paul@308 | 741 | |
paul@308 | 742 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@308 | 743 | */ |