1 /* 2 * A system pager implementation residing in a separate task. 3 * 4 * Copyright (C) 2022, 2023 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <l4/re/env.h> 23 #include <l4/util/util.h> 24 25 #include <ipc/mem_ipc.h> 26 #include <mem/memory_utils.h> 27 #include <systypes/base.h> 28 29 #include <stdio.h> 30 31 #include "external_pager.h" 32 33 34 35 #define DEBUG 0 36 37 38 39 /* A simple system pager also acting as a region mapper. */ 40 41 ExternalPager::ExternalPager(address_t start, address_t end) 42 : ExecPager(start, end), NotificationSupport() 43 { 44 } 45 46 /* Handle a general exception. */ 47 48 long ExternalPager::exception(l4_exc_regs_t regs, l4_snd_fpage_t *region) 49 { 50 (void) region; 51 52 printf("ExternalPager::exception(...) -> pfa = %lx, pc = %lx\n", l4_utcb_exc_pfa(®s), l4_utcb_exc_pc(®s)); 53 54 #if DEBUG 55 while (1) 56 l4_sleep_forever(); 57 #endif 58 59 return L4_EOK; 60 } 61 62 /* Handle a page fault using any configured regions. */ 63 64 long ExternalPager::page_fault(l4_umword_t pfa, l4_umword_t pc, l4_snd_fpage_t *region) 65 { 66 l4_umword_t addr = pfa & ~7UL, flags = pfa & 7; 67 68 #if DEBUG 69 printf("page_fault(%lx, %lx) -> %lx (%lx) -> ", pfa, pc, addr, flags); 70 #endif 71 72 /* Find the first region whose start address is beyond the fault address, 73 testing if any immediately preceding region contains the fault address. */ 74 75 MappedRegions::iterator it = _regions.upper_bound(addr); 76 77 if (it != _regions.begin()) 78 it--; 79 else 80 { 81 printf("not mapped at %lx for pc %lx\n", addr, pc); 82 return -L4_ENOMEM; 83 } 84 85 /* Obtain the region and test if it contains the fault address. */ 86 87 MappedRegion &r = it->second; 88 89 if ((addr >= r.start) && (addr < r.start + r.size)) 90 { 91 l4_addr_t page_addr = trunc(addr, L4_PAGESIZE); 92 map_flags_t map_flags = map_flags_for_fault(flags); 93 94 region->fpage = l4_fpage(r.ds_start + (page_addr - r.start), L4_PAGESHIFT, map_flags & r.flags); 95 region->snd_base = page_addr; 96 97 #if DEBUG 98 printf("%lx...%lx from %lx...%lx offset %lx size %d rights %x ds %lx\n", 99 r.start, region->snd_base, 100 r.ds_start, l4_fpage_memaddr(region->fpage), 101 addr - r.start, 102 l4_fpage_size(region->fpage), 103 l4_fpage_rights(region->fpage), 104 r.ds); 105 106 printf("%lx -> ", addr); 107 108 for (unsigned int i = 0; i < sizeof(l4_umword_t); i++) 109 printf("%02x", *((unsigned char *)(r.ds_start + (addr - r.start) + i))); 110 111 printf("\n"); 112 #endif 113 114 if (r.flags & L4RE_RM_F_W) 115 l4_touch_rw((const void *) (r.ds_start + (page_addr - r.start)), L4_PAGESIZE); 116 else 117 l4_touch_ro((const void *) (r.ds_start + (page_addr - r.start)), L4_PAGESIZE); 118 119 return L4_EOK; 120 } 121 122 #if DEBUG 123 printf("not mapped!\n"); 124 #endif 125 126 return -L4_ENOMEM; 127 } 128 129 /* Attach a region for provision when page faults occur. This is required in 130 the initialisation of a program by the C library which requires a region 131 mapper. */ 132 133 long ExternalPager::attach(address_t *start, address_t size, map_flags_t flags, 134 l4_cap_idx_t ds, address_t offset, 135 unsigned char align) 136 { 137 long err = ExecPager::find(start, &size, flags, offset, align); 138 139 if (!err) 140 { 141 /* Attach the provided dataspace. 142 143 This is only done in this implementation to support the paging 144 mechanism. In a region mapper residing within the actual task, the 145 dataspace's map operation would be invoked to obtain mappings. */ 146 147 l4_addr_t ds_start; 148 err = ipc_attach_dataspace(ds, size, (void **) &ds_start); 149 150 if (err) 151 return err; 152 153 l4_touch_rw((const void *) ds_start, size); 154 155 MappedRegion r(*start, size, flags & L4RE_DS_F_RIGHTS_MASK, ds, ds_start); 156 add(r); 157 } 158 159 return err; 160 } 161 162 /* Receive signals from a task. */ 163 164 long ExternalPager::signal(unsigned long sig, unsigned long val) 165 { 166 notify_all(NOTIFY_TASK_SIGNAL, (notify_values_t) {sig, val}); 167 168 return L4_EOK; 169 } 170 171 /* Subscribe to notifications. */ 172 173 long ExternalPager::subscribe(l4_cap_idx_t notifier, notify_flags_t flags) 174 { 175 _notifier = notifier; 176 return NotificationSupport::subscribe(notifier, flags); 177 } 178 179 /* Unsubscribe from notifications. */ 180 181 long ExternalPager::unsubscribe() 182 { 183 NotificationSupport::unsubscribe(_notifier); 184 return L4_EOK; 185 } 186 187 /* vim: tabstop=2 expandtab shiftwidth=2 188 */