paul@366 | 1 | /* |
paul@366 | 2 | * A system pager implementation residing in a separate task. |
paul@366 | 3 | * |
paul@366 | 4 | * Copyright (C) 2022 Paul Boddie <paul@boddie.org.uk> |
paul@366 | 5 | * |
paul@366 | 6 | * This program is free software; you can redistribute it and/or |
paul@366 | 7 | * modify it under the terms of the GNU General Public License as |
paul@366 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@366 | 9 | * the License, or (at your option) any later version. |
paul@366 | 10 | * |
paul@366 | 11 | * This program is distributed in the hope that it will be useful, |
paul@366 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@366 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@366 | 14 | * GNU General Public License for more details. |
paul@366 | 15 | * |
paul@366 | 16 | * You should have received a copy of the GNU General Public License |
paul@366 | 17 | * along with this program; if not, write to the Free Software |
paul@366 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@366 | 19 | * Boston, MA 02110-1301, USA |
paul@366 | 20 | */ |
paul@366 | 21 | |
paul@366 | 22 | #include <l4/re/env.h> |
paul@366 | 23 | #include <l4/sys/err.h> |
paul@366 | 24 | #include <l4/util/util.h> |
paul@366 | 25 | |
paul@366 | 26 | #include <ipc/mem_ipc.h> |
paul@366 | 27 | #include <mem/memory_utils.h> |
paul@366 | 28 | |
paul@366 | 29 | #include <stdio.h> |
paul@366 | 30 | #include <string.h> |
paul@366 | 31 | |
paul@366 | 32 | #include "external_pager.h" |
paul@366 | 33 | |
paul@366 | 34 | |
paul@366 | 35 | |
paul@366 | 36 | /* A simple system pager also acting as a region mapper. */ |
paul@366 | 37 | |
paul@366 | 38 | /* Add a region to the pager. */ |
paul@366 | 39 | |
paul@366 | 40 | void ExternalPager::add(MappedRegion ®ion) |
paul@366 | 41 | { |
paul@366 | 42 | _regions[region.map_start] = region; |
paul@366 | 43 | } |
paul@366 | 44 | |
paul@366 | 45 | /* Handle a general exception. */ |
paul@366 | 46 | |
paul@366 | 47 | long ExternalPager::exception(l4_exc_regs_t regs, l4_snd_fpage_t *region) |
paul@366 | 48 | { |
paul@366 | 49 | (void) region; |
paul@366 | 50 | |
paul@366 | 51 | printf("exception(...) -> pfa = %lx, pc = %lx\n", l4_utcb_exc_pfa(®s), l4_utcb_exc_pc(®s)); |
paul@366 | 52 | |
paul@366 | 53 | printf("r15 = %lx\n", regs.r15); |
paul@366 | 54 | printf("r14 = %lx\n", regs.r14); |
paul@366 | 55 | printf("r13 = %lx\n", regs.r13); |
paul@366 | 56 | printf("r12 = %lx\n", regs.r12); |
paul@366 | 57 | printf("r11 = %lx\n", regs.r11); |
paul@366 | 58 | printf("r10 = %lx\n", regs.r10); |
paul@366 | 59 | printf("r9 = %lx\n", regs.r9); |
paul@366 | 60 | printf("r8 = %lx\n", regs.r8); |
paul@366 | 61 | printf("rdi = %lx\n", regs.rdi); |
paul@366 | 62 | printf("rsi = %lx\n", regs.rsi); |
paul@366 | 63 | printf("rbp = %lx\n", regs.rbp); |
paul@366 | 64 | printf("pfa = %lx\n", regs.pfa); |
paul@366 | 65 | printf("rbx = %lx\n", regs.rbx); |
paul@366 | 66 | printf("rdx = %lx\n", regs.rdx); |
paul@366 | 67 | printf("rcx = %lx\n", regs.rcx); |
paul@366 | 68 | printf("rax = %lx\n", regs.rax); |
paul@366 | 69 | printf("trapno = %lx\n", regs.trapno); |
paul@366 | 70 | printf("err = %lx\n", regs.err); |
paul@366 | 71 | printf("ip = %lx\n", regs.ip); |
paul@366 | 72 | printf("flags = %lx\n", regs.flags); |
paul@366 | 73 | printf("sp = %lx\n", regs.sp); |
paul@366 | 74 | printf("ss = %lx\n", regs.ss); |
paul@366 | 75 | printf("fs_base = %lx\n", regs.fs_base); |
paul@366 | 76 | printf("gs_base = %lx\n", regs.gs_base); |
paul@366 | 77 | |
paul@376 | 78 | while (1) |
paul@376 | 79 | l4_sleep_forever(); |
paul@376 | 80 | |
paul@366 | 81 | return L4_EOK; |
paul@366 | 82 | } |
paul@366 | 83 | |
paul@366 | 84 | #define DEBUG 0 |
paul@366 | 85 | |
paul@366 | 86 | /* Handle a page fault using any configured regions. */ |
paul@366 | 87 | |
paul@366 | 88 | long ExternalPager::page_fault(l4_umword_t pfa, l4_umword_t pc, l4_snd_fpage_t *region) |
paul@366 | 89 | { |
paul@366 | 90 | l4_umword_t addr = pfa & ~7UL, flags = pfa & 7; |
paul@366 | 91 | |
paul@366 | 92 | #if DEBUG |
paul@366 | 93 | printf("page_fault(%lx, %lx) -> %lx (%lx) -> ", pfa, pc, addr, flags); |
paul@366 | 94 | #endif |
paul@366 | 95 | |
paul@366 | 96 | MappedRegions::iterator it = _regions.upper_bound(addr); |
paul@366 | 97 | |
paul@366 | 98 | if (it != _regions.begin()) |
paul@366 | 99 | it--; |
paul@366 | 100 | else |
paul@366 | 101 | { |
paul@366 | 102 | printf("not mapped!\n"); |
paul@366 | 103 | return -L4_ENOMEM; |
paul@366 | 104 | } |
paul@366 | 105 | |
paul@366 | 106 | MappedRegion &r = it->second; |
paul@366 | 107 | |
paul@366 | 108 | if ((addr >= r.map_start) && (addr < r.map_start + r.size)) |
paul@366 | 109 | { |
paul@366 | 110 | l4_addr_t page_addr = trunc(addr, L4_PAGESIZE); |
paul@376 | 111 | map_flags_t map_flags = (flags & 4 ? L4RE_DS_F_RX : 0) | (flags & 2 ? L4RE_DS_F_W : 0) | (flags & 1 ? L4RE_DS_F_R : 0); |
paul@366 | 112 | |
paul@376 | 113 | if (!map_flags) |
paul@376 | 114 | map_flags = L4RE_DS_F_R; |
paul@376 | 115 | |
paul@376 | 116 | region->fpage = l4_fpage(r.start + (page_addr - r.map_start), L4_PAGESHIFT, map_flags & r.flags); |
paul@366 | 117 | region->snd_base = page_addr; |
paul@366 | 118 | |
paul@366 | 119 | #if DEBUG |
paul@376 | 120 | printf("%lx...%lx from %lx...%lx offset %lx size %d rights %x ds %lx\n", |
paul@366 | 121 | r.map_start, region->snd_base, |
paul@366 | 122 | r.start, l4_fpage_memaddr(region->fpage), |
paul@366 | 123 | addr - r.map_start, |
paul@366 | 124 | l4_fpage_size(region->fpage), |
paul@376 | 125 | l4_fpage_rights(region->fpage), |
paul@376 | 126 | r.ds); |
paul@366 | 127 | |
paul@366 | 128 | printf("%lx -> ", addr); |
paul@366 | 129 | |
paul@366 | 130 | for (unsigned int i = 0; i < sizeof(l4_umword_t); i++) |
paul@366 | 131 | printf("%02x", *((unsigned char *)(r.start + (addr - r.map_start) + i))); |
paul@366 | 132 | |
paul@366 | 133 | printf("\n"); |
paul@366 | 134 | #endif |
paul@366 | 135 | |
paul@366 | 136 | if (r.flags & L4RE_RM_F_W) |
paul@366 | 137 | l4_touch_rw((const void *) (r.start + (page_addr - r.map_start)), L4_PAGESIZE); |
paul@366 | 138 | else |
paul@366 | 139 | l4_touch_ro((const void *) (r.start + (page_addr - r.map_start)), L4_PAGESIZE); |
paul@366 | 140 | |
paul@366 | 141 | return L4_EOK; |
paul@366 | 142 | } |
paul@366 | 143 | |
paul@366 | 144 | #if DEBUG |
paul@366 | 145 | printf("not mapped!\n"); |
paul@366 | 146 | #endif |
paul@366 | 147 | |
paul@366 | 148 | return -L4_ENOMEM; |
paul@366 | 149 | } |
paul@366 | 150 | |
paul@366 | 151 | /* Attach a region for provision when page faults occur. This is required in |
paul@366 | 152 | the initialisation of a program by the C library which requires a region |
paul@366 | 153 | mapper. */ |
paul@366 | 154 | |
paul@366 | 155 | long ExternalPager::attach(address_t *start, offset_t size, map_flags_t flags, |
paul@366 | 156 | l4_cap_idx_t ds, address_t offset, |
paul@366 | 157 | unsigned char align) |
paul@366 | 158 | { |
paul@366 | 159 | #if DEBUG |
paul@366 | 160 | printf("attach(%lx, %ld, %lx, ..., %lx, %d)\n", *start, size, flags, offset, align); |
paul@366 | 161 | #endif |
paul@366 | 162 | |
paul@366 | 163 | if (align < L4_PAGESHIFT) |
paul@366 | 164 | align = L4_PAGESHIFT; |
paul@366 | 165 | |
paul@366 | 166 | offset_t increment = 1UL << align; |
paul@366 | 167 | offset_t region_size = round(size, increment); |
paul@366 | 168 | |
paul@366 | 169 | /* Either attempt to find an address for the specified region, starting from |
paul@366 | 170 | any indicated address. */ |
paul@366 | 171 | |
paul@366 | 172 | if (flags & L4RE_RM_F_SEARCH_ADDR) |
paul@366 | 173 | { |
paul@366 | 174 | address_t region_start = trunc(*start, increment); |
paul@366 | 175 | MappedRegions::iterator it = _regions.upper_bound(*start); |
paul@366 | 176 | |
paul@366 | 177 | if (!region_start) |
paul@366 | 178 | region_start += increment; |
paul@366 | 179 | |
paul@366 | 180 | #if DEBUG |
paul@366 | 181 | printf("-> search from %lx -> %lx...\n", *start, region_start); |
paul@366 | 182 | #endif |
paul@366 | 183 | |
paul@366 | 184 | /* Before last known region. */ |
paul@366 | 185 | |
paul@366 | 186 | while (it != _regions.end()) |
paul@366 | 187 | { |
paul@366 | 188 | MappedRegions::iterator next = it; |
paul@366 | 189 | MappedRegion &r = it->second; |
paul@366 | 190 | address_t start_limit; |
paul@366 | 191 | address_t end_limit = r.map_start; |
paul@366 | 192 | |
paul@366 | 193 | /* Consider any preceding region. If no such region exists, choose an |
paul@366 | 194 | address at the start of memory. */ |
paul@366 | 195 | |
paul@366 | 196 | if (it == _regions.begin()) |
paul@366 | 197 | start_limit = L4_PAGESIZE; |
paul@366 | 198 | else |
paul@366 | 199 | { |
paul@366 | 200 | it--; |
paul@366 | 201 | MappedRegion &pr = it->second; |
paul@366 | 202 | start_limit = pr.map_start + pr.size; |
paul@366 | 203 | it = next; |
paul@366 | 204 | } |
paul@366 | 205 | |
paul@366 | 206 | /* Test against the limits. */ |
paul@366 | 207 | |
paul@366 | 208 | if (region_start < start_limit) |
paul@366 | 209 | region_start = round(start_limit, increment); |
paul@366 | 210 | |
paul@366 | 211 | /* Investigate subsequent regions if not enough space exists between the |
paul@366 | 212 | preceding region (or start of memory) and the current region. */ |
paul@366 | 213 | |
paul@366 | 214 | if ((region_start + region_size) > end_limit) |
paul@366 | 215 | { |
paul@366 | 216 | it++; |
paul@366 | 217 | if (it == _regions.end()) |
paul@366 | 218 | return -L4_ENOMEM; |
paul@366 | 219 | } |
paul@366 | 220 | else |
paul@366 | 221 | break; |
paul@366 | 222 | } |
paul@366 | 223 | |
paul@366 | 224 | /* Attach the provided dataspace. |
paul@366 | 225 | NOTE: This is only done in this implementation to support the paging |
paul@366 | 226 | mechanism. In a region mapper residing within the actual task, the |
paul@366 | 227 | dataspace's map operation would be invoked to obtain mappings. */ |
paul@366 | 228 | |
paul@366 | 229 | l4_addr_t ds_start; |
paul@366 | 230 | |
paul@366 | 231 | long err = ipc_attach_dataspace(ds, size, (void **) &ds_start); |
paul@366 | 232 | |
paul@366 | 233 | if (err) |
paul@366 | 234 | return err; |
paul@366 | 235 | |
paul@366 | 236 | l4_touch_rw((const void *) ds_start, size); |
paul@366 | 237 | |
paul@366 | 238 | #if DEBUG |
paul@376 | 239 | printf("-> added region at %lx size %ld (%d) from %lx ds %lx\n", region_start, region_size, page_order(region_size), ds_start, ds); |
paul@366 | 240 | #endif |
paul@366 | 241 | |
paul@376 | 242 | MappedRegion r(ds_start, region_size, flags & L4RE_DS_F_RIGHTS_MASK, region_start, ds); |
paul@366 | 243 | |
paul@366 | 244 | add(r); |
paul@366 | 245 | |
paul@366 | 246 | *start = region_start; |
paul@366 | 247 | return L4_EOK; |
paul@366 | 248 | } |
paul@366 | 249 | |
paul@366 | 250 | /* Or attempt to add the specified region at a specific address. */ |
paul@366 | 251 | |
paul@366 | 252 | else |
paul@366 | 253 | { |
paul@366 | 254 | // NOTE: To be implemented. |
paul@366 | 255 | |
paul@366 | 256 | #if DEBUG |
paul@366 | 257 | printf("-> region of size %ld (%d) not added!\n", region_size, page_order(region_size)); |
paul@366 | 258 | #endif |
paul@366 | 259 | |
paul@366 | 260 | return -L4_ENOMEM; |
paul@366 | 261 | } |
paul@366 | 262 | } |
paul@366 | 263 | |
paul@366 | 264 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@366 | 265 | */ |