paul@366 | 1 | /* |
paul@366 | 2 | * A system pager implementation residing in the same task as a program. |
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 "dataspace_client.h" |
paul@366 | 33 | #include "internal_pager.h" |
paul@366 | 34 | |
paul@366 | 35 | |
paul@366 | 36 | |
paul@366 | 37 | /* A simple system pager also acting as a region mapper. */ |
paul@366 | 38 | |
paul@366 | 39 | /* Add a region to the pager. */ |
paul@366 | 40 | |
paul@366 | 41 | void InternalPager::add(ExecRegion ®ion) |
paul@366 | 42 | { |
paul@366 | 43 | _regions[region.start] = region; |
paul@366 | 44 | } |
paul@366 | 45 | |
paul@366 | 46 | /* Handle a general exception. */ |
paul@366 | 47 | |
paul@366 | 48 | long InternalPager::exception(l4_exc_regs_t regs, l4_snd_fpage_t *region) |
paul@366 | 49 | { |
paul@366 | 50 | (void) region; |
paul@366 | 51 | |
paul@366 | 52 | printf("exception(...) -> pfa = %lx, pc = %lx\n", l4_utcb_exc_pfa(®s), l4_utcb_exc_pc(®s)); |
paul@366 | 53 | |
paul@366 | 54 | printf("r15 = %lx\n", regs.r15); |
paul@366 | 55 | printf("r14 = %lx\n", regs.r14); |
paul@366 | 56 | printf("r13 = %lx\n", regs.r13); |
paul@366 | 57 | printf("r12 = %lx\n", regs.r12); |
paul@366 | 58 | printf("r11 = %lx\n", regs.r11); |
paul@366 | 59 | printf("r10 = %lx\n", regs.r10); |
paul@366 | 60 | printf("r9 = %lx\n", regs.r9); |
paul@366 | 61 | printf("r8 = %lx\n", regs.r8); |
paul@366 | 62 | printf("rdi = %lx\n", regs.rdi); |
paul@366 | 63 | printf("rsi = %lx\n", regs.rsi); |
paul@366 | 64 | printf("rbp = %lx\n", regs.rbp); |
paul@366 | 65 | printf("pfa = %lx\n", regs.pfa); |
paul@366 | 66 | printf("rbx = %lx\n", regs.rbx); |
paul@366 | 67 | printf("rdx = %lx\n", regs.rdx); |
paul@366 | 68 | printf("rcx = %lx\n", regs.rcx); |
paul@366 | 69 | printf("rax = %lx\n", regs.rax); |
paul@366 | 70 | printf("trapno = %lx\n", regs.trapno); |
paul@366 | 71 | printf("err = %lx\n", regs.err); |
paul@366 | 72 | printf("ip = %lx\n", regs.ip); |
paul@366 | 73 | printf("flags = %lx\n", regs.flags); |
paul@366 | 74 | printf("sp = %lx\n", regs.sp); |
paul@366 | 75 | printf("ss = %lx\n", regs.ss); |
paul@366 | 76 | printf("fs_base = %lx\n", regs.fs_base); |
paul@366 | 77 | printf("gs_base = %lx\n", regs.gs_base); |
paul@366 | 78 | |
paul@366 | 79 | return L4_EOK; |
paul@366 | 80 | } |
paul@366 | 81 | |
paul@366 | 82 | #define DEBUG 0 |
paul@366 | 83 | |
paul@366 | 84 | /* Handle a page fault using any configured regions. */ |
paul@366 | 85 | |
paul@366 | 86 | long InternalPager::page_fault(l4_umword_t pfa, l4_umword_t pc, l4_snd_fpage_t *region) |
paul@366 | 87 | { |
paul@366 | 88 | l4_umword_t addr = pfa & ~7UL, flags = pfa & 7; |
paul@366 | 89 | |
paul@366 | 90 | #if DEBUG |
paul@366 | 91 | printf("page_fault(%lx, %lx) -> %lx (%lx) -> ", pfa, pc, addr, flags); |
paul@366 | 92 | #endif |
paul@366 | 93 | |
paul@366 | 94 | ExecRegions::iterator it = _regions.upper_bound(addr); |
paul@366 | 95 | |
paul@366 | 96 | if (it != _regions.begin()) |
paul@366 | 97 | it--; |
paul@366 | 98 | else |
paul@366 | 99 | { |
paul@366 | 100 | printf("not mapped!\n"); |
paul@366 | 101 | return -L4_ENOMEM; |
paul@366 | 102 | } |
paul@366 | 103 | |
paul@366 | 104 | ExecRegion &r = it->second; |
paul@366 | 105 | |
paul@366 | 106 | if ((addr >= r.start) && (addr < r.start + r.size)) |
paul@366 | 107 | { |
paul@366 | 108 | l4_addr_t page_addr = trunc(addr, L4_PAGESIZE); |
paul@366 | 109 | |
paul@366 | 110 | /* Interact with the region's dataspace, specifying a receive window for a |
paul@366 | 111 | map operation. Here, a single page is specified. */ |
paul@366 | 112 | |
paul@366 | 113 | client_Dataspace dataspace(r.ds); |
paul@366 | 114 | l4_snd_fpage_t region = {0, l4_fpage(page_addr, L4_PAGESHIFT, 0)}; |
paul@366 | 115 | |
paul@366 | 116 | #if DEBUG |
paul@366 | 117 | printf("region = {%lx, {%lx, %d}}\n", region.snd_base, l4_fpage_memaddr(region.fpage), l4_fpage_size(region.fpage)); |
paul@366 | 118 | #endif |
paul@366 | 119 | |
paul@366 | 120 | return dataspace.map(0, 0, L4_FPAGE_RO, ®ion); |
paul@366 | 121 | } |
paul@366 | 122 | |
paul@366 | 123 | #if DEBUG |
paul@366 | 124 | printf("not mapped!\n"); |
paul@366 | 125 | #endif |
paul@366 | 126 | |
paul@366 | 127 | return -L4_ENOMEM; |
paul@366 | 128 | } |
paul@366 | 129 | |
paul@366 | 130 | /* Attach a region for provision when page faults occur. This is required in |
paul@366 | 131 | the initialisation of a program by the C library which requires a region |
paul@366 | 132 | mapper. */ |
paul@366 | 133 | |
paul@366 | 134 | long InternalPager::attach(address_t *start, offset_t size, map_flags_t flags, |
paul@366 | 135 | l4_cap_idx_t ds, address_t offset, |
paul@366 | 136 | unsigned char align) |
paul@366 | 137 | { |
paul@366 | 138 | #if DEBUG |
paul@366 | 139 | printf("attach(%lx, %ld, %lx, ..., %lx, %d)\n", *start, size, flags, offset, align); |
paul@366 | 140 | #endif |
paul@366 | 141 | |
paul@366 | 142 | if (align < L4_PAGESHIFT) |
paul@366 | 143 | align = L4_PAGESHIFT; |
paul@366 | 144 | |
paul@366 | 145 | offset_t increment = 1UL << align; |
paul@366 | 146 | offset_t region_size = round(size, increment); |
paul@366 | 147 | |
paul@366 | 148 | /* Either attempt to find an address for the specified region, starting from |
paul@366 | 149 | any indicated address. */ |
paul@366 | 150 | |
paul@366 | 151 | if (flags & L4RE_RM_F_SEARCH_ADDR) |
paul@366 | 152 | { |
paul@366 | 153 | address_t region_start = trunc(*start, increment); |
paul@366 | 154 | ExecRegions::iterator it = _regions.upper_bound(*start); |
paul@366 | 155 | |
paul@366 | 156 | if (!region_start) |
paul@366 | 157 | region_start += increment; |
paul@366 | 158 | |
paul@366 | 159 | #if DEBUG |
paul@366 | 160 | printf("-> search from %lx -> %lx...\n", *start, region_start); |
paul@366 | 161 | #endif |
paul@366 | 162 | |
paul@366 | 163 | /* Before last known region. */ |
paul@366 | 164 | |
paul@366 | 165 | while (it != _regions.end()) |
paul@366 | 166 | { |
paul@366 | 167 | ExecRegions::iterator next = it; |
paul@366 | 168 | ExecRegion &r = it->second; |
paul@366 | 169 | address_t start_limit; |
paul@366 | 170 | address_t end_limit = r.start; |
paul@366 | 171 | |
paul@366 | 172 | /* Consider any preceding region. If no such region exists, choose an |
paul@366 | 173 | address at the start of memory. */ |
paul@366 | 174 | |
paul@366 | 175 | if (it == _regions.begin()) |
paul@366 | 176 | start_limit = L4_PAGESIZE; |
paul@366 | 177 | else |
paul@366 | 178 | { |
paul@366 | 179 | it--; |
paul@366 | 180 | ExecRegion &pr = it->second; |
paul@366 | 181 | start_limit = pr.start + pr.size; |
paul@366 | 182 | it = next; |
paul@366 | 183 | } |
paul@366 | 184 | |
paul@366 | 185 | /* Test against the limits. */ |
paul@366 | 186 | |
paul@366 | 187 | if (region_start < start_limit) |
paul@366 | 188 | region_start = round(start_limit, increment); |
paul@366 | 189 | |
paul@366 | 190 | /* Investigate subsequent regions if not enough space exists between the |
paul@366 | 191 | preceding region (or start of memory) and the current region. */ |
paul@366 | 192 | |
paul@366 | 193 | if ((region_start + region_size) > end_limit) |
paul@366 | 194 | { |
paul@366 | 195 | it++; |
paul@366 | 196 | if (it == _regions.end()) |
paul@366 | 197 | return -L4_ENOMEM; |
paul@366 | 198 | } |
paul@366 | 199 | else |
paul@366 | 200 | break; |
paul@366 | 201 | } |
paul@366 | 202 | |
paul@366 | 203 | #if DEBUG |
paul@366 | 204 | printf("-> added region for %lx size %ld (%d)\n", region_start, region_size, page_order(region_size)); |
paul@366 | 205 | #endif |
paul@366 | 206 | |
paul@366 | 207 | ExecRegion r = (ExecRegion) {region_start, region_size, flags & L4RE_DS_F_RIGHTS_MASK, ds}; |
paul@366 | 208 | |
paul@366 | 209 | add(r); |
paul@366 | 210 | |
paul@366 | 211 | *start = region_start; |
paul@366 | 212 | return L4_EOK; |
paul@366 | 213 | } |
paul@366 | 214 | |
paul@366 | 215 | /* Or attempt to add the specified region at a specific address. */ |
paul@366 | 216 | |
paul@366 | 217 | else |
paul@366 | 218 | { |
paul@366 | 219 | // NOTE: To be implemented. |
paul@366 | 220 | |
paul@366 | 221 | #if DEBUG |
paul@366 | 222 | printf("-> region of size %ld (%d) not added!\n", region_size, page_order(region_size)); |
paul@366 | 223 | #endif |
paul@366 | 224 | |
paul@366 | 225 | return -L4_ENOMEM; |
paul@366 | 226 | } |
paul@366 | 227 | } |
paul@366 | 228 | |
paul@366 | 229 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@366 | 230 | */ |