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