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 #include "parent_pager_object_server.h" 33 34 35 36 #define DEBUG 0 37 38 39 40 /* A simple system pager also acting as a region mapper. */ 41 42 ExternalPager::ExternalPager(address_t start, address_t end) 43 : ExecPager(start, end), NotificationSupport() 44 { 45 } 46 47 ipc_server_default_config_type ExternalPager::config() 48 { 49 return config_ParentPagerObject; 50 } 51 52 /* Handle a general exception. */ 53 54 long ExternalPager::exception(l4_exc_regs_t regs, l4_snd_fpage_t *region) 55 { 56 (void) region; 57 58 printf("ExternalPager::exception(...) -> pfa = %lx, pc = %lx\n", l4_utcb_exc_pfa(®s), l4_utcb_exc_pc(®s)); 59 60 #if DEBUG 61 while (1) 62 l4_sleep_forever(); 63 #endif 64 65 return L4_EOK; 66 } 67 68 /* Handle a page fault using any configured regions. */ 69 70 long ExternalPager::page_fault(l4_umword_t pfa, l4_umword_t pc, l4_snd_fpage_t *region) 71 { 72 l4_umword_t addr = pfa & ~7UL, flags = pfa & 7; 73 74 #if DEBUG 75 printf("page_fault(%lx, %lx) -> %lx (%lx) -> ", pfa, pc, addr, flags); 76 #endif 77 78 /* Find the first region whose start address is beyond the fault address, 79 testing if any immediately preceding region contains the fault address. */ 80 81 MappedRegions::iterator it = _regions.upper_bound(addr); 82 83 if (it != _regions.begin()) 84 it--; 85 else 86 { 87 printf("not mapped at %lx for pc %lx\n", addr, pc); 88 return -L4_ENOMEM; 89 } 90 91 /* Obtain the region and test if it contains the fault address. */ 92 93 MappedRegion &r = it->second; 94 95 if ((addr >= r.start) && (addr < r.start + r.size)) 96 { 97 l4_addr_t page_addr = trunc(addr, L4_PAGESIZE); 98 map_flags_t map_flags = map_flags_for_fault(flags); 99 100 region->fpage = l4_fpage(r.ds_start + (page_addr - r.start), L4_PAGESHIFT, map_flags & r.flags); 101 region->snd_base = page_addr; 102 103 #if DEBUG 104 printf("%lx...%lx from %lx...%lx offset %lx size %d rights %x ds %lx\n", 105 r.start, region->snd_base, 106 r.ds_start, l4_fpage_memaddr(region->fpage), 107 addr - r.start, 108 l4_fpage_size(region->fpage), 109 l4_fpage_rights(region->fpage), 110 r.ds); 111 112 printf("%lx -> ", addr); 113 114 for (unsigned int i = 0; i < sizeof(l4_umword_t); i++) 115 printf("%02x", *((unsigned char *)(r.ds_start + (addr - r.start) + i))); 116 117 printf("\n"); 118 #endif 119 120 if (r.flags & L4RE_RM_F_W) 121 l4_touch_rw((const void *) (r.ds_start + (page_addr - r.start)), L4_PAGESIZE); 122 else 123 l4_touch_ro((const void *) (r.ds_start + (page_addr - r.start)), L4_PAGESIZE); 124 125 return L4_EOK; 126 } 127 128 #if DEBUG 129 printf("not mapped!\n"); 130 #endif 131 132 return -L4_ENOMEM; 133 } 134 135 /* Attach a region for provision when page faults occur. This is required in 136 the initialisation of a program by the C library which requires a region 137 mapper. */ 138 139 long ExternalPager::attach(address_t *start, address_t size, map_flags_t flags, 140 l4_cap_idx_t ds, address_t offset, 141 unsigned char align) 142 { 143 long err = ExecPager::find(start, &size, flags, offset, align); 144 145 if (!err) 146 { 147 /* Attach the provided dataspace. 148 149 This is only done in this implementation to support the paging 150 mechanism. In a region mapper residing within the actual task, the 151 dataspace's map operation would be invoked to obtain mappings. */ 152 153 l4_addr_t ds_start; 154 err = ipc_attach_dataspace(ds, size, (void **) &ds_start); 155 156 if (err) 157 return err; 158 159 l4_touch_rw((const void *) ds_start, size); 160 161 MappedRegion r(*start, size, flags & L4RE_DS_F_RIGHTS_MASK, ds, ds_start); 162 add(r); 163 } 164 165 return err; 166 } 167 168 /* Receive signals from a task. */ 169 170 long ExternalPager::signal(unsigned long sig, unsigned long val) 171 { 172 notify_all(NOTIFY_TASK_SIGNAL, (notify_values_t) {sig, val}); 173 174 return L4_EOK; 175 } 176 177 /* Subscribe to notifications. */ 178 179 long ExternalPager::subscribe(l4_cap_idx_t notifier, notify_flags_t flags) 180 { 181 return NotificationSupport::subscribe(notifier, flags); 182 } 183 184 /* Unsubscribe from notifications. */ 185 186 long ExternalPager::unsubscribe(l4_cap_idx_t notifier) 187 { 188 return NotificationSupport::unsubscribe(notifier); 189 } 190 191 /* vim: tabstop=2 expandtab shiftwidth=2 192 */