1.1 --- a/libexec/lib/src/internal_pager.cc Sat Oct 01 16:53:23 2022 +0200
1.2 +++ b/libexec/lib/src/internal_pager.cc Sat Oct 01 16:58:16 2022 +0200
1.3 @@ -20,27 +20,26 @@
1.4 */
1.5
1.6 #include <l4/re/env.h>
1.7 -#include <l4/sys/err.h>
1.8 -#include <l4/util/util.h>
1.9 +#include <l4/re/c/dataspace.h>
1.10
1.11 -#include <ipc/mem_ipc.h>
1.12 #include <mem/memory_utils.h>
1.13
1.14 #include <stdio.h>
1.15 -#include <string.h>
1.16
1.17 #include "dataspace_client.h"
1.18 #include "internal_pager.h"
1.19
1.20
1.21
1.22 +#define DEBUG 0
1.23 +
1.24 +
1.25 +
1.26 /* A simple system pager also acting as a region mapper. */
1.27
1.28 -/* Add a region to the pager. */
1.29 -
1.30 -void InternalPager::add(ExecRegion ®ion)
1.31 +InternalPager::InternalPager(address_t start, address_t end)
1.32 +: ExecPager(start, end)
1.33 {
1.34 - _regions[region.start] = region;
1.35 }
1.36
1.37 /* Handle a general exception. */
1.38 @@ -49,43 +48,16 @@
1.39 {
1.40 (void) region;
1.41
1.42 - printf("exception(...) -> pfa = %lx, pc = %lx\n", l4_utcb_exc_pfa(®s), l4_utcb_exc_pc(®s));
1.43 + printf("InternalPager::exception(...) -> pfa = %lx, pc = %lx\n", l4_utcb_exc_pfa(®s), l4_utcb_exc_pc(®s));
1.44
1.45 -#if 0
1.46 - printf("r15 = %lx\n", regs.r15);
1.47 - printf("r14 = %lx\n", regs.r14);
1.48 - printf("r13 = %lx\n", regs.r13);
1.49 - printf("r12 = %lx\n", regs.r12);
1.50 - printf("r11 = %lx\n", regs.r11);
1.51 - printf("r10 = %lx\n", regs.r10);
1.52 - printf("r9 = %lx\n", regs.r9);
1.53 - printf("r8 = %lx\n", regs.r8);
1.54 - printf("rdi = %lx\n", regs.rdi);
1.55 - printf("rsi = %lx\n", regs.rsi);
1.56 - printf("rbp = %lx\n", regs.rbp);
1.57 - printf("pfa = %lx\n", regs.pfa);
1.58 - printf("rbx = %lx\n", regs.rbx);
1.59 - printf("rdx = %lx\n", regs.rdx);
1.60 - printf("rcx = %lx\n", regs.rcx);
1.61 - printf("rax = %lx\n", regs.rax);
1.62 - printf("trapno = %lx\n", regs.trapno);
1.63 - printf("err = %lx\n", regs.err);
1.64 - printf("ip = %lx\n", regs.ip);
1.65 - printf("flags = %lx\n", regs.flags);
1.66 - printf("sp = %lx\n", regs.sp);
1.67 - printf("ss = %lx\n", regs.ss);
1.68 - printf("fs_base = %lx\n", regs.fs_base);
1.69 - printf("gs_base = %lx\n", regs.gs_base);
1.70 -#endif
1.71 -
1.72 +#if DEBUG
1.73 while (1)
1.74 l4_sleep_forever();
1.75 +#endif
1.76
1.77 return L4_EOK;
1.78 }
1.79
1.80 -#define DEBUG 0
1.81 -
1.82 /* Handle a page fault using any configured regions. */
1.83
1.84 long InternalPager::page_fault(l4_umword_t pfa, l4_umword_t pc, l4_snd_fpage_t *region)
1.85 @@ -94,19 +66,21 @@
1.86
1.87 #if DEBUG
1.88 printf("page_fault(%lx, %lx) -> %lx (%lx) -> ", pfa, pc, addr, flags);
1.89 + l4_thread_regs_t *regs = l4_utcb_tcr();
1.90 + printf("regs = %p\n; regs->user[0] = %lx\n", regs, regs->user[0]);
1.91 #endif
1.92
1.93 - ExecRegions::iterator it = _regions.upper_bound(addr);
1.94 + MappedRegions::iterator it = _regions.upper_bound(addr);
1.95
1.96 if (it != _regions.begin())
1.97 it--;
1.98 else
1.99 {
1.100 - printf("not mapped!\n");
1.101 + printf("not mapped at %lx for pc %lx\n", addr, pc);
1.102 return -L4_ENOMEM;
1.103 }
1.104
1.105 - ExecRegion &r = it->second;
1.106 + MappedRegion &r = it->second;
1.107
1.108 if ((addr >= r.start) && (addr < r.start + r.size))
1.109 {
1.110 @@ -157,95 +131,15 @@
1.111 l4_cap_idx_t ds, address_t offset,
1.112 unsigned char align)
1.113 {
1.114 -#if DEBUG
1.115 - printf("attach(%lx, %ld, %lx, ..., %lx, %d)\n", *start, size, flags, offset, align);
1.116 -#endif
1.117 -
1.118 - if (align < L4_PAGESHIFT)
1.119 - align = L4_PAGESHIFT;
1.120 -
1.121 - address_t increment = 1UL << align;
1.122 - address_t region_size = round(size, increment);
1.123 -
1.124 - /* Either attempt to find an address for the specified region, starting from
1.125 - any indicated address. */
1.126 -
1.127 - if (flags & L4RE_RM_F_SEARCH_ADDR)
1.128 - {
1.129 - address_t region_start = trunc(*start, increment);
1.130 - ExecRegions::iterator it = _regions.upper_bound(*start);
1.131 -
1.132 - if (!region_start)
1.133 - region_start += increment;
1.134 -
1.135 -#if DEBUG
1.136 - printf("-> search from %lx -> %lx...\n", *start, region_start);
1.137 -#endif
1.138 -
1.139 - /* Before last known region. */
1.140 -
1.141 - while (it != _regions.end())
1.142 - {
1.143 - ExecRegions::iterator next = it;
1.144 - ExecRegion &r = it->second;
1.145 - address_t start_limit;
1.146 - address_t end_limit = r.start;
1.147 -
1.148 - /* Consider any preceding region. If no such region exists, choose an
1.149 - address at the start of memory. */
1.150 + long err = find(start, &size, flags, offset, align);
1.151
1.152 - if (it == _regions.begin())
1.153 - start_limit = L4_PAGESIZE;
1.154 - else
1.155 - {
1.156 - it--;
1.157 - ExecRegion &pr = it->second;
1.158 - start_limit = pr.start + pr.size;
1.159 - it = next;
1.160 - }
1.161 -
1.162 - /* Test against the limits. */
1.163 -
1.164 - if (region_start < start_limit)
1.165 - region_start = round(start_limit, increment);
1.166 -
1.167 - /* Investigate subsequent regions if not enough space exists between the
1.168 - preceding region (or start of memory) and the current region. */
1.169 -
1.170 - if ((region_start + region_size) > end_limit)
1.171 - {
1.172 - it++;
1.173 - if (it == _regions.end())
1.174 - return -L4_ENOMEM;
1.175 - }
1.176 - else
1.177 - break;
1.178 - }
1.179 -
1.180 -#if DEBUG
1.181 - printf("-> added region at %lx size %ld (%d) ds %lx\n", region_start, region_size, page_order(region_size), ds);
1.182 -#endif
1.183 -
1.184 - ExecRegion r = (ExecRegion) {region_start, region_size, flags & L4RE_DS_F_RIGHTS_MASK, ds};
1.185 -
1.186 + if (!err)
1.187 + {
1.188 + MappedRegion r(*start, size, flags & L4RE_DS_F_RIGHTS_MASK, ds);
1.189 add(r);
1.190 -
1.191 - *start = region_start;
1.192 - return L4_EOK;
1.193 }
1.194
1.195 - /* Or attempt to add the specified region at a specific address. */
1.196 -
1.197 - else
1.198 - {
1.199 - // NOTE: To be implemented.
1.200 -
1.201 -#if DEBUG
1.202 - printf("-> region of size %ld (%d) not added!\n", region_size, page_order(region_size));
1.203 -#endif
1.204 -
1.205 - return -L4_ENOMEM;
1.206 - }
1.207 + return err;
1.208 }
1.209
1.210 /* vim: tabstop=2 expandtab shiftwidth=2